kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 87381d2d6aaed3d1bc3f9994d4de82e6006ee7b5d6deebb0361ab3acfbc2af14 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-07-27 12:04 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-07-27 10:50 -07:00 |
| Parents | 6795b189968d |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +24 −13
| @@ -94,7 +94,7 @@ | ||
| 94 | 94 | * @param name the name of the column to insert/update. |
| 95 | 95 | * @param value the value to insert/update for {@code name}. |
| 96 | 96 | * @return this INSERT statement. |
| 97 | - * @throws IllegalStateException if this method is called and the {@link #json(String)} | |
| 97 | + * @throws IllegalStateException if this method is called and the {@link #json(Object)} | |
| 98 | 98 | * method has been called before, because it's not possible |
| 99 | 99 | * to mix {@code INSERT JSON} syntax with regular {@code INSERT} syntax. |
| 100 | 100 | */ |
| @@ -118,7 +118,7 @@ | ||
| 118 | 118 | * in {@code names}. |
| 119 | 119 | * @return this INSERT statement. |
| 120 | 120 | * @throws IllegalArgumentException if {@code names.length != values.length}. |
| 121 | - * @throws IllegalStateException if this method is called and the {@link #json(String)} | |
| 121 | + * @throws IllegalStateException if this method is called and the {@link #json(Object)} | |
| 122 | 122 | * method has been called before, because it's not possible |
| 123 | 123 | * to mix {@code INSERT JSON} syntax with regular {@code INSERT} syntax. |
| 124 | 124 | */ |
| @@ -135,7 +135,7 @@ | ||
| 135 | 135 | * in {@code names}. |
| 136 | 136 | * @return this INSERT statement. |
| 137 | 137 | * @throws IllegalArgumentException if {@code names.size() != values.size()}. |
| 138 | - * @throws IllegalStateException if this method is called and the {@link #json(String)} | |
| 138 | + * @throws IllegalStateException if this method is called and the {@link #json(Object)} | |
| 139 | 139 | * method has been called before, because it's not possible |
| 140 | 140 | * to mix {@code INSERT JSON} syntax with regular {@code INSERT} syntax. |
| 141 | 141 | */ |
| @@ -156,20 +156,30 @@ | ||
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | - * Inserts the provided JSON string, using the {@code INSERT INTO ... JSON} syntax introduced | |
| 159 | + * Inserts the provided object, using the {@code INSERT INTO ... JSON} syntax introduced | |
| 160 | 160 | * in Cassandra 2.2. |
| 161 | 161 | * <p/> |
| 162 | - * With INSERT statements, the new {@code JSON} keyword can be used to enable inserting a | |
| 163 | - * JSON encoded map as a single row. | |
| 162 | + * With INSERT statements, the new {@code JSON} keyword can be used to enable inserting a JSON | |
| 163 | + * structure as a single row. | |
| 164 | 164 | * <p/> |
| 165 | - * <strong>The provided JSON string will be appended to the query string as is. | |
| 166 | - * It should NOT be surrounded by single quotes.</strong> | |
| 165 | + * The provided object can be of the following types: | |
| 166 | + * <ol> | |
| 167 | + * <li>A raw string. In this case, it will be appended to the query string as is. | |
| 168 | + * <strong>It should NOT be surrounded by single quotes.</strong> | |
| 167 | 169 | * Its format should generally match that returned by a |
| 168 | 170 | * {@code SELECT JSON} statement on the same table. |
| 169 | - * <p/> | |
| 170 | - * Note that it is not possible to insert function calls nor bind markers in the JSON string. | |
| 171 | + * Note that it is not possible to insert function calls nor bind markers in a JSON string.</li> | |
| 172 | + * <li>A {@link QueryBuilder#bindMarker() bind marker}. In this case, the statement is meant to be prepared | |
| 173 | + * and no JSON string will be appended to the query string, only a bind marker for the whole JSON parameter.</li> | |
| 174 | + * <li>Any object that can be serialized to JSON. Such objects can be used provided that | |
| 175 | + * a matching {@link com.datastax.driver.core.TypeCodec codec} is registered with the | |
| 176 | + * {@link CodecRegistry} in use. This allows the usage of JSON libraries, such | |
| 177 | + * as the <a href="https://jcp.org/en/jsr/detail?id=353">Java API for JSON processing</a>, | |
| 178 | + * the popular <a href="http://wiki.fasterxml.com/JacksonHome">Jackson</a> library, or | |
| 179 | + * Google's <a href="https://github.com/google/gson">Gson</a> library, for instance.</li> | |
| 180 | + * </ol> | |
| 171 | 181 | * <h3>Case-sensitive column names</h3> |
| 172 | - * Users are required to handle case-sensitive column names | |
| 182 | + * When passing raw strings to this method, users are required to handle case-sensitive column names | |
| 173 | 183 | * by surrounding them with double quotes. |
| 174 | 184 | * <p/> |
| 175 | 185 | * For example, to insert into a table with two columns named “myKey” and “value”, |
| @@ -182,7 +192,8 @@ | ||
| 182 | 192 | * INSERT INTO mytable JSON '{"\"myKey\"": 0, "value": 0}'; |
| 183 | 193 | * </pre> |
| 184 | 194 | * <h3>Escaping quotes in column values</h3> |
| 185 | - * Double quotes should be escaped with a backslash, but single quotes should be escaped in | |
| 195 | + * When passing raw strings to this method, double quotes should be escaped with a backslash, | |
| 196 | + * but single quotes should be escaped in | |
| 186 | 197 | * the CQL manner, i.e. by another single quote. For example, the column value |
| 187 | 198 | * {@code foo"'bar} should be inserted in the JSON string |
| 188 | 199 | * as {@code "foo\"''bar"}. |
| @@ -191,7 +202,7 @@ | ||
| 191 | 202 | * Any columns which are omitted from the JSON string will be defaulted to a {@code NULL} value |
| 192 | 203 | * (which will result in a tombstone being created). |
| 193 | 204 | * |
| 194 | - * @param json the JSON string, or a bind marker. | |
| 205 | + * @param json the JSON string, or a bind marker, or a JSON object handled by a specific {@link com.datastax.driver.core.TypeCodec codec}. | |
| 195 | 206 | * @return this INSERT statement. |
| 196 | 207 | * @throws IllegalStateException if this method is called and any of the {@code value} or {@code values} |
| 197 | 208 | * methods have been called before, because it's not possible |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +24 −8
| @@ -1057,23 +1057,39 @@ | ||
| 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 | - * converts a JSON-encoded string into the normal Cassandra column value. | |
| 1060 | + * converts a JSON object into the normal Cassandra column value. | |
| 1061 | 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 | 1064 | * <p/> |
| 1065 | - * <strong>The provided JSON string will be appended to the query string as is. | |
| 1066 | - * It should NOT be surrounded by single quotes.</strong> | |
| 1065 | + * The provided object can be of the following types: | |
| 1066 | + * <ol> | |
| 1067 | + * <li>A raw string. In this case, it will be appended to the query string as is. | |
| 1068 | + * <strong>It should NOT be surrounded by single quotes.</strong> | |
| 1069 | + * Its format should generally match that returned by a | |
| 1070 | + * {@code SELECT JSON} statement on the same table. | |
| 1071 | + * Note that it is not possible to insert function calls nor bind markers in a JSON string.</li> | |
| 1072 | + * <li>A {@link QueryBuilder#bindMarker() bind marker}. In this case, the statement is meant to be prepared | |
| 1073 | + * and no JSON string will be appended to the query string, only a bind marker for the whole JSON parameter.</li> | |
| 1074 | + * <li>Any object that can be serialized to JSON. Such objects can be used provided that | |
| 1075 | + * a matching {@link com.datastax.driver.core.TypeCodec codec} is registered with the | |
| 1076 | + * {@link com.datastax.driver.core.CodecRegistry CodecRegistry} in use. This allows the usage of JSON libraries, such | |
| 1077 | + * as the <a href="https://jcp.org/en/jsr/detail?id=353">Java API for JSON processing</a>, | |
| 1078 | + * the popular <a href="http://wiki.fasterxml.com/JacksonHome">Jackson</a> library, or | |
| 1079 | + * Google's <a href="https://github.com/google/gson">Gson</a> library, for instance.</li> | |
| 1080 | + * </ol> | |
| 1067 | 1081 | * <p/> |
| 1068 | - * String values should be enclosed in double quotes. | |
| 1069 | - * Double quotes appearing inside strings should be escaped with a backslash, | |
| 1082 | + * When passing raw strings to this method, the following rules apply: | |
| 1083 | + * <ol> | |
| 1084 | + * <li>String values should be enclosed in double quotes.</li> | |
| 1085 | + * <li>Double quotes appearing inside strings should be escaped with a backslash, | |
| 1070 | 1086 | * but single quotes should be escaped in |
| 1071 | 1087 | * the CQL manner, i.e. by another single quote. For example, the column value |
| 1072 | 1088 | * {@code foo"'bar} should be inserted in the JSON string |
| 1073 | - * as {@code "foo\"''bar"}. | |
| 1074 | - * <p/> | |
| 1075 | - * Note that it is not possible to insert function calls nor bind markers in the JSON string. | |
| 1089 | + * as {@code "foo\"''bar"}.</li> | |
| 1090 | + * </ol> | |
| 1076 | 1091 | * |
| 1092 | + * @param json the JSON string, or a bind marker, or a JSON object handled by a specific {@link com.datastax.driver.core.TypeCodec codec}. | |
| 1077 | 1093 | * @return the function call. |
| 1078 | 1094 | * @see <a href="http://cassandra.apache.org/doc/cql3/CQL-2.2.html#json">JSON Support for CQL</a> |
| 1079 | 1095 | * @see <a href="http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-2-json-support">JSON Support in Cassandra 2.2</a> |