Search in sources :

Example 6 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition 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)

Example 7 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method insert.

private void insert(final Record record, final JdbcRecordDefinition recordDefinition) throws SQLException {
    final PreparedStatement statement = insertStatementGet(recordDefinition, this.typeInsertStatementMap, this.typeInsertSqlMap, false, false);
    if (statement != null) {
        int parameterIndex = 1;
        for (final FieldDefinition fieldDefinition : recordDefinition.getFields()) {
            parameterIndex = ((JdbcFieldDefinition) fieldDefinition).setInsertPreparedStatementValue(statement, parameterIndex, record);
        }
        insertStatementAddBatch(recordDefinition, record, statement, this.typeInsertSqlMap, this.typeInsertBatchCountMap, this.typeInsertRecords, false);
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) PreparedStatement(java.sql.PreparedStatement)

Example 8 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class JdbcRecordWriter method processCurrentBatch.

private void processCurrentBatch(final JdbcRecordDefinition recordDefinition, final Map<JdbcRecordDefinition, String> sqlMap, final PreparedStatement statement, final Map<JdbcRecordDefinition, Integer> batchCountMap, final Map<JdbcRecordDefinition, List<Record>> recordsByType, final boolean hasGeneratedKeys) {
    Integer batchCount = batchCountMap.get(recordDefinition);
    if (batchCount == null) {
        batchCount = 0;
    }
    try {
        Integer typeCount = this.typeCountMap.get(recordDefinition);
        if (typeCount == null) {
            typeCount = batchCount;
        } else {
            typeCount += batchCount;
        }
        this.typeCountMap.put(recordDefinition, typeCount);
        statement.executeBatch();
        if (hasGeneratedKeys) {
            final List<Record> records = recordsByType.remove(recordDefinition);
            if (records != null) {
                final ResultSet generatedKeyResultSet = statement.getGeneratedKeys();
                int recordIndex = 0;
                while (generatedKeyResultSet.next()) {
                    final Record record = records.get(recordIndex++);
                    int columnIndex = 1;
                    for (final FieldDefinition idField : recordDefinition.getIdFields()) {
                        ((JdbcFieldDefinition) idField).setFieldValueFromResultSet(generatedKeyResultSet, columnIndex++, record, false);
                    }
                }
            }
        }
    } catch (final SQLException e) {
        final String sql = sqlMap.get(recordDefinition);
        throw this.connection.getException("Process Batch", sql, e);
    } catch (final RuntimeException e) {
        LOG.error(sqlMap, e);
        throw e;
    } finally {
        batchCountMap.put(recordDefinition, 0);
    }
}
Also used : JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) SQLException(java.sql.SQLException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) ResultSet(java.sql.ResultSet) Record(com.revolsys.record.Record)

Example 9 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class CsvRecordWriter method write.

@Override
public void write(final Record record) {
    Writer out = this.out;
    if (this.paused) {
        this.paused = false;
        final Resource resource = getResource();
        out = this.out = resource.newWriterAppend();
    }
    if (out != null) {
        try {
            final RecordDefinition recordDefinition = this.recordDefinition;
            final char fieldSeparator = this.fieldSeparator;
            boolean first = true;
            for (final FieldDefinition field : recordDefinition.getFields()) {
                if (first) {
                    first = false;
                } else {
                    out.write(fieldSeparator);
                }
                final String fieldName = field.getName();
                final Object value;
                if (isWriteCodeValues()) {
                    value = record.getCodeValue(fieldName);
                } else {
                    value = record.getValue(fieldName);
                }
                if (value instanceof Geometry) {
                    final Geometry geometry = (Geometry) value;
                    if (this.useQuotes) {
                        out.write('"');
                        EWktWriter.write(out, geometry, this.ewkt);
                        out.write('"');
                    } else {
                        EWktWriter.write(out, geometry, this.ewkt);
                    }
                } else if (value != null) {
                    final DataType dataType = field.getDataType();
                    final String stringValue = dataType.toString(value);
                    if (dataType.isRequiresQuotes()) {
                        string(stringValue);
                    } else {
                        out.write(stringValue, 0, stringValue.length());
                    }
                }
            }
            out.write('\n');
        } catch (final IOException e) {
            throw Exceptions.wrap(e);
        }
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Resource(com.revolsys.spring.resource.Resource) DataType(com.revolsys.datatype.DataType) IOException(java.io.IOException) AbstractRecordWriter(com.revolsys.io.AbstractRecordWriter) EWktWriter(com.revolsys.record.io.format.wkt.EWktWriter) Writer(java.io.Writer) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 10 with FieldDefinition

use of com.revolsys.record.schema.FieldDefinition in project com.revolsys.open by revolsys.

the class EsriGeodatabaseXmlRecordWriter method writeFields.

private void writeFields(final RecordDefinition recordDefinition) {
    this.out.startTag(FIELDS);
    this.out.attribute(XsiConstants.TYPE, FIELDS_TYPE);
    this.out.startTag(FIELD_ARRAY);
    this.out.attribute(XsiConstants.TYPE, FIELD_ARRAY_TYPE);
    for (final FieldDefinition attribute : recordDefinition.getFields()) {
        writeField(attribute);
    }
    if (recordDefinition.getField("OBJECTID") == null) {
        writeOidField();
    }
    this.out.endTag(FIELD_ARRAY);
    this.out.endTag(FIELDS);
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition)

Aggregations

FieldDefinition (com.revolsys.record.schema.FieldDefinition)133 RecordDefinition (com.revolsys.record.schema.RecordDefinition)38 DataType (com.revolsys.datatype.DataType)32 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)23 JdbcFieldDefinition (com.revolsys.jdbc.field.JdbcFieldDefinition)19 PathName (com.revolsys.io.PathName)15 Record (com.revolsys.record.Record)15 ArrayList (java.util.ArrayList)15 Geometry (com.revolsys.geometry.model.Geometry)13 CodeTable (com.revolsys.record.code.CodeTable)9 Query (com.revolsys.record.query.Query)8 LineString (com.revolsys.geometry.model.LineString)7 ArrayRecord (com.revolsys.record.ArrayRecord)7 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)7 SQLException (java.sql.SQLException)7 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 IOException (java.io.IOException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 BadLocationException (javax.swing.text.BadLocationException)4