@Override public DatabaseTable aliasForTable(DatabaseTable table) { return typeExpressionBase.aliasForTable(table); }
/** * INTERNAL: * Used in case outer joins should be printed in FROM clause. * Each of the additional tables mapped to expressions that joins it. */ public Map additionalExpressionCriteriaMap() { if (getDescriptor() == null) { return null; } HashMap tablesJoinExpressions = null; if(isUsingOuterJoinForMultitableInheritance()) { tablesJoinExpressions = new HashMap(); List childrenTables = getDescriptor().getInheritancePolicy().getChildrenTables(); for( int i=0; i < childrenTables.size(); i++) { DatabaseTable table = (DatabaseTable)childrenTables.get(i); Expression joinExpression = (Expression)getDescriptor().getInheritancePolicy().getChildrenTablesJoinExpressions().get(table); if (getBaseExpression() != null){ joinExpression = getBaseExpression().twist(joinExpression, this); } else { joinExpression = twist(joinExpression, this); } tablesJoinExpressions.put(table, joinExpression); } } return tablesJoinExpressions; }
public QueryKeyExpression newDerivedExpressionNamed(String attributeName) { QueryKeyExpression result = new QueryKeyExpression(attributeName, this); addDerivedExpression(result); return result; }
@Override public Expression getAllowingNull(String attributeName) { ObjectExpression exp = existingDerivedExpressionNamed(attributeName); // The same (aliased) table cannot participate in a normal join and an outer join. // To help enforce this, if the node already exists if (exp != null) { return exp; } ObjectExpression result = derivedExpressionNamed(attributeName); result.doUseOuterJoin(); return result; }
/** * Return the expression from the attribute dervied from this expression. */ @Override public Expression get(String attributeName, boolean forceInnerJoin) { ObjectExpression result = derivedExpressionNamed(attributeName); if (forceInnerJoin) { result.doNotUseOuterJoin(); } return result; }
/** * Defines a join between this expression and the target expression based on the ON clause. */ @Override public Expression leftJoin(Expression target, Expression onClause) { join(target, onClause); ((ObjectExpression)target).doUseOuterJoin(); return this; }
/** * INTERNAL: * Return the expression to join the main table of this node to any auxiliary tables. */ public Expression additionalExpressionCriteria() { if (getDescriptor() == null) { return null; } Expression criteria = getDescriptor().getQueryManager().getAdditionalJoinExpression(); if(getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) { if(isUsingOuterJoinForMultitableInheritance()) { Expression childrenCriteria = getDescriptor().getInheritancePolicy().getChildrenJoinExpression(); childrenCriteria = getBaseExpression().twist(childrenCriteria, this); childrenCriteria.convertToUseOuterJoin(); if(criteria == null) { criteria = childrenCriteria; } else { criteria = criteria.and(childrenCriteria); } } } return criteria; }
/** * INTERNAL: Not to be confused with the public getField(String) * This returns a collection of all fields associated with this object. Really * only applies to query keys representing an object or to expression builders. */ public Vector getFields() { if (getDescriptor() == null) { DatabaseMapping mapping = getMapping(); if (mapping != null) { return mapping.getSelectFields(); } return new NonSynchronizedVector(0); } if (descriptor.hasInheritance() && descriptor.getInheritancePolicy().shouldReadSubclasses() && (!descriptor.getInheritancePolicy().hasMultipleTableChild()) || shouldUseOuterJoinForMultitableInheritance()) { // return all fields because we can. return descriptor.getAllFields(); } else { return descriptor.getFields(); } }
Expression item = (Expression)items.elementAt(index); if ((this.selector == Ref) || ((this.selector == Deref) && (item.isObjectExpression()))) { DatabaseTable alias = ((ObjectExpression)item).aliasForTable(((ObjectExpression)item).getDescriptor().getTables().firstElement()); printer.printString(alias.getNameDelimited(printer.getPlatform())); } else if ((this.selector == Count) && (item.isExpressionBuilder())) {
ClassDescriptor descriptor = getDescriptor(); List<DatabaseTable> tables = null; if (descriptor == null) { List additionalTables = getAdditionalTables(); if (additionalTables == null) { return null; } else if ((descriptor.getHistoryPolicy() != null) && (getAsOfClause().getValue() != null)) { tables = descriptor.getHistoryPolicy().getHistoricalTables(); } else if (isUsingOuterJoinForMultitableInheritance()) { tables = descriptor.getInheritancePolicy().getAllTables(); } else { tables = descriptor.getTables(); List additionalTables = getAdditionalTables(); if (additionalTables != null) { tables = new Vector(tables);
/** * INTERNAL: * Like QueryKeyExpression, return the descriptor for the class type used, null if one can't be determined yet. * Should only be called when a session is already set. */ @Override public ClassDescriptor getContainingDescriptor() { return ((ObjectExpression)getBaseExpression()).getDescriptor(); }
this.sourceAlias = expression.getBaseExpression().aliasForTable(this.sourceTable); } else if (this.joinExpression != null) { this.sourceTable = ((ObjectExpression)this.joinExpression.getJoinSource()).getDescriptor().getTables().get(0); this.sourceAlias = this.joinExpression.getJoinSource().aliasForTable(this.sourceTable); this.targetTable = this.joinExpression.getDescriptor().getTables().get(0); this.targetAlias = this.joinExpression.aliasForTable(this.targetTable); } else { descriptor = joinExpression.getDescriptor();
/** * INTERNAL: * Rebuild myself against the base, with the values of parameters supplied by the context * expression. This is used for transforming a standalone expression (e.g. the join criteria of a mapping) * into part of some larger expression. You normally would not call this directly, instead calling twist * See the comment there for more details" */ @Override public Expression twistedForBaseAndContext(Expression newBase, Expression context, Expression oldBase) { Expression twistedBase = this.baseExpression.twistedForBaseAndContext(newBase, context, oldBase); DatabaseField field = getField(); ClassDescriptor descriptor = null; // Check for possible table per class rebuild and translate fields to correct table. // TODO: JPA also allows for field to be renamed in subclasses, this needs to account for that (never has...). if (this.baseExpression.isExpressionBuilder() && newBase.isObjectExpression() && ((this.baseExpression == oldBase) || (oldBase == null))) { if (((ObjectExpression)newBase).getSession() != null) { descriptor = ((ObjectExpression)newBase).getDescriptor(); } if ((descriptor != null) && descriptor.hasTablePerClassPolicy()) { field = field.clone(); field.setTable(descriptor.getDefaultTable()); } } return twistedBase.getField(field); }
/** * INTERNAL: * indicates whether additional expressions for multitable inheritance should be used and are available */ public boolean isUsingOuterJoinForMultitableInheritance() { return shouldUseOuterJoinForMultitableInheritance() && getDescriptor() != null && getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().hasMultipleTableChild() && getDescriptor().getInheritancePolicy().shouldReadSubclasses(); }
joinedAttribute.getBuilder().setSession(session.getRootSession(null)); if (joinedAttribute.getBuilder().getQueryClass() == null){ joinedAttribute.getBuilder().setQueryClass(descriptor.getJavaClass()); ClassDescriptor nestedDescriptor = joinedAttribute.getDescriptor();
/** * PUBLIC: * Return an expression representing traversal of a 1:many or many:many relationship. * This allows you to query whether any of the "many" side of the relationship satisfies the remaining criteria. * <p>Example: * <pre><blockquote> * Expression: employee.anyOf("managedEmployees").get("firstName").equal("Bob") * Java: no direct equivalent * SQL: SELECT DISTINCT ... WHERE (t2.MGR_ID = t1.ID) AND (t2.F_NAME = 'Bob') * </pre></blockquote> * @parameter shouldJoinBeIndependent indicates whether a new expression should be created. */ @Override public Expression anyOf(String attributeName, boolean shouldJoinBeIndependent) { QueryKeyExpression queryKey; if (shouldJoinBeIndependent) { queryKey = newDerivedExpressionNamed(attributeName); } else { queryKey = derivedExpressionNamed(attributeName); } queryKey.doQueryToManyRelationship(); return queryKey; }
public <Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute, JoinType jt) { if (((SingularAttribute)attribute).getType().getPersistenceType().equals(PersistenceType.BASIC)){ throw new IllegalStateException(ExceptionLocalization.buildMessage("CAN_NOT_JOIN_TO_BASIC")); } Class clazz = attribute.getBindableJavaType(); Join<X, Y> join = null; ObjectExpression exp = ((ObjectExpression)this.currentNode).newDerivedExpressionNamed(attribute.getName()); if (jt.equals(JoinType.LEFT)){ exp.doUseOuterJoin(); }else if(jt.equals(JoinType.RIGHT)){ throw new UnsupportedOperationException(ExceptionLocalization.buildMessage("RIGHT_JOIN_NOT_SUPPORTED")); }else{ exp.doNotUseOuterJoin(); } join = new JoinImpl<X, Y>(this, this.metamodel.managedType(clazz), this.metamodel, clazz, exp, attribute, jt); this.joins.add(join); ((FromImpl)join).isJoin = true; return join; }
/** * INTERNAL: * much like getOwnedTables(), this gets the tables represented from the descriptor. Difference is this only returns local tables * for the child casted descriptor, and excludes tables owned by the parent descriptor */ public List<DatabaseTable> getOwnedSubTables() { ClassDescriptor parentDescriptor = this.typeExpressionBase.getDescriptor(); Vector<DatabaseTable> childTables = new Vector(2); if (parentDescriptor.hasInheritance() && parentDescriptor.getInheritancePolicy().hasMultipleTableChild() ) { List parentTables = typeExpressionBase.getOwnedTables(); //All tables for this child, including parent tables Vector<DatabaseTable> tables = getDescriptor().getTables(); for (DatabaseTable table : tables) { if (!parentTables.contains(table)) { childTables.add(table); } } } return childTables; }
/** * INTERNAL: */ public Vector getOwnedTables() { if(isUsingOuterJoinForMultitableInheritance()) { return getDescriptor().getInheritancePolicy().getAllTables(); } else { return super.getOwnedTables(); } }
/** * INTERNAL: * Return if the expression is equal to the other. * This is used to allow dynamic expression's SQL to be cached. */ public boolean equals(Object expression) { if (this == expression) { return true; } // Return false for parallel expressions, as equality is unknown. return super.equals(expression) && ((getQueryClass() == null) && ((ExpressionBuilder)expression).getQueryClass() == null); }