From 4a5f74e011bd2ea9f8a93a5c0f3dd04b94479379 Mon Sep 17 00:00:00 2001 From: bernd Date: Wed, 14 Nov 2012 21:24:31 +0100 Subject: [PATCH 15/25] Add custom exception translation mechanism to QueryTool.select() and QueryTool.update(). --- eodsql/src/net/lemnik/eodsql/QueryTool.java | 37 +++++++++++++++++----- .../eodsql/impl/ExceptionTranslationUtils.java | 7 ++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/eodsql/src/net/lemnik/eodsql/QueryTool.java b/eodsql/src/net/lemnik/eodsql/QueryTool.java index b8d0183..c1ca3ed 100644 --- a/eodsql/src/net/lemnik/eodsql/QueryTool.java +++ b/eodsql/src/net/lemnik/eodsql/QueryTool.java @@ -18,6 +18,7 @@ import java.util.IdentityHashMap; import javax.sql.DataSource; +import net.lemnik.eodsql.impl.ExceptionTranslationUtils; import net.lemnik.eodsql.spi.QueryFactory; import net.lemnik.eodsql.spi.MethodImplementationFactory; @@ -410,8 +411,13 @@ public class QueryTool { query, parameters); } catch(final SQLException sqle) { - throw new InvalidQueryException( - "Couldn't execute query: '" + query + "'", sqle); + if (ExceptionTranslationUtils.isDefaultTranslator()) { + throw new InvalidQueryException( + "Couldn't execute query: '" + query + "'", sqle); + } else { + throw ExceptionTranslationUtils.translateException(dataSource, "QueryTool.select()", + query, sqle); + } } catch(final ParseException pe) { throw new InvalidQueryException( "Cannot parser EoD SQL query: '" + query + "'", pe); @@ -478,8 +484,13 @@ public class QueryTool { query, parameters); } catch(SQLException sqle) { - throw new InvalidQueryException( - "Couldn't execute query: '" + query + "'", sqle); + if (ExceptionTranslationUtils.isDefaultTranslator()) { + throw new InvalidQueryException( + "Couldn't execute query: '" + query + "'", sqle); + } else { + throw ExceptionTranslationUtils.translateException(connection, "QueryTool.select()", + query, sqle); + } } catch(ParseException pe) { throw new InvalidQueryException( "Cannot parser EoD SQL query: '" + query + "'", pe); @@ -649,8 +660,13 @@ public class QueryTool { query, parameters); } catch(SQLException sqle) { - throw new InvalidQueryException( - "Couldn't execute query: '" + query + "'", sqle); + if (ExceptionTranslationUtils.isDefaultTranslator()) { + throw new InvalidQueryException( + "Couldn't execute query: '" + query + "'", sqle); + } else { + throw ExceptionTranslationUtils.translateException(connection, "QueryTool.select()", + query, sqle); + } } catch(ParseException pe) { throw new InvalidQueryException( "Cannot parser EoD SQL query: '" + query + "'", pe); @@ -708,8 +724,13 @@ public class QueryTool { query, parameters); } catch(SQLException sqle) { - throw new InvalidQueryException( - "Couldn't execute query: '" + query + "'", sqle); + if (ExceptionTranslationUtils.isDefaultTranslator()) { + throw new InvalidQueryException( + "Couldn't execute query: '" + query + "'", sqle); + } else { + throw ExceptionTranslationUtils.translateException(dataSource, "QueryTool.select()", + query, sqle); + } } catch(ParseException pe) { throw new InvalidQueryException( "Cannot parser EoD SQL query: '" + query + "'", pe); diff --git a/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java b/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java index 175c740..8db4740 100644 --- a/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java +++ b/eodsql/src/net/lemnik/eodsql/impl/ExceptionTranslationUtils.java @@ -43,6 +43,13 @@ public class ExceptionTranslationUtils { "A unique result was expected but the database returned multiple rows."); } } + + /** + * Return true if the exception translator is the EoDSQL default translator. + */ + public static boolean isDefaultTranslator() { + return exceptionTranslator instanceof DefaultExceptionTranslator; + } /** * Sets the {@link ExceptionTranslator} for EoDSQL. Call once before EoDSQL -- 1.8.0