diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java index 57f1d76bd..119f47e13 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractResult.java @@ -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,9 +129,11 @@ public abstract class AbstractResult /** * Close all results in eager map. */ - protected void closeEagerMap(Map eager) { + protected void closeEagerMap(Map eager) { if (eager != null) { - for (Object res : eager.values()) { + Object res; + for (Iterator itr = eager.values().iterator(); itr.hasNext();) { + res = itr.next(); if (res != this && res instanceof Closeable) try { ((Closeable) res).close(); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java index cda8d3248..e603fcba3 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/JoinSet.java @@ -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(itr.next()) || added; + for (Iterator itr = js.iterator(); itr.hasNext();) + added = add((Join) itr.next()) || added; return added; } @@ -198,7 +198,7 @@ class JoinSet { while (_graph.size() < size) _graph.add(null); - Node node = _graph.get(join.getIndex1()); + Node 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 = _graph.get(join.getIndex2()); + node = (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 Join next() { + public Object 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(itr.next()) || remd; + for (Iterator itr = js.iterator(); itr.hasNext();) + remd = remove((Join) itr.next()) || remd; return remd; } public boolean retainAll(JoinSet js) { boolean remd = false; Join join; - for (Iterator itr = iterator(); itr.hasNext();) { - join = itr.next(); + for (Iterator itr = iterator(); itr.hasNext();) { + join = (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(itr.next())) + for (Iterator itr = js.iterator(); itr.hasNext();) + if (!contains((Join) 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(", "); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java index ff0f7ffa9..869be7bcf 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/Select.java @@ -102,7 +102,7 @@ public interface Select /** * Return this select's subselects, or empty collection if none. */ - public List _subsels = null; + private List _subsels = null; private SelectImpl _parent = null; private String _subPath = null; private boolean _hasSub = false; @@ -168,8 +170,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; @@ -178,19 +180,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; } @@ -339,8 +341,9 @@ public class SelectImpl public boolean hasMultipleSelects() { if (_eager == null) return false; - - for (Map.Entry entry : _eager.entrySet()) { + Map.Entry entry; + for (Iterator itr = _eager.entrySet().iterator(); itr.hasNext();) { + entry = (Map.Entry) itr.next(); if (entry.getValue() != this) return true; } @@ -440,17 +443,24 @@ public class SelectImpl return; // execute eager selects + Map.Entry entry; Result eres; - Map eager; - for (Map.Entry entry : sel._eager.entrySet()) { + Map eager; + for (Iterator itr = sel._eager.entrySet().iterator(); itr.hasNext();) { + entry = (Map.Entry) itr.next(); + // 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 - eres = (entry.getValue() == sel) ? res : entry.getValue().execute(store, fetch); + if (entry.getValue() == sel) + eres = res; + else + eres = ((SelectExecutor) 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); @@ -537,10 +547,8 @@ public class SelectImpl return 0; } - public List(2); + _parent._subsels = new ArrayList(2); _parent._subsels.add(this); if (_parent._joinSyntax == JoinSyntaxes.SYNTAX_SQL92) _joinSyntax = JoinSyntaxes.SYNTAX_TRADITIONAL; @@ -588,7 +596,7 @@ public class SelectImpl return _hasSub; } - public Map getAliases() { + public Map getAliases() { return _aliases; } @@ -596,7 +604,7 @@ public class SelectImpl _aliases.remove(key); } - public Map getTables() { + public Map getTables() { return _tables; } @@ -633,27 +641,20 @@ public class SelectImpl return getTableIndex(table, pj, false) != -1; } - public Collection getTableAliases() { - if (_tables == null) - return Collections.emptySet(); - return _tables.values(); + public Collection getTableAliases() { + return (_tables == null) ? Collections.EMPTY_SET : _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() { - List result = new ArrayList(); - List ids = _selects.getAliases(true, _outer != null); - for (Object id : ids) { - result.add((String)id); - } - return result; + public List getIdentifierAliases() { + return _selects.getAliases(true, _outer != null); } public SQLBuffer getOrdering() { @@ -678,8 +679,8 @@ public class SelectImpl // join set iterator allows concurrent modification Join j; - for (Iterator itr = _joins.joins().iterator(); itr.hasNext();) { - j = itr.next(); + for (Iterator itr = _joins.joins().iterator(); itr.hasNext();) { + j = (Join) itr.next(); if (j.getRelationTarget() != null) { j.getRelationTarget().getDiscriminator().addClassConditions (this, j.getSubclasses() == SUBS_JOINABLE, @@ -693,9 +694,9 @@ public class SelectImpl return _joins; } - public Iterator getJoinIterator() { + public Iterator getJoinIterator() { if (_joins == null || _joins.isEmpty()) - return new EmptyJoinIterator(); + return EmptyIterator.INSTANCE; return _joins.joins().joinIterator(); } @@ -741,9 +742,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 = itr.next(); + Join join = (Join) itr.next(); if (join != null) { if (join.getTable1() == table) tableAlias = join.getAlias1(); @@ -966,7 +967,7 @@ public class SelectImpl boolean hasJoins = pj != null && pj.isDirty(); if (hasJoins) { if (_preJoins == null) - _preJoins = new Stack(); + _preJoins = new Stack(); _preJoins.push(pj); } @@ -1106,7 +1107,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) { @@ -1230,7 +1231,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); } @@ -1283,10 +1284,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; @@ -1354,10 +1355,13 @@ public class SelectImpl wherePrimaryKey(oid, mapping, joins, store); return; } - where(oid, mapping, - fk.getPrimaryKeyColumns(), fk.getColumns(), - fk.getConstants(), fk.getConstantColumns(), - getJoins(joins, true), store); + + 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); } /** @@ -1374,39 +1378,16 @@ public class SelectImpl return; } - 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 + // only bother to pack pk values into array if app 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(); @@ -1416,19 +1397,32 @@ public class SelectImpl val = pks[mapping.getField(join.getFieldIndex()).getPrimaryKeyIndex()]; val = join.getJoinValue(val, toCols[i], store); } - vals[i] = val; + + 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]); } - 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]); - } + + 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); } /** @@ -1544,7 +1538,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(", "); @@ -1595,7 +1589,7 @@ public class SelectImpl boolean pre = (pj == null || !pj.isDirty()) && _preJoins != null && !_preJoins.isEmpty(); if (pre) - pj = _preJoins.peek(); + pj = (PathJoins) _preJoins.peek(); if (pj == null || !pj.isDirty()) pj = _joins; @@ -1634,9 +1628,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) @@ -1646,7 +1640,7 @@ public class SelectImpl sel._from._outer = sel; } if (_subsels != null) { - sel._subsels = new ArrayList