diff --git a/src/main/java/com/ljunggren/storm/StormRepository.java b/src/main/java/com/ljunggren/storm/StormRepository.java index e479970..88dc88a 100644 --- a/src/main/java/com/ljunggren/storm/StormRepository.java +++ b/src/main/java/com/ljunggren/storm/StormRepository.java @@ -5,6 +5,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.Type; +import java.sql.SQLException; import java.util.Arrays; import java.util.Iterator; @@ -56,7 +57,7 @@ return execute(annotations, args, returnType); } - private Object execute(Iterator annotations, Object[] args, Type returnType) { + private Object execute(Iterator annotations, Object[] args, Type returnType) throws SQLException { if (annotations.hasNext()) { Object object = getQueryChain().execute(annotations.next(), context, args, returnType); return object == null ? execute(annotations, args, returnType) : object; diff --git a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java index d65484f..8377ee7 100644 --- a/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/DeleteQuery.java @@ -4,6 +4,7 @@ import java.lang.reflect.Type; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.Arrays; import java.util.stream.Collectors; @@ -15,7 +16,7 @@ public class DeleteQuery extends QueryChain { @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws SQLException { if (annotation.annotationType() == Delete.class) { String sql = ((Delete) annotation).sql(); return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeQuery(sql, context, args, returnType); @@ -24,11 +25,11 @@ } private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { - return Arrays.stream(args).map(arg -> executeNonNativeQuery(context, arg, returnType)) + return Arrays.stream(args).map(wrapper(arg -> executeNonNativeQuery(context, arg, returnType))) .collect(Collectors.summingInt(Integer::intValue)); } - private int executeNonNativeQuery(Context context, Object arg, Type returnType) { + private int executeNonNativeQuery(Context context, Object arg, Type returnType) throws SQLException { if (ReflectionUtils.isArray(arg.getClass())) { return executeNonNativeQuery(context, (Object[]) arg, returnType); } @@ -38,16 +39,13 @@ return executeQuery(sql, context, generatedArgs, returnType); } - private int executeQuery(String sql, Context context, Object[] args, Type returnType) { + private int executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { Connection connection = null; try { connection = context.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); setParameters(preparedStatement, args); return preparedStatement.executeUpdate(); - } catch (Exception e) { - e.printStackTrace(); - return 0; } finally { closeConnection(connection); } diff --git a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java index 5fc5417..b0b344f 100644 --- a/src/main/java/com/ljunggren/storm/crud/InsertQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/InsertQuery.java @@ -4,6 +4,7 @@ import java.lang.reflect.Type; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.Arrays; import java.util.stream.Collectors; @@ -15,7 +16,7 @@ public class InsertQuery extends QueryChain { @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws SQLException { if (annotation.annotationType() == Insert.class) { String sql = ((Insert) annotation).sql(); return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeQuery(sql, context, args, returnType); @@ -24,11 +25,11 @@ } private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { - return Arrays.stream(args).map(arg -> executeNonNativeQuery(context, arg, returnType)) + return Arrays.stream(args).map(wrapper(arg ->executeNonNativeQuery(context, arg, returnType))) .collect(Collectors.summingInt(Integer::intValue)); } - private int executeNonNativeQuery(Context context, Object arg, Type returnType) { + private int executeNonNativeQuery(Context context, Object arg, Type returnType) throws SQLException { if (ReflectionUtils.isArray(arg.getClass())) { return executeNonNativeQuery(context, (Object[]) arg, returnType); } @@ -38,16 +39,13 @@ return executeQuery(sql, context, generatedArgs, returnType); } - private int executeQuery(String sql, Context context, Object[] args, Type returnType) { + private int executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { Connection connection = null; try { connection = context.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); setParameters(preparedStatement, args); return preparedStatement.executeUpdate(); - } catch (Exception e) { - e.printStackTrace(); - return 0; } finally { closeConnection(connection); } diff --git a/src/main/java/com/ljunggren/storm/crud/QueryChain.java b/src/main/java/com/ljunggren/storm/crud/QueryChain.java index d114f96..970f80d 100644 --- a/src/main/java/com/ljunggren/storm/crud/QueryChain.java +++ b/src/main/java/com/ljunggren/storm/crud/QueryChain.java @@ -5,8 +5,10 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.function.Function; import com.ljunggren.storm.context.Context; +import com.ljunggren.storm.exceptions.FunctionWithException; public abstract class QueryChain { @@ -17,7 +19,7 @@ return this; } - public abstract Object execute(Annotation annotation, Context context, Object[] args, Type returnType); + public abstract Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws SQLException; protected void setParameters(PreparedStatement preparedStatement, Object[] args) throws SQLException { if (args != null) { @@ -27,14 +29,19 @@ } } - protected void closeConnection(Connection connection) { + protected void closeConnection(Connection connection) throws SQLException { if (connection != null) { - try { - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + connection.close(); } } + protected Function wrapper(FunctionWithException fe) { + return arg -> { + try { + return fe.apply(arg); + } catch (Exception e) { + throw new RuntimeException(e); + } + }; + } } diff --git a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java index a181506..e835aa9 100644 --- a/src/main/java/com/ljunggren/storm/crud/SelectQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/SelectQuery.java @@ -5,6 +5,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import com.ljunggren.storm.annotation.Select; import com.ljunggren.storm.context.Context; @@ -13,7 +14,7 @@ public class SelectQuery extends QueryChain { @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws SQLException { if (annotation.annotationType() == Select.class) { String sql = ((Select) annotation).sql(); return executeQuery(sql, context, args, returnType); @@ -21,7 +22,7 @@ return nextChain.execute(annotation, context, args, returnType); } - private Object executeQuery(String sql, Context context, Object[] args, Type returnType) { + private Object executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { Connection connection = null; try { connection = context.getConnection(); @@ -29,9 +30,6 @@ setParameters(preparedStatement, args); ResultSet resultSet = preparedStatement.executeQuery(); return new ResultSetMapper(resultSet, returnType).map(); - } catch (Exception e) { - e.printStackTrace(); - return null; } finally { closeConnection(connection); } diff --git a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java index 69d6170..b49a36f 100644 --- a/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java +++ b/src/main/java/com/ljunggren/storm/crud/UpdateQuery.java @@ -4,6 +4,7 @@ import java.lang.reflect.Type; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.Arrays; import java.util.stream.Collectors; @@ -15,7 +16,7 @@ public class UpdateQuery extends QueryChain { @Override - public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) { + public Object execute(Annotation annotation, Context context, Object[] args, Type returnType) throws SQLException { if (annotation.annotationType() == Update.class) { String sql = ((Update) annotation).sql(); return sql.isEmpty() ? executeNonNativeQuery(context, args, returnType) : executeQuery(sql, context, args, returnType); @@ -24,11 +25,11 @@ } private int executeNonNativeQuery(Context context, Object[] args, Type returnType) { - return Arrays.stream(args).map(arg -> executeNonNativeQuery(context, arg, returnType)) + return Arrays.stream(args).map(wrapper(arg -> executeNonNativeQuery(context, arg, returnType))) .collect(Collectors.summingInt(Integer::intValue)); } - private int executeNonNativeQuery(Context context, Object arg, Type returnType) { + private int executeNonNativeQuery(Context context, Object arg, Type returnType) throws SQLException { if (ReflectionUtils.isArray(arg.getClass())) { return executeNonNativeQuery(context, (Object[]) arg, returnType); } @@ -38,16 +39,13 @@ return executeQuery(sql, context, generatedArgs, returnType); } - private int executeQuery(String sql, Context context, Object[] args, Type returnType) { + private int executeQuery(String sql, Context context, Object[] args, Type returnType) throws SQLException { Connection connection = null; try { connection = context.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); setParameters(preparedStatement, args); return preparedStatement.executeUpdate(); - } catch (Exception e) { - e.printStackTrace(); - return 0; } finally { closeConnection(connection); } diff --git a/src/main/java/com/ljunggren/storm/exceptions/FunctionWithException.java b/src/main/java/com/ljunggren/storm/exceptions/FunctionWithException.java new file mode 100644 index 0000000..e471d44 --- /dev/null +++ b/src/main/java/com/ljunggren/storm/exceptions/FunctionWithException.java @@ -0,0 +1,8 @@ +package com.ljunggren.storm.exceptions; + +@FunctionalInterface +public interface FunctionWithException { + + R apply(T t) throws E; + +} \ No newline at end of file