Search in sources :

Example 6 with RecordDefinition

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(");");
}
Also used : RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 7 with RecordDefinition

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) FieldDefinition(com.revolsys.record.schema.FieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) ArrayList(java.util.ArrayList) PathName(com.revolsys.io.PathName) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 8 with RecordDefinition

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);
}
Also used : RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 9 with RecordDefinition

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);
}
Also used : RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 10 with RecordDefinition

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

Aggregations

RecordDefinition (com.revolsys.record.schema.RecordDefinition)189 FieldDefinition (com.revolsys.record.schema.FieldDefinition)38 Record (com.revolsys.record.Record)34 Geometry (com.revolsys.geometry.model.Geometry)20 CodeTable (com.revolsys.record.code.CodeTable)19 Query (com.revolsys.record.query.Query)18 LineString (com.revolsys.geometry.model.LineString)17 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)16 PathName (com.revolsys.io.PathName)13 ArrayList (java.util.ArrayList)12 DataType (com.revolsys.datatype.DataType)11 Identifier (com.revolsys.identifier.Identifier)11 RecordReader (com.revolsys.record.io.RecordReader)11 RecordStore (com.revolsys.record.schema.RecordStore)11 HashMap (java.util.HashMap)9 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)8 ArrayRecord (com.revolsys.record.ArrayRecord)8 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)6 Table (com.revolsys.gis.esri.gdb.file.capi.swig.Table)5 RecordWriter (com.revolsys.record.io.RecordWriter)5