Elffuss BraidHub

kikocis/java-driver public

Allow non-string values in QueryBuilder JSON methods (#715)

Allow non-string values in QueryBuilder JSON methods
Changeset05a9af8a479d887a075de2d8e952c777ff6c94d5d1408808ed232c97a434de63
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-07-26 16:36 -07:00
CommitterGitHub <noreply@github.com> · 2016-07-26 16:36 -07:00
Parents5f52492e9e51

Browse the files at this changeset

4 files changed

modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +10 −11
@@ -35,7 +35,7 @@
3535 private final List<Object> values = new ArrayList<Object>();
3636 private final Options usings;
3737 private boolean ifNotExists;
38- private String json;
38+ private Object json;
3939
4040 Insert(String keyspace, String table) {
4141 this(keyspace, table, null, null);
@@ -68,9 +68,8 @@
6868
6969 builder.append(" ");
7070 if (json != null) {
71- builder.append("JSON '");
72- builder.append(json);
73- builder.append("'");
71+ builder.append("JSON ");
72+ Utils.appendValue(json, codecRegistry, builder, variables);
7473 } else {
7574 builder.append("(");
7675 Utils.joinAndAppendNames(builder, codecRegistry, ",", names);
@@ -159,20 +158,20 @@
159158 /**
160159 * Inserts the provided JSON string, using the {@code INSERT INTO ... JSON} syntax introduced
161160 * in Cassandra 2.2.
162- * <p>
161+ * <p/>
163162 * With INSERT statements, the new {@code JSON} keyword can be used to enable inserting a
164163 * JSON encoded map as a single row.
165- * <p>
164+ * <p/>
166165 * <strong>The provided JSON string will be appended to the query string as is.
167166 * It should NOT be surrounded by single quotes.</strong>
168167 * Its format should generally match that returned by a
169168 * {@code SELECT JSON} statement on the same table.
170- * <p>
169+ * <p/>
171170 * Note that it is not possible to insert function calls nor bind markers in the JSON string.
172171 * <h3>Case-sensitive column names</h3>
173172 * Users are required to handle case-sensitive column names
174173 * by surrounding them with double quotes.
175- * <p>
174+ * <p/>
176175 * For example, to insert into a table with two columns named “myKey” and “value”,
177176 * you would do the following:
178177 * <pre>
@@ -187,12 +186,12 @@
187186 * the CQL manner, i.e. by another single quote. For example, the column value
188187 * {@code foo"'bar} should be inserted in the JSON string
189188 * as {@code "foo\"''bar"}.
190- * <p>
189+ * <p/>
191190 * <h3>Null values and tombstones</h3>
192191 * Any columns which are omitted from the JSON string will be defaulted to a {@code NULL} value
193192 * (which will result in a tombstone being created).
194193 *
195- * @param json the JSON string.
194+ * @param json the JSON string, or a bind marker.
196195 * @return this INSERT statement.
197196 * @throws IllegalStateException if this method is called and any of the {@code value} or {@code values}
198197 * methods have been called before, because it's not possible
@@ -201,7 +200,7 @@
201200 * @see <a href="http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support">JSON Support in Cassandra 2.2</a>
202201 * @see <a href="https://docs.datastax.com/en/cql/3.3/cql/cql_using/useInsertJSON.html">Inserting JSON data</a>
203202 */
204- public Insert json(String json) {
203+ public Insert json(Object json) {
205204 checkState(values.isEmpty() && names.isEmpty(), "Cannot mix INSERT JSON syntax with regular INSERT syntax");
206205 this.json = json;
207206 return this;
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +9 −9
@@ -1012,8 +1012,8 @@
10121012 /**
10131013 * Creates a Cast of a column using the given dataType.
10141014 *
1015- * @param column the column to cast.
1016- * @param dataType the data type to cast to.
1015+ * @param column the column to cast.
1016+ * @param dataType the data type to cast to.
10171017 * @return the casted column.
10181018 */
10191019 public static Object cast(Object column, DataType dataType) {
@@ -1053,33 +1053,33 @@
10531053
10541054 /**
10551055 * Creates a {@code fromJson()} function call.
1056- * <p>
1056+ * <p/>
10571057 * Support for JSON functions has been added in Cassandra 2.2.
10581058 * The {@code fromJson()} function is similar to {@code INSERT JSON} statements,
10591059 * but applies to a single column value instead of the entire row, and
10601060 * converts a JSON-encoded string into the normal Cassandra column value.
1061- * <p>
1061+ * <p/>
10621062 * It may be used in {@code INSERT} and {@code UPDATE} statements,
10631063 * but NOT in the selection clause of a {@code SELECT} statement.
1064- * <p>
1064+ * <p/>
10651065 * <strong>The provided JSON string will be appended to the query string as is.
10661066 * It should NOT be surrounded by single quotes.</strong>
1067- * <p>
1067+ * <p/>
10681068 * String values should be enclosed in double quotes.
10691069 * Double quotes appearing inside strings should be escaped with a backslash,
10701070 * but single quotes should be escaped in
10711071 * the CQL manner, i.e. by another single quote. For example, the column value
10721072 * {@code foo"'bar} should be inserted in the JSON string
10731073 * as {@code "foo\"''bar"}.
1074- * <p>
1074+ * <p/>
10751075 * Note that it is not possible to insert function calls nor bind markers in the JSON string.
10761076 *
10771077 * @return the function call.
10781078 * @see <a href="http://cassandra.apache.org/doc/cql3/CQL-2.2.html#json">JSON Support for CQL</a>
10791079 * @see <a href="http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support">JSON Support in Cassandra 2.2</a>
10801080 */
1081- public static Object fromJson(String json) {
1082- return fcall("fromJson", raw("'" + json + "'"));
1081+ public static Object fromJson(Object json) {
1082+ return fcall("fromJson", json);
10831083 }
10841084
10851085 }
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +13 −0
@@ -1043,6 +1043,7 @@
10431043 assertThat(e).hasMessage("A PER PARTITION LIMIT value has already been provided");
10441044 }
10451045 }
1046+
10461047 @Test(groups = "unit")
10471048 public void should_handle_select_json() throws Exception {
10481049 assertThat(
@@ -1067,6 +1068,12 @@
10671068 assertThat(
10681069 insertInto("users").json("{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}").toString())
10691070 .isEqualTo("INSERT INTO users JSON '{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}';");
1071+ assertThat(
1072+ insertInto("users").json(bindMarker()).toString())
1073+ .isEqualTo("INSERT INTO users JSON ?;");
1074+ assertThat(
1075+ insertInto("users").json(bindMarker("json")).toString())
1076+ .isEqualTo("INSERT INTO users JSON :json;");
10701077 }
10711078
10721079 @Test(groups = "unit")
@@ -1087,6 +1094,12 @@
10871094 assertThat(
10881095 insertInto("users").value("id", fromJson("\"user123\"")).value("age", fromJson("42")).toString())
10891096 .isEqualTo("INSERT INTO users (id,age) VALUES (fromJson('\"user123\"'),fromJson('42'));");
1097+ assertThat(
1098+ insertInto("users").value("id", fromJson(bindMarker())).toString())
1099+ .isEqualTo("INSERT INTO users (id) VALUES (fromJson(?));");
1100+ assertThat(
1101+ insertInto("users").value("id", fromJson(bindMarker("id"))).toString())
1102+ .isEqualTo("INSERT INTO users (id) VALUES (fromJson(:id));");
10901103 }
10911104
10921105
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/json/Jsr353JsonCodecTest.java +2 −2
@@ -243,7 +243,7 @@
243243 .add("f", 8.6)
244244 .add("i", 4)
245245 .build();
246- session().execute(insertInto(TABLE1).json(input.toString()));
246+ session().execute(insertInto(TABLE1).json(input));
247247 // then
248248 ResultSet r = session().execute(select().json().from(TABLE1).where(eq("k", key)));
249249 assertThat(r.getAvailableWithoutFetching()).isEqualTo(1);
@@ -272,7 +272,7 @@
272272 .build();
273273
274274 // insert using fromJson on address column, which is a UDT.
275- session().execute(insertInto(TABLE2).value("k", key).value("v", fromJson(inputAddr.toString())));
275+ session().execute(insertInto(TABLE2).value("k", key).value("v", fromJson(inputAddr)));
276276
277277 // retrieve using toJson on address column.
278278 ResultSet r = session().execute(select().toJson("v").as("v").column("k").from(TABLE2).where(eq("k", key)));