Search in sources :

Example 11 with BootException

use of com.github.mgramin.sqlboot.exceptions.BootException 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)

Example 12 with BootException

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

the class ViewJdbcResourceType 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[] { "VIEW" });
        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<>();
            props.put("TABLE_CAT", columnsCount >= 1 ? tables.getString(1) : null);
            props.put("TABLE_SCHEM", columnsCount >= 2 ? tables.getString(2) : null);
            props.put("TABLE_NAME", columnsCount >= 3 ? tables.getString(3) : null);
            props.put("TABLE_TYPE", columnsCount >= 4 ? tables.getString(4) : null);
            props.put("REMARKS", columnsCount >= 5 ? tables.getString(5) : null);
            props.put("TYPE_CAT", columnsCount >= 6 ? tables.getString(6) : null);
            props.put("TYPE_SCHEM", columnsCount >= 7 ? tables.getString(7) : null);
            props.put("TYPE_NAME", columnsCount >= 8 ? tables.getString(8) : null);
            props.put("SELF_REFERENCING_COL_NAME", columnsCount >= 9 ? tables.getString(9) : null);
            props.put("REF_GENERATION", columnsCount >= 10 ? tables.getString(10) : 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 13 with BootException

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

the class SqlResourceType method read.

@Override
public Stream<DbResource> read(final Uri uri) throws BootException {
    final Map<String, Object> variables = new HashMap<>();
    variables.put("uri", uri);
    return sqlQuery.select(variables).map(o -> {
        final List<Object> path = o.entrySet().stream().filter(v -> (v.getKey().startsWith("@") || v.getKey().startsWith("_"))).map(Entry::getValue).collect(toList());
        final String name = path.get(path.size() - 1).toString();
        final Map<String, Object> headers = o.entrySet().stream().collect(toMap(k -> strip(strip(k.getKey(), "@"), "_"), v -> ofNullable(v.getValue()).orElse(""), (a, b) -> a, LinkedHashMap::new));
        return new DbResourceImpl(name, this, new DbUri(this.name(), path.stream().map(Object::toString).collect(toList())), headers);
    });
}
Also used : Optional.ofNullable(java.util.Optional.ofNullable) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) SqlQuery(com.github.mgramin.sqlboot.sql.SqlQuery) HashMap(java.util.HashMap) BootException(com.github.mgramin.sqlboot.exceptions.BootException) DbResourceImpl(com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl) LinkedHashMap(java.util.LinkedHashMap) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) Uri(com.github.mgramin.sqlboot.model.uri.Uri) Map(java.util.Map) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) Entry(java.util.Map.Entry) ToString(lombok.ToString) StringUtils.strip(org.apache.commons.lang3.StringUtils.strip) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) DbResourceImpl(com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ToString(lombok.ToString)

Example 14 with BootException

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

the class LocalFileSystem method listFiles.

@Override
public List<File> listFiles(final String mask) {
    try {
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setIncludes(new String[] { mask });
        scanner.setBasedir(this.basedir.replace("\\", "/"));
        scanner.setCaseSensitive(false);
        scanner.scan();
        String[] files = scanner.getIncludedFiles();
        List<File> result = new ArrayList<>();
        for (String file : files) {
            result.add(new SimpleFile(file, FileUtils.readFileToByteArray(new java.io.File(basedir + "/" + file))));
        }
        return result;
    } catch (IOException exception) {
        throw new BootException(exception);
    }
}
Also used : DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ArrayList(java.util.ArrayList) SimpleFile(com.github.mgramin.sqlboot.tools.files.file.impl.SimpleFile) IOException(java.io.IOException) BootException(com.github.mgramin.sqlboot.exceptions.BootException) File(com.github.mgramin.sqlboot.tools.files.file.File) SimpleFile(com.github.mgramin.sqlboot.tools.files.file.impl.SimpleFile)

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