Search in sources :

Example 11 with AvaticaConnection

use of org.apache.calcite.avatica.AvaticaConnection in project calcite-avatica by apache.

the class AlternatingRemoteMetaTest method testQuery.

@Test
public void testQuery() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        Statement statement = conn.createStatement()) {
        assertFalse(statement.execute("SET SCHEMA \"SCOTT\""));
        assertFalse(statement.execute("CREATE TABLE \"FOO\"(\"KEY\" INTEGER NOT NULL, \"VALUE\" VARCHAR(10))"));
        assertFalse(statement.execute("SET TABLE \"FOO\" READONLY FALSE"));
        final int numRecords = 1000;
        for (int i = 0; i < numRecords; i++) {
            assertFalse(statement.execute("INSERT INTO \"FOO\" VALUES(" + i + ", '" + i + "')"));
        }
        // Make sure all the records are there that we expect
        ResultSet results = statement.executeQuery("SELECT count(KEY) FROM FOO");
        assertTrue(results.next());
        assertEquals(1000, results.getInt(1));
        assertFalse(results.next());
        results = statement.executeQuery("SELECT KEY, VALUE FROM FOO ORDER BY KEY ASC");
        for (int i = 0; i < numRecords; i++) {
            assertTrue(results.next());
            assertEquals(i, results.getInt(1));
            assertEquals(Integer.toString(i), results.getString(2));
        }
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 12 with AvaticaConnection

use of org.apache.calcite.avatica.AvaticaConnection in project calcite-avatica by apache.

the class RemoteMetaTest method testExceptionPropagation.

@Test
public void testExceptionPropagation() throws Exception {
    final String sql = "SELECT * from EMP LIMIT FOOBARBAZ";
    ConnectionSpec.getDatabaseLock().lock();
    try (final AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        final Statement stmt = conn.createStatement()) {
        try {
            // invalid SQL
            stmt.execute(sql);
            fail("Expected an AvaticaSqlException");
        } catch (AvaticaSqlException e) {
            assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, e.getErrorCode());
            assertEquals(ErrorResponse.UNKNOWN_SQL_STATE, e.getSQLState());
            assertTrue("Message should contain original SQL, was '" + e.getMessage() + "'", e.getMessage().contains(sql));
            assertEquals(1, e.getStackTraces().size());
            final String stacktrace = e.getStackTraces().get(0);
            final String substring = "unexpected token: FOOBARBAZ";
            assertTrue("Message should contain '" + substring + "', was '" + e.getMessage() + ",", stacktrace.contains(substring));
        }
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) PreparedStatement(java.sql.PreparedStatement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) AvaticaSqlException(org.apache.calcite.avatica.AvaticaSqlException) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Example 13 with AvaticaConnection

use of org.apache.calcite.avatica.AvaticaConnection in project calcite-avatica by apache.

the class RemoteMetaTest method testRemoteConnectionClosing.

@Ignore("[CALCITE-942] AvaticaConnection should fail-fast when closed.")
@Test
public void testRemoteConnectionClosing() throws Exception {
    AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
    // Verify connection is usable
    conn.createStatement();
    conn.close();
    // After closing the connection, it should not be usable anymore
    try {
        conn.createStatement();
        fail("expected exception");
    } catch (SQLException e) {
        assertThat(e.getMessage(), containsString("Connection is closed"));
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) SQLException(java.sql.SQLException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with AvaticaConnection

use of org.apache.calcite.avatica.AvaticaConnection in project calcite-avatica by apache.

the class RemoteMetaTest method testRemoteConnectionProperties.

@Test
public void testRemoteConnectionProperties() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
        String id = conn.id;
        final Map<String, ConnectionPropertiesImpl> m = ((RemoteMeta) getMeta(conn)).propsMap;
        assertFalse("remote connection map should start ignorant", m.containsKey(id));
        // force creating a connection object on the remote side.
        try (final Statement stmt = conn.createStatement()) {
            assertTrue("creating a statement starts a local object.", m.containsKey(id));
            assertTrue(stmt.execute("select count(1) from EMP"));
        }
        Connection remoteConn = getConnection(FullyRemoteJdbcMetaFactory.getInstance(), id);
        final boolean defaultRO = remoteConn.isReadOnly();
        final boolean defaultAutoCommit = remoteConn.getAutoCommit();
        final String defaultCatalog = remoteConn.getCatalog();
        final String defaultSchema = remoteConn.getSchema();
        conn.setReadOnly(!defaultRO);
        assertTrue("local changes dirty local state", m.get(id).isDirty());
        assertEquals("remote connection has not been touched", defaultRO, remoteConn.isReadOnly());
        conn.setAutoCommit(!defaultAutoCommit);
        assertEquals("remote connection has not been touched", defaultAutoCommit, remoteConn.getAutoCommit());
        // further interaction with the connection will force a sync
        try (final Statement stmt = conn.createStatement()) {
            assertEquals(!defaultAutoCommit, remoteConn.getAutoCommit());
            assertFalse("local values should be clean", m.get(id).isDirty());
        }
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) PreparedStatement(java.sql.PreparedStatement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) HttpURLConnection(java.net.HttpURLConnection) Connection(java.sql.Connection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ConnectionPropertiesImpl(org.apache.calcite.avatica.ConnectionPropertiesImpl) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Example 15 with AvaticaConnection

use of org.apache.calcite.avatica.AvaticaConnection in project calcite-avatica by apache.

the class RemoteMetaTest method testCancel.

/** Test case for
   * <a href="https://issues.apache.org/jira/browse/CALCITE-1301">[CALCITE-1301]
   * Add cancel flag to AvaticaStatement</a>. */
@Test
public void testCancel() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
        final AvaticaStatement statement = conn.createStatement();
        final String sql = "select * from (values ('a', 1), ('b', 2))";
        final ResultSet rs = statement.executeQuery(sql);
        int count = 0;
        loop: for (; ; ) {
            switch(count++) {
                case 0:
                    assertThat(rs.next(), is(true));
                    break;
                case 1:
                    rs.getStatement().cancel();
                    try {
                        boolean x = rs.next();
                        fail("expected exception, got " + x);
                    } catch (SQLException e) {
                        assertThat(e.getMessage(), is("Statement canceled"));
                    }
                    break loop;
                default:
                    fail("count: " + count);
            }
        }
        assertThat(count, is(2));
        assertThat(statement.isClosed(), is(false));
        rs.close();
        assertThat(statement.isClosed(), is(false));
        statement.close();
        assertThat(statement.isClosed(), is(true));
        statement.close();
        assertThat(statement.isClosed(), is(true));
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) StringContains.containsString(org.hamcrest.core.StringContains.containsString) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Test(org.junit.Test)

Aggregations

AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)15 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)12 Test (org.junit.Test)12 ResultSet (java.sql.ResultSet)9 Statement (java.sql.Statement)7 StringContains.containsString (org.hamcrest.core.StringContains.containsString)6 PreparedStatement (java.sql.PreparedStatement)5 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ConnectionPropertiesImpl (org.apache.calcite.avatica.ConnectionPropertiesImpl)2 HttpURLConnection (java.net.HttpURLConnection)1 Array (java.sql.Array)1 AvaticaSqlException (org.apache.calcite.avatica.AvaticaSqlException)1 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)1 Ignore (org.junit.Ignore)1