public void addDataPoint(DataPointsRowKey rowKey, int columnTime, DataPoint dataPoint, int ttl) throws IOException { KDataOutput kDataOutput = new KDataOutput(); dataPoint.writeValueToBuffer(kDataOutput); BoundStatement boundStatement = new BoundStatement(m_clusterConnection.psDataPointsInsert); boundStatement.setBytesUnsafe(0, DATA_POINTS_ROW_KEY_SERIALIZER.toByteBuffer(rowKey)); ByteBuffer b = ByteBuffer.allocate(4); b.putInt(columnTime); b.rewind(); boundStatement.setBytesUnsafe(1, b); boundStatement.setBytesUnsafe(2, ByteBuffer.wrap(kDataOutput.getBytes())); boundStatement.setInt(3, ttl); boundStatement.setLong(4, m_now); boundStatement.setConsistencyLevel(m_consistencyLevel); boundStatement.setIdempotent(true); addBoundStatement(boundStatement); }
public void addRowKey(String metricName, DataPointsRowKey rowKey, int rowKeyTtl) { m_newRowKeys.add(rowKey); ByteBuffer bb = ByteBuffer.allocate(8); bb.putLong(0, rowKey.getTimestamp()); Statement bs = m_clusterConnection.psRowKeyTimeInsert.bind() .setString(0, metricName) .setTimestamp(1, new Date(rowKey.getTimestamp())) //.setBytesUnsafe(1, bb) //Setting timestamp in a more optimal way .setInt(2, rowKeyTtl) .setIdempotent(true); bs.setConsistencyLevel(m_consistencyLevel); rowKeyBatch.add(bs); bs = m_clusterConnection.psRowKeyInsert.bind() .setString(0, metricName) .setTimestamp(1, new Date(rowKey.getTimestamp())) //.setBytesUnsafe(1, bb) //Setting timestamp in a more optimal way .setString(2, rowKey.getDataType()) .setMap(3, rowKey.getTags()) .setInt(4, rowKeyTtl) .setIdempotent(true); bs.setConsistencyLevel(m_consistencyLevel); rowKeyBatch.add(bs); }
statement.setInt(paramIndex, (int) typeCodec.parse(paramValue));
boundStatement.setInt(3, query.getLimit());
@Test(groups = "short") public void batchUpdateTest() { session().execute("TRUNCATE test"); session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)"); session().execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)"); PreparedStatement ps = session().prepare("UPDATE test SET v = :new WHERE k1 = :k1 AND k2 = :k2 IF v = :old"); BatchStatement batch = new BatchStatement(); batch.add( ps.bind().setInt("k1", 1).setInt("k2", 1).setInt("old", 2).setInt("new", 3)); // will fail batch.add(ps.bind().setInt("k1", 1).setInt("k2", 2).setInt("old", 1).setInt("new", 3)); ResultSet rs = session().execute(batch); assertFalse(rs.wasApplied()); }
@Test(groups = "short") public void should_work_with_prepared_statements_3() { session().execute(session().prepare(insertQuery).bind().setInt(0, pk).set(1, v, listType)); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, pk)); Row row = rows.one(); assertRow(row); }
@Test(groups = "short") public void should_work_with_built_statements() { session().execute(session().prepare(insertStmt).bind().setInt(0, pk).set(1, v, listType)); PreparedStatement ps = session().prepare(selectStmt); ResultSet rows = session().execute(ps.bind().setInt(0, pk)); Row row = rows.one(); assertRow(row); }
@Test(groups = "short") public void should_log_all_parameters_when_max_unlimited_bound_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().withMaxLoggedParameters(-1).build(); cluster().register(queryLogger); // when String query = "UPDATE test SET c_int = ? WHERE pk = ?"; PreparedStatement ps = session().prepare(query); BoundStatement bs = ps.bind(); bs.setInt("c_int", 123456); bs.setInt("pk", 42); session().execute(bs); // then String line = normalAppender.waitAndGet(10000); assertThat(line) .contains("Query completed normally") .contains(ipOfNode(1)) .contains("c_int:123456") .contains("pk:42"); }
@Test(groups = "short") public void should_not_truncate_parameter_when_max_length_unlimited_bound_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().withMaxParameterValueLength(-1).build(); cluster().register(queryLogger); // when String query = "UPDATE test SET c_int = ? WHERE pk = ?"; PreparedStatement ps = session().prepare(query); BoundStatement bs = ps.bind(); bs.setInt("c_int", 123456); bs.setInt("pk", 42); session().execute(bs); // then String line = normalAppender.waitAndGet(10000); assertThat(line) .contains("Query completed normally") .contains(ipOfNode(1)) .contains("c_int:123456") .doesNotContain(TRUNCATED_OUTPUT); }
@Test(groups = "short") public void should_log_null_parameter_bound_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().build(); cluster().register(queryLogger); // when String query = "UPDATE test SET c_text = ? WHERE pk = ?"; PreparedStatement ps = session().prepare(query); BoundStatement bs = ps.bind(); bs.setString("c_text", null); bs.setInt("pk", 42); session().execute(bs); // then String line = normalAppender.waitAndGet(10000); assertThat(line) .contains("Query completed normally") .contains(ipOfNode(1)) .contains(query) .contains("pk:42") .contains("c_text:NULL"); }
@Test(groups = "short") public void should_work_with_prepared_statements_2() { session() .execute( session() .prepare(insertQuery) .bind() .setInt(0, pk) .setList(1, v, elementsType) // variant with element type explicitly set ); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, pk)); Row row = rows.one(); assertRow(row); }
@CassandraVersion("2.0.0") @Test(groups = "short") public void should_truncate_parameter_when_max_length_exceeded_bound_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().withMaxParameterValueLength(5).build(); cluster().register(queryLogger); // when String query = "UPDATE test SET c_int = ? WHERE pk = ?"; PreparedStatement ps = session().prepare(query); BoundStatement bs = ps.bind(); bs.setInt("c_int", 123456); bs.setInt("pk", 42); session().execute(bs); // then String line = normalAppender.waitAndGet(10000); assertThat(line) .contains("Query completed normally") .contains(ipOfNode(1)) .contains("c_int:12345" + TRUNCATED_OUTPUT) .doesNotContain("123456"); }
@Test(groups = "short") public void should_truncate_blob_parameter_when_max_length_exceeded_bound_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().withMaxParameterValueLength(6).build(); cluster().register(queryLogger); // when String query = "UPDATE test SET c_blob = ? WHERE pk = ?"; PreparedStatement ps = session().prepare(query); BoundStatement bs = ps.bind(); bs.setBytes("c_blob", ByteBuffer.wrap(Bytes.toArray(Lists.newArrayList(1, 2, 3)))); bs.setInt("pk", 42); session().execute(bs); // then String line = normalAppender.waitAndGet(10000); assertThat(line) .contains("Query completed normally") .contains(ipOfNode(1)) .contains("c_blob:0x0102" + TRUNCATED_OUTPUT) .doesNotContain("0x010203"); }
@Test(groups = "short") public void should_use_default_codecs_with_prepared_statements_2() { session() .execute( session() .prepare(insertQuery) .bind() .setInt(0, n_int) .setLong(1, n_bigint) .setFloat(2, n_float) .setDouble(3, n_double) .setVarint(4, n_varint) .setDecimal(5, n_decimal)); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, n_int).setLong(1, n_bigint)); Row row = rows.one(); assertRow(row); }
@Test(groups = "short") public void should_handle_contains_on_list_with_index() { PreparedStatement byBuyer = session() .prepare( select("id", "description", "buyers") .from("products") .where(contains("buyers", bindMarker("buyer")))); ResultSet results = session().execute(byBuyer.bind().setInt("buyer", 4)); Row row = results.one(); assertThat(row).isNotNull(); assertThat(row.getInt("id")).isEqualTo(38471); assertThat(row.getList("buyers", Integer.class)).contains(4); }
/** Ensures that an incoming payload is propagated from prepared to bound statements. */ @Test(groups = "short") public void should_propagate_incoming_payload_to_bound_statement() throws Exception { RegularStatement statement = new SimpleStatement("SELECT c2 as col1 FROM t1 where c1 = ?"); statement.setOutgoingPayload(payload1); PreparedStatement ps = session().prepare(statement); // Prepared statement should inherit outgoing payload assertThat(ps.getOutgoingPayload()).isEqualTo(payload1); // Prepared statement should receive incoming payload assertThat(ps.getIncomingPayload()).isEqualTo(payload1); ps.setOutgoingPayload(null); // unset outgoing payload // bound statement should inherit from prepared statement's incoming payload BoundStatement bs = ps.bind(1); ResultSet rows = session().execute(bs); Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); assertThat(actual).isEqualTo(payload1); bs = ps.bind(); bs.setInt(0, 1); rows = session().execute(bs); actual = rows.getExecutionInfo().getIncomingPayload(); assertThat(actual).isEqualTo(payload1); }
@Test(groups = "short") public void should_delete_map_entry_with_bind_marker() throws Exception { // given session().execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); // when BuiltStatement statement = delete().mapElt("b", bindMarker()).from("test_coll").where(eq("k", 1)); PreparedStatement ps = session().prepare(statement); session().execute(ps.bind().setInt(0, 1)); // then Map<Integer, String> actual = session() .execute("SELECT b FROM test_coll WHERE k = 1") .one() .getMap("b", Integer.class, String.class); assertThat(actual).containsExactly(entry(2, "bar")); }
@Test(groups = "short") public void should_use_collection_codecs_with_prepared_statements_3() { session() .execute( session() .prepare(insertQuery) .bind() .setInt(0, n_int) .set(1, l_int, TypeTokens.listOf(Integer.class)) .set(2, l_bigint, TypeTokens.listOf(Long.class)) .set(3, s_float, TypeTokens.setOf(Float.class)) .set(4, s_double, TypeTokens.setOf(Double.class)) .set(5, m_varint, TypeTokens.mapOf(Integer.class, BigInteger.class)) .set(6, m_decimal, TypeTokens.mapOf(Integer.class, BigDecimal.class))); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, n_int)); Row row = rows.one(); assertRow(row); }
@Test(groups = "short") public void should_use_collection_codecs_with_built_statements() { session() .execute( session() .prepare(insertStmt) .bind() .setInt(0, n_int) .set(1, l_int, TypeTokens.listOf(Integer.class)) .set(2, l_bigint, TypeTokens.listOf(Long.class)) .set(3, s_float, TypeTokens.setOf(Float.class)) .set(4, s_double, TypeTokens.setOf(Double.class)) .set(5, m_varint, TypeTokens.mapOf(Integer.class, BigInteger.class)) .set(6, m_decimal, TypeTokens.mapOf(Integer.class, BigDecimal.class))); PreparedStatement ps = session().prepare(selectStmt); ResultSet rows = session().execute(ps.bind().setInt(0, n_int)); Row row = rows.one(); assertRow(row); }
@Test(groups = "short") public void should_use_default_codecs_with_prepared_statements_3() { session() .execute( session() .prepare(insertQuery) .bind() .set(0, n_int, Integer.class) .set(1, n_bigint, Long.class) .set(2, n_float, Float.class) .set(3, n_double, Double.class) .set(4, n_varint, BigInteger.class) .set(5, n_decimal, BigDecimal.class)); PreparedStatement ps = session().prepare(selectQuery); ResultSet rows = session().execute(ps.bind().setInt(0, n_int).setLong(1, n_bigint)); Row row = rows.one(); assertRow(row); }