Search in sources :

Example 11 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TestJdbcDriver2 method testMetaDataGetColumns.

@Test
public void testMetaDataGetColumns() throws SQLException {
    Map<String[], Integer> tests = new HashMap<String[], Integer>();
    tests.put(new String[] { "testjdbcdrivertbl", null }, 2);
    tests.put(new String[] { "%jdbcdrivertbl", null }, 2);
    tests.put(new String[] { "%jdbcdrivertbl%", "under\\_col" }, 1);
    tests.put(new String[] { "%jdbcdrivertbl%", "under\\_co_" }, 1);
    tests.put(new String[] { "%jdbcdrivertbl%", "under_col" }, 1);
    tests.put(new String[] { "%jdbcdrivertbl%", "und%" }, 1);
    tests.put(new String[] { "%jdbcdrivertbl%", "%" }, 2);
    tests.put(new String[] { "%jdbcdrivertbl%", "_%" }, 2);
    for (String[] checkPattern : tests.keySet()) {
        ResultSet rs = con.getMetaData().getColumns(null, testDbName, checkPattern[0], checkPattern[1]);
        // validate the metadata for the getColumns result set
        ResultSetMetaData rsmd = rs.getMetaData();
        assertEquals("TABLE_CAT", rsmd.getColumnName(1));
        int cnt = 0;
        while (rs.next()) {
            String columnname = rs.getString("COLUMN_NAME");
            int ordinalPos = rs.getInt("ORDINAL_POSITION");
            switch(cnt) {
                case 0:
                    assertEquals("Wrong column name found", "under_col", columnname);
                    assertEquals("Wrong ordinal position found", ordinalPos, 1);
                    break;
                case 1:
                    assertEquals("Wrong column name found", "value", columnname);
                    assertEquals("Wrong ordinal position found", ordinalPos, 2);
                    break;
                default:
                    break;
            }
            cnt++;
        }
        rs.close();
        assertEquals("Found less columns then we test for.", tests.get(checkPattern).intValue(), cnt);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) String(java.lang.String) Test(org.junit.Test)

Example 12 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TestJdbcDriver2 method doTestSelectAll.

private void doTestSelectAll(String tableName, int maxRows, int fetchSize) throws Exception {
    boolean isPartitionTable = tableName.equals(partitionedTableName);
    Statement stmt = con.createStatement();
    if (maxRows >= 0) {
        stmt.setMaxRows(maxRows);
    }
    if (fetchSize > 0) {
        stmt.setFetchSize(fetchSize);
        assertEquals(fetchSize, stmt.getFetchSize());
    }
    // JDBC says that 0 means return all, which is the default
    int expectedMaxRows = maxRows < 1 ? 0 : maxRows;
    assertNotNull("Statement is null", stmt);
    assertEquals("Statement max rows not as expected", expectedMaxRows, stmt.getMaxRows());
    assertFalse("Statement should not be closed", stmt.isClosed());
    ResultSet res;
    // run some queries
    res = stmt.executeQuery("select * from " + tableName);
    assertNotNull("ResultSet is null", res);
    assertTrue("getResultSet() not returning expected ResultSet", res == stmt.getResultSet());
    assertEquals("get update count not as expected", -1, stmt.getUpdateCount());
    int i = 0;
    ResultSetMetaData meta = res.getMetaData();
    int expectedColCount = isPartitionTable ? 3 : 2;
    assertEquals("Unexpected column count", expectedColCount, meta.getColumnCount());
    boolean moreRow = res.next();
    while (moreRow) {
        try {
            i++;
            assertEquals(res.getInt(1), res.getInt(tableName + ".under_col"));
            assertEquals(res.getInt(1), res.getInt("under_col"));
            assertEquals(res.getString(1), res.getString(tableName + ".under_col"));
            assertEquals(res.getString(1), res.getString("under_col"));
            assertEquals(res.getString(2), res.getString(tableName + ".value"));
            assertEquals(res.getString(2), res.getString("value"));
            if (isPartitionTable) {
                assertEquals(res.getString(3), partitionedColumnValue);
                assertEquals(res.getString(3), res.getString(partitionedColumnName));
                assertEquals(res.getString(3), res.getString(tableName + "." + partitionedColumnName));
            }
            assertFalse("Last result value was not null", res.wasNull());
            assertNull("No warnings should be found on ResultSet", res.getWarnings());
            // verifying that method is supported
            res.clearWarnings();
            // System.out.println(res.getString(1) + " " + res.getString(2));
            assertEquals("getInt and getString don't align for the same result value", String.valueOf(res.getInt(1)), res.getString(1));
            assertEquals("Unexpected result found", "val_" + res.getString(1), res.getString(2));
            moreRow = res.next();
        } catch (SQLException e) {
            System.out.println(e.toString());
            e.printStackTrace();
            throw new Exception(e.toString());
        }
    }
    // supposed to get 500 rows if maxRows isn't set
    int expectedRowCount = maxRows > 0 ? maxRows : 500;
    assertEquals("Incorrect number of rows returned", expectedRowCount, i);
    // should have no more rows
    assertEquals(false, moreRow);
    assertNull("No warnings should be found on statement", stmt.getWarnings());
    // verifying that method is supported
    stmt.clearWarnings();
    assertNull("No warnings should be found on connection", con.getWarnings());
    // verifying that method is supported
    con.clearWarnings();
    stmt.close();
    assertTrue("Statement should be closed", stmt.isClosed());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLTimeoutException(java.sql.SQLTimeoutException) ParseException(java.text.ParseException) Exception(java.lang.Exception) SQLException(java.sql.SQLException) ExpectedException(org.junit.rules.ExpectedException)

Example 13 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TestJdbcDriver2 method testImportedKeys.

/**
   * test getImportedKeys()
   * @throws SQLException
   */
@Test
public void testImportedKeys() throws SQLException {
    DatabaseMetaData dbmd = con.getMetaData();
    assertNotNull(dbmd);
    // currently getImportedKeys always returns an empty resultset for Hive
    ResultSet res = dbmd.getImportedKeys(null, null, null);
    ResultSetMetaData md = res.getMetaData();
    assertEquals(md.getColumnCount(), 14);
    assertFalse(res.next());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 14 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TestJdbcDriver2 method testProcCols.

/**
   * test getProcedureColumns()
   * @throws SQLException
   */
@Test
public void testProcCols() throws SQLException {
    DatabaseMetaData dbmd = con.getMetaData();
    assertNotNull(dbmd);
    // currently getProcedureColumns always returns an empty resultset for Hive
    ResultSet res = dbmd.getProcedureColumns(null, null, null, null);
    ResultSetMetaData md = res.getMetaData();
    assertEquals(md.getColumnCount(), 20);
    assertFalse(res.next());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 15 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hive by apache.

the class TestJdbcDriver2 method testResultSetMetaDataDuplicateColumnNames.

@Test
public void testResultSetMetaDataDuplicateColumnNames() throws SQLException {
    Statement stmt = con.createStatement();
    ResultSet res = stmt.executeQuery("select c1 as c2_1, c2, c1*2 from " + dataTypeTableName + " limit 1");
    ResultSetMetaData meta = res.getMetaData();
    ResultSet colRS = con.getMetaData().getColumns(null, null, dataTypeTableName.toLowerCase(), null);
    assertEquals(3, meta.getColumnCount());
    assertTrue(colRS.next());
    assertEquals("c2_1", meta.getColumnName(1));
    assertTrue(colRS.next());
    assertEquals("c2", meta.getColumnName(2));
    assertTrue(colRS.next());
    assertEquals("_c2", meta.getColumnName(3));
    stmt.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)414 ResultSet (java.sql.ResultSet)262 Statement (java.sql.Statement)118 SQLException (java.sql.SQLException)111 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)95 Connection (java.sql.Connection)64 ArrayList (java.util.ArrayList)61 DatabaseMetaData (java.sql.DatabaseMetaData)40 HashMap (java.util.HashMap)40 Map (java.util.Map)28 List (java.util.List)17 LinkedHashMap (java.util.LinkedHashMap)15 IOException (java.io.IOException)14 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Test (org.testng.annotations.Test)9