kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 325be1be75f938fa45f5343aa9daf1d7766641910b5f36ebd6185241f1e37c48 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-12-07 18:46 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-12-07 18:46 +01:00 |
| Parents | a3674bcf4e77, 2b008721b1bb |
71 files changed
modified changelog/README.md +1 −0
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | - [improvement] JAVA-863: Idempotence propagation in prepared statements. |
| 12 | 12 | - [improvement] JAVA-996: Make CodecRegistry available to ProtocolDecoder. |
| 13 | 13 | - [bug] JAVA-819: Driver shouldn't retry on client timeout if statement is not idempotent. |
| 14 | +- [improvement] JAVA-1007: Make SimpleStatement and QueryBuilder "detached" again. | |
| 14 | 15 | |
| 15 | 16 | Merged from 2.1 branch: |
| 16 | 17 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +7 −21
| @@ -36,7 +36,7 @@ | ||
| 36 | 36 | */ |
| 37 | 37 | @Override |
| 38 | 38 | public ResultSet execute(String query) { |
| 39 | - return execute(newSimpleStatement(query)); | |
| 39 | + return execute(new SimpleStatement(query)); | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | */ |
| 45 | 45 | @Override |
| 46 | 46 | public ResultSet execute(String query, Object... values) { |
| 47 | - return execute(newSimpleStatement(query, values)); | |
| 47 | + return execute(new SimpleStatement(query, values)); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
| @@ -60,7 +60,7 @@ | ||
| 60 | 60 | */ |
| 61 | 61 | @Override |
| 62 | 62 | public ResultSetFuture executeAsync(String query) { |
| 63 | - return executeAsync(newSimpleStatement(query)); | |
| 63 | + return executeAsync(new SimpleStatement(query)); | |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| @@ -68,23 +68,7 @@ | ||
| 68 | 68 | */ |
| 69 | 69 | @Override |
| 70 | 70 | public ResultSetFuture executeAsync(String query, Object... values) { |
| 71 | - return executeAsync(newSimpleStatement(query, values)); | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * {@inheritDoc} | |
| 76 | - */ | |
| 77 | - @Override | |
| 78 | - public SimpleStatement newSimpleStatement(String query) { | |
| 79 | - return new SimpleStatement(query, this.getCluster()); | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * {@inheritDoc} | |
| 84 | - */ | |
| 85 | - @Override | |
| 86 | - public SimpleStatement newSimpleStatement(String query, Object... values) { | |
| 87 | - return new SimpleStatement(query, this.getCluster(), values); | |
| 71 | + return executeAsync(new SimpleStatement(query, values)); | |
| 88 | 72 | } |
| 89 | 73 | |
| 90 | 74 | /** |
| @@ -132,7 +116,9 @@ | ||
| 132 | 116 | return Futures.transform(prepared, new Function<PreparedStatement, PreparedStatement>() { |
| 133 | 117 | @Override |
| 134 | 118 | public PreparedStatement apply(PreparedStatement prepared) { |
| 135 | - ByteBuffer routingKey = statement.getRoutingKey(); | |
| 119 | + ProtocolVersion protocolVersion = getCluster().getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 120 | + CodecRegistry codecRegistry = getCluster().getConfiguration().getCodecRegistry(); | |
| 121 | + ByteBuffer routingKey = statement.getRoutingKey(protocolVersion, codecRegistry); | |
| 136 | 122 | if (routingKey != null) |
| 137 | 123 | prepared.setRoutingKey(routingKey); |
| 138 | 124 | prepared.setConsistencyLevel(statement.getConsistencyLevel()); |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +12 −8
| @@ -45,10 +45,12 @@ | ||
| 45 | 45 | private final boolean wasApplied; |
| 46 | 46 | |
| 47 | 47 | protected final ProtocolVersion protocolVersion; |
| 48 | + protected final CodecRegistry codecRegistry; | |
| 48 | 49 | |
| 49 | - private ArrayBackedResultSet(ColumnDefinitions metadata, Token.Factory tokenFactory, List<ByteBuffer> firstRow, ProtocolVersion protocolVersion) { | |
| 50 | + private ArrayBackedResultSet(ColumnDefinitions metadata, Token.Factory tokenFactory, List<ByteBuffer> firstRow, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 50 | 51 | this.metadata = metadata; |
| 51 | 52 | this.protocolVersion = protocolVersion; |
| 53 | + this.codecRegistry = codecRegistry; | |
| 52 | 54 | this.tokenFactory = tokenFactory; |
| 53 | 55 | this.wasApplied = checkWasApplied(firstRow, metadata, protocolVersion); |
| 54 | 56 | } |
| @@ -79,8 +81,8 @@ | ||
| 79 | 81 | assert r.metadata.pagingState == null || info != null; |
| 80 | 82 | |
| 81 | 83 | return r.metadata.pagingState == null |
| 82 | - ? new SinglePage(columnDefs, tokenFactory, protocolVersion, r.data, info) | |
| 83 | - : new MultiPage(columnDefs, tokenFactory, protocolVersion, r.data, info, r.metadata.pagingState, session, statement); | |
| 84 | + ? new SinglePage(columnDefs, tokenFactory, protocolVersion, columnDefs.codecRegistry, r.data, info) | |
| 85 | + : new MultiPage(columnDefs, tokenFactory, protocolVersion, columnDefs.codecRegistry, r.data, info, r.metadata.pagingState, session, statement); | |
| 84 | 86 | |
| 85 | 87 | case SET_KEYSPACE: |
| 86 | 88 | case SCHEMA_CHANGE: |
| @@ -105,7 +107,7 @@ | ||
| 105 | 107 | |
| 106 | 108 | private static ArrayBackedResultSet empty(ExecutionInfo info) { |
| 107 | 109 | // We could pass the protocol version but we know we won't need it so passing a bogus value (null) |
| 108 | - return new SinglePage(ColumnDefinitions.EMPTY, null, null, EMPTY_QUEUE, info); | |
| 110 | + return new SinglePage(ColumnDefinitions.EMPTY, null, null, null, EMPTY_QUEUE, info); | |
| 109 | 111 | } |
| 110 | 112 | |
| 111 | 113 | public ColumnDefinitions getColumnDefinitions() { |
| @@ -166,9 +168,10 @@ | ||
| 166 | 168 | private SinglePage(ColumnDefinitions metadata, |
| 167 | 169 | Token.Factory tokenFactory, |
| 168 | 170 | ProtocolVersion protocolVersion, |
| 171 | + CodecRegistry codecRegistry, | |
| 169 | 172 | Queue<List<ByteBuffer>> rows, |
| 170 | 173 | ExecutionInfo info) { |
| 171 | - super(metadata, tokenFactory, rows.peek(), protocolVersion); | |
| 174 | + super(metadata, tokenFactory, rows.peek(), protocolVersion, codecRegistry); | |
| 172 | 175 | this.info = info; |
| 173 | 176 | this.rows = rows; |
| 174 | 177 | } |
| @@ -230,6 +233,7 @@ | ||
| 230 | 233 | private MultiPage(ColumnDefinitions metadata, |
| 231 | 234 | Token.Factory tokenFactory, |
| 232 | 235 | ProtocolVersion protocolVersion, |
| 236 | + CodecRegistry codecRegistry, | |
| 233 | 237 | Queue<List<ByteBuffer>> rows, |
| 234 | 238 | ExecutionInfo info, |
| 235 | 239 | ByteBuffer pagingState, |
| @@ -239,9 +243,9 @@ | ||
| 239 | 243 | // Note: as of Cassandra 2.1.0, it turns out that the result of a CAS update is never paged, so |
| 240 | 244 | // we could hard-code the result of wasApplied in this class to "true". However, we can not be sure |
| 241 | 245 | // that this will never change, so apply the generic check by peeking at the first row. |
| 242 | - super(metadata, tokenFactory, rows.peek(), protocolVersion); | |
| 246 | + super(metadata, tokenFactory, rows.peek(), protocolVersion, codecRegistry); | |
| 243 | 247 | this.currentPage = rows; |
| 244 | - this.infos.offer(info.withPagingState(pagingState, protocolVersion).withStatement(statement)); | |
| 248 | + this.infos.offer(info.withPagingState(pagingState, protocolVersion, codecRegistry).withStatement(statement)); | |
| 245 | 249 | |
| 246 | 250 | this.fetchState = new FetchingState(pagingState, null); |
| 247 | 251 | this.session = session; |
| @@ -338,7 +342,7 @@ | ||
| 338 | 342 | if (rm.kind == Responses.Result.Kind.ROWS) { |
| 339 | 343 | Responses.Result.Rows rows = (Responses.Result.Rows)rm; |
| 340 | 344 | if (rows.metadata.pagingState != null) |
| 341 | - info = info.withPagingState(rows.metadata.pagingState, protocolVersion).withStatement(statement); | |
| 345 | + info = info.withPagingState(rows.metadata.pagingState, protocolVersion, codecRegistry).withStatement(statement); | |
| 342 | 346 | MultiPage.this.nextPages.offer(rows.data); |
| 343 | 347 | MultiPage.this.fetchState = rows.metadata.pagingState == null ? null : new FetchingState(rows.metadata.pagingState, null); |
| 344 | 348 | } else if (rm.kind == Responses.Result.Kind.VOID) { |
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +4 −4
| @@ -81,14 +81,14 @@ | ||
| 81 | 81 | this.batchType = batchType; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - IdAndValues getIdAndValues() { | |
| 84 | + IdAndValues getIdAndValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 85 | 85 | IdAndValues idAndVals = new IdAndValues(statements.size()); |
| 86 | 86 | for (Statement statement : statements) { |
| 87 | 87 | if(statement instanceof StatementWrapper) |
| 88 | 88 | statement = ((StatementWrapper) statement).getWrappedStatement(); |
| 89 | 89 | if (statement instanceof RegularStatement) { |
| 90 | 90 | RegularStatement st = (RegularStatement)statement; |
| 91 | - ByteBuffer[] vals = st.getValues(); | |
| 91 | + ByteBuffer[] vals = st.getValues(protocolVersion, codecRegistry); | |
| 92 | 92 | String query = st.getQueryString(); |
| 93 | 93 | idAndVals.ids.add(query); |
| 94 | 94 | idAndVals.values.add(vals == null ? Collections.<ByteBuffer>emptyList() : Arrays.asList(vals)); |
| @@ -213,11 +213,11 @@ | ||
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | @Override |
| 216 | - public ByteBuffer getRoutingKey() { | |
| 216 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 217 | 217 | for (Statement statement : statements) { |
| 218 | 218 | if(statement instanceof StatementWrapper) |
| 219 | 219 | statement = ((StatementWrapper) statement).getWrappedStatement(); |
| 220 | - ByteBuffer rk = statement.getRoutingKey(); | |
| 220 | + ByteBuffer rk = statement.getRoutingKey(protocolVersion, codecRegistry); | |
| 221 | 221 | if (rk != null) |
| 222 | 222 | return rk; |
| 223 | 223 | } |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +5 −1
| @@ -222,9 +222,13 @@ | ||
| 222 | 222 | * precedence. If the routing key has been set through {@link PreparedStatement#setRoutingKey} then that is used |
| 223 | 223 | * next. If neither of those are set then it is computed. |
| 224 | 224 | * |
| 225 | + * @param protocolVersion unused by this implementation (no internal serialization is required to compute the key). | |
| 226 | + * @param codecRegistry unused by this implementation (no internal serialization is required to compute the key). | |
| 227 | + * | |
| 225 | 228 | * @return the routing key for this statement or {@code null}. |
| 226 | 229 | */ |
| 227 | - public ByteBuffer getRoutingKey() { | |
| 230 | + @Override | |
| 231 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 228 | 232 | if (this.routingKey != null) { |
| 229 | 233 | return this.routingKey; |
| 230 | 234 | } |
modified driver-core/src/main/java/com/datastax/driver/core/CodecRegistry.java +3 −3
| @@ -88,8 +88,8 @@ | ||
| 88 | 88 | * <li>all driver types that implement {@link GettableByIndexData}, {@link GettableByNameData}, |
| 89 | 89 | * {@link SettableByIndexData} and/or {@link SettableByNameData}. Namely: {@link Row}, |
| 90 | 90 | * {@link BoundStatement}, {@link UDTValue} and {@link TupleValue};</li> |
| 91 | - * <li>{@link Session#newSimpleStatement(String, Object...) simple statements};</li> | |
| 92 | - * <li>statements created with the {@link com.datastax.driver.core.querybuilder.QueryBuilder#QueryBuilder(Cluster) Query builder}.</li> | |
| 91 | + * <li>{@link SimpleStatement(String, Object...) simple statements};</li> | |
| 92 | + * <li>statements created with the {@link com.datastax.driver.core.querybuilder.QueryBuilder Query builder}.</li> | |
| 93 | 93 | * </ul> |
| 94 | 94 | * |
| 95 | 95 | * Example: |
| @@ -393,7 +393,7 @@ | ||
| 393 | 393 | * Furthermore, this method returns the first matching codec, regardless of its accepted CQL type. |
| 394 | 394 | * It should be reserved for situations where the target CQL type is not available or unknown. |
| 395 | 395 | * In the Java driver, this happens mainly when serializing a value in a |
| 396 | - * {@link Session#newSimpleStatement(String, Object...) SimpleStatement} or in the | |
| 396 | + * {@link SimpleStatement(String, Object...) SimpleStatement} or in the | |
| 397 | 397 | * {@link com.datastax.driver.core.querybuilder.QueryBuilder}, where no CQL type information is available. |
| 398 | 398 | * <p> |
| 399 | 399 | * Codecs returned by this method are <em>NOT</em> cached (see the {@link CodecRegistry top-level documentation} |
modified driver-core/src/main/java/com/datastax/driver/core/ExecutionInfo.java +11 −9
| @@ -34,17 +34,19 @@ | ||
| 34 | 34 | private final QueryTrace trace; |
| 35 | 35 | private final ByteBuffer pagingState; |
| 36 | 36 | private final ProtocolVersion protocolVersion; |
| 37 | + private final CodecRegistry codecRegistry; | |
| 37 | 38 | private final Statement statement; |
| 38 | 39 | private volatile boolean schemaInAgreement; |
| 39 | 40 | private final List<String> warnings; |
| 40 | 41 | private final Map<String, ByteBuffer> incomingPayload; |
| 41 | 42 | |
| 42 | - private ExecutionInfo(List<Host> triedHosts, ConsistencyLevel achievedConsistency, QueryTrace trace, ByteBuffer pagingState, ProtocolVersion protocolVersion, Statement statement, boolean schemaAgreement, List<String> warnings, Map<String, ByteBuffer> incomingPayload) { | |
| 43 | + private ExecutionInfo(List<Host> triedHosts, ConsistencyLevel achievedConsistency, QueryTrace trace, ByteBuffer pagingState, ProtocolVersion protocolVersion, CodecRegistry codecRegistry, Statement statement, boolean schemaAgreement, List<String> warnings, Map<String, ByteBuffer> incomingPayload) { | |
| 43 | 44 | this.triedHosts = triedHosts; |
| 44 | 45 | this.achievedConsistency = achievedConsistency; |
| 45 | 46 | this.trace = trace; |
| 46 | 47 | this.pagingState = pagingState; |
| 47 | 48 | this.protocolVersion = protocolVersion; |
| 49 | + this.codecRegistry = codecRegistry; | |
| 48 | 50 | this.statement = statement; |
| 49 | 51 | this.schemaInAgreement = schemaAgreement; |
| 50 | 52 | this.warnings = warnings; |
| @@ -52,27 +54,27 @@ | ||
| 52 | 54 | } |
| 53 | 55 | |
| 54 | 56 | ExecutionInfo(List<Host> triedHosts) { |
| 55 | - this(triedHosts, null, null, null, null, null, true, Collections.<String>emptyList(), null); | |
| 57 | + this(triedHosts, null, null, null, null, null, null, true, Collections.<String>emptyList(), null); | |
| 56 | 58 | } |
| 57 | 59 | |
| 58 | 60 | ExecutionInfo withTraceAndWarnings(QueryTrace newTrace, List<String> warnings) { |
| 59 | - return new ExecutionInfo(triedHosts, achievedConsistency, newTrace, pagingState, protocolVersion, statement, schemaInAgreement, warnings, incomingPayload); | |
| 61 | + return new ExecutionInfo(triedHosts, achievedConsistency, newTrace, pagingState, protocolVersion, codecRegistry, statement, schemaInAgreement, warnings, incomingPayload); | |
| 60 | 62 | } |
| 61 | 63 | |
| 62 | 64 | ExecutionInfo withAchievedConsistency(ConsistencyLevel newConsistency) { |
| 63 | - return new ExecutionInfo(triedHosts, newConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement, warnings, incomingPayload); | |
| 65 | + return new ExecutionInfo(triedHosts, newConsistency, trace, pagingState, protocolVersion, codecRegistry, statement, schemaInAgreement, warnings, incomingPayload); | |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | - ExecutionInfo withPagingState(ByteBuffer pagingState, ProtocolVersion protocolVersion) { | |
| 67 | - return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement, warnings, incomingPayload); | |
| 68 | + ExecutionInfo withPagingState(ByteBuffer pagingState, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 69 | + return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, codecRegistry, statement, schemaInAgreement, warnings, incomingPayload); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | ExecutionInfo withStatement(Statement statement) { |
| 71 | - return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement, warnings, incomingPayload); | |
| 73 | + return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, codecRegistry, statement, schemaInAgreement, warnings, incomingPayload); | |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | ExecutionInfo withIncomingPayload(Map<String, ByteBuffer> incomingPayload) { |
| 75 | - return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, statement, schemaInAgreement, warnings, incomingPayload); | |
| 77 | + return new ExecutionInfo(triedHosts, achievedConsistency, trace, pagingState, protocolVersion, codecRegistry, statement, schemaInAgreement, warnings, incomingPayload); | |
| 76 | 78 | } |
| 77 | 79 | |
| 78 | 80 | /** |
| @@ -175,7 +177,7 @@ | ||
| 175 | 177 | public PagingState getPagingState() { |
| 176 | 178 | if (this.pagingState == null) |
| 177 | 179 | return null; |
| 178 | - return new PagingState(this.pagingState, this.statement, this.protocolVersion); | |
| 180 | + return new PagingState(this.pagingState, this.statement, this.protocolVersion, this.codecRegistry); | |
| 179 | 181 | } |
| 180 | 182 | |
| 181 | 183 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/PagingState.java +6 −6
| @@ -41,9 +41,9 @@ | ||
| 41 | 41 | private final byte[] hash; |
| 42 | 42 | private final ProtocolVersion protocolVersion; |
| 43 | 43 | |
| 44 | - PagingState(ByteBuffer pagingState, Statement statement, ProtocolVersion protocolVersion) { | |
| 44 | + PagingState(ByteBuffer pagingState, Statement statement, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 45 | 45 | this.pagingState = Bytes.getArray(pagingState); |
| 46 | - this.hash = hash(statement); | |
| 46 | + this.hash = hash(statement, protocolVersion, codecRegistry); | |
| 47 | 47 | this.protocolVersion = protocolVersion; |
| 48 | 48 | } |
| 49 | 49 | |
| @@ -70,7 +70,7 @@ | ||
| 70 | 70 | : ProtocolVersion.V2; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - private byte[] hash(Statement statement) { | |
| 73 | + private byte[] hash(Statement statement, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 74 | 74 | byte[] digest; |
| 75 | 75 | ByteBuffer[] values; |
| 76 | 76 | MessageDigest md; |
| @@ -85,7 +85,7 @@ | ||
| 85 | 85 | //it is a RegularStatement since Batch statements are not allowed |
| 86 | 86 | RegularStatement rs = (RegularStatement)statement; |
| 87 | 87 | md.update(rs.getQueryString().getBytes()); |
| 88 | - values = rs.getValues(); | |
| 88 | + values = rs.getValues(protocolVersion, codecRegistry); | |
| 89 | 89 | } |
| 90 | 90 | if (values != null) { |
| 91 | 91 | for (ByteBuffer value : values) { |
| @@ -101,8 +101,8 @@ | ||
| 101 | 101 | return digest; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - boolean matches(Statement statement) { | |
| 105 | - byte[] toTest = hash(statement); | |
| 104 | + boolean matches(Statement statement, CodecRegistry codecRegistry) { | |
| 105 | + byte[] toTest = hash(statement, protocolVersion, codecRegistry); | |
| 106 | 106 | return Arrays.equals(toTest, this.hash); |
| 107 | 107 | } |
| 108 | 108 |
modified driver-core/src/main/java/com/datastax/driver/core/RegularStatement.java +76 −4
| @@ -19,6 +19,8 @@ | ||
| 19 | 19 | |
| 20 | 20 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 21 | 21 | import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; |
| 22 | +import com.datastax.driver.core.querybuilder.BuiltStatement; | |
| 23 | +import com.datastax.driver.core.schemabuilder.SchemaStatement; | |
| 22 | 24 | |
| 23 | 25 | /** |
| 24 | 26 | * A regular (non-prepared and non batched) CQL statement. |
| @@ -38,9 +40,40 @@ | ||
| 38 | 40 | /** |
| 39 | 41 | * Returns the query string for this statement. |
| 40 | 42 | * |
| 43 | + * @param codecRegistry the codec registry that will be used if the actual | |
| 44 | + * implementation needs to serialize Java objects in the | |
| 45 | + * process of generating the query. Note that it might be | |
| 46 | + * possible to use the no-arg {@link #getQueryString()} | |
| 47 | + * depending on the type of statement this is called on. | |
| 48 | + * | |
| 49 | + * @return a valid CQL query string. | |
| 50 | + * | |
| 51 | + * @see #getQueryString() | |
| 52 | + */ | |
| 53 | + public abstract String getQueryString(CodecRegistry codecRegistry); | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Returns the query string for this statement. | |
| 57 | + * <p> | |
| 58 | + * This method calls {@link #getQueryString(CodecRegistry)} with {@link CodecRegistry#DEFAULT_INSTANCE}. | |
| 59 | + * Whether you should use this or the other variant depends on the type of statement this is | |
| 60 | + * called on: | |
| 61 | + * <ul> | |
| 62 | + * <li>for a {@link SimpleStatement} or {@link SchemaStatement}, the codec registry isn't | |
| 63 | + * actually needed, so it's always safe to use this method;</li> | |
| 64 | + * <li>for a {@link BuiltStatement} you can use this method if you use no custom codecs, or if | |
| 65 | + * your custom codecs are registered with the default registry. Otherwise, use the other method and | |
| 66 | + * provide the registry that contains your codecs (see {@link BuiltStatement} for more explanations | |
| 67 | + * on why this is so);</li> | |
| 68 | + * <li>for a {@link BatchStatement}, use the first rule if it contains no built statements, | |
| 69 | + * or the second rule otherwise.</li> | |
| 70 | + * </ul> | |
| 71 | + * | |
| 41 | 72 | * @return a valid CQL query string. |
| 42 | 73 | */ |
| 43 | - public abstract String getQueryString(); | |
| 74 | + public String getQueryString() { | |
| 75 | + return getQueryString(CodecRegistry.DEFAULT_INSTANCE); | |
| 76 | + } | |
| 44 | 77 | |
| 45 | 78 | /** |
| 46 | 79 | * The values to use for this statement. |
| @@ -52,20 +85,59 @@ | ||
| 52 | 85 | * 1 through {@link Cluster.Builder#withProtocolVersion} or you use |
| 53 | 86 | * Cassandra 1.2). |
| 54 | 87 | * |
| 88 | + * @param protocolVersion the protocol version that will be used to serialize | |
| 89 | + * the values. | |
| 90 | + * @param codecRegistry the codec registry that will be used to serialize the | |
| 91 | + * values. | |
| 92 | + * | |
| 55 | 93 | * @throws InvalidTypeException if one of the values is not of a type |
| 56 | 94 | * that can be serialized to a CQL3 type |
| 57 | - * @see SimpleStatement#SimpleStatement(String, Cluster, Object...) | |
| 95 | + * @see SimpleStatement#SimpleStatement(String, Object...) | |
| 58 | 96 | */ |
| 59 | - public abstract ByteBuffer[] getValues(); | |
| 97 | + public abstract ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry); | |
| 60 | 98 | |
| 61 | 99 | /** |
| 62 | 100 | * Whether or not this statement has values, that is if {@code getValues} |
| 63 | 101 | * will return {@code null} or not. |
| 64 | 102 | * |
| 103 | + * @param codecRegistry the codec registry that will be used if the actual | |
| 104 | + * implementation needs to serialize Java objects in the | |
| 105 | + * process of determining if the query has values. | |
| 106 | + * Note that it might be possible to use the no-arg | |
| 107 | + * {@link #hasValues()} depending on the type of | |
| 108 | + * statement this is called on. | |
| 109 | + * | |
| 110 | + * @return {@code false} if {@link #getValues} returns {@code null}, {@code true} | |
| 111 | + * otherwise. | |
| 112 | + * | |
| 113 | + * @see #hasValues() | |
| 114 | + */ | |
| 115 | + public abstract boolean hasValues(CodecRegistry codecRegistry); | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * Whether or not this statement has values, that is if {@code getValues} | |
| 119 | + * will return {@code null} or not. | |
| 120 | + * <p> | |
| 121 | + * This method calls {@link #hasValues(CodecRegistry)} with {@link ProtocolVersion#NEWEST_SUPPORTED}. | |
| 122 | + * Whether you should use this or the other variant depends on the type of statement this is | |
| 123 | + * called on: | |
| 124 | + * <ul> | |
| 125 | + * <li>for a {@link SimpleStatement} or {@link SchemaStatement}, the codec registry isn't | |
| 126 | + * actually needed, so it's always safe to use this method;</li> | |
| 127 | + * <li>for a {@link BuiltStatement} you can use this method if you use no custom codecs, or if | |
| 128 | + * your custom codecs are registered with the default registry. Otherwise, use the other method and | |
| 129 | + * provide the registry that contains your codecs (see {@link BuiltStatement} for more explanations | |
| 130 | + * on why this is so);</li> | |
| 131 | + * <li>for a {@link BatchStatement}, use the first rule if it contains no built statements, | |
| 132 | + * or the second rule otherwise.</li> | |
| 133 | + * </ul> | |
| 134 | + * | |
| 65 | 135 | * @return {@code false} if {@link #getValues} returns {@code null}, {@code true} |
| 66 | 136 | * otherwise. |
| 67 | 137 | */ |
| 68 | - public abstract boolean hasValues(); | |
| 138 | + public boolean hasValues() { | |
| 139 | + return hasValues(CodecRegistry.DEFAULT_INSTANCE); | |
| 140 | + } | |
| 69 | 141 | |
| 70 | 142 | @Override |
| 71 | 143 | public String toString() { |
modified driver-core/src/main/java/com/datastax/driver/core/Session.java +2 −64
| @@ -119,7 +119,7 @@ | ||
| 119 | 119 | * |
| 120 | 120 | * @param query the CQL query to execute. |
| 121 | 121 | * @param values values required for the execution of {@code query}. See |
| 122 | - * {@link #newSimpleStatement(String, Object...)} for more detail. | |
| 122 | + * {@link SimpleStatement(String, Object...)} for more detail. | |
| 123 | 123 | * @return the result of the query. That result will never be null but can |
| 124 | 124 | * be empty (and will be for any non SELECT query). |
| 125 | 125 | * |
| @@ -181,7 +181,7 @@ | ||
| 181 | 181 | * |
| 182 | 182 | * @param query the CQL query to execute. |
| 183 | 183 | * @param values values required for the execution of {@code query}. See |
| 184 | - * {@link #newSimpleStatement(String, Object...)} for more detail. | |
| 184 | + * {@link SimpleStatement(String, Object...)} for more detail. | |
| 185 | 185 | * @return a future on the result of the query. |
| 186 | 186 | * |
| 187 | 187 | * @throws UnsupportedFeatureException if version 1 of the protocol |
| @@ -190,68 +190,6 @@ | ||
| 190 | 190 | */ |
| 191 | 191 | ResultSetFuture executeAsync(String query, Object... values); |
| 192 | 192 | |
| 193 | - /** | |
| 194 | - * Builds, but does not execute, a simple statement containing the provided query. | |
| 195 | - * | |
| 196 | - * Use this method when you want to customize certain aspects of the statement before | |
| 197 | - * executing it (fetch size, tracing...); you can then pass the statement to | |
| 198 | - * {@link #execute(Statement)}. | |
| 199 | - * | |
| 200 | - * @param query the CQL query to execute. | |
| 201 | - * @return the simple statement. | |
| 202 | - */ | |
| 203 | - SimpleStatement newSimpleStatement(String query); | |
| 204 | - | |
| 205 | - /** | |
| 206 | - * Builds, but does not execute, a simple statement containing the provided query with | |
| 207 | - * the provided parameters. | |
| 208 | - * | |
| 209 | - * Use this method when you want to customize certain aspects of the statement before | |
| 210 | - * executing it (fetch size, tracing...); you can then pass the statement to | |
| 211 | - * {@link #execute(Statement)}. | |
| 212 | - * <p> | |
| 213 | - * Parameterized simple statements are useful when you want to execute a | |
| 214 | - * query only once (and thus do not want to resort to prepared statements), but | |
| 215 | - * do not want to convert all column values to string (typically, if you have blob | |
| 216 | - * values, encoding them to a hexadecimal string is not very efficient). In | |
| 217 | - * that case, you can provide a query string with bind marker to this method, | |
| 218 | - * along with the values for those bind variables. When executed, the server will | |
| 219 | - * prepare the provided query, bind the provided values to that prepare statement and | |
| 220 | - * execute the resulting statement. Thus, | |
| 221 | - * <pre> | |
| 222 | - * session.execute(session.newSimpleStatement(query, value1, value2, value3)); | |
| 223 | - * </pre> | |
| 224 | - * is functionally equivalent to | |
| 225 | - * <pre> | |
| 226 | - * PreparedStatement ps = session.prepare(query); | |
| 227 | - * session.execute(ps.bind(value1, value2, value3)); | |
| 228 | - * </pre> | |
| 229 | - * except that the former version: | |
| 230 | - * <ul> | |
| 231 | - * <li>Requires only one round-trip to a Cassandra node.</li> | |
| 232 | - * <li>Does not leave any prepared statement stored in memory (neither client or | |
| 233 | - * server side) once it has been executed.</li> | |
| 234 | - * </ul> | |
| 235 | - * <p> | |
| 236 | - * Note that the type of the {@code values} provided to this method will | |
| 237 | - * not be validated by the driver as is done by {@link BoundStatement#bind} since | |
| 238 | - * {@code query} is not parsed (and hence the driver cannot know what those value | |
| 239 | - * should be). The codec to serialize each value will be chosen in the codec registry | |
| 240 | - * associated with this session's cluster, based on the value's Java type | |
| 241 | - * (this is the equivalent to calling {@link CodecRegistry#codecFor(Object)}). | |
| 242 | - * If too many or too few values are provided, or if a value is not a valid one for | |
| 243 | - * the variable it is bound to, an | |
| 244 | - * {@link com.datastax.driver.core.exceptions.InvalidQueryException} will be thrown | |
| 245 | - * by Cassandra at execution time. A {@code CodecNotFoundException} may be | |
| 246 | - * thrown by this constructor however, if the codec registry does not know how to | |
| 247 | - * handle one of the values. | |
| 248 | - * | |
| 249 | - * @param query the CQL query to execute. | |
| 250 | - * @param values values required for the execution of {@code query}. | |
| 251 | - * @return the simple statement. | |
| 252 | - */ | |
| 253 | - SimpleStatement newSimpleStatement(String query, Object... values); | |
| 254 | - | |
| 255 | 193 | /** |
| 256 | 194 | * Executes the provided query asynchronously. |
| 257 | 195 | * |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +17 −16
| @@ -478,21 +478,22 @@ | ||
| 478 | 478 | // init() locks, so avoid if we know we don't need it. |
| 479 | 479 | if (!isInit) |
| 480 | 480 | init(); |
| 481 | - ProtocolVersion version = cluster.manager.protocolVersion(); | |
| 481 | + ProtocolVersion protocolVersion = cluster.manager.protocolVersion(); | |
| 482 | + CodecRegistry codecRegistry = cluster.manager.configuration.getCodecRegistry(); | |
| 482 | 483 | |
| 483 | 484 | ConsistencyLevel consistency = statement.getConsistencyLevel(); |
| 484 | 485 | if (consistency == null) |
| 485 | 486 | consistency = configuration().getQueryOptions().getConsistencyLevel(); |
| 486 | 487 | |
| 487 | 488 | ConsistencyLevel serialConsistency = statement.getSerialConsistencyLevel(); |
| 488 | - if (version.compareTo(ProtocolVersion.V3) < 0 && statement instanceof BatchStatement) { | |
| 489 | + if (protocolVersion.compareTo(ProtocolVersion.V3) < 0 && statement instanceof BatchStatement) { | |
| 489 | 490 | if (serialConsistency != null) |
| 490 | - throw new UnsupportedFeatureException(version, "Serial consistency on batch statements is not supported"); | |
| 491 | + throw new UnsupportedFeatureException(protocolVersion, "Serial consistency on batch statements is not supported"); | |
| 491 | 492 | } else if (serialConsistency == null) |
| 492 | 493 | serialConsistency = configuration().getQueryOptions().getSerialConsistencyLevel(); |
| 493 | 494 | |
| 494 | 495 | long defaultTimestamp = Long.MIN_VALUE; |
| 495 | - if (cluster.manager.protocolVersion().compareTo(ProtocolVersion.V3) >= 0) { | |
| 496 | + if (protocolVersion.compareTo(ProtocolVersion.V3) >= 0) { | |
| 496 | 497 | defaultTimestamp = statement.getDefaultTimestamp(); |
| 497 | 498 | if (defaultTimestamp == Long.MIN_VALUE) |
| 498 | 499 | defaultTimestamp = cluster.getConfiguration().getPolicies().getTimestampGenerator().next(); |
| @@ -501,14 +502,14 @@ | ||
| 501 | 502 | int fetchSize = statement.getFetchSize(); |
| 502 | 503 | ByteBuffer usedPagingState = pagingState; |
| 503 | 504 | |
| 504 | - if (version == ProtocolVersion.V1) { | |
| 505 | + if (protocolVersion == ProtocolVersion.V1) { | |
| 505 | 506 | assert pagingState == null; |
| 506 | 507 | // We don't let the user change the fetchSize globally if the proto v1 is used, so we just need to |
| 507 | 508 | // check for the case of a per-statement override |
| 508 | 509 | if (fetchSize <= 0) |
| 509 | 510 | fetchSize = -1; |
| 510 | 511 | else if (fetchSize != Integer.MAX_VALUE) |
| 511 | - throw new UnsupportedFeatureException(version, "Paging is not supported"); | |
| 512 | + throw new UnsupportedFeatureException(protocolVersion, "Paging is not supported"); | |
| 512 | 513 | } else if (fetchSize <= 0) { |
| 513 | 514 | fetchSize = configuration().getQueryOptions().getFetchSize(); |
| 514 | 515 | } |
| @@ -531,13 +532,13 @@ | ||
| 531 | 532 | // It saddens me that we special case for the query builder here, but for now this is simpler. |
| 532 | 533 | // We could provide a general API in RegularStatement instead at some point but it's unclear what's |
| 533 | 534 | // the cleanest way to do that is right now (and it's probably not really that useful anyway). |
| 534 | - if (version == ProtocolVersion.V1 && rs instanceof com.datastax.driver.core.querybuilder.BuiltStatement) | |
| 535 | + if (protocolVersion == ProtocolVersion.V1 && rs instanceof com.datastax.driver.core.querybuilder.BuiltStatement) | |
| 535 | 536 | ((com.datastax.driver.core.querybuilder.BuiltStatement)rs).setForceNoValues(true); |
| 536 | 537 | |
| 537 | - ByteBuffer[] rawValues = rs.getValues(); | |
| 538 | + ByteBuffer[] rawValues = rs.getValues(protocolVersion, codecRegistry); | |
| 538 | 539 | |
| 539 | - if (version == ProtocolVersion.V1 && rawValues != null) | |
| 540 | - throw new UnsupportedFeatureException(version, "Binary values are not supported"); | |
| 540 | + if (protocolVersion == ProtocolVersion.V1 && rawValues != null) | |
| 541 | + throw new UnsupportedFeatureException(protocolVersion, "Binary values are not supported"); | |
| 541 | 542 | |
| 542 | 543 | List<ByteBuffer> values = rawValues == null ? Collections.<ByteBuffer>emptyList() : Arrays.asList(rawValues); |
| 543 | 544 | |
| @@ -552,9 +553,9 @@ | ||
| 552 | 553 | throw new InvalidQueryException(String.format("Tried to execute unknown prepared query : %s. " |
| 553 | 554 | + "You may have used a PreparedStatement that was created with another Cluster instance.", bs.statement.getPreparedId().id)); |
| 554 | 555 | } |
| 555 | - if (version.compareTo(ProtocolVersion.V4) < 0) | |
| 556 | + if (protocolVersion.compareTo(ProtocolVersion.V4) < 0) | |
| 556 | 557 | bs.ensureAllSet(); |
| 557 | - boolean skipMetadata = version != ProtocolVersion.V1 && bs.statement.getPreparedId().resultSetMetadata != null; | |
| 558 | + boolean skipMetadata = protocolVersion != ProtocolVersion.V1 && bs.statement.getPreparedId().resultSetMetadata != null; | |
| 558 | 559 | Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(consistency, Arrays.asList(bs.wrapper.values), skipMetadata, |
| 559 | 560 | fetchSize, usedPagingState, serialConsistency, defaultTimestamp); |
| 560 | 561 | request = new Requests.Execute(bs.statement.getPreparedId().id, options, statement.isTracing()); |
| @@ -562,13 +563,13 @@ | ||
| 562 | 563 | assert statement instanceof BatchStatement : statement; |
| 563 | 564 | assert pagingState == null; |
| 564 | 565 | |
| 565 | - if (version == ProtocolVersion.V1) | |
| 566 | - throw new UnsupportedFeatureException(version, "Protocol level batching is not supported"); | |
| 566 | + if (protocolVersion == ProtocolVersion.V1) | |
| 567 | + throw new UnsupportedFeatureException(protocolVersion, "Protocol level batching is not supported"); | |
| 567 | 568 | |
| 568 | 569 | BatchStatement bs = (BatchStatement)statement; |
| 569 | - if (version.compareTo(ProtocolVersion.V4) < 0) | |
| 570 | + if (protocolVersion.compareTo(ProtocolVersion.V4) < 0) | |
| 570 | 571 | bs.ensureAllSet(); |
| 571 | - BatchStatement.IdAndValues idAndVals = bs.getIdAndValues(); | |
| 572 | + BatchStatement.IdAndValues idAndVals = bs.getIdAndValues(protocolVersion, codecRegistry); | |
| 572 | 573 | Requests.BatchProtocolOptions options = new Requests.BatchProtocolOptions(consistency, serialConsistency, defaultTimestamp); |
| 573 | 574 | request = new Requests.Batch(bs.batchType, idAndVals.ids, idAndVals.values, options, statement.isTracing()); |
| 574 | 575 | } |
modified driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java +59 −22
| @@ -22,49 +22,83 @@ | ||
| 22 | 22 | /** |
| 23 | 23 | * A simple {@code RegularStatement} implementation built directly from a query |
| 24 | 24 | * string. |
| 25 | - * <p> | |
| 26 | - * Use {@link Session#newSimpleStatement(String)} to create a new instance of this | |
| 27 | - * class. | |
| 28 | 25 | */ |
| 29 | 26 | public class SimpleStatement extends RegularStatement { |
| 30 | 27 | |
| 31 | 28 | private final String query; |
| 32 | 29 | private final Object[] values; |
| 33 | - private final Cluster cluster; | |
| 34 | 30 | |
| 35 | 31 | private volatile ByteBuffer routingKey; |
| 36 | 32 | private volatile String keyspace; |
| 37 | - | |
| 38 | - protected SimpleStatement(String query, Cluster cluster) { | |
| 39 | - this(query, cluster, (Object[]) null); | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Creates a new {@code SimpleStatement} with the provided query string (and no values). | |
| 36 | + * | |
| 37 | + * @param query the query string. | |
| 38 | + */ | |
| 39 | + public SimpleStatement(String query) { | |
| 40 | + this(query, (Object[]) null); | |
| 40 | 41 | } |
| 41 | 42 | |
| 42 | - protected SimpleStatement(String query, Cluster cluster, Object... values) { | |
| 43 | + /** | |
| 44 | + * Creates a new {@code SimpleStatement} with the provided query string and values. | |
| 45 | + * <p> | |
| 46 | + * This version of SimpleStatement is useful when you want to execute a | |
| 47 | + * query only once (and thus do not want to resort to prepared statement), but | |
| 48 | + * do not want to convert all column values to string (typically, if you have blob | |
| 49 | + * values, encoding them to a hexadecimal string is not very efficient). In | |
| 50 | + * that case, you can provide a query string with bind markers to this constructor | |
| 51 | + * along with the values for those bind variables. When executed, the server will | |
| 52 | + * prepare the provided, bind the provided values to that prepare statement and | |
| 53 | + * execute the resulting statement. Thus, | |
| 54 | + * <pre> | |
| 55 | + * session.execute(new SimpleStatement(query, value1, value2, value3)); | |
| 56 | + * </pre> | |
| 57 | + * is functionally equivalent to | |
| 58 | + * <pre> | |
| 59 | + * PreparedStatement ps = session.prepare(query); | |
| 60 | + * session.execute(ps.bind(value1, value2, value3)); | |
| 61 | + * </pre> | |
| 62 | + * except that the former version: | |
| 63 | + * <ul> | |
| 64 | + * <li>Requires only one round-trip to a Cassandra node.</li> | |
| 65 | + * <li>Does not left any prepared statement stored in memory (neither client or | |
| 66 | + * server side) once it has been executed.</li> | |
| 67 | + * </ul> | |
| 68 | + * <p> | |
| 69 | + * Note that the type of the {@code values} provided to this method will | |
| 70 | + * not be validated by the driver as is done by {@link BoundStatement#bind} since | |
| 71 | + * {@code query} is not parsed (and hence the driver cannot know what those value | |
| 72 | + * should be). The codec to serialize each value will be chosen in the codec registry | |
| 73 | + * associated with this session's cluster, based on the value's Java type | |
| 74 | + * (this is the equivalent to calling {@link CodecRegistry#codecFor(Object)}). | |
| 75 | + * If too many or too few values are provided, or if a value is not a valid one for | |
| 76 | + * the variable it is bound to, an | |
| 77 | + * {@link com.datastax.driver.core.exceptions.InvalidQueryException} will be thrown | |
| 78 | + * by Cassandra at execution time. A {@code CodecNotFoundException} may be | |
| 79 | + * thrown by this constructor however, if the codec registry does not know how to | |
| 80 | + * handle one of the values. | |
| 81 | + * | |
| 82 | + * @param query the query string. | |
| 83 | + * @param values values required for the execution of {@code query}. | |
| 84 | + * @throws IllegalArgumentException if the number of values is greater than 65535. | |
| 85 | + */ | |
| 86 | + public SimpleStatement(String query, Object... values) { | |
| 43 | 87 | if (values != null && values.length > 65535) |
| 44 | 88 | throw new IllegalArgumentException("Too many values, the maximum allowed is 65535"); |
| 45 | 89 | this.query = query; |
| 46 | 90 | this.values = values; |
| 47 | - this.cluster = cluster; | |
| 48 | 91 | } |
| 49 | 92 | |
| 50 | 93 | @Override |
| 51 | - public String getQueryString() { | |
| 94 | + public String getQueryString(CodecRegistry codecRegistry) { | |
| 52 | 95 | return query; |
| 53 | 96 | } |
| 54 | 97 | |
| 55 | - /** | |
| 56 | - * {@inheritDoc} | |
| 57 | - * <p> | |
| 58 | - * Note: Calling this method may trigger the underlying {@link Cluster} initialization. | |
| 59 | - */ | |
| 60 | 98 | @Override |
| 61 | - public ByteBuffer[] getValues() { | |
| 99 | + public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 62 | 100 | if(values == null) |
| 63 | 101 | return null; |
| 64 | - cluster.init(); | |
| 65 | - Configuration configuration = cluster.getConfiguration(); | |
| 66 | - ProtocolVersion protocolVersion = configuration.getProtocolOptions().getProtocolVersion(); | |
| 67 | - CodecRegistry codecRegistry = configuration.getCodecRegistry(); | |
| 68 | 102 | return convert(values, protocolVersion, codecRegistry); |
| 69 | 103 | } |
| 70 | 104 | |
| @@ -79,7 +113,7 @@ | ||
| 79 | 113 | } |
| 80 | 114 | |
| 81 | 115 | @Override |
| 82 | - public boolean hasValues() { | |
| 116 | + public boolean hasValues(CodecRegistry codecRegistry) { | |
| 83 | 117 | return values != null && values.length > 0; |
| 84 | 118 | } |
| 85 | 119 | |
| @@ -107,13 +141,16 @@ | ||
| 107 | 141 | * {@link #setRoutingKey}, this method will return {@code null} to |
| 108 | 142 | * avoid having to parse the query string to retrieve the partition key. |
| 109 | 143 | * |
| 144 | + * @param protocolVersion unused by this implementation (no internal serialization is required to compute the key). | |
| 145 | + * @param codecRegistry unused by this implementation (no internal serialization is required to compute the key). | |
| 146 | + * | |
| 110 | 147 | * @return the routing key set through {@link #setRoutingKey} if such a key |
| 111 | 148 | * was set, {@code null} otherwise. |
| 112 | 149 | * |
| 113 | 150 | * @see Statement#getRoutingKey |
| 114 | 151 | */ |
| 115 | 152 | @Override |
| 116 | - public ByteBuffer getRoutingKey() { | |
| 153 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 117 | 154 | return routingKey; |
| 118 | 155 | } |
| 119 | 156 |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +35 −4
| @@ -44,7 +44,7 @@ | ||
| 44 | 44 | // used when preparing a statement and for other internal queries. Do not expose publicly. |
| 45 | 45 | static final Statement DEFAULT = new Statement() { |
| 46 | 46 | @Override |
| 47 | - public ByteBuffer getRoutingKey() { | |
| 47 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 48 | 48 | return null; |
| 49 | 49 | } |
| 50 | 50 | |
| @@ -186,9 +186,15 @@ | ||
| 186 | 186 | * to fail and if the load balancing policy used is not token aware, then |
| 187 | 187 | * the routing key can be safely ignored. |
| 188 | 188 | * |
| 189 | + * @param protocolVersion the protocol version that will be used if the actual | |
| 190 | + * implementation needs to serialize something to compute | |
| 191 | + * the key. | |
| 192 | + * @param codecRegistry the codec registry that will be used if the actual | |
| 193 | + * implementation needs to serialize something to compute | |
| 194 | + * this key. | |
| 189 | 195 | * @return the routing key for this query or {@code null}. |
| 190 | 196 | */ |
| 191 | - public abstract ByteBuffer getRoutingKey(); | |
| 197 | + public abstract ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry); | |
| 192 | 198 | |
| 193 | 199 | /** |
| 194 | 200 | * Returns the keyspace this query operates on. |
| @@ -368,17 +374,21 @@ | ||
| 368 | 374 | * |
| 369 | 375 | * @param pagingState the paging state to set, or {@code null} to remove any state that was |
| 370 | 376 | * previously set on this statement. |
| 377 | + * @param codecRegistry the codec registry that will be used if this method needs to serialize the | |
| 378 | + * statement's values in order to check that the paging state matches. | |
| 371 | 379 | * @return this {@code Statement} object. |
| 372 | 380 | * |
| 373 | 381 | * @throws PagingStateException if the paging state does not match this statement. |
| 382 | + * | |
| 383 | + * @see #setPagingState(PagingState) | |
| 374 | 384 | */ |
| 375 | - public Statement setPagingState(PagingState pagingState) { | |
| 385 | + public Statement setPagingState(PagingState pagingState, CodecRegistry codecRegistry) { | |
| 376 | 386 | if (this instanceof BatchStatement) { |
| 377 | 387 | throw new UnsupportedOperationException("Cannot set the paging state on a batch statement"); |
| 378 | 388 | } else { |
| 379 | 389 | if (pagingState == null) { |
| 380 | 390 | this.pagingState = null; |
| 381 | - } else if (pagingState.matches(this)) { | |
| 391 | + } else if (pagingState.matches(this, codecRegistry)) { | |
| 382 | 392 | this.pagingState = pagingState.getRawState(); |
| 383 | 393 | } else { |
| 384 | 394 | throw new PagingStateException("Paging state mismatch, " |
| @@ -389,6 +399,27 @@ | ||
| 389 | 399 | return this; |
| 390 | 400 | } |
| 391 | 401 | |
| 402 | + /** | |
| 403 | + * Sets the paging state. | |
| 404 | + * <p> | |
| 405 | + * This method calls {@link #setPagingState(PagingState, CodecRegistry)} with {@link CodecRegistry#DEFAULT_INSTANCE}. | |
| 406 | + * Whether you should use this or the other variant depends on the type of statement this is | |
| 407 | + * called on: | |
| 408 | + * <ul> | |
| 409 | + * <li>for a {@link BoundStatement}, the codec registry isn't actually needed, so it's always safe to | |
| 410 | + * use this method;</li> | |
| 411 | + * <li>for a {@link SimpleStatement} or {@link BuiltStatement}, you can use this method if you use no | |
| 412 | + * custom codecs, or if your custom codecs are registered with the default registry. Otherwise, use | |
| 413 | + * the other method and provide the registry that contains your codecs.</li> | |
| 414 | + * </ul> | |
| 415 | + * | |
| 416 | + * @param pagingState the paging state to set, or {@code null} to remove any state that was | |
| 417 | + * previously set on this statement. | |
| 418 | + */ | |
| 419 | + public Statement setPagingState(PagingState pagingState) { | |
| 420 | + return setPagingState(pagingState, CodecRegistry.DEFAULT_INSTANCE); | |
| 421 | + } | |
| 422 | + | |
| 392 | 423 | /** |
| 393 | 424 | * Sets the paging state. |
| 394 | 425 | * <p> |
modified driver-core/src/main/java/com/datastax/driver/core/StatementWrapper.java +4 −4
| @@ -111,8 +111,8 @@ | ||
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | @Override |
| 114 | - public ByteBuffer getRoutingKey() { | |
| 115 | - return wrapped.getRoutingKey(); | |
| 114 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 115 | + return wrapped.getRoutingKey(protocolVersion, codecRegistry); | |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | @Override |
| @@ -126,8 +126,8 @@ | ||
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | @Override |
| 129 | - public Statement setPagingState(PagingState pagingState) { | |
| 130 | - return wrapped.setPagingState(pagingState); | |
| 129 | + public Statement setPagingState(PagingState pagingState, CodecRegistry codecRegistry) { | |
| 130 | + return wrapped.setPagingState(pagingState, codecRegistry); | |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | @Override |
modified driver-core/src/main/java/com/datastax/driver/core/policies/TokenAwarePolicy.java +6 −2
| @@ -50,7 +50,9 @@ | ||
| 50 | 50 | |
| 51 | 51 | private final LoadBalancingPolicy childPolicy; |
| 52 | 52 | private final boolean shuffleReplicas; |
| 53 | - private Metadata clusterMetadata; | |
| 53 | + private volatile Metadata clusterMetadata; | |
| 54 | + private volatile ProtocolVersion protocolVersion; | |
| 55 | + private volatile CodecRegistry codecRegistry; | |
| 54 | 56 | |
| 55 | 57 | /** |
| 56 | 58 | * Creates a new {@code TokenAware} policy. |
| @@ -90,6 +92,8 @@ | ||
| 90 | 92 | @Override |
| 91 | 93 | public void init(Cluster cluster, Collection<Host> hosts) { |
| 92 | 94 | clusterMetadata = cluster.getMetadata(); |
| 95 | + protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 96 | + codecRegistry = cluster.getConfiguration().getCodecRegistry(); | |
| 93 | 97 | childPolicy.init(cluster, hosts); |
| 94 | 98 | } |
| 95 | 99 | |
| @@ -118,7 +122,7 @@ | ||
| 118 | 122 | @Override |
| 119 | 123 | public Iterator<Host> newQueryPlan(final String loggedKeyspace, final Statement statement) { |
| 120 | 124 | |
| 121 | - ByteBuffer partitionKey = statement.getRoutingKey(); | |
| 125 | + ByteBuffer partitionKey = statement.getRoutingKey(protocolVersion, codecRegistry); | |
| 122 | 126 | String keyspace = statement.getKeyspace(); |
| 123 | 127 | if (keyspace == null) |
| 124 | 128 | keyspace = loggedKeyspace; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +10 −10
| @@ -33,8 +33,8 @@ | ||
| 33 | 33 | // Only used when we add at last one statement that is not a BuiltStatement subclass |
| 34 | 34 | private int nonBuiltStatementValues; |
| 35 | 35 | |
| 36 | - Batch(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, RegularStatement[] statements, boolean logged) { | |
| 37 | - super((String)null, protocolVersion, codecRegistry); | |
| 36 | + Batch(RegularStatement[] statements, boolean logged) { | |
| 37 | + super((String)null); | |
| 38 | 38 | this.statements = statements.length == 0 |
| 39 | 39 | ? new ArrayList<RegularStatement>() |
| 40 | 40 | : new ArrayList<RegularStatement>(statements.length); |
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | @Override |
| 49 | - StringBuilder buildQueryString(List<Object> variables) { | |
| 49 | + StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { | |
| 50 | 50 | StringBuilder builder = new StringBuilder(); |
| 51 | 51 | |
| 52 | 52 | builder.append(isCounterOp() |
| @@ -55,7 +55,7 @@ | ||
| 55 | 55 | |
| 56 | 56 | if (!usings.usings.isEmpty()) { |
| 57 | 57 | builder.append(" USING "); |
| 58 | - Utils.joinAndAppend(builder, getCodecRegistry(), " AND ", usings.usings, variables); | |
| 58 | + Utils.joinAndAppend(builder, codecRegistry, " AND ", usings.usings, variables); | |
| 59 | 59 | } |
| 60 | 60 | builder.append(' '); |
| 61 | 61 | |
| @@ -63,7 +63,7 @@ | ||
| 63 | 63 | RegularStatement stmt = statements.get(i); |
| 64 | 64 | if (stmt instanceof BuiltStatement) { |
| 65 | 65 | BuiltStatement bst = (BuiltStatement)stmt; |
| 66 | - builder.append(maybeAddSemicolon(bst.buildQueryString(variables))); | |
| 66 | + builder.append(maybeAddSemicolon(bst.buildQueryString(variables, codecRegistry))); | |
| 67 | 67 | |
| 68 | 68 | } else { |
| 69 | 69 | String str = stmt.getQueryString(); |
| @@ -117,14 +117,14 @@ | ||
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | @Override |
| 120 | - public ByteBuffer[] getValues() { | |
| 120 | + public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 121 | 121 | // If there is some non-BuiltStatement inside the batch with values, we shouldn't |
| 122 | 122 | // use super.getValues() since it will ignore the values of said non-BuiltStatement. |
| 123 | 123 | // If that's the case, we just collects all those values (and we know |
| 124 | 124 | // super.getValues() == null in that case since we've explicitely set this.hasBindMarker |
| 125 | 125 | // to true). Otherwise, we simply call super.getValues(). |
| 126 | 126 | if (nonBuiltStatementValues == 0) |
| 127 | - return super.getValues(); | |
| 127 | + return super.getValues(protocolVersion, codecRegistry); | |
| 128 | 128 | |
| 129 | 129 | ByteBuffer[] values = new ByteBuffer[nonBuiltStatementValues]; |
| 130 | 130 | int i = 0; |
| @@ -133,7 +133,7 @@ | ||
| 133 | 133 | if (statement instanceof BuiltStatement) |
| 134 | 134 | continue; |
| 135 | 135 | |
| 136 | - ByteBuffer[] statementValues = statement.getValues(); | |
| 136 | + ByteBuffer[] statementValues = statement.getValues(protocolVersion, codecRegistry); | |
| 137 | 137 | System.arraycopy(statementValues, 0, values, i, statementValues.length); |
| 138 | 138 | i += statementValues.length; |
| 139 | 139 | } |
| @@ -157,9 +157,9 @@ | ||
| 157 | 157 | * @return the routing key for this batch statement. |
| 158 | 158 | */ |
| 159 | 159 | @Override |
| 160 | - public ByteBuffer getRoutingKey() { | |
| 160 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 161 | 161 | for (RegularStatement statement : statements) { |
| 162 | - ByteBuffer routingKey = statement.getRoutingKey(); | |
| 162 | + ByteBuffer routingKey = statement.getRoutingKey(protocolVersion, codecRegistry); | |
| 163 | 163 | if (routingKey != null) { |
| 164 | 164 | return routingKey; |
| 165 | 165 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +112 −60
| @@ -22,10 +22,57 @@ | ||
| 22 | 22 | import java.util.regex.Pattern; |
| 23 | 23 | |
| 24 | 24 | import com.datastax.driver.core.*; |
| 25 | +import com.datastax.driver.core.exceptions.CodecNotFoundException; | |
| 25 | 26 | import com.datastax.driver.core.policies.RetryPolicy; |
| 26 | 27 | |
| 27 | 28 | /** |
| 28 | - * Common ancestor to the query builder built statements. | |
| 29 | + * Common ancestor to statements generated with the {@link QueryBuilder}. | |
| 30 | + * <p> | |
| 31 | + * The actual query string will be generated and cached the first time it is requested, | |
| 32 | + * which is either when the driver tries to execute the query, or when you call certain | |
| 33 | + * public methods (for example {@link RegularStatement#getQueryString(CodecRegistry)}, | |
| 34 | + * {@link #getObject(int, CodecRegistry)}). | |
| 35 | + * <p> | |
| 36 | + * Whenever possible (and unless you call {@link #setForceNoValues(boolean)}, the builder | |
| 37 | + * will try to handle values passed to its methods as standalone values bound to the query | |
| 38 | + * string with placeholders. For instance: | |
| 39 | + * <pre> | |
| 40 | + * select().all().from("foo").where(eq("k", "the key")); | |
| 41 | + * // Is equivalent to: | |
| 42 | + * new SimpleStatement("SELECT * FROM foo WHERE k=?", "the key"); | |
| 43 | + * </pre> | |
| 44 | + * There are a few exceptions to this rule: | |
| 45 | + * <ul> | |
| 46 | + * <li>for fixed-size number types, the builder can't guess what the actual CQL type | |
| 47 | + * is. Standalone values are sent to Cassandra in their serialized form, and number | |
| 48 | + * types aren't all serialized in the same way, so picking the wrong type could | |
| 49 | + * lead to a query error;</li> | |
| 50 | + * <li>if the value is a "special" element like a function call, it can't be serialized. | |
| 51 | + * This also applies to collections mixing special elements and regular objects.</li> | |
| 52 | + * </ul> | |
| 53 | + * In these cases, the builder will inline the value in the query string: | |
| 54 | + * <pre> | |
| 55 | + * select().all().from("foo").where(eq("k", 1)); | |
| 56 | + * // Is equivalent to: | |
| 57 | + * new SimpleStatement("SELECT * FROM foo WHERE k=1"); | |
| 58 | + * </pre> | |
| 59 | + * One final thing to consider is {@link CodecRegistry custom codecs}. If you've registered | |
| 60 | + * codecs to handle your own Java types against the cluster, then you can pass instances of | |
| 61 | + * those types to query builder methods. But should the builder have to inline those values, | |
| 62 | + * it needs your codecs to {@link TypeCodec#format(Object) convert them to string form}. | |
| 63 | + * That is why some of the public methods of this class take a {@code CodecRegistry} as a | |
| 64 | + * parameter: | |
| 65 | + * <pre> | |
| 66 | + * BuiltStatement s = select().all().from("foo").where(eq("k", myCustomObject)); | |
| 67 | + * // if we do this codecs will definitely be needed: | |
| 68 | + * s.forceNoValues(true); | |
| 69 | + * s.getQueryString(myCodecRegistry); | |
| 70 | + * </pre> | |
| 71 | + * For convenience, there are no-arg versions of those methods that use | |
| 72 | + * {@link CodecRegistry#DEFAULT_INSTANCE}. But you should only use them if you are sure that | |
| 73 | + * no custom values will need to be inlined while building the statement, or if you have | |
| 74 | + * registered your custom codecs with the default registry instance. Otherwise, you will get | |
| 75 | + * a {@link CodecNotFoundException}. | |
| 29 | 76 | */ |
| 30 | 77 | public abstract class BuiltStatement extends RegularStatement { |
| 31 | 78 | |
| @@ -34,8 +81,6 @@ | ||
| 34 | 81 | private final List<ColumnMetadata> partitionKey; |
| 35 | 82 | private final List<Object> routingKeyValues; |
| 36 | 83 | final String keyspace; |
| 37 | - private final ProtocolVersion protocolVersion; | |
| 38 | - private final CodecRegistry codecRegistry; | |
| 39 | 84 | |
| 40 | 85 | private boolean dirty; |
| 41 | 86 | private String cache; |
| @@ -48,20 +93,16 @@ | ||
| 48 | 93 | boolean hasBindMarkers; |
| 49 | 94 | private boolean forceNoValues; |
| 50 | 95 | |
| 51 | - BuiltStatement(String keyspace, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 96 | + BuiltStatement(String keyspace) { | |
| 52 | 97 | this.partitionKey = null; |
| 53 | 98 | this.routingKeyValues = null; |
| 54 | 99 | this.keyspace = keyspace; |
| 55 | - this.protocolVersion = protocolVersion; | |
| 56 | - this.codecRegistry = codecRegistry; | |
| 57 | 100 | } |
| 58 | 101 | |
| 59 | - BuiltStatement(TableMetadata tableMetadata, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 102 | + BuiltStatement(TableMetadata tableMetadata) { | |
| 60 | 103 | this.partitionKey = tableMetadata.getPartitionKey(); |
| 61 | 104 | this.routingKeyValues = Arrays.asList(new Object[tableMetadata.getPartitionKey().size()]); |
| 62 | 105 | this.keyspace = escapeId(tableMetadata.getKeyspace().getName()); |
| 63 | - this.protocolVersion = protocolVersion; | |
| 64 | - this.codecRegistry = codecRegistry; | |
| 65 | 106 | } |
| 66 | 107 | |
| 67 | 108 | // Same as Metadata.escapeId, but we don't have access to it here. |
| @@ -71,8 +112,8 @@ | ||
| 71 | 112 | } |
| 72 | 113 | |
| 73 | 114 | @Override |
| 74 | - public String getQueryString() { | |
| 75 | - maybeRebuildCache(); | |
| 115 | + public String getQueryString(CodecRegistry codecRegistry) { | |
| 116 | + maybeRebuildCache(codecRegistry); | |
| 76 | 117 | return cache; |
| 77 | 118 | } |
| 78 | 119 | |
| @@ -80,21 +121,45 @@ | ||
| 80 | 121 | * Returns the {@code i}th value as the Java type matching its CQL type. |
| 81 | 122 | * |
| 82 | 123 | * @param i the index to retrieve. |
| 124 | + * @param codecRegistry the codec registry that will be used if the statement must be | |
| 125 | + * rebuilt in order to determine if it has values, and Java objects | |
| 126 | + * must be inlined in the process (see {@link BuiltStatement} for | |
| 127 | + * more explanations on why this is so). | |
| 83 | 128 | * @return the value of the {@code i}th value of this statement. |
| 84 | 129 | * |
| 85 | 130 | * @throws IllegalStateException if this statement does not have values. |
| 86 | 131 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 132 | + * | |
| 133 | + * @see #getObject(int) | |
| 87 | 134 | */ |
| 88 | - public Object getObject(int i) { | |
| 89 | - maybeRebuildCache(); | |
| 135 | + public Object getObject(int i, CodecRegistry codecRegistry) { | |
| 136 | + maybeRebuildCache(codecRegistry); | |
| 90 | 137 | if (values == null || values.isEmpty()) |
| 91 | 138 | throw new IllegalStateException("This statement does not have values"); |
| 92 | 139 | if (i < 0 || i >= values.size()) |
| 93 | 140 | throw new ArrayIndexOutOfBoundsException(i); |
| 94 | 141 | return values.get(i); |
| 95 | 142 | } |
| 143 | + /** | |
| 144 | + * Returns the {@code i}th value as the Java type matching its CQL type. | |
| 145 | + * <p> | |
| 146 | + * This method calls {@link #getObject(int, CodecRegistry)} with | |
| 147 | + * {@link CodecRegistry#DEFAULT_INSTANCE}. | |
| 148 | + * It's safe to use if you don't use any custom codecs, or if your custom codecs are in | |
| 149 | + * the default registry; otherwise, use the other method and provide the registry that | |
| 150 | + * contains your codecs. | |
| 151 | + * | |
| 152 | + * @param i the index to retrieve. | |
| 153 | + * @return the value of the {@code i}th value of this statement. | |
| 154 | + * | |
| 155 | + * @throws IllegalStateException if this statement does not have values. | |
| 156 | + * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 157 | + */ | |
| 158 | + public Object getObject(int i) { | |
| 159 | + return getObject(i, CodecRegistry.DEFAULT_INSTANCE); | |
| 160 | + } | |
| 96 | 161 | |
| 97 | - private void maybeRebuildCache() { | |
| 162 | + private void maybeRebuildCache(CodecRegistry codecRegistry) { | |
| 98 | 163 | if (!dirty && cache != null) |
| 99 | 164 | return; |
| 100 | 165 | |
| @@ -102,10 +167,10 @@ | ||
| 102 | 167 | values = null; |
| 103 | 168 | |
| 104 | 169 | if (hasBindMarkers || forceNoValues) { |
| 105 | - sb = buildQueryString(null); | |
| 170 | + sb = buildQueryString(null, codecRegistry); | |
| 106 | 171 | } else { |
| 107 | 172 | values = new ArrayList<Object>(); |
| 108 | - sb = buildQueryString(values); | |
| 173 | + sb = buildQueryString(values, codecRegistry); | |
| 109 | 174 | |
| 110 | 175 | if (values.size() > 65535) |
| 111 | 176 | throw new IllegalArgumentException("Too many values for built statement, the maximum allowed is 65535"); |
| @@ -134,7 +199,7 @@ | ||
| 134 | 199 | return sb; |
| 135 | 200 | } |
| 136 | 201 | |
| 137 | - abstract StringBuilder buildQueryString(List<Object> variables); | |
| 202 | + abstract StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry); | |
| 138 | 203 | |
| 139 | 204 | boolean isCounterOp() { |
| 140 | 205 | return isCounterOp == null ? false : isCounterOp; |
| @@ -177,26 +242,17 @@ | ||
| 177 | 242 | } |
| 178 | 243 | } |
| 179 | 244 | |
| 180 | - CodecRegistry getCodecRegistry(){ | |
| 181 | - return codecRegistry; | |
| 182 | - } | |
| 183 | - | |
| 184 | - ProtocolVersion getProtocolVersion() { | |
| 185 | - return protocolVersion; | |
| 186 | - } | |
| 187 | - | |
| 188 | 245 | @Override |
| 189 | - public ByteBuffer getRoutingKey() { | |
| 246 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 190 | 247 | if (routingKeyValues == null) |
| 191 | 248 | return null; |
| 192 | 249 | ByteBuffer[] routingKeyParts = new ByteBuffer[partitionKey.size()]; |
| 193 | - CodecRegistry codecRegistry = getCodecRegistry(); | |
| 194 | 250 | for (int i = 0; i < partitionKey.size(); i++) { |
| 195 | 251 | Object value = routingKeyValues.get(i); |
| 196 | 252 | if(value == null) |
| 197 | 253 | return null; |
| 198 | 254 | TypeCodec<Object> codec = codecRegistry.codecFor(partitionKey.get(i).getType(), value); |
| 199 | - routingKeyParts[i] = codec.serialize(value, getProtocolVersion()); | |
| 255 | + routingKeyParts[i] = codec.serialize(value, protocolVersion); | |
| 200 | 256 | } |
| 201 | 257 | return routingKeyParts.length == 1 |
| 202 | 258 | ? routingKeyParts[0] |
| @@ -208,20 +264,15 @@ | ||
| 208 | 264 | return keyspace; |
| 209 | 265 | } |
| 210 | 266 | |
| 211 | - /** | |
| 212 | - * {@inheritDoc} | |
| 213 | - * <p> | |
| 214 | - * Note: Calling this method may trigger the underlying {@link Cluster} initialization. | |
| 215 | - */ | |
| 216 | 267 | @Override |
| 217 | - public ByteBuffer[] getValues() { | |
| 218 | - maybeRebuildCache(); | |
| 219 | - return values == null ? null : Utils.convert(values.toArray(), getProtocolVersion(), getCodecRegistry()); | |
| 268 | + public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 269 | + maybeRebuildCache(codecRegistry); | |
| 270 | + return values == null ? null : Utils.convert(values.toArray(), protocolVersion, codecRegistry); | |
| 220 | 271 | } |
| 221 | 272 | |
| 222 | 273 | @Override |
| 223 | - public boolean hasValues() { | |
| 224 | - maybeRebuildCache(); | |
| 274 | + public boolean hasValues(CodecRegistry codecRegistry) { | |
| 275 | + maybeRebuildCache(codecRegistry); | |
| 225 | 276 | return values != null; |
| 226 | 277 | } |
| 227 | 278 | |
| @@ -235,17 +286,18 @@ | ||
| 235 | 286 | return !hasNonIdempotentOps(); |
| 236 | 287 | } |
| 237 | 288 | |
| 238 | - /** | |
| 239 | - * {@inheritDoc} | |
| 240 | - * <p> | |
| 241 | - * Note: Calling this method may trigger the underlying {@link Cluster} initialization. | |
| 242 | - */ | |
| 243 | 289 | @Override |
| 244 | 290 | public String toString() { |
| 245 | - if (forceNoValues) { | |
| 246 | - return getQueryString(); | |
| 291 | + try { | |
| 292 | + if (forceNoValues) { | |
| 293 | + return getQueryString(); | |
| 294 | + } | |
| 295 | + StringBuilder queryString = buildQueryString(null, CodecRegistry.DEFAULT_INSTANCE); | |
| 296 | + return maybeAddSemicolon(queryString).toString(); | |
| 297 | + } catch (CodecNotFoundException e) { | |
| 298 | + // Ugly but we have absolutely no context to get the registry from | |
| 299 | + return String.format("built query (could not generate with default codec registry: %s)", e.getMessage()); | |
| 247 | 300 | } |
| 248 | - return maybeAddSemicolon(buildQueryString(null)).toString(); | |
| 249 | 301 | } |
| 250 | 302 | |
| 251 | 303 | /** |
| @@ -253,16 +305,16 @@ | ||
| 253 | 305 | * <p> |
| 254 | 306 | * By default (and unless the protocol version 1 is in use, see below) and |
| 255 | 307 | * for performance reasons, the query builder will not serialize all values |
| 256 | - * provided to strings. This means that the {@link #getQueryString} may | |
| 257 | - * return a query string with bind markers (where and when is at the | |
| 308 | + * provided to strings. This means that {@link #getQueryString(CodecRegistry)} | |
| 309 | + * may return a query string with bind markers (where and when is at the | |
| 258 | 310 | * discretion of the builder) and {@link #getValues} will return the binary |
| 259 | 311 | * values for those markers. This method allows to force the builder to not |
| 260 | - * generate binary values but rather to serialize them all in the query | |
| 312 | + * generate binary values but rather to inline them all in the query | |
| 261 | 313 | * string. In practice, this means that if you call {@code |
| 262 | 314 | * setForceNoValues(true)}, you are guaranteed that {@code getValues()} will |
| 263 | 315 | * return {@code null} and that the string returned by {@code |
| 264 | - * getQueryString()} will contain no other bind markers than the one | |
| 265 | - * inputted by the user. | |
| 316 | + * getQueryString()} will contain no other bind markers than the ones | |
| 317 | + * specified by the user. | |
| 266 | 318 | * <p> |
| 267 | 319 | * If the native protocol version 1 is in use, the driver will default |
| 268 | 320 | * to not generating values since those are not supported by that version of |
| @@ -290,23 +342,23 @@ | ||
| 290 | 342 | T statement; |
| 291 | 343 | |
| 292 | 344 | ForwardingStatement(T statement) { |
| 293 | - super((String)null, statement.getProtocolVersion(), statement.getCodecRegistry()); | |
| 345 | + super((String)null); | |
| 294 | 346 | this.statement = statement; |
| 295 | 347 | } |
| 296 | 348 | |
| 297 | 349 | @Override |
| 298 | - public String getQueryString() { | |
| 299 | - return statement.getQueryString(); | |
| 350 | + public String getQueryString(CodecRegistry codecRegistry) { | |
| 351 | + return statement.getQueryString(codecRegistry); | |
| 300 | 352 | } |
| 301 | 353 | |
| 302 | 354 | @Override |
| 303 | - StringBuilder buildQueryString(List<Object> values) { | |
| 304 | - return statement.buildQueryString(values); | |
| 355 | + StringBuilder buildQueryString(List<Object> values, CodecRegistry codecRegistry) { | |
| 356 | + return statement.buildQueryString(values, codecRegistry); | |
| 305 | 357 | } |
| 306 | 358 | |
| 307 | 359 | @Override |
| 308 | - public ByteBuffer getRoutingKey() { | |
| 309 | - return statement.getRoutingKey(); | |
| 360 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 361 | + return statement.getRoutingKey(protocolVersion, codecRegistry); | |
| 310 | 362 | } |
| 311 | 363 | |
| 312 | 364 | @Override |
| @@ -370,8 +422,8 @@ | ||
| 370 | 422 | } |
| 371 | 423 | |
| 372 | 424 | @Override |
| 373 | - public ByteBuffer[] getValues() { | |
| 374 | - return statement.getValues(); | |
| 425 | + public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 426 | + return statement.getValues(protocolVersion, codecRegistry); | |
| 375 | 427 | } |
| 376 | 428 | |
| 377 | 429 | @Override |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +9 −23
| @@ -18,9 +18,7 @@ | ||
| 18 | 18 | import java.util.ArrayList; |
| 19 | 19 | import java.util.List; |
| 20 | 20 | |
| 21 | -import com.datastax.driver.core.Cluster; | |
| 22 | 21 | import com.datastax.driver.core.CodecRegistry; |
| 23 | -import com.datastax.driver.core.ProtocolVersion; | |
| 24 | 22 | import com.datastax.driver.core.TableMetadata; |
| 25 | 23 | |
| 26 | 24 | /** |
| @@ -35,8 +33,8 @@ | ||
| 35 | 33 | private final Conditions conditions; |
| 36 | 34 | private boolean ifExists; |
| 37 | 35 | |
| 38 | - Delete(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table, List<Selector> columns) { | |
| 39 | - super(keyspace, protocolVersion, codecRegistry); | |
| 36 | + Delete(String keyspace, String table, List<Selector> columns) { | |
| 37 | + super(keyspace); | |
| 40 | 38 | this.table = table; |
| 41 | 39 | this.columns = columns; |
| 42 | 40 | this.where = new Where(this); |
| @@ -44,8 +42,8 @@ | ||
| 44 | 42 | this.conditions = new Conditions(this); |
| 45 | 43 | } |
| 46 | 44 | |
| 47 | - Delete(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table, List<Selector> columns) { | |
| 48 | - super(table, protocolVersion, codecRegistry); | |
| 45 | + Delete(TableMetadata table, List<Selector> columns) { | |
| 46 | + super(table); | |
| 49 | 47 | this.table = escapeId(table.getName()); |
| 50 | 48 | this.columns = columns; |
| 51 | 49 | this.where = new Where(this); |
| @@ -54,10 +52,9 @@ | ||
| 54 | 52 | } |
| 55 | 53 | |
| 56 | 54 | @Override |
| 57 | - StringBuilder buildQueryString(List<Object> variables) { | |
| 55 | + StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { | |
| 58 | 56 | StringBuilder builder = new StringBuilder(); |
| 59 | 57 | |
| 60 | - CodecRegistry codecRegistry = getCodecRegistry(); | |
| 61 | 58 | builder.append("DELETE"); |
| 62 | 59 | if (!columns.isEmpty()) |
| 63 | 60 | Utils.joinAndAppend(builder.append(" "), codecRegistry, ",", columns, variables); |
| @@ -278,19 +275,12 @@ | ||
| 278 | 275 | */ |
| 279 | 276 | public static class Builder { |
| 280 | 277 | |
| 281 | - private final ProtocolVersion protocolVersion; | |
| 282 | - private final CodecRegistry codecRegistry; | |
| 283 | - | |
| 284 | 278 | List<Selector> columns = new ArrayList<Selector>(); |
| 285 | 279 | |
| 286 | - Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 287 | - this.protocolVersion = protocolVersion; | |
| 288 | - this.codecRegistry = codecRegistry; | |
| 280 | + Builder() { | |
| 289 | 281 | } |
| 290 | 282 | |
| 291 | - Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String... columnNames) { | |
| 292 | - this.protocolVersion = protocolVersion; | |
| 293 | - this.codecRegistry = codecRegistry; | |
| 283 | + Builder(String... columnNames) { | |
| 294 | 284 | for (String columnName : columnNames) { |
| 295 | 285 | this.columns.add(new Selector(columnName)); |
| 296 | 286 | } |
| @@ -314,7 +304,7 @@ | ||
| 314 | 304 | * @return a newly built DELETE statement that deletes from {@code keyspace.table}. |
| 315 | 305 | */ |
| 316 | 306 | public Delete from(String keyspace, String table) { |
| 317 | - return new Delete(protocolVersion, codecRegistry, keyspace, table, columns); | |
| 307 | + return new Delete(keyspace, table, columns); | |
| 318 | 308 | } |
| 319 | 309 | |
| 320 | 310 | /** |
| @@ -324,7 +314,7 @@ | ||
| 324 | 314 | * @return a newly built DELETE statement that deletes from {@code table}. |
| 325 | 315 | */ |
| 326 | 316 | public Delete from(TableMetadata table) { |
| 327 | - return new Delete(protocolVersion, codecRegistry, table, columns); | |
| 317 | + return new Delete(table, columns); | |
| 328 | 318 | } |
| 329 | 319 | } |
| 330 | 320 | |
| @@ -333,10 +323,6 @@ | ||
| 333 | 323 | */ |
| 334 | 324 | public static class Selection extends Builder { |
| 335 | 325 | |
| 336 | - Selection(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 337 | - super(protocolVersion, codecRegistry); | |
| 338 | - } | |
| 339 | - | |
| 340 | 326 | /** |
| 341 | 327 | * Deletes all columns (i.e. "DELETE FROM ...") |
| 342 | 328 | * |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +5 −8
| @@ -19,9 +19,7 @@ | ||
| 19 | 19 | import java.util.Arrays; |
| 20 | 20 | import java.util.List; |
| 21 | 21 | |
| 22 | -import com.datastax.driver.core.Cluster; | |
| 23 | 22 | import com.datastax.driver.core.CodecRegistry; |
| 24 | -import com.datastax.driver.core.ProtocolVersion; | |
| 25 | 23 | import com.datastax.driver.core.TableMetadata; |
| 26 | 24 | |
| 27 | 25 | /** |
| @@ -35,23 +33,22 @@ | ||
| 35 | 33 | private final Options usings; |
| 36 | 34 | private boolean ifNotExists; |
| 37 | 35 | |
| 38 | - Insert(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table) { | |
| 39 | - super(keyspace, protocolVersion, codecRegistry); | |
| 36 | + Insert(String keyspace, String table) { | |
| 37 | + super(keyspace); | |
| 40 | 38 | this.table = table; |
| 41 | 39 | this.usings = new Options(this); |
| 42 | 40 | } |
| 43 | 41 | |
| 44 | - Insert(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table) { | |
| 45 | - super(table, protocolVersion, codecRegistry); | |
| 42 | + Insert(TableMetadata table) { | |
| 43 | + super(table); | |
| 46 | 44 | this.table = escapeId(table.getName()); |
| 47 | 45 | this.usings = new Options(this); |
| 48 | 46 | } |
| 49 | 47 | |
| 50 | 48 | @Override |
| 51 | - StringBuilder buildQueryString(List<Object> variables) { | |
| 49 | + StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { | |
| 52 | 50 | StringBuilder builder = new StringBuilder(); |
| 53 | 51 | |
| 54 | - CodecRegistry codecRegistry = getCodecRegistry(); | |
| 55 | 52 | builder.append("INSERT INTO "); |
| 56 | 53 | if (keyspace != null) |
| 57 | 54 | Utils.appendName(keyspace, builder).append('.'); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +31 −65
| @@ -38,41 +38,7 @@ | ||
| 38 | 38 | */ |
| 39 | 39 | public final class QueryBuilder { |
| 40 | 40 | |
| 41 | - private final ProtocolVersion protocolVersion; | |
| 42 | - private final CodecRegistry codecRegistry; | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * Create a new QueryBuilder instance and associate it with the given {@link Cluster}. | |
| 46 | - * Note: if the {@link Cluster} is not yet initialized, this method | |
| 47 | - * will trigger its initialization. | |
| 48 | - * | |
| 49 | - * @param cluster The {@link Cluster} instance this object should be attached to. | |
| 50 | - */ | |
| 51 | - public QueryBuilder(Cluster cluster) { | |
| 52 | - this(getProtocolVersion(cluster), cluster.getConfiguration().getCodecRegistry()); | |
| 53 | - } | |
| 54 | - | |
| 55 | - private static ProtocolVersion getProtocolVersion(Cluster cluster) { | |
| 56 | - cluster.init(); | |
| 57 | - return cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 58 | - } | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * Create a "disconnected" QueryBuilder instance (<b>you should prefer | |
| 62 | - * {@link #QueryBuilder(Cluster)} whenever possible</b>). | |
| 63 | - * <p> | |
| 64 | - * This constructor is only exposed for situations where you don't have a {@code Cluster} | |
| 65 | - * instance available. If you use this constructor, and use statements built from it | |
| 66 | - * with a {@code Cluster} later, you won't be able to rely on custom codecs registered | |
| 67 | - * against the cluster, or you might get errors if the protocol versions don't match. | |
| 68 | - * | |
| 69 | - * @param protocolVersion the protocol version. | |
| 70 | - * @param codecRegistry the codec registry. | |
| 71 | - */ | |
| 72 | - public QueryBuilder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 73 | - this.protocolVersion = protocolVersion; | |
| 74 | - this.codecRegistry = codecRegistry; | |
| 75 | - } | |
| 41 | + private QueryBuilder() {} | |
| 76 | 42 | |
| 77 | 43 | /** |
| 78 | 44 | * Start building a new SELECT query that selects the provided names. |
| @@ -83,8 +49,8 @@ | ||
| 83 | 49 | * @return an in-construction SELECT query (you will need to provide at |
| 84 | 50 | * least a FROM clause to complete the query). |
| 85 | 51 | */ |
| 86 | - public Select.Builder select(String... columns) { | |
| 87 | - return new Select.Builder(protocolVersion, codecRegistry, Arrays.asList((Object[])columns)); | |
| 52 | + public static Select.Builder select(String... columns) { | |
| 53 | + return new Select.Builder(Arrays.asList((Object[])columns)); | |
| 88 | 54 | } |
| 89 | 55 | |
| 90 | 56 | /** |
| @@ -93,9 +59,9 @@ | ||
| 93 | 59 | * @return an in-construction SELECT query (you will need to provide a |
| 94 | 60 | * column selection and at least a FROM clause to complete the query). |
| 95 | 61 | */ |
| 96 | - public Select.Selection select() { | |
| 62 | + public static Select.Selection select() { | |
| 97 | 63 | // Note: the fact we return Select.Selection as return type is on purpose. |
| 98 | - return new Select.SelectionOrAlias(protocolVersion, codecRegistry); | |
| 64 | + return new Select.SelectionOrAlias(); | |
| 99 | 65 | } |
| 100 | 66 | |
| 101 | 67 | /** |
| @@ -104,8 +70,8 @@ | ||
| 104 | 70 | * @param table the name of the table in which to insert. |
| 105 | 71 | * @return an in-construction INSERT query. |
| 106 | 72 | */ |
| 107 | - public Insert insertInto(String table) { | |
| 108 | - return new Insert(protocolVersion, codecRegistry, null, table); | |
| 73 | + public static Insert insertInto(String table) { | |
| 74 | + return new Insert(null, table); | |
| 109 | 75 | } |
| 110 | 76 | |
| 111 | 77 | /** |
| @@ -115,8 +81,8 @@ | ||
| 115 | 81 | * @param table the name of the table to insert into. |
| 116 | 82 | * @return an in-construction INSERT query. |
| 117 | 83 | */ |
| 118 | - public Insert insertInto(String keyspace, String table) { | |
| 119 | - return new Insert(protocolVersion, codecRegistry, keyspace, table); | |
| 84 | + public static Insert insertInto(String keyspace, String table) { | |
| 85 | + return new Insert(keyspace, table); | |
| 120 | 86 | } |
| 121 | 87 | |
| 122 | 88 | /** |
| @@ -125,8 +91,8 @@ | ||
| 125 | 91 | * @param table the name of the table to insert into. |
| 126 | 92 | * @return an in-construction INSERT query. |
| 127 | 93 | */ |
| 128 | - public Insert insertInto(TableMetadata table) { | |
| 129 | - return new Insert(protocolVersion, codecRegistry, table); | |
| 94 | + public static Insert insertInto(TableMetadata table) { | |
| 95 | + return new Insert(table); | |
| 130 | 96 | } |
| 131 | 97 | |
| 132 | 98 | /** |
| @@ -136,8 +102,8 @@ | ||
| 136 | 102 | * @return an in-construction UPDATE query (at least a SET and a WHERE |
| 137 | 103 | * clause needs to be provided to complete the query). |
| 138 | 104 | */ |
| 139 | - public Update update(String table) { | |
| 140 | - return new Update(protocolVersion, codecRegistry, null, table); | |
| 105 | + public static Update update(String table) { | |
| 106 | + return new Update(null, table); | |
| 141 | 107 | } |
| 142 | 108 | |
| 143 | 109 | /** |
| @@ -148,8 +114,8 @@ | ||
| 148 | 114 | * @return an in-construction UPDATE query (at least a SET and a WHERE |
| 149 | 115 | * clause needs to be provided to complete the query). |
| 150 | 116 | */ |
| 151 | - public Update update(String keyspace, String table) { | |
| 152 | - return new Update(protocolVersion, codecRegistry, keyspace, table); | |
| 117 | + public static Update update(String keyspace, String table) { | |
| 118 | + return new Update(keyspace, table); | |
| 153 | 119 | } |
| 154 | 120 | |
| 155 | 121 | /** |
| @@ -159,8 +125,8 @@ | ||
| 159 | 125 | * @return an in-construction UPDATE query (at least a SET and a WHERE |
| 160 | 126 | * clause needs to be provided to complete the query). |
| 161 | 127 | */ |
| 162 | - public Update update(TableMetadata table) { | |
| 163 | - return new Update(protocolVersion, codecRegistry, table); | |
| 128 | + public static Update update(TableMetadata table) { | |
| 129 | + return new Update(table); | |
| 164 | 130 | } |
| 165 | 131 | |
| 166 | 132 | /** |
| @@ -170,8 +136,8 @@ | ||
| 170 | 136 | * @return an in-construction DELETE query (At least a FROM and a WHERE |
| 171 | 137 | * clause needs to be provided to complete the query). |
| 172 | 138 | */ |
| 173 | - public Delete.Builder delete(String... columns) { | |
| 174 | - return new Delete.Builder(protocolVersion, codecRegistry, columns); | |
| 139 | + public static Delete.Builder delete(String... columns) { | |
| 140 | + return new Delete.Builder(columns); | |
| 175 | 141 | } |
| 176 | 142 | |
| 177 | 143 | /** |
| @@ -181,8 +147,8 @@ | ||
| 181 | 147 | * column selection and at least a FROM and a WHERE clause to complete the |
| 182 | 148 | * query). |
| 183 | 149 | */ |
| 184 | - public Delete.Selection delete() { | |
| 185 | - return new Delete.Selection(protocolVersion, codecRegistry); | |
| 150 | + public static Delete.Selection delete() { | |
| 151 | + return new Delete.Selection(); | |
| 186 | 152 | } |
| 187 | 153 | |
| 188 | 154 | /** |
| @@ -198,8 +164,8 @@ | ||
| 198 | 164 | * @param statements the statements to batch. |
| 199 | 165 | * @return a new {@code RegularStatement} that batch {@code statements}. |
| 200 | 166 | */ |
| 201 | - public Batch batch(RegularStatement... statements) { | |
| 202 | - return new Batch(protocolVersion, codecRegistry, statements, true); | |
| 167 | + public static Batch batch(RegularStatement... statements) { | |
| 168 | + return new Batch(statements, true); | |
| 203 | 169 | } |
| 204 | 170 | |
| 205 | 171 | /** |
| @@ -219,8 +185,8 @@ | ||
| 219 | 185 | * @return a new {@code RegularStatement} that batch {@code statements} without |
| 220 | 186 | * using the batch log. |
| 221 | 187 | */ |
| 222 | - public Batch unloggedBatch(RegularStatement... statements) { | |
| 223 | - return new Batch(protocolVersion, codecRegistry, statements, false); | |
| 188 | + public static Batch unloggedBatch(RegularStatement... statements) { | |
| 189 | + return new Batch(statements, false); | |
| 224 | 190 | } |
| 225 | 191 | |
| 226 | 192 | /** |
| @@ -229,8 +195,8 @@ | ||
| 229 | 195 | * @param table the name of the table to truncate. |
| 230 | 196 | * @return the truncation query. |
| 231 | 197 | */ |
| 232 | - public Truncate truncate(String table) { | |
| 233 | - return new Truncate(protocolVersion, codecRegistry, null, table); | |
| 198 | + public static Truncate truncate(String table) { | |
| 199 | + return new Truncate(null, table); | |
| 234 | 200 | } |
| 235 | 201 | |
| 236 | 202 | /** |
| @@ -240,8 +206,8 @@ | ||
| 240 | 206 | * @param table the name of the table to truncate. |
| 241 | 207 | * @return the truncation query. |
| 242 | 208 | */ |
| 243 | - public Truncate truncate(String keyspace, String table) { | |
| 244 | - return new Truncate(protocolVersion, codecRegistry, keyspace, table); | |
| 209 | + public static Truncate truncate(String keyspace, String table) { | |
| 210 | + return new Truncate(keyspace, table); | |
| 245 | 211 | } |
| 246 | 212 | |
| 247 | 213 | /** |
| @@ -250,8 +216,8 @@ | ||
| 250 | 216 | * @param table the table to truncate. |
| 251 | 217 | * @return the truncation query. |
| 252 | 218 | */ |
| 253 | - public Truncate truncate(TableMetadata table) { | |
| 254 | - return new Truncate(protocolVersion, codecRegistry, table); | |
| 219 | + public static Truncate truncate(TableMetadata table) { | |
| 220 | + return new Truncate(table); | |
| 255 | 221 | } |
| 256 | 222 | |
| 257 | 223 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +9 −26
| @@ -20,9 +20,7 @@ | ||
| 20 | 20 | import java.util.Collections; |
| 21 | 21 | import java.util.List; |
| 22 | 22 | |
| 23 | -import com.datastax.driver.core.Cluster; | |
| 24 | 23 | import com.datastax.driver.core.CodecRegistry; |
| 25 | -import com.datastax.driver.core.ProtocolVersion; | |
| 26 | 24 | import com.datastax.driver.core.TableMetadata; |
| 27 | 25 | |
| 28 | 26 | /** |
| @@ -40,16 +38,16 @@ | ||
| 40 | 38 | private Object limit; |
| 41 | 39 | private boolean allowFiltering; |
| 42 | 40 | |
| 43 | - Select(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table, List<Object> columnNames, boolean isDistinct) { | |
| 44 | - super(keyspace, protocolVersion, codecRegistry); | |
| 41 | + Select(String keyspace, String table, List<Object> columnNames, boolean isDistinct) { | |
| 42 | + super(keyspace); | |
| 45 | 43 | this.table = table; |
| 46 | 44 | this.isDistinct = isDistinct; |
| 47 | 45 | this.columnNames = columnNames; |
| 48 | 46 | this.where = new Where(this); |
| 49 | 47 | } |
| 50 | 48 | |
| 51 | - Select(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table, List<Object> columnNames, boolean isDistinct) { | |
| 52 | - super(table, protocolVersion, codecRegistry); | |
| 49 | + Select(TableMetadata table, List<Object> columnNames, boolean isDistinct) { | |
| 50 | + super(table); | |
| 53 | 51 | this.table = escapeId(table.getName()); |
| 54 | 52 | this.isDistinct = isDistinct; |
| 55 | 53 | this.columnNames = columnNames; |
| @@ -57,10 +55,9 @@ | ||
| 57 | 55 | } |
| 58 | 56 | |
| 59 | 57 | @Override |
| 60 | - StringBuilder buildQueryString(List<Object> variables) { | |
| 58 | + StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { | |
| 61 | 59 | StringBuilder builder = new StringBuilder(); |
| 62 | 60 | |
| 63 | - CodecRegistry codecRegistry = getCodecRegistry(); | |
| 64 | 61 | builder.append("SELECT "); |
| 65 | 62 | if (isDistinct) |
| 66 | 63 | builder.append("DISTINCT "); |
| @@ -258,19 +255,13 @@ | ||
| 258 | 255 | */ |
| 259 | 256 | public static class Builder { |
| 260 | 257 | |
| 261 | - private final ProtocolVersion protocolVersion; | |
| 262 | - private final CodecRegistry codecRegistry; | |
| 263 | 258 | List<Object> columnNames; |
| 264 | 259 | boolean isDistinct; |
| 265 | 260 | |
| 266 | - Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 267 | - this.protocolVersion = protocolVersion; | |
| 268 | - this.codecRegistry = codecRegistry; | |
| 261 | + Builder() { | |
| 269 | 262 | } |
| 270 | 263 | |
| 271 | - Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, List<Object> columnNames) { | |
| 272 | - this.protocolVersion = protocolVersion; | |
| 273 | - this.codecRegistry = codecRegistry; | |
| 264 | + Builder(List<Object> columnNames) { | |
| 274 | 265 | this.columnNames = columnNames; |
| 275 | 266 | } |
| 276 | 267 | |
| @@ -302,7 +293,7 @@ | ||
| 302 | 293 | * @return a newly built SELECT statement that selects from {@code keyspace.table}. |
| 303 | 294 | */ |
| 304 | 295 | public Select from(String keyspace, String table) { |
| 305 | - return new Select(protocolVersion, codecRegistry, keyspace, table, columnNames, isDistinct); | |
| 296 | + return new Select(keyspace, table, columnNames, isDistinct); | |
| 306 | 297 | } |
| 307 | 298 | |
| 308 | 299 | /** |
| @@ -312,7 +303,7 @@ | ||
| 312 | 303 | * @return a newly built SELECT statement that selects from {@code table}. |
| 313 | 304 | */ |
| 314 | 305 | public Select from(TableMetadata table) { |
| 315 | - return new Select(protocolVersion, codecRegistry, table, columnNames, isDistinct); | |
| 306 | + return new Select(table, columnNames, isDistinct); | |
| 316 | 307 | } |
| 317 | 308 | } |
| 318 | 309 | |
| @@ -321,10 +312,6 @@ | ||
| 321 | 312 | */ |
| 322 | 313 | public static abstract class Selection extends Builder { |
| 323 | 314 | |
| 324 | - Selection(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 325 | - super(protocolVersion, codecRegistry); | |
| 326 | - } | |
| 327 | - | |
| 328 | 315 | /** |
| 329 | 316 | * Uses DISTINCT selection. |
| 330 | 317 | * |
| @@ -409,10 +396,6 @@ | ||
| 409 | 396 | |
| 410 | 397 | private Object previousSelection; |
| 411 | 398 | |
| 412 | - SelectionOrAlias(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 413 | - super(protocolVersion, codecRegistry); | |
| 414 | - } | |
| 415 | - | |
| 416 | 399 | /** |
| 417 | 400 | * Adds an alias for the just selected item. |
| 418 | 401 | * |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +5 −7
| @@ -17,9 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.util.List; |
| 19 | 19 | |
| 20 | -import com.datastax.driver.core.Cluster; | |
| 21 | 20 | import com.datastax.driver.core.CodecRegistry; |
| 22 | -import com.datastax.driver.core.ProtocolVersion; | |
| 23 | 21 | import com.datastax.driver.core.TableMetadata; |
| 24 | 22 | |
| 25 | 23 | /** |
| @@ -29,18 +27,18 @@ | ||
| 29 | 27 | |
| 30 | 28 | private final String table; |
| 31 | 29 | |
| 32 | - Truncate(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table) { | |
| 33 | - super(keyspace, protocolVersion, codecRegistry); | |
| 30 | + Truncate(String keyspace, String table) { | |
| 31 | + super(keyspace); | |
| 34 | 32 | this.table = table; |
| 35 | 33 | } |
| 36 | 34 | |
| 37 | - Truncate(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table) { | |
| 38 | - super(table, protocolVersion, codecRegistry); | |
| 35 | + Truncate(TableMetadata table) { | |
| 36 | + super(table); | |
| 39 | 37 | this.table = escapeId(table.getName()); |
| 40 | 38 | } |
| 41 | 39 | |
| 42 | 40 | @Override |
| 43 | - protected StringBuilder buildQueryString(List<Object> variables) { | |
| 41 | + protected StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { | |
| 44 | 42 | StringBuilder builder = new StringBuilder(); |
| 45 | 43 | |
| 46 | 44 | builder.append("TRUNCATE "); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +5 −8
| @@ -18,9 +18,7 @@ | ||
| 18 | 18 | import java.util.ArrayList; |
| 19 | 19 | import java.util.List; |
| 20 | 20 | |
| 21 | -import com.datastax.driver.core.Cluster; | |
| 22 | 21 | import com.datastax.driver.core.CodecRegistry; |
| 23 | -import com.datastax.driver.core.ProtocolVersion; | |
| 24 | 22 | import com.datastax.driver.core.TableMetadata; |
| 25 | 23 | import com.datastax.driver.core.querybuilder.Assignment.CounterAssignment; |
| 26 | 24 | |
| @@ -36,8 +34,8 @@ | ||
| 36 | 34 | private final Conditions conditions; |
| 37 | 35 | private boolean ifExists; |
| 38 | 36 | |
| 39 | - Update(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table) { | |
| 40 | - super(keyspace, protocolVersion, codecRegistry); | |
| 37 | + Update(String keyspace, String table) { | |
| 38 | + super(keyspace); | |
| 41 | 39 | this.table = table; |
| 42 | 40 | this.assignments = new Assignments(this); |
| 43 | 41 | this.where = new Where(this); |
| @@ -46,8 +44,8 @@ | ||
| 46 | 44 | this.ifExists = false; |
| 47 | 45 | } |
| 48 | 46 | |
| 49 | - Update(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table) { | |
| 50 | - super(table, protocolVersion, codecRegistry); | |
| 47 | + Update(TableMetadata table) { | |
| 48 | + super(table); | |
| 51 | 49 | this.table = escapeId(table.getName()); |
| 52 | 50 | this.assignments = new Assignments(this); |
| 53 | 51 | this.where = new Where(this); |
| @@ -57,7 +55,7 @@ | ||
| 57 | 55 | } |
| 58 | 56 | |
| 59 | 57 | @Override |
| 60 | - StringBuilder buildQueryString(List<Object> variables) { | |
| 58 | + StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { | |
| 61 | 59 | StringBuilder builder = new StringBuilder(); |
| 62 | 60 | |
| 63 | 61 | builder.append("UPDATE "); |
| @@ -65,7 +63,6 @@ | ||
| 65 | 63 | Utils.appendName(keyspace, builder).append('.'); |
| 66 | 64 | Utils.appendName(table, builder); |
| 67 | 65 | |
| 68 | - CodecRegistry codecRegistry = getCodecRegistry(); | |
| 69 | 66 | if (!usings.usings.isEmpty()) { |
| 70 | 67 | builder.append(" USING "); |
| 71 | 68 | Utils.joinAndAppend(builder, codecRegistry, " AND ", usings.usings, variables); |
modified driver-core/src/main/java/com/datastax/driver/core/schemabuilder/SchemaStatement.java +12 −4
| @@ -21,6 +21,8 @@ | ||
| 21 | 21 | |
| 22 | 22 | import com.google.common.base.Strings; |
| 23 | 23 | |
| 24 | +import com.datastax.driver.core.CodecRegistry; | |
| 25 | +import com.datastax.driver.core.ProtocolVersion; | |
| 24 | 26 | import com.datastax.driver.core.RegularStatement; |
| 25 | 27 | |
| 26 | 28 | /** |
| @@ -42,7 +44,7 @@ | ||
| 42 | 44 | abstract String buildInternal(); |
| 43 | 45 | |
| 44 | 46 | @Override |
| 45 | - public String getQueryString() { | |
| 47 | + public String getQueryString(CodecRegistry codecRegistry) { | |
| 46 | 48 | if (cache == null) { |
| 47 | 49 | cache = buildInternal(); |
| 48 | 50 | } |
| @@ -50,13 +52,13 @@ | ||
| 50 | 52 | } |
| 51 | 53 | |
| 52 | 54 | @Override |
| 53 | - public ByteBuffer[] getValues() { | |
| 55 | + public ByteBuffer[] getValues(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 54 | 56 | // DDL statements never have values |
| 55 | 57 | return new ByteBuffer[0]; |
| 56 | 58 | } |
| 57 | 59 | |
| 58 | 60 | @Override |
| 59 | - public boolean hasValues() { | |
| 61 | + public boolean hasValues(CodecRegistry codecRegistry) { | |
| 60 | 62 | return false; |
| 61 | 63 | } |
| 62 | 64 | |
| @@ -67,8 +69,14 @@ | ||
| 67 | 69 | return null; |
| 68 | 70 | } |
| 69 | 71 | |
| 72 | + /** | |
| 73 | + * {@inheritDoc} | |
| 74 | + * | |
| 75 | + * @param protocolVersion unused by this implementation (the key is always null for schema statements). | |
| 76 | + * @param codecRegistry unused by this implementation (the key is always null for schema statements). | |
| 77 | + */ | |
| 70 | 78 | @Override |
| 71 | - public ByteBuffer getRoutingKey() { | |
| 79 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 72 | 80 | return null; // there is no token awareness for DDL statements |
| 73 | 81 | } |
| 74 | 82 |
modified driver-core/src/test/java/com/datastax/driver/core/AbstractPoliciesTest.java +6 −6
| @@ -24,12 +24,12 @@ | ||
| 24 | 24 | import static org.testng.Assert.assertTrue; |
| 25 | 25 | import static org.testng.Assert.fail; |
| 26 | 26 | |
| 27 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 28 | - | |
| 29 | 27 | import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_GENERIC_FORMAT; |
| 30 | 28 | import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; |
| 31 | 29 | import static com.datastax.driver.core.TestUtils.SIMPLE_KEYSPACE; |
| 32 | 30 | import static com.datastax.driver.core.TestUtils.SIMPLE_TABLE; |
| 31 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.batch; | |
| 32 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 33 | 33 | |
| 34 | 34 | public abstract class AbstractPoliciesTest { |
| 35 | 35 | private static final boolean DEBUG = false; |
| @@ -184,11 +184,11 @@ | ||
| 184 | 184 | for (int i = 0; i < n; ++i) |
| 185 | 185 | if (batch) |
| 186 | 186 | // BUG: WriteType == SIMPLE |
| 187 | - c.session.execute(new QueryBuilder(c.cluster).batch() | |
| 188 | - .add(new QueryBuilder(c.cluster).insertInto(SIMPLE_TABLE).values(new String[]{ "k", "i"}, new Object[]{ 0, 0 })) | |
| 187 | + c.session.execute(batch() | |
| 188 | + .add(insertInto(SIMPLE_TABLE).values(new String[]{ "k", "i"}, new Object[]{ 0, 0 })) | |
| 189 | 189 | .setConsistencyLevel(cl)); |
| 190 | 190 | else |
| 191 | - c.session.execute(c.session.newSimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", SIMPLE_TABLE)).setConsistencyLevel(cl)); | |
| 191 | + c.session.execute(new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", SIMPLE_TABLE)).setConsistencyLevel(cl)); | |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | |
| @@ -216,7 +216,7 @@ | ||
| 216 | 216 | ByteBuffer routingKey = ByteBuffer.allocate(4); |
| 217 | 217 | routingKey.putInt(0, 0); |
| 218 | 218 | for (int i = 0; i < n; ++i) |
| 219 | - addCoordinator(c.session.execute(c.session.newSimpleStatement(String.format("SELECT * FROM %s WHERE k = 0", SIMPLE_TABLE)).setRoutingKey(routingKey).setConsistencyLevel(cl))); | |
| 219 | + addCoordinator(c.session.execute(new SimpleStatement(String.format("SELECT * FROM %s WHERE k = 0", SIMPLE_TABLE)).setRoutingKey(routingKey).setConsistencyLevel(cl))); | |
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | } |
modified driver-core/src/test/java/com/datastax/driver/core/AsyncResultSetTest.java +1 −1
| @@ -57,7 +57,7 @@ | ||
| 57 | 57 | for (int i = 0; i < totalCount; i++) |
| 58 | 58 | session.execute(String.format("insert into ints (i) values (%d)", i)); |
| 59 | 59 | |
| 60 | - Statement statement = session.newSimpleStatement("select * from ints").setFetchSize(fetchSize); | |
| 60 | + Statement statement = new SimpleStatement("select * from ints").setFetchSize(fetchSize); | |
| 61 | 61 | ResultsAccumulator results = new ResultsAccumulator(); |
| 62 | 62 | |
| 63 | 63 | ListenableFuture<ResultSet> future = Futures.transform( |
modified driver-core/src/test/java/com/datastax/driver/core/BatchStatementTest.java +2 −2
| @@ -40,7 +40,7 @@ | ||
| 40 | 40 | |
| 41 | 41 | BatchStatement batch = new BatchStatement(); |
| 42 | 42 | |
| 43 | - batch.add(session.newSimpleStatement("INSERT INTO test (k, v) VALUES (?, ?)", "key1", 0)); | |
| 43 | + batch.add(new SimpleStatement("INSERT INTO test (k, v) VALUES (?, ?)", "key1", 0)); | |
| 44 | 44 | batch.add(st.bind("key1", 1)); |
| 45 | 45 | batch.add(st.bind("key2", 0)); |
| 46 | 46 | |
| @@ -87,7 +87,7 @@ | ||
| 87 | 87 | |
| 88 | 88 | BatchStatement batch = new BatchStatement(); |
| 89 | 89 | |
| 90 | - batch.add(session.newSimpleStatement("INSERT INTO test (k, v) VALUES (?, ?)", "key1", 0)); | |
| 90 | + batch.add(new SimpleStatement("INSERT INTO test (k, v) VALUES (?, ?)", "key1", 0)); | |
| 91 | 91 | batch.add(st.bind("key1", 1)); |
| 92 | 92 | batch.add(st.bind("key1", 2)); |
| 93 | 93 |
modified driver-core/src/test/java/com/datastax/driver/core/ConditionalUpdateTest.java +1 −1
| @@ -78,7 +78,7 @@ | ||
| 78 | 78 | |
| 79 | 79 | // This is really contrived, we just want to cover the code path in ArrayBackedResultSet#MultiPage. |
| 80 | 80 | // Currently CAS update results are never multipage, so it's hard to come up with a meaningful example. |
| 81 | - ResultSet rs = session.execute(session.newSimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1)); | |
| 81 | + ResultSet rs = session.execute(new SimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1)); | |
| 82 | 82 | |
| 83 | 83 | assertTrue(rs.wasApplied()); |
| 84 | 84 | } |
modified driver-core/src/test/java/com/datastax/driver/core/CustomPayloadTest.java +15 −16
| @@ -35,11 +35,11 @@ | ||
| 35 | 35 | |
| 36 | 36 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 37 | 37 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 38 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 39 | 38 | import com.datastax.driver.core.utils.CassandraVersion; |
| 40 | 39 | |
| 41 | 40 | import static com.datastax.driver.core.ProtocolVersion.V3; |
| 42 | 41 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 42 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 43 | 43 | |
| 44 | 44 | @CassandraVersion(major = 2.2) |
| 45 | 45 | public class CustomPayloadTest extends CCMBridge.PerClassSingleNodeCluster { |
| @@ -64,7 +64,7 @@ | ||
| 64 | 64 | |
| 65 | 65 | @Test(groups = "short") |
| 66 | 66 | public void should_echo_custom_payload_when_executing_statement() throws Exception { |
| 67 | - Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 67 | + Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 68 | 68 | statement.setOutgoingPayload(payload1); |
| 69 | 69 | ResultSet rows = session.execute(statement); |
| 70 | 70 | Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); |
| @@ -73,7 +73,7 @@ | ||
| 73 | 73 | |
| 74 | 74 | @Test(groups = "short") |
| 75 | 75 | public void should_echo_custom_payload_when_executing_batch_statement() throws Exception { |
| 76 | - Statement statement = new BatchStatement().add(session.newSimpleStatement("INSERT INTO t1 (c1, c2) values (1, 'foo')")); | |
| 76 | + Statement statement = new BatchStatement().add(new SimpleStatement("INSERT INTO t1 (c1, c2) values (1, 'foo')")); | |
| 77 | 77 | statement.setOutgoingPayload(payload1); |
| 78 | 78 | ResultSet rows = session.execute(statement); |
| 79 | 79 | Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); |
| @@ -82,8 +82,7 @@ | ||
| 82 | 82 | |
| 83 | 83 | @Test(groups = "short") |
| 84 | 84 | public void should_echo_custom_payload_when_building_statement() throws Exception { |
| 85 | - QueryBuilder builder = new QueryBuilder(cluster); | |
| 86 | - Statement statement = builder.select("c2").from("t1").where(eq("c1", 1)).setOutgoingPayload(payload1); | |
| 85 | + Statement statement = select("c2").from("t1").where(eq("c1", 1)).setOutgoingPayload(payload1); | |
| 87 | 86 | ResultSet rows = session.execute(statement); |
| 88 | 87 | Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); |
| 89 | 88 | assertThat(actual).isEqualTo(payload1); |
| @@ -97,7 +96,7 @@ | ||
| 97 | 96 | */ |
| 98 | 97 | @Test(groups = "short") |
| 99 | 98 | public void should_propagate_incoming_payload_to_bound_statement() throws Exception { |
| 100 | - RegularStatement statement = session.newSimpleStatement("SELECT c2 as col1 FROM t1 where c1 = ?"); | |
| 99 | + RegularStatement statement = new SimpleStatement("SELECT c2 as col1 FROM t1 where c1 = ?"); | |
| 101 | 100 | statement.setOutgoingPayload(payload1); |
| 102 | 101 | PreparedStatement ps = session.prepare(statement); |
| 103 | 102 | // Prepared statement should inherit outgoing payload |
| @@ -124,7 +123,7 @@ | ||
| 124 | 123 | */ |
| 125 | 124 | @Test(groups = "short") |
| 126 | 125 | public void should_override_incoming_payload_when_outgoing_payload_explicitly_set_on_preparing_statement() throws Exception { |
| 127 | - RegularStatement statement = session.newSimpleStatement("SELECT c2 as col2 FROM t1 where c1 = ?"); | |
| 126 | + RegularStatement statement = new SimpleStatement("SELECT c2 as col2 FROM t1 where c1 = ?"); | |
| 128 | 127 | statement.setOutgoingPayload(payload1); |
| 129 | 128 | PreparedStatement ps = session.prepare(statement); |
| 130 | 129 | // Prepared statement should inherit outgoing payload |
| @@ -151,7 +150,7 @@ | ||
| 151 | 150 | */ |
| 152 | 151 | @Test(groups = "short") |
| 153 | 152 | public void should_not_set_any_payload_on_bound_statement() throws Exception { |
| 154 | - RegularStatement statement = session.newSimpleStatement("SELECT c2 as col3 FROM t1 where c1 = ?"); | |
| 153 | + RegularStatement statement = new SimpleStatement("SELECT c2 as col3 FROM t1 where c1 = ?"); | |
| 155 | 154 | PreparedStatement ps = session.prepare(statement); |
| 156 | 155 | assertThat(ps.getOutgoingPayload()).isNull(); |
| 157 | 156 | assertThat(ps.getIncomingPayload()).isNull(); |
| @@ -182,7 +181,7 @@ | ||
| 182 | 181 | public void should_echo_custom_payload_when_paginating() throws Exception { |
| 183 | 182 | session.execute("INSERT INTO t1 (c1, c2) VALUES (1, 'a')"); |
| 184 | 183 | session.execute("INSERT INTO t1 (c1, c2) VALUES (1, 'b')"); |
| 185 | - Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 = 1"); | |
| 184 | + Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = 1"); | |
| 186 | 185 | statement.setFetchSize(1); |
| 187 | 186 | statement.setOutgoingPayload(payload1); |
| 188 | 187 | ResultSet rows = session.execute(statement); |
| @@ -198,7 +197,7 @@ | ||
| 198 | 197 | public void should_encode_null_values() throws Exception { |
| 199 | 198 | Map<String, ByteBuffer> payload = new HashMap<String, ByteBuffer>(); |
| 200 | 199 | payload.put("k1", Statement.NULL_PAYLOAD_VALUE); |
| 201 | - Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 200 | + Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 202 | 201 | statement.setOutgoingPayload(payload); |
| 203 | 202 | ResultSet rows = session.execute(statement); |
| 204 | 203 | Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); |
| @@ -209,28 +208,28 @@ | ||
| 209 | 208 | public void should_throw_npe_when_null_key_on_regular_statement() throws Exception { |
| 210 | 209 | Map<String, ByteBuffer> payload = new HashMap<String, ByteBuffer>(); |
| 211 | 210 | payload.put(null, ByteBuffer.wrap(new byte[]{ 1 })); |
| 212 | - session.newSimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1).setOutgoingPayload(payload); | |
| 211 | + new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1).setOutgoingPayload(payload); | |
| 213 | 212 | } |
| 214 | 213 | |
| 215 | 214 | @Test(groups = "unit", expectedExceptions = NullPointerException.class) |
| 216 | 215 | public void should_throw_npe_when_null_value_on_regular_statement() throws Exception { |
| 217 | 216 | Map<String, ByteBuffer> payload = new HashMap<String, ByteBuffer>(); |
| 218 | 217 | payload.put("k1", null); |
| 219 | - session.newSimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1).setOutgoingPayload(payload); | |
| 218 | + new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1).setOutgoingPayload(payload); | |
| 220 | 219 | } |
| 221 | 220 | |
| 222 | 221 | @Test(groups = "short", expectedExceptions = NullPointerException.class) |
| 223 | 222 | public void should_throw_npe_when_null_key_on_prepared_statement() throws Exception { |
| 224 | 223 | Map<String, ByteBuffer> payload = new HashMap<String, ByteBuffer>(); |
| 225 | 224 | payload.put(null, ByteBuffer.wrap(new byte[]{ 1 })); |
| 226 | - session.prepare(session.newSimpleStatement("SELECT c2 FROM t1 where c1 = 1")).setOutgoingPayload(payload); | |
| 225 | + session.prepare(new SimpleStatement("SELECT c2 FROM t1 where c1 = 1")).setOutgoingPayload(payload); | |
| 227 | 226 | } |
| 228 | 227 | |
| 229 | 228 | @Test(groups = "short", expectedExceptions = NullPointerException.class) |
| 230 | 229 | public void should_throw_npe_when_null_value_on_prepared_statement() throws Exception { |
| 231 | 230 | Map<String, ByteBuffer> payload = new HashMap<String, ByteBuffer>(); |
| 232 | 231 | payload.put("k1", null); |
| 233 | - session.prepare(session.newSimpleStatement("SELECT c2 FROM t1 where c1 = 2")).setOutgoingPayload(payload); | |
| 232 | + session.prepare(new SimpleStatement("SELECT c2 FROM t1 where c1 = 2")).setOutgoingPayload(payload); | |
| 234 | 233 | } |
| 235 | 234 | |
| 236 | 235 | @Test(groups = "short") |
| @@ -244,7 +243,7 @@ | ||
| 244 | 243 | .build() |
| 245 | 244 | .init(); |
| 246 | 245 | v3session = v3cluster.connect(); |
| 247 | - Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 246 | + Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 248 | 247 | statement.setOutgoingPayload(payload1); |
| 249 | 248 | v3session.execute(statement); |
| 250 | 249 | fail("Should not send custom payloads with protocol V3"); |
| @@ -280,7 +279,7 @@ | ||
| 280 | 279 | try { |
| 281 | 280 | logger.setLevel(TRACE); |
| 282 | 281 | logger.addAppender(appender); |
| 283 | - Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 282 | + Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); | |
| 284 | 283 | statement.setOutgoingPayload(payload1); |
| 285 | 284 | session.execute(statement); |
| 286 | 285 | String logs = appender.waitAndGet(10000); |
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java +2 −2
| @@ -106,7 +106,7 @@ | ||
| 106 | 106 | session.execute(query); |
| 107 | 107 | break; |
| 108 | 108 | case SIMPLE_WITH_PARAM: |
| 109 | - SimpleStatement statement = session.newSimpleStatement(table.insertStatement, table.sampleValue); | |
| 109 | + SimpleStatement statement = new SimpleStatement(table.insertStatement, table.sampleValue); | |
| 110 | 110 | checkGetValuesReturnsSerializedValue(protocolVersion, statement, table); |
| 111 | 111 | session.execute(statement); |
| 112 | 112 | break; |
| @@ -157,7 +157,7 @@ | ||
| 157 | 157 | |
| 158 | 158 | public void checkGetValuesReturnsSerializedValue(ProtocolVersion protocolVersion, SimpleStatement statement, TestTable table) { |
| 159 | 159 | CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); |
| 160 | - ByteBuffer[] values = statement.getValues(); | |
| 160 | + ByteBuffer[] values = statement.getValues(protocolVersion, codecRegistry); | |
| 161 | 161 | assertThat(values.length).isEqualTo(1); |
| 162 | 162 | assertThat(values[0]) |
| 163 | 163 | .as("Value not serialized as expected for " + table.sampleValue) |
modified driver-core/src/test/java/com/datastax/driver/core/FetchingTest.java +1 −1
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | for (int i = 0; i < 100; i++) |
| 44 | 44 | session.execute(String.format("INSERT INTO test (k, v) VALUES ('%s', %d)", key, i)); |
| 45 | 45 | |
| 46 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", key)); | |
| 46 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", key)); | |
| 47 | 47 | st.setFetchSize(5); // Ridiculously small fetch size for testing purpose. Don't do at home. |
| 48 | 48 | ResultSet rs = session.execute(st); |
| 49 | 49 |
modified driver-core/src/test/java/com/datastax/driver/core/LargeDataTest.java +14 −17
| @@ -24,11 +24,13 @@ | ||
| 24 | 24 | |
| 25 | 25 | import com.datastax.driver.core.querybuilder.Batch; |
| 26 | 26 | import com.datastax.driver.core.querybuilder.Insert; |
| 27 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 28 | 27 | import com.datastax.driver.core.utils.CassandraVersion; |
| 29 | 28 | |
| 30 | 29 | import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; |
| 30 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.batch; | |
| 31 | 31 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 32 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 33 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 32 | 34 | |
| 33 | 35 | /** |
| 34 | 36 | * Test limitations when using large amounts of data with the driver |
| @@ -43,13 +45,12 @@ | ||
| 43 | 45 | */ |
| 44 | 46 | private void testWideRows(CCMBridge.CCMCluster c, int key) throws Throwable { |
| 45 | 47 | // Write data |
| 46 | - QueryBuilder builder = new QueryBuilder(c.cluster); | |
| 47 | 48 | for (int i = 0; i < 1000000; ++i) { |
| 48 | - c.session.execute(builder.insertInto("wide_rows").value("k", key).value("i", i).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 49 | + c.session.execute(insertInto("wide_rows").value("k", key).value("i", i).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | 52 | // Read data |
| 52 | - ResultSet rs = c.session.execute(builder.select("i").from("wide_rows").where(eq("k", key))); | |
| 53 | + ResultSet rs = c.session.execute(select("i").from("wide_rows").where(eq("k", key))); | |
| 53 | 54 | |
| 54 | 55 | // Verify data |
| 55 | 56 | int i = 0; |
| @@ -66,15 +67,14 @@ | ||
| 66 | 67 | */ |
| 67 | 68 | private void testWideBatchRows(CCMBridge.CCMCluster c, int key) throws Throwable { |
| 68 | 69 | // Write data |
| 69 | - QueryBuilder builder = new QueryBuilder(c.cluster); | |
| 70 | - Batch q = builder.batch(); | |
| 70 | + Batch q = batch(); | |
| 71 | 71 | for (int i = 0; i < 4000; ++i) { |
| 72 | - q = q.add(builder.insertInto("wide_batch_rows").value("k", key).value("i", i)); | |
| 72 | + q = q.add(insertInto("wide_batch_rows").value("k", key).value("i", i)); | |
| 73 | 73 | } |
| 74 | 74 | c.session.execute(q.setConsistencyLevel(ConsistencyLevel.QUORUM)); |
| 75 | 75 | |
| 76 | 76 | // Read data |
| 77 | - ResultSet rs = c.session.execute(builder.select("i").from("wide_batch_rows").where(eq("k", key))); | |
| 77 | + ResultSet rs = c.session.execute(select("i").from("wide_batch_rows").where(eq("k", key))); | |
| 78 | 78 | |
| 79 | 79 | // Verify data |
| 80 | 80 | int i = 0; |
| @@ -96,13 +96,12 @@ | ||
| 96 | 96 | bb.flip(); |
| 97 | 97 | |
| 98 | 98 | // Write data |
| 99 | - QueryBuilder builder = new QueryBuilder(c.cluster); | |
| 100 | 99 | for (int i = 0; i < 1000000; ++i) { |
| 101 | - c.session.execute(builder.insertInto("wide_byte_rows").value("k", key).value("i", bb).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 100 | + c.session.execute(insertInto("wide_byte_rows").value("k", key).value("i", bb).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 102 | 101 | } |
| 103 | 102 | |
| 104 | 103 | // Read data |
| 105 | - ResultSet rs = c.session.execute(builder.select("i").from("wide_byte_rows").where(eq("k", key))); | |
| 104 | + ResultSet rs = c.session.execute(select("i").from("wide_byte_rows").where(eq("k", key))); | |
| 106 | 105 | |
| 107 | 106 | // Verify data |
| 108 | 107 | for (Row row : rs) { |
| @@ -118,16 +117,15 @@ | ||
| 118 | 117 | */ |
| 119 | 118 | private void testLargeText(CCMBridge.CCMCluster c, int key) throws Throwable { |
| 120 | 119 | // Write data |
| 121 | - QueryBuilder builder = new QueryBuilder(c.cluster); | |
| 122 | 120 | StringBuilder b = new StringBuilder(); |
| 123 | 121 | for (int i = 0; i < 1000000; ++i) { |
| 124 | 122 | // Create ultra-long text |
| 125 | 123 | b.append(i); |
| 126 | 124 | } |
| 127 | - c.session.execute(builder.insertInto("large_text").value("k", key).value("txt", b.toString()).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 125 | + c.session.execute(insertInto("large_text").value("k", key).value("txt", b.toString()).setConsistencyLevel(ConsistencyLevel.QUORUM)); | |
| 128 | 126 | |
| 129 | 127 | // Read data |
| 130 | - Row row = c.session.execute(builder.select().all().from("large_text").where(eq("k", key))).one(); | |
| 128 | + Row row = c.session.execute(select().all().from("large_text").where(eq("k", key))).one(); | |
| 131 | 129 | |
| 132 | 130 | // Verify data |
| 133 | 131 | assertTrue(b.toString().equals(row.getString("txt"))); |
| @@ -167,15 +165,14 @@ | ||
| 167 | 165 | */ |
| 168 | 166 | private void testWideTable(CCMBridge.CCMCluster c, int key) throws Throwable { |
| 169 | 167 | // Write data |
| 170 | - QueryBuilder builder = new QueryBuilder(c.cluster); | |
| 171 | - Insert insertStatement = builder.insertInto("wide_table").value("k", key); | |
| 168 | + Insert insertStatement = insertInto("wide_table").value("k", key); | |
| 172 | 169 | for (int i = 0; i < 330; ++i) { |
| 173 | 170 | insertStatement = insertStatement.value(createColumnName(i), i); |
| 174 | 171 | } |
| 175 | 172 | c.session.execute(insertStatement.setConsistencyLevel(ConsistencyLevel.QUORUM)); |
| 176 | 173 | |
| 177 | 174 | // Read data |
| 178 | - Row row = c.session.execute(builder.select().all().from("wide_table").where(eq("k", key))).one(); | |
| 175 | + Row row = c.session.execute(select().all().from("wide_table").where(eq("k", key))).one(); | |
| 179 | 176 | |
| 180 | 177 | // Verify data |
| 181 | 178 | for (int i = 0; i < 330; ++i) { |
modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +1 −1
| @@ -82,7 +82,7 @@ | ||
| 82 | 82 | retryDecision = RetryDecision.retry(ConsistencyLevel.ONE); |
| 83 | 83 | |
| 84 | 84 | // We only have one node, this will throw an unavailable exception |
| 85 | - Statement statement = session.newSimpleStatement("SELECT v FROM test WHERE k = 1").setConsistencyLevel(ConsistencyLevel.TWO); | |
| 85 | + Statement statement = new SimpleStatement("SELECT v FROM test WHERE k = 1").setConsistencyLevel(ConsistencyLevel.TWO); | |
| 86 | 86 | session.execute(statement); |
| 87 | 87 | |
| 88 | 88 | Errors errors = cluster.getMetrics().getErrorMetrics(); |
modified driver-core/src/test/java/com/datastax/driver/core/PagingStateTest.java +8 −8
| @@ -55,12 +55,12 @@ | ||
| 55 | 55 | */ |
| 56 | 56 | @Test(groups = "short") |
| 57 | 57 | public void should_complete_when_using_paging_state() { |
| 58 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 58 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 59 | 59 | ResultSet result = session.execute(st.setFetchSize(20)); |
| 60 | 60 | int pageSize = result.getAvailableWithoutFetching(); |
| 61 | 61 | String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); |
| 62 | 62 | |
| 63 | - st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 63 | + st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 64 | 64 | result = session.execute(st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); |
| 65 | 65 | |
| 66 | 66 | //We have the result starting from the next page we stopped |
| @@ -85,7 +85,7 @@ | ||
| 85 | 85 | boolean setWithFalseContent = false; |
| 86 | 86 | boolean setWithWrongStatement = false; |
| 87 | 87 | |
| 88 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 88 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 89 | 89 | ResultSet result = session.execute(st.setFetchSize(20)); |
| 90 | 90 | |
| 91 | 91 | PagingState savedPagingState = result.getExecutionInfo().getPagingState(); |
| @@ -106,7 +106,7 @@ | ||
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // Changing the statement |
| 109 | - st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", "paging")); | |
| 109 | + st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", "paging")); | |
| 110 | 110 | try { |
| 111 | 111 | st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString)); |
| 112 | 112 | } catch (PagingStateException e) { |
| @@ -170,7 +170,7 @@ | ||
| 170 | 170 | */ |
| 171 | 171 | @Test(groups = "short") |
| 172 | 172 | public void should_return_no_rows_when_paged_to_end() { |
| 173 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 173 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 174 | 174 | ResultSet result = session.execute(st.setFetchSize(20)); |
| 175 | 175 | |
| 176 | 176 | // Consume enough of the iterator to cause all the results to be paged in. |
| @@ -181,7 +181,7 @@ | ||
| 181 | 181 | |
| 182 | 182 | String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); |
| 183 | 183 | |
| 184 | - st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 184 | + st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 185 | 185 | result = session.execute(st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); |
| 186 | 186 | |
| 187 | 187 | assertThat(result.one()).isNull(); |
| @@ -218,12 +218,12 @@ | ||
| 218 | 218 | */ |
| 219 | 219 | @Test(groups = "short") |
| 220 | 220 | public void should_complete_when_using_unsafe_paging_state() { |
| 221 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 221 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 222 | 222 | ResultSet result = session.execute(st.setFetchSize(20)); |
| 223 | 223 | int pageSize = result.getAvailableWithoutFetching(); |
| 224 | 224 | byte[] savedPagingState = result.getExecutionInfo().getPagingStateUnsafe(); |
| 225 | 225 | |
| 226 | - st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 226 | + st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 227 | 227 | result = session.execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); |
| 228 | 228 | |
| 229 | 229 | //We have the result starting from the next page we stopped |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +11 −11
| @@ -341,7 +341,7 @@ | ||
| 341 | 341 | @Test(groups = "short") |
| 342 | 342 | public void prepareStatementInheritPropertiesTest() { |
| 343 | 343 | |
| 344 | - RegularStatement toPrepare = session.newSimpleStatement("SELECT * FROM test WHERE k=?"); | |
| 344 | + RegularStatement toPrepare = new SimpleStatement("SELECT * FROM test WHERE k=?"); | |
| 345 | 345 | toPrepare.setConsistencyLevel(ConsistencyLevel.QUORUM); |
| 346 | 346 | toPrepare.enableTracing(); |
| 347 | 347 | |
| @@ -373,7 +373,7 @@ | ||
| 373 | 373 | BatchStatement bs = new BatchStatement(); |
| 374 | 374 | bs.add(ps1.bind("one", "foo")); |
| 375 | 375 | bs.add(ps2.bind("two")); |
| 376 | - bs.add(session.newSimpleStatement("INSERT INTO " + SIMPLE_TABLE2 + " (k, v) VALUES ('three', 'foobar')")); | |
| 376 | + bs.add(new SimpleStatement("INSERT INTO " + SIMPLE_TABLE2 + " (k, v) VALUES ('three', 'foobar')")); | |
| 377 | 377 | |
| 378 | 378 | session.execute(bs); |
| 379 | 379 | |
| @@ -400,7 +400,7 @@ | ||
| 400 | 400 | |
| 401 | 401 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)", keyspace)); |
| 402 | 402 | BoundStatement bs = ps.bind(1); |
| 403 | - assertThat(bs.getRoutingKey()).isNotNull(); | |
| 403 | + assertThat(bs.getRoutingKey(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNotNull(); | |
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | @Test(groups = "short") |
| @@ -413,7 +413,7 @@ | ||
| 413 | 413 | |
| 414 | 414 | PreparedStatement ps = session.prepare("INSERT INTO \"Test\".\"Foo\" (i) VALUES (?)"); |
| 415 | 415 | BoundStatement bs = ps.bind(1); |
| 416 | - assertThat(bs.getRoutingKey()).isNotNull(); | |
| 416 | + assertThat(bs.getRoutingKey(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNotNull(); | |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | @Test(groups = "short", expectedExceptions = InvalidQueryException.class) |
| @@ -517,7 +517,7 @@ | ||
| 517 | 517 | st2.setString(0, "foo"); |
| 518 | 518 | // i is UNSET |
| 519 | 519 | session.execute(st2); |
| 520 | - Statement st3 = session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 520 | + Statement st3 = new SimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 521 | 521 | st3.enableTracing(); |
| 522 | 522 | ResultSet rows = session.execute(st3); |
| 523 | 523 | assertThat(rows.one().getInt("i")).isEqualTo(1234); |
| @@ -546,7 +546,7 @@ | ||
| 546 | 546 | session.execute(bound); |
| 547 | 547 | |
| 548 | 548 | ResultSet rows = session.execute( |
| 549 | - session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'") | |
| 549 | + new SimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'") | |
| 550 | 550 | .enableTracing()); |
| 551 | 551 | |
| 552 | 552 | assertThat(rows.one().isNull("i")); |
| @@ -575,7 +575,7 @@ | ||
| 575 | 575 | session.execute(bound); |
| 576 | 576 | |
| 577 | 577 | ResultSet rows = session.execute( |
| 578 | - session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'") | |
| 578 | + new SimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'") | |
| 579 | 579 | .enableTracing()); |
| 580 | 580 | |
| 581 | 581 | assertThat(rows.one().isNull("i")); |
| @@ -604,7 +604,7 @@ | ||
| 604 | 604 | st2.setString(0, "foo"); |
| 605 | 605 | // i is UNSET |
| 606 | 606 | session.execute(new BatchStatement().add(st2)); |
| 607 | - Statement st3 = session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 607 | + Statement st3 = new SimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 608 | 608 | st3.enableTracing(); |
| 609 | 609 | ResultSet rows = session.execute(st3); |
| 610 | 610 | assertThat(rows.one().getInt("i")).isEqualTo(1234); |
| @@ -626,7 +626,7 @@ | ||
| 626 | 626 | st1.setString(0, "foo"); |
| 627 | 627 | st1.setToNull(1); |
| 628 | 628 | session.execute(st1); |
| 629 | - Statement st2 = session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 629 | + Statement st2 = new SimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 630 | 630 | st2.enableTracing(); |
| 631 | 631 | ResultSet rows = session.execute(st2); |
| 632 | 632 | assertThat(rows.one().isNull(0)).isTrue(); |
| @@ -649,7 +649,7 @@ | ||
| 649 | 649 | st1.setString(0, "foo"); |
| 650 | 650 | st1.setToNull(1); |
| 651 | 651 | session.execute(new BatchStatement().add(st1)); |
| 652 | - Statement st2 = session.newSimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 652 | + Statement st2 = new SimpleStatement("SELECT i from " + SIMPLE_TABLE + " where k = 'foo'"); | |
| 653 | 653 | st2.enableTracing(); |
| 654 | 654 | ResultSet rows = session.execute(st2); |
| 655 | 655 | assertThat(rows.one().isNull(0)).isTrue(); |
| @@ -673,7 +673,7 @@ | ||
| 673 | 673 | PreparedStatement prepared; |
| 674 | 674 | BoundStatement bound; |
| 675 | 675 | |
| 676 | - statement = session.newSimpleStatement(String.format("SELECT * FROM %s.idempotencetest WHERE i = ?", keyspace)); | |
| 676 | + statement = new SimpleStatement(String.format("SELECT * FROM %s.idempotencetest WHERE i = ?", keyspace)); | |
| 677 | 677 | |
| 678 | 678 | prepared = session.prepare(statement); |
| 679 | 679 | bound = prepared.bind(1); |
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java +1 −1
| @@ -274,7 +274,7 @@ | ||
| 274 | 274 | normal.setLevel(DEBUG); |
| 275 | 275 | Statement unknownStatement = new Statement() { |
| 276 | 276 | @Override |
| 277 | - public ByteBuffer getRoutingKey() { | |
| 277 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 278 | 278 | return null; |
| 279 | 279 | } |
| 280 | 280 |
modified driver-core/src/test/java/com/datastax/driver/core/QueryTimestampTest.java +9 −9
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | public void should_use_CQL_timestamp_over_anything_else() { |
| 63 | 63 | timestampFromGenerator = 10; |
| 64 | 64 | String query = "INSERT INTO foo (k, v) VALUES (1, 1) USING TIMESTAMP 20"; |
| 65 | - session.execute(session.newSimpleStatement(query).setDefaultTimestamp(30)); | |
| 65 | + session.execute(new SimpleStatement(query).setDefaultTimestamp(30)); | |
| 66 | 66 | |
| 67 | 67 | long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); |
| 68 | 68 | assertEquals(writeTime, 20); |
| @@ -72,7 +72,7 @@ | ||
| 72 | 72 | public void should_use_statement_timestamp_over_generator() { |
| 73 | 73 | timestampFromGenerator = 10; |
| 74 | 74 | String query = "INSERT INTO foo (k, v) VALUES (1, 1)"; |
| 75 | - session.execute(session.newSimpleStatement(query).setDefaultTimestamp(30)); | |
| 75 | + session.execute(new SimpleStatement(query).setDefaultTimestamp(30)); | |
| 76 | 76 | |
| 77 | 77 | long writeTime = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); |
| 78 | 78 | assertEquals(writeTime, 30); |
| @@ -102,8 +102,8 @@ | ||
| 102 | 102 | @Test(groups = "short") |
| 103 | 103 | public void should_apply_statement_timestamp_only_to_batched_queries_without_timestamp() { |
| 104 | 104 | BatchStatement batch = new BatchStatement(); |
| 105 | - batch.add(session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); | |
| 106 | - batch.add(session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); | |
| 105 | + batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); | |
| 106 | + batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); | |
| 107 | 107 | batch.setDefaultTimestamp(10); |
| 108 | 108 | session.execute(batch); |
| 109 | 109 | |
| @@ -117,8 +117,8 @@ | ||
| 117 | 117 | public void should_apply_generator_timestamp_only_to_batched_queries_without_timestamp() { |
| 118 | 118 | timestampFromGenerator = 10; |
| 119 | 119 | BatchStatement batch = new BatchStatement(); |
| 120 | - batch.add(session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); | |
| 121 | - batch.add(session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); | |
| 120 | + batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); | |
| 121 | + batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); | |
| 122 | 122 | session.execute(batch); |
| 123 | 123 | |
| 124 | 124 | long writeTime1 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); |
| @@ -132,8 +132,8 @@ | ||
| 132 | 132 | timestampFromGenerator = Long.MIN_VALUE; |
| 133 | 133 | long clientTime = System.currentTimeMillis() * 1000; |
| 134 | 134 | BatchStatement batch = new BatchStatement(); |
| 135 | - batch.add(session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); | |
| 136 | - batch.add(session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); | |
| 135 | + batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)")); | |
| 136 | + batch.add(new SimpleStatement("INSERT INTO foo (k, v) VALUES (2, 1) USING TIMESTAMP 20")); | |
| 137 | 137 | session.execute(batch); |
| 138 | 138 | |
| 139 | 139 | long writeTime1 = session.execute("SELECT writeTime(v) FROM foo WHERE k = 1").one().getLong(0); |
| @@ -144,7 +144,7 @@ | ||
| 144 | 144 | |
| 145 | 145 | @Test(groups = "short") |
| 146 | 146 | public void should_preserve_timestamp_when_retrying() { |
| 147 | - SimpleStatement statement = session.newSimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)"); | |
| 147 | + SimpleStatement statement = new SimpleStatement("INSERT INTO foo (k, v) VALUES (1, 1)"); | |
| 148 | 148 | statement.setDefaultTimestamp(10); |
| 149 | 149 | // This will fail since we test against a single-host cluster. The DowngradingConsistencyRetryPolicy |
| 150 | 150 | // will retry it at ONE. |
modified driver-core/src/test/java/com/datastax/driver/core/QueryTracker.java +1 −1
| @@ -48,7 +48,7 @@ | ||
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | public void query(Session session, int times, ConsistencyLevel cl, Class<? extends Exception> expectedException) { |
| 51 | - Statement statement = session.newSimpleStatement(QUERY); | |
| 51 | + Statement statement = new SimpleStatement(QUERY); | |
| 52 | 52 | if(cl != null) { |
| 53 | 53 | statement.setConsistencyLevel(cl); |
| 54 | 54 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +4 −4
| @@ -62,11 +62,11 @@ | ||
| 62 | 62 | |
| 63 | 63 | // execute |
| 64 | 64 | checkExecuteResultSet(session.execute(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)), key); |
| 65 | - checkExecuteResultSet(session.execute(session.newSimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 65 | + checkExecuteResultSet(session.execute(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 66 | 66 | |
| 67 | 67 | // executeAsync |
| 68 | 68 | checkExecuteResultSet(session.executeAsync(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).getUninterruptibly(), key); |
| 69 | - checkExecuteResultSet(session.executeAsync(session.newSimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 69 | + checkExecuteResultSet(session.executeAsync(new SimpleStatement(String.format(TestUtils.SELECT_ALL_FORMAT, TABLE1)).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | @Test(groups = "short") |
| @@ -151,11 +151,11 @@ | ||
| 151 | 151 | |
| 152 | 152 | // execute |
| 153 | 153 | checkExecuteResultSet(compressedSession.execute(SELECT_ALL), key); |
| 154 | - checkExecuteResultSet(compressedSession.execute(session.newSimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 154 | + checkExecuteResultSet(compressedSession.execute(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)), key); | |
| 155 | 155 | |
| 156 | 156 | // executeAsync |
| 157 | 157 | checkExecuteResultSet(compressedSession.executeAsync(SELECT_ALL).getUninterruptibly(), key); |
| 158 | - checkExecuteResultSet(compressedSession.executeAsync(session.newSimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 158 | + checkExecuteResultSet(compressedSession.executeAsync(new SimpleStatement(SELECT_ALL).setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); | |
| 159 | 159 | |
| 160 | 160 | } finally { |
| 161 | 161 | cluster.getConfiguration().getProtocolOptions().setCompression(ProtocolOptions.Compression.NONE); |
modified driver-core/src/test/java/com/datastax/driver/core/SimpleStatementTest.java +6 −22
| @@ -18,58 +18,42 @@ | ||
| 18 | 18 | import java.util.Collections; |
| 19 | 19 | import java.util.List; |
| 20 | 20 | |
| 21 | -import org.testng.annotations.BeforeClass; | |
| 22 | 21 | import org.testng.annotations.Test; |
| 23 | 22 | |
| 24 | 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 25 | 24 | |
| 26 | -import static org.mockito.Mockito.mock; | |
| 27 | -import static org.mockito.Mockito.when; | |
| 28 | - | |
| 29 | 25 | public class SimpleStatementTest { |
| 30 | 26 | |
| 31 | - Cluster cluster; | |
| 32 | - CodecRegistry codecRegistry = new CodecRegistry(); | |
| 33 | - | |
| 34 | - @BeforeClass(groups = "unit") | |
| 35 | - public void setup() { | |
| 36 | - cluster = mock(Cluster.class); | |
| 37 | - Configuration configuration = mock(Configuration.class); | |
| 38 | - when(cluster.getConfiguration()).thenReturn(configuration); | |
| 39 | - ProtocolOptions protocolOptions = mock(ProtocolOptions.class); | |
| 40 | - when(configuration.getProtocolOptions()).thenReturn(protocolOptions); | |
| 41 | - } | |
| 42 | - | |
| 43 | 27 | @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) |
| 44 | 28 | public void should_fail_if_too_many_variables() { |
| 45 | 29 | List<Object> args = Collections.nCopies(1 << 16, (Object)1); |
| 46 | - new SimpleStatement("mock query", cluster, args.toArray()); | |
| 30 | + new SimpleStatement("mock query", args.toArray()); | |
| 47 | 31 | } |
| 48 | 32 | |
| 49 | 33 | @Test(groups = "unit", expectedExceptions = { IllegalStateException.class }) |
| 50 | 34 | public void should_throw_ISE_if_getObject_called_on_statement_without_values() { |
| 51 | - new SimpleStatement("doesn't matter", cluster).getObject(0); | |
| 35 | + new SimpleStatement("doesn't matter").getObject(0); | |
| 52 | 36 | } |
| 53 | 37 | |
| 54 | 38 | @Test(groups = "unit", expectedExceptions = { IndexOutOfBoundsException.class }) |
| 55 | 39 | public void should_throw_IOOBE_if_getObject_called_with_wrong_index() { |
| 56 | - new SimpleStatement("doesn't matter", cluster, new Object()).getObject(1); | |
| 40 | + new SimpleStatement("doesn't matter", new Object()).getObject(1); | |
| 57 | 41 | } |
| 58 | 42 | |
| 59 | 43 | @Test(groups = "unit") |
| 60 | 44 | public void should_return_object_at_ith_index() { |
| 61 | 45 | Object expected = new Object(); |
| 62 | - Object actual = new SimpleStatement("doesn't matter", cluster, expected).getObject(0); | |
| 46 | + Object actual = new SimpleStatement("doesn't matter", expected).getObject(0); | |
| 63 | 47 | assertThat(actual).isSameAs(expected); |
| 64 | 48 | } |
| 65 | 49 | |
| 66 | 50 | @Test(groups = "unit") |
| 67 | 51 | public void should_return_number_of_values() { |
| 68 | 52 | assertThat( |
| 69 | - new SimpleStatement("doesn't matter", cluster).valuesCount() | |
| 53 | + new SimpleStatement("doesn't matter").valuesCount() | |
| 70 | 54 | ).isEqualTo(0); |
| 71 | 55 | assertThat( |
| 72 | - new SimpleStatement("doesn't matter", cluster, 1, 2).valuesCount() | |
| 56 | + new SimpleStatement("doesn't matter", 1, 2).valuesCount() | |
| 73 | 57 | ).isEqualTo(2); |
| 74 | 58 | } |
| 75 | 59 |
modified driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionIntegrationTest.java +1 −1
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | |
| 68 | 68 | BatchStatement batch = new BatchStatement(); |
| 69 | 69 | for (int k = 0; k < 1000; k++) { |
| 70 | - batch.add(session.newSimpleStatement("insert into foo(k,v) values (?,1)", k)); | |
| 70 | + batch.add(new SimpleStatement("insert into foo(k,v) values (?,1)", k)); | |
| 71 | 71 | } |
| 72 | 72 | session.execute(batch); |
| 73 | 73 |
modified driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionTest.java +1 −1
| @@ -119,7 +119,7 @@ | ||
| 119 | 119 | long execStartCount = errors.getSpeculativeExecutions().getCount(); |
| 120 | 120 | long retriesStartCount = errors.getRetriesOnUnavailable().getCount(); |
| 121 | 121 | |
| 122 | - SimpleStatement statement = session.newSimpleStatement("mock query"); | |
| 122 | + SimpleStatement statement = new SimpleStatement("mock query"); | |
| 123 | 123 | statement.setConsistencyLevel(ConsistencyLevel.TWO); |
| 124 | 124 | ResultSet rs = session.execute(statement); |
| 125 | 125 | Row row = rs.one(); |
modified driver-core/src/test/java/com/datastax/driver/core/StatementIdempotenceTest.java +38 −42
| @@ -27,7 +27,6 @@ | ||
| 27 | 27 | import static org.mockito.Mockito.when; |
| 28 | 28 | |
| 29 | 29 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 30 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 31 | 30 | |
| 32 | 31 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 33 | 32 | |
| @@ -35,8 +34,6 @@ | ||
| 35 | 34 | |
| 36 | 35 | private Cluster cluster; |
| 37 | 36 | |
| 38 | - private QueryBuilder builder; | |
| 39 | - | |
| 40 | 37 | @BeforeMethod(groups = "unit") |
| 41 | 38 | public void setUpQueryBuilder() throws Exception { |
| 42 | 39 | CodecRegistry codecRegistry = new CodecRegistry(); |
| @@ -47,7 +44,6 @@ | ||
| 47 | 44 | when(configuration.getCodecRegistry()).thenReturn(codecRegistry); |
| 48 | 45 | when(configuration.getProtocolOptions()).thenReturn(protocolOptions); |
| 49 | 46 | when(protocolOptions.getProtocolVersion()).thenReturn(TestUtils.getDesiredProtocolVersion()); |
| 50 | - builder = new QueryBuilder(cluster); | |
| 51 | 47 | } |
| 52 | 48 | |
| 53 | 49 | @Test(groups = "unit") |
| @@ -112,66 +108,66 @@ | ||
| 112 | 108 | |
| 113 | 109 | private ImmutableList<BuiltStatement> idempotentBuiltStatements() { |
| 114 | 110 | return ImmutableList.<BuiltStatement>of( |
| 115 | - builder.update("foo").with(set("v", 1)).where(eq("k", 1)), // set simple value | |
| 116 | - builder.update("foo").with(add("s", 1)).where(eq("k", 1)), // add to set | |
| 117 | - builder.update("foo").with(put("m", "a", 1)).where(eq("k", 1)), // put in map | |
| 111 | + update("foo").with(set("v", 1)).where(eq("k", 1)), // set simple value | |
| 112 | + update("foo").with(add("s", 1)).where(eq("k", 1)), // add to set | |
| 113 | + update("foo").with(put("m", "a", 1)).where(eq("k", 1)), // put in map | |
| 118 | 114 | |
| 119 | 115 | // select statements should be idempotent even with function calls |
| 120 | - builder.select().countAll() .from("foo").where(eq("k", 1)), | |
| 121 | - builder.select().ttl("v") .from("foo").where(eq("k", 1)), | |
| 122 | - builder.select().writeTime("v") .from("foo").where(eq("k", 1)), | |
| 123 | - builder.select().fcall("token", "k").from("foo").where(eq("k", 1)) | |
| 116 | + select().countAll() .from("foo").where(eq("k", 1)), | |
| 117 | + select().ttl("v") .from("foo").where(eq("k", 1)), | |
| 118 | + select().writeTime("v") .from("foo").where(eq("k", 1)), | |
| 119 | + select().fcall("token", "k").from("foo").where(eq("k", 1)) | |
| 124 | 120 | |
| 125 | 121 | ); |
| 126 | 122 | } |
| 127 | 123 | |
| 128 | 124 | private ImmutableList<BuiltStatement> nonIdempotentBuiltStatements() { |
| 129 | 125 | return ImmutableList.of( |
| 130 | - builder.update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list | |
| 131 | - builder.update("foo").with(set("v", 1)).and(prepend("l", 1)).where(eq("k", 1)), // prepend to list | |
| 132 | - builder.update("foo").with(incr("c")).where(eq("k", 1)), // counter update | |
| 126 | + update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list | |
| 127 | + update("foo").with(set("v", 1)).and(prepend("l", 1)).where(eq("k", 1)), // prepend to list | |
| 128 | + update("foo").with(incr("c")).where(eq("k", 1)), // counter update | |
| 133 | 129 | |
| 134 | 130 | // function calls |
| 135 | 131 | |
| 136 | - builder.insertInto("foo").value("k", 1).value("v", fcall("token", "k")), | |
| 137 | - builder.insertInto("foo").value("k", 1).value("v", now()), | |
| 138 | - builder.insertInto("foo").value("k", 1).value("v", uuid()), | |
| 132 | + insertInto("foo").value("k", 1).value("v", fcall("token", "k")), | |
| 133 | + insertInto("foo").value("k", 1).value("v", now()), | |
| 134 | + insertInto("foo").value("k", 1).value("v", uuid()), | |
| 139 | 135 | |
| 140 | - builder.insertInto("foo").value("k", 1).value("v", Sets.newHashSet(fcall("token", "k"))), | |
| 141 | - builder.insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())), | |
| 142 | - builder.insertInto("foo").value("k", 1).value("v", Sets.newHashSet(uuid())), | |
| 136 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(fcall("token", "k"))), | |
| 137 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())), | |
| 138 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(uuid())), | |
| 143 | 139 | |
| 144 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, fcall("token", "k")}), | |
| 145 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, now()}), | |
| 146 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, uuid()}), | |
| 140 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, fcall("token", "k")}), | |
| 141 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, now()}), | |
| 142 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, uuid()}), | |
| 147 | 143 | |
| 148 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", fcall("token", "k"))}), | |
| 149 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", now())}), | |
| 150 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", uuid())}), | |
| 144 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", fcall("token", "k"))}), | |
| 145 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", now())}), | |
| 146 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", uuid())}), | |
| 151 | 147 | |
| 152 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(fcall("token", "k"), "foo")}), | |
| 153 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(now() , "foo")}), | |
| 154 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(uuid() , "foo")}), | |
| 148 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(fcall("token", "k"), "foo")}), | |
| 149 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(now() , "foo")}), | |
| 150 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(uuid() , "foo")}), | |
| 155 | 151 | |
| 156 | - builder.update("foo").with(set("v", fcall("token", "k"))) .where(eq("k", 1)), | |
| 157 | - builder.update("foo").with(set("v", now())) .where(eq("k", 1)), | |
| 158 | - builder.update("foo").with(set("v", uuid())) .where(eq("k", 1)), | |
| 152 | + update("foo").with(set("v", fcall("token", "k"))) .where(eq("k", 1)), | |
| 153 | + update("foo").with(set("v", now())) .where(eq("k", 1)), | |
| 154 | + update("foo").with(set("v", uuid())) .where(eq("k", 1)), | |
| 159 | 155 | |
| 160 | - builder.update("foo").with(set("v", Lists.newArrayList(fcall("token", "k")))).where(eq("k", 1)), | |
| 161 | - builder.update("foo").with(set("v", Lists.newArrayList(now()))) .where(eq("k", 1)), | |
| 162 | - builder.update("foo").with(set("v", Lists.newArrayList(uuid()))) .where(eq("k", 1)), | |
| 156 | + update("foo").with(set("v", Lists.newArrayList(fcall("token", "k")))).where(eq("k", 1)), | |
| 157 | + update("foo").with(set("v", Lists.newArrayList(now()))) .where(eq("k", 1)), | |
| 158 | + update("foo").with(set("v", Lists.newArrayList(uuid()))) .where(eq("k", 1)), | |
| 163 | 159 | |
| 164 | 160 | // raw() calls |
| 165 | 161 | |
| 166 | - builder.insertInto("foo").value("k", 1).value("v", raw("foo()")), | |
| 167 | - builder.insertInto("foo").value("k", 1).value("v", Sets.newHashSet(raw("foo()"))), | |
| 162 | + insertInto("foo").value("k", 1).value("v", raw("foo()")), | |
| 163 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(raw("foo()"))), | |
| 168 | 164 | |
| 169 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, raw("foo()")}), | |
| 170 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", raw("foo()"))}), | |
| 171 | - builder.insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(raw("foo()"), "foo")}), | |
| 165 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, raw("foo()")}), | |
| 166 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", raw("foo()"))}), | |
| 167 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(raw("foo()"), "foo")}), | |
| 172 | 168 | |
| 173 | - builder.update("foo").with(set("v", raw("foo()"))) .where(eq("k", 1)), | |
| 174 | - builder.update("foo").with(set("v", Lists.newArrayList(raw("foo()")))).where(eq("k", 1)) | |
| 169 | + update("foo").with(set("v", raw("foo()"))) .where(eq("k", 1)), | |
| 170 | + update("foo").with(set("v", Lists.newArrayList(raw("foo()")))).where(eq("k", 1)) | |
| 175 | 171 | |
| 176 | 172 | ); |
| 177 | 173 | } |
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +3 −3
| @@ -49,7 +49,7 @@ | ||
| 49 | 49 | public void should_pass_wrapped_statement_to_load_balancing_policy() { |
| 50 | 50 | loadBalancingPolicy.customStatementsHandled.set(0); |
| 51 | 51 | |
| 52 | - SimpleStatement s = session.newSimpleStatement("select * from system.local"); | |
| 52 | + SimpleStatement s = new SimpleStatement("select * from system.local"); | |
| 53 | 53 | session.execute(s); |
| 54 | 54 | assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(0); |
| 55 | 55 | |
| @@ -61,7 +61,7 @@ | ||
| 61 | 61 | public void should_pass_wrapped_statement_to_speculative_execution_policy() { |
| 62 | 62 | speculativeExecutionPolicy.customStatementsHandled.set(0); |
| 63 | 63 | |
| 64 | - SimpleStatement s = session.newSimpleStatement("select * from system.local"); | |
| 64 | + SimpleStatement s = new SimpleStatement("select * from system.local"); | |
| 65 | 65 | session.execute(s); |
| 66 | 66 | assertThat(speculativeExecutionPolicy.customStatementsHandled.get()).isEqualTo(0); |
| 67 | 67 | |
| @@ -75,7 +75,7 @@ | ||
| 75 | 75 | |
| 76 | 76 | // Set CL TWO with only one node, so the statement will always cause UNAVAILABLE, |
| 77 | 77 | // which our custom policy ignores. |
| 78 | - Statement s = session.newSimpleStatement("select * from system.local") | |
| 78 | + Statement s = new SimpleStatement("select * from system.local") | |
| 79 | 79 | .setConsistencyLevel(ConsistencyLevel.TWO); |
| 80 | 80 | |
| 81 | 81 | session.execute(s); |
modified driver-core/src/test/java/com/datastax/driver/core/TracingTest.java +2 −2
| @@ -58,7 +58,7 @@ | ||
| 58 | 58 | */ |
| 59 | 59 | @Test(groups = "short") |
| 60 | 60 | public void should_have_a_different_tracingId_for_each_page() { |
| 61 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 61 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 62 | 62 | ResultSet result = session.execute(st.setFetchSize(40).enableTracing()); |
| 63 | 63 | result.all(); |
| 64 | 64 | |
| @@ -87,7 +87,7 @@ | ||
| 87 | 87 | */ |
| 88 | 88 | @Test(groups = "short") |
| 89 | 89 | public void should_preserve_tracing_status_across_retries() { |
| 90 | - SimpleStatement st = session.newSimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 90 | + SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); | |
| 91 | 91 | st.setConsistencyLevel(ConsistencyLevel.THREE).enableTracing(); |
| 92 | 92 | |
| 93 | 93 | ResultSet result = session.execute(st); |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecCollectionsIntegrationTest.java +5 −7
| @@ -22,8 +22,6 @@ | ||
| 22 | 22 | import java.util.Map; |
| 23 | 23 | import java.util.Set; |
| 24 | 24 | |
| 25 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 26 | -import com.datastax.driver.core.utils.CassandraVersion; | |
| 27 | 25 | import com.google.common.collect.ImmutableMap; |
| 28 | 26 | import com.google.common.reflect.TypeToken; |
| 29 | 27 | import org.testng.annotations.BeforeMethod; |
| @@ -34,12 +32,12 @@ | ||
| 34 | 32 | import static org.assertj.core.api.Assertions.assertThat; |
| 35 | 33 | |
| 36 | 34 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 35 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 37 | 36 | |
| 38 | -import static com.datastax.driver.core.TypeTokens.listOf; | |
| 39 | -import static com.datastax.driver.core.TypeTokens.mapOf; | |
| 40 | -import static com.datastax.driver.core.TypeTokens.setOf; | |
| 41 | 37 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 42 | 38 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 39 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 40 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 43 | 41 | |
| 44 | 42 | @CassandraVersion(major=2.0) |
| 45 | 43 | public class TypeCodecCollectionsIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { |
| @@ -75,7 +73,7 @@ | ||
| 75 | 73 | |
| 76 | 74 | @BeforeMethod(groups = "short") |
| 77 | 75 | public void createBuiltStatements() throws Exception { |
| 78 | - insertStmt = new QueryBuilder(cluster).insertInto("\"myTable2\"") | |
| 76 | + insertStmt = insertInto("\"myTable2\"") | |
| 79 | 77 | .value("c_int", bindMarker()) |
| 80 | 78 | .value("l_int", bindMarker()) |
| 81 | 79 | .value("l_bigint", bindMarker()) |
| @@ -83,7 +81,7 @@ | ||
| 83 | 81 | .value("s_double", bindMarker()) |
| 84 | 82 | .value("m_varint", bindMarker()) |
| 85 | 83 | .value("m_decimal", bindMarker()); |
| 86 | - selectStmt = new QueryBuilder(cluster).select("c_int", "l_int", "l_bigint", "s_float", "s_double", "m_varint", "m_decimal") | |
| 84 | + selectStmt = select("c_int", "l_int", "l_bigint", "s_float", "s_double", "m_varint", "m_decimal") | |
| 87 | 85 | .from("\"myTable2\"") |
| 88 | 86 | .where(eq("c_int", bindMarker())); |
| 89 | 87 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecEncapsulationIntegrationTest.java +4 −3
| @@ -31,11 +31,12 @@ | ||
| 31 | 31 | |
| 32 | 32 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 33 | 33 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 34 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 35 | 34 | import com.datastax.driver.core.utils.CassandraVersion; |
| 36 | 35 | |
| 37 | 36 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 38 | 37 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 38 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 39 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 39 | 40 | |
| 40 | 41 | public class TypeCodecEncapsulationIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { |
| 41 | 42 | |
| @@ -85,14 +86,14 @@ | ||
| 85 | 86 | |
| 86 | 87 | @BeforeMethod(groups = "short") |
| 87 | 88 | public void createBuiltStatements() throws Exception { |
| 88 | - insertStmt = new QueryBuilder(cluster).insertInto("\"myTable\"") | |
| 89 | + insertStmt = insertInto("\"myTable\"") | |
| 89 | 90 | .value("c_int", bindMarker()) |
| 90 | 91 | .value("c_bigint", bindMarker()) |
| 91 | 92 | .value("c_float", bindMarker()) |
| 92 | 93 | .value("c_double", bindMarker()) |
| 93 | 94 | .value("c_varint", bindMarker()) |
| 94 | 95 | .value("c_decimal", bindMarker()); |
| 95 | - selectStmt = new QueryBuilder(cluster).select("c_int", "c_bigint", "c_float", "c_double", "c_varint", "c_decimal") | |
| 96 | + selectStmt = select("c_int", "c_bigint", "c_float", "c_double", "c_varint", "c_decimal") | |
| 96 | 97 | .from("\"myTable\"") |
| 97 | 98 | .where(eq("c_int", bindMarker())) |
| 98 | 99 | .and(eq("c_bigint", bindMarker())); |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecNestedCollectionsIntegrationTest.java +4 −3
| @@ -30,11 +30,12 @@ | ||
| 30 | 30 | |
| 31 | 31 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 32 | 32 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 33 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 34 | 33 | import com.datastax.driver.core.utils.CassandraVersion; |
| 35 | 34 | |
| 36 | 35 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 37 | 36 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 37 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 38 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 38 | 39 | |
| 39 | 40 | /** |
| 40 | 41 | * Validates that nested collections are properly encoded, |
| @@ -84,10 +85,10 @@ | ||
| 84 | 85 | |
| 85 | 86 | @BeforeMethod(groups = "short") |
| 86 | 87 | public void createBuiltStatements() throws Exception { |
| 87 | - insertStmt = new QueryBuilder(cluster).insertInto("\"myTable\"") | |
| 88 | + insertStmt = insertInto("\"myTable\"") | |
| 88 | 89 | .value("pk", bindMarker()) |
| 89 | 90 | .value("v", bindMarker()); |
| 90 | - selectStmt = new QueryBuilder(cluster).select("pk", "v") | |
| 91 | + selectStmt = select("pk", "v") | |
| 91 | 92 | .from("\"myTable\"") |
| 92 | 93 | .where(eq("pk", bindMarker())); |
| 93 | 94 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicyIntegrationTest.java +1 −1
| @@ -90,7 +90,7 @@ | ||
| 90 | 90 | RetryPolicy retryPolicy = session.getCluster().getConfiguration().getPolicies().getRetryPolicy(); |
| 91 | 91 | Mockito.reset(retryPolicy); |
| 92 | 92 | |
| 93 | - Statement s = session.newSimpleStatement("SELECT * FROM test.foo WHERE k = 0") | |
| 93 | + Statement s = new SimpleStatement("SELECT * FROM test.foo WHERE k = 0") | |
| 94 | 94 | .setConsistencyLevel(requested); |
| 95 | 95 | |
| 96 | 96 | ResultSet rs = session.execute(s); |
modified driver-core/src/test/java/com/datastax/driver/core/policies/IdempotenceAwareRetryPolicyIntegrationTest.java +3 −3
| @@ -63,7 +63,7 @@ | ||
| 63 | 63 | @Test(groups = "short") |
| 64 | 64 | public void should_retry_on_write_timeout_if_statement_idempotent() { |
| 65 | 65 | simulateError(1, write_request_timeout); |
| 66 | - session.execute(session.newSimpleStatement("mock query").setIdempotent(true)); | |
| 66 | + session.execute(new SimpleStatement("mock query").setIdempotent(true)); | |
| 67 | 67 | assertOnWriteTimeoutWasCalled(1); |
| 68 | 68 | assertThat(errors.getWriteTimeouts().getCount()).isEqualTo(1); |
| 69 | 69 | assertThat(errors.getRetries().getCount()).isEqualTo(1); |
| @@ -111,7 +111,7 @@ | ||
| 111 | 111 | .withQuery("mock query") |
| 112 | 112 | .withThen(then().withFixedDelay(1000L).withRows(row("result", "result1"))) |
| 113 | 113 | .build()); |
| 114 | - session.execute(session.newSimpleStatement("mock query").setIdempotent(true)); | |
| 114 | + session.execute(new SimpleStatement("mock query").setIdempotent(true)); | |
| 115 | 115 | assertOnRequestErrorWasCalled(1, OperationTimedOutException.class); |
| 116 | 116 | assertThat(errors.getClientTimeouts().getCount()).isEqualTo(1); |
| 117 | 117 | assertThat(errors.getRetries().getCount()).isEqualTo(1); |
| @@ -157,7 +157,7 @@ | ||
| 157 | 157 | simulateError(2, error); |
| 158 | 158 | simulateError(3, error); |
| 159 | 159 | try { |
| 160 | - session.execute(session.newSimpleStatement("mock query").setIdempotent(true)); | |
| 160 | + session.execute(new SimpleStatement("mock query").setIdempotent(true)); | |
| 161 | 161 | fail("expected a NoHostAvailableException"); |
| 162 | 162 | } catch (NoHostAvailableException e) { |
| 163 | 163 | assertThat(e.getErrors().keySet()).hasSize(3).containsOnly( |
modified driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +4 −4
| @@ -97,7 +97,7 @@ | ||
| 97 | 97 | |
| 98 | 98 | // then: generating a query plan on a statement using that routing key should properly prioritize node 6 and its replicas. |
| 99 | 99 | // Actual query does not matter, only the keyspace and routing key will be used |
| 100 | - SimpleStatement statement = session.newSimpleStatement("select * from table where k=5"); | |
| 100 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5"); | |
| 101 | 101 | statement.setRoutingKey(routingKey); |
| 102 | 102 | statement.setKeyspace("keyspace"); |
| 103 | 103 | |
| @@ -154,7 +154,7 @@ | ||
| 154 | 154 | |
| 155 | 155 | // Encodes into murmur hash '4557949199137838892' which should belong be owned by node 3. |
| 156 | 156 | ByteBuffer routingKey = TypeCodec.varchar().serialize("should_choose_proper_host_based_on_routing_key", ProtocolVersion.NEWEST_SUPPORTED); |
| 157 | - SimpleStatement statement = session.newSimpleStatement("select * from table where k=5") | |
| 157 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5") | |
| 158 | 158 | .setRoutingKey(routingKey) |
| 159 | 159 | .setKeyspace("keyspace"); |
| 160 | 160 | |
| @@ -200,7 +200,7 @@ | ||
| 200 | 200 | |
| 201 | 201 | // Encodes into murmur hash '-8124212968526248339' which should belong to 1:1 in DC1 and 2:1 in DC2. |
| 202 | 202 | ByteBuffer routingKey = TypeCodec.varchar().serialize("should_choose_host_in_local_dc_when_using_network_topology_strategy_and_dc_aware", ProtocolVersion.NEWEST_SUPPORTED); |
| 203 | - SimpleStatement statement = session.newSimpleStatement("select * from table where k=5") | |
| 203 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5") | |
| 204 | 204 | .setRoutingKey(routingKey) |
| 205 | 205 | .setKeyspace("keyspace"); |
| 206 | 206 | |
| @@ -243,7 +243,7 @@ | ||
| 243 | 243 | // when: A query is made with a routing key and both hosts having that key's token are down. |
| 244 | 244 | // Encodes into murmur hash '6444339665561646341' which should belong to node 4. |
| 245 | 245 | ByteBuffer routingKey = TypeCodec.varchar().serialize("should_use_other_nodes_when_replicas_having_token_are_down", ProtocolVersion.NEWEST_SUPPORTED); |
| 246 | - SimpleStatement statement = session.newSimpleStatement("select * from table where k=5") | |
| 246 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5") | |
| 247 | 247 | .setRoutingKey(routingKey) |
| 248 | 248 | .setKeyspace("keyspace"); |
| 249 | 249 |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilder21ExecutionTest.java +13 −18
| @@ -15,26 +15,26 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | +import java.util.Arrays; | |
| 19 | +import java.util.Collection; | |
| 20 | + | |
| 21 | +import org.testng.annotations.Test; | |
| 22 | + | |
| 18 | 23 | import com.datastax.driver.core.CCMBridge; |
| 19 | 24 | import com.datastax.driver.core.PreparedStatement; |
| 20 | 25 | import com.datastax.driver.core.ResultSet; |
| 21 | 26 | import com.datastax.driver.core.Row; |
| 22 | 27 | import com.datastax.driver.core.utils.CassandraVersion; |
| 23 | 28 | |
| 24 | -import org.testng.annotations.BeforeMethod; | |
| 25 | -import org.testng.annotations.Test; | |
| 26 | - | |
| 27 | -import java.util.Arrays; | |
| 28 | -import java.util.Collection; | |
| 29 | - | |
| 30 | 29 | import static com.datastax.driver.core.Assertions.assertThat; |
| 31 | -import static com.datastax.driver.core.querybuilder.QueryBuilder.*; | |
| 30 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; | |
| 31 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.contains; | |
| 32 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.containsKey; | |
| 33 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 32 | 34 | |
| 33 | 35 | @CassandraVersion(major=2.1) |
| 34 | 36 | public class QueryBuilder21ExecutionTest extends CCMBridge.PerClassSingleNodeCluster { |
| 35 | 37 | |
| 36 | - private QueryBuilder builder; | |
| 37 | - | |
| 38 | 38 | @Override |
| 39 | 39 | protected Collection<String> getTableDefinitions() { |
| 40 | 40 | // Taken from http://www.datastax.com/dev/blog/cql-in-2-1 |
| @@ -53,14 +53,9 @@ | ||
| 53 | 53 | ); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - @BeforeMethod(groups = "short") | |
| 57 | - public void setUpQueryBuilder() throws Exception { | |
| 58 | - builder = new QueryBuilder(cluster); | |
| 59 | - } | |
| 60 | - | |
| 61 | 56 | @Test(groups="short") |
| 62 | 57 | public void should_handle_contains_on_set_with_index() { |
| 63 | - PreparedStatement byCategory = session.prepare(builder.select("id", "description", "categories") | |
| 58 | + PreparedStatement byCategory = session.prepare(select("id", "description", "categories") | |
| 64 | 59 | .from("products") |
| 65 | 60 | .where(contains("categories", bindMarker("category")))); |
| 66 | 61 | |
| @@ -74,7 +69,7 @@ | ||
| 74 | 69 | |
| 75 | 70 | @Test(groups="short") |
| 76 | 71 | public void should_handle_contains_on_list_with_index() { |
| 77 | - PreparedStatement byBuyer = session.prepare(builder.select("id", "description", "buyers") | |
| 72 | + PreparedStatement byBuyer = session.prepare(select("id", "description", "buyers") | |
| 78 | 73 | .from("products") |
| 79 | 74 | .where(contains("buyers", bindMarker("buyer")))); |
| 80 | 75 | |
| @@ -88,7 +83,7 @@ | ||
| 88 | 83 | |
| 89 | 84 | @Test(groups="short") |
| 90 | 85 | public void should_handle_contains_on_map_with_index() { |
| 91 | - PreparedStatement byFeatures = session.prepare(builder.select("id", "description", "features_values") | |
| 86 | + PreparedStatement byFeatures = session.prepare(select("id", "description", "features_values") | |
| 92 | 87 | .from("products") |
| 93 | 88 | .where(contains("features_values", bindMarker("feature")))); |
| 94 | 89 | |
| @@ -103,7 +98,7 @@ | ||
| 103 | 98 | |
| 104 | 99 | @Test(groups="short") |
| 105 | 100 | public void should_handle_contains_key_on_map_with_index() { |
| 106 | - PreparedStatement byFeatures = session.prepare(builder.select("id", "description", "features_keys") | |
| 101 | + PreparedStatement byFeatures = session.prepare(select("id", "description", "features_keys") | |
| 107 | 102 | .from("products") |
| 108 | 103 | .where(containsKey("features_keys", bindMarker("feature")))); |
| 109 | 104 |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +18 −26
| @@ -17,7 +17,6 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.util.*; |
| 19 | 19 | |
| 20 | -import org.testng.annotations.BeforeMethod; | |
| 21 | 20 | import org.testng.annotations.Test; |
| 22 | 21 | |
| 23 | 22 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -34,8 +33,6 @@ | ||
| 34 | 33 | |
| 35 | 34 | private static final String TABLE1 = "test1"; |
| 36 | 35 | |
| 37 | - private QueryBuilder builder; | |
| 38 | - | |
| 39 | 36 | @Override |
| 40 | 37 | protected Collection<String> getTableDefinitions() { |
| 41 | 38 | return Arrays.asList( |
| @@ -44,18 +41,13 @@ | ||
| 44 | 41 | "CREATE TABLE test_coll (k int PRIMARY KEY, a list<int>, b map<int,text>, c set<text>)"); |
| 45 | 42 | } |
| 46 | 43 | |
| 47 | - @BeforeMethod(groups = "short") | |
| 48 | - public void setUpQueryBuilder() throws Exception { | |
| 49 | - builder = new QueryBuilder(cluster); | |
| 50 | - } | |
| 51 | - | |
| 52 | 44 | @Test(groups = "short") |
| 53 | 45 | public void executeTest() throws Exception { |
| 54 | 46 | |
| 55 | - session.execute(builder.insertInto(TABLE1).value("k", "k1").value("t", "This is a test").value("i", 3).value("f", 0.42)); | |
| 56 | - session.execute(builder.update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2"))); | |
| 47 | + session.execute(insertInto(TABLE1).value("k", "k1").value("t", "This is a test").value("i", 3).value("f", 0.42)); | |
| 48 | + session.execute(update(TABLE1).with(set("t", "Another test")).where(eq("k", "k2"))); | |
| 57 | 49 | |
| 58 | - List<Row> rows = session.execute(builder.select().from(TABLE1).where(in("k", "k1", "k2"))).all(); | |
| 50 | + List<Row> rows = session.execute(select().from(TABLE1).where(in("k", "k1", "k2"))).all(); | |
| 59 | 51 | |
| 60 | 52 | assertEquals(2, rows.size()); |
| 61 | 53 | |
| @@ -76,8 +68,8 @@ | ||
| 76 | 68 | public void dateHandlingTest() throws Exception { |
| 77 | 69 | |
| 78 | 70 | Date d = new Date(); |
| 79 | - session.execute(builder.insertInto("dateTest").value("t", d)); | |
| 80 | - String query = builder.select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString(); | |
| 71 | + session.execute(insertInto("dateTest").value("t", d)); | |
| 72 | + String query = select().from("dateTest").where(eq(token("t"), fcall("token", d))).toString(); | |
| 81 | 73 | List<Row> rows = session.execute(query).all(); |
| 82 | 74 | |
| 83 | 75 | assertEquals(1, rows.size()); |
| @@ -90,21 +82,21 @@ | ||
| 90 | 82 | public void prepareTest() throws Exception { |
| 91 | 83 | // Just check we correctly avoid values when there is a bind marker |
| 92 | 84 | String query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',?,0);"; |
| 93 | - BuiltStatement stmt = builder.insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0); | |
| 85 | + BuiltStatement stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker()).value("d", 0); | |
| 94 | 86 | assertEquals(stmt.getQueryString(), query); |
| 95 | 87 | |
| 96 | 88 | query = "INSERT INTO foo (a,b,c,d) VALUES ('foo','bar',:c,0);"; |
| 97 | - stmt = builder.insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker("c")).value("d", 0); | |
| 89 | + stmt = insertInto("foo").value("a", "foo").value("b", "bar").value("c", bindMarker("c")).value("d", 0); | |
| 98 | 90 | assertEquals(stmt.getQueryString(), query); |
| 99 | 91 | } |
| 100 | 92 | |
| 101 | 93 | @Test(groups = "short") |
| 102 | 94 | public void batchNonBuiltStatementTest() throws Exception { |
| 103 | - SimpleStatement simple = session.newSimpleStatement("INSERT INTO " + TABLE1 + " (k, t) VALUES ('batchTest1', 'val1')"); | |
| 104 | - RegularStatement built = builder.insertInto(TABLE1).value("k", "batchTest2").value("t", "val2"); | |
| 105 | - session.execute(builder.batch().add(simple).add(built)); | |
| 106 | - | |
| 107 | - List<Row> rows = session.execute(builder.select().from(TABLE1).where(in("k", "batchTest1", "batchTest2"))).all(); | |
| 95 | + SimpleStatement simple = new SimpleStatement("INSERT INTO " + TABLE1 + " (k, t) VALUES ('batchTest1', 'val1')"); | |
| 96 | + RegularStatement built = insertInto(TABLE1).value("k", "batchTest2").value("t", "val2"); | |
| 97 | + session.execute(batch().add(simple).add(built)); | |
| 98 | + | |
| 99 | + List<Row> rows = session.execute(select().from(TABLE1).where(in("k", "batchTest1", "batchTest2"))).all(); | |
| 108 | 100 | assertEquals(2, rows.size()); |
| 109 | 101 | |
| 110 | 102 | Row r1 = rows.get(0); |
| @@ -121,7 +113,7 @@ | ||
| 121 | 113 | //given |
| 122 | 114 | session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, [1,2,3], null)"); |
| 123 | 115 | //when |
| 124 | - BuiltStatement statement = builder.delete().listElt("a", 1).from("test_coll").where(eq("k", 1)); | |
| 116 | + BuiltStatement statement = delete().listElt("a", 1).from("test_coll").where(eq("k", 1)); | |
| 125 | 117 | session.execute(statement); |
| 126 | 118 | //then |
| 127 | 119 | List<Integer> actual = session.execute("SELECT a FROM test_coll WHERE k = 1").one().getList("a", Integer.class); |
| @@ -133,7 +125,7 @@ | ||
| 133 | 125 | //given |
| 134 | 126 | session.execute("INSERT INTO test_coll (k, a) VALUES (1, [1,2,3])"); |
| 135 | 127 | //when |
| 136 | - BuiltStatement statement = builder.delete().listElt("a", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 128 | + BuiltStatement statement = delete().listElt("a", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 137 | 129 | PreparedStatement ps = session.prepare(statement); |
| 138 | 130 | session.execute(ps.bind(1)); |
| 139 | 131 | //then |
| @@ -146,7 +138,7 @@ | ||
| 146 | 138 | //given |
| 147 | 139 | session.execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); |
| 148 | 140 | //when |
| 149 | - BuiltStatement statement = builder.delete().setElt("c", "foo").from("test_coll").where(eq("k", 1)); | |
| 141 | + BuiltStatement statement = delete().setElt("c", "foo").from("test_coll").where(eq("k", 1)); | |
| 150 | 142 | session.execute(statement); |
| 151 | 143 | //then |
| 152 | 144 | Set<String> actual = session.execute("SELECT c FROM test_coll WHERE k = 1").one().getSet("c", String.class); |
| @@ -158,7 +150,7 @@ | ||
| 158 | 150 | //given |
| 159 | 151 | session.execute("INSERT INTO test_coll (k, c) VALUES (1, {'foo','bar','qix'})"); |
| 160 | 152 | //when |
| 161 | - BuiltStatement statement = builder.delete().setElt("c", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 153 | + BuiltStatement statement = delete().setElt("c", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 162 | 154 | PreparedStatement ps = session.prepare(statement); |
| 163 | 155 | session.execute(ps.bind("foo")); |
| 164 | 156 | //then |
| @@ -171,7 +163,7 @@ | ||
| 171 | 163 | //given |
| 172 | 164 | session.execute("INSERT INTO test_coll (k, b) VALUES (1, {1:'foo', 2:'bar'})"); |
| 173 | 165 | //when |
| 174 | - BuiltStatement statement = builder.delete().mapElt("b", 1).from("test_coll").where(eq("k", 1)); | |
| 166 | + BuiltStatement statement = delete().mapElt("b", 1).from("test_coll").where(eq("k", 1)); | |
| 175 | 167 | session.execute(statement); |
| 176 | 168 | //then |
| 177 | 169 | Map<Integer, String> actual = session.execute("SELECT b FROM test_coll WHERE k = 1").one().getMap("b", Integer.class, String.class); |
| @@ -183,7 +175,7 @@ | ||
| 183 | 175 | //given |
| 184 | 176 | session.execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); |
| 185 | 177 | //when |
| 186 | - BuiltStatement statement = builder.delete().mapElt("b", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 178 | + BuiltStatement statement = delete().mapElt("b", bindMarker()).from("test_coll").where(eq("k", 1)); | |
| 187 | 179 | PreparedStatement ps = session.prepare(statement); |
| 188 | 180 | session.execute(ps.bind().setInt(0, 1)); |
| 189 | 181 | //then |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderITest.java +37 −44
| @@ -19,7 +19,6 @@ | ||
| 19 | 19 | import java.util.Collection; |
| 20 | 20 | import java.util.TreeSet; |
| 21 | 21 | |
| 22 | -import org.testng.annotations.BeforeMethod; | |
| 23 | 22 | import org.testng.annotations.Test; |
| 24 | 23 | |
| 25 | 24 | import static org.testng.Assert.*; |
| @@ -34,7 +33,6 @@ | ||
| 34 | 33 | |
| 35 | 34 | private static final String TABLE_TEXT = "test_text"; |
| 36 | 35 | private static final String TABLE_INT = "test_int"; |
| 37 | - private QueryBuilder builder; | |
| 38 | 36 | |
| 39 | 37 | @Override |
| 40 | 38 | protected Collection<String> getTableDefinitions() { |
| @@ -51,11 +49,6 @@ | ||
| 51 | 49 | ); |
| 52 | 50 | } |
| 53 | 51 | |
| 54 | - @BeforeMethod(groups = "short") | |
| 55 | - public void setUpQueryBuilder() throws Exception { | |
| 56 | - builder = new QueryBuilder(cluster); | |
| 57 | - } | |
| 58 | - | |
| 59 | 52 | @Test(groups = "short") |
| 60 | 53 | public void remainingDeleteTests() throws Exception { |
| 61 | 54 | |
| @@ -64,7 +57,7 @@ | ||
| 64 | 57 | assertNotNull(table); |
| 65 | 58 | |
| 66 | 59 | String expected = String.format("DELETE k FROM %s.test_text;", keyspace); |
| 67 | - query = builder.delete("k").from(table); | |
| 60 | + query = delete("k").from(table); | |
| 68 | 61 | assertEquals(query.toString(), expected); |
| 69 | 62 | try { |
| 70 | 63 | session.execute(query); |
| @@ -87,52 +80,52 @@ | ||
| 87 | 80 | session.execute("USE foo"); |
| 88 | 81 | |
| 89 | 82 | query = "SELECT * FROM foo WHERE k=?;"; |
| 90 | - select = builder.select().all().from("foo").where(eq("k", QueryBuilder.bindMarker())); | |
| 83 | + select = select().all().from("foo").where(eq("k", QueryBuilder.bindMarker())); | |
| 91 | 84 | ps = session.prepare(select.toString()); |
| 92 | 85 | bs = ps.bind(); |
| 93 | 86 | assertEquals(select.toString(), query); |
| 94 | 87 | session.execute(bs.setString("k", "4 AND c=5")); |
| 95 | 88 | |
| 96 | 89 | query = "SELECT * FROM foo WHERE k='4'' AND c=''5';"; |
| 97 | - select = builder.select().all().from("foo").where(eq("k", "4' AND c='5")); | |
| 90 | + select = select().all().from("foo").where(eq("k", "4' AND c='5")); | |
| 98 | 91 | assertEquals(select.toString(), query); |
| 99 | 92 | |
| 100 | 93 | query = "SELECT * FROM foo WHERE k='4'' OR ''1''=''1';"; |
| 101 | - select = builder.select().all().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 94 | + select = select().all().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 102 | 95 | assertEquals(select.toString(), query); |
| 103 | 96 | |
| 104 | 97 | query = "SELECT * FROM foo WHERE k='4; --test comment;';"; |
| 105 | - select = builder.select().all().from("foo").where(eq("k", "4; --test comment;")); | |
| 98 | + select = select().all().from("foo").where(eq("k", "4; --test comment;")); | |
| 106 | 99 | assertEquals(select.toString(), query); |
| 107 | 100 | |
| 108 | 101 | query = "SELECT \"*\" FROM foo;"; |
| 109 | - select = builder.select("*").from("foo"); | |
| 102 | + select = select("*").from("foo"); | |
| 110 | 103 | assertEquals(select.toString(), query); |
| 111 | 104 | |
| 112 | 105 | query = "SELECT a,b FROM foo WHERE a IN ('b','c''); --comment');"; |
| 113 | - select = builder.select("a", "b").from("foo") | |
| 106 | + select = select("a", "b").from("foo") | |
| 114 | 107 | .where(in("a", "b", "c'); --comment")); |
| 115 | 108 | assertEquals(select.toString(), query); |
| 116 | 109 | |
| 117 | 110 | // User Injection? |
| 118 | 111 | query = "SELECT * FROM bar; --(b) FROM foo;"; |
| 119 | - select = builder.select().fcall("* FROM bar; --", column("b")).from("foo"); | |
| 112 | + select = select().fcall("* FROM bar; --", column("b")).from("foo"); | |
| 120 | 113 | assertEquals(select.toString(), query); |
| 121 | 114 | |
| 122 | 115 | query = "SELECT writetime(\"a) FROM bar; --\"),ttl(a) FROM foo ALLOW FILTERING;"; |
| 123 | - select = builder.select().writeTime("a) FROM bar; --").ttl("a").from("foo").allowFiltering(); | |
| 116 | + select = select().writeTime("a) FROM bar; --").ttl("a").from("foo").allowFiltering(); | |
| 124 | 117 | assertEquals(select.toString(), query); |
| 125 | 118 | |
| 126 | 119 | query = "SELECT writetime(a),ttl(\"a) FROM bar; --\") FROM foo ALLOW FILTERING;"; |
| 127 | - select = builder.select().writeTime("a").ttl("a) FROM bar; --").from("foo").allowFiltering(); | |
| 120 | + select = select().writeTime("a").ttl("a) FROM bar; --").from("foo").allowFiltering(); | |
| 128 | 121 | assertEquals(select.toString(), query); |
| 129 | 122 | |
| 130 | 123 | query = "SELECT * FROM foo WHERE \"k=1 OR k\">42 LIMIT 42;"; |
| 131 | - select = builder.select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); | |
| 124 | + select = select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); | |
| 132 | 125 | assertEquals(select.toString(), query); |
| 133 | 126 | |
| 134 | 127 | query = "SELECT * FROM foo WHERE token(\"k)>0 OR token(k\")>token(42);"; |
| 135 | - select = builder.select().all().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 128 | + select = select().all().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 136 | 129 | assertEquals(select.toString(), query); |
| 137 | 130 | } |
| 138 | 131 | |
| @@ -144,17 +137,17 @@ | ||
| 144 | 137 | Statement insert; |
| 145 | 138 | |
| 146 | 139 | query = "INSERT INTO foo (a) VALUES ('123); --comment');"; |
| 147 | - insert = builder.insertInto("foo") | |
| 140 | + insert = insertInto("foo") | |
| 148 | 141 | .value("a", "123); --comment"); |
| 149 | 142 | assertEquals(insert.toString(), query); |
| 150 | 143 | |
| 151 | 144 | query = "INSERT INTO foo (\"a,b\") VALUES (123);"; |
| 152 | - insert = builder.insertInto("foo") | |
| 145 | + insert = insertInto("foo") | |
| 153 | 146 | .value("a,b", 123); |
| 154 | 147 | assertEquals(insert.toString(), query); |
| 155 | 148 | |
| 156 | 149 | query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; |
| 157 | - insert = builder.insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<String>(){{ add("2'} space"); add("3"); add("4"); }}, 3.4 }).using(ttl(24)).and(timestamp(42)); | |
| 150 | + insert = insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ new TreeSet<String>(){{ add("2'} space"); add("3"); add("4"); }}, 3.4 }).using(ttl(24)).and(timestamp(42)); | |
| 158 | 151 | assertEquals(insert.toString(), query); |
| 159 | 152 | } |
| 160 | 153 | |
| @@ -165,15 +158,15 @@ | ||
| 165 | 158 | Statement update; |
| 166 | 159 | |
| 167 | 160 | query = "UPDATE foo.bar USING TIMESTAMP 42 SET a=12 WHERE k='2 OR 1=1';"; |
| 168 | - update = builder.update("foo", "bar").using(timestamp(42)).with(set("a", 12)).where(eq("k", "2 OR 1=1")); | |
| 161 | + update = update("foo", "bar").using(timestamp(42)).with(set("a", 12)).where(eq("k", "2 OR 1=1")); | |
| 169 | 162 | assertEquals(update.toString(), query); |
| 170 | 163 | |
| 171 | 164 | query = "UPDATE foo SET b='null WHERE k=1; --comment' WHERE k=2;"; |
| 172 | - update = builder.update("foo").where().and(eq("k", 2)).with(set("b", "null WHERE k=1; --comment")); | |
| 165 | + update = update("foo").where().and(eq("k", 2)).with(set("b", "null WHERE k=1; --comment")); | |
| 173 | 166 | assertEquals(update.toString(), query); |
| 174 | 167 | |
| 175 | 168 | query = "UPDATE foo USING TIMESTAMP 42 SET \"b WHERE k=1; --comment\"=[3,2,1]+\"b WHERE k=1; --comment\" WHERE k=2;"; |
| 176 | - update = builder.update("foo").where().and(eq("k", 2)).with(prependAll("b WHERE k=1; --comment", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 169 | + update = update("foo").where().and(eq("k", 2)).with(prependAll("b WHERE k=1; --comment", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 177 | 170 | assertEquals(update.toString(), query); |
| 178 | 171 | } |
| 179 | 172 | |
| @@ -184,40 +177,40 @@ | ||
| 184 | 177 | Statement delete; |
| 185 | 178 | |
| 186 | 179 | query = "DELETE FROM \"foo WHERE k=4\";"; |
| 187 | - delete = builder.delete().from("foo WHERE k=4"); | |
| 180 | + delete = delete().from("foo WHERE k=4"); | |
| 188 | 181 | assertEquals(delete.toString(), query); |
| 189 | 182 | |
| 190 | 183 | query = "DELETE FROM foo WHERE k='4 AND c=5';"; |
| 191 | - delete = builder.delete().from("foo").where(eq("k", "4 AND c=5")); | |
| 184 | + delete = delete().from("foo").where(eq("k", "4 AND c=5")); | |
| 192 | 185 | assertEquals(delete.toString(), query); |
| 193 | 186 | |
| 194 | 187 | query = "DELETE FROM foo WHERE k='4'' AND c=''5';"; |
| 195 | - delete = builder.delete().from("foo").where(eq("k", "4' AND c='5")); | |
| 188 | + delete = delete().from("foo").where(eq("k", "4' AND c='5")); | |
| 196 | 189 | assertEquals(delete.toString(), query); |
| 197 | 190 | |
| 198 | 191 | query = "DELETE FROM foo WHERE k='4'' OR ''1''=''1';"; |
| 199 | - delete = builder.delete().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 192 | + delete = delete().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 200 | 193 | assertEquals(delete.toString(), query); |
| 201 | 194 | |
| 202 | 195 | query = "DELETE FROM foo WHERE k='4; --test comment;';"; |
| 203 | - delete = builder.delete().from("foo").where(eq("k", "4; --test comment;")); | |
| 196 | + delete = delete().from("foo").where(eq("k", "4; --test comment;")); | |
| 204 | 197 | assertEquals(delete.toString(), query); |
| 205 | 198 | |
| 206 | 199 | query = "DELETE \"*\" FROM foo;"; |
| 207 | - delete = builder.delete("*").from("foo"); | |
| 200 | + delete = delete("*").from("foo"); | |
| 208 | 201 | assertEquals(delete.toString(), query); |
| 209 | 202 | |
| 210 | 203 | query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');"; |
| 211 | - delete = builder.delete("a", "b").from("foo") | |
| 204 | + delete = delete("a", "b").from("foo") | |
| 212 | 205 | .where(in("a", "b", "c'); --comment")); |
| 213 | 206 | assertEquals(delete.toString(), query); |
| 214 | 207 | |
| 215 | 208 | query = "DELETE FROM foo WHERE \"k=1 OR k\">42;"; |
| 216 | - delete = builder.delete().from("foo").where(gt("k=1 OR k", 42)); | |
| 209 | + delete = delete().from("foo").where(gt("k=1 OR k", 42)); | |
| 217 | 210 | assertEquals(delete.toString(), query); |
| 218 | 211 | |
| 219 | 212 | query = "DELETE FROM foo WHERE token(\"k)>0 OR token(k\")>token(42);"; |
| 220 | - delete = builder.delete().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 213 | + delete = delete().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 221 | 214 | assertEquals(delete.toString(), query); |
| 222 | 215 | } |
| 223 | 216 | |
| @@ -228,21 +221,21 @@ | ||
| 228 | 221 | |
| 229 | 222 | Statement delete; |
| 230 | 223 | Row row; |
| 231 | - delete = builder.delete().from(keyspace, TABLE_INT).where(eq("k", 2)).ifExists(); | |
| 224 | + delete = delete().from(keyspace, TABLE_INT).where(eq("k", 2)).ifExists(); | |
| 232 | 225 | row = session.execute(delete).one(); |
| 233 | 226 | assertFalse(row.getBool("[applied]")); |
| 234 | 227 | |
| 235 | - delete = builder.delete().from(keyspace, TABLE_INT).where(eq("k", 1)).ifExists(); | |
| 228 | + delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).ifExists(); | |
| 236 | 229 | row = session.execute(delete).one(); |
| 237 | 230 | assertTrue(row.getBool("[applied]")); |
| 238 | 231 | |
| 239 | 232 | session.execute(String.format("INSERT INTO %s.test_int (k, a, b) VALUES (1, 1, 1)", keyspace)); |
| 240 | 233 | |
| 241 | - delete = builder.delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 2)); | |
| 234 | + delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 2)); | |
| 242 | 235 | row = session.execute(delete).one(); |
| 243 | 236 | assertFalse(row.getBool("[applied]")); |
| 244 | 237 | |
| 245 | - delete = builder.delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 1)); | |
| 238 | + delete = delete().from(keyspace, TABLE_INT).where(eq("k", 1)).onlyIf(eq("a", 1)).and(eq("b", 1)); | |
| 246 | 239 | row = session.execute(delete).one(); |
| 247 | 240 | assertTrue(row.getBool("[applied]")); |
| 248 | 241 | } |
| @@ -254,27 +247,27 @@ | ||
| 254 | 247 | |
| 255 | 248 | Statement update; |
| 256 | 249 | Row row; |
| 257 | - update = builder.update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).ifExists(); | |
| 250 | + update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).ifExists(); | |
| 258 | 251 | row = session.execute(update).one(); |
| 259 | 252 | assertFalse(row.getBool("[applied]")); |
| 260 | 253 | |
| 261 | - update = builder.update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 1)).ifExists(); | |
| 254 | + update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 1)).ifExists(); | |
| 262 | 255 | row = session.execute(update).one(); |
| 263 | 256 | assertTrue(row.getBool("[applied]")); |
| 264 | 257 | |
| 265 | - update = builder.update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).onlyIf(eq("a", 1)).and(eq("b", 2)); | |
| 258 | + update = update(TABLE_INT).with(set("a", 2)).and(set("b", 2)).where(eq("k", 2)).onlyIf(eq("a", 1)).and(eq("b", 2)); | |
| 266 | 259 | row = session.execute(update).one(); |
| 267 | 260 | assertFalse(row.getBool("[applied]")); |
| 268 | 261 | |
| 269 | - update = builder.update(TABLE_INT).with(set("a", 3)).and(set("b", 3)).where(eq("k", 1)).onlyIf(eq("a", 2)).and(eq("b", 2)); | |
| 262 | + update = update(TABLE_INT).with(set("a", 3)).and(set("b", 3)).where(eq("k", 1)).onlyIf(eq("a", 2)).and(eq("b", 2)); | |
| 270 | 263 | row = session.execute(update).one(); |
| 271 | 264 | assertTrue(row.getBool("[applied]")); |
| 272 | 265 | |
| 273 | - update = builder.update(TABLE_INT).with(set("a", 4)).and(set("b", 4)).onlyIf(eq("a", 2)).and(eq("b", 2)).where(eq("k", 1)); | |
| 266 | + update = update(TABLE_INT).with(set("a", 4)).and(set("b", 4)).onlyIf(eq("a", 2)).and(eq("b", 2)).where(eq("k", 1)); | |
| 274 | 267 | row = session.execute(update).one(); |
| 275 | 268 | assertFalse(row.getBool("[applied]")); |
| 276 | 269 | |
| 277 | - update = builder.update(TABLE_INT).with(set("a", 4)).and(set("b", 4)).onlyIf(eq("a", 3)).and(eq("b", 3)).where(eq("k", 1)); | |
| 270 | + update = update(TABLE_INT).with(set("a", 4)).and(set("b", 4)).onlyIf(eq("a", 3)).and(eq("b", 3)).where(eq("k", 1)); | |
| 278 | 271 | row = session.execute(update).one(); |
| 279 | 272 | assertTrue(row.getBool("[applied]")); |
| 280 | 273 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderRoutingKeyTest.java +25 −21
| @@ -54,15 +54,16 @@ | ||
| 54 | 54 | BuiltStatement query; |
| 55 | 55 | TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); |
| 56 | 56 | assertNotNull(table); |
| 57 | + ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 58 | + CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
| 57 | 59 | |
| 58 | 60 | String txt = "If she weighs the same as a duck... she's made of wood."; |
| 59 | - QueryBuilder builder = new QueryBuilder(cluster); | |
| 60 | - query = builder.insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{txt, 1, 2}); | |
| 61 | - assertEquals(query.getRoutingKey(), ByteBuffer.wrap(txt.getBytes())); | |
| 61 | + query = insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{txt, 1, 2}); | |
| 62 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes())); | |
| 62 | 63 | session.execute(query); |
| 63 | 64 | |
| 64 | - query = builder.select().from(table).where(eq("k", txt)); | |
| 65 | - assertEquals(query.getRoutingKey(), ByteBuffer.wrap(txt.getBytes())); | |
| 65 | + query = select().from(table).where(eq("k", txt)); | |
| 66 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes())); | |
| 66 | 67 | Row row = session.execute(query).one(); |
| 67 | 68 | assertEquals(row.getString("k"), txt); |
| 68 | 69 | assertEquals(row.getInt("a"), 1); |
| @@ -75,15 +76,17 @@ | ||
| 75 | 76 | BuiltStatement query; |
| 76 | 77 | TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); |
| 77 | 78 | assertNotNull(table); |
| 79 | + ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 80 | + CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
| 78 | 81 | |
| 79 | - query = new QueryBuilder(cluster).insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{42, 1, 2}); | |
| 82 | + query = insertInto(table).values(new String[]{"k", "a", "b"}, new Object[]{42, 1, 2}); | |
| 80 | 83 | ByteBuffer bb = ByteBuffer.allocate(4); |
| 81 | 84 | bb.putInt(0, 42); |
| 82 | - assertEquals(query.getRoutingKey(), bb); | |
| 85 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 83 | 86 | session.execute(query); |
| 84 | 87 | |
| 85 | - query = new QueryBuilder(cluster).select().from(table).where(eq("k", 42)); | |
| 86 | - assertEquals(query.getRoutingKey(), bb); | |
| 88 | + query = select().from(table).where(eq("k", 42)); | |
| 89 | + assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 87 | 90 | Row row = session.execute(query).one(); |
| 88 | 91 | assertEquals(row.getInt("k"), 42); |
| 89 | 92 | assertEquals(row.getInt("a"), 1); |
| @@ -96,6 +99,8 @@ | ||
| 96 | 99 | BuiltStatement query; |
| 97 | 100 | TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); |
| 98 | 101 | assertNotNull(table); |
| 102 | + ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 103 | + CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; | |
| 99 | 104 | |
| 100 | 105 | ByteBuffer bb = ByteBuffer.allocate(4); |
| 101 | 106 | bb.putInt(0, 42); |
| @@ -103,41 +108,40 @@ | ||
| 103 | 108 | String batch_query; |
| 104 | 109 | BuiltStatement batch; |
| 105 | 110 | |
| 106 | - QueryBuilder builder = new QueryBuilder(cluster); | |
| 107 | - query = builder.select().from(table).where(eq("k", 42)); | |
| 111 | + query = select().from(table).where(eq("k", 42)); | |
| 108 | 112 | |
| 109 | 113 | batch_query = "BEGIN BATCH "; |
| 110 | 114 | batch_query += String.format("INSERT INTO %s.test_int (k,a) VALUES (42,1);", keyspace); |
| 111 | 115 | batch_query += String.format("UPDATE %s.test_int USING TTL 400;", keyspace); |
| 112 | 116 | batch_query += "APPLY BATCH;"; |
| 113 | - batch = builder.batch() | |
| 114 | - .add(builder.insertInto(table).values(new String[]{"k", "a"}, new Object[]{42, 1})) | |
| 115 | - .add(builder.update(table).using(ttl(400))); | |
| 116 | - assertEquals(batch.getRoutingKey(), bb); | |
| 117 | + batch = batch() | |
| 118 | + .add(insertInto(table).values(new String[]{ "k", "a" }, new Object[]{ 42, 1 })) | |
| 119 | + .add(update(table).using(ttl(400))); | |
| 120 | + assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 117 | 121 | assertEquals(batch.toString(), batch_query); |
| 118 | 122 | // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL |
| 119 | 123 | |
| 120 | 124 | batch_query = "BEGIN BATCH "; |
| 121 | 125 | batch_query += String.format("SELECT * FROM %s.test_int WHERE k=42;", keyspace); |
| 122 | 126 | batch_query += "APPLY BATCH;"; |
| 123 | - batch = builder.batch(query); | |
| 124 | - assertEquals(batch.getRoutingKey(), bb); | |
| 127 | + batch = batch(query); | |
| 128 | + assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb); | |
| 125 | 129 | assertEquals(batch.toString(), batch_query); |
| 126 | 130 | // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL |
| 127 | 131 | |
| 128 | 132 | batch_query = "BEGIN BATCH "; |
| 129 | 133 | batch_query += "SELECT * FROM foo WHERE k=42;"; |
| 130 | 134 | batch_query += "APPLY BATCH;"; |
| 131 | - batch = builder.batch().add(builder.select().from("foo").where(eq("k", 42))); | |
| 132 | - assertEquals(batch.getRoutingKey(), null); | |
| 135 | + batch = batch().add(select().from("foo").where(eq("k", 42))); | |
| 136 | + assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null); | |
| 133 | 137 | assertEquals(batch.toString(), batch_query); |
| 134 | 138 | // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL |
| 135 | 139 | |
| 136 | 140 | batch_query = "BEGIN BATCH USING TIMESTAMP 42 "; |
| 137 | 141 | batch_query += "INSERT INTO foo.bar (a) VALUES (123);"; |
| 138 | 142 | batch_query += "APPLY BATCH;"; |
| 139 | - batch = builder.batch().using(timestamp(42)).add(builder.insertInto("foo", "bar").value("a", 123)); | |
| 140 | - assertEquals(batch.getRoutingKey(), null); | |
| 143 | + batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123)); | |
| 144 | + assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null); | |
| 141 | 145 | assertEquals(batch.toString(), batch_query); |
| 142 | 146 | // TODO: rs = session.execute(batch); // Not guaranteed to be valid CQL |
| 143 | 147 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +172 −173
| @@ -41,8 +41,6 @@ | ||
| 41 | 41 | |
| 42 | 42 | public class QueryBuilderTest { |
| 43 | 43 | |
| 44 | - private QueryBuilder builder = new QueryBuilder(TestUtils.getDesiredProtocolVersion(), CodecRegistry.DEFAULT_INSTANCE); | |
| 45 | - | |
| 46 | 44 | @Test(groups = "unit") |
| 47 | 45 | public void selectTest() throws Exception { |
| 48 | 46 | |
| @@ -50,15 +48,15 @@ | ||
| 50 | 48 | Statement select; |
| 51 | 49 | |
| 52 | 50 | query = "SELECT * FROM foo WHERE k=4 AND c>'a' AND c<='z';"; |
| 53 | - select = builder.select().all().from("foo").where(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); | |
| 51 | + select = select().all().from("foo").where(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); | |
| 54 | 52 | assertEquals(select.toString(), query); |
| 55 | 53 | |
| 56 | 54 | // Ensure where() and where(...) are equal |
| 57 | - select = builder.select().all().from("foo").where().and(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); | |
| 55 | + select = select().all().from("foo").where().and(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); | |
| 58 | 56 | assertEquals(select.toString(), query); |
| 59 | 57 | |
| 60 | 58 | query = "SELECT a,b,\"C\" FROM foo WHERE a IN ('127.0.0.1','127.0.0.3') AND \"C\"='foo' ORDER BY a ASC,b DESC LIMIT 42;"; |
| 61 | - select = builder.select("a", "b", quote("C")).from("foo") | |
| 59 | + select = select("a", "b", quote("C")).from("foo") | |
| 62 | 60 | .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3"))) |
| 63 | 61 | .and(eq(quote("C"), "foo")) |
| 64 | 62 | .orderBy(asc("a"), desc("b")) |
| @@ -66,113 +64,113 @@ | ||
| 66 | 64 | assertEquals(select.toString(), query); |
| 67 | 65 | |
| 68 | 66 | query = "SELECT writetime(a),ttl(a) FROM foo ALLOW FILTERING;"; |
| 69 | - select = builder.select().writeTime("a").ttl("a").from("foo").allowFiltering(); | |
| 67 | + select = select().writeTime("a").ttl("a").from("foo").allowFiltering(); | |
| 70 | 68 | assertEquals(select.toString(), query); |
| 71 | 69 | |
| 72 | 70 | query = "SELECT DISTINCT longName AS a,ttl(longName) AS ttla FROM foo LIMIT :limit;"; |
| 73 | - select = builder.select().distinct().column("longName").as("a").ttl("longName").as("ttla").from("foo").limit(bindMarker("limit")); | |
| 71 | + select = select().distinct().column("longName").as("a").ttl("longName").as("ttla").from("foo").limit(bindMarker("limit")); | |
| 74 | 72 | assertEquals(select.toString(), query); |
| 75 | 73 | |
| 76 | 74 | query = "SELECT DISTINCT longName AS a,ttl(longName) AS ttla FROM foo WHERE k IN () LIMIT :limit;"; |
| 77 | - select = builder.select().distinct().column("longName").as("a").ttl("longName").as("ttla").from("foo").where(in("k")).limit(bindMarker("limit")); | |
| 75 | + select = select().distinct().column("longName").as("a").ttl("longName").as("ttla").from("foo").where(in("k")).limit(bindMarker("limit")); | |
| 78 | 76 | assertEquals(select.toString(), query); |
| 79 | 77 | |
| 80 | 78 | query = "SELECT * FROM foo WHERE bar=:barmark AND baz=:bazmark LIMIT :limit;"; |
| 81 | - select = builder.select().all().from("foo").where().and(eq("bar", bindMarker("barmark"))).and(eq("baz", bindMarker("bazmark"))).limit(bindMarker("limit")); | |
| 79 | + select = select().all().from("foo").where().and(eq("bar", bindMarker("barmark"))).and(eq("baz", bindMarker("bazmark"))).limit(bindMarker("limit")); | |
| 82 | 80 | assertEquals(select.toString(), query); |
| 83 | 81 | |
| 84 | 82 | query = "SELECT a FROM foo WHERE k IN ();"; |
| 85 | - select = builder.select("a").from("foo").where(in("k")); | |
| 83 | + select = select("a").from("foo").where(in("k")); | |
| 86 | 84 | assertEquals(select.toString(), query); |
| 87 | 85 | |
| 88 | 86 | query = "SELECT a FROM foo WHERE k IN ?;"; |
| 89 | - select = builder.select("a").from("foo").where(in("k", bindMarker())); | |
| 87 | + select = select("a").from("foo").where(in("k", bindMarker())); | |
| 90 | 88 | assertEquals(select.toString(), query); |
| 91 | 89 | |
| 92 | 90 | query = "SELECT DISTINCT a FROM foo WHERE k=1;"; |
| 93 | - select = builder.select("a").distinct().from("foo").where(eq("k", 1)); | |
| 91 | + select = select("a").distinct().from("foo").where(eq("k", 1)); | |
| 94 | 92 | assertEquals(select.toString(), query); |
| 95 | 93 | |
| 96 | 94 | query = "SELECT DISTINCT a,b FROM foo WHERE k=1;"; |
| 97 | - select = builder.select("a", "b").distinct().from("foo").where(eq("k", 1)); | |
| 95 | + select = select("a", "b").distinct().from("foo").where(eq("k", 1)); | |
| 98 | 96 | assertEquals(select.toString(), query); |
| 99 | 97 | |
| 100 | 98 | query = "SELECT count(*) FROM foo;"; |
| 101 | - select = builder.select().countAll().from("foo"); | |
| 99 | + select = select().countAll().from("foo"); | |
| 102 | 100 | assertEquals(select.toString(), query); |
| 103 | 101 | |
| 104 | 102 | query = "SELECT intToBlob(b) FROM foo;"; |
| 105 | - select = builder.select().fcall("intToBlob", column("b")).from("foo"); | |
| 103 | + select = select().fcall("intToBlob", column("b")).from("foo"); | |
| 106 | 104 | assertEquals(select.toString(), query); |
| 107 | 105 | |
| 108 | 106 | query = "SELECT * FROM foo WHERE k>42 LIMIT 42;"; |
| 109 | - select = builder.select().all().from("foo").where(gt("k", 42)).limit(42); | |
| 107 | + select = select().all().from("foo").where(gt("k", 42)).limit(42); | |
| 110 | 108 | assertEquals(select.toString(), query); |
| 111 | 109 | |
| 112 | 110 | query = "SELECT * FROM foo WHERE token(k)>token(42);"; |
| 113 | - select = builder.select().all().from("foo").where(gt(token("k"), fcall("token", 42))); | |
| 111 | + select = select().all().from("foo").where(gt(token("k"), fcall("token", 42))); | |
| 114 | 112 | assertEquals(select.toString(), query); |
| 115 | 113 | |
| 116 | 114 | query = "SELECT * FROM foo2 WHERE token(a,b)>token(42,101);"; |
| 117 | - select = builder.select().all().from("foo2").where(gt(token("a", "b"), fcall("token", 42, 101))); | |
| 115 | + select = select().all().from("foo2").where(gt(token("a", "b"), fcall("token", 42, 101))); | |
| 118 | 116 | assertEquals(select.toString(), query); |
| 119 | 117 | |
| 120 | 118 | query = "SELECT * FROM words WHERE w='):,ydL ;O,D';"; |
| 121 | - select = builder.select().all().from("words").where(eq("w", "):,ydL ;O,D")); | |
| 119 | + select = select().all().from("words").where(eq("w", "):,ydL ;O,D")); | |
| 122 | 120 | assertEquals(select.toString(), query); |
| 123 | 121 | |
| 124 | 122 | query = "SELECT * FROM words WHERE w='WA(!:gS)r(UfW';"; |
| 125 | - select = builder.select().all().from("words").where(eq("w", "WA(!:gS)r(UfW")); | |
| 123 | + select = select().all().from("words").where(eq("w", "WA(!:gS)r(UfW")); | |
| 126 | 124 | assertEquals(select.toString(), query); |
| 127 | 125 | |
| 128 | 126 | Date date = new Date(); |
| 129 | 127 | date.setTime(1234325); |
| 130 | 128 | query = "SELECT * FROM foo WHERE d=1234325;"; |
| 131 | - select = builder.select().all().from("foo").where(eq("d", date)); | |
| 129 | + select = select().all().from("foo").where(eq("d", date)); | |
| 132 | 130 | assertEquals(select.toString(), query); |
| 133 | 131 | |
| 134 | 132 | query = "SELECT * FROM foo WHERE b=0xcafebabe;"; |
| 135 | - select = builder.select().all().from("foo").where(eq("b", Bytes.fromHexString("0xCAFEBABE"))); | |
| 133 | + select = select().all().from("foo").where(eq("b", Bytes.fromHexString("0xCAFEBABE"))); | |
| 136 | 134 | assertEquals(select.toString(), query); |
| 137 | 135 | |
| 138 | 136 | query = "SELECT * FROM foo WHERE e CONTAINS 'text';"; |
| 139 | - select = builder.select().from("foo").where(contains("e", "text")); | |
| 137 | + select = select().from("foo").where(contains("e", "text")); | |
| 140 | 138 | assertEquals(select.toString(), query); |
| 141 | 139 | |
| 142 | 140 | query = "SELECT * FROM foo WHERE e CONTAINS KEY 'key1';"; |
| 143 | - select = builder.select().from("foo").where(containsKey("e", "key1")); | |
| 141 | + select = select().from("foo").where(containsKey("e", "key1")); | |
| 144 | 142 | assertEquals(select.toString(), query); |
| 145 | 143 | |
| 146 | 144 | try { |
| 147 | - builder.select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); | |
| 145 | + select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); | |
| 148 | 146 | fail(); |
| 149 | 147 | } catch (IllegalStateException e) { |
| 150 | 148 | assertEquals(e.getMessage(), "An ORDER BY clause has already been provided"); |
| 151 | 149 | } |
| 152 | 150 | |
| 153 | 151 | try { |
| 154 | - builder.select().column("a").all().from("foo"); | |
| 152 | + select().column("a").all().from("foo"); | |
| 155 | 153 | fail(); |
| 156 | 154 | } catch (IllegalStateException e) { |
| 157 | 155 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 158 | 156 | } |
| 159 | 157 | |
| 160 | 158 | try { |
| 161 | - builder.select().column("a").countAll().from("foo"); | |
| 159 | + select().column("a").countAll().from("foo"); | |
| 162 | 160 | fail(); |
| 163 | 161 | } catch (IllegalStateException e) { |
| 164 | 162 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 165 | 163 | } |
| 166 | 164 | |
| 167 | 165 | try { |
| 168 | - builder.select().all().from("foo").limit(-42); | |
| 166 | + select().all().from("foo").limit(-42); | |
| 169 | 167 | fail(); |
| 170 | 168 | } catch (IllegalArgumentException e) { |
| 171 | 169 | assertEquals(e.getMessage(), "Invalid LIMIT value, must be strictly positive"); |
| 172 | 170 | } |
| 173 | 171 | |
| 174 | 172 | try { |
| 175 | - builder.select().all().from("foo").limit(42).limit(42); | |
| 173 | + select().all().from("foo").limit(42).limit(42); | |
| 176 | 174 | fail(); |
| 177 | 175 | } catch (IllegalStateException e) { |
| 178 | 176 | assertEquals(e.getMessage(), "A LIMIT value has already been provided"); |
| @@ -187,7 +185,7 @@ | ||
| 187 | 185 | Statement insert; |
| 188 | 186 | |
| 189 | 187 | query = "INSERT INTO foo (a,b,\"C\",d) VALUES (123,'127.0.0.1','foo''bar',{'x':3,'y':2}) USING TIMESTAMP 42 AND TTL 24;"; |
| 190 | - insert = builder.insertInto("foo") | |
| 188 | + insert = insertInto("foo") | |
| 191 | 189 | .value("a", 123) |
| 192 | 190 | .value("b", InetAddress.getByName("127.0.0.1")) |
| 193 | 191 | .value(quote("C"), "foo'bar") |
| @@ -199,13 +197,13 @@ | ||
| 199 | 197 | assertEquals(insert.toString(), query); |
| 200 | 198 | |
| 201 | 199 | query = "INSERT INTO foo (a,b) VALUES (2,null);"; |
| 202 | - insert = builder.insertInto("foo") | |
| 200 | + insert = insertInto("foo") | |
| 203 | 201 | .value("a", 2) |
| 204 | 202 | .value("b", null); |
| 205 | 203 | assertEquals(insert.toString(), query); |
| 206 | 204 | |
| 207 | 205 | query = "INSERT INTO foo (a,b) VALUES ({2,3,4},3.4) USING TTL 24 AND TIMESTAMP 42;"; |
| 208 | - insert = builder.insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ | |
| 206 | + insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ | |
| 209 | 207 | add(2); |
| 210 | 208 | add(3); |
| 211 | 209 | add(4); |
| @@ -213,7 +211,7 @@ | ||
| 213 | 211 | assertEquals(insert.toString(), query); |
| 214 | 212 | |
| 215 | 213 | query = "INSERT INTO foo.bar (a,b) VALUES ({2,3,4},3.4) USING TTL ? AND TIMESTAMP ?;"; |
| 216 | - insert = builder.insertInto("foo", "bar") | |
| 214 | + insert = insertInto("foo", "bar") | |
| 217 | 215 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| 218 | 216 | add(2); |
| 219 | 217 | add(3); |
| @@ -225,7 +223,7 @@ | ||
| 225 | 223 | |
| 226 | 224 | // commutative result of TIMESTAMP |
| 227 | 225 | query = "INSERT INTO foo.bar (a,b,c) VALUES ({2,3,4},3.4,123) USING TIMESTAMP 42;"; |
| 228 | - insert = builder.insertInto("foo", "bar") | |
| 226 | + insert = insertInto("foo", "bar") | |
| 229 | 227 | .using(timestamp(42)) |
| 230 | 228 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| 231 | 229 | add(2); |
| @@ -237,7 +235,7 @@ | ||
| 237 | 235 | |
| 238 | 236 | // commutative result of value() and values() |
| 239 | 237 | query = "INSERT INTO foo (c,a,b) VALUES (123,{2,3,4},3.4) USING TIMESTAMP 42;"; |
| 240 | - insert = builder.insertInto("foo") | |
| 238 | + insert = insertInto("foo") | |
| 241 | 239 | .using(timestamp(42)) |
| 242 | 240 | .value("c", 123) |
| 243 | 241 | .values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ |
| @@ -248,7 +246,7 @@ | ||
| 248 | 246 | assertEquals(insert.toString(), query); |
| 249 | 247 | |
| 250 | 248 | try { |
| 251 | - builder.insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ 1, 2, 3 }); | |
| 249 | + insertInto("foo").values(new String[]{ "a", "b"}, new Object[]{ 1, 2, 3 }); | |
| 252 | 250 | fail(); |
| 253 | 251 | } catch (IllegalArgumentException e) { |
| 254 | 252 | assertEquals(e.getMessage(), "Got 2 names but 3 values"); |
| @@ -256,7 +254,7 @@ | ||
| 256 | 254 | |
| 257 | 255 | // CAS test |
| 258 | 256 | query = "INSERT INTO foo (k,x) VALUES (0,1) IF NOT EXISTS;"; |
| 259 | - insert = builder.insertInto("foo").value("k", 0).value("x", 1).ifNotExists(); | |
| 257 | + insert = insertInto("foo").value("k", 0).value("x", 1).ifNotExists(); | |
| 260 | 258 | assertEquals(insert.toString(), query); |
| 261 | 259 | |
| 262 | 260 | // Tuples: see QueryBuilderTupleExecutionTest |
| @@ -271,23 +269,23 @@ | ||
| 271 | 269 | Statement update; |
| 272 | 270 | |
| 273 | 271 | query = "UPDATE foo.bar USING TIMESTAMP 42 SET a=12,b=[3,2,1],c=c+3 WHERE k=2;"; |
| 274 | - update = builder.update("foo", "bar").using(timestamp(42)).with(set("a", 12)).and(set("b", Arrays.asList(3, 2, 1))).and(incr("c", 3)).where(eq("k", 2)); | |
| 272 | + update = update("foo", "bar").using(timestamp(42)).with(set("a", 12)).and(set("b", Arrays.asList(3, 2, 1))).and(incr("c", 3)).where(eq("k", 2)); | |
| 275 | 273 | assertEquals(update.toString(), query); |
| 276 | 274 | |
| 277 | 275 | query = "UPDATE foo SET b=null WHERE k=2;"; |
| 278 | - update = builder.update("foo").where().and(eq("k", 2)).with(set("b", null)); | |
| 276 | + update = update("foo").where().and(eq("k", 2)).with(set("b", null)); | |
| 279 | 277 | assertEquals(update.toString(), query); |
| 280 | 278 | |
| 281 | 279 | query = "UPDATE foo SET a[2]='foo',b=[3,2,1]+b,c=c-{'a'} WHERE k=2 AND l='foo' AND m<4 AND n>=1;"; |
| 282 | - update = builder.update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2)).and(eq("l", "foo")).and(lt("m", 4)).and(gte("n", 1)); | |
| 280 | + update = update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2)).and(eq("l", "foo")).and(lt("m", 4)).and(gte("n", 1)); | |
| 283 | 281 | assertEquals(update.toString(), query); |
| 284 | 282 | |
| 285 | 283 | query = "UPDATE foo SET b=[3]+b,c=c+['a'],d=d+[1,2,3],e=e-[1];"; |
| 286 | - update = builder.update("foo").with().and(prepend("b", 3)).and(append("c", "a")).and(appendAll("d", Arrays.asList(1, 2, 3))).and(discard("e", 1)); | |
| 284 | + update = update("foo").with().and(prepend("b", 3)).and(append("c", "a")).and(appendAll("d", Arrays.asList(1, 2, 3))).and(discard("e", 1)); | |
| 287 | 285 | assertEquals(update.toString(), query); |
| 288 | 286 | |
| 289 | 287 | query = "UPDATE foo SET b=b-[1,2,3],c=c+{1},d=d+{2,3,4};"; |
| 290 | - update = builder.update("foo").with(discardAll("b", Arrays.asList(1, 2, 3))).and(add("c", 1)).and(addAll("d", new TreeSet<Integer>() {{ | |
| 288 | + update = update("foo").with(discardAll("b", Arrays.asList(1, 2, 3))).and(add("c", 1)).and(addAll("d", new TreeSet<Integer>() {{ | |
| 291 | 289 | add(2); |
| 292 | 290 | add(3); |
| 293 | 291 | add(4); |
| @@ -295,7 +293,7 @@ | ||
| 295 | 293 | assertEquals(update.toString(), query); |
| 296 | 294 | |
| 297 | 295 | query = "UPDATE foo SET b=b-{2,3,4},c['k']='v',d=d+{'x':3,'y':2};"; |
| 298 | - update = builder.update("foo").with(removeAll("b", new TreeSet<Integer>() {{ | |
| 296 | + update = update("foo").with(removeAll("b", new TreeSet<Integer>() {{ | |
| 299 | 297 | add(2); |
| 300 | 298 | add(3); |
| 301 | 299 | add(4); |
| @@ -308,27 +306,27 @@ | ||
| 308 | 306 | assertEquals(update.toString(), query); |
| 309 | 307 | |
| 310 | 308 | query = "UPDATE foo USING TTL 400;"; |
| 311 | - update = builder.update("foo").using(ttl(400)); | |
| 309 | + update = update("foo").using(ttl(400)); | |
| 312 | 310 | assertEquals(update.toString(), query); |
| 313 | 311 | |
| 314 | 312 | query = "UPDATE foo SET a=" + new BigDecimal(3.2) + ",b=42 WHERE k=2;"; |
| 315 | - update = builder.update("foo").with(set("a", new BigDecimal(3.2))).and(set("b", new BigInteger("42"))).where(eq("k", 2)); | |
| 313 | + update = update("foo").with(set("a", new BigDecimal(3.2))).and(set("b", new BigInteger("42"))).where(eq("k", 2)); | |
| 316 | 314 | assertEquals(update.toString(), query); |
| 317 | 315 | |
| 318 | 316 | query = "UPDATE foo USING TIMESTAMP 42 SET b=[3,2,1]+b WHERE k=2 AND l='foo';"; |
| 319 | - update = builder.update("foo").where().and(eq("k", 2)).and(eq("l", "foo")).with(prependAll("b", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 317 | + update = update("foo").where().and(eq("k", 2)).and(eq("l", "foo")).with(prependAll("b", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 320 | 318 | assertEquals(update.toString(), query); |
| 321 | 319 | |
| 322 | 320 | // Test commutative USING |
| 323 | - update = builder.update("foo").where().and(eq("k", 2)).and(eq("l", "foo")).using(timestamp(42)).with(prependAll("b", Arrays.asList(3, 2, 1))); | |
| 321 | + update = update("foo").where().and(eq("k", 2)).and(eq("l", "foo")).using(timestamp(42)).with(prependAll("b", Arrays.asList(3, 2, 1))); | |
| 324 | 322 | assertEquals(update.toString(), query); |
| 325 | 323 | |
| 326 | 324 | // Test commutative USING |
| 327 | - update = builder.update("foo").using(timestamp(42)).where(eq("k", 2)).and(eq("l", "foo")).with(prependAll("b", Arrays.asList(3, 2, 1))); | |
| 325 | + update = update("foo").using(timestamp(42)).where(eq("k", 2)).and(eq("l", "foo")).with(prependAll("b", Arrays.asList(3, 2, 1))); | |
| 328 | 326 | assertEquals(update.toString(), query); |
| 329 | 327 | |
| 330 | 328 | try { |
| 331 | - builder.update("foo").using(ttl(-400)); | |
| 329 | + update("foo").using(ttl(-400)); | |
| 332 | 330 | fail(); |
| 333 | 331 | } catch (IllegalArgumentException e) { |
| 334 | 332 | assertEquals(e.getMessage(), "Invalid ttl, must be positive"); |
| @@ -336,11 +334,11 @@ | ||
| 336 | 334 | |
| 337 | 335 | // CAS test |
| 338 | 336 | query = "UPDATE foo SET x=4 WHERE k=0 IF x=1;"; |
| 339 | - update = builder.update("foo").with(set("x", 4)).where(eq("k", 0)).onlyIf(eq("x", 1)); | |
| 337 | + update = update("foo").with(set("x", 4)).where(eq("k", 0)).onlyIf(eq("x", 1)); | |
| 340 | 338 | assertEquals(update.toString(), query); |
| 341 | 339 | |
| 342 | 340 | // IF EXISTS CAS test |
| 343 | - update = builder.update("foo").with(set("x", 3)).where(eq("k", 2)).ifExists(); | |
| 341 | + update = update("foo").with(set("x", 3)).where(eq("k", 2)).ifExists(); | |
| 344 | 342 | assertThat(update.toString()).isEqualTo("UPDATE foo SET x=3 WHERE k=2 IF EXISTS;"); |
| 345 | 343 | } |
| 346 | 344 | |
| @@ -351,60 +349,60 @@ | ||
| 351 | 349 | Statement delete; |
| 352 | 350 | |
| 353 | 351 | query = "DELETE a,b,c FROM foo USING TIMESTAMP 0 WHERE k=1;"; |
| 354 | - delete = builder.delete("a", "b", "c").from("foo").using(timestamp(0)).where(eq("k", 1)); | |
| 352 | + delete = delete("a", "b", "c").from("foo").using(timestamp(0)).where(eq("k", 1)); | |
| 355 | 353 | assertEquals(delete.toString(), query); |
| 356 | 354 | |
| 357 | 355 | query = "DELETE a[3],b['foo'],c FROM foo WHERE k=1;"; |
| 358 | - delete = builder.delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1)); | |
| 356 | + delete = delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1)); | |
| 359 | 357 | assertEquals(delete.toString(), query); |
| 360 | 358 | |
| 361 | 359 | query = "DELETE a[?],b[?],c FROM foo WHERE k=1;"; |
| 362 | - delete = builder.delete().listElt("a", bindMarker()).mapElt("b", bindMarker()).column("c").from("foo").where(eq("k", 1)); | |
| 360 | + delete = delete().listElt("a", bindMarker()).mapElt("b", bindMarker()).column("c").from("foo").where(eq("k", 1)); | |
| 363 | 361 | assertEquals(delete.toString(), query); |
| 364 | 362 | |
| 365 | 363 | // Invalid CQL, testing edge case |
| 366 | 364 | query = "DELETE a,b,c FROM foo;"; |
| 367 | - delete = builder.delete("a", "b", "c").from("foo"); | |
| 365 | + delete = delete("a", "b", "c").from("foo"); | |
| 368 | 366 | assertEquals(delete.toString(), query); |
| 369 | 367 | |
| 370 | 368 | query = "DELETE FROM foo USING TIMESTAMP 1240003134 WHERE k='value';"; |
| 371 | - delete = builder.delete().all().from("foo").using(timestamp(1240003134L)).where(eq("k", "value")); | |
| 369 | + delete = delete().all().from("foo").using(timestamp(1240003134L)).where(eq("k", "value")); | |
| 372 | 370 | assertEquals(delete.toString(), query); |
| 373 | - delete = builder.delete().from("foo").using(timestamp(1240003134L)).where(eq("k", "value")); | |
| 371 | + delete = delete().from("foo").using(timestamp(1240003134L)).where(eq("k", "value")); | |
| 374 | 372 | assertEquals(delete.toString(), query); |
| 375 | 373 | |
| 376 | 374 | query = "DELETE a,b,c FROM foo.bar USING TIMESTAMP 1240003134 WHERE k=1;"; |
| 377 | - delete = builder.delete("a", "b", "c").from("foo", "bar").where().and(eq("k", 1)).using(timestamp(1240003134L)); | |
| 375 | + delete = delete("a", "b", "c").from("foo", "bar").where().and(eq("k", 1)).using(timestamp(1240003134L)); | |
| 378 | 376 | assertEquals(delete.toString(), query); |
| 379 | 377 | |
| 380 | 378 | query = "DELETE FROM foo.bar WHERE k1='foo' AND k2=1;"; |
| 381 | - delete = builder.delete().from("foo", "bar").where(eq("k1", "foo")).and(eq("k2", 1)); | |
| 379 | + delete = delete().from("foo", "bar").where(eq("k1", "foo")).and(eq("k2", 1)); | |
| 382 | 380 | assertEquals(delete.toString(), query); |
| 383 | 381 | |
| 384 | 382 | try { |
| 385 | - builder.delete().column("a").all().from("foo"); | |
| 383 | + delete().column("a").all().from("foo"); | |
| 386 | 384 | fail(); |
| 387 | 385 | } catch (IllegalStateException e) { |
| 388 | 386 | assertEquals(e.getMessage(), "Some columns ([a]) have already been selected."); |
| 389 | 387 | } |
| 390 | 388 | |
| 391 | 389 | try { |
| 392 | - builder.delete().from("foo").using(timestamp(-1240003134L)); | |
| 390 | + delete().from("foo").using(timestamp(-1240003134L)); | |
| 393 | 391 | fail(); |
| 394 | 392 | } catch (IllegalArgumentException e) { |
| 395 | 393 | assertEquals(e.getMessage(), "Invalid timestamp, must be positive"); |
| 396 | 394 | } |
| 397 | 395 | |
| 398 | 396 | query = "DELETE FROM foo.bar WHERE k1='foo' IF EXISTS;"; |
| 399 | - delete = builder.delete().from("foo", "bar").where(eq("k1", "foo")).ifExists(); | |
| 397 | + delete = delete().from("foo", "bar").where(eq("k1", "foo")).ifExists(); | |
| 400 | 398 | assertEquals(delete.toString(), query); |
| 401 | 399 | |
| 402 | 400 | query = "DELETE FROM foo.bar WHERE k1='foo' IF a=1 AND b=2;"; |
| 403 | - delete = builder.delete().from("foo", "bar").where(eq("k1", "foo")).onlyIf(eq("a", 1)).and(eq("b", 2)); | |
| 401 | + delete = delete().from("foo", "bar").where(eq("k1", "foo")).onlyIf(eq("a", 1)).and(eq("b", 2)); | |
| 404 | 402 | assertEquals(delete.toString(), query); |
| 405 | 403 | |
| 406 | 404 | query = "DELETE FROM foo WHERE k=:key;"; |
| 407 | - delete = builder.delete().from("foo").where(eq("k", bindMarker("key"))); | |
| 405 | + delete = delete().from("foo").where(eq("k", bindMarker("key"))); | |
| 408 | 406 | assertEquals(delete.toString(), query); |
| 409 | 407 | } |
| 410 | 408 | |
| @@ -419,14 +417,14 @@ | ||
| 419 | 417 | query += "UPDATE foo SET a[2]='foo',b=[3,2,1]+b,c=c-{'a'} WHERE k=2;"; |
| 420 | 418 | query += "DELETE a[3],b['foo'],c FROM foo WHERE k=1;"; |
| 421 | 419 | query += "APPLY BATCH;"; |
| 422 | - batch = builder.batch() | |
| 423 | - .add(builder.insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ | |
| 420 | + batch = batch() | |
| 421 | + .add(insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<Integer>() {{ | |
| 424 | 422 | add(2); |
| 425 | 423 | add(3); |
| 426 | 424 | add(4); |
| 427 | 425 | }}, 3.4 })) |
| 428 | - .add(builder.update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2))) | |
| 429 | - .add(builder.delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1))) | |
| 426 | + .add(update("foo").with(setIdx("a", 2, "foo")).and(prependAll("b", Arrays.asList(3, 2, 1))).and(remove("c", "a")).where(eq("k", 2))) | |
| 427 | + .add(delete().listElt("a", 3).mapElt("b", "foo").column("c").from("foo").where(eq("k", 1))) | |
| 430 | 428 | .using(timestamp(42)); |
| 431 | 429 | assertEquals(batch.toString(), query); |
| 432 | 430 | |
| @@ -434,10 +432,10 @@ | ||
| 434 | 432 | query = "BEGIN BATCH "; |
| 435 | 433 | query += "DELETE a[3] FROM foo WHERE k=1;"; |
| 436 | 434 | query += "APPLY BATCH;"; |
| 437 | - batch = builder.batch(builder.delete().listElt("a", 3).from("foo").where(eq("k", 1))); | |
| 435 | + batch = batch(delete().listElt("a", 3).from("foo").where(eq("k", 1))); | |
| 438 | 436 | assertEquals(batch.toString(), query); |
| 439 | 437 | |
| 440 | - assertEquals(builder.batch().toString(), "BEGIN BATCH APPLY BATCH;"); | |
| 438 | + assertEquals(batch().toString(), "BEGIN BATCH APPLY BATCH;"); | |
| 441 | 439 | } |
| 442 | 440 | |
| 443 | 441 | @Test(groups = "unit") |
| @@ -451,10 +449,10 @@ | ||
| 451 | 449 | query += "UPDATE foo SET b=b+2;"; |
| 452 | 450 | query += "UPDATE foo SET c=c+3;"; |
| 453 | 451 | query += "APPLY BATCH;"; |
| 454 | - batch = builder.batch() | |
| 455 | - .add(builder.update("foo").with(incr("a", 1))) | |
| 456 | - .add(builder.update("foo").with(incr("b", 2))) | |
| 457 | - .add(builder.update("foo").with(incr("c", 3))) | |
| 452 | + batch = batch() | |
| 453 | + .add(update("foo").with(incr("a", 1))) | |
| 454 | + .add(update("foo").with(incr("b", 2))) | |
| 455 | + .add(update("foo").with(incr("c", 3))) | |
| 458 | 456 | .using(timestamp(42)); |
| 459 | 457 | assertEquals(batch.toString(), query); |
| 460 | 458 | |
| @@ -464,10 +462,10 @@ | ||
| 464 | 462 | query += "UPDATE foo SET b=b+1;"; |
| 465 | 463 | query += "UPDATE foo SET c=c+1;"; |
| 466 | 464 | query += "APPLY BATCH;"; |
| 467 | - batch = builder.batch() | |
| 468 | - .add(builder.update("foo").with(incr("a"))) | |
| 469 | - .add(builder.update("foo").with(incr("b"))) | |
| 470 | - .add(builder.update("foo").with(incr("c"))) | |
| 465 | + batch = batch() | |
| 466 | + .add(update("foo").with(incr("a"))) | |
| 467 | + .add(update("foo").with(incr("b"))) | |
| 468 | + .add(update("foo").with(incr("c"))) | |
| 471 | 469 | .using(timestamp(42)); |
| 472 | 470 | assertEquals(batch.toString(), query); |
| 473 | 471 | |
| @@ -477,10 +475,10 @@ | ||
| 477 | 475 | query += "UPDATE foo SET b=b-2;"; |
| 478 | 476 | query += "UPDATE foo SET c=c-3;"; |
| 479 | 477 | query += "APPLY BATCH;"; |
| 480 | - batch = builder.batch() | |
| 481 | - .add(builder.update("foo").with(decr("a", 1))) | |
| 482 | - .add(builder.update("foo").with(decr("b", 2))) | |
| 483 | - .add(builder.update("foo").with(decr("c", 3))) | |
| 478 | + batch = batch() | |
| 479 | + .add(update("foo").with(decr("a", 1))) | |
| 480 | + .add(update("foo").with(decr("b", 2))) | |
| 481 | + .add(update("foo").with(decr("c", 3))) | |
| 484 | 482 | .using(timestamp(42)); |
| 485 | 483 | assertEquals(batch.toString(), query); |
| 486 | 484 | |
| @@ -490,10 +488,10 @@ | ||
| 490 | 488 | query += "UPDATE foo SET b=b-1;"; |
| 491 | 489 | query += "UPDATE foo SET c=c-1;"; |
| 492 | 490 | query += "APPLY BATCH;"; |
| 493 | - batch = builder.batch() | |
| 494 | - .add(builder.update("foo").with(decr("a"))) | |
| 495 | - .add(builder.update("foo").with(decr("b"))) | |
| 496 | - .add(builder.update("foo").with(decr("c"))) | |
| 491 | + batch = batch() | |
| 492 | + .add(update("foo").with(decr("a"))) | |
| 493 | + .add(update("foo").with(decr("b"))) | |
| 494 | + .add(update("foo").with(decr("c"))) | |
| 497 | 495 | .using(timestamp(42)); |
| 498 | 496 | assertEquals(batch.toString(), query); |
| 499 | 497 | |
| @@ -503,20 +501,20 @@ | ||
| 503 | 501 | query += "UPDATE foo SET b=b+-2;"; |
| 504 | 502 | query += "UPDATE foo SET c=c-3;"; |
| 505 | 503 | query += "APPLY BATCH;"; |
| 506 | - batch = builder.batch() | |
| 507 | - .add(builder.update("foo").with(decr("a", -1))) | |
| 508 | - .add(builder.update("foo").with(incr("b", -2))) | |
| 509 | - .add(builder.update("foo").with(decr("c", 3))) | |
| 504 | + batch = batch() | |
| 505 | + .add(update("foo").with(decr("a", -1))) | |
| 506 | + .add(update("foo").with(incr("b", -2))) | |
| 507 | + .add(update("foo").with(decr("c", 3))) | |
| 510 | 508 | .using(timestamp(42)); |
| 511 | 509 | assertEquals(batch.toString(), query); |
| 512 | 510 | } |
| 513 | 511 | |
| 514 | 512 | @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class }) |
| 515 | 513 | public void batchMixedCounterTest() throws Exception { |
| 516 | - builder.batch() | |
| 517 | - .add(builder.update("foo").with(incr("a", 1))) | |
| 518 | - .add(builder.update("foo").with(set("b", 2))) | |
| 519 | - .add(builder.update("foo").with(incr("c", 3))) | |
| 514 | + batch() | |
| 515 | + .add(update("foo").with(incr("a", 1))) | |
| 516 | + .add(update("foo").with(set("b", 2))) | |
| 517 | + .add(update("foo").with(incr("c", 3))) | |
| 520 | 518 | .using(timestamp(42)); |
| 521 | 519 | } |
| 522 | 520 | |
| @@ -526,7 +524,7 @@ | ||
| 526 | 524 | Statement insert; |
| 527 | 525 | |
| 528 | 526 | query = "INSERT INTO test (k,c) VALUES (0,?);"; |
| 529 | - insert = builder.insertInto("test") | |
| 527 | + insert = insertInto("test") | |
| 530 | 528 | .value("k", 0) |
| 531 | 529 | .value("c", bindMarker()); |
| 532 | 530 | assertEquals(insert.toString(), query); |
| @@ -539,19 +537,19 @@ | ||
| 539 | 537 | Statement select; |
| 540 | 538 | |
| 541 | 539 | query = "SELECT * FROM t WHERE c='C''est la vie!';"; |
| 542 | - select = builder.select().from("t").where(eq("c", "C'est la vie!")); | |
| 540 | + select = select().from("t").where(eq("c", "C'est la vie!")); | |
| 543 | 541 | assertEquals(select.toString(), query); |
| 544 | 542 | |
| 545 | 543 | query = "SELECT * FROM t WHERE c=C'est la vie!;"; |
| 546 | - select = builder.select().from("t").where(eq("c", raw("C'est la vie!"))); | |
| 544 | + select = select().from("t").where(eq("c", raw("C'est la vie!"))); | |
| 547 | 545 | assertEquals(select.toString(), query); |
| 548 | 546 | |
| 549 | 547 | query = "SELECT * FROM t WHERE c=now();"; |
| 550 | - select = builder.select().from("t").where(eq("c", fcall("now"))); | |
| 548 | + select = select().from("t").where(eq("c", fcall("now"))); | |
| 551 | 549 | assertEquals(select.toString(), query); |
| 552 | 550 | |
| 553 | 551 | query = "SELECT * FROM t WHERE c='now()';"; |
| 554 | - select = builder.select().from("t").where(eq("c", raw("'now()'"))); | |
| 552 | + select = select().from("t").where(eq("c", raw("'now()'"))); | |
| 555 | 553 | assertEquals(select.toString(), query); |
| 556 | 554 | } |
| 557 | 555 | |
| @@ -562,52 +560,52 @@ | ||
| 562 | 560 | Statement select; |
| 563 | 561 | |
| 564 | 562 | query = "SELECT * FROM \"foo WHERE k=4\";"; |
| 565 | - select = builder.select().all().from("foo WHERE k=4"); | |
| 563 | + select = select().all().from("foo WHERE k=4"); | |
| 566 | 564 | assertEquals(select.toString(), query); |
| 567 | 565 | |
| 568 | 566 | query = "SELECT * FROM foo WHERE k='4 AND c=5';"; |
| 569 | - select = builder.select().all().from("foo").where(eq("k", "4 AND c=5")); | |
| 567 | + select = select().all().from("foo").where(eq("k", "4 AND c=5")); | |
| 570 | 568 | assertEquals(select.toString(), query); |
| 571 | 569 | |
| 572 | 570 | query = "SELECT * FROM foo WHERE k='4'' AND c=''5';"; |
| 573 | - select = builder.select().all().from("foo").where(eq("k", "4' AND c='5")); | |
| 571 | + select = select().all().from("foo").where(eq("k", "4' AND c='5")); | |
| 574 | 572 | assertEquals(select.toString(), query); |
| 575 | 573 | |
| 576 | 574 | query = "SELECT * FROM foo WHERE k='4'' OR ''1''=''1';"; |
| 577 | - select = builder.select().all().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 575 | + select = select().all().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 578 | 576 | assertEquals(select.toString(), query); |
| 579 | 577 | |
| 580 | 578 | query = "SELECT * FROM foo WHERE k='4; --test comment;';"; |
| 581 | - select = builder.select().all().from("foo").where(eq("k", "4; --test comment;")); | |
| 579 | + select = select().all().from("foo").where(eq("k", "4; --test comment;")); | |
| 582 | 580 | assertEquals(select.toString(), query); |
| 583 | 581 | |
| 584 | 582 | query = "SELECT \"*\" FROM foo;"; |
| 585 | - select = builder.select("*").from("foo"); | |
| 583 | + select = select("*").from("foo"); | |
| 586 | 584 | assertEquals(select.toString(), query); |
| 587 | 585 | |
| 588 | 586 | query = "SELECT a,b FROM foo WHERE a IN ('b','c''); --comment');"; |
| 589 | - select = builder.select("a", "b").from("foo").where(in("a", "b", "c'); --comment")); | |
| 587 | + select = select("a", "b").from("foo").where(in("a", "b", "c'); --comment")); | |
| 590 | 588 | assertEquals(select.toString(), query); |
| 591 | 589 | |
| 592 | 590 | // User Injection? |
| 593 | 591 | query = "SELECT * FROM bar; --(b) FROM foo;"; |
| 594 | - select = builder.select().fcall("* FROM bar; --", column("b")).from("foo"); | |
| 592 | + select = select().fcall("* FROM bar; --", column("b")).from("foo"); | |
| 595 | 593 | assertEquals(select.toString(), query); |
| 596 | 594 | |
| 597 | 595 | query = "SELECT writetime(\"a) FROM bar; --\"),ttl(a) FROM foo ALLOW FILTERING;"; |
| 598 | - select = builder.select().writeTime("a) FROM bar; --").ttl("a").from("foo").allowFiltering(); | |
| 596 | + select = select().writeTime("a) FROM bar; --").ttl("a").from("foo").allowFiltering(); | |
| 599 | 597 | assertEquals(select.toString(), query); |
| 600 | 598 | |
| 601 | 599 | query = "SELECT writetime(a),ttl(\"a) FROM bar; --\") FROM foo ALLOW FILTERING;"; |
| 602 | - select = builder.select().writeTime("a").ttl("a) FROM bar; --").from("foo").allowFiltering(); | |
| 600 | + select = select().writeTime("a").ttl("a) FROM bar; --").from("foo").allowFiltering(); | |
| 603 | 601 | assertEquals(select.toString(), query); |
| 604 | 602 | |
| 605 | 603 | query = "SELECT * FROM foo WHERE \"k=1 OR k\">42 LIMIT 42;"; |
| 606 | - select = builder.select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); | |
| 604 | + select = select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); | |
| 607 | 605 | assertEquals(select.toString(), query); |
| 608 | 606 | |
| 609 | 607 | query = "SELECT * FROM foo WHERE token(\"k)>0 OR token(k\")>token(42);"; |
| 610 | - select = builder.select().all().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 608 | + select = select().all().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 611 | 609 | assertEquals(select.toString(), query); |
| 612 | 610 | } |
| 613 | 611 | |
| @@ -619,15 +617,15 @@ | ||
| 619 | 617 | Statement insert; |
| 620 | 618 | |
| 621 | 619 | query = "INSERT INTO foo (a) VALUES ('123); --comment');"; |
| 622 | - insert = builder.insertInto("foo").value("a", "123); --comment"); | |
| 620 | + insert = insertInto("foo").value("a", "123); --comment"); | |
| 623 | 621 | assertEquals(insert.toString(), query); |
| 624 | 622 | |
| 625 | 623 | query = "INSERT INTO foo (\"a,b\") VALUES (123);"; |
| 626 | - insert = builder.insertInto("foo").value("a,b", 123); | |
| 624 | + insert = insertInto("foo").value("a,b", 123); | |
| 627 | 625 | assertEquals(insert.toString(), query); |
| 628 | 626 | |
| 629 | 627 | query = "INSERT INTO foo (a,b) VALUES ({'2''} space','3','4'},3.4) USING TTL 24 AND TIMESTAMP 42;"; |
| 630 | - insert = builder.insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<String>() {{ | |
| 628 | + insert = insertInto("foo").values(new String[]{ "a", "b" }, new Object[]{ new TreeSet<String>() {{ | |
| 631 | 629 | add("2'} space"); |
| 632 | 630 | add("3"); |
| 633 | 631 | add("4"); |
| @@ -642,15 +640,15 @@ | ||
| 642 | 640 | Statement update; |
| 643 | 641 | |
| 644 | 642 | query = "UPDATE foo.bar USING TIMESTAMP 42 SET a=12 WHERE k='2 OR 1=1';"; |
| 645 | - update = builder.update("foo", "bar").using(timestamp(42)).with(set("a", 12)).where(eq("k", "2 OR 1=1")); | |
| 643 | + update = update("foo", "bar").using(timestamp(42)).with(set("a", 12)).where(eq("k", "2 OR 1=1")); | |
| 646 | 644 | assertEquals(update.toString(), query); |
| 647 | 645 | |
| 648 | 646 | query = "UPDATE foo SET b='null WHERE k=1; --comment' WHERE k=2;"; |
| 649 | - update = builder.update("foo").where().and(eq("k", 2)).with(set("b", "null WHERE k=1; --comment")); | |
| 647 | + update = update("foo").where().and(eq("k", 2)).with(set("b", "null WHERE k=1; --comment")); | |
| 650 | 648 | assertEquals(update.toString(), query); |
| 651 | 649 | |
| 652 | 650 | query = "UPDATE foo USING TIMESTAMP 42 SET \"b WHERE k=1; --comment\"=[3,2,1]+\"b WHERE k=1; --comment\" WHERE k=2;"; |
| 653 | - update = builder.update("foo").where().and(eq("k", 2)).with(prependAll("b WHERE k=1; --comment", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 651 | + update = update("foo").where().and(eq("k", 2)).with(prependAll("b WHERE k=1; --comment", Arrays.asList(3, 2, 1))).using(timestamp(42)); | |
| 654 | 652 | assertEquals(update.toString(), query); |
| 655 | 653 | } |
| 656 | 654 | |
| @@ -661,47 +659,47 @@ | ||
| 661 | 659 | Statement delete; |
| 662 | 660 | |
| 663 | 661 | query = "DELETE FROM \"foo WHERE k=4\";"; |
| 664 | - delete = builder.delete().from("foo WHERE k=4"); | |
| 662 | + delete = delete().from("foo WHERE k=4"); | |
| 665 | 663 | assertEquals(delete.toString(), query); |
| 666 | 664 | |
| 667 | 665 | query = "DELETE FROM foo WHERE k='4 AND c=5';"; |
| 668 | - delete = builder.delete().from("foo").where(eq("k", "4 AND c=5")); | |
| 666 | + delete = delete().from("foo").where(eq("k", "4 AND c=5")); | |
| 669 | 667 | assertEquals(delete.toString(), query); |
| 670 | 668 | |
| 671 | 669 | query = "DELETE FROM foo WHERE k='4'' AND c=''5';"; |
| 672 | - delete = builder.delete().from("foo").where(eq("k", "4' AND c='5")); | |
| 670 | + delete = delete().from("foo").where(eq("k", "4' AND c='5")); | |
| 673 | 671 | assertEquals(delete.toString(), query); |
| 674 | 672 | |
| 675 | 673 | query = "DELETE FROM foo WHERE k='4'' OR ''1''=''1';"; |
| 676 | - delete = builder.delete().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 674 | + delete = delete().from("foo").where(eq("k", "4' OR '1'='1")); | |
| 677 | 675 | assertEquals(delete.toString(), query); |
| 678 | 676 | |
| 679 | 677 | query = "DELETE FROM foo WHERE k='4; --test comment;';"; |
| 680 | - delete = builder.delete().from("foo").where(eq("k", "4; --test comment;")); | |
| 678 | + delete = delete().from("foo").where(eq("k", "4; --test comment;")); | |
| 681 | 679 | assertEquals(delete.toString(), query); |
| 682 | 680 | |
| 683 | 681 | query = "DELETE \"*\" FROM foo;"; |
| 684 | - delete = builder.delete("*").from("foo"); | |
| 682 | + delete = delete("*").from("foo"); | |
| 685 | 683 | assertEquals(delete.toString(), query); |
| 686 | 684 | |
| 687 | 685 | query = "DELETE a,b FROM foo WHERE a IN ('b','c''); --comment');"; |
| 688 | - delete = builder.delete("a", "b").from("foo") | |
| 686 | + delete = delete("a", "b").from("foo") | |
| 689 | 687 | .where(in("a", "b", "c'); --comment")); |
| 690 | 688 | assertEquals(delete.toString(), query); |
| 691 | 689 | |
| 692 | 690 | query = "DELETE FROM foo WHERE \"k=1 OR k\">42;"; |
| 693 | - delete = builder.delete().from("foo").where(gt("k=1 OR k", 42)); | |
| 691 | + delete = delete().from("foo").where(gt("k=1 OR k", 42)); | |
| 694 | 692 | assertEquals(delete.toString(), query); |
| 695 | 693 | |
| 696 | 694 | query = "DELETE FROM foo WHERE token(\"k)>0 OR token(k\")>token(42);"; |
| 697 | - delete = builder.delete().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 695 | + delete = delete().from("foo").where(gt(token("k)>0 OR token(k"), fcall("token", 42))); | |
| 698 | 696 | assertEquals(delete.toString(), query); |
| 699 | 697 | } |
| 700 | 698 | |
| 701 | 699 | @Test(groups = "unit") |
| 702 | 700 | public void statementForwardingTest() throws Exception { |
| 703 | 701 | |
| 704 | - Update upd = builder.update("foo"); | |
| 702 | + Update upd = update("foo"); | |
| 705 | 703 | upd.setConsistencyLevel(ConsistencyLevel.QUORUM); |
| 706 | 704 | upd.enableTracing(); |
| 707 | 705 | |
| @@ -714,9 +712,10 @@ | ||
| 714 | 712 | @Test(groups = "unit") |
| 715 | 713 | public void rejectUnknownValueTest() throws Exception { |
| 716 | 714 | try { |
| 717 | - //noinspection ResultOfMethodCallIgnored | |
| 718 | - builder.update("foo").with(set("a", new byte[13])).where(eq("k", 2)).toString(); | |
| 719 | - fail("Byte arrays should not be valid, ByteBuffer should be used instead"); | |
| 715 | + RegularStatement s = update("foo").with(set("a", new byte[13])).where(eq("k", 2)) | |
| 716 | + .setForceNoValues(true); | |
| 717 | + s.getQueryString(); | |
| 718 | + fail("should have failed to inline a byte[]"); | |
| 720 | 719 | } catch (CodecNotFoundException e) { |
| 721 | 720 | // Ok, that's what we expect |
| 722 | 721 | } |
| @@ -724,28 +723,28 @@ | ||
| 724 | 723 | |
| 725 | 724 | @Test(groups = "unit") |
| 726 | 725 | public void truncateTest() throws Exception { |
| 727 | - assertEquals(builder.truncate("foo").toString(), "TRUNCATE foo;"); | |
| 728 | - assertEquals(builder.truncate("foo", quote("Bar")).toString(), "TRUNCATE foo.\"Bar\";"); | |
| 726 | + assertEquals(truncate("foo").toString(), "TRUNCATE foo;"); | |
| 727 | + assertEquals(truncate("foo", quote("Bar")).toString(), "TRUNCATE foo.\"Bar\";"); | |
| 729 | 728 | } |
| 730 | 729 | |
| 731 | 730 | @Test(groups = "unit") |
| 732 | 731 | public void quotingTest() { |
| 733 | - assertEquals(builder.select().from("Metrics", "epochs").toString(), | |
| 732 | + assertEquals(select().from("Metrics", "epochs").toString(), | |
| 734 | 733 | "SELECT * FROM Metrics.epochs;"); |
| 735 | - assertEquals(builder.select().from("Metrics", quote("epochs")).toString(), | |
| 734 | + assertEquals(select().from("Metrics", quote("epochs")).toString(), | |
| 736 | 735 | "SELECT * FROM Metrics.\"epochs\";"); |
| 737 | - assertEquals(builder.select().from(quote("Metrics"), "epochs").toString(), | |
| 736 | + assertEquals(select().from(quote("Metrics"), "epochs").toString(), | |
| 738 | 737 | "SELECT * FROM \"Metrics\".epochs;"); |
| 739 | - assertEquals(builder.select().from(quote("Metrics"), quote("epochs")).toString(), | |
| 738 | + assertEquals(select().from(quote("Metrics"), quote("epochs")).toString(), | |
| 740 | 739 | "SELECT * FROM \"Metrics\".\"epochs\";"); |
| 741 | 740 | |
| 742 | - assertEquals(builder.insertInto("Metrics", "epochs").toString(), | |
| 741 | + assertEquals(insertInto("Metrics", "epochs").toString(), | |
| 743 | 742 | "INSERT INTO Metrics.epochs () VALUES ();"); |
| 744 | - assertEquals(builder.insertInto("Metrics", quote("epochs")).toString(), | |
| 743 | + assertEquals(insertInto("Metrics", quote("epochs")).toString(), | |
| 745 | 744 | "INSERT INTO Metrics.\"epochs\" () VALUES ();"); |
| 746 | - assertEquals(builder.insertInto(quote("Metrics"), "epochs").toString(), | |
| 745 | + assertEquals(insertInto(quote("Metrics"), "epochs").toString(), | |
| 747 | 746 | "INSERT INTO \"Metrics\".epochs () VALUES ();"); |
| 748 | - assertEquals(builder.insertInto(quote("Metrics"), quote("epochs")).toString(), | |
| 747 | + assertEquals(insertInto(quote("Metrics"), quote("epochs")).toString(), | |
| 749 | 748 | "INSERT INTO \"Metrics\".\"epochs\" () VALUES ();"); |
| 750 | 749 | } |
| 751 | 750 | |
| @@ -755,23 +754,23 @@ | ||
| 755 | 754 | Statement select; |
| 756 | 755 | |
| 757 | 756 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>('a',2);"; |
| 758 | - select = builder.select().all().from("foo").where(eq("k", 4)).and(gt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); | |
| 757 | + select = select().all().from("foo").where(eq("k", 4)).and(gt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); | |
| 759 | 758 | assertEquals(select.toString(), query); |
| 760 | 759 | |
| 761 | 760 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)>=('a',2) AND (c1,c2)<('b',0);"; |
| 762 | - select = builder.select().all().from("foo").where(eq("k", 4)).and(gte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))) | |
| 761 | + select = select().all().from("foo").where(eq("k", 4)).and(gte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))) | |
| 763 | 762 | .and(lt(Arrays.asList("c1", "c2"), Arrays.<Object>asList("b", 0))); |
| 764 | 763 | assertEquals(select.toString(), query); |
| 765 | 764 | |
| 766 | 765 | query = "SELECT * FROM foo WHERE k=4 AND (c1,c2)<=('a',2);"; |
| 767 | - select = builder.select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); | |
| 766 | + select = select().all().from("foo").where(eq("k", 4)).and(lte(Arrays.asList("c1", "c2"), Arrays.<Object>asList("a", 2))); | |
| 768 | 767 | assertEquals(select.toString(), query); |
| 769 | 768 | } |
| 770 | 769 | |
| 771 | 770 | @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) |
| 772 | 771 | public void should_fail_if_in_clause_has_too_many_values() { |
| 773 | 772 | List<Object> values = Collections.<Object>nCopies(65536, "a"); |
| 774 | - builder.select().all().from("foo").where(in("bar", values.toArray())); | |
| 773 | + select().all().from("foo").where(in("bar", values.toArray())); | |
| 775 | 774 | } |
| 776 | 775 | |
| 777 | 776 | @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class) |
| @@ -779,13 +778,13 @@ | ||
| 779 | 778 | List<Object> values = Collections.<Object>nCopies(65535, "a"); |
| 780 | 779 | |
| 781 | 780 | // If the excessive count results from successive DSL calls, we don't check it on the fly so this statement works: |
| 782 | - BuiltStatement statement = builder.select().all().from("foo") | |
| 781 | + BuiltStatement statement = select().all().from("foo") | |
| 783 | 782 | .where(eq("bar", "a")) |
| 784 | 783 | .and(in("baz", values.toArray())); |
| 785 | 784 | |
| 786 | 785 | // But we still want to check it client-side, to fail fast instead of sending a bad query to Cassandra. |
| 787 | 786 | // getValues() is called on any RegularStatement before we send it (see SessionManager.makeRequestMessage). |
| 788 | - statement.getValues(); | |
| 787 | + statement.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE); | |
| 789 | 788 | } |
| 790 | 789 | |
| 791 | 790 | @Test(groups = "unit") |
| @@ -796,57 +795,57 @@ | ||
| 796 | 795 | |
| 797 | 796 | query = "UPDATE foo SET l=[[1],[2]] WHERE k=1;"; |
| 798 | 797 | ImmutableList<ImmutableList<Integer>> list = ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2)); |
| 799 | - statement = builder.update("foo").with(set("l", list)).where(eq("k", 1)); | |
| 798 | + statement = update("foo").with(set("l", list)).where(eq("k", 1)); | |
| 800 | 799 | assertThat(statement.toString()).isEqualTo(query); |
| 801 | 800 | |
| 802 | 801 | query = "UPDATE foo SET m={1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; |
| 803 | - statement = builder.update("foo").with(set("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); | |
| 802 | + statement = update("foo").with(set("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); | |
| 804 | 803 | assertThat(statement.toString()).isEqualTo(query); |
| 805 | 804 | |
| 806 | 805 | query = "UPDATE foo SET m=m+{1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; |
| 807 | - statement = builder.update("foo").with(putAll("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); | |
| 806 | + statement = update("foo").with(putAll("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); | |
| 808 | 807 | assertThat(statement.toString()).isEqualTo(query); |
| 809 | 808 | |
| 810 | 809 | query = "UPDATE foo SET l=[[1]]+l WHERE k=1;"; |
| 811 | - statement = builder.update("foo").with(prepend("l", ImmutableList.of(1))).where(eq("k", 1)); | |
| 810 | + statement = update("foo").with(prepend("l", ImmutableList.of(1))).where(eq("k", 1)); | |
| 812 | 811 | assertThat(statement.toString()).isEqualTo(query); |
| 813 | 812 | |
| 814 | 813 | query = "UPDATE foo SET l=[[1],[2]]+l WHERE k=1;"; |
| 815 | - statement = builder.update("foo").with(prependAll("l", list)).where(eq("k", 1)); | |
| 814 | + statement = update("foo").with(prependAll("l", list)).where(eq("k", 1)); | |
| 816 | 815 | assertThat(statement.toString()).isEqualTo(query); |
| 817 | 816 | } |
| 818 | 817 | |
| 819 | 818 | @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) |
| 820 | 819 | public void should_not_allow_bind_marker_for_add() { |
| 821 | 820 | // This generates the query "UPDATE foo SET s = s + {?} WHERE k = 1", which is invalid in Cassandra |
| 822 | - builder.update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 821 | + update("foo").with(add("s", bindMarker())).where(eq("k", 1)); | |
| 823 | 822 | } |
| 824 | 823 | |
| 825 | 824 | @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) |
| 826 | 825 | public void should_now_allow_bind_marker_for_prepend() { |
| 827 | - builder.update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 826 | + update("foo").with(prepend("l", bindMarker())).where(eq("k", 1)); | |
| 828 | 827 | } |
| 829 | 828 | |
| 830 | 829 | @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) |
| 831 | 830 | public void should_not_allow_bind_marker_for_append() { |
| 832 | - builder.update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 831 | + update("foo").with(append("l", bindMarker())).where(eq("k", 1)); | |
| 833 | 832 | } |
| 834 | 833 | |
| 835 | 834 | @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) |
| 836 | 835 | public void should_not_allow_bind_marker_for_remove() { |
| 837 | - builder.update("foo").with(remove("s", bindMarker())).where(eq("k", 1)); | |
| 836 | + update("foo").with(remove("s", bindMarker())).where(eq("k", 1)); | |
| 838 | 837 | } |
| 839 | 838 | |
| 840 | 839 | @Test(groups = "unit", expectedExceptions = InvalidQueryException.class) |
| 841 | 840 | public void should_not_allow_bind_marker_for_discard() { |
| 842 | - builder.update("foo").with(discard("l", bindMarker())).where(eq("k", 1)); | |
| 841 | + update("foo").with(discard("l", bindMarker())).where(eq("k", 1)); | |
| 843 | 842 | } |
| 844 | 843 | |
| 845 | 844 | @Test(groups = "unit") |
| 846 | 845 | public void should_quote_complex_column_names() { |
| 847 | 846 | // A column name can be anything as long as it's quoted, so "foo.bar" is a valid name |
| 848 | 847 | String query = "SELECT * FROM foo WHERE \"foo.bar\"=1;"; |
| 849 | - Statement statement = builder.select().from("foo").where(eq(quote("foo.bar"), 1)); | |
| 848 | + Statement statement = select().from("foo").where(eq(quote("foo.bar"), 1)); | |
| 850 | 849 | |
| 851 | 850 | assertThat(statement.toString()).isEqualTo(query); |
| 852 | 851 | } |
| @@ -855,32 +854,32 @@ | ||
| 855 | 854 | public void should_quote_column_names_with_escaped_quotes() { |
| 856 | 855 | // A column name can include quotes as long as it is escaped with another set of quotes, so "foo""bar" is a valid name. |
| 857 | 856 | String query = "SELECT * FROM foo WHERE \"foo \"\" bar\"=1;"; |
| 858 | - Statement statement = builder.select().from("foo").where(eq(quote("foo \"\" bar"), 1)); | |
| 857 | + Statement statement = select().from("foo").where(eq(quote("foo \"\" bar"), 1)); | |
| 859 | 858 | |
| 860 | 859 | assertThat(statement.toString()).isEqualTo(query); |
| 861 | 860 | } |
| 862 | 861 | |
| 863 | 862 | @Test(groups = "unit") |
| 864 | 863 | public void should_not_serialize_raw_query_values() { |
| 865 | - RegularStatement select = builder.select().from("test").where(gt("i", raw("1"))); | |
| 864 | + RegularStatement select = select().from("test").where(gt("i", raw("1"))); | |
| 866 | 865 | assertThat(select.getQueryString()).doesNotContain("?"); |
| 867 | - assertThat(select.getValues()).isNull(); | |
| 866 | + assertThat(select.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNull(); | |
| 868 | 867 | } |
| 869 | 868 | |
| 870 | 869 | @Test(groups = "unit", expectedExceptions = { IllegalStateException.class }) |
| 871 | 870 | public void should_throw_ISE_if_getObject_called_on_statement_without_values() { |
| 872 | - builder.select().from("test").where(eq("foo", 42)).getObject(0); // integers are appended to the CQL string | |
| 871 | + select().from("test").where(eq("foo", 42)).getObject(0); // integers are appended to the CQL string | |
| 873 | 872 | } |
| 874 | 873 | |
| 875 | 874 | @Test(groups = "unit", expectedExceptions = { IndexOutOfBoundsException.class }) |
| 876 | 875 | public void should_throw_IOOBE_if_getObject_called_with_wrong_index() { |
| 877 | - builder.select().from("test").where(eq("foo", new Object())).getObject(1); | |
| 876 | + select().from("test").where(eq("foo", new Object())).getObject(1); | |
| 878 | 877 | } |
| 879 | 878 | |
| 880 | 879 | @Test(groups = "unit") |
| 881 | 880 | public void should_return_object_at_ith_index() { |
| 882 | 881 | Object expected = new Object(); |
| 883 | - Object actual = builder.select().from("test").where(eq("foo", expected)).getObject(0); | |
| 882 | + Object actual = select().from("test").where(eq("foo", expected)).getObject(0); | |
| 884 | 883 | assertThat(actual).isSameAs(expected); |
| 885 | 884 | } |
| 886 | 885 | |
| @@ -890,13 +889,13 @@ | ||
| 890 | 889 | Set<UUID> set = Sets.newHashSet(UUID.randomUUID()); |
| 891 | 890 | List<Date> list = Lists.newArrayList(new Date()); |
| 892 | 891 | Map<BigInteger, String> map = ImmutableMap.of(new BigInteger("1"), "foo"); |
| 893 | - BuiltStatement query = builder.insertInto("foo").value("v", set); | |
| 892 | + BuiltStatement query = insertInto("foo").value("v", set); | |
| 894 | 893 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); |
| 895 | 894 | assertThat(query.getObject(0)).isEqualTo(set); |
| 896 | - query = builder.insertInto("foo").value("v", list); | |
| 895 | + query = insertInto("foo").value("v", list); | |
| 897 | 896 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); |
| 898 | 897 | assertThat(query.getObject(0)).isEqualTo(list); |
| 899 | - query = builder.insertInto("foo").value("v", map); | |
| 898 | + query = insertInto("foo").value("v", map); | |
| 900 | 899 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES (?);"); |
| 901 | 900 | assertThat(query.getObject(0)).isEqualTo(map); |
| 902 | 901 | } |
| @@ -904,17 +903,17 @@ | ||
| 904 | 903 | @Test(groups = "unit") |
| 905 | 904 | @CassandraVersion(major = 2.1) |
| 906 | 905 | public void should_not_attempt_to_serialize_function_calls_in_collections() { |
| 907 | - BuiltStatement query = builder.insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1))); | |
| 906 | + BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1))); | |
| 908 | 907 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});"); |
| 909 | - assertThat(query.getValues()).isNullOrEmpty(); | |
| 908 | + assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNullOrEmpty(); | |
| 910 | 909 | } |
| 911 | 910 | |
| 912 | 911 | @Test(groups = "unit") |
| 913 | 912 | @CassandraVersion(major = 2.1) |
| 914 | 913 | public void should_not_attempt_to_serialize_raw_values_in_collections() { |
| 915 | - BuiltStatement query = builder.insertInto("foo").value("v", ImmutableMap.of(1, raw("x"))); | |
| 914 | + BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x"))); | |
| 916 | 915 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});"); |
| 917 | - assertThat(query.getValues()).isNullOrEmpty(); | |
| 916 | + assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNullOrEmpty(); | |
| 918 | 917 | } |
| 919 | 918 | |
| 920 | 919 | @Test(groups = "unit") |
| @@ -923,17 +922,17 @@ | ||
| 923 | 922 | BuiltStatement query; |
| 924 | 923 | // lists |
| 925 | 924 | List<Integer> list = Lists.newArrayList(1, 2, 3); |
| 926 | - query = builder.insertInto("foo").value("v", list); | |
| 925 | + query = insertInto("foo").value("v", list); | |
| 927 | 926 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,3]);"); |
| 928 | 927 | assertThat(query.hasValues()).isFalse(); |
| 929 | 928 | // sets |
| 930 | 929 | Set<Integer> set = Sets.newHashSet(1, 2, 3); |
| 931 | - query = builder.insertInto("foo").value("v", set); | |
| 930 | + query = insertInto("foo").value("v", set); | |
| 932 | 931 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1,2,3});"); |
| 933 | 932 | assertThat(query.hasValues()).isFalse(); |
| 934 | 933 | // maps |
| 935 | 934 | Map<Integer, Float> map = ImmutableMap.of(1, 12.34f); |
| 936 | - query = builder.insertInto("foo").value("v", map); | |
| 935 | + query = insertInto("foo").value("v", map); | |
| 937 | 936 | assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:12.34});"); |
| 938 | 937 | assertThat(query.hasValues()).isFalse(); |
| 939 | 938 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTupleExecutionTest.java +4 −2
| @@ -32,7 +32,9 @@ | ||
| 32 | 32 | |
| 33 | 33 | import static com.datastax.driver.core.DataType.cint; |
| 34 | 34 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 35 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 35 | 36 | import static com.datastax.driver.core.querybuilder.QueryBuilder.set; |
| 37 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.update; | |
| 36 | 38 | |
| 37 | 39 | @CassandraVersion(major=2.1, minor=3) |
| 38 | 40 | public class QueryBuilderTupleExecutionTest extends CCMBridge.PerClassSingleNodeCluster { |
| @@ -46,7 +48,7 @@ | ||
| 46 | 48 | public void should_handle_tuple() throws Exception { |
| 47 | 49 | String query = "INSERT INTO foo (k,x) VALUES (0,(1));"; |
| 48 | 50 | TupleType tupleType = cluster.getMetadata().newTupleType(cint()); |
| 49 | - BuiltStatement insert = new QueryBuilder(cluster).insertInto("foo").value("k", 0).value("x", tupleType.newValue(1)); | |
| 51 | + BuiltStatement insert = insertInto("foo").value("k", 0).value("x", tupleType.newValue(1)); | |
| 50 | 52 | assertEquals(insert.toString(), query); |
| 51 | 53 | } |
| 52 | 54 | |
| @@ -58,7 +60,7 @@ | ||
| 58 | 60 | query = "UPDATE foo SET l=[(1,2)] WHERE k=1;"; |
| 59 | 61 | TupleType tupleType = cluster.getMetadata().newTupleType(cint(), cint()); |
| 60 | 62 | List<TupleValue> list = ImmutableList.of(tupleType.newValue(1, 2)); |
| 61 | - statement = new QueryBuilder(cluster).update("foo").with(set("l", list)).where(eq("k", 1)); | |
| 63 | + statement = update("foo").with(set("l", list)).where(eq("k", 1)); | |
| 62 | 64 | assertThat(statement.toString()).isEqualTo(query); |
| 63 | 65 | } |
| 64 | 66 |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderUDTExecutionTest.java +7 −8
| @@ -31,8 +31,7 @@ | ||
| 31 | 31 | import com.datastax.driver.core.*; |
| 32 | 32 | import com.datastax.driver.core.utils.CassandraVersion; |
| 33 | 33 | |
| 34 | -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; | |
| 35 | -import static com.datastax.driver.core.querybuilder.QueryBuilder.putAll; | |
| 34 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.*; | |
| 36 | 35 | |
| 37 | 36 | @CassandraVersion(major=2.1, minor=3) |
| 38 | 37 | public class QueryBuilderUDTExecutionTest extends CCMBridge.PerClassSingleNodeCluster { |
| @@ -48,12 +47,12 @@ | ||
| 48 | 47 | UserType udtType = cluster.getMetadata().getKeyspace(keyspace).getUserType("udt"); |
| 49 | 48 | UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); |
| 50 | 49 | |
| 51 | - Statement insert = new QueryBuilder(cluster).insertInto("udtTest").value("k", 1).value("t", udtValue); | |
| 50 | + Statement insert = insertInto("udtTest").value("k", 1).value("t", udtValue); | |
| 52 | 51 | assertEquals(insert.toString(), "INSERT INTO udtTest (k,t) VALUES (1,{i:2,a:'127.0.0.1'});"); |
| 53 | 52 | |
| 54 | 53 | session.execute(insert); |
| 55 | 54 | |
| 56 | - List<Row> rows = session.execute(new QueryBuilder(cluster).select().from("udtTest").where(eq("k", 1))).all(); | |
| 55 | + List<Row> rows = session.execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 57 | 56 | |
| 58 | 57 | assertEquals(rows.size(), 1); |
| 59 | 58 | |
| @@ -67,12 +66,12 @@ | ||
| 67 | 66 | UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); |
| 68 | 67 | UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost")); |
| 69 | 68 | |
| 70 | - Statement insert = new QueryBuilder(cluster).insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue)); | |
| 69 | + Statement insert = insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue)); | |
| 71 | 70 | assertThat(insert.toString()).isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2,a:'127.0.0.1'}]);"); |
| 72 | 71 | |
| 73 | 72 | session.execute(insert); |
| 74 | 73 | |
| 75 | - List<Row> rows = session.execute(new QueryBuilder(cluster).select().from("udtTest").where(eq("k", 1))).all(); | |
| 74 | + List<Row> rows = session.execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 76 | 75 | |
| 77 | 76 | assertThat(rows.size()).isEqualTo(1); |
| 78 | 77 | |
| @@ -82,13 +81,13 @@ | ||
| 82 | 81 | Map<Integer, UDTValue> map = Maps.newHashMap(); |
| 83 | 82 | map.put(0, udtValue); |
| 84 | 83 | map.put(2, udtValue2); |
| 85 | - Statement updateMap = new QueryBuilder(cluster).update("udtTest").with(putAll("m", map)).where(eq("k", 1)); | |
| 84 | + Statement updateMap = update("udtTest").with(putAll("m", map)).where(eq("k", 1)); | |
| 86 | 85 | assertThat(updateMap.toString()) |
| 87 | 86 | .isEqualTo("UPDATE udtTest SET m=m+{0:{i:2,a:'127.0.0.1'},2:{i:3,a:'127.0.0.1'}} WHERE k=1;"); |
| 88 | 87 | |
| 89 | 88 | session.execute(updateMap); |
| 90 | 89 | |
| 91 | - rows = session.execute(new QueryBuilder(cluster).select().from("udtTest").where(eq("k", 1))).all(); | |
| 90 | + rows = session.execute(select().from("udtTest").where(eq("k", 1))).all(); | |
| 92 | 91 | r1 = rows.get(0); |
| 93 | 92 | assertThat(r1.getMap("m", Integer.class, UDTValue.class)).isEqualTo(map); |
| 94 | 93 | } |
modified driver-examples/osgi/src/main/java/com/datastax/driver/osgi/impl/MailboxImpl.java +4 −7
| @@ -22,14 +22,12 @@ | ||
| 22 | 22 | |
| 23 | 23 | import com.datastax.driver.core.*; |
| 24 | 24 | import com.datastax.driver.core.exceptions.InvalidQueryException; |
| 25 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 26 | 25 | import com.datastax.driver.core.utils.UUIDs; |
| 27 | 26 | import com.datastax.driver.osgi.api.MailboxException; |
| 28 | 27 | import com.datastax.driver.osgi.api.MailboxMessage; |
| 29 | 28 | import com.datastax.driver.osgi.api.MailboxService; |
| 30 | 29 | |
| 31 | -import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; | |
| 32 | -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; | |
| 30 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.*; | |
| 33 | 31 | |
| 34 | 32 | public class MailboxImpl implements MailboxService { |
| 35 | 33 | |
| @@ -71,18 +69,17 @@ | ||
| 71 | 69 | "PRIMARY KEY (recipient, time))"); |
| 72 | 70 | } |
| 73 | 71 | |
| 74 | - QueryBuilder builder = new QueryBuilder(session.getCluster()); | |
| 75 | - retrieveStatement = session.prepare(builder.select() | |
| 72 | + retrieveStatement = session.prepare(select() | |
| 76 | 73 | .from(keyspace, TABLE) |
| 77 | 74 | .where(eq("recipient", bindMarker()))); |
| 78 | 75 | |
| 79 | - insertStatement = session.prepare(builder.insertInto(keyspace, TABLE) | |
| 76 | + insertStatement = session.prepare(insertInto(keyspace, TABLE) | |
| 80 | 77 | .value("recipient", bindMarker()) |
| 81 | 78 | .value("time", bindMarker()) |
| 82 | 79 | .value("sender", bindMarker()) |
| 83 | 80 | .value("body", bindMarker())); |
| 84 | 81 | |
| 85 | - deleteStatement = session.prepare(builder.delete().from(keyspace, TABLE) | |
| 82 | + deleteStatement = session.prepare(delete().from(keyspace, TABLE) | |
| 86 | 83 | .where(eq("recipient", bindMarker()))); |
| 87 | 84 | |
| 88 | 85 | initialized = true; |
modified driver-examples/stress/src/main/java/com/datastax/driver/stress/Generators.java +2 −2
| @@ -124,7 +124,7 @@ | ||
| 124 | 124 | } |
| 125 | 125 | sb.append(" WHERE key = ").append(prefix | iteration); |
| 126 | 126 | ++iteration; |
| 127 | - return new QueryGenerator.Request.SimpleQuery(session.newSimpleStatement(sb.toString())); | |
| 127 | + return new QueryGenerator.Request.SimpleQuery(new SimpleStatement(sb.toString())); | |
| 128 | 128 | } |
| 129 | 129 | }; |
| 130 | 130 | } |
| @@ -196,7 +196,7 @@ | ||
| 196 | 196 | StringBuilder sb = new StringBuilder(); |
| 197 | 197 | sb.append("SELECT * FROM standard1 WHERE key = ").append(prefix | iteration); |
| 198 | 198 | ++iteration; |
| 199 | - return new QueryGenerator.Request.SimpleQuery(session.newSimpleStatement(sb.toString())); | |
| 199 | + return new QueryGenerator.Request.SimpleQuery(new SimpleStatement(sb.toString())); | |
| 200 | 200 | } |
| 201 | 201 | }; |
| 202 | 202 | } |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/guava/OptionalCodecTest.java +4 −3
| @@ -30,7 +30,6 @@ | ||
| 30 | 30 | import com.datastax.driver.core.*; |
| 31 | 31 | import com.datastax.driver.core.CCMBridge.PerClassSingleNodeCluster; |
| 32 | 32 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 33 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 34 | 33 | import com.datastax.driver.core.utils.CassandraVersion; |
| 35 | 34 | |
| 36 | 35 | import static com.datastax.driver.core.TypeCodec.bigint; |
| @@ -39,6 +38,8 @@ | ||
| 39 | 38 | import static com.datastax.driver.core.TypeCodec.varchar; |
| 40 | 39 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 41 | 40 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 41 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 42 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 42 | 43 | |
| 43 | 44 | public class OptionalCodecTest extends PerClassSingleNodeCluster { |
| 44 | 45 | |
| @@ -62,8 +63,8 @@ | ||
| 62 | 63 | |
| 63 | 64 | @BeforeMethod(groups = "short") |
| 64 | 65 | public void createBuiltStatements() throws Exception { |
| 65 | - insertStmt = new QueryBuilder(cluster).insertInto("foo").value("c1", bindMarker()).value("c2", bindMarker()).value("c3", bindMarker()); | |
| 66 | - selectStmt = new QueryBuilder(cluster).select("c2", "c3").from("foo").where(eq("c1", bindMarker())); | |
| 66 | + insertStmt = insertInto("foo").value("c1", bindMarker()).value("c2", bindMarker()).value("c3", bindMarker()); | |
| 67 | + selectStmt = select("c2", "c3").from("foo").where(eq("c1", bindMarker())); | |
| 67 | 68 | } |
| 68 | 69 | |
| 69 | 70 | /** |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/jdk8/OptionalCodecTest.java +4 −3
| @@ -30,7 +30,6 @@ | ||
| 30 | 30 | import com.datastax.driver.core.*; |
| 31 | 31 | import com.datastax.driver.core.CCMBridge.PerClassSingleNodeCluster; |
| 32 | 32 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 33 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 34 | 33 | import com.datastax.driver.core.utils.CassandraVersion; |
| 35 | 34 | |
| 36 | 35 | import static com.datastax.driver.core.TypeCodec.bigint; |
| @@ -39,6 +38,8 @@ | ||
| 39 | 38 | import static com.datastax.driver.core.TypeCodec.varchar; |
| 40 | 39 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 41 | 40 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 41 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 42 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 42 | 43 | |
| 43 | 44 | public class OptionalCodecTest extends PerClassSingleNodeCluster { |
| 44 | 45 | |
| @@ -62,8 +63,8 @@ | ||
| 62 | 63 | |
| 63 | 64 | @BeforeMethod(groups = "short") |
| 64 | 65 | public void createBuiltStatements() throws Exception { |
| 65 | - insertStmt = new QueryBuilder(cluster).insertInto("foo").value("c1", bindMarker()).value("c2", bindMarker()).value("c3", bindMarker()); | |
| 66 | - selectStmt = new QueryBuilder(cluster).select("c2", "c3").from("foo").where(eq("c1", bindMarker())); | |
| 66 | + insertStmt = insertInto("foo").value("c1", bindMarker()).value("c2", bindMarker()).value("c3", bindMarker()); | |
| 67 | + selectStmt = select("c2", "c3").from("foo").where(eq("c1", bindMarker())); | |
| 67 | 68 | } |
| 68 | 69 | |
| 69 | 70 | /** |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/json/JacksonJsonCodecTest.java +6 −6
| @@ -28,12 +28,12 @@ | ||
| 28 | 28 | |
| 29 | 29 | import com.datastax.driver.core.*; |
| 30 | 30 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 31 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 32 | 31 | import com.datastax.driver.core.utils.CassandraVersion; |
| 33 | -import com.datastax.driver.extras.codecs.json.JacksonJsonCodec; | |
| 34 | 32 | |
| 35 | 33 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 36 | 34 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 35 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 36 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 37 | 37 | |
| 38 | 38 | public class JacksonJsonCodecTest extends CCMBridge.PerClassSingleNodeCluster { |
| 39 | 39 | |
| @@ -97,11 +97,11 @@ | ||
| 97 | 97 | |
| 98 | 98 | @Test(groups = "short") |
| 99 | 99 | public void should_use_custom_codec_with_built_statements_1() { |
| 100 | - BuiltStatement insertStmt = new QueryBuilder(cluster).insertInto("t1") | |
| 100 | + BuiltStatement insertStmt = insertInto("t1") | |
| 101 | 101 | .value("c1", bindMarker()) |
| 102 | 102 | .value("c2", bindMarker()) |
| 103 | 103 | .value("c3", bindMarker()); |
| 104 | - BuiltStatement selectStmt = new QueryBuilder(cluster).select("c1", "c2", "c3") | |
| 104 | + BuiltStatement selectStmt = select("c1", "c2", "c3") | |
| 105 | 105 | .from("t1") |
| 106 | 106 | .where(eq("c1", bindMarker())) |
| 107 | 107 | .and(eq("c2", bindMarker())); |
| @@ -113,12 +113,12 @@ | ||
| 113 | 113 | |
| 114 | 114 | @Test(groups = "short") |
| 115 | 115 | public void should_use_custom_codec_with_built_statements_2() { |
| 116 | - BuiltStatement insertStmt = new QueryBuilder(cluster).insertInto("t1") | |
| 116 | + BuiltStatement insertStmt = insertInto("t1") | |
| 117 | 117 | .value("c1", notAJsonString) |
| 118 | 118 | .value("c2", alice) |
| 119 | 119 | .value("c3", bobAndCharlie); |
| 120 | 120 | BuiltStatement selectStmt = |
| 121 | - new QueryBuilder(cluster).select("c1", "c2", "c3") | |
| 121 | + select("c1", "c2", "c3") | |
| 122 | 122 | .from("t1") |
| 123 | 123 | .where(eq("c1", notAJsonString)) |
| 124 | 124 | .and(eq("c2", alice)); |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/json/Jsr353JsonCodecTest.java +6 −6
| @@ -28,12 +28,12 @@ | ||
| 28 | 28 | |
| 29 | 29 | import com.datastax.driver.core.*; |
| 30 | 30 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 31 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 32 | 31 | import com.datastax.driver.core.utils.CassandraVersion; |
| 33 | -import com.datastax.driver.extras.codecs.json.Jsr353JsonCodec; | |
| 34 | 32 | |
| 35 | 33 | import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; |
| 36 | 34 | import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; |
| 35 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; | |
| 36 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.select; | |
| 37 | 37 | |
| 38 | 38 | public class Jsr353JsonCodecTest extends CCMBridge.PerClassSingleNodeCluster { |
| 39 | 39 | |
| @@ -99,10 +99,10 @@ | ||
| 99 | 99 | |
| 100 | 100 | @Test(groups = "short", dataProvider = "Jsr353JsonCodecTest") |
| 101 | 101 | public void should_use_custom_codec_with_built_statements_1(JsonStructure object) throws IOException { |
| 102 | - BuiltStatement insertStmt = new QueryBuilder(cluster).insertInto("t1") | |
| 102 | + BuiltStatement insertStmt = insertInto("t1") | |
| 103 | 103 | .value("c1", bindMarker()) |
| 104 | 104 | .value("c2", bindMarker()); |
| 105 | - BuiltStatement selectStmt = new QueryBuilder(cluster).select("c1", "c2") | |
| 105 | + BuiltStatement selectStmt = select("c1", "c2") | |
| 106 | 106 | .from("t1") |
| 107 | 107 | .where(eq("c1", bindMarker())) |
| 108 | 108 | .and(eq("c2", bindMarker())); |
| @@ -114,11 +114,11 @@ | ||
| 114 | 114 | |
| 115 | 115 | @Test(groups = "short", dataProvider = "Jsr353JsonCodecTest") |
| 116 | 116 | public void should_use_custom_codec_with_built_statements_2(JsonStructure object) throws IOException { |
| 117 | - BuiltStatement insertStmt = new QueryBuilder(cluster).insertInto("t1") | |
| 117 | + BuiltStatement insertStmt = insertInto("t1") | |
| 118 | 118 | .value("c1", notAJsonString) |
| 119 | 119 | .value("c2", object); |
| 120 | 120 | BuiltStatement selectStmt = |
| 121 | - new QueryBuilder(cluster).select("c1", "c2") | |
| 121 | + select("c1", "c2") | |
| 122 | 122 | .from("t1") |
| 123 | 123 | .where(eq("c1", notAJsonString)) |
| 124 | 124 | .and(eq("c2", object)); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/QueryType.java +7 −9
| @@ -25,7 +25,6 @@ | ||
| 25 | 25 | import com.datastax.driver.core.TableMetadata; |
| 26 | 26 | import com.datastax.driver.core.querybuilder.Delete; |
| 27 | 27 | import com.datastax.driver.core.querybuilder.Insert; |
| 28 | -import com.datastax.driver.core.querybuilder.QueryBuilder; | |
| 29 | 28 | import com.datastax.driver.core.querybuilder.Select; |
| 30 | 29 | |
| 31 | 30 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| @@ -59,12 +58,11 @@ | ||
| 59 | 58 | } |
| 60 | 59 | |
| 61 | 60 | String makePreparedQueryString(TableMetadata table, EntityMapper<?> mapper, MappingManager manager, Set<ColumnMapper<?>> columns, Collection<Mapper.Option> options) { |
| 62 | - QueryBuilder builder = new QueryBuilder(manager.getSession().getCluster()); | |
| 63 | 61 | switch (kind) { |
| 64 | 62 | case SAVE: { |
| 65 | 63 | Insert insert = table == null |
| 66 | - ? builder.insertInto(mapper.getKeyspace(), mapper.getTable()) | |
| 67 | - : builder.insertInto(table); | |
| 64 | + ? insertInto(mapper.getKeyspace(), mapper.getTable()) | |
| 65 | + : insertInto(table); | |
| 68 | 66 | for (ColumnMapper<?> cm : columns) |
| 69 | 67 | if (cm.kind != ColumnMapper.Kind.COMPUTED) |
| 70 | 68 | insert.value(cm.getColumnName(), bindMarker()); |
| @@ -78,7 +76,7 @@ | ||
| 78 | 76 | return insert.toString(); |
| 79 | 77 | } |
| 80 | 78 | case GET: { |
| 81 | - Select.Selection selection = builder.select(); | |
| 79 | + Select.Selection selection = select(); | |
| 82 | 80 | for (ColumnMapper cm : mapper.allColumns()) { |
| 83 | 81 | Select.SelectionOrAlias column = (cm.kind == ColumnMapper.Kind.COMPUTED) |
| 84 | 82 | ? ((Select.SelectionOrAlias)selection).raw(cm.getColumnName()) |
| @@ -106,8 +104,8 @@ | ||
| 106 | 104 | } |
| 107 | 105 | case DEL: { |
| 108 | 106 | Delete delete = table == null |
| 109 | - ? builder.delete().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 110 | - : builder.delete().all().from(table); | |
| 107 | + ? delete().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 108 | + : delete().all().from(table); | |
| 111 | 109 | Delete.Where where = delete.where(); |
| 112 | 110 | for (int i = 0; i < mapper.primaryKeySize(); i++) |
| 113 | 111 | where.and(eq(mapper.getPrimaryKeyColumn(i).getColumnName(), bindMarker())); |
| @@ -122,8 +120,8 @@ | ||
| 122 | 120 | case SLICE: |
| 123 | 121 | case REVERSED_SLICE: { |
| 124 | 122 | Select select = table == null |
| 125 | - ? builder.select().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 126 | - : builder.select().all().from(table); | |
| 123 | + ? select().all().from(mapper.getKeyspace(), mapper.getTable()) | |
| 124 | + : select().all().from(table); | |
| 127 | 125 | Select.Where where = select.where(); |
| 128 | 126 | for (int i = 0; i < mapper.partitionKeys.size(); i++) |
| 129 | 127 | where.and(eq(mapper.partitionKeys.get(i).getColumnName(), bindMarker())); |
modified features/custom_codecs/README.md +2 −2
| @@ -171,9 +171,9 @@ | ||
| 171 | 171 | Session session = ... |
| 172 | 172 | MyPojo myPojo = ... |
| 173 | 173 | // Using SimpleStatement |
| 174 | -Statement stmt = session.newSimpleStatement("INSERT INTO t (id, json) VALUES (?, ?)", 42, myPojo)); | |
| 174 | +Statement stmt = new SimpleStatement("INSERT INTO t (id, json) VALUES (?, ?)", 42, myPojo)); | |
| 175 | 175 | // Using the Query Builder |
| 176 | -BuiltStatement insertStmt = new QueryBuilder(cluster).insertInto("t") | |
| 176 | +BuiltStatement insertStmt = QueryBuilder.insertInto("t") | |
| 177 | 177 | .value("id", 42) |
| 178 | 178 | .value("json", myPojo); |
| 179 | 179 | // Using BoundStatements |
modified features/custom_payloads/README.md +8 −8
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | in both cases, the payload will be sent along with the `PREPARE` or `QUERY` request respectively: |
| 72 | 72 | |
| 73 | 73 | ```java |
| 74 | -Statement statement = session.newSimpleStatement("SELECT foo FROM bar WHERE qix = 1"); | |
| 74 | +Statement statement = new SimpleStatement("SELECT foo FROM bar WHERE qix = 1"); | |
| 75 | 75 | statement.setOutgoingPayload(myCustomPayload); |
| 76 | 76 | session.execute(statement); // myCustomPayload will be sent with QUERY request |
| 77 | 77 | session.prepare(statement); // myCustomPayload will be sent with PREPARE request |
| @@ -88,7 +88,7 @@ | ||
| 88 | 88 | `BatchStatement` itself, *not* to internal statements: |
| 89 | 89 | |
| 90 | 90 | ```java |
| 91 | -Statement statement = session.newSimpleStatement("INSERT INTO t1 (c1, c2) values ('foo', 'bar')") | |
| 91 | +Statement statement = new SimpleStatement("INSERT INTO t1 (c1, c2) values ('foo', 'bar')") | |
| 92 | 92 | // correct |
| 93 | 93 | Statement batch = new BatchStatement().add(statement); |
| 94 | 94 | batch.setOutgoingPayload(myCustomPayload); |
| @@ -104,7 +104,7 @@ | ||
| 104 | 104 | sent over and over for every new page request*. |
| 105 | 105 | |
| 106 | 106 | ```java |
| 107 | -Statement statement = session.newSimpleStatement("..."); | |
| 107 | +Statement statement = new SimpleStatement("..."); | |
| 108 | 108 | statement.setOutgoingPayload(myCustomPayload); |
| 109 | 109 | ResultSet rows = session.execute(...); // myCustomPayload will be sent with first QUERY request |
| 110 | 110 | for (Row row : rows) { |
| @@ -148,7 +148,7 @@ | ||
| 148 | 148 | 1) From a `ResultSet`: use the `ExecutionInfo` class to retrieve the payload sent by the server. |
| 149 | 149 | |
| 150 | 150 | ```java |
| 151 | -Statement statement = session.newSimpleStatement("..."); | |
| 151 | +Statement statement = new SimpleStatement("..."); | |
| 152 | 152 | ResultSet rows = session.execute(...); |
| 153 | 153 | // last payload sent by the server |
| 154 | 154 | Map<String,ByteBuffer> serverPayload = rows.getExecutionInfo().getIncomingPayload(); |
| @@ -160,7 +160,7 @@ | ||
| 160 | 160 | payloads for all `RESULT` responses, use the following method instead: |
| 161 | 161 | |
| 162 | 162 | ```java |
| 163 | -Statement statement = session.newSimpleStatement("..."); | |
| 163 | +Statement statement = new SimpleStatement("..."); | |
| 164 | 164 | ResultSet rows = session.execute(...); |
| 165 | 165 | //all payloads sent by the server |
| 166 | 166 | for (ExecutionInfo info : rows.getAllExecutionInfo()) { |
| @@ -183,7 +183,7 @@ | ||
| 183 | 183 | payload, if any, will become the default outgoing payload for the prepared statement. |
| 184 | 184 | |
| 185 | 185 | ```java |
| 186 | -Statement statement = session.newSimpleStatement("..."); | |
| 186 | +Statement statement = new SimpleStatement("..."); | |
| 187 | 187 | statement.setOutgoingPayload(myCustomPayload) |
| 188 | 188 | PreparedStatement ps = session.prepare(...); // myCustomPayload will be sent with PREPARE request |
| 189 | 189 | // AND become the default outgoing payload |
| @@ -194,7 +194,7 @@ | ||
| 194 | 194 | with the `PREPARE` request: |
| 195 | 195 | |
| 196 | 196 | ```java |
| 197 | -Statement statement = session.newSimpleStatement("..."); | |
| 197 | +Statement statement = new SimpleStatement("..."); | |
| 198 | 198 | PreparedStatement ps = session.prepare(...); // no payload sent with PREPARE request |
| 199 | 199 | ps.setOutgoingPayload(myCustomPayload) // default outgoing payload is now myCustomPayload |
| 200 | 200 | ``` |
| @@ -218,7 +218,7 @@ | ||
| 218 | 218 | like in the example below: |
| 219 | 219 | |
| 220 | 220 | ```java |
| 221 | -Statement statement = session.newSimpleStatement("..."); | |
| 221 | +Statement statement = new SimpleStatement("..."); | |
| 222 | 222 | PreparedStatement ps = session.prepare(...); // no payload sent with PREPARE request |
| 223 | 223 | BoundStatement bs = ps.bind(1); // no outgoing payload by default |
| 224 | 224 | bs.setOutgoingPayload(myCustomPayload); // outgoing payload for this specific statement |
modified features/paging/README.md +3 −3
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | The fetch size can also be set on a statement: |
| 26 | 26 | |
| 27 | 27 | ```java |
| 28 | -Statement statement = session.newSimpleStatement("your query"); | |
| 28 | +Statement statement = new SimpleStatement("your query"); | |
| 29 | 29 | statement.setFetchSize(2000); |
| 30 | 30 | ``` |
| 31 | 31 | |
| @@ -130,7 +130,7 @@ | ||
| 130 | 130 | |
| 131 | 131 | ```java |
| 132 | 132 | PagingState pagingState = PagingState.fromString(string); |
| 133 | -Statement st = session.newSimpleStatement("your query"); | |
| 133 | +Statement st = new SimpleStatement("your query"); | |
| 134 | 134 | st.setPagingState(pagingState); |
| 135 | 135 | ResultSet rs = session.execute(st); |
| 136 | 136 | ``` |
| @@ -147,7 +147,7 @@ | ||
| 147 | 147 | ```java |
| 148 | 148 | final int RESULTS_PER_PAGE = 100; |
| 149 | 149 | |
| 150 | -Statement st = session.newSimpleStatement("your query"); | |
| 150 | +Statement st = new SimpleStatement("your query"); | |
| 151 | 151 | st.setFetchSize(RESULTS_PER_PAGE); |
| 152 | 152 | |
| 153 | 153 | String requestedPage = extractPagingStateStringFromURL(); |
modified features/query_timestamps/README.md +1 −1
| @@ -36,7 +36,7 @@ | ||
| 36 | 36 | per-statement basis: |
| 37 | 37 | |
| 38 | 38 | ```java |
| 39 | -Statement statement = session.newSimpleStatement( | |
| 39 | +Statement statement = new SimpleStatement( | |
| 40 | 40 | "UPDATE users SET email = 'x@y.com' where id = 1"); |
| 41 | 41 | statement.setDefaultTimestamp(1234567890); |
| 42 | 42 | session.execute(statement); |
modified features/speculative_execution/README.md +1 −1
| @@ -92,7 +92,7 @@ | ||
| 92 | 92 | You can override the value on each statement: |
| 93 | 93 | |
| 94 | 94 | ```java |
| 95 | -Statement s = session.newSimpleStatement("SELECT * FROM users WHERE id = 1"); | |
| 95 | +Statement s = new SimpleStatement("SELECT * FROM users WHERE id = 1"); | |
| 96 | 96 | s.setIdempotent(true); |
| 97 | 97 | ``` |
| 98 | 98 |
modified upgrade_guide/README.md +5 −12
| @@ -42,19 +42,12 @@ | ||
| 42 | 42 | * `<V> T set(String name, V v, Class<V> targetClass)` |
| 43 | 43 | * `<V> T set(String name, V v, TypeToken<V> targetType)` |
| 44 | 44 | * `<V> T set(String name, V v, TypeCodec<V> codec)` |
| 45 | - * `Session`. The following methods were added: | |
| 46 | - * `newSimpleStatement(String query)` | |
| 47 | - * `newSimpleStatement(String query, Object... values)` | |
| 45 | + * `Statement`. The following public methods were modified: | |
| 46 | + * `getRoutingKey(ProtocolVersion, CodecRegistry)`: both parameters added. | |
| 48 | 47 | * `RegularStatement`. The following public methods were modified: |
| 49 | - * `getValues()` | |
| 50 | - * `hasValues()` | |
| 51 | - * `SimpleStatement`. Public constructors were removed; users are required to call either: | |
| 52 | - * `Session.newSimpleStatement(String query)` or | |
| 53 | - * `Session.newSimpleStatement(String query, Object... values)` | |
| 54 | - * `QueryBuilder`. This class now has a single, public constructor: `QueryBuilder(Cluster cluster)`. | |
| 55 | - Its fluent API has also changed; instead of e.g.: `QueryBuilder.select(...)`, | |
| 56 | - users should now use the following idiom: `new QueryBuilder(cluster).select(...)` | |
| 57 | - All methods that start a query are now instance methods instead of static ones. | |
| 48 | + * `getValues(ProtocolVersion, CodecRegistry)`: second parameter added. | |
| 49 | + * `getQueryString(CodecRegistry)` and `hasValues(CodecRegistry)`: parameter added. No-arg versions are still present | |
| 50 | + and use the default codec registry; refer to the Javadocs for guidance on which version to use. | |
| 58 | 51 | * `PreparedStatement`. The following public method was added: |
| 59 | 52 | * `CodecRegistry getCodecRegistry()`. |
| 60 | 53 | * `TupleType`. The following public method was deleted: |