Search in sources :

Example 1 with ResourceType

use of com.github.mgramin.sqlboot.model.resource_type.ResourceType 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 ResourceType

use of com.github.mgramin.sqlboot.model.resource_type.ResourceType in project sql-boot by sql-boot.

the class ColumnJdbcResourceTypeTest method read.

@Test
public void read() {
    final ResourceType table = new ColumnJdbcResourceType(dataSource);
    final Stream<DbResource> tables = table.read(new SqlPlaceholdersWrapper(new DbUri("table", asList("MAIN_SCHEMA", "CITY"))));
    tables.forEach(v -> System.out.println(v.name()));
}
Also used : SqlPlaceholdersWrapper(com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) Test(org.junit.Test)

Example 3 with ResourceType

use of com.github.mgramin.sqlboot.model.resource_type.ResourceType in project sql-boot by sql-boot.

the class IndexJdbcResourceTypeTest method read.

@Test
public void read() {
    final ResourceType index = new IndexJdbcResourceType(dataSource);
    final Stream<DbResource> indexes = index.read(new SqlPlaceholdersWrapper(new DbUri("index", asList("*"))));
    assertEquals(5, indexes.count());
}
Also used : SqlPlaceholdersWrapper(com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) Test(org.junit.Test)

Example 4 with ResourceType

use of com.github.mgramin.sqlboot.model.resource_type.ResourceType in project sql-boot by sql-boot.

the class PkJdbcResourceTypeTest method read.

@Test
public void read() {
    final ResourceType pk = new PkJdbcResourceType(dataSource);
    final Stream<DbResource> primaryKeys = pk.read(new SqlPlaceholdersWrapper(new DbUri("pk", asList("*"))));
    assertEquals(2, primaryKeys.count());
}
Also used : SqlPlaceholdersWrapper(com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) Test(org.junit.Test)

Example 5 with ResourceType

use of com.github.mgramin.sqlboot.model.resource_type.ResourceType in project sql-boot by sql-boot.

the class ParentTableJdbcResourceTypeTest method read.

@Test
public void read() {
    final ResourceType pk = new ParentTableJdbcResourceType(dataSource);
    final Stream<DbResource> primaryKeys = pk.read(new SqlPlaceholdersWrapper(new DbUri("parent_table", asList("*"))));
    assertEquals(1, primaryKeys.count());
}
Also used : SqlPlaceholdersWrapper(com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper) DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) DbResource(com.github.mgramin.sqlboot.model.resource.DbResource) ResourceType(com.github.mgramin.sqlboot.model.resource_type.ResourceType) Test(org.junit.Test)

Aggregations

ResourceType (com.github.mgramin.sqlboot.model.resource_type.ResourceType)22 Test (org.junit.Test)18 DbUri (com.github.mgramin.sqlboot.model.uri.impl.DbUri)16 SqlPlaceholdersWrapper (com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper)14 DbResource (com.github.mgramin.sqlboot.model.resource.DbResource)13 JdbcSqlQuery (com.github.mgramin.sqlboot.sql.impl.JdbcSqlQuery)4 GroovyTemplateGenerator (com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerator)4 FsResourceTypes (com.github.mgramin.sqlboot.model.resource_type.impl.composite.FsResourceTypes)3 Uri (com.github.mgramin.sqlboot.model.uri.Uri)3 ProcedureJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.procedure.ProcedureJdbcResourceType)2 WhereWrapper (com.github.mgramin.sqlboot.model.resource_type.wrappers.list.WhereWrapper)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ResponseEntity (org.springframework.http.ResponseEntity)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 BootException (com.github.mgramin.sqlboot.exceptions.BootException)1 DbConnectionList (com.github.mgramin.sqlboot.model.connection.DbConnectionList)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