Search in sources :

Example 1 with JdbcRecordDefinition

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

Aggregations

PathName (com.revolsys.io.PathName)1 JdbcConnection (com.revolsys.jdbc.JdbcConnection)1 JdbcRecordDefinition (com.revolsys.jdbc.io.JdbcRecordDefinition)1 FieldDefinition (com.revolsys.record.schema.FieldDefinition)1 RecordStoreSchemaElement (com.revolsys.record.schema.RecordStoreSchemaElement)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 DelegatingConnection (org.apache.commons.dbcp2.DelegatingConnection)1 CoreConnection (org.sqlite.core.CoreConnection)1