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