Elffuss BraidHub

kikocis/java-driver public

6cbc334fa86f · 4.7 KiBRenderedRawPermalink
1## Simple statements
2
3Use [SimpleStatement] for queries that will be executed only once (or a few times) in your application:
4
5```java
6SimpleStatement statement = new SimpleStatement(
7 "SELECT value FROM application_params WHERE name = 'greeting_message'");
8session.execute(statement);
9```
10
11When you don't need to customize anything on the `SimpleStatement` object, there is a convenient shortcut:
12
13```java
14session.execute("SELECT value FROM application_params WHERE name = 'greeting_message'");
15```
16
17Each time you execute a simple statement, Cassandra will parse the query string again; nothing is cached (neither on the
18client nor on the server):
19
20```ditaa
21client driver Cassandra
22--+----------------------------------+---------------------+------
23 | | |
24 | session.execute(SimpleStatement) | |
25 |--------------------------------->| |
26 | | QUERY(query_string) |
27 | |-------------------->|
28 | | |
29 | | |
30 | | | - parse query string
31 | | | - execute query
32 | | |
33 | | ROWS |
34 | |<--------------------|
35 | | |
36 |<---------------------------------| |
37```
38
39If you execute the same query often (or a similar query with different column values), consider a
40[prepared statement](../prepared/) instead.
41
42
43### Using values
44
45Instead of sending a raw query string, you can use anonymous bind markers and provide values separately:
46
47```java
48String paramName = ...
49session.execute(
50 "SELECT value FROM application_params WHERE name = ?",
51 paramName);
52```
53
54This syntax has a few advantages:
55
56* if the values already come from some other part of your code, it looks cleaner than doing the concatenation yourself;
57* you don't need to translate the values to their string representation. The driver will sent them alongside the query,
58 in their serialized binary form.
59
60The number of values must match the query string, and their types must match the database schema. Note that the driver
61does not parse query strings, so it cannot perform those checks on the client side; if you make a mistake, the query
62will be sent anyway, and the error will be caught by Cassandra (`InvalidQueryException` is a server-side error):
63
64```java
65session.execute(
66 "SELECT value FROM application_params WHERE name = ?",
67 "foo", "bar");
68// Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException:
69// Invalid amount of bind variables
70```
71
72### Value type inference
73
74Another consequence of not parsing query strings is that the driver has to make a guess on how to serialize values,
75based on their Java type (see the [default type mappings](../../#cql-to-java-type-mapping)). This can be tricky, in
76particular for numeric types:
77
78```java
79// schema: create table bigints(b bigint primary key)
80session.execute(
81 "INSERT INTO bigints (b) VALUES (?)",
82 1);
83// Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException:
84// Expected 8 or 0 byte long (4)
85```
86
87The problem here is that the literal `1` has the Java type `int`. So the driver serializes it as a CQL `int` (4 bytes),
88but the server expects a CQL `bigint` (8 bytes). The fix is to specify the correct Java type:
89
90```java
91session.execute(
92 "INSERT INTO bigints (b) VALUES (?)",
93 1L);
94```
95
96In the same vein, strings are always serialized to `varchar`, so you could have a problem if you target an `ascii`
97column:
98
99```java
100// schema: create table ascii_quotes(id int primary key, t ascii)
101session.execute(
102 "INSERT INTO ascii_quotes (id, t) VALUES (?, ?)",
103 1, "Touché sir, touché...");
104// Exception in thread "main" com.datastax.driver.core.exceptions.InvalidQueryException:
105// Invalid byte for ascii: -61
106```
107
108In that situation, there is no way to hint at the correct type. Your only option is to serialize the value manually:
109
110```java
111ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersionEnum();
112ByteBuffer bytes = DataType.ascii().serialize("Touché sir, touché...", protocolVersion);
113session.execute(
114 "INSERT INTO ascii_quotes (id, t) VALUES (?, ?)",
115 1, bytes);
116```
117
118[SimpleStatement]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/SimpleStatement.html