Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
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
package com.megatim.fdxconsultation.dao.impl.dataproduction.metadata;
 
import com.megatim.fdxcommons.model.dataproduction.DataProductionType;
import com.megatim.fdxcommons.model.dataproduction.metadata.ProductionMetaData;
import com.megatim.fdxcommons.tools.database.contrat.QueryMetaData;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.NamingException;
 
/**
 *
 * @author Gabuntu
 */
public class CreateProductionMetaDataQuery implements QueryMetaData<Void> {
 
    private final ProductionMetaData pmd;
    private final DataProductionType dataProductionType;
    private final Connection connection;
    private final int BATCH_SIZE = 100_000;
 
    public CreateProductionMetaDataQuery(ProductionMetaData pmd, DataProductionType dataProductionType, Connection connection) {
        this.pmd = pmd;
        this.dataProductionType = dataProductionType;
        this.connection = connection;
    }
 
    @Override
    public Void execute() throws NamingException, SQLException {
 
        if (dataProductionType.equals(DataProductionType.ADD)) {
            executeAddDataType();
 
        } else if (dataProductionType.equals(DataProductionType.UPDATE)) {
            executeUpdateDataType();
        }
        return null;
    }
 
    private void executeAddDataType() throws SQLException {
        System.out.println("--------------------- pmd.getNbreElts() = " + pmd.getNbreElts());
        try ( PreparedStatement statement = connection.prepareStatement(productionMetaDataQueryString())) {
 
            statement.setLong(1, pmd.getProductionId());
            statement.setLong(2, pmd.getStartIndex());
            statement.setLong(3, pmd.getNbreElts());
            statement.setString(4, pmd.getDataProductionType());
 
            statement.executeUpdate();
 
        }
    }
 
    private void executeUpdateDataType() throws SQLException {
        try ( PreparedStatement metaDataStatement = connection.prepareStatement(productionMetaDataQueryString());  PreparedStatement indexesStatement = connection.prepareStatement(productionMetaDataIndexesQuery())) {
            metaDataStatement.executeUpdate();
            executeInBatch(indexesStatement);
            indexesStatement.executeBatch();
        }
    }
 
    private String productionMetaDataQueryString() {
        return "INSERT INTO ProductionMetaData(productionId, startIndex, nbreElts, dataProductionType)"
                + " VALUES(?, ?, ?, ?)";
    }
 
    private String productionMetaDataIndexesQuery() {
        return "INSERT INTO productionMetaData_indexes(productionId, indexes) VALUES(?, ?)";
    }
 
    private void executeInBatch(PreparedStatement preparedStatement) throws SQLException {
        int count = 0;
 
        for (Long l : pmd.getIndexes()) {
            preparedStatement.setLong(1, pmd.getProductionId());
            preparedStatement.setLong(2, l);
            preparedStatement.addBatch();
 
            if (++count % BATCH_SIZE == 0) {
                preparedStatement.executeBatch();
            }
        }
    }
 
}