Search in sources :

Example 1 with RecordStoreSchemaElement

use of com.revolsys.record.schema.RecordStoreSchemaElement 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 2 with RecordStoreSchemaElement

use of com.revolsys.record.schema.RecordStoreSchemaElement 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 3 with RecordStoreSchemaElement

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

the class RecordStoreConnectionTrees method addLayer.

private static void addLayer(final RecordDefinitionImpl recordDefinition) {
    final PathName typePath = recordDefinition.getPathName();
    final RecordStore recordStore = recordDefinition.getRecordStore();
    final Map<String, Object> connection = recordStore.getConnectionProperties();
    final Map<String, Object> layerConfig = new LinkedHashMap<>();
    MapObjectFactory.setType(layerConfig, "recordStoreLayer");
    layerConfig.put("name", recordDefinition.getName());
    layerConfig.put("connection", connection);
    layerConfig.put("typePath", typePath);
    layerConfig.put("showTableView", AbstractLayer.isShowNewLayerTableView());
    final LinkedList<String> path = new LinkedList<>();
    {
        BaseTreeNode node = BaseTree.getMenuNode();
        node = node.getParent();
        while (node != null) {
            final Object nodeValue = node.getUserObject();
            String nodeName = node.getName();
            if (node instanceof PathTreeNode) {
                nodeName = FileUtil.getBaseName(nodeName);
            }
            if (nodeValue instanceof RecordStoreSchemaElement) {
                path.addFirst(nodeName);
                node = node.getParent();
            } else {
                path.addFirst(nodeName);
                node = null;
            }
        }
    }
    final AbstractLayer layer = new RecordStoreLayer(layerConfig);
    LayerGroup layerGroup = Project.get();
    for (final String name : path) {
        try {
            layerGroup = layerGroup.addLayerGroup(name);
        } catch (final IllegalArgumentException e) {
            int i = 1;
            while (layerGroup.hasLayerWithSameName(null, name + i)) {
                i++;
            }
            layerGroup = layerGroup.addLayerGroup(name + i);
        }
    }
    layerGroup.addLayer(layer);
}
Also used : RecordStoreLayer(com.revolsys.swing.map.layer.record.RecordStoreLayer) AbstractLayer(com.revolsys.swing.map.layer.AbstractLayer) LayerGroup(com.revolsys.swing.map.layer.LayerGroup) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) PathTreeNode(com.revolsys.swing.tree.node.file.PathTreeNode) RecordStore(com.revolsys.record.schema.RecordStore) BaseTreeNode(com.revolsys.swing.tree.BaseTreeNode) PathName(com.revolsys.io.PathName) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement)

Example 4 with RecordStoreSchemaElement

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

the class PathRecordStoreTreeNode method loadChildrenDo.

@Override
protected List<BaseTreeNode> loadChildrenDo() {
    final RecordStore recordStore = getRecordStore();
    if (recordStore != null) {
        final RecordStoreSchema schema = recordStore.getRootSchema();
        if (schema != null) {
            schema.refresh();
            final List<BaseTreeNode> children = new ArrayList<>();
            for (final RecordStoreSchemaElement element : schema.getElements()) {
                final BaseTreeNode node = BaseTreeNode.newTreeNode(element);
                children.add(node);
            }
            return children;
        }
    }
    return Collections.emptyList();
}
Also used : RecordStoreSchema(com.revolsys.record.schema.RecordStoreSchema) RecordStore(com.revolsys.record.schema.RecordStore) ArrayList(java.util.ArrayList) BaseTreeNode(com.revolsys.swing.tree.BaseTreeNode) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement)

Example 5 with RecordStoreSchemaElement

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

the class FileGdbRecordStore method refreshSchemaElements.

@Override
protected Map<PathName, ? extends RecordStoreSchemaElement> refreshSchemaElements(final RecordStoreSchema schema) {
    synchronized (this.apiSync) {
        synchronized (API_SYNC) {
            final Map<PathName, RecordStoreSchemaElement> elementsByPath = new TreeMap<>();
            final Geodatabase geodatabase = getGeodatabase();
            if (geodatabase != null) {
                try {
                    final PathName schemaPath = schema.getPathName();
                    final String schemaCatalogPath = getCatalogPath(schema);
                    final VectorOfWString childDatasets = getChildDatasets(geodatabase, schemaCatalogPath, "Feature Dataset");
                    if (childDatasets != null) {
                        for (int i = 0; i < childDatasets.size(); i++) {
                            final String childCatalogPath = childDatasets.get(i);
                            final PathName childPath = toPath(childCatalogPath);
                            RecordStoreSchema childSchema = schema.getSchema(childPath);
                            if (childSchema == null) {
                                childSchema = newFeatureDatasetSchema(schema, childPath);
                            } else {
                                if (childSchema.isInitialized()) {
                                    childSchema.refresh();
                                }
                            }
                            elementsByPath.put(childPath, childSchema);
                        }
                    }
                    if (schemaPath.isParentOf(this.defaultSchemaPath) && !elementsByPath.containsKey(this.defaultSchemaPath)) {
                        final SpatialReference spatialReference = getSpatialReference(getGeometryFactory());
                        final RecordStoreSchema childSchema = newSchema(this.defaultSchemaPath, spatialReference);
                        elementsByPath.put(this.defaultSchemaPath, childSchema);
                    }
                    if (schema.equalPath(this.defaultSchemaPath)) {
                        refreshSchemaRecordDefinitions(elementsByPath, schemaPath, "\\", "Feature Class");
                        refreshSchemaRecordDefinitions(elementsByPath, schemaPath, "\\", "Table");
                    }
                    refreshSchemaRecordDefinitions(elementsByPath, schemaPath, schemaCatalogPath, "Feature Class");
                    refreshSchemaRecordDefinitions(elementsByPath, schemaPath, schemaCatalogPath, "Table");
                } finally {
                    releaseGeodatabase();
                }
            }
            return elementsByPath;
        }
    }
}
Also used : Geodatabase(com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) RecordStoreSchema(com.revolsys.record.schema.RecordStoreSchema) SpatialReference(com.revolsys.record.io.format.esri.gdb.xml.model.SpatialReference) PathName(com.revolsys.io.PathName) VectorOfWString(com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString) TreeMap(java.util.TreeMap) RecordStoreSchemaElement(com.revolsys.record.schema.RecordStoreSchemaElement)

Aggregations

RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)8 PathName (com.revolsys.io.PathName)7 TreeMap (java.util.TreeMap)6 RecordStoreSchema (com.revolsys.record.schema.RecordStoreSchema)4 ArrayList (java.util.ArrayList)3 JdbcConnection (com.revolsys.jdbc.JdbcConnection)2 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)2 RecordStore (com.revolsys.record.schema.RecordStore)2 BaseTreeNode (com.revolsys.swing.tree.BaseTreeNode)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2 Geodatabase (com.revolsys.gis.esri.gdb.file.capi.swig.Geodatabase)1 VectorOfWString (com.revolsys.gis.esri.gdb.file.capi.swig.VectorOfWString)1 ExtensionFilenameFilter (com.revolsys.io.filter.ExtensionFilenameFilter)1 JdbcRecordDefinition (com.revolsys.jdbc.io.JdbcRecordDefinition)1 SpatialReference (com.revolsys.record.io.format.esri.gdb.xml.model.SpatialReference)1 FieldDefinition (com.revolsys.record.schema.FieldDefinition)1 RecordDefinition (com.revolsys.record.schema.RecordDefinition)1 PathResource (com.revolsys.spring.resource.PathResource)1