diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index d0927cc..63addde 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -8,6 +8,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; +import java.util.Map; import java.util.function.Consumer; import com.ljunggren.reflectionUtils.ReflectionUtils; @@ -23,6 +24,7 @@ import com.ljunggren.storm.crud.UpdateQuery; import com.ljunggren.storm.exceptions.StormException; import com.ljunggren.storm.utils.AnnotationUtils; +import com.ljunggren.storm.utils.ParameterUtils; public class StormRepository implements InvocationHandler { @@ -61,8 +63,9 @@ Iterator annotations = Arrays.stream(method.getAnnotations()).iterator(); Type returnType = method.getGenericReturnType(); Object[] argsAsArrays = convertCollectionsToArray(args); + Map parameterArgumentMap = new ParameterUtils().mapArgumentsToParameterNames(method.getParameters(), argsAsArrays); try { - return execute(annotations, argsAsArrays, returnType); + return execute(annotations, parameterArgumentMap, returnType); } catch (Exception e) { throw new StormException(e.getMessage()); } @@ -81,10 +84,10 @@ }).toArray(); } - private Object execute(Iterator annotations, Object[] args, Type returnType) throws Exception { + private Object execute(Iterator annotations, Map parameterArgumentMap, Type returnType) throws Exception { if (annotations.hasNext()) { - Object object = getQueryChain().execute(annotations.next(), context, args, returnType); - return object == null ? execute(annotations, args, returnType) : object; + Object object = getQueryChain().execute(annotations.next(), context, parameterArgumentMap, returnType); + return object == null ? execute(annotations, parameterArgumentMap, returnType) : object; } return null; } diff --git a/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java b/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java index a2d4535..2b56ff2 100644 --- a/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java @@ -2,6 +2,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.util.Map; import com.ljunggren.storm.context.Context; @@ -12,7 +13,7 @@ } @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Map parameterArgumentMap, Type returnType) throws Exception { return null; } diff --git a/src/main/java/com/ljunggren/storm/crud/QueryChain.java b/src/main/java/com/ljunggren/storm/crud/QueryChain.java index e0e9780..182de1a 100644 --- a/src/main/java/com/ljunggren/storm/crud/QueryChain.java +++ b/src/main/java/com/ljunggren/storm/crud/QueryChain.java @@ -6,6 +6,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Map; import java.util.function.Consumer; import java.util.stream.IntStream; @@ -27,9 +28,9 @@ return this; } - public abstract Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception; + public abstract Object execute(Annotation annotation, Context context, Map parameterArgumentMap, Type returnType) throws Exception; - protected Object executeQuery(String sql, Context context, Object[] args, Type returnType) throws Exception { + protected Object executeQuery(String sql, Context context, Map parameterArgumentMap, Type returnType) throws Exception { Connection connection = null; PreparedStatement preparedStatement = null; try { @@ -46,7 +47,7 @@ } } - protected int executeUpdate(String sql, Context context, Object[] args, Type returnType) throws SQLException { + protected int executeUpdate(String sql, Context context, Map parameterArgumentMap, Type returnType) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; try { @@ -62,7 +63,7 @@ } } - protected int executeBatch(String sql, Context context, Object[] argsArray, Type returnType) throws SQLException { + protected int executeBatch(String sql, Context context, Map parameterArgumentMap, Type returnType) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; try { diff --git a/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java b/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java new file mode 100644 index 0000000..1af6275 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java @@ -0,0 +1,30 @@ +package com.ljunggren.storm.utils; + +import java.lang.reflect.Parameter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +public class ParameterUtils { + + private final String parameterRegex = "#\\{[a-zA-Z0-9_]+\\}"; + + public Map mapArgumentsToParameterNames(Parameter[] parameters, Object[] arguments) { + return IntStream.range(0, parameters.length) + .collect(HashMap::new, (m, i) -> m.put(parameters[i].getName(), arguments[i]), Map::putAll); + } + + public List findParameterIds(String sql) { + return Stream.of(sql.split(" ")).filter(string -> string.matches(parameterRegex)) + .map(parameter -> parameter.replace("#{", "").replace("}", "")) + .collect(Collectors.toList()); + } + + public String replaceParamaterIdsWithQuestionMarks(String sql) { + return sql.replaceAll(parameterRegex, "?"); + } + +} diff --git a/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java new file mode 100644 index 0000000..f797165 --- /dev/null +++ b/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java @@ -0,0 +1,51 @@ +package com.ljunggren.storm.utils; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +import com.ljunggren.reflectionUtils.ReflectionUtils; + +public class ParameterUtilsTest { + + private class DummyClass { + @SuppressWarnings("unused") + public void find(int id, boolean asc) {} + } + + private ParameterUtils parameterUtils = new ParameterUtils(); + + @Test + public void mapArgumentsToParameterNamesTest() throws NoSuchMethodException, SecurityException { + Method method = DummyClass.class.getMethod("find", int.class, boolean.class); + Parameter[] parameters = ReflectionUtils.getMethodParameters(method); + Object[] arguments = new Object[] { 1, true}; + Map map = parameterUtils.mapArgumentsToParameterNames(parameters, arguments); + assertEquals(2, map.size()); + assertEquals(arguments[0], map.get(parameters[0].getName())); + assertEquals(arguments[1], map.get(parameters[1].getName())); + } + + @Test + public void findParameterIdsTest() { + String sql = "select * from users where id = #{id} and active = #{active}"; + List parameterIds = parameterUtils.findParameterIds(sql); + assertEquals(2, parameterIds.size()); + assertEquals("id", parameterIds.get(0)); + assertEquals("active", parameterIds.get(1)); + } + + @Test + public void replaceParamaterIdsWithQuestionMarksTest() { + String sql = "select * from users where id = #{id} and active = #{active}"; + String actual = parameterUtils.replaceParamaterIdsWithQuestionMarks(sql); + String expected = "select * from users where id = ? and active = ?"; + assertEquals(expected, actual); + } + +}