kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5dad311f50939bb08887b9cab34ed04f44e5e0aa4c2b973bf968269279501252 |
|---|---|
| Author | Kevin Gallardo <kevingallardo@Kevins-MacBook-Pro.local> · 2015-03-05 16:47 UTC |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-06-29 14:32 +02:00 |
| Parents | c0b5f31a435c |
5 files changed
modified changelog/README.md +1 −0
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | - [bug] Preserve default timestamp for retries and speculative executions (JAVA-724) |
| 19 | 19 | - [improvement] Use same pool implementation for protocol v2 and v3 |
| 20 | 20 | (JAVA-738). |
| 21 | +- [improvement] Support CONTAINS / CONTAINS KEY in QueryBuilder (JAVA-677) | |
| 21 | 22 | |
| 22 | 23 | Merged from 2.0 branch: |
| 23 | 24 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +60 −0
| @@ -110,6 +110,66 @@ | ||
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | + static class ContainsClause extends AbstractClause { | |
| 114 | + | |
| 115 | + private final Object value; | |
| 116 | + | |
| 117 | + ContainsClause(String name, Object value) { | |
| 118 | + super(name); | |
| 119 | + this.value = value; | |
| 120 | + | |
| 121 | + if (value == null) | |
| 122 | + throw new IllegalArgumentException("Missing value for CONTAINS clause"); | |
| 123 | + } | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 127 | + Utils.appendName(name, sb).append(" CONTAINS "); | |
| 128 | + Utils.appendValue(value, sb, variables); | |
| 129 | + } | |
| 130 | + | |
| 131 | + @Override | |
| 132 | + Object firstValue() { | |
| 133 | + return value; | |
| 134 | + } | |
| 135 | + | |
| 136 | + @Override | |
| 137 | + boolean containsBindMarker() { | |
| 138 | + return Utils.containsBindMarker(value); | |
| 139 | + } | |
| 140 | + } | |
| 141 | + | |
| 142 | + | |
| 143 | + static class ContainsKeyClause extends AbstractClause { | |
| 144 | + | |
| 145 | + private final Object value; | |
| 146 | + | |
| 147 | + ContainsKeyClause(String name, Object value) { | |
| 148 | + super(name); | |
| 149 | + this.value = value; | |
| 150 | + | |
| 151 | + if (value == null) | |
| 152 | + throw new IllegalArgumentException("Missing value for CONTAINS KEY clause"); | |
| 153 | + } | |
| 154 | + | |
| 155 | + @Override | |
| 156 | + void appendTo(StringBuilder sb, List<Object> variables) { | |
| 157 | + Utils.appendName(name, sb).append(" CONTAINS KEY "); | |
| 158 | + Utils.appendValue(value, sb, variables); | |
| 159 | + } | |
| 160 | + | |
| 161 | + @Override | |
| 162 | + Object firstValue() { | |
| 163 | + return value; | |
| 164 | + } | |
| 165 | + | |
| 166 | + @Override | |
| 167 | + boolean containsBindMarker() { | |
| 168 | + return Utils.containsBindMarker(value); | |
| 169 | + } | |
| 170 | + } | |
| 171 | + | |
| 172 | + | |
| 113 | 173 | static class CompoundClause extends Clause { |
| 114 | 174 | private String op; |
| 115 | 175 | private final List<String> names; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +24 −0
| @@ -296,6 +296,30 @@ | ||
| 296 | 296 | return new Clause.InClause(name, values); |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | + /** | |
| 300 | + * Creates a "contains" where clause stating the provided column must contain | |
| 301 | + * the value provided. | |
| 302 | + * | |
| 303 | + * @param name the column name | |
| 304 | + * @param value the value | |
| 305 | + * @return the corresponding where clause. | |
| 306 | + */ | |
| 307 | + public static Clause contains(String name, Object value) { | |
| 308 | + return new Clause.ContainsClause(name, value); | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Creates a "contains key" where clause stating the provided column must contain | |
| 313 | + * the key provided. | |
| 314 | + * | |
| 315 | + * @param name the column name | |
| 316 | + * @param key the key | |
| 317 | + * @return the corresponding where clause. | |
| 318 | + */ | |
| 319 | + public static Clause containsKey(String name, Object key) { | |
| 320 | + return new Clause.ContainsKeyClause(name, key); | |
| 321 | + } | |
| 322 | + | |
| 299 | 323 | /** |
| 300 | 324 | * Creates a "lesser than" where clause stating the provided column must be less than |
| 301 | 325 | * the provided value. |
added driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilder21ExecutionTest.java +109 −0
| @@ -0,0 +1,109 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.querybuilder; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.CCMBridge; | |
| 19 | +import com.datastax.driver.core.PreparedStatement; | |
| 20 | +import com.datastax.driver.core.ResultSet; | |
| 21 | +import com.datastax.driver.core.Row; | |
| 22 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 23 | +import org.testng.annotations.Test; | |
| 24 | + | |
| 25 | +import java.util.Arrays; | |
| 26 | +import java.util.Collection; | |
| 27 | + | |
| 28 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 29 | +import static com.datastax.driver.core.querybuilder.QueryBuilder.*; | |
| 30 | + | |
| 31 | +@CassandraVersion(major=2.1) | |
| 32 | +public class QueryBuilder21ExecutionTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 33 | + | |
| 34 | + @Override | |
| 35 | + protected Collection<String> getTableDefinitions() { | |
| 36 | + // Taken from http://www.datastax.com/dev/blog/cql-in-2-1 | |
| 37 | + return Arrays.asList( | |
| 38 | + "CREATE TABLE products (id int PRIMARY KEY, description text, price int, categories set<text>, buyers list<int>, features_keys map<text, text>, features_values map<text, text>)", | |
| 39 | + "CREATE INDEX cat_index ON products(categories)", | |
| 40 | + "CREATE INDEX buyers_index ON products(buyers)", | |
| 41 | + "CREATE INDEX feat_index ON products(features_values)", | |
| 42 | + "CREATE INDEX feat_key_index ON products(KEYS(features_keys))", | |
| 43 | + "INSERT INTO products(id, description, price, categories, buyers, features_keys, features_values) " + | |
| 44 | + "VALUES (34134, '120-inch 1080p 3D plasma TV', 9999, {'tv', '3D', 'hdtv'}, [1], {'screen' : '120-inch', 'refresh-rate' : '400hz', 'techno' : 'plasma'}, {'screen' : '120-inch', 'refresh-rate' : '400hz', 'techno' : 'plasma'})", | |
| 45 | + "INSERT INTO products(id, description, price, categories, buyers, features_keys, features_values) " + | |
| 46 | + "VALUES (29412, '32-inch LED HDTV (black)', 929, {'tv', 'hdtv'}, [1,2,3], {'screen' : '32-inch', 'techno' : 'LED'}, {'screen' : '32-inch', 'techno' : 'LED'})", | |
| 47 | + "INSERT INTO products(id, description, price, categories, buyers, features_keys, features_values) " + | |
| 48 | + "VALUES (38471, '32-inch LCD TV', 110, {'tv', 'used'}, [2,4], {'screen' : '32-inch', 'techno' : 'LCD'}, {'screen' : '32-inch', 'techno' : 'LCD'})" | |
| 49 | + ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Test(groups="short") | |
| 53 | + public void should_handle_contains_on_set_with_index() { | |
| 54 | + PreparedStatement byCategory = session.prepare(select("id", "description", "categories") | |
| 55 | + .from("products") | |
| 56 | + .where(contains("categories", bindMarker("category")))); | |
| 57 | + | |
| 58 | + ResultSet results = session.execute(byCategory.bind().setString("category", "hdtv")); | |
| 59 | + | |
| 60 | + assertThat(results.getAvailableWithoutFetching()).isEqualTo(2); | |
| 61 | + for(Row row : results) { | |
| 62 | + assertThat(row.getSet("categories", String.class)).contains("hdtv"); | |
| 63 | + } | |
| 64 | + } | |
| 65 | + | |
| 66 | + @Test(groups="short") | |
| 67 | + public void should_handle_contains_on_list_with_index() { | |
| 68 | + PreparedStatement byBuyer = session.prepare(select("id", "description", "buyers") | |
| 69 | + .from("products") | |
| 70 | + .where(contains("buyers", bindMarker("buyer")))); | |
| 71 | + | |
| 72 | + ResultSet results = session.execute(byBuyer.bind().setInt("buyer", 4)); | |
| 73 | + | |
| 74 | + Row row = results.one(); | |
| 75 | + assertThat(row).isNotNull(); | |
| 76 | + assertThat(row.getInt("id")).isEqualTo(38471); | |
| 77 | + assertThat(row.getList("buyers", Integer.class)).contains(4); | |
| 78 | + } | |
| 79 | + | |
| 80 | + @Test(groups="short") | |
| 81 | + public void should_handle_contains_on_map_with_index() { | |
| 82 | + PreparedStatement byFeatures = session.prepare(select("id", "description", "features_values") | |
| 83 | + .from("products") | |
| 84 | + .where(contains("features_values", bindMarker("feature")))); | |
| 85 | + | |
| 86 | + ResultSet results = session.execute(byFeatures.bind().setString("feature", "LED")); | |
| 87 | + | |
| 88 | + Row row = results.one(); | |
| 89 | + assertThat(row).isNotNull(); | |
| 90 | + assertThat(row.getInt("id")).isEqualTo(29412); | |
| 91 | + assertThat(row.getMap("features_values", String.class, String.class)).containsEntry("techno", "LED"); | |
| 92 | + } | |
| 93 | + | |
| 94 | + | |
| 95 | + @Test(groups="short") | |
| 96 | + public void should_handle_contains_key_on_map_with_index() { | |
| 97 | + PreparedStatement byFeatures = session.prepare(select("id", "description", "features_keys") | |
| 98 | + .from("products") | |
| 99 | + .where(containsKey("features_keys", bindMarker("feature")))); | |
| 100 | + | |
| 101 | + ResultSet results = session.execute(byFeatures.bind().setString("feature", "refresh-rate")); | |
| 102 | + | |
| 103 | + Row row = results.one(); | |
| 104 | + assertThat(row).isNotNull(); | |
| 105 | + assertThat(row.getInt("id")).isEqualTo(34134); | |
| 106 | + assertThat(row.getMap("features_keys", String.class, String.class)).containsEntry("refresh-rate", "400hz"); | |
| 107 | + } | |
| 108 | + | |
| 109 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +9 −0
| @@ -131,6 +131,15 @@ | ||
| 131 | 131 | select = select().all().from("foo").where(eq("b", Bytes.fromHexString("0xCAFEBABE"))); |
| 132 | 132 | assertEquals(select.toString(), query); |
| 133 | 133 | |
| 134 | + query = "SELECT * FROM foo WHERE e CONTAINS 'text';"; | |
| 135 | + select = select().from("foo").where(contains("e", "text")); | |
| 136 | + assertEquals(select.toString(), query); | |
| 137 | + | |
| 138 | + query = "SELECT * FROM foo WHERE e CONTAINS KEY 'key1';"; | |
| 139 | + select = select().from("foo").where(containsKey("e", "key1")); | |
| 140 | + assertEquals(select.toString(), query); | |
| 141 | + | |
| 142 | + | |
| 134 | 143 | try { |
| 135 | 144 | select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); |
| 136 | 145 | fail(); |