kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 955409e5a7ada6ff26fb44dbbd9a3ebaf9eb5e4e5f0db68ca7acf235638ab011 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-19 14:52 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-02-24 18:31 +01:00 |
| Parents | 05d50b12e01d |
4 files changed
modified changelog/README.md +1 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | - [improvement] JAVA-851: Improve UUIDs javadoc with regard to user-provided timestamps. |
| 18 | 18 | - [improvement] JAVA-979: Update javadoc for RegularStatement toString() and getQueryString() to indicate that consistency level and other parameters are not maintained in the query string. |
| 19 | 19 | - [improvement] JAVA-1038: Fetch node info by rpc_address if its broadcast_address is not in system.peers. |
| 20 | +- [improvement] JAVA-974: Validate accessor parameter types against bound statement. | |
| 20 | 21 | |
| 21 | 22 | Merged from 2.0 branch: |
| 22 | 23 |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +1 −2
| @@ -320,8 +320,7 @@ | ||
| 320 | 320 | // We need a specialized mapper to convert UDT instances in the hierarchy. |
| 321 | 321 | return new NestedUDTParamMapper(paramName, idx, inferredCQLType); |
| 322 | 322 | } else { |
| 323 | - // Use the default mapper but provide the extracted type | |
| 324 | - return new ParamMapper(paramName, idx, inferredCQLType.dataType); | |
| 323 | + return new ParamMapper(paramName, idx); | |
| 325 | 324 | } |
| 326 | 325 | } else { |
| 327 | 326 | throw new IllegalArgumentException(String.format("Cannot map class %s for parameter %s of %s.%s", paramType, paramName, className, methodName)); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +15 −16
| @@ -177,32 +177,31 @@ | ||
| 177 | 177 | // We'll only set one of the other. If paramName is null, then paramIdx is used. |
| 178 | 178 | private final String paramName; |
| 179 | 179 | private final int paramIdx; |
| 180 | - private final DataType dataType; | |
| 181 | 180 | |
| 182 | - public ParamMapper(String paramName, int paramIdx, DataType dataType) { | |
| 181 | + ParamMapper(String paramName, int paramIdx) { | |
| 183 | 182 | this.paramName = paramName; |
| 184 | 183 | this.paramIdx = paramIdx; |
| 185 | - this.dataType = dataType; | |
| 186 | - } | |
| 187 | - | |
| 188 | - public ParamMapper(String paramName, int paramIdx) { | |
| 189 | - this(paramName, paramIdx, null); | |
| 190 | 184 | } |
| 191 | 185 | |
| 192 | 186 | void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) { |
| 193 | - ByteBuffer serializedArg = (dataType == null) | |
| 194 | - ? DataType.serializeValue(arg, protocolVersion) | |
| 195 | - : dataType.serialize(arg, protocolVersion); | |
| 196 | - if (paramName == null) { | |
| 197 | - if (arg == null) | |
| 187 | + | |
| 188 | + if (arg == null) { | |
| 189 | + if (paramName == null) | |
| 198 | 190 | boundStatement.setToNull(paramIdx); |
| 199 | 191 | else |
| 200 | - boundStatement.setBytesUnsafe(paramIdx, serializedArg); | |
| 201 | - } else { | |
| 202 | - if (arg == null) | |
| 203 | 192 | boundStatement.setToNull(paramName); |
| 204 | - else | |
| 193 | + } else { | |
| 194 | + ColumnDefinitions variables = boundStatement.preparedStatement().getVariables(); | |
| 195 | + if (paramName == null) { | |
| 196 | + DataType dataType = variables.getType(paramIdx); | |
| 197 | + ByteBuffer serializedArg = dataType.serialize(arg, protocolVersion); | |
| 198 | + boundStatement.setBytesUnsafe(paramIdx, serializedArg); | |
| 199 | + } else { | |
| 200 | + DataType dataType = variables.getType(paramName); | |
| 201 | + ByteBuffer serializedArg = dataType.serialize(arg, protocolVersion); | |
| 205 | 202 | boundStatement.setBytesUnsafe(paramName, serializedArg); |
| 203 | + } | |
| 204 | + | |
| 206 | 205 | } |
| 207 | 206 | } |
| 208 | 207 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java +25 −0
| @@ -18,9 +18,13 @@ | ||
| 18 | 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | 19 | import com.datastax.driver.core.ResultSet; |
| 20 | 20 | import com.datastax.driver.core.Row; |
| 21 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 21 | 22 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | 23 | import com.datastax.driver.mapping.annotations.*; |
| 23 | 24 | import org.testng.annotations.Test; |
| 25 | +import org.testng.collections.Lists; | |
| 26 | + | |
| 27 | +import java.util.List; | |
| 24 | 28 | |
| 25 | 29 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 30 | |
| @@ -106,6 +110,19 @@ | ||
| 106 | 110 | .createAccessor(UserPhoneAccessor_TooManyParams.class); |
| 107 | 111 | } |
| 108 | 112 | |
| 113 | + /** | |
| 114 | + * Ensures that a wrong parameter type is detected when | |
| 115 | + * the accessor is called with a wrong data type. | |
| 116 | + * | |
| 117 | + * @jira_ticket JAVA-974 | |
| 118 | + */ | |
| 119 | + @Test(groups = "short", expectedExceptions = InvalidTypeException.class) | |
| 120 | + public void should_fail_if_wrong_parameter_type() { | |
| 121 | + UserPhoneAccessor_WrongParameterTypes accessor = new MappingManager(session()) | |
| 122 | + .createAccessor(UserPhoneAccessor_WrongParameterTypes.class); | |
| 123 | + accessor.findUsersBykeys(Lists.newArrayList(1, 2, 3)); | |
| 124 | + } | |
| 125 | + | |
| 109 | 126 | private void assertPhonesEqual(int key, String home, String work) { |
| 110 | 127 | Row row = session().execute("select * from user where key = " + key).one(); |
| 111 | 128 | assertThat(row.getString("home_phone")).isEqualTo(home); |
| @@ -245,4 +262,12 @@ | ||
| 245 | 262 | this.gender = val; |
| 246 | 263 | } |
| 247 | 264 | } |
| 265 | + | |
| 266 | + @Accessor | |
| 267 | + public interface UserPhoneAccessor_WrongParameterTypes { | |
| 268 | + @Query("select * from user where key IN (:keys)") | |
| 269 | + // WRONG, should be "where key IN :keys" | |
| 270 | + void findUsersBykeys(@Param("keys") List<Integer> keys); | |
| 271 | + } | |
| 272 | + | |
| 248 | 273 | } |