Search in sources :

Example 1 with BootException

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

the class SchemaJdbcResourceType method read.

@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
    try {
        final List<DbResource> result = new ArrayList<>();
        final ResultSet columns = dataSource.getConnection().getMetaData().getSchemas(null, uri.path(0));
        final ResultSetMetaData tableMetaData = columns.getMetaData();
        final int columnsCount = tableMetaData.getColumnCount();
        while (columns.next()) {
            final String schemaName = columnsCount >= 1 ? columns.getString(1) : null;
            final Map<String, Object> properties = new LinkedHashMap<>();
            properties.put("TABLE_SCHEM", columnsCount >= 1 ? columns.getString(1) : null);
            properties.put("TABLE_CATALOG", columnsCount >= 2 ? columns.getString(2) : null);
            result.add(new DbResourceImpl(schemaName, this, new DbUri(name(), asList(schemaName)), properties));
        }
        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 2 with BootException

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

the class ProcedureJdbcResourceType 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().getProcedures(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 procedureName = 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(procedureName, this, new DbUri(name(), asList(tableSchem, procedureName)), 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 3 with BootException

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

the class ColumnJdbcResourceType method read.

@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
    try {
        final List<DbResource> result = new ArrayList<>();
        final ResultSet columns = dataSource.getConnection().getMetaData().getColumns(null, uri.path(0), uri.path(1), uri.path(2));
        final ResultSetMetaData tableMetaData = columns.getMetaData();
        final int columnsCount = tableMetaData.getColumnCount();
        while (columns.next()) {
            final String schemaName = columnsCount >= 2 ? columns.getString(2) : null;
            final String tableName = columnsCount >= 3 ? columns.getString(3) : null;
            final String columnName = columnsCount >= 4 ? columns.getString(4) : null;
            final Map<String, Object> props = new LinkedHashMap<>();
            int i = 1;
            for (String s : properties.keySet()) {
                props.put(s, columnsCount >= i ? columns.getString(i++) : null);
            }
            result.add(new DbResourceImpl(columnName, this, new DbUri(name(), asList(schemaName, tableName, columnName)), 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 4 with BootException

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

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

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

the class ZippedFile method unzip.

private byte[] unzip() throws BootException {
    try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(bytes)) {
        for (final File ent : this.origins) {
            zip.putNextEntry(new ZipEntry(ent.name()));
            zip.write(ent.content());
            zip.closeEntry();
        }
        zip.close();
        bytes.close();
        return bytes.toByteArray();
    } catch (final IOException exception) {
        throw new BootException(exception);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BootException(com.github.mgramin.sqlboot.exceptions.BootException) File(com.github.mgramin.sqlboot.tools.files.file.File)

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