From 53dc345c610756d180f9e664784175e8bc0ed146 Mon Sep 17 00:00:00 2001 From: bernd Date: Thu, 4 Aug 2011 02:11:38 +0200 Subject: [PATCH 13/16] Refactor: use a cleaner way for the exception translator to get the SQL query. --- .../src/net/lemnik/eodsql/impl/BaseQueryImpl.java | 14 +++++--- .../eodsql/impl/ExceptionTranslationUtils.java | 32 ++++---------------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/eodsql/src/net/lemnik/eodsql/impl/BaseQueryImpl.java b/eodsql/src/net/lemnik/eodsql/impl/BaseQueryImpl.java index 1c71ff0..6b56cdf 100644 --- a/eodsql/src/net/lemnik/eodsql/impl/BaseQueryImpl.java +++ b/eodsql/src/net/lemnik/eodsql/impl/BaseQueryImpl.java @@ -163,7 +163,7 @@ class BaseQueryImpl implements InvocationHandler { } // if we got here, the Exception was not declared, wrap it in a RuntimeException - throw ExceptionTranslationUtils.translateException(connectionSource, method, impl, exception); + throw ExceptionTranslationUtils.translateException(connectionSource, method, args, impl, exception); } } @@ -192,7 +192,11 @@ class BaseQueryImpl implements InvocationHandler { } } - class MethodImpl implements Callable { + interface SQLCallable extends Callable { + String getSQL(Method method, Object[] args); + } + + class MethodImpl implements SQLCallable { private final MethodImplementation implementation; @@ -227,9 +231,9 @@ class BaseQueryImpl implements InvocationHandler { } } } - - AbstractMethodImplementation getMethodImpl() { - return (AbstractMethodImplementation)implementation; + + public String getSQL(Method method, Object[] args) { + return ((AbstractMethodImplementation)implementation).query.toString(); } } diff --git a/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java b/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java index 8db4740..f15f0da 100644 --- a/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java +++ b/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java @@ -1,6 +1,5 @@ package net.lemnik.eodsql.impl; -import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; @@ -11,7 +10,7 @@ import net.lemnik.eodsql.EoDException; import net.lemnik.eodsql.impl.BaseQueryImpl.Callable; import net.lemnik.eodsql.impl.BaseQueryImpl.ConnectionSource; import net.lemnik.eodsql.impl.BaseQueryImpl.DataSourceConnectionSource; -import net.lemnik.eodsql.impl.BaseQueryImpl.MethodImpl; +import net.lemnik.eodsql.impl.BaseQueryImpl.SQLCallable; /** * A class that supports exception translation of {@link SQLException} into a @@ -63,10 +62,10 @@ public class ExceptionTranslationUtils { * Translates the exception ex to a RuntimeException. */ public static RuntimeException translateException( - ConnectionSource connectionSource, final Method method, + ConnectionSource connectionSource, final Method method, final Object[] args, final Callable callable, final Exception ex) { return translateException(connectionSource, method.getName(), - getSql(callable), ex); + getSql(callable, method, args), ex); } /** @@ -101,15 +100,6 @@ public class ExceptionTranslationUtils { * Translates the exception ex to a RuntimeException. */ public static RuntimeException translateException(DataSource dataSource, - final Method method, final Callable callable, final Exception ex) { - return translateException(dataSource, method.getName(), - getSql(callable), ex); - } - - /** - * Translates the exception ex to a RuntimeException. - */ - public static RuntimeException translateException(DataSource dataSource, final String task, final String sql, final Exception ex) { if(ex instanceof SQLException) { return exceptionTranslator.translateException(dataSource, task, @@ -123,15 +113,6 @@ public class ExceptionTranslationUtils { * Translates the exception ex to a RuntimeException. */ public static RuntimeException translateException(Connection connection, - final Method method, final Callable callable, final Exception ex) { - return translateException(connection, method.getName(), - getSql(callable), ex); - } - - /** - * Translates the exception ex to a RuntimeException. - */ - public static RuntimeException translateException(Connection connection, final String task, final String sql, final Exception ex) { if(ex instanceof SQLException) { return exceptionTranslator.translateException(connection, task, @@ -153,10 +134,9 @@ public class ExceptionTranslationUtils { return task + "[SQL: '" + sql + "']."; } - private static String getSql(final Callable callable) { - if(callable instanceof MethodImpl) { - final AbstractMethodImplementation m = ((MethodImpl)callable).getMethodImpl(); - return m.query.toString(); + private static String getSql(final Callable callable, final Method method, final Object[] args) { + if(callable instanceof SQLCallable) { + return ((SQLCallable)callable).getSQL(method, args); } else { return "?"; } -- 1.7.6