Search in sources :

Example 21 with FieldDefinition

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

the class Query method addFilter.

private static void addFilter(final Query query, final RecordDefinition recordDefinition, final Map<String, ?> filter, final AbstractMultiCondition multipleCondition) {
    if (filter != null && !filter.isEmpty()) {
        for (final Entry<String, ?> entry : filter.entrySet()) {
            final String name = entry.getKey();
            final FieldDefinition fieldDefinition = recordDefinition.getField(name);
            if (fieldDefinition == null) {
                final Object value = entry.getValue();
                if (value == null) {
                    multipleCondition.addCondition(Q.isNull(name));
                } else if (value instanceof Collection) {
                    final Collection<?> values = (Collection<?>) value;
                    multipleCondition.addCondition(new In(name, values));
                } else {
                    multipleCondition.addCondition(Q.equal(name, value));
                }
            } else {
                final Object value = entry.getValue();
                if (value == null) {
                    multipleCondition.addCondition(Q.isNull(name));
                } else if (value instanceof Collection) {
                    final Collection<?> values = (Collection<?>) value;
                    multipleCondition.addCondition(new In(fieldDefinition, values));
                } else {
                    multipleCondition.addCondition(Q.equal(fieldDefinition, value));
                }
            }
        }
        query.setWhereCondition(multipleCondition);
    }
}
Also used : FieldDefinition(com.revolsys.record.schema.FieldDefinition) Collection(java.util.Collection)

Example 22 with FieldDefinition

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

the class GeoPackageRecordStore method refreshSchemaElementsDo.

@Override
protected Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElementsDo(final JdbcRecordStoreSchema schema, final PathName schemaPath) {
    final String schemaName = schema.getPath();
    final Map<String, String> tableDescriptionMap = new HashMap<>();
    final Map<String, List<String>> tablePermissionsMap = new TreeMap<>();
    final Map<PathName, JdbcRecordDefinition> recordDefinitionMap = loadRecordDefinitionsPermissions(schema);
    final Map<PathName, RecordStoreSchemaElement> elementsByPath = new TreeMap<>();
    try {
        try (final Connection connection = getJdbcConnection()) {
            for (final JdbcRecordDefinition recordDefinition : recordDefinitionMap.values()) {
                final PathName typePath = recordDefinition.getPathName();
                elementsByPath.put(typePath, recordDefinition);
            }
            for (final JdbcRecordDefinition recordDefinition : recordDefinitionMap.values()) {
                final String tableName = recordDefinition.getDbTableName();
                final List<String> idFieldNames = new ArrayList<>();
                try (PreparedStatement columnStatement = connection.prepareStatement("PRAGMA table_info(" + tableName + ")")) {
                    try (final ResultSet columnsRs = columnStatement.executeQuery()) {
                        while (columnsRs.next()) {
                            final String dbColumnName = columnsRs.getString("name");
                            final String fieldName = dbColumnName.toUpperCase();
                            final int sqlType = Types.OTHER;
                            String dataType = columnsRs.getString("type");
                            int length = -1;
                            final int scale = -1;
                            if (dataType.startsWith("TEXT(")) {
                                length = Integer.parseInt(dataType.substring(5, dataType.length() - 1));
                                dataType = "TEXT";
                            }
                            final boolean required = columnsRs.getString("notnull").equals("1");
                            final boolean primaryKey = columnsRs.getString("pk").equals("1");
                            if (primaryKey) {
                                idFieldNames.add(fieldName);
                            }
                            final Object defaultValue = columnsRs.getString("dflt_value");
                            final FieldDefinition field = addField(recordDefinition, dbColumnName, fieldName, dataType, sqlType, length, scale, required, null);
                            field.setDefaultValue(defaultValue);
                        }
                    }
                }
                recordDefinition.setIdFieldNames(idFieldNames);
            }
        }
    } catch (final Throwable e) {
        throw new IllegalArgumentException("Unable to load metadata for schema " + schemaName, e);
    }
    return elementsByPath;
}
Also used : HashMap(java.util.HashMap) FieldDefinition(com.revolsys.record.schema.FieldDefinition) Connection(java.sql.Connection) JdbcConnection(com.revolsys.jdbc.JdbcConnection) CoreConnection(org.sqlite.core.CoreConnection) DelegatingConnection(org.apache.commons.dbcp2.DelegatingConnection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) JdbcRecordDefinition(com.revolsys.jdbc.io.JdbcRecordDefinition) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) PathName(com.revolsys.io.PathName) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement)

Example 23 with FieldDefinition

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

the class OracleRecordStore method appendEnvelopeIntersects.

private void appendEnvelopeIntersects(final Query query, final StringBuilder sql, final EnvelopeIntersects envelopeIntersects) {
    final FieldDefinition geometryField = query.getGeometryField();
    if (geometryField instanceof OracleSdoGeometryJdbcFieldDefinition) {
        sql.append("SDO_RELATE(");
        final QueryValue boundingBox1Value = envelopeIntersects.getBoundingBox1Value();
        if (boundingBox1Value == null) {
            sql.append("NULL");
        } else {
            boundingBox1Value.appendSql(query, this, sql);
        }
        sql.append(",");
        final QueryValue boundingBox2Value = envelopeIntersects.getBoundingBox2Value();
        if (boundingBox2Value == null) {
            sql.append("NULL");
        } else {
            boundingBox2Value.appendSql(query, this, sql);
        }
        sql.append(",'mask=ANYINTERACT querytype=WINDOW') = 'TRUE'");
    } else if (geometryField instanceof ArcSdeStGeometryFieldDefinition) {
        sql.append("SDE.ST_ENVINTERSECTS(");
        final QueryValue boundingBox1Value = envelopeIntersects.getBoundingBox1Value();
        if (boundingBox1Value == null) {
            sql.append("NULL");
        } else {
            boundingBox1Value.appendSql(query, this, sql);
        }
        sql.append(",");
        final QueryValue boundingBox2Value = envelopeIntersects.getBoundingBox2Value();
        if (boundingBox2Value == null) {
            sql.append("NULL");
        } else {
            boundingBox2Value.appendSql(query, this, sql);
        }
        sql.append(") = 1");
    } else {
        throw new IllegalArgumentException("Unknown geometry attribute type " + geometryField.getClass());
    }
}
Also used : OracleSdoGeometryJdbcFieldDefinition(com.revolsys.oracle.recordstore.field.OracleSdoGeometryJdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) OracleJdbcRowIdFieldDefinition(com.revolsys.oracle.recordstore.field.OracleJdbcRowIdFieldDefinition) ArcSdeStGeometryFieldDefinition(com.revolsys.oracle.recordstore.esri.ArcSdeStGeometryFieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) OracleSdoGeometryJdbcFieldDefinition(com.revolsys.oracle.recordstore.field.OracleSdoGeometryJdbcFieldDefinition) ArcSdeStGeometryFieldDefinition(com.revolsys.oracle.recordstore.esri.ArcSdeStGeometryFieldDefinition) QueryValue(com.revolsys.record.query.QueryValue)

Example 24 with FieldDefinition

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

the class OracleRecordStore method appendGeometryEqual2d.

private void appendGeometryEqual2d(final Query query, final StringBuilder sql, final GeometryEqual2d equals) {
    final FieldDefinition geometryField = query.getGeometryField();
    if (geometryField instanceof OracleSdoGeometryJdbcFieldDefinition) {
        sql.append("MDSYS.SDO_EQUAL(");
        final QueryValue geometry1Value = equals.getGeometry1Value();
        if (geometry1Value == null) {
            sql.append("NULL");
        } else {
            geometry1Value.appendSql(query, this, sql);
        }
        sql.append(",");
        final QueryValue geometry2Value = equals.getGeometry2Value();
        if (geometry2Value == null) {
            sql.append("NULL");
        } else {
            geometry2Value.appendSql(query, this, sql);
        }
        sql.append(") = 'TRUE'");
    } else if (geometryField instanceof ArcSdeStGeometryFieldDefinition) {
        sql.append("SDE.ST_EQUALS(");
        final QueryValue geometry1Value = equals.getGeometry1Value();
        if (geometry1Value == null) {
            sql.append("NULL");
        } else {
            geometry1Value.appendSql(query, this, sql);
        }
        sql.append(",");
        final QueryValue geometry2Value = equals.getGeometry2Value();
        if (geometry2Value == null) {
            sql.append("NULL");
        } else {
            geometry2Value.appendSql(query, this, sql);
        }
        sql.append(") = 1");
    } else {
        throw new IllegalArgumentException("Unknown geometry attribute type " + geometryField.getClass());
    }
}
Also used : OracleSdoGeometryJdbcFieldDefinition(com.revolsys.oracle.recordstore.field.OracleSdoGeometryJdbcFieldDefinition) FieldDefinition(com.revolsys.record.schema.FieldDefinition) OracleJdbcRowIdFieldDefinition(com.revolsys.oracle.recordstore.field.OracleJdbcRowIdFieldDefinition) ArcSdeStGeometryFieldDefinition(com.revolsys.oracle.recordstore.esri.ArcSdeStGeometryFieldDefinition) JdbcFieldDefinition(com.revolsys.jdbc.field.JdbcFieldDefinition) OracleSdoGeometryJdbcFieldDefinition(com.revolsys.oracle.recordstore.field.OracleSdoGeometryJdbcFieldDefinition) ArcSdeStGeometryFieldDefinition(com.revolsys.oracle.recordstore.esri.ArcSdeStGeometryFieldDefinition) QueryValue(com.revolsys.record.query.QueryValue)

Example 25 with FieldDefinition

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

the class OracleDdlWriter method writeAddGeometryColumn.

public void writeAddGeometryColumn(final RecordDefinition recordDefinition) {
    final PrintWriter out = getOut();
    final String typePath = recordDefinition.getPath();
    String schemaName = JdbcUtils.getSchemaName(typePath);
    if (schemaName.length() == 0) {
        schemaName = "public";
    }
    final String tableName = PathUtil.getName(typePath);
    final FieldDefinition geometryField = recordDefinition.getGeometryField();
    if (geometryField != null) {
        final GeometryFactory geometryFactory = geometryField.getProperty(FieldProperties.GEOMETRY_FACTORY);
        final String name = geometryField.getName();
        String geometryType = "GEOMETRY";
        final DataType dataType = geometryField.getDataType();
        if (dataType == DataTypes.POINT) {
            geometryType = "POINT";
        } else if (dataType == DataTypes.LINE_STRING) {
            geometryType = "LINESTRING";
        } else if (dataType == DataTypes.POLYGON) {
            geometryType = "POLYGON";
        } else if (dataType == DataTypes.MULTI_POINT) {
            geometryType = "MULTIPOINT";
        } else if (dataType == DataTypes.MULTI_LINE_STRING) {
            geometryType = "MULTILINESTRING";
        } else if (dataType == DataTypes.MULTI_POLYGON) {
            geometryType = "MULTIPOLYGON";
        }
        out.print("select addgeometrycolumn('");
        out.print(schemaName.toLowerCase());
        out.print("', '");
        out.print(tableName.toLowerCase());
        out.print("','");
        out.print(name.toLowerCase());
        out.print("',");
        final CoordinateSystem coordinateSystem = geometryFactory.getCoordinateSystem();
        out.print(coordinateSystem.getCoordinateSystemId());
        out.print(",'");
        out.print(geometryType);
        out.print("', ");
        out.print(geometryFactory.getAxisCount());
        out.println(");");
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) CoordinateSystem(com.revolsys.geometry.cs.CoordinateSystem) FieldDefinition(com.revolsys.record.schema.FieldDefinition) DataType(com.revolsys.datatype.DataType) PrintWriter(java.io.PrintWriter)

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