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