use of com.revolsys.record.schema.RecordDefinition in project com.revolsys.open by revolsys.
the class JdbcDdlWriter method writeInsert.
public void writeInsert(final Record row) {
final RecordDefinition recordDefinition = row.getRecordDefinition();
final String typePath = recordDefinition.getPath();
this.out.print("INSERT INTO ");
writeTableName(typePath);
this.out.print(" (");
for (int i = 0; i < recordDefinition.getFieldCount(); i++) {
if (i > 0) {
this.out.print(", ");
}
this.out.print(recordDefinition.getFieldName(i));
}
this.out.print(" ) VALUES (");
for (int i = 0; i < recordDefinition.getFieldCount(); i++) {
if (i > 0) {
this.out.print(", ");
}
final Object value = row.getValue(i);
if (value == null) {
this.out.print("NULL");
} else if (value instanceof Number) {
final Number number = (Number) value;
this.out.print(Numbers.toString(number));
} else {
this.out.print("'");
this.out.print(value.toString().replaceAll("'", "''"));
this.out.print("'");
}
}
this.out.println(");");
}
use of com.revolsys.record.schema.RecordDefinition in project com.revolsys.open by revolsys.
the class JdbcQueryIterator method getResultSet.
protected ResultSet getResultSet() {
final String tableName = this.query.getTypeName();
final RecordDefinition queryRecordDefinition = this.query.getRecordDefinition();
if (queryRecordDefinition != null) {
this.recordDefinition = this.recordStore.getRecordDefinition(queryRecordDefinition);
if (this.recordDefinition != null) {
this.query.setRecordDefinition(this.recordDefinition);
}
}
if (this.recordDefinition == null) {
if (tableName != null) {
this.recordDefinition = this.recordStore.getRecordDefinition(tableName);
this.query.setRecordDefinition(this.recordDefinition);
}
}
String dbTableName;
if (this.recordDefinition == null) {
final PathName pathName = PathName.newPathName(tableName);
if (pathName == null) {
dbTableName = null;
} else {
dbTableName = pathName.getName();
}
} else {
dbTableName = this.recordDefinition.getDbTableName();
}
final String sql = getSql(this.query);
try {
this.statement = this.connection.prepareStatement(sql);
this.statement.setFetchSize(this.fetchSize);
this.resultSet = getResultSet(this.statement, this.query);
final ResultSetMetaData resultSetMetaData = this.resultSet.getMetaData();
if (this.recordDefinition == null) {
this.recordDefinition = this.recordStore.getRecordDefinition(tableName, resultSetMetaData, dbTableName);
}
final List<String> fieldNames = new ArrayList<>(this.query.getFieldNames());
if (fieldNames.isEmpty()) {
this.fields.addAll(this.recordDefinition.getFields());
} else {
for (String fieldName : fieldNames) {
if (fieldName.equals("*")) {
this.fields.addAll(this.recordDefinition.getFields());
} else {
if (fieldName.endsWith("\"")) {
final int index = fieldName.indexOf('"');
if (index > 0 && fieldName.charAt(index - 1) == ' ') {
fieldName = fieldName.substring(index + 1, fieldName.length() - 1);
}
}
final FieldDefinition field = this.recordDefinition.getField(fieldName);
if (field != null) {
this.fields.add(field);
}
}
}
}
final String typePath = this.query.getTypeNameAlias();
if (typePath != null) {
final JdbcRecordDefinition newRecordDefinition = this.recordDefinition.rename(typePath);
this.recordDefinition = newRecordDefinition;
}
} catch (final SQLException e) {
JdbcUtils.close(this.statement, this.resultSet);
throw this.connection.getException("Execute Query", sql, e);
}
return this.resultSet;
}
use of com.revolsys.record.schema.RecordDefinition in project com.revolsys.open by revolsys.
the class RecordWriterFactory method newGeometryWriter.
@Override
default GeometryWriter newGeometryWriter(final Resource resource, final MapEx properties) {
final RecordDefinition recordDefinition = Records.newGeometryRecordDefinition();
final RecordWriter recordWriter = newRecordWriter(recordDefinition, resource);
recordWriter.setProperties(properties);
return new RecordWriterGeometryWriter(recordWriter);
}
use of com.revolsys.record.schema.RecordDefinition in project com.revolsys.open by revolsys.
the class RecordWriterFactory method newGeometryWriter.
@Override
default GeometryWriter newGeometryWriter(final String baseName, final OutputStream out, final Charset charset) {
final RecordDefinition recordDefinition = Records.newGeometryRecordDefinition();
final RecordWriter recordWriter = newRecordWriter(baseName, recordDefinition, out, charset);
return new RecordWriterGeometryWriter(recordWriter);
}
use of com.revolsys.record.schema.RecordDefinition 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);
}
}
}
Aggregations