use of com.revolsys.jdbc.io.JdbcRecordDefinition 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;
}
Aggregations