mirror of https://github.com/apache/openjpa.git
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
This commit is contained in:
parent
4ed05803e1
commit
2065e905dd
|
@ -32,6 +32,7 @@ import org.apache.openjpa.jdbc.sql.SelectExecutor;
|
||||||
import org.apache.openjpa.kernel.Broker;
|
import org.apache.openjpa.kernel.Broker;
|
||||||
import org.apache.openjpa.kernel.PreparedQuery;
|
import org.apache.openjpa.kernel.PreparedQuery;
|
||||||
import org.apache.openjpa.kernel.Query;
|
import org.apache.openjpa.kernel.Query;
|
||||||
|
import org.apache.openjpa.kernel.QueryImpl;
|
||||||
import org.apache.openjpa.kernel.QueryLanguages;
|
import org.apache.openjpa.kernel.QueryLanguages;
|
||||||
import org.apache.openjpa.lib.rop.ResultList;
|
import org.apache.openjpa.lib.rop.ResultList;
|
||||||
import org.apache.openjpa.util.ImplHelper;
|
import org.apache.openjpa.util.ImplHelper;
|
||||||
|
@ -125,23 +126,35 @@ public class PreparedQueryImpl implements PreparedQuery {
|
||||||
* {@link ResultList#getUserObject() user object}.
|
* {@link ResultList#getUserObject() user object}.
|
||||||
*/
|
*/
|
||||||
public boolean initialize(Object result) {
|
public boolean initialize(Object result) {
|
||||||
boolean initialized = false;
|
SelectExecutor selector = extractSelectExecutor(result);
|
||||||
if (result instanceof ResultList) {
|
if (selector == null)
|
||||||
Object provider = ((ResultList)result).getUserObject();
|
return false;
|
||||||
if (provider instanceof SelectResultObjectProvider) {
|
|
||||||
SelectResultObjectProvider rop =
|
|
||||||
(SelectResultObjectProvider)provider;
|
|
||||||
SelectExecutor selector = rop.getSelect();
|
|
||||||
SQLBuffer buffer = selector == null ? null : selector.getSQL();
|
SQLBuffer buffer = selector == null ? null : selector.getSQL();
|
||||||
if (buffer != null && !selector.hasMultipleSelects()) {
|
if (buffer != null && !selector.hasMultipleSelects()) {
|
||||||
setTargetQuery(buffer.getSQL());
|
setTargetQuery(buffer.getSQL());
|
||||||
setParameters(buffer.getParameters());
|
setParameters(buffer.getParameters());
|
||||||
setUserParameterPositions(buffer.getUserParameters());
|
setUserParameterPositions(buffer.getUserParameters());
|
||||||
initialized = true;
|
return true;
|
||||||
}
|
}
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
return initialized;
|
if (provider instanceof SelectResultObjectProvider) {
|
||||||
|
return ((SelectResultObjectProvider)provider).getSelect();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1982,7 +1982,7 @@ public class QueryImpl
|
||||||
/**
|
/**
|
||||||
* Result object provider that packs results before returning them.
|
* Result object provider that packs results before returning them.
|
||||||
*/
|
*/
|
||||||
private static class PackingResultObjectProvider
|
public static class PackingResultObjectProvider
|
||||||
implements ResultObjectProvider {
|
implements ResultObjectProvider {
|
||||||
|
|
||||||
private final ResultObjectProvider _delegate;
|
private final ResultObjectProvider _delegate;
|
||||||
|
@ -2045,6 +2045,10 @@ public class QueryImpl
|
||||||
public void handleCheckedException(Exception e) {
|
public void handleCheckedException(Exception e) {
|
||||||
_delegate.handleCheckedException(e);
|
_delegate.handleCheckedException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResultObjectProvider getDelegate() {
|
||||||
|
return _delegate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -415,6 +415,15 @@ public class TestPreparedQueryCache extends SQLListenerTestCase {
|
||||||
compare(!IS_NAMED_QUERY, jpql, EMPLOYEE_NAMES.length*DEPARTMENT_NAMES.length, params);
|
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
|
* Compare the result of execution of the same query with and without
|
||||||
* Prepared Query Cache.
|
* Prepared Query Cache.
|
||||||
|
|
Loading…
Reference in New Issue