public final class QueryBuilder extends Object
The queries built by this builder will provide a value for the
Statement.getRoutingKey()
method only when a
TableMetadata
is provided to the builder.
It is thus advised to do so if a TokenAwarePolicy
is in use.
The provider builders perform very little validation of the built query. There is thus no guarantee that a built query is valid, and it is definitively possible to create invalid queries.
Note that it could be convenient to use an 'import static' to use the methods of this class.
Modifier and Type | Method and Description |
---|---|
static Assignment |
add(String name,
Object value)
Adds a value to a set column.
|
static Assignment |
addAll(String name,
BindMarker set)
Adds a set of values to a set column.
|
static Assignment |
addAll(String name,
Set<?> set)
Adds a set of values to a set column.
|
static Assignment |
append(String name,
Object value)
Append a value to a list column.
|
static Assignment |
appendAll(String name,
BindMarker list)
Append a list of values to a list column.
|
static Assignment |
appendAll(String name,
List<?> list)
Append a list of values to a list column.
|
static Ordering |
asc(String columnName)
Ascending ordering for the provided column.
|
static Batch |
batch(RegularStatement... statements)
Built a new BATCH query on the provided statements.
|
static BindMarker |
bindMarker()
An object representing an anonymous bind marker (a question mark).
|
static BindMarker |
bindMarker(String name)
An object representing a named bind marker.
|
static Object |
column(String name)
Declares that the name in argument should be treated as a column name.
|
static Assignment |
decr(String name)
Decrementation of a counter column.
|
static Assignment |
decr(String name,
BindMarker value)
Decrementation of a counter column by a provided value.
|
static Assignment |
decr(String name,
long value)
Decrementation of a counter column by a provided value.
|
static Delete.Selection |
delete()
Start building a new DELETE query.
|
static Delete.Builder |
delete(String... columns)
Start building a new DELETE query that deletes the provided names.
|
static Ordering |
desc(String columnName)
Descending ordering for the provided column.
|
static Assignment |
discard(String name,
Object value)
Discard a value from a list column.
|
static Assignment |
discardAll(String name,
BindMarker list)
Discard a list of values to a list column.
|
static Assignment |
discardAll(String name,
List<?> list)
Discard a list of values to a list column.
|
static Clause |
eq(String name,
Object value)
Creates an "equal" where clause stating the provided column must be
equal to the provided value.
|
static Object |
fcall(String name,
Object... parameters)
Creates a function call.
|
static Clause |
gt(List<String> names,
List<?> values)
Creates a "greater than" where clause for a group of clustering columns.
|
static Clause |
gt(String name,
Object value)
Creates a "greater than" where clause stating the provided column must
be greater to the provided value.
|
static Clause |
gte(List<String> names,
List<?> values)
Creates a "greater than or equal" where clause for a group of clustering columns.
|
static Clause |
gte(String name,
Object value)
Creates a "greater than or equal" where clause stating the provided
column must be greater than or equal to the provided value.
|
static Clause |
in(String name,
List<?> values)
Create an "in" where clause stating the provided column must be equal
to one of the provided values.
|
static Clause |
in(String name,
Object... values)
Create an "in" where clause stating the provided column must be equal
to one of the provided values.
|
static Assignment |
incr(String name)
Incrementation of a counter column.
|
static Assignment |
incr(String name,
BindMarker value)
Incrementation of a counter column by a provided value.
|
static Assignment |
incr(String name,
long value)
Incrementation of a counter column by a provided value.
|
static Insert |
insertInto(String table)
Start building a new INSERT query.
|
static Insert |
insertInto(String keyspace,
String table)
Start building a new INSERT query.
|
static Insert |
insertInto(TableMetadata table)
Start building a new INSERT query.
|
static Clause |
lt(List<String> names,
List<?> values)
Creates a "lesser than" where clause for a group of clustering columns.
|
static Clause |
lt(String name,
Object value)
Creates a "lesser than" where clause stating the provided column must be less than
the provided value.
|
static Clause |
lte(List<String> names,
List<?> values)
Creates a "lesser than or equal" where clause for a group of clustering columns.
|
static Clause |
lte(String name,
Object value)
Creates a "lesser than or equal" where clause stating the provided column must
be lesser than or equal to the provided value.
|
static Object |
now()
Creates a
now() function call. |
static Assignment |
prepend(String name,
Object value)
Prepend a value to a list column.
|
static Assignment |
prependAll(String name,
BindMarker list)
Prepend a list of values to a list column.
|
static Assignment |
prependAll(String name,
List<?> list)
Prepend a list of values to a list column.
|
static Assignment |
put(String name,
Object key,
Object value)
Puts a new key/value pair to a map column.
|
static Assignment |
putAll(String name,
BindMarker map)
Puts a map of new key/value pairs to a map column.
|
static Assignment |
putAll(String name,
Map<?,?> map)
Puts a map of new key/value pairs to a map column.
|
static String |
quote(String columnName)
Quotes a columnName to make it case sensitive.
|
static Object |
raw(String str)
Protects a value from any interpretation by the query builder.
|
static Assignment |
remove(String name,
Object value)
Remove a value from a set column.
|
static Assignment |
removeAll(String name,
BindMarker set)
Remove a set of values from a set column.
|
static Assignment |
removeAll(String name,
Set<?> set)
Remove a set of values from a set column.
|
static Select.Selection |
select()
Start building a new SELECT query.
|
static Select.Builder |
select(String... columns)
Start building a new SELECT query that selects the provided names.
|
static Assignment |
set(String name,
Object value)
Simple "set" assignment of a value to a column.
|
static Assignment |
setIdx(String name,
int idx,
Object value)
Sets a list column value by index.
|
static Using |
timestamp(BindMarker marker)
Option to prepare the timestamp (in microseconds) for a modification query (insert, update or delete).
|
static Using |
timestamp(long timestamp)
Option to set the timestamp for a modification query (insert, update or delete).
|
static String |
token(String... columnNames)
The token of column names.
|
static String |
token(String columnName)
The token of a column name.
|
static Truncate |
truncate(String table)
Creates a new TRUNCATE query.
|
static Truncate |
truncate(String keyspace,
String table)
Creates a new TRUNCATE query.
|
static Truncate |
truncate(TableMetadata table)
Creates a new TRUNCATE query.
|
static Using |
ttl(BindMarker marker)
Option to prepare the ttl (in seconds) for a modification query (insert, update or delete).
|
static Using |
ttl(int ttl)
Option to set the ttl for a modification query (insert, update or delete).
|
static Batch |
unloggedBatch(RegularStatement... statements)
Built a new UNLOGGED BATCH query on the provided statements.
|
static Update |
update(String table)
Start building a new UPDATE query.
|
static Update |
update(String keyspace,
String table)
Start building a new UPDATE query.
|
static Update |
update(TableMetadata table)
Start building a new UPDATE query.
|
static Object |
uuid()
Creates a
uuid() function call. |
public static Select.Builder select(String... columns)
select(c1, c2)
is just a shortcut for select().column(c1).column(c2)
.columns
- the columns names that should be selected by the query.public static Select.Selection select()
public static Insert insertInto(String table)
table
- the name of the table in which to insert.public static Insert insertInto(String keyspace, String table)
keyspace
- the name of the keyspace to use.table
- the name of the table to insert into.public static Insert insertInto(TableMetadata table)
table
- the name of the table to insert into.public static Update update(String table)
table
- the name of the table to update.public static Update update(String keyspace, String table)
keyspace
- the name of the keyspace to use.table
- the name of the table to update.public static Update update(TableMetadata table)
table
- the name of the table to update.public static Delete.Builder delete(String... columns)
columns
- the columns names that should be deleted by the query.public static Delete.Selection delete()
public static Batch batch(RegularStatement... statements)
This method will build a logged batch (this is the default in CQL3). To
create unlogged batches, use unloggedBatch(com.datastax.driver.core.RegularStatement...)
. Also note that
for convenience, if the provided statements are counter statements, this
method will create a COUNTER batch even though COUNTER batches are never
logged (so for counters, using this method is effectively equivalent to
using unloggedBatch(com.datastax.driver.core.RegularStatement...)
).
statements
- the statements to batch.RegularStatement
that batch statements
.public static Batch unloggedBatch(RegularStatement... statements)
Compared to logged batches (the default), unlogged batch don't use the distributed batch log server side and as such are not guaranteed to be atomic. In other words, if an unlogged batch timeout, some of the batched statements may have been persisted while some have not. Unlogged batch will however be slightly faster than logged batch.
If the statements added to the batch are counter statements, the resulting batch will be a COUNTER one.
statements
- the statements to batch.RegularStatement
that batch statements
without
using the batch log.public static Truncate truncate(String table)
table
- the name of the table to truncate.public static Truncate truncate(String keyspace, String table)
keyspace
- the name of the keyspace to use.table
- the name of the table to truncate.public static Truncate truncate(TableMetadata table)
table
- the table to truncate.public static String quote(String columnName)
columnName
- the column name to quote.public static String token(String columnName)
columnName
- the column name to take the token of."token(" + columnName + ")"
.public static String token(String... columnNames)
This variant is most useful when the partition key is composite.
columnNames
- the column names to take the token of.public static Clause eq(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause in(String name, Object... values)
name
- the column namevalues
- the valuespublic static Clause in(String name, List<?> values)
name
- the column namevalues
- the valuespublic static Clause lt(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause lt(List<String> names, List<?> values)
For instance, lt(Arrays.asList("a", "b"), Arrays.asList(2, "test"))
will generate the CQL WHERE clause (a, b) < (2, 'test')
.
Please note that this variant is only supported starting with Cassandra 2.0.6.
names
- the column namesvalues
- the valuesIllegalArgumentException
- if names.size() != values.size()
.public static Clause lte(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause lte(List<String> names, List<?> values)
For instance, lte(Arrays.asList("a", "b"), Arrays.asList(2, "test"))
will generate the CQL WHERE clause (a, b) <e; (2, 'test')
.
Please note that this variant is only supported starting with Cassandra 2.0.6.
names
- the column namesvalues
- the valuesIllegalArgumentException
- if names.size() != values.size()
.public static Clause gt(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause gt(List<String> names, List<?> values)
For instance, gt(Arrays.asList("a", "b"), Arrays.asList(2, "test"))
will generate the CQL WHERE clause (a, b) > (2, 'test')
.
Please note that this variant is only supported starting with Cassandra 2.0.6.
names
- the column namesvalues
- the valuesIllegalArgumentException
- if names.size() != values.size()
.public static Clause gte(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause gte(List<String> names, List<?> values)
For instance, gte(Arrays.asList("a", "b"), Arrays.asList(2, "test"))
will generate the CQL WHERE clause (a, b) >e; (2, 'test')
.
Please note that this variant is only supported starting with Cassandra 2.0.6.
names
- the column namesvalues
- the valuesIllegalArgumentException
- if names.size() != values.size()
.public static Ordering asc(String columnName)
columnName
- the column namepublic static Ordering desc(String columnName)
columnName
- the column namepublic static Using timestamp(long timestamp)
timestamp
- the timestamp (in microseconds) to use.IllegalArgumentException
- if timestamp > 0
.public static Using timestamp(BindMarker marker)
marker
- bind marker to use for the timestamp.public static Using ttl(int ttl)
ttl
- the ttl (in seconds) to use.IllegalArgumentException
- if ttl > 0
.public static Using ttl(BindMarker marker)
marker
- bind marker to use for the ttl.public static Assignment set(String name, Object value)
This will generate: name = value
.
name
- the column namevalue
- the value to assignpublic static Assignment incr(String name)
This will generate: name = name + 1
.
name
- the column name to incrementpublic static Assignment incr(String name, long value)
This will generate: name = name + value
.
name
- the column name to incrementvalue
- the value by which to incrementpublic static Assignment incr(String name, BindMarker value)
This will generate: name = name + value
.
name
- the column name to incrementvalue
- a bind marker representing the value by which to incrementpublic static Assignment decr(String name)
This will generate: name = name - 1
.
name
- the column name to decrementpublic static Assignment decr(String name, long value)
This will generate: name = name - value
.
name
- the column name to decrementvalue
- the value by which to decrementpublic static Assignment decr(String name, BindMarker value)
This will generate: name = name - value
.
name
- the column name to decrementvalue
- a bind marker representing the value by which to decrementpublic static Assignment prepend(String name, Object value)
This will generate: name = [ value ] + name
.
name
- the column name (must be of type list).value
- the value to prepend. Using a BindMarker here is not supported.
To use a BindMarker use QueryBuilder#prependAll
with a
singleton list.public static Assignment prependAll(String name, List<?> list)
This will generate: name = list + name
.
name
- the column name (must be of type list).list
- the list of values to prepend.public static Assignment prependAll(String name, BindMarker list)
This will generate: name = list + name
.
name
- the column name (must be of type list).list
- a bind marker representing the list of values to prepend.public static Assignment append(String name, Object value)
This will generate: name = name + [value]
.
name
- the column name (must be of type list).value
- the value to append. Using a BindMarker here is not supported.
To use a BindMarker use QueryBuilder#appendAll
with a
singleton list.public static Assignment appendAll(String name, List<?> list)
This will generate: name = name + list
.
name
- the column name (must be of type list).list
- the list of values to appendpublic static Assignment appendAll(String name, BindMarker list)
This will generate: name = name + list
.
name
- the column name (must be of type list).list
- a bind marker representing the list of values to appendpublic static Assignment discard(String name, Object value)
This will generate: name = name - [value]
.
name
- the column name (must be of type list).value
- the value to discard. Using a BindMarker here is not supported.
To use a BindMarker use QueryBuilder#discardAll
with a singleton list.public static Assignment discardAll(String name, List<?> list)
This will generate: name = name - list
.
name
- the column name (must be of type list).list
- the list of values to discardpublic static Assignment discardAll(String name, BindMarker list)
This will generate: name = name - list
.
name
- the column name (must be of type list).list
- a bind marker representing the list of values to discardpublic static Assignment setIdx(String name, int idx, Object value)
This will generate: name[idx] = value
.
name
- the column name (must be of type list).idx
- the index to setvalue
- the value to setpublic static Assignment add(String name, Object value)
This will generate: name = name + {value}
.
name
- the column name (must be of type set).value
- the value to add. Using a BindMarker here is not supported.
To use a BindMarker use QueryBuilder#addAll
with a
singleton set.public static Assignment addAll(String name, Set<?> set)
This will generate: name = name + set
.
name
- the column name (must be of type set).set
- the set of values to appendpublic static Assignment addAll(String name, BindMarker set)
This will generate: name = name + set
.
name
- the column name (must be of type set).set
- a bind marker representing the set of values to appendpublic static Assignment remove(String name, Object value)
This will generate: name = name - {value}
.
name
- the column name (must be of type set).value
- the value to remove. Using a BindMarker here is not supported.
To use a BindMarker use QueryBuilder#removeAll
with a singleton set.public static Assignment removeAll(String name, Set<?> set)
This will generate: name = name - set
.
name
- the column name (must be of type set).set
- the set of values to removepublic static Assignment removeAll(String name, BindMarker set)
This will generate: name = name - set
.
name
- the column name (must be of type set).set
- a bind marker representing the set of values to removepublic static Assignment put(String name, Object key, Object value)
This will generate: name[key] = value
.
name
- the column name (must be of type map).key
- the key to putvalue
- the value to putpublic static Assignment putAll(String name, Map<?,?> map)
This will generate: name = name + map
.
name
- the column name (must be of type map).map
- the map of key/value pairs to putpublic static Assignment putAll(String name, BindMarker map)
This will generate: name = name + map
.
name
- the column name (must be of type map).map
- a bind marker representing the map of key/value pairs to putpublic static BindMarker bindMarker()
This can be used wherever a value is expected. For instance, one can do:
Insert i = QueryBuilder.insertInto("test").value("k", 0)
.value("c", QueryBuilder.bindMarker());
PreparedState p = session.prepare(i.toString());
public static BindMarker bindMarker(String name)
This can be used wherever a value is expected. For instance, one can do:
Insert i = QueryBuilder.insertInto("test").value("k", 0)
.value("c", QueryBuilder.bindMarker("c_val"));
PreparedState p = session.prepare(i.toString());
Please note that named bind makers are only supported starting with Cassandra 2.0.1.
name
- the name for the bind marker.name
.public static Object raw(String str)
The following table exemplify the behavior of this function:
Code | Resulting query string |
---|---|
select().from("t").where(eq("c", "C'est la vie!")); | "SELECT * FROM t WHERE c='C''est la vie!';" |
select().from("t").where(eq("c", raw("C'est la vie!"))); | "SELECT * FROM t WHERE c=C'est la vie!;" |
select().from("t").where(eq("c", raw("'C'est la vie!'"))); | "SELECT * FROM t WHERE c='C'est la vie!';" |
select().from("t").where(eq("c", "now()")); | "SELECT * FROM t WHERE c='now()';" |
select().from("t").where(eq("c", raw("now()"))); | "SELECT * FROM t WHERE c=now();" |
The use of that method is generally discouraged since it lead to security risks. However, if you know what you are doing, it allows to escape the interpretations done by the QueryBuilder.
str
- the raw value to use as a stringpublic static Object fcall(String name, Object... parameters)
name
- the name of the function to call.parameters
- the parameters for the function.public static Object now()
now()
function call.public static Object uuid()
uuid()
function call.public static Object column(String name)
This mainly meant for use with Select.Selection.fcall(java.lang.String, java.lang.Object...)
when a
function should apply to a column name, not a string value.
name
- the name of the column.Copyright © 2012–2015. All rights reserved.