Search in sources :

Example 81 with FieldDefinition

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

the class OgrRecordWriter method insert.

private void insert(final Record record) {
    final RecordDefinition sourceRecordDefinition = record.getRecordDefinition();
    final RecordDefinition recordDefinition = this.recordStore.getRecordDefinition(sourceRecordDefinition);
    final String typePath = sourceRecordDefinition.getPath();
    final List<FieldDefinition> attributes = recordDefinition.getFields();
    final List<String> idFieldNames = recordDefinition.getIdFieldNames();
    for (final FieldDefinition attribute : attributes) {
        final String name = attribute.getName();
        if (!idFieldNames.contains(name)) {
            if (attribute.isRequired()) {
                final Object value = record.getValue(name);
                if (value == null) {
                    throw new IllegalArgumentException("Atribute " + typePath + "." + name + " is required");
                }
            }
        }
    }
    try {
        final Layer layer = getLayer(sourceRecordDefinition);
        final FeatureDefn featureDefinition = layer.GetLayerDefn();
        final Feature feature = new Feature(featureDefinition);
        try {
            setFieldValues(featureDefinition, record, feature);
            setGeometries(featureDefinition, record, feature);
            layer.CreateFeature(feature);
            final String driverName = this.recordStore.getDriverName();
            if (OgrRecordStore.SQLITE.equals(driverName) || OgrRecordStore.GEO_PAKCAGE.equals(driverName)) {
                record.setValue(OgrRecordStore.ROWID, feature.GetFieldAsInteger(OgrRecordStore.ROWID));
            }
            record.setState(RecordState.PERSISTED);
        } finally {
            feature.delete();
            this.recordStore.addStatistic("Insert", record);
        }
    } catch (final IllegalArgumentException e) {
        throw new RuntimeException("Unable to insert row " + e.getMessage() + "\n" + record.toString(), e);
    } catch (final RuntimeException e) {
        Logs.debug(OgrRecordWriter.class, "Unable to insert row \n:" + record.toString());
        throw new RuntimeException("Unable to insert row", e);
    }
}
Also used : FeatureDefn(org.gdal.ogr.FeatureDefn) FieldDefinition(com.revolsys.record.schema.FieldDefinition) LineString(com.revolsys.geometry.model.LineString) Layer(org.gdal.ogr.Layer) Feature(org.gdal.ogr.Feature) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 82 with FieldDefinition

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

the class FileGdbTable method writeFieldHeader.

private void writeFieldHeader(final RecordDefinition recordDefinition) {
    byte geometryType = 0;
    final FieldDefinition geometryField = recordDefinition.getGeometryField();
    if (geometryField != null) {
        final Class<?> geometryClass = geometryField.getTypeClass();
        if (Punctual.class.isAssignableFrom(geometryClass)) {
            geometryType = 1;
        } else if (LineString.class.isAssignableFrom(geometryClass)) {
            geometryType = 3;
        } else if (Lineal.class.isAssignableFrom(geometryClass)) {
            geometryType = 3;
        } else if (Polygonal.class.isAssignableFrom(geometryClass)) {
            geometryType = 4;
        } else {
            throw new IllegalArgumentException("Geometry type not supported " + Classes.className(geometryClass));
        }
    }
    final short fieldCount = (short) (recordDefinition.getFieldCount() + 1);
    this.out.writeLEInt(0);
    // Version
    this.out.writeLEInt(4);
    this.out.write(geometryType);
    this.out.write(0x03);
    this.out.write(0);
    this.out.write(0);
    this.out.writeLEShort(fieldCount);
    for (final FieldDefinition field : recordDefinition.getFields()) {
        writeFieldHeader(field);
    }
    // Update field header size
    final long offset = this.out.getFilePointer();
    final int fieldHeaderSize = (int) (offset - 14 - this.fieldHeaderOffset);
    this.out.seek(this.fieldHeaderOffset);
    this.out.write(fieldHeaderSize);
    this.out.seek(offset);
    this.out.write(0xDE);
    this.out.write(0xAD);
    this.out.write(0xBE);
    this.out.write(0xEF);
}
Also used : LineString(com.revolsys.geometry.model.LineString) Polygonal(com.revolsys.geometry.model.Polygonal) FieldDefinition(com.revolsys.record.schema.FieldDefinition)

Example 83 with FieldDefinition

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

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

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

the class JdbcUtils method addColumnNames.

public static void addColumnNames(final StringBuilder sql, final RecordDefinition recordDefinition, final String tablePrefix, final List<String> fieldNames, boolean hasColumns) {
    for (final String fieldName : fieldNames) {
        if (hasColumns) {
            sql.append(", ");
        }
        final FieldDefinition attribute = recordDefinition.getField(fieldName);
        if (attribute == null) {
            sql.append(fieldName);
        } else {
            addSelectColumnName(sql, tablePrefix, attribute);
        }
        hasColumns = true;
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition)

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