Search in sources :

Example 31 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project druid by druid-io.

the class DruidAvaticaHandlerTest method testDatabaseMetaDataColumns.

@Test
public void testDatabaseMetaDataColumns() throws Exception {
    final DatabaseMetaData metaData = client.getMetaData();
    Assert.assertEquals(ImmutableList.of(ROW(Pair.of("TABLE_SCHEM", "druid"), Pair.of("TABLE_NAME", "foo"), Pair.of("COLUMN_NAME", "__time"), Pair.of("DATA_TYPE", Types.TIMESTAMP), Pair.of("TYPE_NAME", "TIMESTAMP"), Pair.of("IS_NULLABLE", "NO")), ROW(Pair.of("TABLE_SCHEM", "druid"), Pair.of("TABLE_NAME", "foo"), Pair.of("COLUMN_NAME", "cnt"), Pair.of("DATA_TYPE", Types.BIGINT), Pair.of("TYPE_NAME", "BIGINT"), Pair.of("IS_NULLABLE", "NO")), ROW(Pair.of("TABLE_SCHEM", "druid"), Pair.of("TABLE_NAME", "foo"), Pair.of("COLUMN_NAME", "dim1"), Pair.of("DATA_TYPE", Types.VARCHAR), Pair.of("TYPE_NAME", "VARCHAR"), Pair.of("IS_NULLABLE", "NO")), ROW(Pair.of("TABLE_SCHEM", "druid"), Pair.of("TABLE_NAME", "foo"), Pair.of("COLUMN_NAME", "dim2"), Pair.of("DATA_TYPE", Types.VARCHAR), Pair.of("TYPE_NAME", "VARCHAR"), Pair.of("IS_NULLABLE", "NO")), ROW(Pair.of("TABLE_SCHEM", "druid"), Pair.of("TABLE_NAME", "foo"), Pair.of("COLUMN_NAME", "m1"), Pair.of("DATA_TYPE", Types.FLOAT), Pair.of("TYPE_NAME", "FLOAT"), Pair.of("IS_NULLABLE", "NO")), ROW(Pair.of("TABLE_SCHEM", "druid"), Pair.of("TABLE_NAME", "foo"), Pair.of("COLUMN_NAME", "unique_dim1"), Pair.of("DATA_TYPE", Types.OTHER), Pair.of("TYPE_NAME", "OTHER"), Pair.of("IS_NULLABLE", "NO"))), getRows(metaData.getColumns(null, "dr_id", "foo", null), ImmutableSet.of("IS_NULLABLE", "TABLE_NAME", "TABLE_SCHEM", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME")));
}
Also used : DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 32 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project druid by druid-io.

the class DruidAvaticaHandlerTest method testDatabaseMetaDataCatalogs.

@Test
public void testDatabaseMetaDataCatalogs() throws Exception {
    final DatabaseMetaData metaData = client.getMetaData();
    Assert.assertEquals(ImmutableList.of(ROW(Pair.of("TABLE_CAT", ""))), getRows(metaData.getCatalogs()));
}
Also used : DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 33 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project druid by druid-io.

the class DruidAvaticaHandlerTest method testDatabaseMetaDataSchemas.

@Test
public void testDatabaseMetaDataSchemas() throws Exception {
    final DatabaseMetaData metaData = client.getMetaData();
    Assert.assertEquals(ImmutableList.of(ROW(Pair.of("TABLE_CATALOG", ""), Pair.of("TABLE_SCHEM", "druid"))), getRows(metaData.getSchemas(null, "druid")));
}
Also used : DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 34 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project sharding-jdbc by dangdangdotcom.

the class ShardingDataSourceFactoryTest method createShardingRule.

private ShardingRule createShardingRule() throws SQLException {
    DataSource dataSource = mock(DataSource.class);
    Connection connection = mock(Connection.class);
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    when(dataSource.getConnection()).thenReturn(connection);
    when(connection.getMetaData()).thenReturn(databaseMetaData);
    when(databaseMetaData.getDatabaseProductName()).thenReturn("H2");
    Map<String, DataSource> dataSourceMap = new HashMap<>(1);
    dataSourceMap.put("ds", dataSource);
    DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
    TableRule tableRule = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(dataSourceRule).build();
    return ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Collections.singletonList(tableRule)).build();
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) HashMap(java.util.HashMap) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource)

Example 35 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project nutz by nutzam.

the class DaoSupport method setDataSource.

public void setDataSource(DataSource ds, boolean isLazy) {
    if (null != dataSource)
        if (log.isWarnEnabled())
            log.warn("Replaced a running dataSource!");
    dataSource = ds;
    if (expert == null)
        expert = Jdbcs.getExpert(ds);
    log.debug("select expert : " + expert.getClass().getName());
    pojoMaker = new NutPojoMaker(expert);
    meta = new DatabaseMeta();
    final Set<String> keywords = new HashSet<String>(Daos.sql2003Keywords());
    run(new ConnCallback() {

        public void invoke(Connection conn) throws Exception {
            try {
                DatabaseMetaData dmd = conn.getMetaData();
                meta.setProductName(dmd.getDatabaseProductName());
                meta.setVersion(dmd.getDatabaseProductVersion());
                log.debug("JDBC Driver --> " + dmd.getDriverVersion());
                log.debug("JDBC Name   --> " + dmd.getDriverName());
                if (!Strings.isBlank(dmd.getURL()))
                    log.debug("JDBC URL    --> " + dmd.getURL());
                if (dmd.getDriverName().contains("mariadb") || dmd.getDriverName().contains("sqlite")) {
                    log.warn("Auto-select fetch size to Integer.MIN_VALUE, enable for ResultSet Streaming");
                    SqlContext.DEFAULT_FETCH_SIZE = Integer.MIN_VALUE;
                }
                String tmp = dmd.getSQLKeywords();
                if (tmp != null) {
                    for (String keyword : tmp.split(",")) {
                        keywords.add(keyword.toUpperCase());
                    }
                }
                expert.checkDataSource(conn);
            } catch (Exception e) {
                log.info("something wrong when checking DataSource", e);
            }
        }
    });
    if (log.isDebugEnabled())
        log.debug("Database info --> " + meta);
    expert.setKeywords(keywords);
    if (!isLazy) {
        holder = new EntityHolder(this.expert, new Callback<ConnCallback>() {

            public void invoke(ConnCallback obj) {
                run(obj);
            }
        });
        holder.maker = createEntityMaker();
    }
    setRunner(runner);
    setExecutor(executor);
}
Also used : ConnCallback(org.nutz.dao.ConnCallback) Callback(org.nutz.lang.util.Callback) ConnCallback(org.nutz.dao.ConnCallback) NutPojoMaker(org.nutz.dao.impl.sql.NutPojoMaker) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DatabaseMeta(org.nutz.dao.DatabaseMeta) HashSet(java.util.HashSet)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)361 ResultSet (java.sql.ResultSet)216 Connection (java.sql.Connection)169 SQLException (java.sql.SQLException)155 Test (org.junit.Test)87 Statement (java.sql.Statement)49 ResultSetMetaData (java.sql.ResultSetMetaData)42 ArrayList (java.util.ArrayList)40 PreparedStatement (java.sql.PreparedStatement)38 IOException (java.io.IOException)26 Properties (java.util.Properties)25 HashSet (java.util.HashSet)16 PhoenixDatabaseMetaData (org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)16 HashMap (java.util.HashMap)15 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)15 PrintWriter (java.io.PrintWriter)10 Savepoint (java.sql.Savepoint)10 List (java.util.List)10 Map (java.util.Map)9 DataSource (javax.sql.DataSource)9