Search in sources :

Example 1 with JdbcFieldDefinition

use of com.revolsys.jdbc.field.JdbcFieldDefinition in project com.revolsys.open by revolsys.

the class JdbcUtils method addSelectColumnName.

public static void addSelectColumnName(final StringBuilder sql, final String tablePrefix, final FieldDefinition fieldDefinition) {
    if (fieldDefinition instanceof JdbcFieldDefinition) {
        final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) fieldDefinition;
        jdbcFieldDefinition.appendSelectColumnName(sql, tablePrefix);
    } else {
        sql.append(fieldDefinition.getName());
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Example 2 with JdbcFieldDefinition

use of com.revolsys.jdbc.field.JdbcFieldDefinition in project com.revolsys.open by revolsys.

the class JdbcUtils method setPreparedStatementParameters.

public static void setPreparedStatementParameters(final PreparedStatement statement, final Query query) {
    int index = 1;
    for (final Object parameter : query.getParameters()) {
        final JdbcFieldDefinition attribute = JdbcFieldDefinition.newFieldDefinition(parameter);
        try {
            index = attribute.setPreparedStatementValue(statement, index, parameter);
        } catch (final SQLException e) {
            throw new RuntimeException("Error setting value:" + parameter, e);
        }
    }
    final Condition where = query.getWhereCondition();
    if (!where.isEmpty()) {
        where.appendParameters(index, statement);
    }
}
Also used : Condition(com.revolsys.record.query.Condition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) SQLException(java.sql.SQLException)

Example 3 with JdbcFieldDefinition

use of com.revolsys.jdbc.field.JdbcFieldDefinition in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method getUpdateSql.

private String getUpdateSql(final JdbcRecordDefinition recordDefinition) {
    final List<FieldDefinition> idFields = recordDefinition.getIdFields();
    if (idFields.isEmpty()) {
        throw new RuntimeException("No primary key found for: " + recordDefinition);
    } else {
        final String tableName = recordDefinition.getDbTableQualifiedName();
        String sql = this.typeUpdateSqlMap.get(recordDefinition);
        if (sql == null) {
            final StringBuilder sqlBuffer = new StringBuilder();
            if (this.sqlPrefix != null) {
                sqlBuffer.append(this.sqlPrefix);
            }
            sqlBuffer.append("update ");
            sqlBuffer.append(tableName);
            sqlBuffer.append(" set ");
            boolean first = true;
            for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
                if (!idFields.contains(fieldDefinition)) {
                    final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) fieldDefinition;
                    if (first) {
                        first = false;
                    } else {
                        sqlBuffer.append(", ");
                    }
                    jdbcFieldDefinition.appendColumnName(sqlBuffer, this.quoteColumnNames);
                    sqlBuffer.append(" = ");
                    jdbcFieldDefinition.addInsertStatementPlaceHolder(sqlBuffer, false);
                }
            }
            sqlBuffer.append(" where ");
            appendIdEquals(sqlBuffer, idFields);
            sqlBuffer.append(" ");
            if (this.sqlSuffix != null) {
                sqlBuffer.append(this.sqlSuffix);
            }
            sql = sqlBuffer.toString();
            this.typeUpdateSqlMap.put(recordDefinition, sql);
        }
        return sql;
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Example 4 with JdbcFieldDefinition

use of com.revolsys.jdbc.field.JdbcFieldDefinition in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method setIdEqualsValues.

private int setIdEqualsValues(final PreparedStatement statement, int parameterIndex, final JdbcRecordDefinition recordDefinition, final Record record) throws SQLException {
    for (final FieldDefinition idField : recordDefinition.getIdFields()) {
        final Object value = record.getValue(idField);
        parameterIndex = ((JdbcFieldDefinition) idField).setPreparedStatementValue(statement, parameterIndex++, value);
    }
    return parameterIndex;
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Example 5 with JdbcFieldDefinition

use of com.revolsys.jdbc.field.JdbcFieldDefinition in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method appendIdEquals.

public void appendIdEquals(final StringBuilder sqlBuffer, final List<FieldDefinition> idFields) {
    boolean first = true;
    for (final FieldDefinition idField : idFields) {
        if (first) {
            first = false;
        } else {
            sqlBuffer.append(" AND ");
        }
        idField.appendColumnName(sqlBuffer, this.quoteColumnNames);
        sqlBuffer.append(" = ");
        ((JdbcFieldDefinition) idField).addStatementPlaceHolder(sqlBuffer);
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

Aggregations

JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)18 FieldDefinition (com.revolsys.record.schema.FieldDefinition)11 SQLException (java.sql.SQLException)6 PostgreSQLArrayFieldDefinition (com.revolsys.gis.postgresql.type.PostgreSQLArrayFieldDefinition)2 Record (com.revolsys.record.Record)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 PreparedStatement (java.sql.PreparedStatement)2 CollectionDataType (com.revolsys.datatype.CollectionDataType)1 DataType (com.revolsys.datatype.DataType)1 PostgreSQLJdbcBlobFieldDefinition (com.revolsys.gis.postgresql.type.PostgreSQLJdbcBlobFieldDefinition)1 PostgreSQLOidFieldDefinition (com.revolsys.gis.postgresql.type.PostgreSQLOidFieldDefinition)1 Identifier (com.revolsys.identifier.Identifier)1 TypedIdentifier (com.revolsys.identifier.TypedIdentifier)1 JdbcFieldAdder (com.revolsys.jdbc.field.JdbcFieldAdder)1 CodeTable (com.revolsys.record.code.CodeTable)1 CodeTableProperty (com.revolsys.record.code.CodeTableProperty)1 Condition (com.revolsys.record.query.Condition)1 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)1 ResultSet (java.sql.ResultSet)1