Search in sources :

Example 11 with DbUri

use of com.github.mgramin.sqlboot.model.uri.impl.DbUri 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 12 with DbUri

use of com.github.mgramin.sqlboot.model.uri.impl.DbUri in project sql-boot by sql-boot.

the class SchemaJdbcResourceTypeTest method read.

@Test
public void read() {
    final ResourceType schema = new SchemaJdbcResourceType(dataSource);
    final Stream<DbResource> schemas = schema.read(new SqlPlaceholdersWrapper(new DbUri("schema", asList("*"))));
    assertEquals(3, schemas.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 13 with DbUri

use of com.github.mgramin.sqlboot.model.uri.impl.DbUri in project sql-boot by sql-boot.

the class PageWrapperTest method read.

@Test
public void read() {
    assertEquals("persons", type.read(new DbUri("table/hr?page=1:1")).findAny().get().name());
    assertEquals("users", type.read(new DbUri("table/hr?page=2:1")).findAny().get().name());
    assertEquals("jobs", type.read(new DbUri("table/hr?page=3:1")).findAny().get().name());
    assertEquals(1, type.read(new DbUri("table/hr?page=1:1")).count());
    assertEquals(2, type.read(new DbUri("table/hr?page=1:2")).count());
    assertEquals(3, type.read(new DbUri("table/hr?page=1:3")).count());
    assertEquals(3, type.read(new DbUri("table/hr?page=1")).count());
    assertEquals(0, type.read(new DbUri("table/hr?page=2")).count());
}
Also used : DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) Test(org.junit.Test)

Example 14 with DbUri

use of com.github.mgramin.sqlboot.model.uri.impl.DbUri in project sql-boot by sql-boot.

the class WhereWrapperTest method read.

@Test
public void read() throws Exception {
    assertEquals(1, type.read(new DbUri("table/hr.persons")).count());
    assertEquals(3, type.read(new DbUri("table/hr.s")).count());
}
Also used : DbUri(com.github.mgramin.sqlboot.model.uri.impl.DbUri) Test(org.junit.Test)

Example 15 with DbUri

use of com.github.mgramin.sqlboot.model.uri.impl.DbUri 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)

Aggregations

DbUri (com.github.mgramin.sqlboot.model.uri.impl.DbUri)33 DbResource (com.github.mgramin.sqlboot.model.resource.DbResource)25 Test (org.junit.Test)18 ResourceType (com.github.mgramin.sqlboot.model.resource_type.ResourceType)16 SqlPlaceholdersWrapper (com.github.mgramin.sqlboot.model.uri.wrappers.SqlPlaceholdersWrapper)13 BootException (com.github.mgramin.sqlboot.exceptions.BootException)12 DbResourceImpl (com.github.mgramin.sqlboot.model.resource.impl.DbResourceImpl)12 LinkedHashMap (java.util.LinkedHashMap)12 ResultSet (java.sql.ResultSet)11 ResultSetMetaData (java.sql.ResultSetMetaData)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 TableJdbcResourceType (com.github.mgramin.sqlboot.model.resource_type.impl.jdbc.schema.table.TableJdbcResourceType)5 Uri (com.github.mgramin.sqlboot.model.uri.Uri)4 FsResourceTypes (com.github.mgramin.sqlboot.model.resource_type.impl.composite.FsResourceTypes)2 WhereWrapper (com.github.mgramin.sqlboot.model.resource_type.wrappers.list.WhereWrapper)2 JdbcSqlQuery (com.github.mgramin.sqlboot.sql.impl.JdbcSqlQuery)2 GroovyTemplateGenerator (com.github.mgramin.sqlboot.template.generator.impl.GroovyTemplateGenerator)2 Connection (java.sql.Connection)2 Map (java.util.Map)2