kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | abb70b8a63fef31abded04a0fdca779fd7694ca320675e3b99af0252183070b2 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-04-26 15:01 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-04-26 15:01 +02:00 |
| Parents | 23ea99b23d04, 99a744a68372 |
4 files changed
modified changelog/README.md +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [improvement] JAVA-1136: Enable JDK signature check in module driver-extras. |
| 13 | 13 | - [improvement] JAVA-866: Support tuple notation in QueryBuilder.eq/in. |
| 14 | 14 | - [bug] JAVA-1140: Use same connection to check for schema agreement after a DDL query. |
| 15 | +- [improvement] JAVA-1113: Support Cassandra 3.4 LIKE operator in QueryBuilder. | |
| 15 | 16 | |
| 16 | 17 | Merged from 2.1 branch: |
| 17 | 18 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +11 −0
| @@ -296,6 +296,17 @@ | ||
| 296 | 296 | return new Clause.CompoundClause(names, "=", values); |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | + /** | |
| 300 | + * Creates a "like" where clause stating that the provided column must be equal to the provided value. | |
| 301 | + * | |
| 302 | + * @param name the column name. | |
| 303 | + * @param value the value. | |
| 304 | + * @return the corresponding where clause. | |
| 305 | + */ | |
| 306 | + public static Clause like(String name, Object value) { | |
| 307 | + return new Clause.SimpleClause(name, " LIKE ", value); | |
| 308 | + } | |
| 309 | + | |
| 299 | 310 | /** |
| 300 | 311 | * Create an "in" where clause stating the provided column must be equal |
| 301 | 312 | * to one of the provided values. |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderExecutionTest.java +42 −0
| @@ -16,6 +16,8 @@ | ||
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.*; |
| 19 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 20 | +import org.assertj.core.api.iterable.Extractor; | |
| 19 | 21 | import org.testng.annotations.Test; |
| 20 | 22 | |
| 21 | 23 | import java.util.Date; |
| @@ -24,6 +26,7 @@ | ||
| 24 | 26 | import java.util.Set; |
| 25 | 27 | |
| 26 | 28 | import static com.datastax.driver.core.querybuilder.QueryBuilder.*; |
| 29 | +import static com.datastax.driver.core.schemabuilder.SchemaBuilder.createTable; | |
| 27 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| 28 | 31 | import static org.assertj.core.data.MapEntry.entry; |
| 29 | 32 | import static org.testng.Assert.*; |
| @@ -182,4 +185,43 @@ | ||
| 182 | 185 | assertThat(actual).containsExactly(entry(2, "bar")); |
| 183 | 186 | } |
| 184 | 187 | |
| 188 | + /** | |
| 189 | + * Validates that {@link QueryBuilder} can construct a query using the 'LIKE' operator to retrieve data from a | |
| 190 | + * table on a column that has a SASI index, i.e.: | |
| 191 | + * <p/> | |
| 192 | + * <code>select n from s_table where n like 'Hello%'</code> | |
| 193 | + * <p/> | |
| 194 | + * | |
| 195 | + * @test_category queries:builder | |
| 196 | + * @jira_ticket JAVA-1113 | |
| 197 | + * @since 3.0.1 | |
| 198 | + */ | |
| 199 | + @Test(groups = "short") | |
| 200 | + @CassandraVersion(major = 3.6) | |
| 201 | + public void should_retrieve_using_like_operator_on_table_with_sasi_index() { | |
| 202 | + //given | |
| 203 | + String table = "s_table"; | |
| 204 | + session().execute(createTable(table).addPartitionKey("k", DataType.text()) | |
| 205 | + .addClusteringColumn("cc", DataType.cint()) | |
| 206 | + .addColumn("n", DataType.text()) | |
| 207 | + ); | |
| 208 | + session().execute(String.format( | |
| 209 | + "CREATE CUSTOM INDEX on %s (n) USING 'org.apache.cassandra.index.sasi.SASIIndex';", table)); | |
| 210 | + session().execute(insertInto(table).value("k", "a").value("cc", 0).value("n", "Hello World")); | |
| 211 | + session().execute(insertInto(table).value("k", "a").value("cc", 1).value("n", "Goodbye World")); | |
| 212 | + session().execute(insertInto(table).value("k", "b").value("cc", 2).value("n", "Hello Moon")); | |
| 213 | + | |
| 214 | + //when | |
| 215 | + BuiltStatement query = select("n").from(table).where(like("n", "Hello%")); | |
| 216 | + ResultSet r = session().execute(query); | |
| 217 | + | |
| 218 | + //then | |
| 219 | + assertThat(r.getAvailableWithoutFetching()).isEqualTo(2); | |
| 220 | + assertThat(r.all()).extracting(new Extractor<Row, String>() { | |
| 221 | + @Override | |
| 222 | + public String extract(Row input) { | |
| 223 | + return input.getString("n"); | |
| 224 | + } | |
| 225 | + }).containsOnly("Hello World", "Hello Moon"); | |
| 226 | + } | |
| 185 | 227 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +4 −0
| @@ -143,6 +143,10 @@ | ||
| 143 | 143 | select = select().from("foo").where(containsKey("e", "key1")); |
| 144 | 144 | assertEquals(select.toString(), query); |
| 145 | 145 | |
| 146 | + query = "SELECT * FROM foo WHERE e LIKE 'a%';"; | |
| 147 | + select = select().from("foo").where(like("e", "a%")); | |
| 148 | + assertEquals(select.toString(), query); | |
| 149 | + | |
| 146 | 150 | try { |
| 147 | 151 | select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b")); |
| 148 | 152 | fail("Expected an IllegalStateException"); |