Search in sources :

Example 1 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method getRecordDefinition.

@Override
public JdbcRecordDefinition getRecordDefinition(String typePath, final ResultSetMetaData resultSetMetaData, final String dbTableName) {
    if (Property.isEmpty(typePath)) {
        typePath = "Record";
    }
    try {
        final PathName pathName = PathName.newPathName(typePath);
        final PathName schemaName = pathName.getParent();
        final JdbcRecordStoreSchema schema = getSchema(schemaName);
        final JdbcRecordDefinition resultRecordDefinition = newRecordDefinition(schema, pathName, dbTableName);
        final RecordDefinition recordDefinition = getRecordDefinition(typePath);
        for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
            final String fieldName = resultSetMetaData.getColumnName(i).toUpperCase();
            if (recordDefinition != null && recordDefinition.isIdField(fieldName)) {
                resultRecordDefinition.setIdFieldIndex(i - 1);
            }
            addField(resultSetMetaData, resultRecordDefinition, fieldName, i, null);
        }
        addRecordDefinitionProperties(resultRecordDefinition);
        return resultRecordDefinition;
    } catch (final SQLException e) {
        throw new IllegalArgumentException("Unable to load metadata for " + typePath);
    }
}
Also used : SQLException(java.sql.SQLException) PathName(com.revolsys.io.PathName) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 2 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method loadRecordDefinitionsPermissions.

protected Map<PathName, JdbcRecordDefinition> loadRecordDefinitionsPermissions(final JdbcRecordStoreSchema schema) {
    final PathName schemaPath = schema.getPathName();
    final String dbSchemaName = schema.getDbName();
    try (final Connection connection = getJdbcConnection();
        final PreparedStatement statement = connection.prepareStatement(this.schemaTablePermissionsSql)) {
        if (this.schemaTablePermissionsSql.indexOf('?') != -1) {
            statement.setString(1, dbSchemaName);
        }
        try (final ResultSet resultSet = statement.executeQuery()) {
            final Map<PathName, JdbcRecordDefinition> recordDefinitionMap = new TreeMap<>();
            while (resultSet.next()) {
                final String dbTableName = resultSet.getString("TABLE_NAME");
                if (!isExcluded(dbSchemaName, dbTableName)) {
                    final String tableName = dbTableName.toUpperCase();
                    final PathName pathName = schemaPath.newChild(tableName);
                    JdbcRecordDefinition recordDefinition = recordDefinitionMap.get(pathName);
                    Set<String> tablePermissions;
                    if (recordDefinition == null) {
                        recordDefinition = newRecordDefinition(schema, pathName, dbTableName);
                        recordDefinitionMap.put(pathName, recordDefinition);
                        tablePermissions = new LinkedHashSet<>();
                        recordDefinition.setProperty("permissions", tablePermissions);
                        final String description = resultSet.getString("REMARKS");
                        recordDefinition.setDescription(description);
                        final String tableType = resultSet.getString("TABLE_TYPE");
                        recordDefinition.setProperty("tableType", tableType);
                    } else {
                        tablePermissions = recordDefinition.getProperty("permissions");
                    }
                    final String privilege = resultSet.getString("PRIVILEGE");
                    if ("ALL".equals(privilege)) {
                        tablePermissions.add("SELECT");
                        tablePermissions.add("INSERT");
                        tablePermissions.add("UPDATE");
                        tablePermissions.add("DELETE");
                    } else {
                        tablePermissions.add(privilege);
                    }
                }
            }
            return recordDefinitionMap;
        }
    } catch (final Throwable e) {
        throw Exceptions.wrap("Unable to get schema and table permissions: " + dbSchemaName, e);
    }
}
Also used : Connection(java.sql.Connection) JdbcConnection(com.revolsys.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PathName(com.revolsys.io.PathName) TreeMap(java.util.TreeMap)

Example 3 with PathName

use of com.revolsys.io.PathName 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 4 with PathName

use of com.revolsys.io.PathName 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 5 with PathName

use of com.revolsys.io.PathName in project com.revolsys.open by revolsys.

the class JdbcRecordDefinition method rename.

@Override
public JdbcRecordDefinition rename(final String path) {
    final JdbcRecordStoreSchema schema = getSchema();
    final PathName pathName = PathName.newPathName(path);
    return new JdbcRecordDefinition(schema, pathName, this.dbTableName);
}
Also used : PathName(com.revolsys.io.PathName)

Aggregations

PathName (com.revolsys.io.PathName)64 FieldDefinition (com.revolsys.record.schema.FieldDefinition)15 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)14 DataType (com.revolsys.datatype.DataType)13 RecordDefinition (com.revolsys.record.schema.RecordDefinition)13 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)12 RecordStoreSchema (com.revolsys.record.schema.RecordStoreSchema)11 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)9 RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)7 ArrayList (java.util.ArrayList)7 TreeMap (java.util.TreeMap)7 MapEx (com.revolsys.collection.map.MapEx)5 ResultSet (java.sql.ResultSet)5 Geodatabase (com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase)4 Identifier (com.revolsys.identifier.Identifier)4 JdbcConnection (com.revolsys.jdbc.JdbcConnection)4 Record (com.revolsys.record.Record)4 File (java.io.File)4 PreparedStatement (java.sql.PreparedStatement)4 Geometry (com.revolsys.geometry.model.Geometry)3