Search in sources :

Example 16 with ResultSetMetaData

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

the class cbo_rp_TestJdbcDriver2 method testMetaDataGetSchemas.

@Test
public void testMetaDataGetSchemas() throws SQLException {
    ResultSet rs = con.getMetaData().getSchemas();
    ResultSetMetaData resMeta = rs.getMetaData();
    assertEquals(2, resMeta.getColumnCount());
    assertEquals("TABLE_SCHEM", resMeta.getColumnName(1));
    assertEquals("TABLE_CATALOG", resMeta.getColumnName(2));
    assertTrue(rs.next());
    assertEquals("default", rs.getString(1));
    assertFalse(rs.next());
    rs.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 17 with ResultSetMetaData

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

the class cbo_rp_TestJdbcDriver2 method testExprCol.

/**
   * Verify selecting named expression columns
   * @throws SQLException
   */
@Test
public void testExprCol() throws SQLException {
    Statement stmt = con.createStatement();
    ResultSet res = stmt.executeQuery("select c1+1 as col1, length(c4) as len from " + dataTypeTableName + " where c1=1");
    ResultSetMetaData md = res.getMetaData();
    // only one result column
    assertEquals(md.getColumnCount(), 2);
    // verify the column name
    assertEquals(md.getColumnLabel(1), "col1");
    // verify the column name
    assertEquals(md.getColumnLabel(2), "len");
    assertTrue(res.next());
    assertEquals(res.getInt(1), 2);
    assertEquals(res.getInt(2), 1);
    res.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 18 with ResultSetMetaData

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

the class cbo_rp_TestJdbcDriver2 method testMetaDataGetColumnsMetaData.

/**
   * Validate the Metadata for the result set of a metadata getColumns call.
   */
@Test
public void testMetaDataGetColumnsMetaData() throws SQLException {
    ResultSet rs = con.getMetaData().getColumns(null, null, "testhivejdbcdriver\\_table", null);
    ResultSetMetaData rsmd = rs.getMetaData();
    assertEquals("TABLE_CAT", rsmd.getColumnName(1));
    assertEquals(Types.VARCHAR, rsmd.getColumnType(1));
    assertEquals(Integer.MAX_VALUE, rsmd.getColumnDisplaySize(1));
    assertEquals("ORDINAL_POSITION", rsmd.getColumnName(17));
    assertEquals(Types.INTEGER, rsmd.getColumnType(17));
    assertEquals(11, rsmd.getColumnDisplaySize(17));
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 19 with ResultSetMetaData

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

the class TestBufferedRows method setupMockData.

private void setupMockData() throws SQLException {
    // Mock BeeLine
    mockBeeline = mock(BeeLine.class);
    // Mock BeeLineOpts
    mockBeeLineOpts = mock(BeeLineOpts.class);
    when(mockBeeLineOpts.getMaxColumnWidth()).thenReturn(BeeLineOpts.DEFAULT_MAX_COLUMN_WIDTH);
    when(mockBeeLineOpts.getNumberFormat()).thenReturn("default");
    when(mockBeeLineOpts.getNullString()).thenReturn("NULL");
    when(mockBeeline.getOpts()).thenReturn(mockBeeLineOpts);
    // MockResultSet
    mockResultSet = mock(ResultSet.class);
    ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
    when(mockResultSetMetaData.getColumnCount()).thenReturn(2);
    when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("Key");
    when(mockResultSetMetaData.getColumnLabel(2)).thenReturn("Value");
    when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
    mockRow = new MockRow();
    // returns true as long as there is more data in mockResultData array
    when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {

        private int mockRowDataIndex = 0;

        public Boolean answer(InvocationOnMock invocation) {
            if (mockRowDataIndex < mockRowData.length) {
                mockRow.setCurrentRowData(mockRowData[mockRowDataIndex]);
                mockRowDataIndex++;
                return true;
            } else {
                return false;
            }
        }
    });
    when(mockResultSet.getString(Matchers.anyInt())).thenAnswer(new Answer<String>() {

        public String answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            int index = ((Integer) args[0]).intValue();
            return mockRow.getColumn(index);
        }
    });
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultSet(java.sql.ResultSet)

Example 20 with ResultSetMetaData

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

the class TestIncrementalRowsWithNormalization method testIncrementalRows.

@Test
public void testIncrementalRows() throws SQLException {
    Integer incrementalBufferRows = 5;
    // Mock BeeLineOpts
    BeeLineOpts mockBeeLineOpts = mock(BeeLineOpts.class);
    when(mockBeeLineOpts.getIncrementalBufferRows()).thenReturn(incrementalBufferRows);
    when(mockBeeLineOpts.getMaxColumnWidth()).thenReturn(BeeLineOpts.DEFAULT_MAX_COLUMN_WIDTH);
    when(mockBeeLineOpts.getNumberFormat()).thenReturn("default");
    when(mockBeeLineOpts.getNullString()).thenReturn("NULL");
    // Mock BeeLine
    BeeLine mockBeeline = mock(BeeLine.class);
    when(mockBeeline.getOpts()).thenReturn(mockBeeLineOpts);
    // MockResultSet
    ResultSet mockResultSet = mock(ResultSet.class);
    ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
    when(mockResultSetMetaData.getColumnCount()).thenReturn(1);
    when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("Mock Table");
    when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
    // First 10 calls to resultSet.next() should return true
    when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {

        private int iterations = 10;

        @Override
        public Boolean answer(InvocationOnMock invocation) {
            return this.iterations-- > 0;
        }
    });
    when(mockResultSet.getString(1)).thenReturn("Hello World");
    // IncrementalRows constructor should buffer the first "incrementalBufferRows" rows
    IncrementalRowsWithNormalization incrementalRowsWithNormalization = new IncrementalRowsWithNormalization(mockBeeline, mockResultSet);
    // When the first buffer is loaded ResultSet.next() should be called "incrementalBufferRows" times
    verify(mockResultSet, times(5)).next();
    // Iterating through the buffer should not cause the next buffer to be fetched
    for (int i = 0; i < incrementalBufferRows + 1; i++) {
        incrementalRowsWithNormalization.next();
    }
    verify(mockResultSet, times(5)).next();
    // When a new buffer is fetched ResultSet.next() should be called "incrementalBufferRows" more times
    incrementalRowsWithNormalization.next();
    verify(mockResultSet, times(10)).next();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) 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