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();
|
}
|
}
|
}
|
|
}
|