Search in sources :

Example 1 with StatementField

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();
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(org.nextprot.commons.statements.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) StatementField(org.nextprot.commons.statements.StatementField)

Example 2 with StatementField

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();
}
Also used : StatementBuilder(org.nextprot.commons.statements.StatementBuilder) StatementField(org.nextprot.commons.statements.StatementField)

Aggregations

StatementField (org.nextprot.commons.statements.StatementField)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 Statement (org.nextprot.commons.statements.Statement)1 StatementBuilder (org.nextprot.commons.statements.StatementBuilder)1