Refine search
scanStmt.append(QueryBuilder.bindMarker()); stmt = session.prepare(scanStmt.toString()); stmt.setConsistencyLevel(readConsistencyLevel); if (trace) { stmt.enableTracing(); logger.debug(stmt.getQueryString()); logger.debug("startKey = {}, recordcount = {}", startkey, recordcount); ResultSet rs = session.execute(stmt.bind(startkey, Integer.valueOf(recordcount))); while (!rs.isExhausted()) { Row row = rs.one(); tuple = new HashMap<String, ByteIterator>(); ColumnDefinitions cd = row.getColumnDefinitions(); ByteBuffer val = row.getBytesUnsafe(def.getName()); if (val != null) { tuple.put(def.getName(), new ByteArrayByteIterator(val.array()));
stmt = session.prepare(insertStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { BoundStatement boundStmt = stmt.bind().setString(0, key); ColumnDefinitions vars = stmt.getVariables(); for (int i = 1; i < vars.size(); i++) { boundStmt.setString(i, values.get(vars.getName(i)).toString()); session.execute(boundStmt);
/** {@inheritDoc} */ @Override public PreparedStatement setIdempotent(Boolean idempotent) { return st.setIdempotent(idempotent); }
stmt = session.prepare(QueryBuilder.delete().from(table) .where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker()))); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { stmt.enableTracing(); logger.debug(stmt.getQueryString()); logger.debug("key = {}", key); session.execute(stmt.bind(key));
@Test(groups = "short") public void should_set_routing_key_on_case_insensitive_keyspace_and_table() { session().execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)", keyspace)); PreparedStatement ps = session().prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)", keyspace)); BoundStatement bs = ps.bind(1); assertThat(bs.getRoutingKey(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNotNull(); }
@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_handle_contains_on_map_with_index() { PreparedStatement byFeatures = session() .prepare( select("id", "description", "features_values") .from("products") .where(contains("features_values", bindMarker("feature")))); ResultSet results = session().execute(byFeatures.bind().setString("feature", "LED")); Row row = results.one(); assertThat(row).isNotNull(); assertThat(row.getInt("id")).isEqualTo(29412); assertThat(row.getMap("features_values", String.class, String.class)) .containsEntry("techno", "LED"); }
@Test(groups = "short") @CassandraVersion(value = "3.6", description = "Non-frozen UDTs were introduced in C* 3.6") public void should_indicate_user_type_is_not_frozen() { session().execute("CREATE TABLE not_frozen_table(k int primary key, v type_for_frozen_test)"); KeyspaceMetadata keyspaceMetadata = cluster().getMetadata().getKeyspace(this.keyspace); assertThat(keyspaceMetadata.getUserType("type_for_frozen_test")).isNotFrozen(); DataType userType = keyspaceMetadata.getTable("not_frozen_table").getColumn("v").getType(); assertThat(userType).isNotFrozen(); assertThat(userType.toString()).isEqualTo(keyspace + ".type_for_frozen_test"); ResultSet rs = session().execute("SELECT v FROM not_frozen_table WHERE k = 1"); assertThat(rs.getColumnDefinitions().getType(0)).isNotFrozen(); PreparedStatement pst = session().prepare("SELECT v FROM not_frozen_table WHERE k = ?"); assertThat(pst.getVariables().getType(0)).isNotFrozen(); } }
@Test(groups = "short") public void prepareWithNullValuesTest() throws Exception { PreparedStatement ps = session().prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, ?)"); session().execute(ps.bind("prepWithNull1", null)); BoundStatement bs = ps.bind(); bs.setString("k", "prepWithNull2"); bs.setString("v", null); session().execute(bs); ResultSet rs = session() .execute( "SELECT * FROM " + SIMPLE_TABLE2 + " WHERE k IN ('prepWithNull1', 'prepWithNull2')"); Row r1 = rs.one(); Row r2 = rs.one(); assertTrue(rs.isExhausted()); assertEquals(r1.getString("k"), "prepWithNull1"); assertEquals(r1.getString("v"), null); assertEquals(r2.getString("k"), "prepWithNull2"); assertEquals(r2.getString("v"), null); }
@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); }
/** * Validates that {@link PagingState} can be reused with the same {@link BoundStatement}. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided paging state starts from * the subsequent row from the first query. */ @Test(groups = "short") @CassandraVersion("2.0.0") public void should_be_able_to_use_state_with_bound_statement() { PreparedStatement prepared = session().prepare("SELECT v from test where k=?"); BoundStatement bs = prepared.bind(KEY); ResultSet result = session().execute(bs.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); PagingState pagingState = result.getExecutionInfo().getPagingState(); result = session().execute(bs.setFetchSize(20).setPagingState(pagingState)); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
@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_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_handle_udts_with_default_codecs() { setUpUserTypes(cluster()); // simple statement session().execute(insertQuery, uuid, "John Doe", addressValue); ResultSet rows = session().execute(selectQuery, uuid); Row row = rows.one(); assertRow(row); // prepared + values PreparedStatement ps = session().prepare(insertQuery); session().execute(ps.bind(uuid, "John Doe", addressValue)); rows = session().execute(selectQuery, uuid); row = rows.one(); assertRow(row); // bound with setUDTValue session() .execute(ps.bind().setUUID(0, uuid).setString(1, "John Doe").setUDTValue(2, addressValue)); rows = session().execute(selectQuery, uuid); row = rows.one(); assertRow(row); }
@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); }
@Test(groups = "short") public void should_execute_prepared_counter_statement() 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(); assertThat(rows).hasSize(1); assertThat(rows.get(0).getLong("c")).isEqualTo(2L); }
@Test(groups = "short") public void should_handle_contains_on_set_with_index() { PreparedStatement byCategory = session() .prepare( select("id", "description", "categories") .from("products") .where(contains("categories", bindMarker("category")))); ResultSet results = session().execute(byCategory.bind().setString("category", "hdtv")); assertThat(results.getAvailableWithoutFetching()).isEqualTo(2); for (Row row : results) { assertThat(row.getSet("categories", String.class)).contains("hdtv"); } }
@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_update_statement_id_when_metadata_changed_across_executions() { // given PreparedStatement ps = session().prepare("SELECT * FROM prepared_statement_invalidation_test WHERE a = ?"); MD5Digest idBefore = ps.getPreparedId().resultSetMetadata.id; // when session().execute("ALTER TABLE prepared_statement_invalidation_test ADD d int"); BoundStatement bs = ps.bind(1); ResultSet rows = session().execute(bs); // then MD5Digest idAfter = ps.getPreparedId().resultSetMetadata.id; assertThat(idBefore).isNotEqualTo(idAfter); assertThat(ps.getPreparedId().resultSetMetadata.variables) .hasSize(4) .containsVariable("d", DataType.cint()); assertThat(bs.preparedStatement().getPreparedId().resultSetMetadata.variables) .hasSize(4) .containsVariable("d", DataType.cint()); assertThat(rows.getColumnDefinitions()).hasSize(4).containsVariable("d", DataType.cint()); }
/** * Validates that {@link PagingState} cannot be reused with a different {@link BoundStatement} * than the original, even if its source {@link PreparedStatement} was the same. * * @test_category paging * @expected_result A failure is thrown when setting paging state on a different {@link * BoundStatement}. */ @Test( groups = "short", expectedExceptions = {PagingStateException.class}) @CassandraVersion("2.0.0") public void should_not_be_able_to_use_state_with_different_bound_statement() { PreparedStatement prepared = session().prepare("SELECT v from test where k=?"); BoundStatement bs0 = prepared.bind(KEY); ResultSet result = session().execute(bs0.setFetchSize(20)); PagingState pagingState = result.getExecutionInfo().getPagingState(); BoundStatement bs1 = prepared.bind("different_key"); session().execute(bs1.setFetchSize(20).setPagingState(pagingState)); }