kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 05a9af8a479d887a075de2d8e952c777ff6c94d5d1408808ed232c97a434de63 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-07-26 16:36 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-07-26 16:36 -07:00 |
| Parents | 5f52492e9e51 |
4 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +10 −11
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | private final List<Object> values = new ArrayList<Object>(); |
| 36 | 36 | private final Options usings; |
| 37 | 37 | private boolean ifNotExists; |
| 38 | - private String json; | |
| 38 | + private Object json; | |
| 39 | 39 | |
| 40 | 40 | Insert(String keyspace, String table) { |
| 41 | 41 | this(keyspace, table, null, null); |
| @@ -68,9 +68,8 @@ | ||
| 68 | 68 | |
| 69 | 69 | builder.append(" "); |
| 70 | 70 | 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); | |
| 74 | 73 | } else { |
| 75 | 74 | builder.append("("); |
| 76 | 75 | Utils.joinAndAppendNames(builder, codecRegistry, ",", names); |
| @@ -159,20 +158,20 @@ | ||
| 159 | 158 | /** |
| 160 | 159 | * Inserts the provided JSON string, using the {@code INSERT INTO ... JSON} syntax introduced |
| 161 | 160 | * in Cassandra 2.2. |
| 162 | - * <p> | |
| 161 | + * <p/> | |
| 163 | 162 | * With INSERT statements, the new {@code JSON} keyword can be used to enable inserting a |
| 164 | 163 | * JSON encoded map as a single row. |
| 165 | - * <p> | |
| 164 | + * <p/> | |
| 166 | 165 | * <strong>The provided JSON string will be appended to the query string as is. |
| 167 | 166 | * It should NOT be surrounded by single quotes.</strong> |
| 168 | 167 | * Its format should generally match that returned by a |
| 169 | 168 | * {@code SELECT JSON} statement on the same table. |
| 170 | - * <p> | |
| 169 | + * <p/> | |
| 171 | 170 | * Note that it is not possible to insert function calls nor bind markers in the JSON string. |
| 172 | 171 | * <h3>Case-sensitive column names</h3> |
| 173 | 172 | * Users are required to handle case-sensitive column names |
| 174 | 173 | * by surrounding them with double quotes. |
| 175 | - * <p> | |
| 174 | + * <p/> | |
| 176 | 175 | * For example, to insert into a table with two columns named “myKey” and “value”, |
| 177 | 176 | * you would do the following: |
| 178 | 177 | * <pre> |
| @@ -187,12 +186,12 @@ | ||
| 187 | 186 | * the CQL manner, i.e. by another single quote. For example, the column value |
| 188 | 187 | * {@code foo"'bar} should be inserted in the JSON string |
| 189 | 188 | * as {@code "foo\"''bar"}. |
| 190 | - * <p> | |
| 189 | + * <p/> | |
| 191 | 190 | * <h3>Null values and tombstones</h3> |
| 192 | 191 | * Any columns which are omitted from the JSON string will be defaulted to a {@code NULL} value |
| 193 | 192 | * (which will result in a tombstone being created). |
| 194 | 193 | * |
| 195 | - * @param json the JSON string. | |
| 194 | + * @param json the JSON string, or a bind marker. | |
| 196 | 195 | * @return this INSERT statement. |
| 197 | 196 | * @throws IllegalStateException if this method is called and any of the {@code value} or {@code values} |
| 198 | 197 | * methods have been called before, because it's not possible |
| @@ -201,7 +200,7 @@ | ||
| 201 | 200 | * @see <a href="http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support">JSON Support in Cassandra 2.2</a> |
| 202 | 201 | * @see <a href="https://docs.datastax.com/en/cql/3.3/cql/cql_using/useInsertJSON.html">Inserting JSON data</a> |
| 203 | 202 | */ |
| 204 | - public Insert json(String json) { | |
| 203 | + public Insert json(Object json) { | |
| 205 | 204 | checkState(values.isEmpty() && names.isEmpty(), "Cannot mix INSERT JSON syntax with regular INSERT syntax"); |
| 206 | 205 | this.json = json; |
| 207 | 206 | return this; |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +9 −9
| @@ -1012,8 +1012,8 @@ | ||
| 1012 | 1012 | /** |
| 1013 | 1013 | * Creates a Cast of a column using the given dataType. |
| 1014 | 1014 | * |
| 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. | |
| 1017 | 1017 | * @return the casted column. |
| 1018 | 1018 | */ |
| 1019 | 1019 | public static Object cast(Object column, DataType dataType) { |
| @@ -1053,33 +1053,33 @@ | ||
| 1053 | 1053 | |
| 1054 | 1054 | /** |
| 1055 | 1055 | * Creates a {@code fromJson()} function call. |
| 1056 | - * <p> | |
| 1056 | + * <p/> | |
| 1057 | 1057 | * Support for JSON functions has been added in Cassandra 2.2. |
| 1058 | 1058 | * The {@code fromJson()} function is similar to {@code INSERT JSON} statements, |
| 1059 | 1059 | * but applies to a single column value instead of the entire row, and |
| 1060 | 1060 | * converts a JSON-encoded string into the normal Cassandra column value. |
| 1061 | - * <p> | |
| 1061 | + * <p/> | |
| 1062 | 1062 | * It may be used in {@code INSERT} and {@code UPDATE} statements, |
| 1063 | 1063 | * but NOT in the selection clause of a {@code SELECT} statement. |
| 1064 | - * <p> | |
| 1064 | + * <p/> | |
| 1065 | 1065 | * <strong>The provided JSON string will be appended to the query string as is. |
| 1066 | 1066 | * It should NOT be surrounded by single quotes.</strong> |
| 1067 | - * <p> | |
| 1067 | + * <p/> | |
| 1068 | 1068 | * String values should be enclosed in double quotes. |
| 1069 | 1069 | * Double quotes appearing inside strings should be escaped with a backslash, |
| 1070 | 1070 | * but single quotes should be escaped in |
| 1071 | 1071 | * the CQL manner, i.e. by another single quote. For example, the column value |
| 1072 | 1072 | * {@code foo"'bar} should be inserted in the JSON string |
| 1073 | 1073 | * as {@code "foo\"''bar"}. |
| 1074 | - * <p> | |
| 1074 | + * <p/> | |
| 1075 | 1075 | * Note that it is not possible to insert function calls nor bind markers in the JSON string. |
| 1076 | 1076 | * |
| 1077 | 1077 | * @return the function call. |
| 1078 | 1078 | * @see <a href="http://cassandra.apache.org/doc/cql3/CQL-2.2.html#json">JSON Support for CQL</a> |
| 1079 | 1079 | * @see <a href="http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support">JSON Support in Cassandra 2.2</a> |
| 1080 | 1080 | */ |
| 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); | |
| 1083 | 1083 | } |
| 1084 | 1084 | |
| 1085 | 1085 | } |
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +13 −0
| @@ -1043,6 +1043,7 @@ | ||
| 1043 | 1043 | assertThat(e).hasMessage("A PER PARTITION LIMIT value has already been provided"); |
| 1044 | 1044 | } |
| 1045 | 1045 | } |
| 1046 | + | |
| 1046 | 1047 | @Test(groups = "unit") |
| 1047 | 1048 | public void should_handle_select_json() throws Exception { |
| 1048 | 1049 | assertThat( |
| @@ -1067,6 +1068,12 @@ | ||
| 1067 | 1068 | assertThat( |
| 1068 | 1069 | insertInto("users").json("{\"id\": \"user123\", \"\\\"Age\\\"\": 42, \"\\\"State\\\"\": \"TX\"}").toString()) |
| 1069 | 1070 | .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;"); | |
| 1070 | 1077 | } |
| 1071 | 1078 | |
| 1072 | 1079 | @Test(groups = "unit") |
| @@ -1087,6 +1094,12 @@ | ||
| 1087 | 1094 | assertThat( |
| 1088 | 1095 | insertInto("users").value("id", fromJson("\"user123\"")).value("age", fromJson("42")).toString()) |
| 1089 | 1096 | .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));"); | |
| 1090 | 1103 | } |
| 1091 | 1104 | |
| 1092 | 1105 |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/json/Jsr353JsonCodecTest.java +2 −2
| @@ -243,7 +243,7 @@ | ||
| 243 | 243 | .add("f", 8.6) |
| 244 | 244 | .add("i", 4) |
| 245 | 245 | .build(); |
| 246 | - session().execute(insertInto(TABLE1).json(input.toString())); | |
| 246 | + session().execute(insertInto(TABLE1).json(input)); | |
| 247 | 247 | // then |
| 248 | 248 | ResultSet r = session().execute(select().json().from(TABLE1).where(eq("k", key))); |
| 249 | 249 | assertThat(r.getAvailableWithoutFetching()).isEqualTo(1); |
| @@ -272,7 +272,7 @@ | ||
| 272 | 272 | .build(); |
| 273 | 273 | |
| 274 | 274 | // 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))); | |
| 276 | 276 | |
| 277 | 277 | // retrieve using toJson on address column. |
| 278 | 278 | ResultSet r = session().execute(select().toJson("v").as("v").column("k").from(TABLE2).where(eq("k", key))); |