Search in sources :

Example 31 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class AuditLoggerTest method testCqlBatchAuditing.

@Test
public void testCqlBatchAuditing() throws Throwable {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    Session session = sessionNet();
    BatchStatement batchStatement = new BatchStatement();
    String cqlInsert = "INSERT INTO " + KEYSPACE + "." + currentTable() + " (id, v1, v2) VALUES (?, ?, ?)";
    PreparedStatement prep = session.prepare(cqlInsert);
    AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    batchStatement.add(prep.bind(2, "Apapche1", "Cassandra1"));
    String cqlUpdate = "UPDATE " + KEYSPACE + "." + currentTable() + " SET v1 = ? WHERE id = ?";
    prep = session.prepare(cqlUpdate);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlUpdate, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind("Apache Cassandra", 1));
    String cqlDelete = "DELETE FROM " + KEYSPACE + "." + currentTable() + " WHERE id = ?";
    prep = session.prepare(cqlDelete);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlDelete, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1));
    ResultSet rs = session.execute(batchStatement);
    assertEquals(5, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertEquals(AuditLogEntryType.BATCH, logEntry.getType());
    assertTrue(logEntry.getOperation().contains("BatchId"));
    assertNotEquals(0, logEntry.getTimestamp());
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert, AuditLogEntryType.UPDATE, logEntry, false);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert, AuditLogEntryType.UPDATE, logEntry, false);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlUpdate, AuditLogEntryType.UPDATE, logEntry, false);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlDelete, AuditLogEntryType.DELETE, logEntry, false);
    int size = rs.all().size();
    assertEquals(0, size);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 32 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class ReprepareTestOldBehaviour method testReprepareMixedVersionWithoutReset.

@Test
public void testReprepareMixedVersionWithoutReset() throws Throwable {
    try (ICluster<IInvokableInstance> c = init(builder().withNodes(2).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).withInstanceInitializer(PrepareBehaviour::oldBehaviour).start())) {
        ForceHostLoadBalancingPolicy lbp = new ForceHostLoadBalancingPolicy();
        c.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, v int, PRIMARY KEY (pk, ck));"));
        // 1 has old behaviour
        for (int firstContact : new int[] { 1, 2 }) {
            for (boolean withUse : new boolean[] { true, false }) {
                for (boolean clearBetweenExecutions : new boolean[] { true, false }) {
                    try (com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").addContactPoint("127.0.0.2").withLoadBalancingPolicy(lbp).build();
                        Session session = cluster.connect()) {
                        if (withUse)
                            session.execute(withKeyspace("USE %s"));
                        lbp.setPrimary(firstContact);
                        final PreparedStatement select = session.prepare(withKeyspace("SELECT * FROM %s.tbl"));
                        session.execute(select.bind());
                        if (clearBetweenExecutions)
                            c.get(2).runOnInstance(QueryProcessor::clearPreparedStatementsCache);
                        lbp.setPrimary(firstContact == 1 ? 2 : 1);
                        session.execute(select.bind());
                        if (clearBetweenExecutions)
                            c.get(2).runOnInstance(QueryProcessor::clearPreparedStatementsCache);
                        lbp.setPrimary(firstContact);
                        session.execute(select.bind());
                        c.get(2).runOnInstance(QueryProcessor::clearPreparedStatementsCache);
                    }
                }
            }
        }
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) Session(com.datastax.driver.core.Session) ICluster(org.apache.cassandra.distributed.api.ICluster) Test(org.junit.Test) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) NETWORK(org.apache.cassandra.distributed.api.Feature.NETWORK) GOSSIP(org.apache.cassandra.distributed.api.Feature.GOSSIP) NATIVE_PROTOCOL(org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL) IInvokableInstance(org.apache.cassandra.distributed.api.IInvokableInstance) PreparedStatement(com.datastax.driver.core.PreparedStatement) QueryProcessor(org.apache.cassandra.cql3.QueryProcessor) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 33 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class PreparedStatementsTest method testInvalidatePreparedStatementOnAlter.

private void testInvalidatePreparedStatementOnAlter(ProtocolVersion version, boolean supportsMetadataChange) {
    Session session = sessionNet(version);
    String createTableStatement = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + ".qp_cleanup (a int PRIMARY KEY, b int, c int);";
    String alterTableStatement = "ALTER TABLE " + KEYSPACE + ".qp_cleanup ADD d int;";
    session.execute(dropKsStatement);
    session.execute(createKsStatement);
    session.execute(createTableStatement);
    PreparedStatement preparedSelect = session.prepare("SELECT * FROM " + KEYSPACE + ".qp_cleanup");
    session.execute("INSERT INTO " + KEYSPACE + ".qp_cleanup (a, b, c) VALUES (?, ?, ?);", 1, 2, 3);
    session.execute("INSERT INTO " + KEYSPACE + ".qp_cleanup (a, b, c) VALUES (?, ?, ?);", 2, 3, 4);
    assertRowsNet(session.execute(preparedSelect.bind()), row(1, 2, 3), row(2, 3, 4));
    session.execute(alterTableStatement);
    session.execute("INSERT INTO " + KEYSPACE + ".qp_cleanup (a, b, c, d) VALUES (?, ?, ?, ?);", 3, 4, 5, 6);
    ResultSet rs;
    if (supportsMetadataChange) {
        rs = session.execute(preparedSelect.bind());
        assertRowsNet(version, rs, row(1, 2, 3, null), row(2, 3, 4, null), row(3, 4, 5, 6));
        assertEquals(rs.getColumnDefinitions().size(), 4);
    } else {
        rs = session.execute(preparedSelect.bind());
        assertRowsNet(rs, row(1, 2, 3), row(2, 3, 4), row(3, 4, 5));
        assertEquals(rs.getColumnDefinitions().size(), 3);
    }
    session.execute(dropKsStatement);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session)

Example 34 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class PreparedStatementsTest method testInvalidatePreparedStatementOnAlterUnchangedMetadata.

private void testInvalidatePreparedStatementOnAlterUnchangedMetadata(ProtocolVersion version) {
    Session session = sessionNet(version);
    String createTableStatement = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + ".qp_cleanup (a int PRIMARY KEY, b int, c int);";
    String alterTableStatement = "ALTER TABLE " + KEYSPACE + ".qp_cleanup ADD d int;";
    session.execute(dropKsStatement);
    session.execute(createKsStatement);
    session.execute(createTableStatement);
    PreparedStatement preparedSelect = session.prepare("SELECT a, b, c FROM " + KEYSPACE + ".qp_cleanup");
    session.execute("INSERT INTO " + KEYSPACE + ".qp_cleanup (a, b, c) VALUES (?, ?, ?);", 1, 2, 3);
    session.execute("INSERT INTO " + KEYSPACE + ".qp_cleanup (a, b, c) VALUES (?, ?, ?);", 2, 3, 4);
    ResultSet rs = session.execute(preparedSelect.bind());
    assertRowsNet(rs, row(1, 2, 3), row(2, 3, 4));
    assertEquals(rs.getColumnDefinitions().size(), 3);
    session.execute(alterTableStatement);
    session.execute("INSERT INTO " + KEYSPACE + ".qp_cleanup (a, b, c, d) VALUES (?, ?, ?, ?);", 3, 4, 5, 6);
    rs = session.execute(preparedSelect.bind());
    assertRowsNet(rs, row(1, 2, 3), row(2, 3, 4), row(3, 4, 5));
    assertEquals(rs.getColumnDefinitions().size(), 3);
    session.execute(dropKsStatement);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session)

Example 35 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project cassandra by apache.

the class PreparedStatementsTest method testInvalidatePreparedStatementsOnDrop.

@Test
public void testInvalidatePreparedStatementsOnDrop() {
    Session session = sessionNet(ProtocolVersion.V5);
    session.execute(dropKsStatement);
    session.execute(createKsStatement);
    String createTableStatement = "CREATE TABLE IF NOT EXISTS " + KEYSPACE + ".qp_cleanup (id int PRIMARY KEY, cid int, val text);";
    String dropTableStatement = "DROP TABLE IF EXISTS " + KEYSPACE + ".qp_cleanup;";
    session.execute(createTableStatement);
    PreparedStatement prepared = session.prepare("INSERT INTO " + KEYSPACE + ".qp_cleanup (id, cid, val) VALUES (?, ?, ?)");
    PreparedStatement preparedBatch = session.prepare("BEGIN BATCH " + "INSERT INTO " + KEYSPACE + ".qp_cleanup (id, cid, val) VALUES (?, ?, ?);" + "APPLY BATCH;");
    session.execute(dropTableStatement);
    session.execute(createTableStatement);
    session.execute(prepared.bind(1, 1, "value"));
    session.execute(preparedBatch.bind(2, 2, "value2"));
    session.execute(dropKsStatement);
    session.execute(createKsStatement);
    session.execute(createTableStatement);
    // The driver will get a response about the prepared statement being invalid, causing it to transparently
    // re-prepare the statement.  We'll rely on the fact that we get no errors while executing this to show that
    // the statements have been invalidated.
    session.execute(prepared.bind(1, 1, "value"));
    session.execute(preparedBatch.bind(2, 2, "value2"));
    session.execute(dropKsStatement);
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

PreparedStatement (com.datastax.driver.core.PreparedStatement)113 ResultSet (com.datastax.driver.core.ResultSet)60 BoundStatement (com.datastax.driver.core.BoundStatement)59 Session (com.datastax.driver.core.Session)39 Test (org.junit.Test)30 Row (com.datastax.driver.core.Row)27 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)27 XMLStreamException (javolution.xml.stream.XMLStreamException)25 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)15 Cluster (com.datastax.driver.core.Cluster)9 Date (java.util.Date)9 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 QueryProcessor (org.apache.cassandra.cql3.QueryProcessor)7 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)7 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)7 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)7 ICluster (org.apache.cassandra.distributed.api.ICluster)7