Refine search
/** * @return cql query statement to add a new task_id to workflow_id mapping to the "task_lookup" table */ public String getUpdateTaskLookupStatement() { return QueryBuilder.update(keyspace, TABLE_TASK_LOOKUP) .with(set(WORKFLOW_ID_KEY, bindMarker())) .where(eq(TASK_ID_KEY, bindMarker())) .getQueryString(); }
private void decrementCounterInStorage( String queueName, DatabaseQueueMessage.Type type, long decrement ) { Statement update = QueryBuilder.update( TABLE_MESSAGE_COUNTERS ) .where( QueryBuilder.eq( COLUMN_QUEUE_NAME, queueName ) ) .and( QueryBuilder.eq( COLUMN_MESSAGE_TYPE, type.toString() ) ) .with( QueryBuilder.decr( COLUMN_COUNTER_VALUE, decrement ) ); cassandraClient.getQueueMessageSession().execute( update ); }
/** * Adds an option to the UPDATE statement those assignments are part of. * * @param using the using clause to add. * @return the options of the UPDATE statement those assignments are part of. */ public Options using(Using using) { return statement.using(using); }
Update updateStmt = QueryBuilder.update(table); updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker())); updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())); stmt = session.prepare(updateStmt); stmt.setConsistencyLevel(writeConsistencyLevel); if (trace) { session.execute(boundStmt);
@Test(groups = "unit") public void statementForwardingTest() throws Exception { Update upd = update("foo"); upd.setConsistencyLevel(ConsistencyLevel.QUORUM); upd.enableTracing(); Statement query = upd.using(timestamp(42)).with(set("a", 12)).and(incr("c", 3)).where(eq("k", 2)); assertEquals(query.getConsistencyLevel(), ConsistencyLevel.QUORUM); assertTrue(query.isTracing()); }
@Test(groups = "short") public void executeTest() throws Exception { session() .execute( insertInto(TABLE1) .value("k", "k1") .value("t", "This is a test") .value("i", 3) .value("f", 0.42)); session().execute(update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2"))); List<Row> rows = session().execute(select().from(TABLE1).where(in("k", "k1", "k2"))).all(); assertEquals(2, rows.size()); Row r1 = rows.get(0); assertEquals("k1", r1.getString("k")); assertEquals("This is a test", r1.getString("t")); assertEquals(3, r1.getInt("i")); assertFalse(r1.isNull("f")); Row r2 = rows.get(1); assertEquals("k2", r2.getString("k")); assertEquals("Another test", r2.getString("t")); assertTrue(r2.isNull("i")); assertTrue(r2.isNull("f")); }
value = "3.6", description = "Requires CASSANDRA-7423 introduced in Cassandra 3.6") @Test(groups = "short") public void should_support_setting_and_retrieving_udt_fields() { String udt = "person"; session() .execute( createType(udt).addColumn("first", DataType.text()).addColumn("last", DataType.text())); UserType userType = cluster().getMetadata().getKeyspace(keyspace).getUserType(udt); .execute( createTable(table) .addPartitionKey("k", DataType.text()) value.setString("first", "Bob"); value.setString("last", "Smith"); session().execute(insertInto(table).value("k", "key").value("u", value)); .execute( update(table) .with(set(path("u", "first"), "Rick")) .and(set(raw("u.last"), "Jones")) .where(eq("k", "key")));
@Test(groups = "short") public void should_handle_collections_of_UDT() throws Exception { UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt"); UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost")); Statement insert = insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue)); assertThat(insert.toString()) .isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2,a:'127.0.0.1'}]);"); session().execute(insert); List<Row> rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); assertThat(rows.size()).isEqualTo(1); Row r1 = rows.get(0); assertThat(r1.getList("l", UDTValue.class).get(0).getInet("a").getHostAddress()) .isEqualTo("127.0.0.1"); Map<Integer, UDTValue> map = Maps.newHashMap(); map.put(0, udtValue); map.put(2, udtValue2); Statement updateMap = update("udtTest").with(putAll("m", map)).where(eq("k", 1)); assertThat(updateMap.toString()) .isEqualTo( "UPDATE udtTest SET m=m+{0:{i:2,a:'127.0.0.1'},2:{i:3,a:'127.0.0.1'}} WHERE k=1;"); session().execute(updateMap); rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); r1 = rows.get(0); assertThat(r1.getMap("m", Integer.class, UDTValue.class)).isEqualTo(map); }
update("foo", "bar") .using(timestamp(42)) .with(set("a", 12)) update = update("foo").where().and(eq("k", 2)).with(set("b", null)); assertEquals(update.toString(), query); .with(setIdx("a", 2, "foo")) .with() .with(discardAll("b", Arrays.asList(1, 2, 3))) .with( update = update("foo").using(ttl(400)); assertEquals(update.toString(), query); .with(set("a", new BigDecimal(3.2))) .where() .where() .using(timestamp(42)) update("foo").using(ttl(-400)); fail("Expected an IllegalArgumentException"); } catch (IllegalArgumentException e) {
@Test(groups = "short") public void intRoutingBatchKeyTest() throws Exception { BuiltStatement batch; query = select().from(table).where(eq("k", 42)); batch_query += "APPLY BATCH;"; batch = batch() .add(insertInto(table).values(new String[] {"k", "a"}, new Object[] {42, 1})) .add(update(table).using(ttl(400))); assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb); assertEquals(batch.toString(), batch_query);
@Test(groups = "unit") public void should_handle_from_json() throws Exception { assertThat( update("users") .with(set("age", fromJson("42"))) .where(eq("id", fromJson("\"user123\""))) .toString()) .isEqualTo("UPDATE users SET age=fromJson('42') WHERE id=fromJson('\"user123\"');"); assertThat( insertInto("users") .value("id", fromJson("\"user123\"")) .value("age", fromJson("42")) .toString()) .isEqualTo("INSERT INTO users (id,age) VALUES (fromJson('\"user123\"'),fromJson('42'));"); assertThat(insertInto("users").value("id", fromJson(bindMarker())).toString()) .isEqualTo("INSERT INTO users (id) VALUES (fromJson(?));"); assertThat(insertInto("users").value("id", fromJson(bindMarker("id"))).toString()) .isEqualTo("INSERT INTO users (id) VALUES (fromJson(:id));"); }
@Test(groups = "unit") public void updateInjectionTest() throws Exception { String query; Statement update; query = "UPDATE foo.bar USING TIMESTAMP 42 SET a=12 WHERE k='2 OR 1=1';"; update = update("foo", "bar").using(timestamp(42)).with(set("a", 12)).where(eq("k", "2 OR 1=1")); assertEquals(update.toString(), query); query = "UPDATE foo SET b='null WHERE k=1; --comment' WHERE k=2;"; update = update("foo").where().and(eq("k", 2)).with(set("b", "null WHERE k=1; --comment")); assertEquals(update.toString(), query); query = "UPDATE foo USING TIMESTAMP 42 SET \"b WHERE k=1; --comment\"=[3,2,1]+\"b WHERE k=1; --comment\" WHERE k=2;"; update = update("foo") .where() .and(eq("k", 2)) .with(prependAll("b WHERE k=1; --comment", Arrays.asList(3, 2, 1))) .using(timestamp(42)); assertEquals(update.toString(), query); }
@Test(groups = "unit") public void should_handle_nested_collections() { String query; Statement statement; query = "UPDATE foo SET l=[[1],[2]] WHERE k=1;"; ImmutableList<ImmutableList<Integer>> list = ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2)); statement = update("foo").with(set("l", list)).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET m={1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; statement = update("foo").with(set("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET m=m+{1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; statement = update("foo").with(putAll("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET l=[[1]]+l WHERE k=1;"; statement = update("foo").with(prepend("l", ImmutableList.of(1))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET l=[[1],[2]]+l WHERE k=1;"; statement = update("foo").with(prependAll("l", list)).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); }
/** * @test_category queries:builder * @jira_ticket JAVA-1286 * @jira_ticket CASSANDRA-7423 */ @Test(groups = "unit") public void should_handle_setting_udt_fields() throws Exception { assertThat( update("tbl") .with(set(path("a", quote("B")), "foo")) .and(set(raw("c.\"D\""), "bar")) .where(eq("k", 0)) .getQueryString()) .isEqualTo("UPDATE tbl SET a.\"B\"=?,c.\"D\"=? WHERE k=0;"); }