Search in sources :

Example 1 with Driver

use of org.apache.calcite.jdbc.Driver in project calcite by apache.

the class JdbcTest method testStatementCloseOnCompletion.

/**
 * Tests {@link java.sql.Statement}.{@code closeOnCompletion()}.
 */
@Test
public void testStatementCloseOnCompletion() throws Exception {
    String javaVersion = System.getProperty("java.version");
    if (javaVersion.compareTo("1.7") < 0) {
        // Statement.closeOnCompletion was introduced in JDK 1.7.
        return;
    }
    final Driver driver = new Driver();
    CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties());
    SchemaPlus rootSchema = connection.getRootSchema();
    rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
    connection.setSchema("hr");
    final Statement statement = connection.createStatement();
    assertFalse((Boolean) CalciteAssert.call(statement, "isCloseOnCompletion"));
    CalciteAssert.call(statement, "closeOnCompletion");
    assertTrue((Boolean) CalciteAssert.call(statement, "isCloseOnCompletion"));
    final ResultSet resultSet = statement.executeQuery("select * from \"emps\"");
    assertFalse(resultSet.isClosed());
    assertFalse(statement.isClosed());
    assertFalse(connection.isClosed());
    // when result set is closed, statement is closed automatically
    resultSet.close();
    assertTrue(resultSet.isClosed());
    assertTrue(statement.isClosed());
    assertFalse(connection.isClosed());
    connection.close();
    assertTrue(resultSet.isClosed());
    assertTrue(statement.isClosed());
    assertTrue(connection.isClosed());
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) Driver(org.apache.calcite.jdbc.Driver) org.hsqldb.jdbcDriver(org.hsqldb.jdbcDriver) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 2 with Driver

use of org.apache.calcite.jdbc.Driver in project calcite by apache.

the class ReflectiveSchemaTest method testReflectiveSchemaInUnnamedPackage.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-281">[CALCITE-1919]
 * NPE when target in ReflectiveSchema belongs to the unnamed package</a>.
 */
@Test
public void testReflectiveSchemaInUnnamedPackage() throws Exception {
    final Driver driver = new Driver();
    try (CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties())) {
        SchemaPlus rootSchema = connection.getRootSchema();
        final Class<?> c = Class.forName("RootHr");
        final Object o = c.getDeclaredConstructor().newInstance();
        rootSchema.add("hr", new ReflectiveSchema(o));
        connection.setSchema("hr");
        final Statement statement = connection.createStatement();
        final String sql = "select * from \"emps\"";
        final ResultSet resultSet = statement.executeQuery(sql);
        final String expected = "empid=100; name=Bill\n" + "empid=200; name=Eric\n" + "empid=150; name=Sebastian\n";
        assertThat(CalciteAssert.toString(resultSet), is(expected));
    }
}
Also used : Statement(java.sql.Statement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) Driver(org.apache.calcite.jdbc.Driver) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 3 with Driver

use of org.apache.calcite.jdbc.Driver in project calcite by apache.

the class JdbcTest method testAggMultipleMeasures.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1097">[CALCITE-1097]
 * Exception when executing query with too many aggregation columns</a>.
 */
@Test
public void testAggMultipleMeasures() throws SQLException {
    final Driver driver = new Driver();
    CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties());
    SchemaPlus rootSchema = connection.getRootSchema();
    rootSchema.add("sale", new ReflectiveSchema(new Smalls.WideSaleSchema()));
    connection.setSchema("sale");
    final Statement statement = connection.createStatement();
    // 200 columns: sum(sale0) + ... sum(sale199)
    ResultSet resultSet = statement.executeQuery("select s.\"prodId\"" + sums(200, true) + "\n" + "from \"sale\".\"prod\" as s group by s.\"prodId\"\n");
    assertThat(resultSet.next(), is(true));
    assertThat(resultSet.getInt(1), is(100));
    assertThat(resultSet.getInt(2), is(10));
    assertThat(resultSet.getInt(200), is(10));
    assertThat(resultSet.next(), is(false));
    // 800 columns:
    // sum(sale0 + 0) + ... + sum(sale0 + 100) + ... sum(sale99 + 799)
    final int n = 800;
    resultSet = statement.executeQuery("select s.\"prodId\"" + sums(n, false) + "\n" + "from \"sale\".\"prod\" as s group by s.\"prodId\"\n");
    assertThat(resultSet.next(), is(true));
    assertThat(resultSet.getInt(1), is(100));
    assertThat(resultSet.getInt(2), is(10));
    assertThat(resultSet.getInt(n), is(n + 8));
    assertThat(resultSet.next(), is(false));
    connection.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) Driver(org.apache.calcite.jdbc.Driver) org.hsqldb.jdbcDriver(org.hsqldb.jdbcDriver) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Aggregations

ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 Properties (java.util.Properties)3 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)3 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)3 Driver (org.apache.calcite.jdbc.Driver)3 SchemaPlus (org.apache.calcite.schema.SchemaPlus)3 Test (org.junit.Test)3 PreparedStatement (java.sql.PreparedStatement)2 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)2 org.hsqldb.jdbcDriver (org.hsqldb.jdbcDriver)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1