Search in sources :

Example 91 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project phoenix by apache.

the class QueryDatabaseMetaDataIT method testMultiTableColumnsMetadataScan.

@Test
public void testMultiTableColumnsMetadataScan() throws SQLException {
    long ts = nextTimestamp();
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts));
    Connection conn = DriverManager.getConnection(getUrl(), props);
    createGroupByTestTable(conn, GROUPBYTEST_NAME);
    ensureTableCreated(getUrl(), MDTEST_NAME, MDTEST_NAME, ts);
    ensureTableCreated(getUrl(), PTSDB_NAME, PTSDB_NAME, ts);
    ensureTableCreated(getUrl(), CUSTOM_ENTITY_DATA_FULL_NAME, CUSTOM_ENTITY_DATA_FULL_NAME, ts);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 5));
    conn = DriverManager.getConnection(getUrl(), props);
    DatabaseMetaData dbmd = conn.getMetaData();
    ResultSet rs = dbmd.getColumns(null, "", "%TEST%", null);
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), GROUPBYTEST_NAME);
    assertEquals(null, rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("id"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), GROUPBYTEST_NAME);
    assertEquals(PhoenixDatabaseMetaData.TABLE_FAMILY, rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("uri"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), GROUPBYTEST_NAME);
    assertEquals(PhoenixDatabaseMetaData.TABLE_FAMILY, rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("appcpu"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), MDTEST_NAME);
    assertEquals(null, rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("id"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), MDTEST_NAME);
    assertEquals(SchemaUtil.normalizeIdentifier("a"), rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("col1"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), MDTEST_NAME);
    assertEquals(SchemaUtil.normalizeIdentifier("b"), rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("col2"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), MDTEST_NAME);
    assertEquals(SchemaUtil.normalizeIdentifier("b"), rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("col3"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), MDTEST_NAME);
    assertEquals(SchemaUtil.normalizeIdentifier("b"), rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("col4"), rs.getString("COLUMN_NAME"));
    assertTrue(rs.next());
    assertEquals(rs.getString("TABLE_SCHEM"), null);
    assertEquals(rs.getString("TABLE_NAME"), MDTEST_NAME);
    assertEquals(SchemaUtil.normalizeIdentifier("b"), rs.getString("COLUMN_FAMILY"));
    assertEquals(SchemaUtil.normalizeIdentifier("col5"), rs.getString("COLUMN_NAME"));
    assertFalse(rs.next());
}
Also used : Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) PhoenixDatabaseMetaData(org.apache.phoenix.jdbc.PhoenixDatabaseMetaData) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 92 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project lucene-solr by apache.

the class JdbcTest method testJDBCMethods.

private void testJDBCMethods(String collection, String connectionString, Properties properties, String sql) throws Exception {
    try (Connection con = DriverManager.getConnection(connectionString, properties)) {
        assertTrue(con.isValid(DEFAULT_CONNECTION_TIMEOUT));
        assertEquals(zkHost, con.getCatalog());
        con.setCatalog(zkHost);
        assertEquals(zkHost, con.getCatalog());
        assertEquals(null, con.getSchema());
        con.setSchema("myschema");
        assertEquals(null, con.getSchema());
        DatabaseMetaData databaseMetaData = con.getMetaData();
        assertNotNull(databaseMetaData);
        assertEquals(con, databaseMetaData.getConnection());
        assertEquals(connectionString, databaseMetaData.getURL());
        assertEquals(4, databaseMetaData.getJDBCMajorVersion());
        assertEquals(0, databaseMetaData.getJDBCMinorVersion());
        assertEquals("Apache Solr", databaseMetaData.getDatabaseProductName());
        // The following tests require package information that is not available when running via Maven
        //      assertEquals(this.getClass().getPackage().getSpecificationVersion(), databaseMetaData.getDatabaseProductVersion());
        //      assertEquals(0, databaseMetaData.getDatabaseMajorVersion());
        //      assertEquals(0, databaseMetaData.getDatabaseMinorVersion());
        //      assertEquals(this.getClass().getPackage().getSpecificationTitle(), databaseMetaData.getDriverName());
        //      assertEquals(this.getClass().getPackage().getSpecificationVersion(), databaseMetaData.getDriverVersion());
        //      assertEquals(0, databaseMetaData.getDriverMajorVersion());
        //      assertEquals(0, databaseMetaData.getDriverMinorVersion());
        List<String> tableSchemas = new ArrayList<>(Arrays.asList(zkHost, "metadata"));
        try (ResultSet rs = databaseMetaData.getSchemas()) {
            assertTrue(rs.next());
            assertTrue(tableSchemas.contains(rs.getString("tableSchem")));
            tableSchemas.remove(rs.getString("tableSchem"));
            assertNull(rs.getString("tableCat"));
            assertTrue(rs.next());
            assertTrue(tableSchemas.contains(rs.getString("tableSchem")));
            tableSchemas.remove(rs.getString("tableSchem"));
            assertNull(rs.getString("tableCat"));
            assertFalse(rs.next());
            assertTrue(tableSchemas.isEmpty());
        }
        try (ResultSet rs = databaseMetaData.getCatalogs()) {
            assertTrue(rs.next());
            assertNull(rs.getString("tableCat"));
            assertFalse(rs.next());
        }
        CloudSolrClient solrClient = cluster.getSolrClient();
        solrClient.connect();
        ZkStateReader zkStateReader = solrClient.getZkStateReader();
        SortedSet<String> tables = new TreeSet<>();
        Set<String> collectionsSet = zkStateReader.getClusterState().getCollectionsMap().keySet();
        tables.addAll(collectionsSet);
        Aliases aliases = zkStateReader.getAliases();
        if (aliases != null) {
            Map<String, String> collectionAliasMap = aliases.getCollectionAliasMap();
            if (collectionAliasMap != null) {
                Set<String> aliasesSet = collectionAliasMap.keySet();
                tables.addAll(aliasesSet);
            }
        }
        try (ResultSet rs = databaseMetaData.getTables(null, zkHost, "%", null)) {
            for (String table : tables) {
                assertTrue(rs.next());
                assertNull(rs.getString("tableCat"));
                assertEquals(zkHost, rs.getString("tableSchem"));
                assertEquals(table, rs.getString("tableName"));
                assertEquals("TABLE", rs.getString("tableType"));
                assertNull(rs.getString("remarks"));
            }
            assertFalse(rs.next());
        }
        assertTrue(con.isReadOnly());
        con.setReadOnly(true);
        assertTrue(con.isReadOnly());
        assertNull(con.getWarnings());
        con.clearWarnings();
        assertNull(con.getWarnings());
        try (Statement statement = con.createStatement()) {
            checkStatement(con, statement);
            try (ResultSet rs = statement.executeQuery(sql)) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertTrue(statement.execute(sql));
            assertEquals(-1, statement.getUpdateCount());
            try (ResultSet rs = statement.getResultSet()) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertFalse(statement.getMoreResults());
        }
        try (PreparedStatement statement = con.prepareStatement(sql)) {
            checkStatement(con, statement);
            try (ResultSet rs = statement.executeQuery()) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertTrue(statement.execute());
            assertEquals(-1, statement.getUpdateCount());
            try (ResultSet rs = statement.getResultSet()) {
                assertEquals(statement, rs.getStatement());
                checkResultSetMetadata(rs);
                checkResultSet(rs);
            }
            assertFalse(statement.getMoreResults());
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Aliases(org.apache.solr.common.cloud.Aliases) DatabaseMetaData(java.sql.DatabaseMetaData) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) TreeSet(java.util.TreeSet) ResultSet(java.sql.ResultSet)

Example 93 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project phoenix by apache.

the class BaseTest method deletePriorTables.

private static void deletePriorTables(long ts, Connection globalConn, String url) throws Exception {
    DatabaseMetaData dbmd = globalConn.getMetaData();
    // Drop VIEWs first, as we don't allow a TABLE with views to be dropped
    // Tables are sorted by TENANT_ID
    List<String[]> tableTypesList = Arrays.asList(new String[] { PTableType.VIEW.toString() }, new String[] { PTableType.TABLE.toString() });
    for (String[] tableTypes : tableTypesList) {
        ResultSet rs = dbmd.getTables(null, null, null, tableTypes);
        String lastTenantId = null;
        Connection conn = globalConn;
        while (rs.next()) {
            String fullTableName = SchemaUtil.getEscapedTableName(rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM), rs.getString(PhoenixDatabaseMetaData.TABLE_NAME));
            String ddl = "DROP " + rs.getString(PhoenixDatabaseMetaData.TABLE_TYPE) + " " + fullTableName;
            String tenantId = rs.getString(1);
            if (tenantId != null && !tenantId.equals(lastTenantId)) {
                if (lastTenantId != null) {
                    conn.close();
                }
                // Open tenant-specific connection when we find a new one
                Properties props = PropertiesUtil.deepCopy(globalConn.getClientInfo());
                props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
                conn = DriverManager.getConnection(url, props);
                lastTenantId = tenantId;
            }
            try {
                conn.createStatement().executeUpdate(ddl);
            } catch (NewerTableAlreadyExistsException ex) {
                logger.info("Newer table " + fullTableName + " or its delete marker exists. Ignore current deletion");
            } catch (TableNotFoundException ex) {
                logger.info("Table " + fullTableName + " is already deleted.");
            }
        }
        rs.close();
        if (lastTenantId != null) {
            conn.close();
        }
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) ResultSet(java.sql.ResultSet) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) PhoenixDatabaseMetaData(org.apache.phoenix.jdbc.PhoenixDatabaseMetaData) Properties(java.util.Properties) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException)

Example 94 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project phoenix by apache.

the class BaseTest method deletePriorSchemas.

private static void deletePriorSchemas(long ts, String url) throws Exception {
    Properties props = new Properties();
    props.put(QueryServices.QUEUE_SIZE_ATTRIB, Integer.toString(1024));
    if (ts != HConstants.LATEST_TIMESTAMP) {
        props.setProperty(CURRENT_SCN_ATTRIB, Long.toString(ts));
    }
    try (Connection conn = DriverManager.getConnection(url, props)) {
        DatabaseMetaData dbmd = conn.getMetaData();
        ResultSet rs = dbmd.getSchemas();
        while (rs.next()) {
            String schemaName = rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM);
            if (schemaName.equals(PhoenixDatabaseMetaData.SYSTEM_SCHEMA_NAME)) {
                continue;
            }
            schemaName = SchemaUtil.getEscapedArgument(schemaName);
            String ddl = "DROP SCHEMA " + schemaName;
            conn.createStatement().executeUpdate(ddl);
        }
        rs.close();
    }
    // Make sure all schemas have been dropped
    props.remove(CURRENT_SCN_ATTRIB);
    try (Connection seeLatestConn = DriverManager.getConnection(url, props)) {
        DatabaseMetaData dbmd = seeLatestConn.getMetaData();
        ResultSet rs = dbmd.getSchemas();
        boolean hasSchemas = rs.next();
        if (hasSchemas) {
            String schemaName = rs.getString(PhoenixDatabaseMetaData.TABLE_SCHEM);
            if (schemaName.equals(PhoenixDatabaseMetaData.SYSTEM_SCHEMA_NAME)) {
                hasSchemas = rs.next();
            }
        }
        if (hasSchemas) {
            fail("The following schemas are not dropped that should be:" + getSchemaNames(rs));
        }
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData) PhoenixDatabaseMetaData(org.apache.phoenix.jdbc.PhoenixDatabaseMetaData)

Example 95 with DatabaseMetaData

use of java.sql.DatabaseMetaData in project phoenix by apache.

the class QueryDatabaseMetaDataIT method testSequenceMetadataScan.

@Test
public void testSequenceMetadataScan() throws SQLException {
    long ts = nextTimestamp();
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    Connection conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("CREATE SEQUENCE b.s1");
    conn.createStatement().execute("CREATE SEQUENCE a.s2");
    conn.createStatement().execute("CREATE SEQUENCE b.s3");
    conn.createStatement().execute("CREATE SEQUENCE c.s1");
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 20));
    conn = DriverManager.getConnection(getUrl(), props);
    DatabaseMetaData dbmd = conn.getMetaData();
    ResultSet rs = dbmd.getTables(null, null, null, new String[] { "FOO" });
    assertFalse(rs.next());
    rs = dbmd.getTables(null, null, null, new String[] { "FOO", PhoenixDatabaseMetaData.SEQUENCE_TABLE_TYPE });
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("A", rs.getString("TABLE_SCHEM"));
    assertEquals("S2", rs.getString("TABLE_NAME"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("B", rs.getString("TABLE_SCHEM"));
    assertEquals("S1", rs.getString("TABLE_NAME"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("B", rs.getString("TABLE_SCHEM"));
    assertEquals("S3", rs.getString("TABLE_NAME"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("C", rs.getString("TABLE_SCHEM"));
    assertEquals("S1", rs.getString("TABLE_NAME"));
    assertFalse(rs.next());
    conn.createStatement().execute("CREATE TABLE foo (k bigint primary key)");
    conn.createStatement().execute("CREATE TABLE z.bas (k bigint primary key)");
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
    conn = DriverManager.getConnection(getUrl(), props);
    dbmd = conn.getMetaData();
    rs = dbmd.getTables(null, null, null, new String[] { PTableType.TABLE.toString(), PhoenixDatabaseMetaData.SEQUENCE_TABLE_TYPE });
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("A", rs.getString("TABLE_SCHEM"));
    assertEquals("S2", rs.getString("TABLE_NAME"));
    assertEquals(PhoenixDatabaseMetaData.SEQUENCE_TABLE_TYPE, rs.getString("TABLE_TYPE"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("B", rs.getString("TABLE_SCHEM"));
    assertEquals("S1", rs.getString("TABLE_NAME"));
    assertEquals(PhoenixDatabaseMetaData.SEQUENCE_TABLE_TYPE, rs.getString("TABLE_TYPE"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("B", rs.getString("TABLE_SCHEM"));
    assertEquals("S3", rs.getString("TABLE_NAME"));
    assertEquals(PhoenixDatabaseMetaData.SEQUENCE_TABLE_TYPE, rs.getString("TABLE_TYPE"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("C", rs.getString("TABLE_SCHEM"));
    assertEquals("S1", rs.getString("TABLE_NAME"));
    assertEquals(PhoenixDatabaseMetaData.SEQUENCE_TABLE_TYPE, rs.getString("TABLE_TYPE"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertNull(rs.getString("TABLE_SCHEM"));
    assertEquals("FOO", rs.getString("TABLE_NAME"));
    assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
    assertTrue(rs.next());
    assertNull(rs.getString("TABLE_CAT"));
    assertEquals("Z", rs.getString("TABLE_SCHEM"));
    assertEquals("BAS", rs.getString("TABLE_NAME"));
    assertEquals(PTableType.TABLE.toString(), rs.getString("TABLE_TYPE"));
    assertFalse(rs.next());
}
Also used : Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) PhoenixDatabaseMetaData(org.apache.phoenix.jdbc.PhoenixDatabaseMetaData) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)360 ResultSet (java.sql.ResultSet)215 Connection (java.sql.Connection)169 SQLException (java.sql.SQLException)154 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