Elffuss BraidHub

kikocis/java-driver public

Allow accessor methods with less parameters in case named bind markers are repeated (JAVA-835).
Changeset430409b970e52edcc6e380d5159346c5dbf6f41c9a219fd657b58f82b72775a4
Authorolim7t <omichallat+github@gmail.com> · 2015-07-17 11:50 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-07-17 15:51 +02:00
Parents787c0e2ddd14

Browse the files at this changeset

3 files changed

modified changelog/README.md +2 −0
@@ -3,6 +3,8 @@
33 ### 2.1.7.1
44
55 - [bug] Special case check for 'null' string in index_options column (JAVA-834)
6+- [improvement] Allow accessor methods with less parameters in case
7+ named bind markers are repeated (JAVA-835)
68
79
810 ### 2.1.7
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +27 −7
@@ -19,7 +19,9 @@
1919 import java.lang.reflect.ParameterizedType;
2020 import java.lang.reflect.Type;
2121 import java.nio.ByteBuffer;
22+import java.util.Set;
2223
24+import com.google.common.collect.Sets;
2325 import com.google.common.util.concurrent.Futures;
2426 import com.google.common.util.concurrent.ListenableFuture;
2527
@@ -58,13 +60,7 @@
5860 this.session = manager.getSession();
5961 this.statement = ps;
6062
61- if (method.isVarArgs())
62- throw new IllegalArgumentException(String.format("Invalid varargs method %s in @Accessor interface"));
63- if (ps.getVariables().size() != method.getParameterTypes().length)
64- throw new IllegalArgumentException(String.format("The number of arguments for method %s (%d) does not match the number of bind parameters in the @Query (%d)",
65- method.getName(), method.getParameterTypes().length, ps.getVariables().size()));
66-
67- // TODO: we should also validate the types of the parameters...
63+ validateParameters();
6864
6965 Class<?> returnType = method.getReturnType();
7066 if (Void.TYPE.isAssignableFrom(returnType) || ResultSet.class.isAssignableFrom(returnType))
@@ -92,6 +88,30 @@
9288 }
9389 }
9490
91+ // Checks the method parameters against the query's bind variables
92+ private void validateParameters() {
93+ if (method.isVarArgs())
94+ throw new IllegalArgumentException(String.format("Invalid varargs method %s in @Accessor interface", method.getName()));
95+
96+ ColumnDefinitions variables = statement.getVariables();
97+ Set<String> names = Sets.newHashSet();
98+ for (ColumnDefinitions.Definition variable : variables) {
99+ names.add(variable.getName());
100+ }
101+
102+ if (method.getParameterTypes().length < names.size())
103+ throw new IllegalArgumentException(String.format("Not enough arguments for method %s, "
104+ + "found %d but it should be at least the number of unique bind parameter names in the @Query (%d)",
105+ method.getName(), method.getParameterTypes().length, names.size()));
106+
107+ if (method.getParameterTypes().length > variables.size())
108+ throw new IllegalArgumentException(String.format("Too many arguments for method %s, "
109+ + "found %d but it should be at most the number of bind parameters in the @Query (%d)",
110+ method.getName(), method.getParameterTypes().length, variables.size()));
111+
112+ // TODO could go further, e.g. check that the types match, inspect @Param annotations to check that all names are bound...
113+ }
114+
95115 @SuppressWarnings("rawtypes")
96116 private void mapType(MappingManager manager, Class<?> fullReturnType, Type type) {
97117
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java +93 −7
@@ -17,27 +17,30 @@
1717
1818 import java.util.Collection;
1919
20-import com.datastax.driver.core.ResultSet;
2120 import com.google.common.collect.Lists;
2221 import org.testng.annotations.Test;
2322
2423 import static org.assertj.core.api.Assertions.assertThat;
2524
2625 import com.datastax.driver.core.CCMBridge;
26+import com.datastax.driver.core.ResultSet;
27+import com.datastax.driver.core.Row;
28+import com.datastax.driver.core.utils.CassandraVersion;
2729 import com.datastax.driver.mapping.annotations.*;
2830
2931 public class MapperAccessorParamsTest extends CCMBridge.PerClassSingleNodeCluster {
3032 @Override
3133 protected Collection<String> getTableDefinitions() {
3234 return Lists.newArrayList(
33- "CREATE TABLE user ( key int primary key, gender int)",
34- "CREATE INDEX on user(gender)",
35- "CREATE TABLE user_str ( key int primary key, gender text)",
36- "CREATE INDEX on user_str(gender)");
35+ "CREATE TABLE user ( key int primary key, gender int, home_phone text, work_phone text)",
36+ "CREATE INDEX on user(gender)",
37+ "CREATE TABLE user_str ( key int primary key, gender text)",
38+ "CREATE INDEX on user_str(gender)"
39+ );
3740 }
3841
3942 @Test(groups = "short")
40- void should_include_enumtype_in_accessor_ordinal() {
43+ public void should_allow_enum_as_int_in_accessor_params() {
4144 UserAccessor accessor = new MappingManager(session)
4245 .createAccessor(UserAccessor.class);
4346
@@ -52,7 +55,7 @@
5255 }
5356
5457 @Test(groups = "short")
55- void should_include_enumtype_in_accessor_string() {
58+ public void should_allow_enum_as_string_in_accessor_params() {
5659 UserAccessor accessor = new MappingManager(session)
5760 .createAccessor(UserAccessor.class);
5861
@@ -66,6 +69,51 @@
6669 assertThat(accessor.getUserStr(Enum.FEMALE).one().getKey()).isEqualTo(0);
6770 }
6871
72+ @Test(groups = "short")
73+ @CassandraVersion(major = 2.0, description = "Uses named parameters")
74+ public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() {
75+ UserPhoneAccessor accessor = new MappingManager(session)
76+ .createAccessor(UserPhoneAccessor.class);
77+
78+ session.execute("delete from user where key = 0");
79+ accessor.updatePhones_positional("1111", "2222", 0);
80+ assertPhonesEqual(0, "1111", "2222");
81+
82+ session.execute("delete from user where key = 0");
83+ accessor.updatePhones_named(0, "1111", "2222");
84+ assertPhonesEqual(0, "1111", "2222");
85+
86+ session.execute("delete from user where key = 0");
87+ accessor.updatePhones_fallback("1111", "2222", 0);
88+ assertPhonesEqual(0, "1111", "2222");
89+
90+ session.execute("delete from user where key = 0");
91+ accessor.updateBothPhones(0, "1111");
92+ assertPhonesEqual(0, "1111", "1111");
93+
94+ session.execute("delete from user where key = 0");
95+ accessor.updatePhones_fallback2("1111", "2222", 0);
96+ assertPhonesEqual(0, "1111", "2222");
97+ }
98+
99+ @Test(groups = "short", expectedExceptions = RuntimeException.class)
100+ public void should_fail_if_not_enough_parameters() {
101+ new MappingManager(session)
102+ .createAccessor(UserPhoneAccessor_NotEnoughParams.class);
103+ }
104+
105+ @Test(groups = "short", expectedExceptions = RuntimeException.class)
106+ public void should_fail_if_too_many_parameters() {
107+ new MappingManager(session)
108+ .createAccessor(UserPhoneAccessor_TooManyParams.class);
109+ }
110+
111+ private void assertPhonesEqual(int key, String home, String work) {
112+ Row row = session.execute("select * from user where key = " + key).one();
113+ assertThat(row.getString("home_phone")).isEqualTo(home);
114+ assertThat(row.getString("work_phone")).isEqualTo(work);
115+ }
116+
69117 enum Enum {
70118 MALE, FEMALE
71119 }
@@ -85,6 +133,44 @@
85133 ResultSet addUserStr(int key, @Enumerated(EnumType.STRING) Enum value);
86134 }
87135
136+ /** Tests various ways to match method parameters to query bind markers. */
137+ @Accessor
138+ public interface UserPhoneAccessor {
139+ /** Standard positional markers */
140+ @Query("update user set home_phone = ?, work_phone = ? where key = ?")
141+ void updatePhones_positional(String homePhone, String workPhone, int key);
142+
143+ /** Standard named markers */
144+ @Query("update user set home_phone = :home, work_phone = :work where key = :key")
145+ void updatePhones_named(@Param("key") int key, @Param("home") String homePhone, @Param("work") String workPhone);
146+
147+ /** Named markers with no @Param. Should fallback to positional matching */
148+ @Query("update user set home_phone = :home, work_phone = :work where key = :key")
149+ void updatePhones_fallback(String homePhone, String workPhone, int key);
150+
151+ /** Named markers with repeated names */
152+ @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
153+ void updateBothPhones(@Param("key") int key, @Param("phone") String uniquePhone);
154+
155+ /** Named markers with repeated names, but no @Param. Should fallback to positional matching */
156+ @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
157+ void updatePhones_fallback2(String homePhone, String workPhone, int key);
158+ }
159+
160+ @Accessor
161+ public interface UserPhoneAccessor_NotEnoughParams {
162+ @SuppressWarnings("unused")
163+ @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
164+ void updateBothPhones(@Param("key") int key);
165+ }
166+
167+ @Accessor
168+ public interface UserPhoneAccessor_TooManyParams {
169+ @SuppressWarnings("unused")
170+ @Query("update user set home_phone = ?, work_phone = ? where key = ?")
171+ void updatePhones(String homePhone, String workPhone, int key, int extra);
172+ }
173+
88174 @Table(name = "user")
89175 public static class User {
90176 @PartitionKey