Search in sources :

Example 6 with AvaticaConnection

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

the class AlternatingRemoteMetaTest 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) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ConnectionPropertiesImpl(org.apache.calcite.avatica.ConnectionPropertiesImpl) Test(org.junit.Test)

Example 7 with AvaticaConnection

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

the class RemoteMetaTest method testBigints.

@Test
public void testBigints() throws Exception {
    final String table = "TESTBIGINTS";
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        Statement stmt = conn.createStatement()) {
        assertFalse(stmt.execute("DROP TABLE IF EXISTS " + table));
        assertFalse(stmt.execute("CREATE TABLE " + table + " (id BIGINT)"));
        assertFalse(stmt.execute("INSERT INTO " + table + " values(10)"));
        ResultSet results = conn.getMetaData().getColumns(null, null, table, null);
        assertTrue(results.next());
        assertEquals(table, results.getString(3));
        // ordinal position
        assertEquals(1L, results.getLong(17));
    } 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) ResultSet(java.sql.ResultSet) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Example 8 with AvaticaConnection

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

the class RemoteMetaTest method testRemoteColumnsMeta.

@Test
public void testRemoteColumnsMeta() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try {
        // Verify all columns are retrieved, thus that frame-based fetching works correctly
        // for columns
        int rowCount = 0;
        try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
            ResultSet rs = conn.getMetaData().getColumns(null, null, null, null);
            while (rs.next()) {
                rowCount++;
            }
            rs.close();
            // The implicitly created statement should have been closed
            assertTrue(rs.getStatement().isClosed());
        }
        // default fetch size is 100, we are well beyond it
        assertTrue(rowCount > 900);
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 9 with AvaticaConnection

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

the class Driver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    AvaticaConnection conn = (AvaticaConnection) super.connect(url, info);
    if (conn == null) {
        // It's not an url for our driver
        return null;
    }
    Service service = conn.getService();
    // super.connect(...) should be creating a service and setting it in the AvaticaConnection
    assert null != service;
    service.apply(new Service.OpenConnectionRequest(conn.id, Service.OpenConnectionRequest.serializeProperties(info)));
    return conn;
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection)

Example 10 with AvaticaConnection

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

the class AlternatingRemoteMetaTest method checkLargeQuery.

private void checkLargeQuery(int n) throws Exception {
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
        final AvaticaStatement statement = conn.createStatement();
        final String frenchDisko = "It said human existence is pointless\n" + "As acts of rebellious solidarity\n" + "Can bring sense in this world\n" + "La resistance!\n";
        final String sql = "select '" + longString(frenchDisko, n) + "' as s from (values 'x')";
        prepareAndExecuteInternal(conn, statement, sql, -1);
        ResultSet rs = statement.getResultSet();
        int count = 0;
        while (rs.next()) {
            count++;
        }
        assertThat(count, is(1));
        rs.close();
        statement.close();
        conn.close();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ResultSet(java.sql.ResultSet) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement)

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