Search in sources :

Example 1 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 2 with AvaticaConnection

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

the class RemoteMetaTest method testRemoteStatementInsert.

@Test
public void testRemoteStatementInsert() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try {
        final String t = AvaticaUtils.unique("TEST_TABLE2");
        AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        Statement statement = conn.createStatement();
        final String create = String.format(Locale.ROOT, "create table if not exists %s (" + "  id int not null, msg varchar(255) not null)", t);
        int status = statement.executeUpdate(create);
        assertEquals(status, 0);
        statement = conn.createStatement();
        final String update = String.format(Locale.ROOT, "insert into %s values ('%d', '%s')", t, RANDOM.nextInt(Integer.MAX_VALUE), UUID.randomUUID());
        status = statement.executeUpdate(update);
        assertEquals(status, 1);
    } 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) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Example 3 with AvaticaConnection

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

the class RemoteMetaTest method testRemoteExecuteMaxRowCount.

@Test
public void testRemoteExecuteMaxRowCount() throws Exception {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
        final AvaticaStatement statement = conn.createStatement();
        prepareAndExecuteInternal(conn, statement, "select * from (values ('a', 1), ('b', 2))", 0);
        ResultSet rs = statement.getResultSet();
        int count = 0;
        while (rs.next()) {
            count++;
        }
        assertEquals("Check maxRowCount=0 and ResultSets is 0 row", count, 0);
        assertEquals("Check result set meta is still there", rs.getMetaData().getColumnCount(), 2);
        rs.close();
        statement.close();
        conn.close();
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ResultSet(java.sql.ResultSet) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Test(org.junit.Test)

Example 4 with AvaticaConnection

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

the class RemoteMetaTest method testArrays.

@Test
public void testArrays() throws SQLException {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        Statement stmt = conn.createStatement()) {
        ResultSet resultSet = stmt.executeQuery("select * from (values ('a', array['b', 'c']));");
        assertTrue(resultSet.next());
        assertEquals("a", resultSet.getString(1));
        Array arr = resultSet.getArray(2);
        assertTrue(arr instanceof ArrayImpl);
        Object[] values = (Object[]) ((ArrayImpl) arr).getArray();
        assertArrayEquals(new String[] { "b", "c" }, values);
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : Array(java.sql.Array) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) PreparedStatement(java.sql.PreparedStatement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 5 with AvaticaConnection

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

the class RemoteMetaTest 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) StringContains.containsString(org.hamcrest.core.StringContains.containsString) 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