From 2065e905dd039558700a302bcec49232266b844b Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Sun, 1 Feb 2009 17:34:04 +0000 Subject: [PATCH] OPENJPA-703: Use Prepared Query for packed results. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@739796 13f79535-47bb-0310-9956-ffa450edef68 --- .../jdbc/kernel/PreparedQueryImpl.java | 45 ++++++++++++------- .../org/apache/openjpa/kernel/QueryImpl.java | 6 ++- .../jdbc/sqlcache/TestPreparedQueryCache.java | 9 ++++ 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java index 82fa424d3..5b4f56e20 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryImpl.java @@ -32,6 +32,7 @@ import org.apache.openjpa.jdbc.sql.SelectExecutor; import org.apache.openjpa.kernel.Broker; import org.apache.openjpa.kernel.PreparedQuery; import org.apache.openjpa.kernel.Query; +import org.apache.openjpa.kernel.QueryImpl; import org.apache.openjpa.kernel.QueryLanguages; import org.apache.openjpa.lib.rop.ResultList; import org.apache.openjpa.util.ImplHelper; @@ -125,23 +126,35 @@ public class PreparedQueryImpl implements PreparedQuery { * {@link ResultList#getUserObject() user object}. */ public boolean initialize(Object result) { - boolean initialized = false; - if (result instanceof ResultList) { - Object provider = ((ResultList)result).getUserObject(); - if (provider instanceof SelectResultObjectProvider) { - SelectResultObjectProvider rop = - (SelectResultObjectProvider)provider; - SelectExecutor selector = rop.getSelect(); - SQLBuffer buffer = selector == null ? null : selector.getSQL(); - if (buffer != null && !selector.hasMultipleSelects()) { - setTargetQuery(buffer.getSQL()); - setParameters(buffer.getParameters()); - setUserParameterPositions(buffer.getUserParameters()); - initialized = true; - } - } + SelectExecutor selector = extractSelectExecutor(result); + if (selector == null) + return false; + SQLBuffer buffer = selector == null ? null : selector.getSQL(); + if (buffer != null && !selector.hasMultipleSelects()) { + setTargetQuery(buffer.getSQL()); + setParameters(buffer.getParameters()); + setUserParameterPositions(buffer.getUserParameters()); + return true; } - return initialized; + return false; + } + + /** + * Extract the underlying SelectExecutor from the given argument, if + * possible. + */ + private SelectExecutor extractSelectExecutor(Object result) { + if (result instanceof ResultList == false) + return null; + Object provider = ((ResultList)result).getUserObject(); + if (provider instanceof QueryImpl.PackingResultObjectProvider) { + provider = ((QueryImpl.PackingResultObjectProvider)provider) + .getDelegate(); + } + if (provider instanceof SelectResultObjectProvider) { + return ((SelectResultObjectProvider)provider).getSelect(); + } + return null; } /** diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryImpl.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryImpl.java index 308f6db34..5143cfc33 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryImpl.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryImpl.java @@ -1982,7 +1982,7 @@ public class QueryImpl /** * Result object provider that packs results before returning them. */ - private static class PackingResultObjectProvider + public static class PackingResultObjectProvider implements ResultObjectProvider { private final ResultObjectProvider _delegate; @@ -2045,6 +2045,10 @@ public class QueryImpl public void handleCheckedException(Exception e) { _delegate.handleCheckedException(e); } + + public ResultObjectProvider getDelegate() { + return _delegate; + } } /** diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java index fae91656a..d80c61b04 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java @@ -415,6 +415,15 @@ public class TestPreparedQueryCache extends SQLListenerTestCase { compare(!IS_NAMED_QUERY, jpql, EMPLOYEE_NAMES.length*DEPARTMENT_NAMES.length, params); } + /** + * Project results are returned with different types of ROP. + */ + public void testProjectionResult() { + String jpql = "select e.name from Employee e where e.address.city=:city"; + Object[] params = {"city", CITY_NAMES[0]}; + compare(!IS_NAMED_QUERY, jpql, COMPANY_NAMES.length*DEPARTMENT_NAMES.length, params); + } + /** * Compare the result of execution of the same query with and without * Prepared Query Cache.