Search in sources :

Example 6 with TableJdbcResourceType

use of com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType in project sql-boot by sql-boot.

the class ParentTableJdbcResourceType method read.

@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
    try {
        final TableJdbcResourceType tableJdbcResourceType = new TableJdbcResourceType(dataSource);
        final Stream<DbResource> tables = tableJdbcResourceType.read(uri);
        final List<DbResource> result = new ArrayList<>();
        try (final Connection connection = dataSource.getConnection()) {
            for (final DbResource table : tables.collect(toList())) {
                final String tableSchem = table.headers().get("TABLE_SCHEM").toString();
                final String tableName = table.headers().get("TABLE_NAME").toString();
                final ResultSet parentTables = connection.getMetaData().getImportedKeys(null, tableSchem, tableName);
                final ResultSetMetaData tableMetaData = parentTables.getMetaData();
                final int columnsCount = tableMetaData.getColumnCount();
                while (parentTables.next()) {
                    final String pkName = columnsCount >= 12 ? parentTables.getString(12) : null;
                    final Map<String, Object> props = new LinkedHashMap<>();
                    int i = 1;
                    for (String s : properties.keySet()) {
                        props.put(s, columnsCount >= i ? parentTables.getString(i++) : null);
                    }
                    result.add(new DbResourceImpl(pkName, this, new DbUri(name(), asList(tableSchem, tableName, pkName)), props));
                }
            }
        }
        return result.stream();
    } catch (SQLException e) {
        throw new BootException(e);
    }
}
Also used : DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) DbResourceImpl(com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) TableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResultSet(java.sql.ResultSet) BootException(com.github.mgramin.sqlboot.exceptions.BootException)

Aggregations

TableJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType)6 ArrayList (java.util.ArrayList)6 BootException (com.github.mgramin.sqlboot.exceptions.BootException)5 DbResource (com.github.mgramin.sqlboot.model.resource.DbResource)5 DbResourceImpl (com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl)5 DbUri (com.github.mgramin.sqlboot.model.uri.impl.DbUri)5 ResultSet (java.sql.ResultSet)5 ResultSetMetaData (java.sql.ResultSetMetaData)5 SQLException (java.sql.SQLException)5 LinkedHashMap (java.util.LinkedHashMap)5 Connection (java.sql.Connection)2 ResourceType (com.github.mgramin.sqlboot.model.resource_type.ResourceType)1 MarkdownFile (com.github.mgramin.sqlboot.model.resource_type.impl.composite.md.MarkdownFile)1 SchemaJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.SchemaJdbcResourceType)1 FunctionJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.function.FunctionJdbcResourceType)1 ProcedureJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.procedure.ProcedureJdbcResourceType)1 ColumnJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.column.ColumnJdbcResourceType)1 FkJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.fk.FkJdbcResourceType)1 IndexJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.index.IndexJdbcResourceType)1 PkJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.pk.PkJdbcResourceType)1