/** * Adds a new assignment for this UPDATE statement. * * @param assignment the new Assignment to add. * @return these Assignments. */ public Assignments and(Assignment assignment) { statement.setCounterOp(assignment instanceof CounterAssignment); if (!hasNonIdempotentOps() && !Utils.isIdempotent(assignment)) statement.setNonIdempotentOps(); assignments.add(assignment); checkForBindMarkers(assignment); return this; }
query = ((Delete) select).where(QueryBuilder.eq(columnKeys[i], "?")); } else { query = ((Update.Assignments) select).where(QueryBuilder.eq(columnKeys[i], "?")); query = ((Delete) select).where(QueryBuilder.eq(primaryKey, "?")); } else { query = ((Update.Assignments) select).where(QueryBuilder.eq(primaryKey, "?")); query = ((Update.Assignments) select).where(QueryBuilder.gte(columnKeys[i], "?")); query = ((Update.Assignments) select).where(QueryBuilder.gte(primaryKey, "?")); query = ((Update.Assignments) select).where(QueryBuilder.lte(columnKeys[i], "?")); query = ((Update.Assignments) select).where(QueryBuilder.lte(primaryKey, "?"));
query = ((Delete) select).where(QueryBuilder.eq(columnKeys[i], "?")); } else { query = ((Update.Assignments) select).where(QueryBuilder.eq(columnKeys[i], "?")); query = ((Delete) select).where(QueryBuilder.eq(primaryKey, "?")); } else { query = ((Update.Assignments) select).where(QueryBuilder.eq(primaryKey, "?")); query = ((Update.Assignments) select).where(QueryBuilder.gte(columnKeys[i], "?")); query = ((Update.Assignments) select).where(QueryBuilder.gte(primaryKey, "?")); query = ((Update.Assignments) select).where(QueryBuilder.lte(columnKeys[i], "?")); query = ((Update.Assignments) select).where(QueryBuilder.lte(primaryKey, "?"));
.using(timestamp(42)) .with(set("a", 12)) .and(set("b", Arrays.asList(3, 2, 1))) .and(incr("c", 3)) .where(eq("k", 2)); assertEquals(update.toString(), query); assertThat(update.getKeyspace()).isEqualTo("foo"); // keyspace set since provided. update("foo") .with(setIdx("a", 2, "foo")) .and(prependAll("b", Arrays.asList(3, 2, 1))) .and(remove("c", "a")) .where(eq("k", 2)) .and(eq("l", "foo")) .and(lt("m", 4)) update("foo") .with() .and(prepend("b", 3)) .and(append("c", "a")) .and(appendAll("d", Arrays.asList(1, 2, 3))) .and(discard("e", 1)); assertEquals(update.toString(), query); update("foo") .with(discardAll("b", Arrays.asList(1, 2, 3))) .and(add("c", 1)) .and( addAll(
private ImmutableList<BuiltStatement> nonIdempotentBuiltStatements() { return ImmutableList.of( update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list update("foo").with(set("v", 1)).and(prepend("l", 1)).where(eq("k", 1)), // prepend to list update("foo").with(incr("c")).where(eq("k", 1)), // counter update insertInto("foo") .values(new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of(uuid(), "foo")}), update("foo").with(set("v", fcall("token", "k"))).where(eq("k", 1)), update("foo").with(set("v", now())).where(eq("k", 1)), update("foo").with(set("v", uuid())).where(eq("k", 1)), update("foo").with(set("v", Lists.newArrayList(fcall("token", "k")))).where(eq("k", 1)), update("foo").with(set("v", Lists.newArrayList(now()))).where(eq("k", 1)), update("foo").with(set("v", Lists.newArrayList(uuid()))).where(eq("k", 1)), delete().from("foo").where(lt("k", fcall("now"))), delete().from("foo").where(lt("k", now())), update("foo").onlyIf().and(contains("developers", "datastax")).where(eq("good", "drivers")), update("foo").onlyIf(contains("developers", "datastax")).with(set("v", 0)), update("foo").with(set("v", 0)).onlyIf(contains("hello", "world")), insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())).ifNotExists(), delete().from("foo").where(eq("k", 2)).ifExists(), .values( new String[] {"k", "v"}, new Object[] {1, ImmutableMap.of(raw("foo()"), "foo")}), update("foo").with(set("v", raw("foo()"))).where(eq("k", 1)), update("foo").with(set("v", Lists.newArrayList(raw("foo()")))).where(eq("k", 1)));
update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).ifExists(); row = session().execute(update).one(); assertFalse(row.getBool("[applied]")); update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 1)).ifExists(); row = session().execute(update).one(); assertTrue(row.getBool("[applied]")); update(TABLE_INT) .with(set("a", 2)) .and(set("b", 2)) .where(eq("k", 2)) .onlyIf(eq("a", 1)) .and(eq("b", 2)); update(TABLE_INT) .with(set("a", 3)) .and(set("b", 3)) .where(eq("k", 1)) .onlyIf(eq("a", 2)) .and(eq("b", 2)); update(TABLE_INT) .with(set("a", 4)) .and(set("b", 4)) .onlyIf(eq("a", 2)) .and(eq("b", 2)) .where(eq("k", 1));
update(CassandraSieveClusterQuotaTable.TABLE_NAME) .with(set(CassandraSieveClusterQuotaTable.VALUE, bindMarker(CassandraSieveClusterQuotaTable.VALUE))) .where(eq(CassandraSieveClusterQuotaTable.NAME, bindMarker(CassandraSieveClusterQuotaTable.NAME)))); .where(eq(CassandraSieveSpaceTable.USER_NAME, bindMarker(CassandraSieveSpaceTable.USER_NAME)))); .where(eq(CassandraSieveQuotaTable.USER_NAME, bindMarker(CassandraSieveQuotaTable.USER_NAME))));
.update(tableName.getKeyspace(), tableName.getTable()) .with(incr(SCHEMA_COUNTER_COLUMN_VALUE, 0)) .where(eq(SCHEMA_COUNTER_COLUMN_KEY, SCHEMA_COUNTER_ONLY_KEY_VALUE));
Update.Assignments assignments = update.with(); if (scope != null && scope.length() > 0) { assignments.and(QueryBuilder.set("scope", scope)); assignments.and(QueryBuilder.set("descr", description)); assignments.and(QueryBuilder.set("status", status)); assignments.and(QueryBuilder.set("details", applicationDetails)); Update.Where stmt = assignments.where(QueryBuilder.eq("client_id", clientId)); try { session.execute(stmt);
@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); }
@Inject public CassandraSieveDAO(Session session) { this.cassandraAsyncExecutor = new CassandraAsyncExecutor(session); insertScriptStatement = session.prepare( insertInto(TABLE_NAME) .value(USER_NAME, bindMarker(USER_NAME)) .value(SCRIPT_NAME, bindMarker(SCRIPT_NAME)) .value(SCRIPT_CONTENT, bindMarker(SCRIPT_CONTENT)) .value(IS_ACTIVE, bindMarker(IS_ACTIVE)) .value(SIZE, bindMarker(SIZE))); selectScriptsStatement = session.prepare(getScriptsQuery()); selectScriptStatement = session.prepare(getScriptsQuery() .and(eq(SCRIPT_NAME, bindMarker(SCRIPT_NAME)))); updateScriptActivationStatement = session.prepare( update(TABLE_NAME) .with(set(IS_ACTIVE, bindMarker(IS_ACTIVE))) .where(eq(USER_NAME, bindMarker(USER_NAME))) .and(eq(SCRIPT_NAME, bindMarker(SCRIPT_NAME))) .ifExists()); deleteScriptStatement = session.prepare( delete() .from(TABLE_NAME) .where(eq(USER_NAME, bindMarker(USER_NAME))) .and(eq(SCRIPT_NAME, bindMarker(SCRIPT_NAME))) .ifExists()); }
@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(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")); }
@Override public void updateAuthCodeValidStatus(String authCode, boolean valid) { Update.Where stmt = QueryBuilder.update(KEYSPACE_NAME, AUTH_CODE_TABLE_NAME) .with(QueryBuilder.set("valid", valid)) .where(QueryBuilder.eq("code", authCode)) ; try { session.execute(stmt); } catch (NoHostAvailableException e) { log.error("No host in the %s cluster can be contacted to execute the query.\n", session.getCluster()); } catch (QueryExecutionException e) { log.error("An exception was thrown by Cassandra because it cannot " + "successfully execute the query with the specified consistency level."); } catch (QueryValidationException e) { log.error(String.format("The query %s \nis not valid, for example, incorrect syntax.\n", stmt.getQueryString())); } catch (IllegalStateException e) { log.error("The BoundStatement is not ready."); } }
@Override public void updateAccessTokenValidStatus(String accessToken, boolean valid) { Update.Where stmt = QueryBuilder.update(KEYSPACE_NAME, ACCESS_TOKEN_TABLE_NAME) .with(QueryBuilder.set("valid", valid)) .where(QueryBuilder.eq("access_token", accessToken)) ; try { session.execute(stmt); } catch (NoHostAvailableException e) { log.error("No host in the %s cluster can be contacted to execute the query.\n", session.getCluster()); } catch (QueryExecutionException e) { log.error("An exception was thrown by Cassandra because it cannot " + "successfully execute the query with the specified consistency level."); } catch (QueryValidationException e) { log.error(String.format("The query %s \nis not valid, for example, incorrect syntax.\n", stmt.getQueryString())); } catch (IllegalStateException e) { log.error("The BoundStatement is not ready."); } }
@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_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));"); }