Elffuss BraidHub

kikocis/java-driver public

Provide "disconnected" constructors for QueryBuilder and TupleType.
Changeset4ae4b04a5f43d2e36beaf967c328dcb6f3378d8ef0bf28df863b370d452ca686
Authorolim7t <omichallat+github@gmail.com> · 2015-08-17 12:34 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-17 15:01 +02:00
Parentsa22272b12861

Browse the files at this changeset

9 files changed

modified driver-core/src/main/java/com/datastax/driver/core/TupleType.java +21 −0
@@ -40,6 +40,27 @@
4040 this.codecRegistry = codecRegistry;
4141 }
4242
43+ /**
44+ * Creates a "disconnected" tuple type (<b>you should prefer
45+ * {@link Metadata#newTupleType(DataType...) cluster.getMetadata().newTupleType(...)}
46+ * whenever possible</b>).
47+ * <p>
48+ * This method is only exposed for situations where you don't have a {@code Cluster}
49+ * instance available. If you create a type with this method and use it with a
50+ * {@code Cluster} later, you won't be able to set tuple fields with custom codecs
51+ * registered against the cluster, or you might get errors if the protocol versions don't
52+ * match.
53+ *
54+ * @param protocolVersion the protocol version to use.
55+ * @param codecRegistry the codec registry to use.
56+ * @param types the types for the tuple type.
57+ *
58+ * @return the newly created tuple type.
59+ */
60+ public static TupleType of(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, DataType... types) {
61+ return new TupleType(Arrays.asList(types), protocolVersion, codecRegistry);
62+ }
63+
4364 /**
4465 * The (immutable) list of types composing this tuple type.
4566 *
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +2 −2
@@ -33,8 +33,8 @@
3333 // Only used when we add at last one statement that is not a BuiltStatement subclass
3434 private int nonBuiltStatementValues;
3535
36- Batch(Cluster cluster, RegularStatement[] statements, boolean logged) {
37- super((String)null, cluster);
36+ Batch(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, RegularStatement[] statements, boolean logged) {
37+ super((String)null, protocolVersion, codecRegistry);
3838 this.statements = statements.length == 0
3939 ? new ArrayList<RegularStatement>()
4040 : new ArrayList<RegularStatement>(statements.length);
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +11 −14
@@ -37,7 +37,8 @@
3737 private final List<ColumnMetadata> partitionKey;
3838 private final List<Object> routingKeyValues;
3939 final String keyspace;
40- protected final Cluster cluster;
40+ private final ProtocolVersion protocolVersion;
41+ private final CodecRegistry codecRegistry;
4142
4243 private boolean dirty;
4344 private String cache;
@@ -50,18 +51,20 @@
5051 boolean hasBindMarkers;
5152 private boolean forceNoValues;
5253
53- BuiltStatement(String keyspace, Cluster cluster) {
54+ BuiltStatement(String keyspace, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
5455 this.partitionKey = null;
5556 this.routingKeyValues = null;
5657 this.keyspace = keyspace;
57- this.cluster = cluster;
58+ this.protocolVersion = protocolVersion;
59+ this.codecRegistry = codecRegistry;
5860 }
5961
60- BuiltStatement(TableMetadata tableMetadata, Cluster cluster) {
62+ BuiltStatement(TableMetadata tableMetadata, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
6163 this.partitionKey = tableMetadata.getPartitionKey();
6264 this.routingKeyValues = Arrays.asList(new Object[tableMetadata.getPartitionKey().size()]);
6365 this.keyspace = escapeId(tableMetadata.getKeyspace().getName());
64- this.cluster = cluster;
66+ this.protocolVersion = protocolVersion;
67+ this.codecRegistry = codecRegistry;
6568 }
6669
6770 // Same as Metadata.escapeId, but we don't have access to it here.
@@ -160,19 +163,13 @@
160163 }
161164
162165 CodecRegistry getCodecRegistry(){
163- return cluster.getConfiguration().getCodecRegistry();
166+ return codecRegistry;
164167 }
165168
166169 ProtocolVersion getProtocolVersion() {
167- cluster.init();
168- return cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
170+ return protocolVersion;
169171 }
170172
171- /**
172- * {@inheritDoc}
173- * <p>
174- * Note: Calling this method may trigger the underlying {@link Cluster} initialization.
175- */
176173 @Override
177174 public ByteBuffer getRoutingKey() {
178175 if (routingKeyValues == null)
@@ -278,7 +275,7 @@
278275 T statement;
279276
280277 ForwardingStatement(T statement) {
281- super((String)null, statement.cluster);
278+ super((String)null, statement.getProtocolVersion(), statement.getCodecRegistry());
282279 this.statement = statement;
283280 }
284281
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +17 −13
@@ -20,6 +20,7 @@
2020
2121 import com.datastax.driver.core.Cluster;
2222 import com.datastax.driver.core.CodecRegistry;
23+import com.datastax.driver.core.ProtocolVersion;
2324 import com.datastax.driver.core.TableMetadata;
2425
2526 /**
@@ -34,8 +35,8 @@
3435 private final Conditions conditions;
3536 private boolean ifExists;
3637
37- Delete(Cluster cluster, String keyspace, String table, List<Selector> columns) {
38- super(keyspace, cluster);
38+ Delete(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table, List<Selector> columns) {
39+ super(keyspace, protocolVersion, codecRegistry);
3940 this.table = table;
4041 this.columns = columns;
4142 this.where = new Where(this);
@@ -43,8 +44,8 @@
4344 this.conditions = new Conditions(this);
4445 }
4546
46- Delete(Cluster cluster, TableMetadata table, List<Selector> columns) {
47- super(table, cluster);
47+ Delete(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table, List<Selector> columns) {
48+ super(table, protocolVersion, codecRegistry);
4849 this.table = escapeId(table.getName());
4950 this.columns = columns;
5051 this.where = new Where(this);
@@ -277,16 +278,19 @@
277278 */
278279 public static class Builder {
279280
280- private final Cluster cluster;
281+ private final ProtocolVersion protocolVersion;
282+ private final CodecRegistry codecRegistry;
281283
282284 List<Selector> columns = new ArrayList<Selector>();
283285
284- Builder(Cluster cluster) {
285- this.cluster = cluster;
286+ Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
287+ this.protocolVersion = protocolVersion;
288+ this.codecRegistry = codecRegistry;
286289 }
287290
288- Builder(Cluster cluster, String... columnNames) {
289- this.cluster = cluster;
291+ Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String... columnNames) {
292+ this.protocolVersion = protocolVersion;
293+ this.codecRegistry = codecRegistry;
290294 for (String columnName : columnNames) {
291295 this.columns.add(new Selector(columnName));
292296 }
@@ -310,7 +314,7 @@
310314 * @return a newly built DELETE statement that deletes from {@code keyspace.table}.
311315 */
312316 public Delete from(String keyspace, String table) {
313- return new Delete(cluster, keyspace, table, columns);
317+ return new Delete(protocolVersion, codecRegistry, keyspace, table, columns);
314318 }
315319
316320 /**
@@ -320,7 +324,7 @@
320324 * @return a newly built DELETE statement that deletes from {@code table}.
321325 */
322326 public Delete from(TableMetadata table) {
323- return new Delete(cluster, table, columns);
327+ return new Delete(protocolVersion, codecRegistry, table, columns);
324328 }
325329 }
326330
@@ -329,8 +333,8 @@
329333 */
330334 public static class Selection extends Builder {
331335
332- Selection(Cluster cluster) {
333- super(cluster);
336+ Selection(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
337+ super(protocolVersion, codecRegistry);
334338 }
335339
336340 /**
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +5 −4
@@ -21,6 +21,7 @@
2121
2222 import com.datastax.driver.core.Cluster;
2323 import com.datastax.driver.core.CodecRegistry;
24+import com.datastax.driver.core.ProtocolVersion;
2425 import com.datastax.driver.core.TableMetadata;
2526
2627 /**
@@ -34,14 +35,14 @@
3435 private final Options usings;
3536 private boolean ifNotExists;
3637
37- Insert(Cluster cluster, String keyspace, String table) {
38- super(keyspace, cluster);
38+ Insert(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table) {
39+ super(keyspace, protocolVersion, codecRegistry);
3940 this.table = table;
4041 this.usings = new Options(this);
4142 }
4243
43- Insert(Cluster cluster, TableMetadata table) {
44- super(table, cluster);
44+ Insert(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table) {
45+ super(table, protocolVersion, codecRegistry);
4546 this.table = escapeId(table.getName());
4647 this.usings = new Options(this);
4748 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +43 −22
@@ -17,9 +17,7 @@
1717
1818 import java.util.*;
1919
20-import com.datastax.driver.core.Cluster;
21-import com.datastax.driver.core.RegularStatement;
22-import com.datastax.driver.core.TableMetadata;
20+import com.datastax.driver.core.*;
2321 import com.datastax.driver.core.exceptions.InvalidQueryException;
2422
2523 /**
@@ -40,17 +38,40 @@
4038 */
4139 public final class QueryBuilder {
4240
43- private final Cluster cluster;
41+ private final ProtocolVersion protocolVersion;
42+ private final CodecRegistry codecRegistry;
4443
4544 /**
4645 * Create a new QueryBuilder instance and associate it with the given {@link Cluster}.
47- * Note: if the {@link Cluster} is not yet initialized, certain methods of the QueryBuilder API
48- * may trigger its initialization.
46+ * Note: if the {@link Cluster} is not yet initialized, this method
47+ * will trigger its initialization.
4948 *
5049 * @param cluster The {@link Cluster} instance this object should be attached to.
5150 */
5251 public QueryBuilder(Cluster cluster) {
53- this.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;
5475 }
5576
5677 /**
@@ -63,7 +84,7 @@
6384 * least a FROM clause to complete the query).
6485 */
6586 public Select.Builder select(String... columns) {
66- return new Select.Builder(cluster, Arrays.asList((Object[])columns));
87+ return new Select.Builder(protocolVersion, codecRegistry, Arrays.asList((Object[])columns));
6788 }
6889
6990 /**
@@ -74,7 +95,7 @@
7495 */
7596 public Select.Selection select() {
7697 // Note: the fact we return Select.Selection as return type is on purpose.
77- return new Select.SelectionOrAlias(cluster);
98+ return new Select.SelectionOrAlias(protocolVersion, codecRegistry);
7899 }
79100
80101 /**
@@ -84,7 +105,7 @@
84105 * @return an in-construction INSERT query.
85106 */
86107 public Insert insertInto(String table) {
87- return new Insert(cluster, null, table);
108+ return new Insert(protocolVersion, codecRegistry, null, table);
88109 }
89110
90111 /**
@@ -95,7 +116,7 @@
95116 * @return an in-construction INSERT query.
96117 */
97118 public Insert insertInto(String keyspace, String table) {
98- return new Insert(cluster, keyspace, table);
119+ return new Insert(protocolVersion, codecRegistry, keyspace, table);
99120 }
100121
101122 /**
@@ -105,7 +126,7 @@
105126 * @return an in-construction INSERT query.
106127 */
107128 public Insert insertInto(TableMetadata table) {
108- return new Insert(cluster, table);
129+ return new Insert(protocolVersion, codecRegistry, table);
109130 }
110131
111132 /**
@@ -116,7 +137,7 @@
116137 * clause needs to be provided to complete the query).
117138 */
118139 public Update update(String table) {
119- return new Update(cluster, null, table);
140+ return new Update(protocolVersion, codecRegistry, null, table);
120141 }
121142
122143 /**
@@ -128,7 +149,7 @@
128149 * clause needs to be provided to complete the query).
129150 */
130151 public Update update(String keyspace, String table) {
131- return new Update(cluster, keyspace, table);
152+ return new Update(protocolVersion, codecRegistry, keyspace, table);
132153 }
133154
134155 /**
@@ -139,7 +160,7 @@
139160 * clause needs to be provided to complete the query).
140161 */
141162 public Update update(TableMetadata table) {
142- return new Update(cluster, table);
163+ return new Update(protocolVersion, codecRegistry, table);
143164 }
144165
145166 /**
@@ -150,7 +171,7 @@
150171 * clause needs to be provided to complete the query).
151172 */
152173 public Delete.Builder delete(String... columns) {
153- return new Delete.Builder(cluster, columns);
174+ return new Delete.Builder(protocolVersion, codecRegistry, columns);
154175 }
155176
156177 /**
@@ -161,7 +182,7 @@
161182 * query).
162183 */
163184 public Delete.Selection delete() {
164- return new Delete.Selection(cluster);
185+ return new Delete.Selection(protocolVersion, codecRegistry);
165186 }
166187
167188 /**
@@ -178,7 +199,7 @@
178199 * @return a new {@code RegularStatement} that batch {@code statements}.
179200 */
180201 public Batch batch(RegularStatement... statements) {
181- return new Batch(cluster, statements, true);
202+ return new Batch(protocolVersion, codecRegistry, statements, true);
182203 }
183204
184205 /**
@@ -199,7 +220,7 @@
199220 * using the batch log.
200221 */
201222 public Batch unloggedBatch(RegularStatement... statements) {
202- return new Batch(cluster, statements, false);
223+ return new Batch(protocolVersion, codecRegistry, statements, false);
203224 }
204225
205226 /**
@@ -209,7 +230,7 @@
209230 * @return the truncation query.
210231 */
211232 public Truncate truncate(String table) {
212- return new Truncate(cluster, null, table);
233+ return new Truncate(protocolVersion, codecRegistry, null, table);
213234 }
214235
215236 /**
@@ -220,7 +241,7 @@
220241 * @return the truncation query.
221242 */
222243 public Truncate truncate(String keyspace, String table) {
223- return new Truncate(cluster, keyspace, table);
244+ return new Truncate(protocolVersion, codecRegistry, keyspace, table);
224245 }
225246
226247 /**
@@ -230,7 +251,7 @@
230251 * @return the truncation query.
231252 */
232253 public Truncate truncate(TableMetadata table) {
233- return new Truncate(cluster, table);
254+ return new Truncate(protocolVersion, codecRegistry, table);
234255 }
235256
236257 /**
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +19 −15
@@ -22,6 +22,7 @@
2222
2323 import com.datastax.driver.core.Cluster;
2424 import com.datastax.driver.core.CodecRegistry;
25+import com.datastax.driver.core.ProtocolVersion;
2526 import com.datastax.driver.core.TableMetadata;
2627
2728 /**
@@ -39,16 +40,16 @@
3940 private Object limit;
4041 private boolean allowFiltering;
4142
42- Select(Cluster cluster, String keyspace, String table, List<Object> columnNames, boolean isDistinct) {
43- super(keyspace, cluster);
43+ Select(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table, List<Object> columnNames, boolean isDistinct) {
44+ super(keyspace, protocolVersion, codecRegistry);
4445 this.table = table;
4546 this.isDistinct = isDistinct;
4647 this.columnNames = columnNames;
4748 this.where = new Where(this);
4849 }
4950
50- Select(Cluster cluster, TableMetadata table, List<Object> columnNames, boolean isDistinct) {
51- super(table, cluster);
51+ Select(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table, List<Object> columnNames, boolean isDistinct) {
52+ super(table, protocolVersion, codecRegistry);
5253 this.table = escapeId(table.getName());
5354 this.isDistinct = isDistinct;
5455 this.columnNames = columnNames;
@@ -257,16 +258,19 @@
257258 */
258259 public static class Builder {
259260
260- private final Cluster cluster;
261+ private final ProtocolVersion protocolVersion;
262+ private final CodecRegistry codecRegistry;
261263 List<Object> columnNames;
262264 boolean isDistinct;
263265
264- Builder(Cluster cluster) {
265- this.cluster = cluster;
266+ Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
267+ this.protocolVersion = protocolVersion;
268+ this.codecRegistry = codecRegistry;
266269 }
267270
268- Builder(Cluster cluster, List<Object> columnNames) {
269- this.cluster = cluster;
271+ Builder(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, List<Object> columnNames) {
272+ this.protocolVersion = protocolVersion;
273+ this.codecRegistry = codecRegistry;
270274 this.columnNames = columnNames;
271275 }
272276
@@ -298,7 +302,7 @@
298302 * @return a newly built SELECT statement that selects from {@code keyspace.table}.
299303 */
300304 public Select from(String keyspace, String table) {
301- return new Select(cluster, keyspace, table, columnNames, isDistinct);
305+ return new Select(protocolVersion, codecRegistry, keyspace, table, columnNames, isDistinct);
302306 }
303307
304308 /**
@@ -308,7 +312,7 @@
308312 * @return a newly built SELECT statement that selects from {@code table}.
309313 */
310314 public Select from(TableMetadata table) {
311- return new Select(cluster, table, columnNames, isDistinct);
315+ return new Select(protocolVersion, codecRegistry, table, columnNames, isDistinct);
312316 }
313317 }
314318
@@ -317,8 +321,8 @@
317321 */
318322 public static abstract class Selection extends Builder {
319323
320- Selection(Cluster cluster) {
321- super(cluster);
324+ Selection(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
325+ super(protocolVersion, codecRegistry);
322326 }
323327
324328 /**
@@ -405,8 +409,8 @@
405409
406410 private Object previousSelection;
407411
408- SelectionOrAlias(Cluster cluster) {
409- super(cluster);
412+ SelectionOrAlias(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
413+ super(protocolVersion, codecRegistry);
410414 }
411415
412416 /**
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +6 −4
@@ -18,6 +18,8 @@
1818 import java.util.List;
1919
2020 import com.datastax.driver.core.Cluster;
21+import com.datastax.driver.core.CodecRegistry;
22+import com.datastax.driver.core.ProtocolVersion;
2123 import com.datastax.driver.core.TableMetadata;
2224
2325 /**
@@ -27,13 +29,13 @@
2729
2830 private final String table;
2931
30- Truncate(Cluster cluster, String keyspace, String table) {
31- super(keyspace, cluster);
32+ Truncate(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table) {
33+ super(keyspace, protocolVersion, codecRegistry);
3234 this.table = table;
3335 }
3436
35- Truncate(Cluster cluster, TableMetadata table) {
36- super(table, cluster);
37+ Truncate(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table) {
38+ super(table, protocolVersion, codecRegistry);
3739 this.table = escapeId(table.getName());
3840 }
3941
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +5 −4
@@ -20,6 +20,7 @@
2020
2121 import com.datastax.driver.core.Cluster;
2222 import com.datastax.driver.core.CodecRegistry;
23+import com.datastax.driver.core.ProtocolVersion;
2324 import com.datastax.driver.core.TableMetadata;
2425 import com.datastax.driver.core.querybuilder.Assignment.CounterAssignment;
2526
@@ -34,8 +35,8 @@
3435 private final Options usings;
3536 private final Conditions conditions;
3637
37- Update(Cluster cluster, String keyspace, String table) {
38- super(keyspace, cluster);
38+ Update(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String keyspace, String table) {
39+ super(keyspace, protocolVersion, codecRegistry);
3940 this.table = table;
4041 this.assignments = new Assignments(this);
4142 this.where = new Where(this);
@@ -43,8 +44,8 @@
4344 this.conditions = new Conditions(this);
4445 }
4546
46- Update(Cluster cluster, TableMetadata table) {
47- super(table, cluster);
47+ Update(ProtocolVersion protocolVersion, CodecRegistry codecRegistry, TableMetadata table) {
48+ super(table, protocolVersion, codecRegistry);
4849 this.table = escapeId(table.getName());
4950 this.assignments = new Assignments(this);
5051 this.where = new Where(this);