Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Query builder Conditions

Conditions¶

A condition is a clause that appears after the IF keyword in a conditional UPDATE or DELETE statement.

The easiest way to add a condition is with an ifXxx method in the fluent API:

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .ifColumn("v1").isEqualTo(literal(1))
    .ifColumn("v2").isEqualTo(literal(2));
// DELETE FROM user WHERE k=? IF v1=1 AND v2=2    

You can also create it manually with one of the factory methods in Condition, and then pass it to if_():

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.*;

Condition vCondition = Condition.column("v").isEqualTo(literal(1));
deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .if_(vCondition);
// DELETE FROM user WHERE k=? IF v=1

If you call if_() multiple times, the clauses will be joined with the AND keyword. You can also add multiple conditions in a single call. This is a bit more efficient since it creates less temporary objects:

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .if_(
        Condition.column("v1").isEqualTo(literal(1)), 
        Condition.column("v2").isEqualTo(literal(2)));
// DELETE FROM user WHERE k=? IF v1=1 AND v2=2

Conditions are composed of a left operand, an operator, and a right-hand-side term.

Simple columns¶

ifColumn operates on a single column. It supports basic arithmetic comparison operators:

| Comparison operator | Method name | |———————|————————–| | = | isEqualTo | | < | isLessThan | | <= | isLessThanOrEqualTo | | > | isGreaterThan | | >= | isGreaterThanOrEqualTo | | != | isNotEqualTo |

Note: we support != because it is present in the CQL grammar but, as of Cassandra 4, it is not implemented yet.

In addition, in() can test for equality with various alternatives. You can either provide each alternative as a term:

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .ifColumn("v").in(bindMarker(), bindMarker(), bindMarker());
// DELETE FROM user WHERE k=? IF v IN (?,?,?)

Or bind the whole list of alternatives as a single variable:

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .ifColumn("v").in(bindMarker());
// DELETE FROM user WHERE k=? IF v IN ?

UDT fields¶

ifField tests a field in a top-level UDT (nested UDTs are not allowed):

deleteFrom("user")
    .whereColumn("k").isEqualTo(bindMarker())
    .ifField("address", "zip").isEqualTo(literal(94040));
// DELETE FROM user WHERE k=? IF address.zip=94040

It supports the same set of operators as simple columns.

Collection elements¶

ifElement tests an element in a top-level collection (nested collections are not allowed):

deleteFrom("product")
    .whereColumn("sku").isEqualTo(bindMarker())
    .ifElement("features", literal("color")).in(literal("red"), literal("blue"));
// DELETE FROM product WHERE sku=? IF features['color'] IN ('red','blue')

It supports the same set of operators as simple columns.

Raw snippets¶

You can also provide a condition as a raw CQL snippet, that will get appended to the query as-is, without any syntax checking or escaping:

deleteFrom("product")
    .whereColumn("sku").isEqualTo(bindMarker())
    .ifRaw("features['color'] IN ('red', 'blue') /*some random comment*/");
// DELETE FROM product WHERE sku=? IF features['color'] IN ('red', 'blue') /*some random comment*/

This should be used with caution, as it’s possible to generate invalid CQL that will fail at execution time; on the other hand, it can be used as a workaround to handle new CQL features that are not yet covered by the query builder.

IF EXISTS¶

Finally, you can specify an IF EXISTS clause:

deleteFrom("product").whereColumn("sku").isEqualTo(bindMarker()).ifExists();
// DELETE FROM product WHERE sku=? IF EXISTS

It is mutually exclusive with column conditions: if you previously specified column conditions on the statement, they will be ignored; conversely, adding a column condition cancels a previous IF EXISTS clause.

PREVIOUS
Query builder
NEXT
DELETE
  • 4.10.0.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Conditions
    • Simple columns
    • UDT fields
    • Collection elements
    • Raw snippets
    • IF EXISTS
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2