Search in sources :

Example 11 with JdbcFieldDefinition

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

the class CollectionValue method appendParameters.

@Override
public int appendParameters(int index, final PreparedStatement statement) {
    for (final QueryValue queryValue : getQueryValues()) {
        JdbcFieldDefinition jdbcField = this.jdbcField;
        if (queryValue instanceof Value) {
            final Value valueWrapper = (Value) queryValue;
            final Object value = valueWrapper.getQueryValue();
            if (jdbcField == null) {
                jdbcField = JdbcFieldDefinition.newFieldDefinition(value);
            }
            try {
                index = jdbcField.setPreparedStatementValue(statement, index, value);
            } catch (final SQLException e) {
                ExceptionUtil.throwIfUnchecked(e);
            }
        } else {
            index = queryValue.appendParameters(index, statement);
        }
    }
    return index;
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) SQLException(java.sql.SQLException)

Example 12 with JdbcFieldDefinition

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

the class JdbcRecordWriter method getInsertSql.

private String getInsertSql(final JdbcRecordDefinition recordDefinition, final boolean generatePrimaryKey, final Map<JdbcRecordDefinition, String> sqlMap) {
    String sql = sqlMap.get(recordDefinition);
    if (sql == null) {
        final JdbcRecordStore recordStore = this.recordStore;
        final String tableName = recordDefinition.getDbTableQualifiedName();
        final boolean hasRowIdField = recordStore.isIdFieldRowid(recordDefinition);
        final StringBuilder sqlBuffer = new StringBuilder();
        if (this.sqlPrefix != null) {
            sqlBuffer.append(this.sqlPrefix);
        }
        sqlBuffer.append("insert ");
        sqlBuffer.append(" into ");
        sqlBuffer.append(tableName);
        sqlBuffer.append(" (");
        boolean first = true;
        for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
            if (!(hasRowIdField && fieldDefinition.isIdField())) {
                if (first) {
                    first = false;
                } else {
                    sqlBuffer.append(',');
                }
                fieldDefinition.appendColumnName(sqlBuffer, this.quoteColumnNames);
            }
        }
        sqlBuffer.append(") VALUES (");
        first = true;
        for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
            final boolean idField = fieldDefinition.isIdField();
            if (!(hasRowIdField && idField)) {
                if (first) {
                    first = false;
                } else {
                    sqlBuffer.append(',');
                }
                if (idField && generatePrimaryKey) {
                    final String primaryKeySql = recordStore.getGeneratePrimaryKeySql(recordDefinition);
                    sqlBuffer.append(primaryKeySql);
                } else {
                    ((JdbcFieldDefinition) fieldDefinition).addInsertStatementPlaceHolder(sqlBuffer, generatePrimaryKey);
                }
            }
        }
        sqlBuffer.append(")");
        if (this.sqlSuffix != null) {
            sqlBuffer.append(this.sqlSuffix);
        }
        sql = sqlBuffer.toString();
        sqlMap.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 13 with JdbcFieldDefinition

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

the class JdbcRecordWriter method update.

private void update(final JdbcRecordDefinition recordDefinition, final Record record) throws SQLException {
    flushIfRequired(recordDefinition);
    PreparedStatement statement = this.typeUpdateStatementMap.get(recordDefinition);
    if (statement == null) {
        final String sql = getUpdateSql(recordDefinition);
        try {
            statement = this.connection.prepareStatement(sql);
            this.typeUpdateStatementMap.put(recordDefinition, statement);
        } catch (final SQLException e) {
            LOG.error(sql, e);
        }
    }
    int parameterIndex = 1;
    for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
        if (!fieldDefinition.isIdField()) {
            final JdbcFieldDefinition jdbcFieldDefinition = (JdbcFieldDefinition) fieldDefinition;
            parameterIndex = jdbcFieldDefinition.setInsertPreparedStatementValue(statement, parameterIndex, record);
        }
    }
    parameterIndex = setIdEqualsValues(statement, parameterIndex, recordDefinition, record);
    statement.addBatch();
    final Integer batchCount = Maps.addCount(this.typeUpdateBatchCountMap, recordDefinition);
    if (batchCount >= this.batchSize) {
        processCurrentBatch(recordDefinition, this.typeUpdateSqlMap, statement, this.typeUpdateBatchCountMap, this.typeUpdateRecords, false);
    }
    this.recordStore.addStatistic("Update", record);
}
Also used : SQLException(java.sql.SQLException) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) PreparedStatement(java.sql.PreparedStatement)

Example 14 with JdbcFieldDefinition

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

the class JdbcQueryIterator method getNextRecord.

public static Record getNextRecord(final JdbcRecordStore recordStore, final RecordDefinition recordDefinition, final List<FieldDefinition> fields, final RecordFactory<Record> recordFactory, final ResultSet resultSet, final boolean internStrings) {
    final Record record = recordFactory.newRecord(recordDefinition);
    if (record != null) {
        record.setState(RecordState.INITIALIZING);
        int columnIndex = 1;
        for (final FieldDefinition field : fields) {
            final JdbcFieldDefinition jdbcField = (JdbcFieldDefinition) field;
            try {
                columnIndex = jdbcField.setFieldValueFromResultSet(resultSet, columnIndex, record, internStrings);
            } catch (final SQLException e) {
                throw new RuntimeException("Unable to get value " + (columnIndex + 1) + " from result set", e);
            }
        }
        record.setState(RecordState.PERSISTED);
        recordStore.addStatistic("query", record);
    }
    return record;
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) SQLException(java.sql.SQLException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) Record(com.revolsys.record.Record)

Example 15 with JdbcFieldDefinition

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

the class AbstractJdbcRecordStore method getField.

public JdbcFieldDefinition getField(final String schemaName, final String tableName, final String columnName) {
    final String typePath = PathUtil.toPath(schemaName, tableName);
    final RecordDefinition recordDefinition = getRecordDefinition(typePath);
    if (recordDefinition == null) {
        return null;
    } else {
        final FieldDefinition attribute = recordDefinition.getField(columnName);
        return (JdbcFieldDefinition) attribute;
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

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