Class QueryBuilder
- java.lang.Object
-
- com.datastax.driver.core.querybuilder.QueryBuilder
-
public final class QueryBuilder extends Object
Builds CQL3 query via a fluent API.The queries built by this builder will provide a value for the
Statement.getRoutingKey(com.datastax.driver.core.ProtocolVersion, com.datastax.driver.core.CodecRegistry)method only when aTableMetadatais provided to the builder. It is thus advised to do so if aTokenAwarePolicyis 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.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Assignmentadd(String name, Object value)Adds a value to a set column.static AssignmentaddAll(String name, BindMarker set)Adds a set of values to a set column.static AssignmentaddAll(String name, Set<?> set)Adds a set of values to a set column.static Objectalias(Object column, String alias)Creates an alias for a given column.static Assignmentappend(String name, Object value)Append a value to a list column.static AssignmentappendAll(String name, BindMarker list)Append a list of values to a list column.static AssignmentappendAll(String name, List<?> list)Append a list of values to a list column.static Orderingasc(String columnName)Ascending ordering for the provided column.static Objectavg(Object column)Creates anavg(x)built-in function call.static Batchbatch(RegularStatement... statements)Builds a newBATCHquery on the provided statements.static BindMarkerbindMarker()An object representing an anonymous bind marker (a question mark).static BindMarkerbindMarker(String name)An object representing a named bind marker.static Objectcast(Object column, DataType dataType)Creates a Cast of a column using the given dataType.static Objectcolumn(String name)Declares that the name in argument should be treated as a column name.static Clausecontains(String name, Object value)Creates a "contains"WHEREclause stating the provided column must contain the value provided.static ClausecontainsKey(String name, Object key)Creates a "contains key"WHEREclause stating the provided column must contain the key provided.static Objectcount(Object column)Creates acount(x)built-in function call.static Assignmentdecr(String name)Decrementation of a counter column.static Assignmentdecr(String name, long value)Decrementation of a counter column by a provided value.static Assignmentdecr(String name, BindMarker value)Decrementation of a counter column by a provided value.static Delete.Selectiondelete()Starts building a newDELETEquery.static Delete.Builderdelete(String... columns)Starts building a newDELETEquery that deletes the provided names.static Orderingdesc(String columnName)Descending ordering for the provided column.static Assignmentdiscard(String name, Object value)Discard a value from a list column.static AssignmentdiscardAll(String name, BindMarker list)Discard a list of values to a list column.static AssignmentdiscardAll(String name, List<?> list)Discard a list of values to a list column.static Clauseeq(Iterable<String> names, Iterable<?> values)Creates an "equal"WHEREclause for a group of clustering columns.static Clauseeq(String name, Object value)Creates an "equal"WHEREclause stating the provided column must be equal to the provided value.static Objectfcall(String name, Object... parameters)Creates a function call.static ObjectfromJson(Object json)Creates afromJson()function call.static Clausegt(Iterable<String> names, Iterable<?> values)Creates a "greater than"WHEREclause for a group of clustering columns.static Clausegt(String name, Object value)Creates a "greater than"WHEREclause stating the provided column must be greater to the provided value.static Clausegte(Iterable<String> names, Iterable<?> values)Creates a "greater than or equal"WHEREclause for a group of clustering columns.static Clausegte(String name, Object value)Creates a "greater than or equal"WHEREclause stating the provided column must be greater than or equal to the provided value.static Clausein(Iterable<String> names, Iterable<?> values)Creates an "in"WHEREclause for a group of clustering columns (a.k.a.static Clausein(String name, Iterable<?> values)Create an "in"WHEREclause stating the provided column must be equal to one of the provided values.static Clausein(String name, Object... values)Create an "in"WHEREclause stating the provided column must be equal to one of the provided values.static Assignmentincr(String name)Incrementation of a counter column.static Assignmentincr(String name, long value)Incrementation of a counter column by a provided value.static Assignmentincr(String name, BindMarker value)Incrementation of a counter column by a provided value.static InsertinsertInto(TableMetadata table)Starts building a newINSERTquery.static InsertinsertInto(String table)Starts building a newINSERTquery.static InsertinsertInto(String keyspace, String table)Starts building a newINSERTquery.static Clauselike(String name, Object value)Creates a "like"WHEREclause stating that the provided column must be equal to the provided value.static Clauselt(Iterable<String> names, Iterable<?> values)Creates a "lesser than"WHEREclause for a group of clustering columns.static Clauselt(String name, Object value)Creates a "lesser than"WHEREclause stating the provided column must be less than the provided value.static Clauselte(Iterable<String> names, Iterable<?> values)Creates a "lesser than or equal"WHEREclause for a group of clustering columns.static Clauselte(String name, Object value)Creates a "lesser than or equal"WHEREclause stating the provided column must be lesser than or equal to the provided value.static Objectmax(Object column)Creates amax(x)built-in function call.static Objectmin(Object column)Creates amin(x)built-in function call.static Clausene(Iterable<String> names, Iterable<?> values)Creates an "not equal"WHEREclause for a group of clustering columns.static Clausene(String name, Object value)Creates a "not equal"WHEREclause stating the provided column must be different from the provided value.static ClausenotNull(String name)Creates an "IS NOT NULL"WHEREclause for the provided column.static Objectnow()Creates anow()function call.static Objectpath(String... segments)Creates a path composed of the given pathsegments.static Assignmentprepend(String name, Object value)Prepend a value to a list column.static AssignmentprependAll(String name, BindMarker list)Prepend a list of values to a list column.static AssignmentprependAll(String name, List<?> list)Prepend a list of values to a list column.static Assignmentput(String name, Object key, Object value)Puts a new key/value pair to a map column.static AssignmentputAll(String name, BindMarker map)Puts a map of new key/value pairs to a map column.static AssignmentputAll(String name, Map<?,?> map)Puts a map of new key/value pairs to a map column.static Stringquote(String columnName)Quotes a column name to make it case sensitive.static Objectraw(String str)Protects a value from any interpretation by the query builder.static Assignmentremove(String name, Object value)Remove a value from a set column.static AssignmentremoveAll(String name, BindMarker set)Remove a set of values from a set column.static AssignmentremoveAll(String name, Set<?> set)Remove a set of values from a set column.static Select.Selectionselect()Starts building a newSELECTquery.static Select.Builderselect(Object... columns)Starts building a newSELECTquery that selects the provided names.static Select.Builderselect(String... columns)Starts building a newSELECTquery that selects the provided names.static Assignmentset(Object name, Object value)Advanced "set" assignment of a value to a column or aUDTfield.static Assignmentset(String name, Object value)Simple "set" assignment of a value to a column.static AssignmentsetIdx(String name, int idx, Object value)Sets a list column value by index.static Objectsum(Object column)Creates asum(x)built-in function call.static Usingtimeout(Duration timeout)Adds aUSING TIMEOUTclause to this statement with aDurationvalue.static Usingtimeout(BindMarker marker)Adds aUSING TIMEOUTclause to this statement with a bind marker.static Usingtimestamp(long timestamp)Option to set the timestamp for a modification query (insert, update or delete).static Usingtimestamp(BindMarker marker)Option to prepare the timestamp (in microseconds) for a modification query (insert, update or delete).static ObjecttoJson(Object column)Creates atoJson()function call.static Objecttoken(Object... values)Returns a generictokenfunction call.static Stringtoken(String columnName)The token of a column name.static Stringtoken(String... columnNames)The token of column names.static Truncatetruncate(TableMetadata table)Creates a newTRUNCATEquery.static Truncatetruncate(String table)Creates a newTRUNCATEquery.static Truncatetruncate(String keyspace, String table)Creates a newTRUNCATEquery.static Usingttl(int ttl)Option to set the ttl for a modification query (insert, update or delete).static Usingttl(BindMarker marker)Option to prepare the ttl (in seconds) for a modification query (insert, update or delete).static BatchunloggedBatch(RegularStatement... statements)Builds a newUNLOGGED BATCHquery on the provided statements.static Updateupdate(TableMetadata table)Starts building a newUPDATEquery.static Updateupdate(String table)Starts building a newUPDATEquery.static Updateupdate(String keyspace, String table)Starts building a newUPDATEquery.static Objectuuid()Creates auuid()function call.
-
-
-
Method Detail
-
select
public static Select.Builder select(String... columns)
Starts building a newSELECTquery that selects the provided names.Note that
select(c1, c2)is just a shortcut forselect().column(c1).column(c2).- Parameters:
columns- the columns names that should be selected by the query.- Returns:
- an in-construction
SELECTquery (you will need to provide at least aFROMclause to complete the query).
-
select
public static Select.Builder select(Object... columns)
Starts building a newSELECTquery that selects the provided names.Note that
select(c1, c2)is just a shortcut forselect().column(c1).column(c2).- Parameters:
columns- the columns names that should be selected by the query.- Returns:
- an in-construction
SELECTquery (you will need to provide at least aFROMclause to complete the query).
-
select
public static Select.Selection select()
Starts building a newSELECTquery.- Returns:
- an in-construction
SELECTquery (you will need to provide a column selection and at least aFROMclause to complete the query).
-
insertInto
public static Insert insertInto(String table)
Starts building a newINSERTquery.- Parameters:
table- the name of the table in which to insert.- Returns:
- an in-construction
INSERTquery.
-
insertInto
public static Insert insertInto(String keyspace, String table)
Starts building a newINSERTquery.- Parameters:
keyspace- the name of the keyspace to use.table- the name of the table to insert into.- Returns:
- an in-construction
INSERTquery.
-
insertInto
public static Insert insertInto(TableMetadata table)
Starts building a newINSERTquery.- Parameters:
table- the name of the table to insert into.- Returns:
- an in-construction
INSERTquery.
-
update
public static Update update(String table)
Starts building a newUPDATEquery.- Parameters:
table- the name of the table to update.- Returns:
- an in-construction
UPDATEquery (at least aSETand aWHEREclause needs to be provided to complete the query).
-
update
public static Update update(String keyspace, String table)
Starts building a newUPDATEquery.- Parameters:
keyspace- the name of the keyspace to use.table- the name of the table to update.- Returns:
- an in-construction
UPDATEquery (at least aSETand aWHEREclause needs to be provided to complete the query).
-
update
public static Update update(TableMetadata table)
Starts building a newUPDATEquery.- Parameters:
table- the name of the table to update.- Returns:
- an in-construction
UPDATEquery (at least aSETand aWHEREclause needs to be provided to complete the query).
-
delete
public static Delete.Builder delete(String... columns)
Starts building a newDELETEquery that deletes the provided names.- Parameters:
columns- the columns names that should be deleted by the query.- Returns:
- an in-construction
DELETEquery (At least aFROMand aWHEREclause needs to be provided to complete the query).
-
delete
public static Delete.Selection delete()
Starts building a newDELETEquery.- Returns:
- an in-construction
DELETEquery (you will need to provide a column selection and at least aFROMand aWHEREclause to complete the query).
-
batch
public static Batch batch(RegularStatement... statements)
Builds a newBATCHquery on the provided 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 aCOUNTERbatch even though COUNTER batches are never logged (so for counters, using this method is effectively equivalent to usingunloggedBatch(com.datastax.driver.core.RegularStatement...)).- Parameters:
statements- the statements to batch.- Returns:
- a new
RegularStatementthat batchstatements.
-
unloggedBatch
public static Batch unloggedBatch(RegularStatement... statements)
Builds a newUNLOGGED BATCHquery on the provided 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
COUNTERone.- Parameters:
statements- the statements to batch.- Returns:
- a new
RegularStatementthat batchstatementswithout using the batch log.
-
truncate
public static Truncate truncate(String table)
Creates a newTRUNCATEquery.- Parameters:
table- the name of the table to truncate.- Returns:
- the truncation query.
-
truncate
public static Truncate truncate(String keyspace, String table)
Creates a newTRUNCATEquery.- Parameters:
keyspace- the name of the keyspace to use.table- the name of the table to truncate.- Returns:
- the truncation query.
-
truncate
public static Truncate truncate(TableMetadata table)
Creates a newTRUNCATEquery.- Parameters:
table- the table to truncate.- Returns:
- the truncation query.
-
quote
public static String quote(String columnName)
Quotes a column name to make it case sensitive.- Parameters:
columnName- the column name to quote.- Returns:
- the quoted column name.
- See Also:
Metadata.quote(String)
-
token
public static String token(String columnName)
The token of a column name.- Parameters:
columnName- the column name to take the token of.- Returns:
"token(" + columnName + ")".
-
token
public static String token(String... columnNames)
The token of column names.This variant is most useful when the partition key is composite.
- Parameters:
columnNames- the column names to take the token of.- Returns:
- a string representing the token of the provided column names.
-
token
public static Object token(Object... values)
Returns a generictokenfunction call.- Parameters:
values- the arguments of thetokenfunction.- Returns:
tokenfunction call.
-
eq
public static Clause eq(String name, Object value)
Creates an "equal"WHEREclause stating the provided column must be equal to the provided value.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
eq
public static Clause eq(Iterable<String> names, Iterable<?> values)
Creates an "equal"WHEREclause for a group of clustering columns.For instance,
eq(Arrays.asList("a", "b"), Arrays.asList(2, "test"))will generate the CQLWHEREclause(a, b) = (2, 'test').Please note that this variant is only supported starting with Cassandra 2.0.6.
- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().
-
ne
public static Clause ne(String name, Object value)
Creates a "not equal"WHEREclause stating the provided column must be different from the provided value.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
ne
public static Clause ne(Iterable<String> names, Iterable<?> values)
Creates an "not equal"WHEREclause for a group of clustering columns.For instance,
eq(Arrays.asList("a", "b"), Arrays.asList(2, "test"))will generate the CQLWHEREclause(a, b) != (2, 'test').- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().
-
notNull
public static Clause notNull(String name)
Creates an "IS NOT NULL"WHEREclause for the provided column.- Parameters:
name- the column name- Returns:
- the corresponding where clause.
-
like
public static Clause like(String name, Object value)
Creates a "like"WHEREclause stating that the provided column must be equal to the provided value.- Parameters:
name- the column name.value- the value.- Returns:
- the corresponding where clause.
-
in
public static Clause in(String name, Object... values)
Create an "in"WHEREclause stating the provided column must be equal to one of the provided values.- Parameters:
name- the column namevalues- the values- Returns:
- the corresponding where clause.
-
in
public static Clause in(String name, Iterable<?> values)
Create an "in"WHEREclause stating the provided column must be equal to one of the provided values.- Parameters:
name- the column namevalues- the values- Returns:
- the corresponding where clause.
-
in
public static Clause in(Iterable<String> names, Iterable<?> values)
Creates an "in"WHEREclause for a group of clustering columns (a.k.a. "multi-column IN restriction").For instance,
in(Arrays.asList("a", "b"), Arrays.asList(Arrays.asList(1, "foo"), Arrays.asList(2, "bar")))will generate the CQLWHEREclause(a, b) IN ((1, 'foo'), (2, 'bar')).Each element in
valuesmust be either aniterablecontaining exactly as many values as there are columns to match innames, or abind 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.
- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- if the size of any tuple invaluesis not equal tonames.size(), or ifvaluescontains elements that are neitherlistsnorbind markers.
-
contains
public static Clause contains(String name, Object value)
Creates a "contains"WHEREclause stating the provided column must contain the value provided.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
containsKey
public static Clause containsKey(String name, Object key)
Creates a "contains key"WHEREclause stating the provided column must contain the key provided.- Parameters:
name- the column namekey- the key- Returns:
- the corresponding where clause.
-
lt
public static Clause lt(String name, Object value)
Creates a "lesser than"WHEREclause stating the provided column must be less than the provided value.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
lt
public static Clause lt(Iterable<String> names, Iterable<?> values)
Creates a "lesser than"WHEREclause for a group of clustering columns.For instance,
lt(Arrays.asList("a", "b"), Arrays.asList(2, "test"))will generate the CQLWHEREclause(a, b) < (2, 'test').Please note that this variant is only supported starting with Cassandra 2.0.6.
- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().
-
lte
public static Clause lte(String name, Object value)
Creates a "lesser than or equal"WHEREclause stating the provided column must be lesser than or equal to the provided value.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
lte
public static Clause lte(Iterable<String> names, Iterable<?> values)
Creates a "lesser than or equal"WHEREclause for a group of clustering columns.For instance,
lte(Arrays.asList("a", "b"), Arrays.asList(2, "test"))will generate the CQLWHEREclause(a, b) <e; (2, 'test').Please note that this variant is only supported starting with Cassandra 2.0.6.
- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().
-
gt
public static Clause gt(String name, Object value)
Creates a "greater than"WHEREclause stating the provided column must be greater to the provided value.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
gt
public static Clause gt(Iterable<String> names, Iterable<?> values)
Creates a "greater than"WHEREclause for a group of clustering columns.For instance,
gt(Arrays.asList("a", "b"), Arrays.asList(2, "test"))will generate the CQLWHEREclause(a, b) > (2, 'test').Please note that this variant is only supported starting with Cassandra 2.0.6.
- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().
-
gte
public static Clause gte(String name, Object value)
Creates a "greater than or equal"WHEREclause stating the provided column must be greater than or equal to the provided value.- Parameters:
name- the column namevalue- the value- Returns:
- the corresponding where clause.
-
gte
public static Clause gte(Iterable<String> names, Iterable<?> values)
Creates a "greater than or equal"WHEREclause for a group of clustering columns.For instance,
gte(Arrays.asList("a", "b"), Arrays.asList(2, "test"))will generate the CQLWHEREclause(a, b) >e; (2, 'test').Please note that this variant is only supported starting with Cassandra 2.0.6.
- Parameters:
names- the column namesvalues- the values- Returns:
- the corresponding where clause.
- Throws:
IllegalArgumentException- ifnames.size() != values.size().
-
asc
public static Ordering asc(String columnName)
Ascending ordering for the provided column.- Parameters:
columnName- the column name- Returns:
- the corresponding ordering
-
desc
public static Ordering desc(String columnName)
Descending ordering for the provided column.- Parameters:
columnName- the column name- Returns:
- the corresponding ordering
-
timestamp
public static Using timestamp(long timestamp)
Option to set the timestamp for a modification query (insert, update or delete).- Parameters:
timestamp- the timestamp (in microseconds) to use.- Returns:
- the corresponding option
- Throws:
IllegalArgumentException- iftimestamp < 0.
-
timestamp
public static Using timestamp(BindMarker marker)
Option to prepare the timestamp (in microseconds) for a modification query (insert, update or delete).- Parameters:
marker- bind marker to use for the timestamp.- Returns:
- the corresponding option.
-
ttl
public static Using ttl(int ttl)
Option to set the ttl for a modification query (insert, update or delete).- Parameters:
ttl- the ttl (in seconds) to use.- Returns:
- the corresponding option
- Throws:
IllegalArgumentException- ifttl < 0.
-
ttl
public static Using ttl(BindMarker marker)
Option to prepare the ttl (in seconds) for a modification query (insert, update or delete).- Parameters:
marker- bind marker to use for the ttl.- Returns:
- the corresponding option
-
timeout
public static Using timeout(Duration timeout)
Adds aUSING TIMEOUTclause to this statement with aDurationvalue.If this method or
timeout(BindMarker)is called multiple times, the value from the last invocation is used.- Parameters:
timeout- A timeout value controlling server-side query timeout.
-
timeout
public static Using timeout(BindMarker marker)
Adds aUSING TIMEOUTclause to this statement with a bind marker.If this method or
timeout(Duration)is called multiple times, the value from the last invocation is used.- Parameters:
marker- A bind marker understood asDurationcontrolling server-side query timeout.
-
set
public static Assignment set(String name, Object value)
Simple "set" assignment of a value to a column.This will generate:
name = value
The column name will only be quoted if it contains special characters, as in:"a name that contains spaces" = value
Otherwise, if you want to force case sensitivity, usequote(String):set(quote("aCaseSensitiveName"), value)This method won't work to set UDT fields; useset(Object, Object)with apathinstead:set(path("udt", "field"), value)- Parameters:
name- the column namevalue- the value to assign- Returns:
- the correspond assignment (to use in an update query)
-
set
public static Assignment set(Object name, Object value)
Advanced "set" assignment of a value to a column or aUDTfield.This method is seldom preferable to
set(String, Object); it is only useful:- when assigning values to individual fields of a UDT (see
path(String...)):set(path("udt", "field"), value) - if you wish to pass a "raw" string that will get appended as-is to the query (see
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)
nameisString, this method is equivalent toset(String, Object).- Parameters:
name- the column or UDT field namevalue- the value to assign- Returns:
- the correspond assignment (to use in an update query)
- when assigning values to individual fields of a UDT (see
-
incr
public static Assignment incr(String name)
Incrementation of a counter column.This will generate:
name = name + 1.- Parameters:
name- the column name to increment- Returns:
- the correspond assignment (to use in an update query)
-
incr
public static Assignment incr(String name, long value)
Incrementation of a counter column by a provided value.This will generate:
name = name + value.- Parameters:
name- the column name to incrementvalue- the value by which to increment- Returns:
- the correspond assignment (to use in an update query)
-
incr
public static Assignment incr(String name, BindMarker value)
Incrementation of a counter column by a provided value.This will generate:
name = name + value.- Parameters:
name- the column name to incrementvalue- a bind marker representing the value by which to increment- Returns:
- the correspond assignment (to use in an update query)
-
decr
public static Assignment decr(String name)
Decrementation of a counter column.This will generate:
name = name - 1.- Parameters:
name- the column name to decrement- Returns:
- the correspond assignment (to use in an update query)
-
decr
public static Assignment decr(String name, long value)
Decrementation of a counter column by a provided value.This will generate:
name = name - value.- Parameters:
name- the column name to decrementvalue- the value by which to decrement- Returns:
- the correspond assignment (to use in an update query)
-
decr
public static Assignment decr(String name, BindMarker value)
Decrementation of a counter column by a provided value.This will generate:
name = name - value.- Parameters:
name- the column name to decrementvalue- a bind marker representing the value by which to decrement- Returns:
- the correspond assignment (to use in an update query)
-
prepend
public static Assignment prepend(String name, Object value)
Prepend a value to a list column.This will generate:
name = [ value ] + name.- Parameters:
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 useQueryBuilder#prependAllwith a singleton list.- Returns:
- the correspond assignment (to use in an update query)
-
prependAll
public static Assignment prependAll(String name, List<?> list)
Prepend a list of values to a list column.This will generate:
name = list + name.- Parameters:
name- the column name (must be of type list).list- the list of values to prepend.- Returns:
- the correspond assignment (to use in an update query)
-
prependAll
public static Assignment prependAll(String name, BindMarker list)
Prepend a list of values to a list column.This will generate:
name = list + name.- Parameters:
name- the column name (must be of type list).list- a bind marker representing the list of values to prepend.- Returns:
- the correspond assignment (to use in an update query)
-
append
public static Assignment append(String name, Object value)
Append a value to a list column.This will generate:
name = name + [value].- Parameters:
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 useQueryBuilder#appendAllwith a singleton list.- Returns:
- the correspond assignment (to use in an update query)
-
appendAll
public static Assignment appendAll(String name, List<?> list)
Append a list of values to a list column.This will generate:
name = name + list.- Parameters:
name- the column name (must be of type list).list- the list of values to append- Returns:
- the correspond assignment (to use in an update query)
-
appendAll
public static Assignment appendAll(String name, BindMarker list)
Append a list of values to a list column.This will generate:
name = name + list.- Parameters:
name- the column name (must be of type list).list- a bind marker representing the list of values to append- Returns:
- the correspond assignment (to use in an update query)
-
discard
public static Assignment discard(String name, Object value)
Discard a value from a list column.This will generate:
name = name - [value].- Parameters:
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 useQueryBuilder#discardAllwith a singleton list.- Returns:
- the correspond assignment (to use in an update query)
-
discardAll
public static Assignment discardAll(String name, List<?> list)
Discard a list of values to a list column.This will generate:
name = name - list.- Parameters:
name- the column name (must be of type list).list- the list of values to discard- Returns:
- the correspond assignment (to use in an update query)
-
discardAll
public static Assignment discardAll(String name, BindMarker list)
Discard a list of values to a list column.This will generate:
name = name - list.- Parameters:
name- the column name (must be of type list).list- a bind marker representing the list of values to discard- Returns:
- the correspond assignment (to use in an update query)
-
setIdx
public static Assignment setIdx(String name, int idx, Object value)
Sets a list column value by index.This will generate:
name[idx] = value.- Parameters:
name- the column name (must be of type list).idx- the index to setvalue- the value to set- Returns:
- the correspond assignment (to use in an update query)
-
add
public static Assignment add(String name, Object value)
Adds a value to a set column.This will generate:
name = name + {value}.- Parameters:
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 useQueryBuilder#addAllwith a singleton set.- Returns:
- the correspond assignment (to use in an update query)
-
addAll
public static Assignment addAll(String name, Set<?> set)
Adds a set of values to a set column.This will generate:
name = name + set.- Parameters:
name- the column name (must be of type set).set- the set of values to append- Returns:
- the correspond assignment (to use in an update query)
-
addAll
public static Assignment addAll(String name, BindMarker set)
Adds a set of values to a set column.This will generate:
name = name + set.- Parameters:
name- the column name (must be of type set).set- a bind marker representing the set of values to append- Returns:
- the correspond assignment (to use in an update query)
-
remove
public static Assignment remove(String name, Object value)
Remove a value from a set column.This will generate:
name = name - {value}.- Parameters:
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 useQueryBuilder#removeAllwith a singleton set.- Returns:
- the correspond assignment (to use in an update query)
-
removeAll
public static Assignment removeAll(String name, Set<?> set)
Remove a set of values from a set column.This will generate:
name = name - set.- Parameters:
name- the column name (must be of type set).set- the set of values to remove- Returns:
- the correspond assignment (to use in an update query)
-
removeAll
public static Assignment removeAll(String name, BindMarker set)
Remove a set of values from a set column.This will generate:
name = name - set.- Parameters:
name- the column name (must be of type set).set- a bind marker representing the set of values to remove- Returns:
- the correspond assignment (to use in an update query)
-
put
public static Assignment put(String name, Object key, Object value)
Puts a new key/value pair to a map column.This will generate:
name[key] = value.- Parameters:
name- the column name (must be of type map).key- the key to putvalue- the value to put- Returns:
- the correspond assignment (to use in an update query)
-
putAll
public static Assignment putAll(String name, Map<?,?> map)
Puts a map of new key/value pairs to a map column.This will generate:
name = name + map.- Parameters:
name- the column name (must be of type map).map- the map of key/value pairs to put- Returns:
- the correspond assignment (to use in an update query)
-
putAll
public static Assignment putAll(String name, BindMarker map)
Puts a map of new key/value pairs to a map column.This will generate:
name = name + map.- Parameters:
name- the column name (must be of type map).map- a bind marker representing the map of key/value pairs to put- Returns:
- the correspond assignment (to use in an update query)
-
bindMarker
public static BindMarker bindMarker()
An object representing an anonymous bind marker (a question mark).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()); PreparedStatement p = session.prepare(i.toString());- Returns:
- a new bind marker.
-
bindMarker
public static BindMarker bindMarker(String name)
An object representing a named bind marker.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")); PreparedStatement p = session.prepare(i.toString());Please note that named bind makers are only supported starting with Cassandra 2.0.1.
- Parameters:
name- the name for the bind marker.- Returns:
- an object representing a bind marker named
name.
-
raw
public static Object raw(String str)
Protects a value from any interpretation by the query builder.The following table exemplify the behavior of this function:
Note: the 2nd and 3rd examples in this table are not a valid CQL3 queries.Examples of use 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.
- Parameters:
str- the raw value to use as a string- Returns:
- the value but protected from being interpreted/escaped by the query builder.
-
fcall
public static Object fcall(String name, Object... parameters)
Creates a function call.- Parameters:
name- the name of the function to call.parameters- the parameters for the function.- Returns:
- the function call.
-
cast
public static Object cast(Object column, DataType dataType)
Creates a Cast of a column using the given dataType.- Parameters:
column- the column to cast.dataType- the data type to cast to.- Returns:
- the casted column.
-
now
public static Object now()
Creates anow()function call.- Returns:
- the function call.
-
uuid
public static Object uuid()
Creates auuid()function call.- Returns:
- the function call.
-
column
public static Object column(String name)
Declares that the name in argument should be treated as a column 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.- Parameters:
name- the name of the column.- Returns:
- the name as a column name.
-
path
public static Object path(String... segments)
Creates a path composed of the given pathsegments.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.- Parameters:
segments- the segments of the path to create.- Returns:
- the segments concatenated as a single path.
- See Also:
- CASSANDRA-7423
-
fromJson
public static Object fromJson(Object json)
Creates afromJson()function call.Support for JSON functions has been added in Cassandra 2.2. The
fromJson()function is similar toINSERT JSONstatements, 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
INSERTandUPDATEstatements, but NOT in the selection clause of aSELECTstatement.The provided object can be of the following types:
- A raw string. In this case, it will be appended to the query string as is. It
should NOT be surrounded by single quotes. Its format should generally match
that returned by a
SELECT JSONstatement on the same table. Note that it is not possible to insert function calls nor bind markers in a JSON string. - A
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. - Any object that can be serialized to JSON. Such objects can be used provided that a
matching
codecis registered with theCodecRegistryin 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.
When passing raw strings to this method, the following rules apply:
- String values should be enclosed in double quotes.
- Double quotes appearing inside strings should be escaped with a backslash, but single
quotes should be escaped in the CQL manner, i.e. by another single quote. For example,
the column value
foo"'barshould be inserted in the JSON string as"foo\"''bar".
- Parameters:
json- the JSON string, or a bind marker, or a JSON object handled by a specificcodec.- Returns:
- the function call.
- See Also:
- JSON Support for CQL, JSON Support in Cassandra 2.2
- A raw string. In this case, it will be appended to the query string as is. It
should NOT be surrounded by single quotes. Its format should generally match
that returned by a
-
toJson
public static Object toJson(Object column)
Creates atoJson()function call. This is a shortcut forfcall("toJson", QueryBuilder.column(name)).Support for JSON functions has been added in Cassandra 2.2. The
toJson()function is similar toSELECT JSONstatements, but applies to a single column value instead of the entire row, and produces a JSON-encoded string representing the normal Cassandra column value.It may only be used in the selection clause of a
SELECTstatement.- Parameters:
column- the column to retrieve JSON from.- Returns:
- the function call.
- See Also:
- JSON Support for CQL, JSON Support in Cassandra 2.2
-
alias
public static Object alias(Object column, String alias)
Creates an alias for a given column.This is most useful when used with the method
select(Object...).- Parameters:
column- The column to create an alias for.alias- The column alias.- Returns:
- a column alias.
-
count
public static Object count(Object column)
Creates acount(x)built-in function call.- Returns:
- the function call.
-
max
public static Object max(Object column)
Creates amax(x)built-in function call.- Returns:
- the function call.
-
min
public static Object min(Object column)
Creates amin(x)built-in function call.- Returns:
- the function call.
-
sum
public static Object sum(Object column)
Creates asum(x)built-in function call.- Returns:
- the function call.
-
-