Search in sources :

Example 1 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 FsResourceTypes method walk.

/**
 * @param path
 * @return
 */
private List<ResourceType> walk(final String path) {
    File[] files = new File(path).listFiles();
    if (files == null)
        return null;
    List<ResourceType> list = new ArrayList<>();
    for (File f : files) {
        if (f.isDirectory()) {
            File sqlFile = new File(f, "README.md");
            list.addAll(walk(f.getAbsolutePath()));
            final ResourceType jdbcResourceType;
            switch(f.getName()) {
                case "schema":
                    jdbcResourceType = new SchemaJdbcResourceType(dataSource);
                    break;
                case "table":
                    jdbcResourceType = new TableJdbcResourceType(dataSource);
                    break;
                case "child":
                    jdbcResourceType = new ChildTableJdbcResourceType(dataSource);
                    break;
                case "parent":
                    jdbcResourceType = new ParentTableJdbcResourceType(dataSource);
                    break;
                case "pk":
                    jdbcResourceType = new PkJdbcResourceType(dataSource);
                    break;
                case "index":
                    jdbcResourceType = new IndexJdbcResourceType(dataSource);
                    break;
                case "fk":
                    jdbcResourceType = new FkJdbcResourceType(dataSource);
                    break;
                case "view":
                    jdbcResourceType = new ViewJdbcResourceType(dataSource);
                    break;
                case "column":
                    jdbcResourceType = new ColumnJdbcResourceType(dataSource);
                    break;
                case "function":
                    jdbcResourceType = new FunctionJdbcResourceType(dataSource);
                    break;
                case "procedure":
                    jdbcResourceType = new ProcedureJdbcResourceType(dataSource);
                    break;
                default:
                    jdbcResourceType = null;
            }
            String sql = null;
            try {
                MarkdownFile markdownFile = new MarkdownFile(readFileToString(sqlFile, UTF_8));
                Map<String, String> parse = markdownFile.parse();
                Iterator<Map.Entry<String, String>> iterator = parse.entrySet().iterator();
                if (iterator.hasNext()) {
                    sql = iterator.next().getValue();
                }
            } catch (IOException e) {
            // TODO catch and process this exception
            }
            final ResourceType baseResourceType;
            if (sqlFile.exists() && sql != null) {
                baseResourceType = new SqlResourceType(new JdbcSqlQuery(dataSource, new GroovyTemplateGenerator(sql)), singletonList(f.getName()));
            } else if (jdbcResourceType != null) {
                baseResourceType = jdbcResourceType;
            } else {
                baseResourceType = null;
            }
            final ResourceType resourceType = new SelectWrapper(// new SqlBodyWrapper(
            new TemplateBodyWrapper(new PageWrapper(new LimitWrapper(// new WhereWrapper(
            baseResourceType)), new GroovyTemplateGenerator("EMPTY BODY ...")));
            if (baseResourceType != null) {
                list.add(resourceType);
            }
        }
    }
    return list;
}
Also used : SelectWrapper(com.github.mgramin.sqlboot.model.resource_type.wrappers.header.SelectWrapper) LimitWrapper(com.github.mgramin.sqlboot.model.resource_type.wrappers.list.LimitWrapper) SqlResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.sql.SqlResourceType) MarkdownFile(com.github.mgramin.sqlboot.model.resource_type.impl.composite.md.MarkdownFile) ArrayList(java.util.ArrayList) JdbcSqlQuery(com.github.mgramin.sqlboot.sql.impl.JdbcSqlQuery) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) ParentTableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ParentTableJdbcResourceType) PkJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.pk.PkJdbcResourceType) ProcedureJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.procedure.ProcedureJdbcResourceType) PageWrapper(com.github.mgramin.sqlboot.model.resource_type.wrappers.list.PageWrapper) TemplateBodyWrapper(com.github.mgramin.sqlboot.model.resource_type.wrappers.body.TemplateBodyWrapper) FunctionJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.function.FunctionJdbcResourceType) PkJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.pk.PkJdbcResourceType) SqlResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.sql.SqlResourceType) FunctionJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.function.FunctionJdbcResourceType) TableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType) ParentTableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ParentTableJdbcResourceType) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) SchemaJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.SchemaJdbcResourceType) FkJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.fk.FkJdbcResourceType) ViewJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.view.ViewJdbcResourceType) IndexJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.index.IndexJdbcResourceType) ColumnJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.column.ColumnJdbcResourceType) ChildTableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ChildTableJdbcResourceType) ProcedureJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.procedure.ProcedureJdbcResourceType) IOException(java.io.IOException) IndexJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.index.IndexJdbcResourceType) SchemaJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.SchemaJdbcResourceType) GroovyTemplateGenerator(com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerator) FkJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.fk.FkJdbcResourceType) TableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType) ParentTableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ParentTableJdbcResourceType) ChildTableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ChildTableJdbcResourceType) ViewJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.view.ViewJdbcResourceType) ChildTableJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.relation.ChildTableJdbcResourceType) MarkdownFile(com.github.mgramin.sqlboot.model.resource_type.impl.composite.md.MarkdownFile) File(java.io.File) ColumnJdbcResourceType(com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.column.ColumnJdbcResourceType)

Example 2 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 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 3 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 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 4 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 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 5 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 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

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