use of org.nextprot.commons.statements.StatementField in project nextprot-api by calipho-sib.
the class JDBCStatementLoaderServiceImpl method load.
private void load(Set<Statement> statements, String tableName, NextProtSource source) throws SQLException {
java.sql.Statement deleteStatement = null;
PreparedStatement pstmt = null;
try (Connection conn = dataSourceServiceLocator.getStatementsDataSource().getConnection()) {
deleteStatement = conn.createStatement();
deleteStatement.addBatch("DELETE FROM nxflat." + tableName + " WHERE SOURCE = '" + source.getSourceName() + "'");
String columnNames = StringUtils.mkString(StatementField.values(), "", ",", "");
List<String> bindVariablesList = new ArrayList<>();
for (int i = 0; i < StatementField.values().length; i++) {
bindVariablesList.add("?");
}
String bindVariables = StringUtils.mkString(bindVariablesList, "", ",", "");
pstmt = conn.prepareStatement("INSERT INTO nxflat." + tableName + " (" + columnNames + ") VALUES ( " + bindVariables + ")");
for (Statement s : statements) {
for (int i = 0; i < StatementField.values().length; i++) {
StatementField sf = StatementField.values()[i];
String value = null;
if (StatementField.SOURCE.equals(sf)) {
value = source.getSourceName();
} else
value = s.getValue(sf);
if (value != null) {
pstmt.setString(i + 1, value.replace("'", "''"));
} else {
pstmt.setNull(i + 1, java.sql.Types.VARCHAR);
}
}
pstmt.addBatch();
}
deleteStatement.executeBatch();
pstmt.executeBatch();
} finally {
if (deleteStatement != null) {
deleteStatement.close();
}
if (pstmt != null) {
pstmt.close();
}
}
}
use of org.nextprot.commons.statements.StatementField in project nextprot-api by calipho-sib.
the class StatementMapper method mapRow.
public Statement mapRow(ResultSet rs, int rowNum) throws SQLException {
StatementBuilder sfbuilder = StatementBuilder.createNew();
for (StatementField key : StatementField.values()) {
String value = rs.getString(key.name());
sfbuilder.addField(key, value);
}
return sfbuilder.build();
}
Aggregations