diff --git a/README.md b/README.md index 2117254..a4e29ab 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ @Select(sql = "select * from users") public List fetchAll(); - @Delete(sql = "delete from users where id = ?") + @Delete(sql = "delete from users where id = #{id}") public int delete(int id); } @@ -75,7 +75,7 @@ @Delete public int deleteAll(User... users); -@Delete(sql = "delete from users where id = ?") +@Delete(sql = "delete from users where id = #{id}") public int deleteById(int id); ``` @@ -88,8 +88,8 @@ @Update public int updateAll(User... users); -@Update(sql = "update users set firstname = ? where id = ?") -public int updateFirstName(String name, int id); +@Update(sql = "update users set firstname = #{firstname} where id = #{id}") +public int updateFirstName(String firstname, int id); ``` Insert @@ -101,8 +101,8 @@ @Insert public int insertAll(User... users); -@Insert(sql = "insert into users (firstname, lastname, employee_id) values (?, ?, ?)") -public int insert(String firstName, String lastName, int employeeID); +@Insert(sql = "insert into users (firstname, lastname, employee_id) values (#{firstname}, #{lastname}, #{employeeID})") +public int insert(String firstname, String lastname, int employeeID); ``` Insert Batch @@ -162,13 +162,19 @@ ** Note: ** Paging can be passed in at any point in the argument list ```java -@Select(sql = "select * from users where lastname = ?") -public findByLastName(Paging paging, String lastName); +@Select(sql = "select * from users where lastname = #{lastname}") +public findByLastName(Paging paging, String lastname); // or public findByLastName(String lastName, Paging paging); ``` +## Compiling ## + +Compile with debug information + +- Eclipse - Window > Preferences > Java > Compiler > Store information about method parameters + ## TODO ## 1. Handle embedded objects & parameterized objects on insert, delete, update diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index 63addde..cd479fe 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -3,12 +3,12 @@ import java.lang.annotation.Annotation; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import java.lang.reflect.Proxy; import java.lang.reflect.Type; 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; @@ -24,7 +24,6 @@ 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 { @@ -63,9 +62,9 @@ Iterator annotations = Arrays.stream(method.getAnnotations()).iterator(); Type returnType = method.getGenericReturnType(); Object[] argsAsArrays = convertCollectionsToArray(args); - Map parameterArgumentMap = new ParameterUtils().mapArgumentsToParameterNames(method.getParameters(), argsAsArrays); + Parameter[] parameters = method.getParameters(); try { - return execute(annotations, parameterArgumentMap, returnType); + return execute(annotations, parameters, argsAsArrays, returnType); } catch (Exception e) { throw new StormException(e.getMessage()); } @@ -84,10 +83,10 @@ }).toArray(); } - private Object execute(Iterator annotations, Map parameterArgumentMap, Type returnType) throws Exception { + private Object execute(Iterator annotations, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { if (annotations.hasNext()) { - Object object = getQueryChain().execute(annotations.next(), context, parameterArgumentMap, returnType); - return object == null ? execute(annotations, parameterArgumentMap, returnType) : object; + Object object = getQueryChain().execute(annotations.next(), context, parameters, arguments, returnType); + return object == null ? execute(annotations, parameters, arguments, returnType) : object; } return null; } diff --git a/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java b/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java index f5a2320..70e6252 100644 --- a/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java +++ b/src/main/java/com/ljunggren/storm/builders/QueryBuilder.java @@ -13,7 +13,7 @@ public abstract class QueryBuilder { public abstract String buildSQL(); - public abstract Object[] getArgs() throws IllegalAccessException ; + public abstract Object[] getArgs() throws IllegalAccessException; protected String getTableName(Class clazz) { Table table = AnnotationUtils.getAnnotationFromClass(Table.class, clazz); diff --git a/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java b/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java index 2b56ff2..ba0bf32 100644 --- a/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/CatchAllQuery.java @@ -1,8 +1,8 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; -import java.util.Map; import com.ljunggren.storm.context.Context; @@ -13,7 +13,7 @@ } @Override - public Object execute(Annotation annotation, Context context, Map parameterArgumentMap, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { return null; } diff --git a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java index 13700b4..7aaa2ce 100644 --- a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java @@ -1,6 +1,7 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.util.Arrays; import java.util.function.Consumer; @@ -20,16 +21,16 @@ } @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { if (annotation.annotationType() == Delete.class) { String sql = ((Delete) annotation).sql(); - return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeUpdate(sql, context, args, returnType); + return sql.isEmpty() ? executeNonNativeQuery(context, arguments, returnType) : executeUpdate(sql, context, parameters, arguments, returnType); } - return nextChain.execute(annotation, context, args, returnType); + return nextChain.execute(annotation, context, parameters, arguments, returnType); } - private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { - return Arrays.stream(args).map(ExceptionUtils.rethrowFunction(arg -> executeNonNativeQuery(context, arg, returnType))) + private int executeNonNativeQuery(Context context, Object[] arguments, Type returnType) { + return Arrays.stream(arguments).map(ExceptionUtils.rethrowFunction(arg -> executeNonNativeQuery(context, arg, returnType))) .collect(Collectors.summingInt(Integer::intValue)); } @@ -42,5 +43,5 @@ Object[] generatedArgs = queryBuilder.getArgs(); return executeUpdate(sql, context, generatedArgs, returnType); } - + } diff --git a/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java index c34bbcb..cc407c9 100644 --- a/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/InsertBatchQuery.java @@ -1,6 +1,7 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.util.Arrays; import java.util.function.Consumer; @@ -19,11 +20,11 @@ } @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { if (annotation.annotationType() == InsertBatch.class) { - return executeNonNativeQuery(context, args, returnType); + return executeNonNativeQuery(context, arguments, returnType); } - return nextChain.execute(annotation, context, args, returnType); + return nextChain.execute(annotation, context, parameters, arguments, returnType); } private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { diff --git a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java index 3925b89..e5c3c59 100644 --- a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java @@ -1,6 +1,7 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.util.Arrays; import java.util.function.Consumer; @@ -20,12 +21,12 @@ } @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { if (annotation.annotationType() == Insert.class) { String sql = ((Insert) annotation).sql(); - return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeUpdate(sql, context, args, returnType); + return sql.isEmpty() ? executeNonNativeQuery(context, arguments, returnType) : executeUpdate(sql, context, parameters, arguments, returnType); } - return nextChain.execute(annotation, context, args, returnType); + return nextChain.execute(annotation, context, parameters, arguments, returnType); } private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { @@ -33,11 +34,11 @@ .collect(Collectors.summingInt(Integer::intValue)); } - private int executeNonNativeQuery(Context context, Object arg, Type returnType) throws Exception { - if (ReflectionUtils.isArray(arg.getClass())) { - return executeNonNativeQuery(context, (Object[]) arg, returnType); + private int executeNonNativeQuery(Context context, Object arguments, Type returnType) throws Exception { + if (ReflectionUtils.isArray(arguments.getClass())) { + return executeNonNativeQuery(context, (Object[]) arguments, returnType); } - QueryBuilder queryBuilder = new InsertQueryBuilder(arg); + QueryBuilder queryBuilder = new InsertQueryBuilder(arguments); String sql = queryBuilder.buildSQL(); Object[] generatedArgs = queryBuilder.getArgs(); return executeUpdate(sql, context, generatedArgs, returnType); diff --git a/src/main/java/com/ljunggren/storm/crud/QueryChain.java b/src/main/java/com/ljunggren/storm/crud/QueryChain.java index 182de1a..a3e9be3 100644 --- a/src/main/java/com/ljunggren/storm/crud/QueryChain.java +++ b/src/main/java/com/ljunggren/storm/crud/QueryChain.java @@ -1,17 +1,20 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.stream.IntStream; import com.ljunggren.storm.context.Context; import com.ljunggren.storm.mapper.ResultSetMapper; +import com.ljunggren.storm.utils.ParameterUtils; public abstract class QueryChain { @@ -28,15 +31,16 @@ return this; } - public abstract Object execute(Annotation annotation, Context context, Map parameterArgumentMap, Type returnType) throws Exception; + private ParameterUtils parameterUtils = new ParameterUtils(); - protected Object executeQuery(String sql, Context context, Map parameterArgumentMap, Type returnType) throws Exception { + public abstract Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception; + + protected Object executeQuery(String sql, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = context.getConnection(); - preparedStatement = connection.prepareStatement(sql); - setParameters(preparedStatement, args); + preparedStatement = generatePreparedStatement(connection, sql, parameters, arguments); ResultSet resultSet = preparedStatement.executeQuery(); return new ResultSetMapper(resultSet, returnType).map(); } finally { @@ -47,13 +51,12 @@ } } - protected int executeUpdate(String sql, Context context, Map parameterArgumentMap, Type returnType) throws SQLException { + protected int executeUpdate(String sql, Context context, Object[] arguments, Type returnType) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = context.getConnection(); - preparedStatement = connection.prepareStatement(sql); - setParameters(preparedStatement, args); + preparedStatement = generatePreparedStatement(connection, sql, arguments); return preparedStatement.executeUpdate(); } finally { if (peek != null) { @@ -63,12 +66,51 @@ } } - protected int executeBatch(String sql, Context context, Map parameterArgumentMap, Type returnType) throws SQLException { + protected int executeUpdate(String sql, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = context.getConnection(); - preparedStatement = connection.prepareStatement(sql); + preparedStatement = generatePreparedStatement(connection, sql, parameters, arguments); + return preparedStatement.executeUpdate(); + } finally { + if (peek != null) { + peek.accept(preparedStatement.toString()); + } + closeConnection(connection); + } + } + + private PreparedStatement generatePreparedStatement(Connection connection, String sql, Object[] arguments) throws SQLException { + PreparedStatement preparedStatement = connection.prepareStatement(sql); + if (arguments == null) { + return preparedStatement; + } + for (int i = 0; i < arguments.length; i++) { + preparedStatement.setObject(i + 1, arguments[i]); + } + return preparedStatement; + } + + private PreparedStatement generatePreparedStatement(Connection connection, String sql, Parameter[] parameters, Object[] arguments) throws SQLException { + List parameterIds = parameterUtils.findParameterIds(sql); + String preparedSql = parameterUtils.replaceParamaterIdsWithQuestionMarks(sql); + PreparedStatement preparedStatement = connection.prepareStatement(preparedSql); + Map parameterArgumentMap = new ParameterUtils().mapArgumentsToParameterNames(parameters, arguments); + for (int i = 0; i < parameterIds.size(); i++) { + preparedStatement.setObject(i + 1, parameterArgumentMap.get(parameterIds.get(i))); + } + return preparedStatement; + } + + + protected int executeBatch(String sql, Context context, Object[] argsArray, Type returnType) throws SQLException { + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = context.getConnection(); + String preparedSql = parameterUtils.replaceParamaterIdsWithQuestionMarks(sql); + preparedStatement = connection.prepareStatement(preparedSql); for (Object args: argsArray) { // TODO: Refactor so args does not have to be cast to array setParameters(preparedStatement, (Object[]) args); diff --git a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java index 14c3e35..e8660e7 100644 --- a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java @@ -1,6 +1,7 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.util.Arrays; import java.util.function.Consumer; @@ -18,29 +19,29 @@ } @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { if (annotation.annotationType() == Select.class) { String sql = ((Select) annotation).sql(); - Paging paging = findPaging(args); - return paging == null ? executeQuery(sql, context, args, returnType) : executePagedQuery(sql, context, args, returnType, paging); + Paging paging = findPaging(arguments); + return paging == null ? executeQuery(sql, context, parameters, arguments, returnType) : + executePagedQuery(sql, context, parameters, arguments, returnType, paging); } - return nextChain.execute(annotation, context, args, returnType); + return nextChain.execute(annotation, context, parameters, arguments, returnType); } - private Paging findPaging(Object[] args) { - if (args == null) { + private Paging findPaging(Object[] arguments) { + if (arguments == null) { return null; } - return (Paging) Arrays.stream(args).filter(arg -> arg.getClass() == Paging.class) + return (Paging) Arrays.stream(arguments).filter(argument -> argument.getClass() == Paging.class) .findFirst() .orElse(null); } - private Object executePagedQuery(String sql, Context context, Object[] args, Type returnType, Paging paging) throws Exception { - QueryBuilder queryBuilder = new PagingQueryBuilder(sql, args, paging); + private Object executePagedQuery(String sql, Context context, Parameter[] parameters, Object[] arguments, Type returnType, Paging paging) throws Exception { + QueryBuilder queryBuilder = new PagingQueryBuilder(sql, arguments, paging); String pagingSQL = queryBuilder.buildSQL(); - Object[] generatedArgs = queryBuilder.getArgs(); - return executeQuery(pagingSQL, context, generatedArgs, returnType); + return executeQuery(pagingSQL, context, parameters, arguments, returnType); } } diff --git a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java index 1a42ea8..b5689c5 100644 --- a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java @@ -1,6 +1,7 @@ package com.ljunggren.storm.crud; import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; import java.lang.reflect.Type; import java.util.Arrays; import java.util.function.Consumer; @@ -20,24 +21,25 @@ } @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws Exception { + public Object execute(Annotation annotation, Context context, Parameter[] parameters, Object[] arguments, Type returnType) throws Exception { if (annotation.annotationType() == Update.class) { String sql = ((Update) annotation).sql(); - return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeUpdate(sql, context, args, returnType); + return sql.isEmpty() ? executeNonNativeQuery(context, arguments, returnType) : + executeUpdate(sql, context, parameters, arguments, returnType); } - return nextChain.execute(annotation, context, args, returnType); + return nextChain.execute(annotation, context, parameters, arguments, returnType); } - private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { - return Arrays.stream(args).map(ExceptionUtils.rethrowFunction(arg -> executeNonNativeQuery(context, arg, returnType))) + private int executeNonNativeQuery(Context context, Object[] arguments, Type returnType) { + return Arrays.stream(arguments).map(ExceptionUtils.rethrowFunction(argument -> executeNonNativeQuery(context, argument, returnType))) .collect(Collectors.summingInt(Integer::intValue)); } - private int executeNonNativeQuery(Context context, Object arg, Type returnType) throws Exception { - if (ReflectionUtils.isArray(arg.getClass())) { - return executeNonNativeQuery(context, (Object[]) arg, returnType); + private int executeNonNativeQuery(Context context, Object argument, Type returnType) throws Exception { + if (ReflectionUtils.isArray(argument.getClass())) { + return executeNonNativeQuery(context, (Object[]) argument, returnType); } - QueryBuilder queryBuilder = new UpdateQueryBuilder(arg); + QueryBuilder queryBuilder = new UpdateQueryBuilder(argument); String sql = queryBuilder.buildSQL(); Object[] generatedArgs = queryBuilder.getArgs(); return executeUpdate(sql, context, generatedArgs, returnType); diff --git a/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java b/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java index 1af6275..074ae74 100644 --- a/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java +++ b/src/main/java/com/ljunggren/storm/utils/ParameterUtils.java @@ -1,12 +1,13 @@ package com.ljunggren.storm.utils; import java.lang.reflect.Parameter; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.IntStream; -import java.util.stream.Stream; public class ParameterUtils { @@ -18,9 +19,13 @@ } public List findParameterIds(String sql) { - return Stream.of(sql.split(" ")).filter(string -> string.matches(parameterRegex)) - .map(parameter -> parameter.replace("#{", "").replace("}", "")) - .collect(Collectors.toList()); + List parameterIds = new ArrayList<>(); + Pattern pattern = Pattern.compile(parameterRegex); + Matcher matcher = pattern.matcher(sql); + while (matcher.find()) { + parameterIds.add(matcher.group(0).replace("#{", "").replace("}", "")); + } + return parameterIds; } public String replaceParamaterIdsWithQuestionMarks(String sql) { diff --git a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java index 8632e19..ec971da 100644 --- a/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/DeleteQueryTest.java @@ -25,7 +25,7 @@ @Database("H2") private interface UserRepository extends Peek { - @Delete(sql = "delete from users where id = ?") + @Delete(sql = "delete from users where id = #{id}") public int deleteById(int id); @Delete @@ -40,7 +40,7 @@ @Select(sql = "select count(*) from users") public long count(); - @Select(sql = "select * from users where id = ?") + @Select(sql = "select * from users where id = #{id}") public TestUser findById(int id); } diff --git a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java index 8fa0036..f85ad3a 100644 --- a/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/InsertQueryTest.java @@ -25,8 +25,8 @@ @Database("H2") private interface UserRepository extends Peek { - @Insert(sql = "insert into users (firstname, lastname, employee_id) values (?, ?, ?)") - public int insert(String firstName, String lastName, int employeeID); + @Insert(sql = "insert into users (firstname, lastname, employee_id) values (#{firstname}, #{lastname}, #{employeeID})") + public int insert(String firstname, String lastname, int employeeID); @Insert public int insert(TestUser user); diff --git a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java index bf4f31f..d0392c2 100644 --- a/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/SelectQueryTest.java @@ -34,17 +34,17 @@ @Select(sql = "select * from users order by id") public TestUser[] fetchAllOrdered(Paging paging); - @Select(sql = "select * from users where lastname = ?") - public List findByLastName(String lastName, Paging paging); + @Select(sql = "select * from users where lastname = #{lastname}") + public List findByLastName(String lastname, Paging paging); - @Select(sql = "select * from users where lastname = ?") - public List findByLastName(Paging paging, String lastName); + @Select(sql = "select * from users where lastname = #{lastname}") + public List findByLastName(Paging paging, String lastname); - @Select(sql = "select * from users where id = ?") + @Select(sql = "select * from users where id = #{id}") public TestUser findById(int id); - @Select(sql = "select * from users where firstname = ? and lastname = ?") - public TestUser findByFirstAndLastName(String firstName, String lastName); + @Select(sql = "select * from users where firstname = #{firstname} and lastname = #{lastname}") + public TestUser findByFirstAndLastName(String firstname, String lastname); @Select(sql = "select count(*) from users") public long count(); diff --git a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java index 16fbdef..fd6ced4 100644 --- a/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java +++ b/src/test/java/com/ljunggren/storm/crud/UpdateQueryTest.java @@ -25,8 +25,8 @@ @Database("H2") private interface UserRepository extends Peek { - @Update(sql = "update users set firstname = ? where id = ?") - public int updateFirstName(String name, int id); + @Update(sql = "update users set firstname = #{firstname} where id = #{id}") + public int updateFirstName(String firstname, int id); @Update public int update(TestUser user); @@ -37,7 +37,7 @@ @Update(sql = "nonsense") public int nonsense(); - @Select(sql = "select * from users where id = ?") + @Select(sql = "select * from users where id = #{id}") public TestUser findById(int id); } diff --git a/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java b/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java index f797165..b29d3b5 100644 --- a/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java +++ b/src/test/java/com/ljunggren/storm/utils/ParameterUtilsTest.java @@ -41,6 +41,15 @@ } @Test + public void findParameterIdsWithOutSpacesTest() { + String sql = "select * from users where id in (#{id1},#{id2})"; + List parameterIds = parameterUtils.findParameterIds(sql); + assertEquals(2, parameterIds.size()); + assertEquals("id1", parameterIds.get(0)); + assertEquals("id2", parameterIds.get(1)); + } + + @Test public void replaceParamaterIdsWithQuestionMarksTest() { String sql = "select * from users where id = #{id} and active = #{active}"; String actual = parameterUtils.replaceParamaterIdsWithQuestionMarks(sql);