Search in sources :

Example 26 with ResultSetMetaData

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

the class TestJdbcDriver2 method testMetaDataGetCatalogs.

@Test
public void testMetaDataGetCatalogs() throws SQLException {
    ResultSet rs = con.getMetaData().getCatalogs();
    ResultSetMetaData resMeta = rs.getMetaData();
    assertEquals(1, resMeta.getColumnCount());
    assertEquals("TABLE_CAT", resMeta.getColumnName(1));
    assertFalse(rs.next());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 27 with ResultSetMetaData

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

the class TestJdbcDriver2 method testBuiltInUDFCol.

/**
   * Verify selecting using builtin UDFs
   * @throws SQLException
   */
@Test
public void testBuiltInUDFCol() throws SQLException {
    Statement stmt = con.createStatement();
    ResultSet res = stmt.executeQuery("select c12, bin(c12) from " + dataTypeTableName + " where c1=1");
    ResultSetMetaData md = res.getMetaData();
    // only one result column
    assertEquals(md.getColumnCount(), 2);
    // verify the system generated column name
    assertEquals(md.getColumnLabel(2), "_c1");
    assertTrue(res.next());
    assertEquals(res.getLong(1), 1);
    assertEquals(res.getString(2), "1");
    res.close();
    stmt.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 28 with ResultSetMetaData

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

the class TestJdbcDriver2 method testIntervalTypes.

@Test
public void testIntervalTypes() throws Exception {
    Statement stmt = con.createStatement();
    // Since interval types not currently supported as table columns, need to create them
    // as expressions.
    ResultSet res = stmt.executeQuery("select case when c17 is null then null else interval '1' year end as col1," + " c17 -  c17 as col2 from " + dataTypeTableName);
    ResultSetMetaData meta = res.getMetaData();
    assertEquals("col1", meta.getColumnLabel(1));
    assertEquals(java.sql.Types.OTHER, meta.getColumnType(1));
    assertEquals("interval_year_month", meta.getColumnTypeName(1));
    assertEquals(11, meta.getColumnDisplaySize(1));
    assertEquals(11, meta.getPrecision(1));
    assertEquals(0, meta.getScale(1));
    assertEquals(HiveIntervalYearMonth.class.getName(), meta.getColumnClassName(1));
    assertEquals("col2", meta.getColumnLabel(2));
    assertEquals(java.sql.Types.OTHER, meta.getColumnType(2));
    assertEquals("interval_day_time", meta.getColumnTypeName(2));
    assertEquals(29, meta.getColumnDisplaySize(2));
    assertEquals(29, meta.getPrecision(2));
    assertEquals(0, meta.getScale(2));
    assertEquals(HiveIntervalDayTime.class.getName(), meta.getColumnClassName(2));
    // row 1 - results should be null
    assertTrue(res.next());
    // skip the last (partitioning) column since it is always non-null
    for (int i = 1; i < meta.getColumnCount(); i++) {
        assertNull("Column " + i + " should be null", res.getObject(i));
    }
    // row 2 - results should be null
    assertTrue(res.next());
    for (int i = 1; i < meta.getColumnCount(); i++) {
        assertNull("Column " + i + " should be null", res.getObject(i));
    }
    // row 3
    assertTrue(res.next());
    assertEquals("1-0", res.getString(1));
    assertEquals(1, ((HiveIntervalYearMonth) res.getObject(1)).getYears());
    assertEquals("0 00:00:00.000000000", res.getString(2));
    assertEquals(0, ((HiveIntervalDayTime) res.getObject(2)).getDays());
    stmt.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HiveIntervalYearMonth(org.apache.hadoop.hive.common.type.HiveIntervalYearMonth) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime) Test(org.junit.Test)

Example 29 with ResultSetMetaData

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

the class JDBCInterpreter method getResults.

private String getResults(ResultSet resultSet, boolean isTableType) throws SQLException {
    ResultSetMetaData md = resultSet.getMetaData();
    StringBuilder msg;
    if (isTableType) {
        msg = new StringBuilder(TABLE_MAGIC_TAG);
    } else {
        msg = new StringBuilder();
    }
    for (int i = 1; i < md.getColumnCount() + 1; i++) {
        if (i > 1) {
            msg.append(TAB);
        }
        msg.append(replaceReservedChars(md.getColumnName(i)));
    }
    msg.append(NEWLINE);
    int displayRowCount = 0;
    while (resultSet.next() && displayRowCount < getMaxResult()) {
        for (int i = 1; i < md.getColumnCount() + 1; i++) {
            Object resultObject;
            String resultValue;
            resultObject = resultSet.getObject(i);
            if (resultObject == null) {
                resultValue = "null";
            } else {
                resultValue = resultSet.getString(i);
            }
            msg.append(replaceReservedChars(resultValue));
            if (i != md.getColumnCount()) {
                msg.append(TAB);
            }
        }
        msg.append(NEWLINE);
        displayRowCount++;
    }
    return msg.toString();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 30 with ResultSetMetaData

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

the class IgniteSqlInterpreter method interpret.

@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
    if (connEx != null) {
        return new InterpreterResult(Code.ERROR, connEx.getMessage());
    }
    StringBuilder msg = new StringBuilder("%table ");
    try (Statement stmt = conn.createStatement()) {
        curStmt = stmt;
        try (ResultSet res = stmt.executeQuery(st)) {
            ResultSetMetaData md = res.getMetaData();
            for (int i = 1; i <= md.getColumnCount(); i++) {
                if (i > 1) {
                    msg.append('\t');
                }
                msg.append(md.getColumnName(i));
            }
            msg.append('\n');
            while (res.next()) {
                for (int i = 1; i <= md.getColumnCount(); i++) {
                    msg.append(res.getString(i));
                    if (i != md.getColumnCount()) {
                        msg.append('\t');
                    }
                }
                msg.append('\n');
            }
        }
    } catch (Exception e) {
        logger.error("Exception in IgniteSqlInterpreter while InterpreterResult interpret: ", e);
        return IgniteInterpreterUtils.buildErrorResult(e);
    } finally {
        curStmt = null;
    }
    return new InterpreterResult(Code.SUCCESS, msg.toString());
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) SQLException(java.sql.SQLException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException)

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