/** * Return a join that is this join in reverse. */ public Join reverse() { Join join = new Join(); join._type = _type; join._table1 = _table2; join._alias1 = _alias2; join._table2 = _table1; join._alias2 = _alias1; join._inverse = !_inverse; join._fk = _fk; join._target = _target; join._subs = _subs; join._joins = _joins; return join; }
/** * Return the node for the specified join and index. */ private Node getNode(Join join, int idx) { if (_graph.size() <= idx) return null; Node node = (Node) _graph.get(idx); for (; node != null; node = node.next) if (node.join.equals(join)) return node; return null; }
private void setCorrelated(Join j) { if (_sel._parent == null) return; if (_sel._aliases == null) { j.setIsNotMyJoin(); return; } Object aliases[] = _sel._aliases.values().toArray(); boolean found1 = false; boolean found2 = false; for (int i = 0; i < aliases.length; i++) { int alias = ((Integer)aliases[i]).intValue(); if (alias == j.getIndex1()) found1 = true; if (alias == j.getIndex2()) found2 = true; } if (found1 && found2) return; else if (!found1 && !found2) { j.setIsNotMyJoin(); return; } else { j.setCorrelated(); } }
public void addJoinClassConditions() { if (_joins == null || _joins.joins() == null) return; // join set iterator allows concurrent modification Join j; 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, j.getRelationJoins()); j.setRelation(null, 0, null); } } }
/** * Use the given join instance to create SQL joining its tables in * the SQL92 style. */ public SQLBuffer toSQL92Join(Join join, boolean forUpdate, boolean first) { SQLBuffer buf = new SQLBuffer(this); if (first) { buf.append(join.getTable1()).append(" "). append(join.getAlias1()); if (forUpdate && tableForUpdateClause != null) buf.append(" ").append(tableForUpdateClause); } buf.append(" "); if (join.getType() == Join.TYPE_OUTER) buf.append(outerJoinClause); else if (join.getType() == Join.TYPE_INNER) buf.append(innerJoinClause); else // cross buf.append(crossJoinClause); buf.append(" "); buf.append(join.getTable2()).append(" ").append(join.getAlias2()); if (forUpdate && tableForUpdateClause != null) buf.append(" ").append(tableForUpdateClause); if (join.getForeignKey() != null) buf.append(" ON ").append(toTraditionalJoin(join)); else if (requiresConditionForCrossJoin && join.getType() == Join.TYPE_CROSS) buf.append(" ON (1 = 1)"); return buf; }
private boolean correlatedJoinCondition(Join join, Select sel) { if (!join.isCorrelated()) return false; Iterator itr = sel.getJoinIterator(); boolean skip = false; //if table1 in join is in the main query, table2 is in //subquery, and table2 participates in other joins //in subquery, the join condition can only be placed in //the where clause in the subquery while (itr.hasNext()) { Join join1 = (Join) itr.next(); if (join == join1 && !join.isForeignKeyInversed()) { continue; } if (join.getIndex2() == join1.getIndex1() || join.getIndex2() == join1.getIndex2()) { skip = true; if (join.getForeignKey() != null){ SQLBuffer where = new SQLBuffer(this); where.append("(").append(toTraditionalJoin(join)).append(")"); sel.where(where.getSQL()); } break; } } return skip; }
ForeignKey fk = join.getForeignKey(); if (fk == null) return null; boolean inverse = join.isForeignKeyInversed(); Column[] from = (inverse) ? fk.getPrimaryKeyColumns() : fk.getColumns(); if (count > 0) buf.append(" AND "); buf.append(join.getAlias1()).append(".").append(from[i]); buf.append(" = "); buf.append(join.getAlias2()).append(".").append(to[i]); buf.appendValue(fk.getConstant(constCols[i]), constCols[i]); else buf.append(join.getAlias1()).append("."). append(constCols[i]); buf.append(" = "); buf.append(join.getAlias2()).append("."). append(constCols[i]); else buf.append(" AND "); if (inverse) buf.append(join.getAlias1()).append("."). append(constColsPK[i]); else
/** * Return the alias for the give column */ public String getColumnAlias(Column col, Object path) { Table table = col.getTable(); String tableAlias = null; Iterator itr = getJoinIterator(); while (itr.hasNext()) { Join join = (Join) itr.next(); if (join != null) { if (join.getTable1() == table) tableAlias = join.getAlias1(); else if (join.getTable2() == table) tableAlias = join.getAlias2(); if (tableAlias != null) return new StringBuilder(tableAlias).append("."). append(_dict.getNamingUtil().toDBName(col.getIdentifier())).toString(); } } throw new InternalException("Can not resolve alias for field: " + path.toString() + " mapped to column: " + col.getIdentifier().getName() + " table: "+table.getIdentifier().getName()); }
public boolean add(Join join) { if (join.getType() == Join.TYPE_OUTER) { // outer shouldn't override any other join type if (!contains(join)) { addNode(join); return true; } return false; } // replace any existing join with this one Node node = getNode(join, join.getIndex1()); if (node != null) { node.join = join; getNode(join, join.getIndex2()).join = join; _sorted = null; } else addNode(join); return true; }
for (Iterator itr = pj.joins().iterator(); itr.hasNext();) { join = (Join) itr.next(); if (join.getType() == Join.TYPE_INNER) { if (join.getForeignKey() != null && !_dict.canOuterJoin(_joinSyntax, join.getForeignKey())) { Log log = _conf.getLog(JDBCConfiguration.LOG_JDBC); if (log.isWarnEnabled()) log.warn(_loc.get("cant-outer-fk", join.getForeignKey())); } else join.setType(Join.TYPE_OUTER);
: fk.getPrimaryKeyTable(); int alias2 = _sel.getTableIndex(table2, this, true); Join j = new Join(table1, alias1, table2, alias2, fk, inverse); j.setType((outer) ? Join.TYPE_OUTER : Join.TYPE_INNER); if (_joins.add(j) && (subs == Select.SUBS_JOINABLE || subs == Select.SUBS_NONE)) j.setRelation(target, subs, clone(_sel));
Join join = (Join) itr.next(); if (join.isCorrelated() && join.getForeignKey() != null) { SQLBuffer where = new SQLBuffer(this); where.append("(").append(toTraditionalJoin(join)).append(")"); continue; if (join.isCorrelated()) toCorrelatedJoin(sel, join, forUpdate, first); else first)); first = false; if (itr.hasNext() && join.isCorrelated()) { if (fromSQL.getSQL().length() > 0) fromSQL.append(", ");
/** * Return the recorded join matching the given join's aliases. */ public Join getRecordedJoin(Join join) { if (join == null) return null; Node node = getNode(join, join.getIndex1()); return (node == null) ? null : node.join; }
private SQLBuffer toCorrelatedJoin(Select sel, Join join, boolean forUpdate, boolean first) { if (join.getForeignKey() != null){ SQLBuffer where = new SQLBuffer(this); where.append("(").append(toTraditionalJoin(join)).append(")"); sel.where(where.getSQL()); } return null; }
buf.append(join.getTable1()).append(" "). append(join.getAlias1()); if (forUpdate && tableForUpdateClause != null) buf.append(" ").append(tableForUpdateClause); if (join.getType() == Join.TYPE_OUTER) buf.append(outerJoinClause); else if (join.getType() == Join.TYPE_INNER) buf.append(innerJoinClause); else // cross buf.append(" "); buf.append(join.getTable2()).append(" ").append(join.getAlias2()); if (forUpdate && tableForUpdateClause != null) buf.append(" ").append(tableForUpdateClause); if (join.getForeignKey() != null) buf.append(" ON ").append(toTraditionalJoin(join)); else if (requiresConditionForCrossJoin && join.getType() == Join.TYPE_CROSS) buf.append(" ON (1 = 1)");
private boolean correlatedJoinCondition(Join join, Select sel) { if (!join.isCorrelated()) return false; Iterator itr = sel.getJoinIterator(); boolean skip = false; //if table1 in join is in the main query, table2 is in //subquery, and table2 participates in other joins //in subquery, the join condition can only be placed in //the where clause in the subquery while (itr.hasNext()) { Join join1 = (Join) itr.next(); if (join == join1 && !join.isForeignKeyInversed()) { continue; } if (join.getIndex2() == join1.getIndex1() || join.getIndex2() == join1.getIndex2()) { skip = true; if (join.getForeignKey() != null){ SQLBuffer where = new SQLBuffer(this); where.append("(").append(toTraditionalJoin(join)).append(")"); sel.where(where.getSQL()); } break; } } return skip; }
public void addJoinClassConditions() { if (_joins == null || _joins.joins() == null) return; // join set iterator allows concurrent modification Join j; 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, j.getRelationJoins()); j.setRelation(null, 0, null); } } }