Search in sources :

Example 6 with BootException

use of com.github.mgramin.sqlboot.exceptions.BootException in project sql-boot by sql-boot.

the class FunctionJdbcResourceType method read.

@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
    try {
        final List<DbResource> result = new ArrayList<>();
        final ResultSet tables = dataSource.getConnection().getMetaData().getFunctions(null, uri.path(0), uri.path(1));
        final ResultSetMetaData tableMetaData = tables.getMetaData();
        final int columnsCount = tableMetaData.getColumnCount();
        while (tables.next()) {
            final String tableSchem = columnsCount >= 2 ? tables.getString(2) : null;
            final String functionName = columnsCount >= 3 ? tables.getString(3) : null;
            final Map<String, Object> props = new LinkedHashMap<>();
            int i = 1;
            for (String s : properties.keySet()) {
                props.put(s, columnsCount >= i ? tables.getString(i++) : null);
            }
            result.add(new DbResourceImpl(functionName, this, new DbUri(name(), asList(tableSchem, functionName)), 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) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResultSet(java.sql.ResultSet) BootException(com.github.mgramin.sqlboot.exceptions.BootException)

Example 7 with BootException

use of com.github.mgramin.sqlboot.exceptions.BootException in project sql-boot by sql-boot.

the class TableJdbcResourceType method read.

@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
    try {
        final List<DbResource> result = new ArrayList<>();
        final ResultSet tables = dataSource.getConnection().getMetaData().getTables(null, uri.path(0), uri.path(1), new String[] { "TABLE" });
        final ResultSetMetaData tableMetaData = tables.getMetaData();
        final int columnsCount = tableMetaData.getColumnCount();
        while (tables.next()) {
            final String tableSchem = columnsCount >= 2 ? tables.getString(2) : null;
            final String tableName = columnsCount >= 3 ? tables.getString(3) : null;
            final Map<String, Object> props = new LinkedHashMap<>();
            int i = 1;
            for (String s : properties.keySet()) {
                props.put(s, columnsCount >= i ? tables.getString(i++) : null);
            }
            result.add(new DbResourceImpl(tableName, this, new DbUri(name(), asList(tableSchem, tableName)), 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) LinkedHashMap(java.util.LinkedHashMap) ResultSetMetaData(java.sql.ResultSetMetaData) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResultSet(java.sql.ResultSet) BootException(com.github.mgramin.sqlboot.exceptions.BootException)

Example 8 with BootException

use of com.github.mgramin.sqlboot.exceptions.BootException in project sql-boot by sql-boot.

the class FkJdbcResourceType 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<>();
        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 foreignKeys = dataSource.getConnection().getMetaData().getImportedKeys(null, tableSchem, tableName);
            final ResultSetMetaData tableMetaData = foreignKeys.getMetaData();
            final int columnsCount = tableMetaData.getColumnCount();
            while (foreignKeys.next()) {
                final String fkName = columnsCount >= 12 ? foreignKeys.getString(12) : null;
                final Map<String, Object> props = new LinkedHashMap<>();
                int i = 1;
                for (String s : properties.keySet()) {
                    props.put(s, columnsCount >= i ? foreignKeys.getString(i++) : null);
                }
                result.add(new DbResourceImpl(fkName, this, new DbUri(name(), asList(tableSchem, tableName, fkName)), 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) 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)

Example 9 with BootException

use of com.github.mgramin.sqlboot.exceptions.BootException in project sql-boot by sql-boot.

the class PkJdbcResourceType 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<>();
        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 primaryKeys = dataSource.getConnection().getMetaData().getPrimaryKeys(null, tableSchem, tableName);
            final ResultSetMetaData tableMetaData = primaryKeys.getMetaData();
            final int columnsCount = tableMetaData.getColumnCount();
            while (primaryKeys.next()) {
                final String pkName = columnsCount >= 6 ? primaryKeys.getString(6) : null;
                final Map<String, Object> props = new LinkedHashMap<>();
                int i = 1;
                for (String s : properties.keySet()) {
                    props.put(s, columnsCount >= i ? primaryKeys.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) 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)

Example 10 with BootException

use of com.github.mgramin.sqlboot.exceptions.BootException in project sql-boot by sql-boot.

the class ChildTableJdbcResourceType 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().getExportedKeys(null, tableSchem, tableName);
                final ResultSetMetaData tableMetaData = parentTables.getMetaData();
                final int columnsCount = tableMetaData.getColumnCount();
                while (parentTables.next()) {
                    final String pkName = columnsCount >= 13 ? parentTables.getString(13) : 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

BootException (com.github.mgramin.sqlboot.exceptions.BootException)14 DbResource (com.github.mgramin.sqlboot.model.resource.DbResource)12 DbResourceImpl (com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl)12 DbUri (com.github.mgramin.sqlboot.model.uri.impl.DbUri)12 ArrayList (java.util.ArrayList)12 LinkedHashMap (java.util.LinkedHashMap)12 ResultSet (java.sql.ResultSet)11 ResultSetMetaData (java.sql.ResultSetMetaData)11 SQLException (java.sql.SQLException)11 TableJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType)5 File (com.github.mgramin.sqlboot.tools.files.file.File)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 ResourceType (com.github.mgramin.sqlboot.model.resource_type.ResourceType)1 Uri (com.github.mgramin.sqlboot.model.uri.Uri)1 SqlQuery (com.github.mgramin.sqlboot.sql.SqlQuery)1 SimpleFile (com.github.mgramin.sqlboot.tools.files.file.impl.SimpleFile)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1