Kenmegne
2025-12-10 e9d80d486b912144b59ebd5939d4837105b37b99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.queryadhoc.queries;
 
import com.megatim.queryadhoc.contrat.DataColumn;
import com.megatim.queryadhoc.contrat.DataRow;
import com.megatim.queryadhoc.model.QueryParam;
import com.megatim.queryadhoc.contrat.DefaultPreparedStatementProcessor;
import com.megatim.queryadhoc.contrat.PreparedStatementProcessor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
/**
 *
 * @author ASUS
 */
public class CommonInsertQuery {
 
    private final String tableName;
    protected final Connection connection;
    private final List<String> columnNames;
    private final List<DataRow> rows;
    private final int BATCH_SIZE = 1000;
 
    public CommonInsertQuery(String tableName, final Connection connection, List<String> columnNames, List<DataRow> rows) {
        this.tableName = tableName;
        this.connection = connection;
        this.columnNames = columnNames;
        this.rows = rows;
    }
 
    public void execute() throws Exception {
 
        try ( PreparedStatement statement = connection.prepareStatement(insertQueryString());) {
 
            executeInBatch(statement, rows);
            statement.executeBatch();
        }
    }
 
    private String insertQueryString() {
 
        StringBuilder query = new StringBuilder("INSERT INTO " + tableName + "(");
 
        columnNames.forEach(column -> query.append(column).append(","));
 
        query.replace(query.length() - 1, query.length(), ") VALUES(");
 
        columnNames.forEach(s -> query.append("?,"));
 
        query.replace(query.length() - 1, query.length(), ")");
 
        return query.toString();
    }
 
    private void executeInBatch(PreparedStatement preparedStatement, List<DataRow> rows) throws SQLException, Exception {
 
        int index = 0;
        int count = 0;
 
        for (DataRow row : rows) {
 
            List<QueryParam> queryParameters = new ArrayList<>();
 
            for (DataColumn dataColumn : row.columns()) {
                queryParameters.add(new QueryParam(dataColumn.column(), dataColumn.value()));
            }
 
            PreparedStatementProcessor statementProcessor = new DefaultPreparedStatementProcessor(index);
            statementProcessor.process(preparedStatement, queryParameters);
            index = statementProcessor.lastProcessedIndex();
 
            preparedStatement.addBatch();
 
            if (++count % BATCH_SIZE == 0) {
                preparedStatement.executeBatch();
            }
 
        }
 
    }
}