Elffuss BraidHub

kikocis/java-driver public

JAVA-1113: Support Cassandra 3.4 LIKE operator in QueryBuilder.
Changeset99a744a6837209206c8af18574ec300865e2022bc63a7f9b72187b2644264d1d
Authorolim7t <omichallat+github@gmail.com> · 2016-03-25 15:05 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-26 14:19 +02:00
Parents23ea99b23d04

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -12,6 +12,7 @@
1212 - [improvement] JAVA-1136: Enable JDK signature check in module driver-extras.
1313 - [improvement] JAVA-866: Support tuple notation in QueryBuilder.eq/in.
1414 - [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.
1516
1617 Merged from 2.1 branch:
1718
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +11 −0
@@ -296,6 +296,17 @@
296296 return new Clause.CompoundClause(names, "=", values);
297297 }
298298
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+
299310 /**
300311 * Create an "in" where clause stating the provided column must be equal
301312 * 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 @@
1616 package com.datastax.driver.core.querybuilder;
1717
1818 import com.datastax.driver.core.*;
19+import com.datastax.driver.core.utils.CassandraVersion;
20+import org.assertj.core.api.iterable.Extractor;
1921 import org.testng.annotations.Test;
2022
2123 import java.util.Date;
@@ -24,6 +26,7 @@
2426 import java.util.Set;
2527
2628 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
29+import static com.datastax.driver.core.schemabuilder.SchemaBuilder.createTable;
2730 import static org.assertj.core.api.Assertions.assertThat;
2831 import static org.assertj.core.data.MapEntry.entry;
2932 import static org.testng.Assert.*;
@@ -182,4 +185,43 @@
182185 assertThat(actual).containsExactly(entry(2, "bar"));
183186 }
184187
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+ }
185227 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +4 −0
@@ -143,6 +143,10 @@
143143 select = select().from("foo").where(containsKey("e", "key1"));
144144 assertEquals(select.toString(), query);
145145
146+ query = "SELECT * FROM foo WHERE e LIKE 'a%';";
147+ select = select().from("foo").where(like("e", "a%"));
148+ assertEquals(select.toString(), query);
149+
146150 try {
147151 select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b"));
148152 fail("Expected an IllegalStateException");