public final class QueryBuilder extends Object
Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)
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 bring the static methods of
this class into scope.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 |
cast(Object column,
DataType dataType)
Creates a Cast of a column using the given dataType.
|
static Object |
column(String name)
Declares that the name in argument should be treated as a column name.
|
static Clause |
contains(String name,
Object value)
Creates a "contains" where clause stating the provided column must contain
the value provided.
|
static Clause |
containsKey(String name,
Object key)
Creates a "contains key" where clause stating the provided column must contain
the key provided.
|
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(List<String> names,
List<?> values)
Creates an "equal" where clause for a group of clustering columns.
|
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 Object |
fromJson(Object json)
Creates a
fromJson() 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(List<String> names,
List<?> values)
Creates an "in" where clause for a group of clustering columns (a.k.a.
|
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 |
like(String name,
Object value)
Creates a "like" where clause stating that the provided column must be equal to the provided value.
|
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 Object |
path(String... segments)
Creates a path composed of the given path
segments . |
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(Object name,
Object value)
Advanced "set" assignment of a value to a column or a
UDT field. |
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)
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)
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.Metadata.quote(String)
public static String token(String columnName)
columnName
- the column name to take the token of."token(" + columnName + ")"
.public static String token(String... columnNames)
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 eq(List<String> names, List<?> values)
eq(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 like(String name, Object value)
name
- the column name.value
- the value.public 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 in(List<String> names, List<?> values)
in(Arrays.asList("a", "b"), Arrays.asList(Arrays.asList(1, "foo"), Arrays.asList(2, "bar")))
will generate the CQL WHERE clause (a, b) IN ((1, 'foo'), (2, 'bar'))
.
Each element in values
must be either a list
containing exactly as many values
as there are columns to match in names
,
or a bind marker
– in which case, that marker is to be considered as
a placeholder for one whole tuple of values to match.
Please note that this variant is only supported starting with Cassandra 2.0.9.names
- the column namesvalues
- the valuesIllegalArgumentException
- if the size of any tuple in values
is not equal to names.size()
,
or if values
contains elements that are neither lists
nor bind markers
.public static Clause contains(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause containsKey(String name, Object key)
name
- the column namekey
- the keypublic static Clause lt(String name, Object value)
name
- the column namevalue
- the valuepublic static Clause lt(List<String> names, List<?> values)
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)
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)
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)
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)
name = valueThe column name will only be quoted if it contains special characters, as in:
"a name that contains spaces" = valueOtherwise, if you want to force case sensitivity, use
quote(String)
:
set(quote("aCaseSensitiveName"), value)This method won't work to set UDT fields; use
set(Object, Object)
with a
path
instead:
set(path("udt", "field"), value)
name
- the column namevalue
- the value to assignpublic static Assignment set(Object name, Object value)
UDT
field.
This method is seldom preferable to set(String, Object)
; it is only useful:
path(String...)
):
set(path("udt", "field"), value)
raw(String)
).
There is no practical usage for this the time of writing, but it will serve as a workaround if new features are
added to Cassandra and you're using a older driver version that is not yet aware of them:
set(raw("some custom string"), value)
name
is String
, this method is equivalent to set(String, Object)
.name
- the column or UDT field namevalue
- the value to assignpublic static Assignment incr(String name)
name = name + 1
.name
- the column name to incrementpublic static Assignment incr(String name, long value)
name = name + value
.name
- the column name to incrementvalue
- the value by which to incrementpublic static Assignment incr(String name, BindMarker value)
name = name + value
.name
- the column name to incrementvalue
- a bind marker representing the value by which to incrementpublic static Assignment decr(String name)
name = name - 1
.name
- the column name to decrementpublic static Assignment decr(String name, long value)
name = name - value
.name
- the column name to decrementvalue
- the value by which to decrementpublic static Assignment decr(String name, BindMarker value)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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()
Insert i = QueryBuilder.insertInto("test").value("k", 0)
.value("c", QueryBuilder.bindMarker());
PreparedState p = session.prepare(i.toString());
public static BindMarker bindMarker(String name)
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)
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();" |
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 cast(Object column, DataType dataType)
column
- the column to cast.dataType
- the data type to cast to.public static Object now()
now()
function call.public static Object uuid()
uuid()
function call.public static Object column(String name)
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.public static Object path(String... segments)
segments
.
All provided path segments will be concatenated together with dots.
If any segment contains an identifier that needs quoting,
caller code is expected to call quote(String)
prior to
invoking this method.
This method is currently only useful when accessing individual fields of a
user-defined type
(UDT),
which is only possible since CASSANDRA-7423.
Note that currently nested UDT fields are not supported and
will be rejected by the server as a
syntax error
.segments
- the segments of the path to create.public static Object fromJson(Object json)
fromJson()
function call.
Support for JSON functions has been added in Cassandra 2.2.
The fromJson()
function is similar to INSERT JSON
statements,
but applies to a single column value instead of the entire row, and
converts a JSON object into the normal Cassandra column value.
It may be used in INSERT
and UPDATE
statements,
but NOT in the selection clause of a SELECT
statement.
The provided object can be of the following types:
SELECT JSON
statement on the same table.
Note that it is not possible to insert function calls nor bind markers in a JSON string.bind marker
. In this case, the statement is meant to be prepared
and no JSON string will be appended to the query string, only a bind marker for the whole JSON parameter.codec
is registered with the
CodecRegistry
in use. This allows the usage of JSON libraries, such
as the Java API for JSON processing,
the popular Jackson library, or
Google's Gson library, for instance.foo"'bar
should be inserted in the JSON string
as "foo\"''bar"
.json
- the JSON string, or a bind marker, or a JSON object handled by a specific codec
.Copyright © 2012–2018. All rights reserved.