@Test public void executePreparedCounterTest() throws Exception { PreparedStatement p = session.prepare("UPDATE " + COUNTER_TABLE + " SET c = c + ? WHERE k = ?"); session.execute(p.bind(1L, "row")); session.execute(p.bind(1L, "row")); ResultSet rs = session.execute("SELECT * FROM " + COUNTER_TABLE); List<Row> rows = rs.all(); assertEquals(1, rows.size()); assertEquals(2L, rows.get(0).getLong("c")); }
@Test public void executePreparedTest() throws Exception { // Simple calls to all versions of the execute/executeAsync methods for prepared statements // Note: the goal is only to exercice the Session methods, PreparedStatementTest have better // prepared statement tests. String key = "execute_prepared_test"; ResultSet rs = session.execute(String.format(INSERT_FORMAT, TABLE, key, "foo", 42, 24.03f)); assertTrue(rs.isExhausted()); PreparedStatement p = session.prepare(String.format(SELECT_ALL_FORMAT + " WHERE k = ?", TABLE)); BoundStatement bs = p.bind(key); // executePrepared checkExecuteResultSet(session.execute(bs), key); checkExecuteResultSet(session.execute(bs.setConsistencyLevel(ConsistencyLevel.ONE)), key); // executePreparedAsync checkExecuteResultSet(session.executeAsync(bs).getUninterruptibly(), key); checkExecuteResultSet( session.executeAsync(bs.setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); }
private PreparedStatement toPreparedStatement(String query, Connection.Future future) throws NoHostAvailableException { try { Message.Response response = null; try { while (response == null) { try { response = future.get(); } catch (InterruptedException e) { // TODO: decide wether we want to expose Interrupted exceptions or not throw new RuntimeException(e); } } } catch (ExecutionException e) { ResultSetFuture.extractCauseFromExecutionException(e); throw new AssertionError(); } assert response != null; switch (response.type) { case RESULT: ResultMessage rm = (ResultMessage) response; switch (rm.kind) { case PREPARED: ResultMessage.Prepared pmsg = (ResultMessage.Prepared) rm; manager.cluster.manager.prepare(pmsg.statementId, query, future.getAddress()); return PreparedStatement.fromMessage(pmsg, manager.cluster.getMetadata()); default: throw new DriverInternalError( String.format( "%s response received when prepared statement was expected", rm.kind)); } case ERROR: ResultSetFuture.extractCause( ResultSetFuture.convertException(((ErrorMessage) response).error)); break; default: throw new DriverInternalError( String.format( "%s response received when prepared statement was expected", response.type)); } throw new AssertionError(); } catch (QueryExecutionException e) { // Preparing a statement cannot throw any of the QueryExecutionException throw new DriverInternalError( "Received unexpected QueryExecutionException while preparing statement", e); } }
private PreparedStatement toPreparedStatement(String query, Connection.Future future) { try { Message.Response response = Uninterruptibles.getUninterruptibly(future); switch (response.type) { case RESULT: ResultMessage rm = (ResultMessage) response; switch (rm.kind) { case PREPARED: ResultMessage.Prepared pmsg = (ResultMessage.Prepared) rm; PreparedStatement stmt = PreparedStatement.fromMessage( pmsg, manager.cluster.getMetadata(), query, manager.poolsState.keyspace); try { manager.cluster.manager.prepare(pmsg.statementId, stmt, future.getAddress()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // This method doesn't propagate interruption, at least not for now. However, if // we've // interrupted preparing queries on other node it's not a problem as we'll // re-prepare // later if need be. So just ignore. } return stmt; default: throw new DriverInternalError( String.format( "%s response received when prepared statement was expected", rm.kind)); } case ERROR: ResultSetFuture.extractCause( ResultSetFuture.convertException(((ErrorMessage) response).error)); break; default: throw new DriverInternalError( String.format( "%s response received when prepared statement was expected", response.type)); } throw new AssertionError(); } catch (ExecutionException e) { ResultSetFuture.extractCauseFromExecutionException(e); throw new AssertionError(); } }