/** * INTERNAL: * Return the descriptor which contains this query key. */ public ClassDescriptor getContainingDescriptor() { return ((DataExpression)getBaseExpression()).getDescriptor(); }
/** * Iterate through a list of expressions searching for the given attribute name. * Return true if it is found, false otherwise. */ protected boolean isAttributeNameInJoinedExpressionList(String attributeName, List joinedExpressionList) { for (Iterator joinEnum = joinedExpressionList.iterator(); joinEnum.hasNext();) { QueryKeyExpression expression = (QueryKeyExpression)joinEnum.next(); while (!expression.getBaseExpression().isExpressionBuilder()) { expression = (QueryKeyExpression)expression.getBaseExpression(); } if (expression.getName().equals(attributeName)) { return true; } } return false; }
/** * Iterate through a list of expressions searching for the given attribute name. * Return true if it is found, false otherwise. */ protected boolean isAttributeNameInJoinedExpressionList(String attributeName, List joinedExpressionList) { for (Iterator joinEnum = joinedExpressionList.iterator(); joinEnum.hasNext();) { QueryKeyExpression expression = (QueryKeyExpression)joinEnum.next(); while (!expression.getBaseExpression().isExpressionBuilder()) { expression = (QueryKeyExpression)expression.getBaseExpression(); } if (expression.getName().equals(attributeName)) { return true; } } return false; }
/** * Iterate through a list of expressions searching for the given attribute name. * Return true if it is found, false otherwise. */ protected boolean isAttributeNameInJoinedExpressionList(String attributeName, List joinedExpressionList) { for (Iterator joinEnum = joinedExpressionList.iterator(); joinEnum.hasNext();) { QueryKeyExpression expression = (QueryKeyExpression)joinEnum.next(); while (!expression.getBaseExpression().isExpressionBuilder()) { expression = (QueryKeyExpression)expression.getBaseExpression(); } if (expression.getName().equals(attributeName)) { return true; } } return false; }
/** * INTERNAL: * Order by foreign key fields if a foreign key mapping (avoids joins). */ @Override public List<Expression> getOrderByNormalizedExpressions(Expression base) { if (this.foreignKeyFields.size() > 0) { List<Expression> orderBys = new ArrayList(this.foreignKeyFields.size()); for (DatabaseField field : this.foreignKeyFields) { orderBys.add(((QueryKeyExpression)base).getBaseExpression().getField(field)); } return orderBys; } return super.getOrderByNormalizedExpressions(base); }
/** * INTERNAL: * Order by foreign key fields if a foreign key mapping (avoids joins). */ @Override public List<Expression> getOrderByNormalizedExpressions(Expression base) { if (this.foreignKeyFields.size() > 0) { List<Expression> orderBys = new ArrayList(this.foreignKeyFields.size()); for (DatabaseField field : this.foreignKeyFields) { orderBys.add(((QueryKeyExpression)base).getBaseExpression().getField(field)); } return orderBys; } return super.getOrderByNormalizedExpressions(base); } }
/** * INTERNAL: * Return if partial attribute. */ public boolean isPartialAttribute(String attributeName) { if (this.partialAttributeExpressions == null) { return false; } List<Expression> partialAttributeExpressions = getPartialAttributeExpressions(); int size = partialAttributeExpressions.size(); for (int index = 0; index < size; index++) { QueryKeyExpression expression = (QueryKeyExpression)partialAttributeExpressions.get(index); while (!expression.getBaseExpression().isExpressionBuilder()) { expression = (QueryKeyExpression)expression.getBaseExpression(); } if (expression.getName().equals(attributeName)) { return true; } } return false; }
/** * INTERNAL: * Returns the join criteria stored in the mapping selection query. This criteria * is used to read reference objects across the tables from the database. */ public Expression getJoinCriteria(QueryKeyExpression exp) { Expression selectionCriteria = getSelectionCriteria(); return exp.getBaseExpression().twist(selectionCriteria, exp); }
/** * INTERNAL: * Return if partial attribute. */ public boolean isPartialAttribute(String attributeName) { if (!hasPartialAttributeExpressions()) { return false; } List<Expression> partialAttributeExpressions = getPartialAttributeExpressions(); int size = partialAttributeExpressions.size(); for (int index = 0; index < size; index++) { QueryKeyExpression expression = (QueryKeyExpression)partialAttributeExpressions.get(index); while (!expression.getBaseExpression().isExpressionBuilder()) { expression = (QueryKeyExpression)expression.getBaseExpression(); } if (expression.getName().equals(attributeName)) { return true; } } return false; }
/** * INTERNAL: * If there is root expression in the list then indicates whether it shouldUseOuterJoin, * otherwise return false. */ protected boolean hasRootExpressionThatShouldUseOuterJoin(List expressions) { for (Iterator expressionsEnum = expressions.iterator(); expressionsEnum.hasNext();) { Expression next = (Expression)expressionsEnum.next(); // The expressionBuilder can be one of the locked expressions in // the ForUpdateOfClause. if (!next.isQueryKeyExpression()) { continue; } QueryKeyExpression expression = (QueryKeyExpression)next; if (expression.getBaseExpression().isExpressionBuilder() && expression.getName().equals(getAttributeName())) { return expression.shouldUseOuterJoin(); } } return false; }
/** * INTERNAL: * If there is root expression in the list then indicates whether it shouldUseOuterJoin, * otherwise return false. */ protected boolean hasRootExpressionThatShouldUseOuterJoin(List expressions) { for (Iterator expressionsEnum = expressions.iterator(); expressionsEnum.hasNext();) { Expression next = (Expression)expressionsEnum.next(); // The expressionBuilder can be one of the locked expressions in // the ForUpdateOfClause. if (!next.isQueryKeyExpression()) { continue; } QueryKeyExpression expression = (QueryKeyExpression)next; if (expression.getBaseExpression().isExpressionBuilder() && expression.getName().equals(getAttributeName())) { return expression.shouldUseOuterJoin(); } } return false; }
/** * INTERNAL: * Returns the join criteria stored in the mapping selection query. This criteria * is used to read reference objects across the tables from the database. */ public Expression getJoinCriteria(QueryKeyExpression exp) { Expression selectionCriteria = getSelectionCriteria(); Expression keySelectionCriteria = containerPolicy.getKeySelectionCriteria(); if (keySelectionCriteria != null){ selectionCriteria = selectionCriteria.and(keySelectionCriteria); } return exp.getBaseExpression().twist(selectionCriteria, exp); }
/** * Return the alias for our table */ protected DatabaseTable getAliasedTable() { DataExpression base = (DataExpression)getBaseExpression(); DatabaseTable alias = base.aliasForTable(getField().getTable()); if (alias == null) { return getField().getTable(); } else { return alias; } }
/** * INTERNAL: * Join criteria is created to read target records (nested table) from the table. */ public Expression getJoinCriteria(QueryKeyExpression exp) { ExpressionBuilder builder = new ExpressionBuilder(); Expression selectionCriteria = builder.ref().equal(builder.value()); return exp.getBaseExpression().twist(selectionCriteria, exp); }
/** * INTERNAL: * Lookup the query key for this item. * If an aggregate of foreign mapping is found it is traversed. */ protected QueryKey getLeafQueryKeyFor(DatabaseQuery query, Expression expression, ClassDescriptor rootDescriptor, AbstractSession session) throws QueryException { // Check for database field expressions or place holder if ((expression == null) || (expression.isFieldExpression())) { return null; } if (!(expression.isQueryKeyExpression())) { return null; } QueryKeyExpression qkExpression = (QueryKeyExpression)expression; Expression baseExpression = qkExpression.getBaseExpression(); ClassDescriptor descriptor = baseExpression.getLeafDescriptor(query, rootDescriptor, session); return descriptor.getQueryKeyNamed(qkExpression.getName()); }
/** * 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" */ public Expression twistedForBaseAndContext(Expression newBase, Expression context) { Expression twistedBase = getBaseExpression().twistedForBaseAndContext(newBase, context); QueryKeyExpression result = (QueryKeyExpression)twistedBase.get(getName()); if (shouldUseOuterJoin) { result.doUseOuterJoin(); } if (shouldQueryToManyRelationship) { result.doQueryToManyRelationship(); } return result; }
/** * INTERNAL: * Lookup the query key for this item. * If an aggregate of foreign mapping is found it is traversed. */ protected QueryKey getLeafQueryKeyFor(DatabaseQuery query, Expression expression, ClassDescriptor rootDescriptor, AbstractSession session) throws QueryException { // Check for database field expressions or place holder if ((expression == null) || (expression.isFieldExpression())) { return null; } if (!(expression.isQueryKeyExpression())) { return null; } QueryKeyExpression qkExpression = (QueryKeyExpression)expression; Expression baseExpression = qkExpression.getBaseExpression(); ClassDescriptor descriptor = baseExpression.getLeafDescriptor(query, rootDescriptor, session); return descriptor.getQueryKeyNamed(qkExpression.getName()); }
/** * Calculate the source table for based on the various QueryKeyExpression * usages (join query keys, custom defined query keys, or query keys for * mappings). * * Called from {@link SQLSelectStatement#appendFromClauseForOuterJoin}. * * @return DatabaseTable */ public DatabaseTable getSourceTable() { if(getMapping() != null) { // Grab the source table from the mapping not just the first table // from the descriptor. In an joined inheritance hierarchy, the // fk used in the outer join may be from a subclasses's table. if (getMapping().isObjectReferenceMapping() && ((ObjectReferenceMapping) getMapping()).isForeignKeyRelationship()) { return getMapping().getFields().firstElement().getTable(); } else { return ((ObjectExpression)getBaseExpression()).getDescriptor().getTables().firstElement(); } } else { return ((ForeignReferenceQueryKey)getQueryKeyOrNull()).getSourceTable(); } }
/** * INTERNAL: * This expression is built on a different base than the one we want. Rebuild it and * return the root of the new tree */ public Expression rebuildOn(Expression newBase) { Expression newLocalBase = getBaseExpression().rebuildOn(newBase); QueryKeyExpression result = null; // For bug 3096634 rebuild outer joins correctly from the start. if (shouldUseOuterJoin) { result = (QueryKeyExpression)newLocalBase.getAllowingNull(getName()); } else { result = (QueryKeyExpression)newLocalBase.get(getName()); } if (shouldQueryToManyRelationship) { result.doQueryToManyRelationship(); } result.setSelectIfOrderedBy(selectIfOrderedBy()); return result; }
/** * INTERNAL: * Print java for project class generation */ public void printJava(ExpressionJavaPrinter printer) { getBaseExpression().printJava(printer); if (!shouldUseOuterJoin()) { if (!shouldQueryToManyRelationship()) { printer.printString(".get("); } else { printer.printString(".anyOf("); } } else { if (!shouldQueryToManyRelationship()) { printer.printString(".getAllowingNull("); } else { printer.printString(".anyOfAllowingNone("); } } printer.printString("\"" + getName() + "\")"); }