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();
}
}
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();
}
}
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"));
}
}
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();
}
}
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();
}
}
Aggregations