private void incrementCounterInStorage( String queueName, DatabaseQueueMessage.Type type, long increment ) { Statement update = QueryBuilder.update( TABLE_MESSAGE_COUNTERS ) .where( QueryBuilder.eq( COLUMN_QUEUE_NAME, queueName ) ) .and( QueryBuilder.eq( COLUMN_MESSAGE_TYPE, type.toString() ) ) .with( QueryBuilder.incr( COLUMN_COUNTER_VALUE, increment ) ); cassandraClient.getQueueMessageSession().execute( update ); }
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 ); }
@Override public void resetCounter(String queueName, DatabaseQueueMessage.Type type) { // this sucks: "You cannot index, delete, or re-add a counter column" // https://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.html // so instead we decrement or increment the counter to zero // get value first, before resetting in memory counter long value = getCounterValue( queueName, type ); String key = buildKey( queueName, type ); InMemoryCount inMemoryCount = inMemoryCounters.get( key ); if ( inMemoryCount != null ) { inMemoryCount.reset(); } if ( value < 0 ) { Statement update = QueryBuilder.update( TABLE_MESSAGE_COUNTERS ) .where( QueryBuilder.eq( COLUMN_QUEUE_NAME, queueName ) ) .and( QueryBuilder.eq( COLUMN_MESSAGE_TYPE, type.toString() ) ) .with( QueryBuilder.incr( COLUMN_COUNTER_VALUE, -1 * value ) ); // incr must be positive cassandraClient.getQueueMessageSession().execute( update ); } else { 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, value ) ); cassandraClient.getQueueMessageSession().execute( update ); } }
/** * Generate an UPDATE FROM ... <strong>SET listofoptional = listofoptional + [?]</strong> */ @SuppressWarnings("static-access") public final EntityWithComplexTypes_Update.Cols AppendTo(final Optional<String> listOfOptional_element) { where.with(QueryBuilder.appendAll("listofoptional", QueryBuilder.bindMarker("listofoptional"))); boundValues.add(Arrays.asList(listOfOptional_element)); encodedValues.add(meta.listOfOptional.encodeFromJava(Arrays.asList(listOfOptional_element), Optional.of(cassandraOptions))); return new EntityWithComplexTypes_Update.Cols(where, cassandraOptions); }
/** * Generate an UPDATE FROM ... <strong>SET collectionIndex[index] = ?</strong> */ public final EntityWithComplexIndices_Update.Cols SetAtIndex(final int index, final String collectionIndex_element) { where.with(QueryBuilder.setIdx("collectionindex", index, QueryBuilder.bindMarker("collectionindex"))); boundValues.add(collectionIndex_element); encodedValues.add(meta.collectionIndex.valueProperty.encodeFromJava(collectionIndex_element, Optional.of(cassandraOptions))); return new EntityWithComplexIndices_Update.Cols(where, cassandraOptions); }
/** * Generate an UPDATE FROM ... <strong>SET collectionIndex = collectionIndex - ?</strong> */ @SuppressWarnings("static-access") public final EntityWithComplexIndices_Update.Cols RemoveAllFrom(final List<String> collectionIndex_element) { where.with(QueryBuilder.discardAll("collectionindex", QueryBuilder.bindMarker("collectionindex"))); boundValues.add(collectionIndex_element); encodedValues.add(meta.collectionIndex.encodeFromJava(collectionIndex_element, Optional.of(cassandraOptions))); return EntityWithComplexIndices_Update.Cols.this; }
/** * Generate an UPDATE FROM ... <strong>SET staticCounter = staticCounter - ?</strong> */ @SuppressWarnings("static-access") public final EntityWithComplexCounters_Update.Cols Decr(final Long staticCounter_decrement) { where.with(QueryBuilder.decr("static_count", QueryBuilder.bindMarker("static_count"))); boundValues.add(staticCounter_decrement); encodedValues.add(meta.staticCounter.encodeFromJava(staticCounter_decrement, Optional.of(cassandraOptions))); return EntityWithComplexCounters_Update.Cols.this; } }
/** * Generate an UPDATE FROM ... <strong>SET list = list - ?</strong> */ @SuppressWarnings("static-access") public final EntityWithBeanValidation_Update.Cols RemoveAllFrom(final java.util.List<String> list_element) { where.with(QueryBuilder.discardAll("list", QueryBuilder.bindMarker("list"))); boundValues.add(list_element); encodedValues.add(meta.list.encodeFromJava(list_element, Optional.of(cassandraOptions))); return new EntityWithBeanValidation_Update.Cols(where, cassandraOptions); }
/** * Generate an UPDATE FROM ... <strong>SET list = ? + list</strong> */ @SuppressWarnings("static-access") public final EntityWithBeanValidation_Update.Cols PrependAllTo(final java.util.List<String> list_element) { where.with(QueryBuilder.prependAll("list", QueryBuilder.bindMarker("list"))); boundValues.add(list_element); encodedValues.add(meta.list.encodeFromJava(list_element, Optional.of(cassandraOptions))); return new EntityWithBeanValidation_Update.Cols(where, cassandraOptions); }
/** * Generate an UPDATE FROM ... <strong>SET list[index] = null</strong> */ public final EntityWithBeanValidation_Update.Cols RemoveAtIndex(final int index) { where.with(QueryBuilder.setIdx("list", index, QueryBuilder.bindMarker("list"))); boundValues.add(null); encodedValues.add(null); return EntityWithBeanValidation_Update.Cols.this; }
/** * Generate an UPDATE FROM ... <strong>SET list = list + ?</strong> */ @SuppressWarnings("static-access") public final EntityWithBeanValidation_Update.Cols AppendAllTo(final java.util.List<String> list_element) { where.with(QueryBuilder.appendAll("list", QueryBuilder.bindMarker("list"))); boundValues.add(list_element); encodedValues.add(meta.list.encodeFromJava(list_element, Optional.of(cassandraOptions))); return EntityWithBeanValidation_Update.Cols.this; }
/** * Generate an UPDATE FROM ... <strong>SET value = ?</strong> */ @SuppressWarnings("static-access") public final EntityWithBeanValidation_Update.Cols Set(final String value) { where.with(NonEscapingSetAssignment.of("value", QueryBuilder.bindMarker("value"))); boundValues.add(value); encodedValues.add(meta.value.encodeFromJava(value, Optional.of(cassandraOptions))); return EntityWithBeanValidation_Update.Cols.this; } }
/** * Generate an UPDATE FROM ... <strong>SET value = ?</strong> */ @SuppressWarnings("static-access") public final EntityAsChild_Update.Cols Set(final String value) { where.with(NonEscapingSetAssignment.of("value", QueryBuilder.bindMarker("value"))); boundValues.add(value); encodedValues.add(meta.value.encodeFromJava(value, Optional.of(cassandraOptions))); return new EntityAsChild_Update.Cols(where, cassandraOptions); } }
/** * Generate an UPDATE FROM ... <strong>SET list = ?</strong> */ @SuppressWarnings("static-access") public final EntityWithCaseSensitivePK_Update.Cols Set(final java.util.List<String> list_element) { where.with(NonEscapingSetAssignment.of("\"listString\"", QueryBuilder.bindMarker("\"listString\""))); boundValues.add(list_element); encodedValues.add(meta.list.encodeFromJava(list_element, Optional.of(cassandraOptions))); return new EntityWithCaseSensitivePK_Update.Cols(where, cassandraOptions); } }
/** * Generate an UPDATE FROM ... <strong>SET "mapIntString"[?] = ?</strong> */ public final EntityWithCaseSensitivePK_Update.Cols PutTo(final Integer map_key, final String map_value) { where.with(QueryBuilder.put("\"mapIntString\"", QueryBuilder.bindMarker("map_key"), QueryBuilder.bindMarker("map_value"))); boundValues.add(map_key); boundValues.add(map_value); encodedValues.add(meta.map.keyProperty.encodeFromJava(map_key, Optional.of(cassandraOptions))); encodedValues.add(meta.map.valueProperty.encodeFromJava(map_value, Optional.of(cassandraOptions))); return EntityWithCaseSensitivePK_Update.Cols.this; }
update = update("foo").where().and(eq("k", 2)).with(set("b", null)); assertEquals(update.toString(), query); .and(eq("k", 2)) .and(eq("l", "foo")) .with(prependAll("b", Arrays.asList(3, 2, 1))) .using(timestamp(42)); assertEquals(update.toString(), query); .where(eq("k", 2)) .and(eq("l", "foo")) .with(prependAll("b", Arrays.asList(3, 2, 1))); assertEquals(update.toString(), query);
@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); }
.and( QueryBuilder.eq( COLUMN_SHARD_TYPE, type.toString() ) ) .and( QueryBuilder.eq( COLUMN_SHARD_ID, shardId ) ) .with( QueryBuilder.incr( COLUMN_COUNTER_VALUE, -1 * value ) ); // incr must be positive cassandraClient.getQueueMessageSession().execute( update ); .and( QueryBuilder.eq( COLUMN_SHARD_TYPE, type.toString() ) ) .and( QueryBuilder.eq( COLUMN_SHARD_ID, shardId ) ) .with( QueryBuilder.decr( COLUMN_COUNTER_VALUE, value ) ); cassandraClient.getQueueMessageSession().execute( update );
@Override public Optional<CassandraCredentials> updateStatus(String applicationId, String credentialsId, CredentialsStatus status) { LOG.debug("Updating credentials status with applicationID[{}] " + "and credentialsID[{}] to STATUS[{}]", applicationId, credentialsId, status.toString()); Update.Assignments query = update(getColumnFamilyName()) .where(eq(CREDENTIALS_ID_PROPERTY, credentialsId)) .and(eq(CREDENTIALS_APPLICATION_ID_PROPERTY, applicationId)) .with(set(CREDENTIALS_STATUS_PROPERTY, status.toString())); execute(query); return find(applicationId, credentialsId); }
void incrementCounterInStorage( String queueName, Shard.Type type, long shardId, long increment ) { Statement update = QueryBuilder.update( TABLE_COUNTERS ) .where( QueryBuilder.eq( COLUMN_QUEUE_NAME, queueName ) ) .and( QueryBuilder.eq( COLUMN_SHARD_TYPE, type.toString() ) ) .and( QueryBuilder.eq( COLUMN_SHARD_ID, shardId ) ) .with( QueryBuilder.incr( COLUMN_COUNTER_VALUE, increment ) ); cassandraClient.getQueueMessageSession().execute( update ); }