Return-Path: X-Original-To: apmail-openjpa-commits-archive@www.apache.org Delivered-To: apmail-openjpa-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 555B69237 for ; Wed, 28 Dec 2011 19:06:22 +0000 (UTC) Received: (qmail 7316 invoked by uid 500); 28 Dec 2011 19:06:22 -0000 Delivered-To: apmail-openjpa-commits-archive@openjpa.apache.org Received: (qmail 7271 invoked by uid 500); 28 Dec 2011 19:06:21 -0000 Mailing-List: contact commits-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openjpa.apache.org Delivered-To: mailing list commits@openjpa.apache.org Received: (qmail 7264 invoked by uid 99); 28 Dec 2011 19:06:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Dec 2011 19:06:21 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Dec 2011 19:06:15 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 91C8B2388900 for ; Wed, 28 Dec 2011 19:05:53 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1225299 - in /openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql: AbstractResult.java JoinSet.java Select.java SelectImpl.java Date: Wed, 28 Dec 2011 19:05:53 -0000 To: commits@openjpa.apache.org From: ppoddar@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111228190553.91C8B2388900@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ppoddar Date: Wed Dec 28 19:05:52 2011 New Revision: 1225299 URL: http://svn.apache.org/viewvc?rev=1225299&view=rev Log: OPENJPA-2099: make use of generic types Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java?rev=1225299&r1=1225298&r2=1225299&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java Wed Dec 28 19:05:52 2011 @@ -74,7 +74,7 @@ public abstract class AbstractResult private static final Joins JOINS = new NoOpJoins(); - private Map _eager = null; + private Map _eager = null; private ClassMapping _base = null; private int _index = 0; private boolean _gotEager = false; @@ -86,14 +86,14 @@ public abstract class AbstractResult private Object _mappedByValue = null; public Object getEager(FieldMapping key) { - Map map = getEagerMap(true); + Map map = getEagerMap(true); return (map == null) ? null : map.get(key); } public void putEager(FieldMapping key, Object res) { - Map map = getEagerMap(false); + Map map = getEagerMap(false); if (map == null) { - map = new HashMap(); + map = new HashMap(); setEagerMap(map); } map.put(key, res); @@ -104,7 +104,7 @@ public abstract class AbstractResult * * @param client whether the client is accessing eager information */ - protected Map getEagerMap(boolean client) { + protected Map getEagerMap(boolean client) { if (client) _gotEager = true; return _eager; @@ -113,7 +113,7 @@ public abstract class AbstractResult /** * Raw eager information. */ - protected void setEagerMap(Map eager) { + protected void setEagerMap(Map eager) { _eager = eager; } @@ -129,11 +129,9 @@ public abstract class AbstractResult /** * Close all results in eager map. */ - protected void closeEagerMap(Map eager) { + protected void closeEagerMap(Map eager) { if (eager != null) { - Object res; - for (Iterator itr = eager.values().iterator(); itr.hasNext();) { - res = itr.next(); + for (Object res : eager.values()) { if (res != this && res instanceof Closeable) try { ((Closeable) res).close(); Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java?rev=1225299&r1=1225298&r2=1225299&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java Wed Dec 28 19:05:52 2011 @@ -39,9 +39,9 @@ class JoinSet { // efficient representation with O(1) lookup, add, remove operations for // typical sets of joins, and it means we'd have to create a graph anyway // when joinIterator() is called - private final List _graph = new ArrayList(); + private final List _graph = new ArrayList(); private int _size = 0; - private List _sorted = null; + private List _sorted = null; public JoinSet() { } @@ -51,7 +51,7 @@ class JoinSet { if (copy._graph.get(i) == null) _graph.add(null); else - _graph.add(((Node) copy._graph.get(i)).clone()); + _graph.add((Node) copy._graph.get(i).clone()); } _size = copy._size; _sorted = copy._sorted; @@ -95,14 +95,14 @@ class JoinSet { /** * Iterator over joins that prepares them for SQL translation. */ - public Iterator joinIterator() { + public Iterator joinIterator() { if (_size < 2) return iterator(); if (_sorted != null) return _sorted.iterator(); - List sorted = new ArrayList(_size); - LinkedList queue = new LinkedList(); + List sorted = new ArrayList(_size); + LinkedList queue = new LinkedList(); BitSet seen = new BitSet(_graph.size() * _graph.size() + _graph.size()); @@ -183,8 +183,8 @@ class JoinSet { return false; boolean added = false; - for (Iterator itr = js.iterator(); itr.hasNext();) - added = add((Join) itr.next()) || added; + for (Iterator itr = js.iterator(); itr.hasNext();) + added = add(itr.next()) || added; return added; } @@ -198,7 +198,7 @@ class JoinSet { while (_graph.size() < size) _graph.add(null); - Node node = (Node) _graph.get(join.getIndex1()); + Node node = _graph.get(join.getIndex1()); if (node == null) _graph.set(join.getIndex1(), new Node(join, true)); else { @@ -207,7 +207,7 @@ class JoinSet { node.next = new Node(join, true); } - node = (Node) _graph.get(join.getIndex2()); + node = _graph.get(join.getIndex2()); if (node == null) _graph.set(join.getIndex2(), new Node(join, false)); else { @@ -218,8 +218,8 @@ class JoinSet { _size++; } - public Iterator iterator() { - return new Iterator() { + public Iterator iterator() { + return new Iterator() { private Node _next = null; private int _idx = -1; @@ -237,7 +237,7 @@ class JoinSet { return false; } - public Object next() { + public Join next() { if (!hasNext()) throw new NoSuchElementException(); Join j = _next.join; @@ -289,16 +289,16 @@ class JoinSet { public boolean removeAll(JoinSet js) { boolean remd = false; - for (Iterator itr = js.iterator(); itr.hasNext();) - remd = remove((Join) itr.next()) || remd; + for (Iterator itr = js.iterator(); itr.hasNext();) + remd = remove(itr.next()) || remd; return remd; } public boolean retainAll(JoinSet js) { boolean remd = false; Join join; - for (Iterator itr = iterator(); itr.hasNext();) { - join = (Join) itr.next(); + for (Iterator itr = iterator(); itr.hasNext();) { + join = itr.next(); if (!js.contains(join)) remd = remove(join); } @@ -318,8 +318,8 @@ class JoinSet { public boolean containsAll(JoinSet js) { if (js._size > _size || js._graph.size() > _graph.size()) return false; - for (Iterator itr = js.iterator(); itr.hasNext();) - if (!contains((Join) itr.next())) + for (Iterator itr = js.iterator(); itr.hasNext();) + if (!contains(itr.next())) return false; return true; } @@ -347,7 +347,7 @@ class JoinSet { public String toString() { StringBuilder buf = new StringBuilder(); buf.append("["); - for (Iterator itr = iterator(); itr.hasNext();) { + for (Iterator itr = iterator(); itr.hasNext();) { buf.append("<").append(itr.next()).append(">"); if (itr.hasNext()) buf.append(", "); Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java?rev=1225299&r1=1225298&r2=1225299&view=diff ============================================================================== --- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java (original) +++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java Wed Dec 28 19:05:52 2011 @@ -102,7 +102,7 @@ public interface Select /** * Return this select's subselects, or empty collection if none. */ - public List getSubselects(); + public List _subsels = null; private SelectImpl _parent = null; private String _subPath = null; private boolean _hasSub = false; @@ -170,8 +168,8 @@ public class SelectImpl // A path navigation is begin with this schema alias private String _schemaAlias = null; private ClassMapping _tpcMeta = null; - private List _joinedTables = null; - private List _exJoinedTables = null; + private List _joinedTables = null; + private List _exJoinedTables = null; public ClassMapping getTablePerClassMeta() { return _tpcMeta; @@ -180,19 +178,19 @@ public class SelectImpl _tpcMeta = meta; } - public void setJoinedTableClassMeta(List meta) { + public void setJoinedTableClassMeta(List meta) { _joinedTables = meta; } - public List getJoinedTableClassMeta() { + public List getJoinedTableClassMeta() { return _joinedTables; } - public void setExcludedJoinedTableClassMeta(List meta) { + public void setExcludedJoinedTableClassMeta(List meta) { _exJoinedTables = meta; } - public List getExcludedJoinedTableClassMeta() { + public List getExcludedJoinedTableClassMeta() { return _exJoinedTables; } @@ -341,9 +339,8 @@ public class SelectImpl public boolean hasMultipleSelects() { if (_eager == null) return false; - Map.Entry entry; - for (Iterator itr = _eager.entrySet().iterator(); itr.hasNext();) { - entry = (Map.Entry) itr.next(); + + for (Map.Entry entry : _eager.entrySet()) { if (entry.getValue() != this) return true; } @@ -443,24 +440,17 @@ public class SelectImpl return; // execute eager selects - Map.Entry entry; Result eres; - Map eager; - for (Iterator itr = sel._eager.entrySet().iterator(); itr.hasNext();) { - entry = (Map.Entry) itr.next(); - + Map eager; + for (Map.Entry entry : sel._eager.entrySet()) { // simulated batched selects for inner/outer joins; for separate // selects, don't pass on lock level, because they're probably // for relations and therefore should use default level - if (entry.getValue() == sel) - eres = res; - else - eres = ((SelectExecutor) entry.getValue()).execute(store, - fetch); + eres = (entry.getValue() == sel) ? res : entry.getValue().execute(store, fetch); eager = res.getEagerMap(false); if (eager == null) { - eager = new HashMap(); + eager = new HashMap(); res.setEagerMap(eager); } eager.put(entry.getKey(), eres); @@ -547,8 +537,10 @@ public class SelectImpl return 0; } - public List getSubselects() { - return (_subsels == null) ? Collections.EMPTY_LIST : _subsels; + public List(2); _parent._subsels.add(this); if (_parent._joinSyntax == JoinSyntaxes.SYNTAX_SQL92) _joinSyntax = JoinSyntaxes.SYNTAX_TRADITIONAL; @@ -596,7 +588,7 @@ public class SelectImpl return _hasSub; } - public Map getAliases() { + public Map getAliases() { return _aliases; } @@ -604,7 +596,7 @@ public class SelectImpl _aliases.remove(key); } - public Map getTables() { + public Map getTables() { return _tables; } @@ -641,20 +633,27 @@ public class SelectImpl return getTableIndex(table, pj, false) != -1; } - public Collection getTableAliases() { - return (_tables == null) ? Collections.EMPTY_SET : _tables.values(); + public Collection getTableAliases() { + if (_tables == null) + return Collections.emptySet(); + return _tables.values(); } - public List getSelects() { + public List getSelects() { return Collections.unmodifiableList(_selects); } - public List getSelectAliases() { + public List getSelectAliases() { return _selects.getAliases(false, _outer != null); } - public List getIdentifierAliases() { - return _selects.getAliases(true, _outer != null); + public List getIdentifierAliases() { + List result = new ArrayList(); + List ids = _selects.getAliases(true, _outer != null); + for (Object id : ids) { + result.add((String)id); + } + return result; } public SQLBuffer getOrdering() { @@ -679,8 +678,8 @@ public class SelectImpl // join set iterator allows concurrent modification Join j; - for (Iterator itr = _joins.joins().iterator(); itr.hasNext();) { - j = (Join) itr.next(); + for (Iterator itr = _joins.joins().iterator(); itr.hasNext();) { + j = itr.next(); if (j.getRelationTarget() != null) { j.getRelationTarget().getDiscriminator().addClassConditions (this, j.getSubclasses() == SUBS_JOINABLE, @@ -694,9 +693,9 @@ public class SelectImpl return _joins; } - public Iterator getJoinIterator() { + public Iterator getJoinIterator() { if (_joins == null || _joins.isEmpty()) - return EmptyIterator.INSTANCE; + return new EmptyJoinIterator(); return _joins.joins().joinIterator(); } @@ -742,9 +741,9 @@ public class SelectImpl public String getColumnAlias(Column col, Object path) { Table table = col.getTable(); String tableAlias = null; - Iterator itr = getJoinIterator(); + Iterator itr = getJoinIterator(); while (itr.hasNext()) { - Join join = (Join) itr.next(); + Join join = itr.next(); if (join != null) { if (join.getTable1() == table) tableAlias = join.getAlias1(); @@ -967,7 +966,7 @@ public class SelectImpl boolean hasJoins = pj != null && pj.isDirty(); if (hasJoins) { if (_preJoins == null) - _preJoins = new Stack(); + _preJoins = new Stack(); _preJoins.push(pj); } @@ -1107,7 +1106,7 @@ public class SelectImpl id = getColumnAlias(col, pj); if ((_flags & RECORD_ORDERED) != 0) { if (_ordered == null) - _ordered = new ArrayList(5); + _ordered = new ArrayList(5); _ordered.add(id); } if (aliasOrder) { @@ -1231,7 +1230,7 @@ public class SelectImpl } if ((_flags & RECORD_ORDERED) != 0) { if (_ordered == null) - _ordered = new ArrayList(5); + _ordered = new ArrayList(5); _ordered.add(selAs == null ? sql : selAs); } @@ -1284,10 +1283,10 @@ public class SelectImpl * Return the indexes in the select list of all items we're ordering * by, or null if none. For use with unions. */ - List getOrderedIndexes() { + List getOrderedIndexes() { if (_ordered == null) return null; - List idxs = new ArrayList(_ordered.size()); + List idxs = new ArrayList(_ordered.size()); for (int i = 0; i < _ordered.size(); i++) idxs.add(_selects.indexOf(_ordered.get(i))); return idxs; @@ -1355,13 +1354,10 @@ public class SelectImpl wherePrimaryKey(oid, mapping, joins, store); return; } - - Column[] fromCols = fk.getColumns(); - Column[] toCols = fk.getPrimaryKeyColumns(); - Column[] constCols = fk.getConstantColumns(); - Object[] consts = fk.getConstants(); - where(oid, mapping, toCols, fromCols, consts, constCols, - getJoins(joins, true), store); + where(oid, mapping, + fk.getPrimaryKeyColumns(), fk.getColumns(), + fk.getConstants(), fk.getConstantColumns(), + getJoins(joins, true), store); } /** @@ -1378,16 +1374,39 @@ public class SelectImpl return; } - // only bother to pack pk values into array if app id + SQLBuffer buf = new SQLBuffer(_dict); + Object[] params = getBindParametrs(oid, mapping, toCols, fromCols, store); + bindToBuffer(buf, params, fromCols, pj); + + if (constCols != null && constCols.length > 0) { + bindToBuffer(buf, vals, constCols, pj); + } + + where(buf, pj); + } + + /** + * Gets the values to bind in a WHERE clause for joining a relation. + * + * @param oid the identifier to be joined + * @param mapping the class mapping of the identifier + * @param toCols the target column(s) + * @param fromCols the source column(s) + * @param store the store + * @return bind parameter values in the same index order of the target columns + */ + Object[] getBindParametrs(Object oid, ClassMapping mapping, Column[] toCols, Column[] fromCols, + JDBCStore store) { + // only pack primary key values into array if application id Object[] pks = null; boolean relationId = RelationStrategies.isRelationId(fromCols); if (!relationId && mapping.getIdentityType() == ClassMapping.ID_APPLICATION) pks = ApplicationIds.toPKValues(oid, mapping); - SQLBuffer buf = new SQLBuffer(_dict); Joinable join; Object val; int count = 0; + Object[] vals = new Object[toCols.length]; for (int i = 0; i < toCols.length; i++, count++) { if (pks == null) { val = (oid == null) ? null : relationId ? oid : ((Id) oid).getId(); @@ -1397,32 +1416,19 @@ public class SelectImpl val = pks[mapping.getField(join.getFieldIndex()).getPrimaryKeyIndex()]; val = join.getJoinValue(val, toCols[i], store); } - - if (count > 0) - buf.append(" AND "); - buf.append(getColumnAlias(fromCols[i], pj)); - if (val == null) - buf.append(" IS "); - else - buf.append(" = "); - buf.appendValue(val, fromCols[i]); + vals[i] = val; } - - if (constCols != null && constCols.length > 0) { - for (int i = 0; i < constCols.length; i++, count++) { - if (count > 0) - buf.append(" AND "); - buf.append(getColumnAlias(constCols[i], pj)); - - if (vals[i] == null) - buf.append(" IS "); - else - buf.append(" = "); - buf.appendValue(vals[i], constCols[i]); - } - } - - where(buf, pj); + return vals; + } + + void bindToBuffer(SQLBuffer buf, Object[] vals, Column[] fromCols, PathJoins pj) { + for (int i = 0; i < vals.length; i++) { + if (i > 0) + buf.append(" AND "); + buf.append(getColumnAlias(fromCols[i], pj)); + buf.append(vals[i] == null ? " IS " : " = "); + buf.appendValue(vals[i], fromCols[i]); + } } /** @@ -1538,7 +1544,7 @@ public class SelectImpl if (_grouped == null || !_grouped.contains(sql)) { if (_grouping == null) { _grouping = new SQLBuffer(_dict); - _grouped = new ArrayList(); + _grouped = new ArrayList(); } else _grouping.append(", "); @@ -1589,7 +1595,7 @@ public class SelectImpl boolean pre = (pj == null || !pj.isDirty()) && _preJoins != null && !_preJoins.isEmpty(); if (pre) - pj = (PathJoins) _preJoins.peek(); + pj = _preJoins.peek(); if (pj == null || !pj.isDirty()) pj = _joins; @@ -1628,9 +1634,9 @@ public class SelectImpl sel._joinSyntax = _joinSyntax; sel._schemaAlias = _schemaAlias; if (_aliases != null) - sel._aliases = new HashMap(_aliases); + sel._aliases = new HashMap(_aliases); if (_tables != null) - sel._tables = new TreeMap(_tables); + sel._tables = new TreeMap(_tables); if (_joins != null) sel._joins = _joins.clone(sel); if (_where != null) @@ -1640,7 +1646,7 @@ public class SelectImpl sel._from._outer = sel; } if (_subsels != null) { - sel._subsels = new ArrayList(_subsels.size()); + sel._subsels = new ArrayList