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);
}
}
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);
}
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;
}
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);
}
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;
}
}
Aggregations