From fee930886b9c0f3e177d6257d60b158d5f2d9654 Mon Sep 17 00:00:00 2001 From: "David J. Wisneski" Date: Tue, 27 Mar 2007 20:47:21 +0000 Subject: [PATCH] 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 --- .../openjpa/jdbc/kernel/JDBCStoreManager.java | 16 ++-- .../openjpa/jdbc/kernel/JDBCStoreQuery.java | 28 +++--- .../openjpa/jdbc/sql/DB2Dictionary.java | 93 +++++++++---------- .../apache/openjpa/jdbc/sql/LogicalUnion.java | 40 ++++---- .../openjpa/jdbc/sql/SelectExecutor.java | 6 +- .../apache/openjpa/jdbc/sql/SelectImpl.java | 39 ++++---- .../openjpa/kernel/AbstractStoreQuery.java | 3 +- .../org/apache/openjpa/kernel/QueryHints.java | 25 +++++ .../org/apache/openjpa/kernel/QueryImpl.java | 2 +- .../openjpa/persistence/OpenJPAQuery.java | 3 +- .../apache/openjpa/persistence/QueryImpl.java | 41 ++++---- 11 files changed, 155 insertions(+), 141 deletions(-) create mode 100644 openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java index 94c86c74d..0b9809bbd 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java @@ -363,18 +363,18 @@ public class JDBCStoreManager * null if there is no data in the current fetch groups to select. */ private Result getInitializeStateResult(OpenJPAStateManager sm, - ClassMapping mapping, JDBCFetchConfiguration fetch, int subs) - throws SQLException { + ClassMapping mapping, JDBCFetchConfiguration fetch, int subs) + throws SQLException { Select sel = _sql.newSelect(); if (!select(sel, mapping, subs, sm, null, fetch, - JDBCFetchConfiguration.EAGER_JOIN, true, false)) + JDBCFetchConfiguration.EAGER_JOIN, true, false)) return null; sel.wherePrimaryKey(sm.getObjectId(), mapping, this); - //Set the expectedResultCount for the select as 1 as a single - //object is being loaded. force = true is an indicator that it is - //internally generated value - sel.setExpectedResultCount(1,true); - return sel.execute(this, fetch); + // Set the expectedResultCount for the select as 1 as a single + // object is being loaded. force = true is an indicator that it is + // internally generated value + sel.setExpectedResultCount(1,true); + return sel.execute(this, fetch); } /** diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java index e70e9ef5a..89798611d 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java @@ -48,6 +48,7 @@ import org.apache.openjpa.jdbc.sql.Select; import org.apache.openjpa.jdbc.sql.Union; import org.apache.openjpa.kernel.ExpressionStoreQuery; 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.ExpressionParser; import org.apache.openjpa.kernel.exps.FilterListener; @@ -319,23 +320,22 @@ public class JDBCStoreQuery subclassMode); if (verts.length == 1 && subclasses) subclassBits.set(sels.size()); - // create criteria select and clone for each vert mapping sel = ((JDBCExpressionFactory) facts[i]).getSelectConstructor(). evaluate(ctx, null, null, exps[i], states[i]); - //it means it is coming from getSingleResult so set the - //expectedResultCount to 1.force = true indicates that this is - //internally generated value - if(this.ctx.isUnique()) - sel.setExpectedResultCount(1,true); - //it means this is coming from getResultList so set the - //expectedResultCount based on any optimize hint if provided - else{ - if((optHint = ctx.fetch.getHint - (this.optimizeHint))!= null) - sel.setExpectedResultCount - (((Integer)optHint).intValue(),false); - } + // It means it is coming from getSingleResult so set the + // expectedResultCount to 1.force = true indicates that this is + // internally generated value + if (this.ctx.isUnique()) + sel.setExpectedResultCount(1,true); + // It means this is coming from getResultList so set the + // expectedResultCount based on any optimize hint if provided + else { + if ((optHint = ctx.fetch.getHint + (QueryHints.HINT_RESULT_COUNT))!= null) + sel.setExpectedResultCount + (((Integer)optHint).intValue(),false); + } for (int j = 0; j < verts.length; j++) { selMappings.add(verts[j]); if (j == verts.length - 1) { diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java index dffd7cf05..720bdf344 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java @@ -28,8 +28,7 @@ import org.apache.openjpa.jdbc.schema.Sequence; */ public class DB2Dictionary extends AbstractDB2Dictionary { - - //variables to support optimize clause + // variables to support optimize clause public String optimizeClause = "optimize for"; public String rowClause = "row"; public DB2Dictionary() { @@ -195,67 +194,63 @@ public class DB2Dictionary 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, - int expectedResultCount) { + int expectedResultCount) { Integer rows = null; StringBuffer optimizeString = new StringBuffer(); - - if(expectedResultCount != 0) - optimizeString.append(" ").append(optimizeClause).append(" ") - .append(expectedResultCount).append(" ") - .append(rowClause).append(" "); - return optimizeString.toString(); + if (expectedResultCount != 0) + optimizeString.append(" ").append(optimizeClause).append(" ") + .append(expectedResultCount).append(" ") + .append(rowClause).append(" "); + return optimizeString.toString(); } - //override the DBDictionary toSelect to call getOptimizeClause and append - //to the select string + /** Override the DBDictionary toSelect to call getOptimizeClause and append + * to the select string + */ public SQLBuffer toSelect(SQLBuffer selects, JDBCFetchConfiguration fetch, SQLBuffer from, SQLBuffer where, SQLBuffer group, SQLBuffer having, SQLBuffer order, boolean distinct, boolean forUpdate, long start, long end, int expectedResultCount) { - - String optimizeString = null; - SQLBuffer selString = toOperation(getSelectOperation(fetch), - 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 + 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; + } + + /** Override the DBDictionary toSelect to pass expectedResultcount to the + * other toSelect method + */ public SQLBuffer toSelect(Select sel, boolean forUpdate, JDBCFetchConfiguration fetch) { - sel.addJoinClassConditions(); - - boolean update = forUpdate && sel.getFromSelect() == null; - SQLBuffer select = getSelects(sel, false, update); - SQLBuffer ordering = null; - if (!sel.isAggregate() || sel.getGrouping() != null) - ordering = sel.getOrdering(); - SQLBuffer from; - if (sel.getFromSelect() != null) - from = getFromSelect(sel, forUpdate); - else - from = getFrom(sel, update); - SQLBuffer where = getWhere(sel, update); - return toSelect(select, fetch, from, where, sel.getGrouping(), + sel.addJoinClassConditions(); + boolean update = forUpdate && sel.getFromSelect() == null; + SQLBuffer select = getSelects(sel, false, update); + SQLBuffer ordering = null; + if (!sel.isAggregate() || sel.getGrouping() != null) + ordering = sel.getOrdering(); + SQLBuffer from; + if (sel.getFromSelect() != null) + from = getFromSelect(sel, forUpdate); + else + from = getFrom(sel, update); + SQLBuffer where = getWhere(sel, update); + return toSelect(select, fetch, from, where, sel.getGrouping(), sel.getHaving(), ordering, sel.isDistinct(), forUpdate, sel.getStartIndex(), sel.getEndIndex(),sel.getExpectedResultCount()); - } - + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/LogicalUnion.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/LogicalUnion.java index 359e42a14..7dad3af69 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/LogicalUnion.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/LogicalUnion.java @@ -47,11 +47,7 @@ public class LogicalUnion private static final Localizer _loc = Localizer.forPackage (LogicalUnion.class); - //expected number of results for this select to be used in - // optimize for clause protected int expectedResultCount = 0; - //indicate if this is internally generated result count - //or not protected boolean force = false; protected final UnionSelect[] sels; protected final DBDictionary dict; @@ -215,9 +211,10 @@ public class LogicalUnion if (this.getExpectedResultCount()== 1) { AbstractResult res; for (int i = 0; i < sels.length; i++) { - //for each select set the expected result count to 1 - //and force true indicating that this internally generated value - sels[i].sel.setExpectedResultCount(1,true); + // For each select set the expected result count to 1 + // and force true indicating that this internally generated + // value + sels[i].sel.setExpectedResultCount(1,true); res = (AbstractResult) sels[i].execute(store, fetch, lockLevel); res.setBaseMapping(mappings[i]); @@ -305,10 +302,7 @@ public class LogicalUnion protected final int pos; protected int orders = 0; protected List orderIdxs = null; - // expected number of results for this select to be used in - // optimize for clause protected int expectedResultCount = 0; - //force indicates it is internally generated result count or not protected boolean force = false; public UnionSelect(SelectImpl sel, int pos) { @@ -844,17 +838,19 @@ public class LogicalUnion return sel.toString(); } - public int getExpectedResultCount() { - return expectedResultCount; - } + public int getExpectedResultCount() { + return expectedResultCount; + } - public void setExpectedResultCount(int expectedResultCount, boolean force) { - this.expectedResultCount = expectedResultCount; + public void setExpectedResultCount(int expectedResultCount, + boolean force) { + this.expectedResultCount = expectedResultCount; this.force = force; - } - public boolean isExpRsltCntForced() { - return force; - } + } + + public boolean isExpRsltCntForced() { + return force; + } } /** @@ -939,11 +935,13 @@ public class LogicalUnion public int getExpectedResultCount() { return expectedResultCount; } - - public void setExpectedResultCount(int expectedResultCount,boolean force) { + + public void setExpectedResultCount(int expectedResultCount, + boolean force) { this.expectedResultCount = expectedResultCount; this.force = force; } + public boolean isExpRsltCntForced() { return force; } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectExecutor.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectExecutor.java index 3a7994c8e..1805cc2b2 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectExecutor.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectExecutor.java @@ -130,12 +130,10 @@ public interface SelectExecutor { * force indicates whether the count is internally generated * or given by the user as optimize hint */ - public void setExpectedResultCount(int expectedResultCount,boolean force) ; - + /** * Indicates whether the expectedResultCount is internally generated */ - - public boolean isExpRsltCntForced(); + public boolean isExpRsltCntForced(); } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java index df73b6365..3654a3593 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java @@ -154,11 +154,7 @@ public class SelectImpl private SelectImpl _from = null; private SelectImpl _outer = null; - //expected number of results for this select to be used in - // optimize for clause private int expectedResultCount = 0; - //true if the expectedResultCount is internally set false if - //it is set by the user private boolean force = false; /** @@ -312,19 +308,19 @@ public class SelectImpl throws SQLException { boolean forUpdate = false; - //expectedResultCount = 1 and force means that it is internally generated value - //for getSingleResult,single valued relationship.We need to check if - //there are any eager joins in the select if there are then the - //optimize for 1 row clause is not generated else we do. if - //!force then it is set by the user through hint and we - //do not check the eager joins - if(this.expectedResultCount == 1 && force ){ - if(this.hasEagerJoin(true)) + // ExpectedResultCount = 1 and force means that it is internally + // generated value for getSingleResult,single valued relationship. + // We need to check if there are any eager joins in the select if + // there are then the optimize for 1 row clause is not generated + // else we do. if !force then it is set by the user through hint + // and we do not check the eager joins + if (this.expectedResultCount == 1 && force ) { + if (this.hasEagerJoin(true)) this.setExpectedResultCount(0,false); else - this.setExpectedResultCount(1,false); - } - + this.setExpectedResultCount(1,false); + } + if (!isAggregate() && _grouping == null) { JDBCLockManager lm = store.getLockManager(); if (lm != null) @@ -2821,14 +2817,15 @@ public class SelectImpl } } - public int getExpectedResultCount() { - return expectedResultCount; - } + public int getExpectedResultCount() { + return expectedResultCount; + } - public void setExpectedResultCount(int expectedResultCount, boolean force) { - this.expectedResultCount = expectedResultCount; + public void setExpectedResultCount(int expectedResultCount, + boolean force) { + this.expectedResultCount = expectedResultCount; this.force = force; - } + } public boolean isExpRsltCntForced() { return force; diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractStoreQuery.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractStoreQuery.java index 81af05675..e6a983fea 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractStoreQuery.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/AbstractStoreQuery.java @@ -33,8 +33,7 @@ public abstract class AbstractStoreQuery implements StoreQuery { protected QueryContext ctx = null; - public static final String optimizeHint ="openjpa.hint.OptimizeResultCount"; - + public QueryContext getContext() { return ctx; } diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java new file mode 100644 index 000000000..fd9623a7c --- /dev/null +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryHints.java @@ -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"; +} 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 aabb34b9b..9e4aa3279 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 @@ -1290,7 +1290,7 @@ public class QueryImpl return Arrays.asList(new Object[]{ single }); } - if(single == null) + if (single == null) throw new InvalidStateException(_loc.get("is-null", _class, _query)); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAQuery.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAQuery.java index d30c5f854..06feacb24 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAQuery.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAQuery.java @@ -24,6 +24,7 @@ import javax.persistence.Query; import javax.persistence.TemporalType; import org.apache.openjpa.kernel.QueryFlushModes; +import org.apache.openjpa.kernel.QueryHints; import org.apache.openjpa.kernel.QueryOperations; import org.apache.openjpa.kernel.exps.AggregateListener; import org.apache.openjpa.kernel.exps.FilterListener; @@ -36,7 +37,7 @@ import org.apache.openjpa.kernel.exps.FilterListener; * @published */ public interface OpenJPAQuery - extends Query, QueryOperations, QueryFlushModes { + extends Query, QueryOperations, QueryFlushModes, QueryHints { /** * The owning entity manage. diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java index 883c5be37..8f5a37d8d 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java @@ -279,13 +279,12 @@ public class QueryImpl */ public Object getSingleResult() { _em.assertNotCloseInvoked(); - //Indicate that this query returns single result.Later copied into - //select.expectedResultCount + // Indicate that this query returns single result.Later copied into + // select.expectedResultCount _query.setUnique(true); - try{ - Object ob = execute(); - return ob; - + try { + Object ob = execute(); + return ob; } finally { _query.setUnique(false); } @@ -366,27 +365,29 @@ public class QueryImpl } else if (k.startsWith("FetchPlan.")) { k = k.substring("FetchPlan.".length()); Filters.hintToSetter(getFetchPlan(), k, value); - } else if (k.startsWith("hint.")){ - if("hint.OptimizeResultCount".equals(k)){ - if((!(value instanceof String)&&!(value instanceof Integer)) - || (value instanceof String &&(Integer.parseInt - ((String)value)< 0))||((value instanceof Integer) - && (((Integer)value).intValue()<0)) ) - throw new ArgumentException(_loc.get - ("bad-hint-value", key), - null, null, false); - if(value instanceof String) + } else if (k.startsWith("hint.")) { + if ("hint.OptimizeResultCount".equals(k)) { + if ((!(value instanceof String)&&! (value instanceof Integer)) + || (value instanceof String &&(Integer.parseInt + ((String)value)< 0))|| + ((value instanceof Integer) + && (((Integer)value).intValue()<0))) + throw new ArgumentException(_loc.get + ("bad-hint-value", key), + null, null, false); + if (value instanceof String) value = new Integer((String)value); - } + } _query.getFetchConfiguration().setHint(key, value); } else throw new ArgumentException(_loc.get("bad-query-hint", key), null, null, false); return this; - }catch(NumberFormatException e1){ - throw new ArgumentException(_loc.get("bad-hint-value", key), - null, null, false); + } + catch (NumberFormatException e1) { + throw new ArgumentException(_loc.get("bad-hint-value", key), + null, null, false); } catch (Exception e) { throw PersistenceExceptions.toPersistenceException(e);