Search in sources :

Example 1 with RecordDefinitionImpl

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

the class JdbcUtils method getSelectSql.

public static String getSelectSql(final Query query) {
    final String tableName = query.getTypeName();
    final String dbTableName = getQualifiedTableName(tableName);
    String sql = query.getSql();
    final Map<? extends CharSequence, Boolean> orderBy = query.getOrderBy();
    RecordDefinition recordDefinition = query.getRecordDefinition();
    if (sql == null) {
        if (recordDefinition == null) {
            recordDefinition = new RecordDefinitionImpl(PathName.newPathName(tableName));
        // throw new IllegalArgumentException("Unknown table name " +
        // tableName);
        }
        final List<String> fieldNames = new ArrayList<>(query.getFieldNames());
        if (fieldNames.isEmpty()) {
            final List<String> recordDefinitionFieldNames = recordDefinition.getFieldNames();
            if (recordDefinitionFieldNames.isEmpty()) {
                fieldNames.add("T.*");
            } else {
                fieldNames.addAll(recordDefinitionFieldNames);
            }
        }
        final String fromClause = query.getFromClause();
        final LockMode lockMode = query.getLockMode();
        final boolean distinct = query.isDistinct();
        sql = newSelectSql(recordDefinition, "T", distinct, fromClause, fieldNames, query, orderBy, lockMode);
    } else {
        if (sql.toUpperCase().startsWith("SELECT * FROM ")) {
            final StringBuilder newSql = new StringBuilder("SELECT ");
            addColumnNames(newSql, recordDefinition, dbTableName);
            newSql.append(" FROM ");
            newSql.append(sql.substring(14));
            sql = newSql.toString();
        }
        if (!orderBy.isEmpty()) {
            final StringBuilder buffer = new StringBuilder(sql);
            addOrderBy(buffer, orderBy);
            sql = buffer.toString();
        }
    }
    return sql;
}
Also used : ArrayList(java.util.ArrayList) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) LockMode(com.revolsys.record.schema.LockMode) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 2 with RecordDefinitionImpl

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

the class AbstractJdbcRecordStore method refreshSchemaElementsDo.

protected Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElementsDo(final JdbcRecordStoreSchema schema, final PathName schemaPath) {
    final String dbSchemaName = schema.getDbName();
    final Map<PathName, JdbcRecordDefinition> recordDefinitionMap = loadRecordDefinitionsPermissions(schema);
    final Map<PathName, RecordStoreSchemaElement> elementsByPath = new TreeMap<>();
    try {
        try (final Connection connection = getJdbcConnection()) {
            final DatabaseMetaData databaseMetaData = connection.getMetaData();
            final Map<String, List<String>> idFieldNameMap = loadIdFieldNames(dbSchemaName);
            for (final JdbcRecordDefinition recordDefinition : recordDefinitionMap.values()) {
                final PathName typePath = recordDefinition.getPathName();
                final List<String> idFieldNames = idFieldNameMap.get(typePath);
                if (Property.isEmpty(idFieldNames)) {
                    addRowIdFieldDefinition(recordDefinition);
                }
                elementsByPath.put(typePath, recordDefinition);
            }
            try (final ResultSet columnsRs = databaseMetaData.getColumns(null, dbSchemaName, "%", "%")) {
                while (columnsRs.next()) {
                    final String tableName = columnsRs.getString("TABLE_NAME").toUpperCase();
                    final PathName typePath = schemaPath.newChild(tableName);
                    final JdbcRecordDefinition recordDefinition = recordDefinitionMap.get(typePath);
                    if (recordDefinition != null) {
                        final String dbColumnName = columnsRs.getString("COLUMN_NAME");
                        final String name = dbColumnName.toUpperCase();
                        final int sqlType = columnsRs.getInt("DATA_TYPE");
                        final String dataType = columnsRs.getString("TYPE_NAME");
                        final int length = columnsRs.getInt("COLUMN_SIZE");
                        int scale = columnsRs.getInt("DECIMAL_DIGITS");
                        if (columnsRs.wasNull()) {
                            scale = -1;
                        }
                        final boolean required = !columnsRs.getString("IS_NULLABLE").equals("YES");
                        final String description = columnsRs.getString("REMARKS");
                        addField(recordDefinition, dbColumnName, name, dataType, sqlType, length, scale, required, description);
                    }
                }
                for (final RecordDefinitionImpl recordDefinition : recordDefinitionMap.values()) {
                    final String typePath = recordDefinition.getPath();
                    final List<String> idFieldNames = idFieldNameMap.get(typePath);
                    if (!Property.isEmpty(idFieldNames)) {
                        recordDefinition.setIdFieldNames(idFieldNames);
                    }
                }
            }
        }
    } catch (final Throwable e) {
        throw new IllegalArgumentException("Unable to load metadata for schema " + dbSchemaName, e);
    }
    return elementsByPath;
}
Also used : Connection(java.sql.Connection) JdbcConnection(com.revolsys.jdbc.JdbcConnection) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) TreeMap(java.util.TreeMap) DatabaseMetaData(java.sql.DatabaseMetaData) ResultSet(java.sql.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) PathName(com.revolsys.io.PathName) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement)

Example 3 with RecordDefinitionImpl

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

the class EsriXmlRecordDefinitionUtil method getRecordDefinition.

public static RecordDefinition getRecordDefinition(final String schemaName, final Domain domain, final boolean appendIdToName) {
    final String tableName;
    if (appendIdToName) {
        tableName = domain.getName() + "_ID";
    } else {
        tableName = domain.getName();
    }
    final PathName typePath = PathName.newPathName(PathUtil.toPath(schemaName, tableName));
    final RecordDefinitionImpl recordDefinition = new RecordDefinitionImpl(typePath);
    final FieldType fieldType = domain.getFieldType();
    final DataType dataType = EsriGeodatabaseXmlFieldTypeRegistry.INSTANCE.getDataType(fieldType);
    int length = 0;
    for (final CodedValue codedValue : domain.getCodedValues()) {
        length = Math.max(length, codedValue.getCode().toString().length());
    }
    recordDefinition.addField(tableName, dataType, length, true);
    recordDefinition.addField("DESCRIPTION", DataTypes.STRING, 255, true);
    recordDefinition.setIdFieldIndex(0);
    return recordDefinition;
}
Also used : RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) DataType(com.revolsys.datatype.DataType) PathName(com.revolsys.io.PathName) FieldType(com.revolsys.record.io.format.esri.gdb.xml.model.enums.FieldType) EsriGeodatabaseXmlFieldType(com.revolsys.record.io.format.esri.gdb.xml.type.EsriGeodatabaseXmlFieldType)

Example 4 with RecordDefinitionImpl

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

the class MoepConstants method newRecordDefinition.

public static RecordDefinitionImpl newRecordDefinition(final String typePath) {
    final RecordDefinitionImpl type = new RecordDefinitionImpl(PathName.newPathName(typePath));
    type.addField(FEATURE_CODE, DataTypes.STRING, 10, true);
    type.addField(MAPSHEET_NAME, DataTypes.STRING, 7, false);
    type.addField(DISPLAY_TYPE, DataTypes.STRING, 20, true);
    type.addField(ANGLE, DataTypes.DECIMAL, false);
    type.addField(ELEVATION, DataTypes.DECIMAL, false);
    type.addField(TEXT_GROUP, DataTypes.DECIMAL, false);
    type.addField(TEXT_INDEX, DataTypes.DECIMAL, false);
    type.addField(TEXT, DataTypes.STRING, 200, false);
    type.addField(FONT_NAME, DataTypes.STRING, 10, false);
    type.addField(FONT_SIZE, DataTypes.DECIMAL, false);
    type.addField(FONT_WEIGHT, DataTypes.STRING, 10, false);
    type.addField(ORIGINAL_FILE_TYPE, DataTypes.STRING, 20, false);
    type.addField(ADMIT_SOURCE_DATE, DataTypes.DATE, false);
    type.addField(ADMIT_REASON_FOR_CHANGE, DataTypes.STRING, 1, false);
    type.addField(ADMIT_INTEGRATION_DATE, DataTypes.DATE, false);
    type.addField(ADMIT_REVISION_KEY, DataTypes.STRING, 10, false);
    type.addField(ADMIT_SPECIFICATIONS_RELEASE, DataTypes.STRING, 10, false);
    type.addField(RETIRE_SOURCE_DATE, DataTypes.DATE, false);
    type.addField(RETIRE_REASON_FOR_CHANGE, DataTypes.STRING, 1, false);
    type.addField(RETIRE_INTEGRATION_DATE, DataTypes.DATE, false);
    type.addField(RETIRE_REVISION_KEY, DataTypes.STRING, 10, false);
    type.addField(RETIRE_SPECIFICATIONS_RELEASE, DataTypes.STRING, 10, false);
    type.addField(GEOMETRY, DataTypes.GEOMETRY, true);
    type.setGeometryFieldName(GEOMETRY);
    return type;
}
Also used : RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl)

Example 5 with RecordDefinitionImpl

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

the class OsnReader method skipToFirstRecord.

/**
 * Skip all objects and attributes until the first object in the collection.
 *
 * @return True if an object was found.
 * @throws IOException If an I/O error occurs.
 */
private boolean skipToFirstRecord() throws IOException {
    if (this.osnIterator.next() == OsnIterator.START_DEFINITION) {
        final String typePath = this.osnIterator.getPathValue();
        final RecordDefinitionImpl type = (RecordDefinitionImpl) this.recordDefinitionFactory.getRecordDefinition(typePath);
        final RecordDefinition spatialDataSetType = this.recordDefinitionFactory.getRecordDefinition("/SpatialDataSet");
        if (type != null && type.isInstanceOf(spatialDataSetType)) {
            final String oiName = this.osnIterator.nextFieldName();
            if (oiName != null && oiName.equals("objectIdentifier")) {
                this.osnIterator.nextStringValue();
                final String fieldName = this.osnIterator.nextFieldName();
                if (fieldName != null && (fieldName.equals("geoComponents") || fieldName.equals("annotationComponents"))) {
                    if (this.osnIterator.next() == OsnIterator.START_SET) {
                        return true;
                    } else {
                        this.osnIterator.throwParseError("Expecting a set of objects");
                    }
                } else {
                    this.osnIterator.throwParseError("Excepecting the 'geoComponents' attribute");
                }
            } else {
                this.osnIterator.throwParseError("Expecting the 'objectIdentifier' attribute");
            }
        } else {
            return true;
        }
    } else {
        this.osnIterator.throwParseError("Expecting a start of an object definition");
    }
    return false;
}
Also used : RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)30 PathName (com.revolsys.io.PathName)14 DataType (com.revolsys.datatype.DataType)10 FieldDefinition (com.revolsys.record.schema.FieldDefinition)7 RecordDefinition (com.revolsys.record.schema.RecordDefinition)6 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)5 RecordStoreSchema (com.revolsys.record.schema.RecordStoreSchema)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Geometry (com.revolsys.geometry.model.Geometry)3 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)3 ArrayRecord (com.revolsys.record.ArrayRecord)3 Record (com.revolsys.record.Record)3 DETable (com.revolsys.record.io.format.esri.gdb.xml.model.DETable)3 MapEx (com.revolsys.collection.map.MapEx)2 FileGdbRecordStore (com.revolsys.gis.esri.gdb.file.FileGdbRecordStore)2 FileGdbDomainCodeTable (com.revolsys.gis.esri.gdb.file.capi.FileGdbDomainCodeTable)2 CodeTable (com.revolsys.record.code.CodeTable)2 DEFeatureClass (com.revolsys.record.io.format.esri.gdb.xml.model.DEFeatureClass)2 Field (com.revolsys.record.io.format.esri.gdb.xml.model.Field)2