Search in sources :

Example 36 with ParameterMetaData

use of java.sql.ParameterMetaData in project calcite-avatica by apache.

the class RemoteDriverTest method testDecimalParameters.

@Test
public void testDecimalParameters() throws Exception {
    final String tableName = "decimalParameters";
    BigDecimal decimal = new BigDecimal("123451234512345");
    try (Connection conn = getLocalConnection();
        Statement stmt = conn.createStatement()) {
        assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
        String sql = "CREATE TABLE " + tableName + " (keycolumn VARCHAR(5), column1 DECIMAL(15,0))";
        assertFalse(stmt.execute(sql));
        // Insert a single decimal
        try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + tableName + " values (?, ?)")) {
            ParameterMetaData metadata = pstmt.getParameterMetaData();
            assertNotNull(metadata);
            assertEquals(5, metadata.getPrecision(1));
            assertEquals(0, metadata.getScale(1));
            assertEquals(15, metadata.getPrecision(2));
            assertEquals(0, metadata.getScale(2));
            pstmt.setString(1, "asdfg");
            pstmt.setBigDecimal(2, decimal);
            assertEquals(1, pstmt.executeUpdate());
        }
        ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
        assertNotNull(results);
        assertTrue(results.next());
        BigDecimal actualDecimal = results.getBigDecimal(2);
        assertEquals(decimal, actualDecimal);
        ResultSetMetaData resultMetadata = results.getMetaData();
        assertNotNull(resultMetadata);
        assertEquals(15, resultMetadata.getPrecision(2));
        assertEquals(0, resultMetadata.getScale(2));
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) BigDecimal(java.math.BigDecimal) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test)

Example 37 with ParameterMetaData

use of java.sql.ParameterMetaData in project calcite-avatica by apache.

the class RemoteDriverTest method checkStatementExecute.

private void checkStatementExecute(Connection connection, boolean prepare, int maxRowCount) throws SQLException {
    final String sql = "select * from (\n" + "  values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)";
    final Statement statement;
    final ResultSet resultSet;
    final ParameterMetaData parameterMetaData;
    if (prepare) {
        final PreparedStatement ps = connection.prepareStatement(sql);
        statement = ps;
        ps.setMaxRows(maxRowCount);
        parameterMetaData = ps.getParameterMetaData();
        assertTrue(ps.execute());
        resultSet = ps.getResultSet();
    } else {
        statement = connection.createStatement();
        statement.setMaxRows(maxRowCount);
        parameterMetaData = null;
        assertTrue(statement.execute(sql));
        resultSet = statement.getResultSet();
    }
    if (parameterMetaData != null) {
        assertThat(parameterMetaData.getParameterCount(), equalTo(0));
    }
    final ResultSetMetaData metaData = resultSet.getMetaData();
    assertEquals(2, metaData.getColumnCount());
    assertEquals("C1", metaData.getColumnName(1));
    assertEquals("C2", metaData.getColumnName(2));
    for (int i = 0; i < maxRowCount || (maxRowCount == 0 && i < 3); i++) {
        assertTrue(resultSet.next());
    }
    assertFalse(resultSet.next());
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ParameterMetaData(java.sql.ParameterMetaData)

Example 38 with ParameterMetaData

use of java.sql.ParameterMetaData in project calcite-avatica by apache.

the class RemoteDriverTest method checkStatementExecuteQuery.

private void checkStatementExecuteQuery(Connection connection, boolean prepare) throws SQLException {
    final String sql = "select * from (\n" + "  values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)";
    final Statement statement;
    final ResultSet resultSet;
    final ParameterMetaData parameterMetaData;
    if (prepare) {
        final PreparedStatement ps = connection.prepareStatement(sql);
        statement = ps;
        parameterMetaData = ps.getParameterMetaData();
        resultSet = ps.executeQuery();
    } else {
        statement = connection.createStatement();
        parameterMetaData = null;
        resultSet = statement.executeQuery(sql);
    }
    if (parameterMetaData != null) {
        assertThat(parameterMetaData.getParameterCount(), equalTo(0));
    }
    final ResultSetMetaData metaData = resultSet.getMetaData();
    assertEquals(2, metaData.getColumnCount());
    assertEquals("C1", metaData.getColumnName(1));
    assertEquals("C2", metaData.getColumnName(2));
    assertTrue(resultSet.next());
    assertTrue(resultSet.next());
    assertTrue(resultSet.next());
    assertFalse(resultSet.next());
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ParameterMetaData(java.sql.ParameterMetaData)

Example 39 with ParameterMetaData

use of java.sql.ParameterMetaData in project calcite-avatica by apache.

the class RemoteDriverTest method testSignedParameters.

@Test
public void testSignedParameters() throws Exception {
    final String tableName = "signedParameters";
    try (Connection conn = getLocalConnection();
        Statement stmt = conn.createStatement()) {
        assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
        String sql = "CREATE TABLE " + tableName + " (keycolumn VARCHAR(5), column1 integer)";
        assertFalse(stmt.execute(sql));
        // Insert a single decimal
        try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + tableName + " values (?, ?)")) {
            ParameterMetaData metadata = pstmt.getParameterMetaData();
            assertNotNull(metadata);
            assertFalse("Varchar should not be signed", metadata.isSigned(1));
            assertTrue("Integer should be signed", metadata.isSigned(2));
            pstmt.setString(1, "asdfg");
            pstmt.setInt(2, 10);
            assertEquals(1, pstmt.executeUpdate());
        }
        ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
        assertNotNull(results);
        assertTrue(results.next());
        assertEquals("asdfg", results.getString(1));
        assertEquals(10, results.getInt(2));
        ResultSetMetaData resultMetadata = results.getMetaData();
        assertNotNull(resultMetadata);
        assertFalse("Varchar should not be signed", resultMetadata.isSigned(1));
        assertTrue("Integer should be signed", resultMetadata.isSigned(2));
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test)

Example 40 with ParameterMetaData

use of java.sql.ParameterMetaData in project calcite-avatica by apache.

the class RemoteDriverTest method testDateParameterWithGMT0.

@Test
public void testDateParameterWithGMT0() throws Exception {
    final String tableName = "dateParameters";
    try (Connection conn = getLocalConnection();
        Statement stmt = conn.createStatement()) {
        assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName));
        String sql = "CREATE TABLE " + tableName + " (keycolumn VARCHAR(5), column1 date)";
        assertFalse(stmt.execute(sql));
        TimeZone tzUtc = TimeZone.getTimeZone("UTC");
        Calendar cUtc = Calendar.getInstance(tzUtc, Locale.ROOT);
        cUtc.set(Calendar.YEAR, 1970);
        cUtc.set(Calendar.MONTH, Calendar.JANUARY);
        cUtc.set(Calendar.DAY_OF_MONTH, 1);
        cUtc.set(Calendar.HOUR_OF_DAY, 0);
        cUtc.set(Calendar.MINUTE, 0);
        cUtc.set(Calendar.SECOND, 0);
        cUtc.set(Calendar.MILLISECOND, 0);
        Date inputDate = new Date(cUtc.getTimeInMillis());
        // Insert a single date
        try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + tableName + " values (?, ?)")) {
            ParameterMetaData metadata = pstmt.getParameterMetaData();
            assertNotNull(metadata);
            pstmt.setString(1, "asdfg");
            pstmt.setDate(2, inputDate, cUtc);
            assertEquals(1, pstmt.executeUpdate());
        }
        ResultSet results = stmt.executeQuery("SELECT * FROM " + tableName);
        assertNotNull(results);
        assertTrue(results.next());
        assertEquals("asdfg", results.getString(1));
        Date outputDate = results.getDate(2, cUtc);
        assertEquals(inputDate.getTime(), outputDate.getTime());
        assertEquals(0, outputDate.getTime());
    }
}
Also used : TimeZone(java.util.TimeZone) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Calendar(java.util.Calendar) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Date(java.sql.Date) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test)

Aggregations

ParameterMetaData (java.sql.ParameterMetaData)58 PreparedStatement (java.sql.PreparedStatement)49 Test (org.junit.Test)41 Connection (java.sql.Connection)39 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)31 ResultSet (java.sql.ResultSet)14 ResultSetMetaData (java.sql.ResultSetMetaData)10 Statement (java.sql.Statement)9 StringContains.containsString (org.hamcrest.core.StringContains.containsString)9 BigDecimal (java.math.BigDecimal)8 Date (java.sql.Date)6 SQLException (java.sql.SQLException)6 Calendar (java.util.Calendar)2 List (java.util.List)2 Set (java.util.Set)2 TimeZone (java.util.TimeZone)2 Scan (org.apache.hadoop.hbase.client.Scan)2 FilterAdapter (com.alibaba.druid.filter.FilterAdapter)1 FilterChain (com.alibaba.druid.filter.FilterChain)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1