fixing indentation and few changes suggested by Abe for OpenJPA patch-168

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@523071 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David J. Wisneski 2007-03-27 20:47:21 +00:00
parent f7aef45814
commit fee930886b
11 changed files with 155 additions and 141 deletions

View File

@ -363,18 +363,18 @@ public class JDBCStoreManager
* null if there is no data in the current fetch groups to select. * null if there is no data in the current fetch groups to select.
*/ */
private Result getInitializeStateResult(OpenJPAStateManager sm, private Result getInitializeStateResult(OpenJPAStateManager sm,
ClassMapping mapping, JDBCFetchConfiguration fetch, int subs) ClassMapping mapping, JDBCFetchConfiguration fetch, int subs)
throws SQLException { throws SQLException {
Select sel = _sql.newSelect(); Select sel = _sql.newSelect();
if (!select(sel, mapping, subs, sm, null, fetch, if (!select(sel, mapping, subs, sm, null, fetch,
JDBCFetchConfiguration.EAGER_JOIN, true, false)) JDBCFetchConfiguration.EAGER_JOIN, true, false))
return null; return null;
sel.wherePrimaryKey(sm.getObjectId(), mapping, this); sel.wherePrimaryKey(sm.getObjectId(), mapping, this);
//Set the expectedResultCount for the select as 1 as a single // Set the expectedResultCount for the select as 1 as a single
//object is being loaded. force = true is an indicator that it is // object is being loaded. force = true is an indicator that it is
//internally generated value // internally generated value
sel.setExpectedResultCount(1,true); sel.setExpectedResultCount(1,true);
return sel.execute(this, fetch); return sel.execute(this, fetch);
} }
/** /**

View File

@ -48,6 +48,7 @@ import org.apache.openjpa.jdbc.sql.Select;
import org.apache.openjpa.jdbc.sql.Union; import org.apache.openjpa.jdbc.sql.Union;
import org.apache.openjpa.kernel.ExpressionStoreQuery; import org.apache.openjpa.kernel.ExpressionStoreQuery;
import org.apache.openjpa.kernel.OrderingMergedResultObjectProvider; import org.apache.openjpa.kernel.OrderingMergedResultObjectProvider;
import org.apache.openjpa.kernel.QueryHints;
import org.apache.openjpa.kernel.exps.ExpressionFactory; import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.ExpressionParser; import org.apache.openjpa.kernel.exps.ExpressionParser;
import org.apache.openjpa.kernel.exps.FilterListener; import org.apache.openjpa.kernel.exps.FilterListener;
@ -319,23 +320,22 @@ public class JDBCStoreQuery
subclassMode); subclassMode);
if (verts.length == 1 && subclasses) if (verts.length == 1 && subclasses)
subclassBits.set(sels.size()); subclassBits.set(sels.size());
// create criteria select and clone for each vert mapping // create criteria select and clone for each vert mapping
sel = ((JDBCExpressionFactory) facts[i]).getSelectConstructor(). sel = ((JDBCExpressionFactory) facts[i]).getSelectConstructor().
evaluate(ctx, null, null, exps[i], states[i]); evaluate(ctx, null, null, exps[i], states[i]);
//it means it is coming from getSingleResult so set the // It means it is coming from getSingleResult so set the
//expectedResultCount to 1.force = true indicates that this is // expectedResultCount to 1.force = true indicates that this is
//internally generated value // internally generated value
if(this.ctx.isUnique()) if (this.ctx.isUnique())
sel.setExpectedResultCount(1,true); sel.setExpectedResultCount(1,true);
//it means this is coming from getResultList so set the // It means this is coming from getResultList so set the
//expectedResultCount based on any optimize hint if provided // expectedResultCount based on any optimize hint if provided
else{ else {
if((optHint = ctx.fetch.getHint if ((optHint = ctx.fetch.getHint
(this.optimizeHint))!= null) (QueryHints.HINT_RESULT_COUNT))!= null)
sel.setExpectedResultCount sel.setExpectedResultCount
(((Integer)optHint).intValue(),false); (((Integer)optHint).intValue(),false);
} }
for (int j = 0; j < verts.length; j++) { for (int j = 0; j < verts.length; j++) {
selMappings.add(verts[j]); selMappings.add(verts[j]);
if (j == verts.length - 1) { if (j == verts.length - 1) {

View File

@ -28,8 +28,7 @@ import org.apache.openjpa.jdbc.schema.Sequence;
*/ */
public class DB2Dictionary public class DB2Dictionary
extends AbstractDB2Dictionary { extends AbstractDB2Dictionary {
// variables to support optimize clause
//variables to support optimize clause
public String optimizeClause = "optimize for"; public String optimizeClause = "optimize for";
public String rowClause = "row"; public String rowClause = "row";
public DB2Dictionary() { public DB2Dictionary() {
@ -195,67 +194,63 @@ public class DB2Dictionary
bigintTypeName = "DECIMAL(31,0)"; bigintTypeName = "DECIMAL(31,0)";
} }
} }
} }
} }
//based on the expectedResultCount of the select create the optimize
//for clause /** Based on the expectedResultCount of the select create the optimize
* for clause
*/
public String getOptimizeClause(JDBCFetchConfiguration fetch, public String getOptimizeClause(JDBCFetchConfiguration fetch,
int expectedResultCount) { int expectedResultCount) {
Integer rows = null; Integer rows = null;
StringBuffer optimizeString = new StringBuffer(); StringBuffer optimizeString = new StringBuffer();
if (expectedResultCount != 0)
if(expectedResultCount != 0) optimizeString.append(" ").append(optimizeClause).append(" ")
optimizeString.append(" ").append(optimizeClause).append(" ") .append(expectedResultCount).append(" ")
.append(expectedResultCount).append(" ") .append(rowClause).append(" ");
.append(rowClause).append(" "); return optimizeString.toString();
return optimizeString.toString();
} }
//override the DBDictionary toSelect to call getOptimizeClause and append /** Override the DBDictionary toSelect to call getOptimizeClause and append
//to the select string * to the select string
*/
public SQLBuffer toSelect(SQLBuffer selects, JDBCFetchConfiguration fetch, public SQLBuffer toSelect(SQLBuffer selects, JDBCFetchConfiguration fetch,
SQLBuffer from, SQLBuffer where, SQLBuffer group, SQLBuffer from, SQLBuffer where, SQLBuffer group,
SQLBuffer having, SQLBuffer order, SQLBuffer having, SQLBuffer order,
boolean distinct, boolean forUpdate, long start, long end, boolean distinct, boolean forUpdate, long start, long end,
int expectedResultCount) { int expectedResultCount) {
String optimizeString = null;
SQLBuffer selString = toOperation(getSelectOperation(fetch),
selects, from, where,
group, having, order, distinct,
forUpdate, start, end);
if (fetch != null)
optimizeString = getOptimizeClause(fetch, expectedResultCount);
if (optimizeString != null && optimizeString.length() > 0)
selString.append(optimizeString);
return selString;
}
String optimizeString = null; /** Override the DBDictionary toSelect to pass expectedResultcount to the
SQLBuffer selString = toOperation(getSelectOperation(fetch), * other toSelect method
selects, from, where, */
group, having, order, distinct,
forUpdate, start, end);
//return toOperation(getSelectOperation(fetch), selects, from, where,
//group, having, order, distinct, forUpdate, start, end);
if(fetch != null)
optimizeString = getOptimizeClause(fetch, expectedResultCount);
if(optimizeString != null && optimizeString.length() > 0)
selString.append(optimizeString);
return selString;
}
//override the DBDictionary toSelect to pass expectedResultcount to the
//other toSelect method
public SQLBuffer toSelect(Select sel, boolean forUpdate, public SQLBuffer toSelect(Select sel, boolean forUpdate,
JDBCFetchConfiguration fetch) { JDBCFetchConfiguration fetch) {
sel.addJoinClassConditions(); sel.addJoinClassConditions();
boolean update = forUpdate && sel.getFromSelect() == null;
boolean update = forUpdate && sel.getFromSelect() == null; SQLBuffer select = getSelects(sel, false, update);
SQLBuffer select = getSelects(sel, false, update); SQLBuffer ordering = null;
SQLBuffer ordering = null; if (!sel.isAggregate() || sel.getGrouping() != null)
if (!sel.isAggregate() || sel.getGrouping() != null) ordering = sel.getOrdering();
ordering = sel.getOrdering(); SQLBuffer from;
SQLBuffer from; if (sel.getFromSelect() != null)
if (sel.getFromSelect() != null) from = getFromSelect(sel, forUpdate);
from = getFromSelect(sel, forUpdate); else
else from = getFrom(sel, update);
from = getFrom(sel, update); SQLBuffer where = getWhere(sel, update);
SQLBuffer where = getWhere(sel, update); return toSelect(select, fetch, from, where, sel.getGrouping(),
return toSelect(select, fetch, from, where, sel.getGrouping(),
sel.getHaving(), ordering, sel.isDistinct(), forUpdate, sel.getHaving(), ordering, sel.isDistinct(), forUpdate,
sel.getStartIndex(), sel.getStartIndex(),
sel.getEndIndex(),sel.getExpectedResultCount()); sel.getEndIndex(),sel.getExpectedResultCount());
} }
} }

View File

@ -47,11 +47,7 @@ public class LogicalUnion
private static final Localizer _loc = Localizer.forPackage private static final Localizer _loc = Localizer.forPackage
(LogicalUnion.class); (LogicalUnion.class);
//expected number of results for this select to be used in
// optimize for clause
protected int expectedResultCount = 0; protected int expectedResultCount = 0;
//indicate if this is internally generated result count
//or not
protected boolean force = false; protected boolean force = false;
protected final UnionSelect[] sels; protected final UnionSelect[] sels;
protected final DBDictionary dict; protected final DBDictionary dict;
@ -215,9 +211,10 @@ public class LogicalUnion
if (this.getExpectedResultCount()== 1) { if (this.getExpectedResultCount()== 1) {
AbstractResult res; AbstractResult res;
for (int i = 0; i < sels.length; i++) { for (int i = 0; i < sels.length; i++) {
//for each select set the expected result count to 1 // For each select set the expected result count to 1
//and force true indicating that this internally generated value // and force true indicating that this internally generated
sels[i].sel.setExpectedResultCount(1,true); // value
sels[i].sel.setExpectedResultCount(1,true);
res = (AbstractResult) sels[i].execute(store, fetch, res = (AbstractResult) sels[i].execute(store, fetch,
lockLevel); lockLevel);
res.setBaseMapping(mappings[i]); res.setBaseMapping(mappings[i]);
@ -305,10 +302,7 @@ public class LogicalUnion
protected final int pos; protected final int pos;
protected int orders = 0; protected int orders = 0;
protected List orderIdxs = null; protected List orderIdxs = null;
// expected number of results for this select to be used in
// optimize for clause
protected int expectedResultCount = 0; protected int expectedResultCount = 0;
//force indicates it is internally generated result count or not
protected boolean force = false; protected boolean force = false;
public UnionSelect(SelectImpl sel, int pos) { public UnionSelect(SelectImpl sel, int pos) {
@ -844,17 +838,19 @@ public class LogicalUnion
return sel.toString(); return sel.toString();
} }
public int getExpectedResultCount() { public int getExpectedResultCount() {
return expectedResultCount; return expectedResultCount;
} }
public void setExpectedResultCount(int expectedResultCount, boolean force) { public void setExpectedResultCount(int expectedResultCount,
this.expectedResultCount = expectedResultCount; boolean force) {
this.expectedResultCount = expectedResultCount;
this.force = force; this.force = force;
} }
public boolean isExpRsltCntForced() {
return force; public boolean isExpRsltCntForced() {
} return force;
}
} }
/** /**
@ -940,10 +936,12 @@ public class LogicalUnion
return expectedResultCount; return expectedResultCount;
} }
public void setExpectedResultCount(int expectedResultCount,boolean force) { public void setExpectedResultCount(int expectedResultCount,
boolean force) {
this.expectedResultCount = expectedResultCount; this.expectedResultCount = expectedResultCount;
this.force = force; this.force = force;
} }
public boolean isExpRsltCntForced() { public boolean isExpRsltCntForced() {
return force; return force;
} }

View File

@ -130,12 +130,10 @@ public interface SelectExecutor {
* force indicates whether the count is internally generated * force indicates whether the count is internally generated
* or given by the user as optimize hint * or given by the user as optimize hint
*/ */
public void setExpectedResultCount(int expectedResultCount,boolean force) ; public void setExpectedResultCount(int expectedResultCount,boolean force) ;
/** /**
* Indicates whether the expectedResultCount is internally generated * Indicates whether the expectedResultCount is internally generated
*/ */
public boolean isExpRsltCntForced();
public boolean isExpRsltCntForced();
} }

View File

@ -154,11 +154,7 @@ public class SelectImpl
private SelectImpl _from = null; private SelectImpl _from = null;
private SelectImpl _outer = null; private SelectImpl _outer = null;
//expected number of results for this select to be used in
// optimize for clause
private int expectedResultCount = 0; private int expectedResultCount = 0;
//true if the expectedResultCount is internally set false if
//it is set by the user
private boolean force = false; private boolean force = false;
/** /**
@ -312,18 +308,18 @@ public class SelectImpl
throws SQLException { throws SQLException {
boolean forUpdate = false; boolean forUpdate = false;
//expectedResultCount = 1 and force means that it is internally generated value // ExpectedResultCount = 1 and force means that it is internally
//for getSingleResult,single valued relationship.We need to check if // generated value for getSingleResult,single valued relationship.
//there are any eager joins in the select if there are then the // We need to check if there are any eager joins in the select if
//optimize for 1 row clause is not generated else we do. if // there are then the optimize for 1 row clause is not generated
//!force then it is set by the user through hint and we // else we do. if !force then it is set by the user through hint
//do not check the eager joins // and we do not check the eager joins
if(this.expectedResultCount == 1 && force ){ if (this.expectedResultCount == 1 && force ) {
if(this.hasEagerJoin(true)) if (this.hasEagerJoin(true))
this.setExpectedResultCount(0,false); this.setExpectedResultCount(0,false);
else else
this.setExpectedResultCount(1,false); this.setExpectedResultCount(1,false);
} }
if (!isAggregate() && _grouping == null) { if (!isAggregate() && _grouping == null) {
JDBCLockManager lm = store.getLockManager(); JDBCLockManager lm = store.getLockManager();
@ -2821,14 +2817,15 @@ public class SelectImpl
} }
} }
public int getExpectedResultCount() { public int getExpectedResultCount() {
return expectedResultCount; return expectedResultCount;
} }
public void setExpectedResultCount(int expectedResultCount, boolean force) { public void setExpectedResultCount(int expectedResultCount,
this.expectedResultCount = expectedResultCount; boolean force) {
this.expectedResultCount = expectedResultCount;
this.force = force; this.force = force;
} }
public boolean isExpRsltCntForced() { public boolean isExpRsltCntForced() {
return force; return force;

View File

@ -33,7 +33,6 @@ public abstract class AbstractStoreQuery
implements StoreQuery { implements StoreQuery {
protected QueryContext ctx = null; protected QueryContext ctx = null;
public static final String optimizeHint ="openjpa.hint.OptimizeResultCount";
public QueryContext getContext() { public QueryContext getContext() {
return ctx; return ctx;

View File

@ -0,0 +1,25 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openjpa.kernel;
public interface QueryHints {
/** Hint to specify the number of rows for the optimize
* clause for DB2
*/
public static final String HINT_RESULT_COUNT =
"openjpa.hint.OptimizeResultCount";
}

View File

@ -1290,7 +1290,7 @@ public class QueryImpl
return Arrays.asList(new Object[]{ single }); return Arrays.asList(new Object[]{ single });
} }
if(single == null) if (single == null)
throw new InvalidStateException(_loc.get("is-null", throw new InvalidStateException(_loc.get("is-null",
_class, _query)); _class, _query));

View File

@ -24,6 +24,7 @@ import javax.persistence.Query;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import org.apache.openjpa.kernel.QueryFlushModes; import org.apache.openjpa.kernel.QueryFlushModes;
import org.apache.openjpa.kernel.QueryHints;
import org.apache.openjpa.kernel.QueryOperations; import org.apache.openjpa.kernel.QueryOperations;
import org.apache.openjpa.kernel.exps.AggregateListener; import org.apache.openjpa.kernel.exps.AggregateListener;
import org.apache.openjpa.kernel.exps.FilterListener; import org.apache.openjpa.kernel.exps.FilterListener;
@ -36,7 +37,7 @@ import org.apache.openjpa.kernel.exps.FilterListener;
* @published * @published
*/ */
public interface OpenJPAQuery public interface OpenJPAQuery
extends Query, QueryOperations, QueryFlushModes { extends Query, QueryOperations, QueryFlushModes, QueryHints {
/** /**
* The owning entity manage. * The owning entity manage.

View File

@ -279,13 +279,12 @@ public class QueryImpl
*/ */
public Object getSingleResult() { public Object getSingleResult() {
_em.assertNotCloseInvoked(); _em.assertNotCloseInvoked();
//Indicate that this query returns single result.Later copied into // Indicate that this query returns single result.Later copied into
//select.expectedResultCount // select.expectedResultCount
_query.setUnique(true); _query.setUnique(true);
try{ try {
Object ob = execute(); Object ob = execute();
return ob; return ob;
} finally { } finally {
_query.setUnique(false); _query.setUnique(false);
} }
@ -366,27 +365,29 @@ public class QueryImpl
} else if (k.startsWith("FetchPlan.")) { } else if (k.startsWith("FetchPlan.")) {
k = k.substring("FetchPlan.".length()); k = k.substring("FetchPlan.".length());
Filters.hintToSetter(getFetchPlan(), k, value); Filters.hintToSetter(getFetchPlan(), k, value);
} else if (k.startsWith("hint.")){ } else if (k.startsWith("hint.")) {
if("hint.OptimizeResultCount".equals(k)){ if ("hint.OptimizeResultCount".equals(k)) {
if((!(value instanceof String)&&!(value instanceof Integer)) if ((!(value instanceof String)&&! (value instanceof Integer))
|| (value instanceof String &&(Integer.parseInt || (value instanceof String &&(Integer.parseInt
((String)value)< 0))||((value instanceof Integer) ((String)value)< 0))||
&& (((Integer)value).intValue()<0)) ) ((value instanceof Integer)
throw new ArgumentException(_loc.get && (((Integer)value).intValue()<0)))
("bad-hint-value", key), throw new ArgumentException(_loc.get
null, null, false); ("bad-hint-value", key),
if(value instanceof String) null, null, false);
if (value instanceof String)
value = new Integer((String)value); value = new Integer((String)value);
} }
_query.getFetchConfiguration().setHint(key, value); _query.getFetchConfiguration().setHint(key, value);
} }
else else
throw new ArgumentException(_loc.get("bad-query-hint", key), throw new ArgumentException(_loc.get("bad-query-hint", key),
null, null, false); null, null, false);
return this; return this;
}catch(NumberFormatException e1){ }
throw new ArgumentException(_loc.get("bad-hint-value", key), catch (NumberFormatException e1) {
null, null, false); throw new ArgumentException(_loc.get("bad-hint-value", key),
null, null, false);
} }
catch (Exception e) { catch (Exception e) {
throw PersistenceExceptions.toPersistenceException(e); throw PersistenceExceptions.toPersistenceException(e);