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