/** * INTERNAL: * Indicates whether this expression corresponds to OneToOne. */ public boolean isOneToOne() { if(getMapping() != null) { return getMapping().isOneToOneMapping(); } else { if(getQueryKeyOrNull() != null) { return this.queryKey.isOneToOneQueryKey(); } else { return false; } } }
/** * INTERNAL: * A special version of rebuildOn where the newBase need not be a new * ExpressionBuilder but any expression. * <p> * For nested joined attributes, the joined attribute query must have * its joined attributes rebuilt relative to it. */ public Expression rebuildOn(Expression oldBase, Expression newBase) { if (this == oldBase) { return newBase; } Expression newLocalBase = ((QueryKeyExpression)getBaseExpression()).rebuildOn(oldBase, 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: * Alias the database field for our current environment */ protected void initializeAliasedField() { DatabaseField tempField = getField().clone(); DatabaseTable aliasedTable = getAliasedTable(); // Put in a special check here so that if the aliasing does nothing we don't cache the // result because it's invalid. This saves us from caching premature data if e.g. debugging // causes us to print too early" // if (aliasedTable.equals(getField().getTable())) { // return; // } else { aliasedField = tempField; aliasedField.setTable(aliasedTable); // } }
/** * INTERNAL: * Used to print a debug form of the expression tree. */ public void writeDescriptionOn(BufferedWriter writer) throws IOException { writer.write(getName()); writer.write(tableAliasesDescription()); }
/** * 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 object) { if (this == object) { return true; } if (!super.equals(object)) { return false; } QueryKeyExpression expression = (QueryKeyExpression) object; // Return false for anyOf expressions, as equality is unknown. if (shouldQueryToManyRelationship() || expression.shouldQueryToManyRelationship()) { return false; } return ((getName() == expression.getName()) || ((getName() != null) && getName().equals(expression.getName()))); }
Expression clonedExpression = checkJoinForSubSelectWithParent(normalizer, base, foreignKeyJoinPointer); if (clonedExpression != null) { return clonedExpression; DatabaseMapping mapping = getMapping(); SQLSelectStatement statement = normalizer.getStatement(); if ((mapping != null) && mapping.isDirectToXMLTypeMapping()) { if (isAttribute()) { return this; query.getQueryResultsCachePolicy().getInvalidationClasses().add(mapping.getReferenceDescriptor().getJavaClass()); } else { QueryKey queryKey = getQueryKeyOrNull(); if ((queryKey != null) && queryKey.isForeignReferenceQueryKey()) { query.getQueryResultsCachePolicy().getInvalidationClasses().add(((ForeignReferenceQueryKey)queryKey).getReferenceClass()); if (shouldQueryToManyRelationship() && (!statement.isDistinctComputed()) && (!statement.isAggregateSelect())) { statement.useDistinct(); if (!shouldQueryToManyRelationship()) { return this; Expression mappingExpression = mappingCriteria(base); if (mappingExpression != null) { mappingExpression = mappingExpression.normalize(normalizer); if (shouldUseOuterJoin() && (getSession().getPlatform().isInformixOuterJoin())) { setOuterJoinExpIndex(statement.addOuterJoinExpressionsHolders(this, mappingExpression, null, null));
if (hasBeenNormalized()) { return this; setHasBeenNormalized(true); if ((getMapping() != null) && getMapping().isDirectToXMLTypeMapping()) { normalizer.getStatement().setRequiresAliases(true); if (isAttribute()) { return this; if ((getMapping() != null) && (getMapping().isReferenceMapping() || getMapping().isStructureMapping())) { normalizer.getStatement().setRequiresAliases(true); return this; if (shouldQueryToManyRelationship() && (!normalizer.getStatement().isDistinctComputed()) && (!normalizer.getStatement().isAggregateSelect())) { normalizer.getStatement().useDistinct(); if ((getMapping() != null) && getMapping().isNestedTableMapping()) { if (!shouldQueryToManyRelationship()) { return this; Expression mappingExpression = mappingCriteria(); if (mappingExpression != null) { mappingExpression = mappingExpression.normalize(normalizer); if (shouldUseOuterJoin() && (getSession().getPlatform().isInformixOuterJoin())) { normalizer.getStatement().getOuterJoinExpressions().addElement(this); normalizer.getStatement().getOuterJoinedMappingCriteria().addElement(mappingExpression);
if ((getQueryKeyOrNull() == null) && (getMapping() == null)) { throw QueryException.invalidQueryKeyInExpression(getName()); QueryKey queryKey = getQueryKeyOrNull(); DatabaseMapping mapping = getMapping(); throw QueryException.outerJoinIsOnlyValidForOneToOneMappings(getMapping()); CollectionMapping collectionMapping = (CollectionMapping)getMapping(); if(collectionMapping.getListOrderField() != null) { index.setField(collectionMapping.getListOrderField()); if(collectionMapping.shouldUseListOrderFieldTableExpression()) { Expression newBase = getTable(collectionMapping.getListOrderField().getTable()); index.setBaseExpression(newBase); } else { addDerivedField(index); if ((!shouldQueryToManyRelationship()) && qkIsToMany && (!isNestedMapping)) { throw QueryException.invalidUseOfToManyQueryKeyInExpression(theOneThatsNotNull); if (shouldQueryToManyRelationship() && !qkIsToMany) { throw QueryException.invalidUseOfAnyOfInExpression(theOneThatsNotNull);
if(!isClonedForSubQuery && statement.isSubSelect() && statement.getParentStatement().getBuilder().equals(getBuilder())) { if (baseExpression.isQueryKeyExpression()) { QueryKeyExpression baseQueryKeyExpression = (QueryKeyExpression) baseExpression; DatabaseMapping mapping = baseQueryKeyExpression.getMapping(); if (mapping != null && mapping.isOneToOneMapping()) { if (statement.getOptimizedClonedExpressions().containsKey(this)) { if (baseQueryKeyExpression.hasBeenNormalized()) { clonedBaseExpression = (QueryKeyExpression) baseQueryKeyExpression.normalize(normalizer); if (clonedBaseExpression == null && baseQueryKeyExpression.getBaseExpression().isQueryKeyExpression()) { DatabaseMapping basebaseExprMapping = ((QueryKeyExpression)baseQueryKeyExpression.getBaseExpression()).getMapping(); if (basebaseExprMapping != null && basebaseExprMapping.isOneToOneMapping()) { clonedBaseExpression = (QueryKeyExpression) baseQueryKeyExpression.normalize(normalizer); clonedBaseExpression = new QueryKeyExpression(baseQueryKeyExpression.getName(), baseQueryKeyExpression.getBaseExpression()); clonedBaseExpression.shouldQueryToManyRelationship = baseQueryKeyExpression.shouldQueryToManyRelationship; clonedBaseExpression.shouldUseOuterJoin = baseQueryKeyExpression.shouldUseOuterJoin; clonedBaseExpression = (QueryKeyExpression) clonedBaseExpression.normalize(normalizer); statement.addOptimizedClonedExpressions(baseQueryKeyExpression, clonedBaseExpression); QueryKeyExpression clonedExpression = new QueryKeyExpression(name, clonedBaseExpression); clonedExpression.shouldQueryToManyRelationship = this.shouldQueryToManyRelationship; clonedExpression.shouldUseOuterJoin = this.shouldUseOuterJoin; clonedExpression = (QueryKeyExpression) clonedExpression.normalize(normalizer, clonedExpression, foreignKeyJoinPointer);
/** * INTERNAL: * Print SQL onto the stream, using the ExpressionPrinter for context */ @Override public void printSQL(ExpressionSQLPrinter printer) { if (isAttribute()) { printer.printField(getAliasedField()); } // If the mapping is a direct collection then this falls into a gray area. // It must be treated as an attribute at this moment for it has a direct field. // However it is not an attribute in the sense that it also represents a foreign // reference and a mapping criteria has been added. // For bug 2900974 these are now handled as non-attributes during normalize but // as attributes when printing SQL. // if ((!isAttribute()) && (getMapping() != null) && getMapping().isDirectCollectionMapping()) { DirectCollectionMapping directCollectionMapping = (DirectCollectionMapping)getMapping(); // The aliased table comes for free as it was a required part of the join criteria. TableExpression table = (TableExpression)getTable(directCollectionMapping.getReferenceTable()); DatabaseTable aliasedTable = table.aliasForTable(table.getTable()); DatabaseField aliasedField = directCollectionMapping.getDirectField().clone(); aliasedField.setTable(aliasedTable); printer.printField(aliasedField); } if ((getMapping() != null) && getMapping().isNestedTableMapping()) { DatabaseTable tableAlias = aliasForTable(new NestedTable(this)); printer.printString(tableAlias.getName()); } }
QueryKeyExpression expression = (QueryKeyExpression)this.joinExpression; if (isMapKeyHolder) { descriptor = expression.getMapKeyDescriptor(); this.targetTable = descriptor.getTables().get(0); this.targetAlias = outerJoinedMappingCriteria.aliasForTable(this.targetTable); } else { if(expression.isMapKeyObjectRelationship()) { this.mapKeyHolder = new OuterJoinExpressionHolder(this); this.mapKeyHolder.process(usesHistory, true); descriptor = expression.getDescriptor(); this.targetTable = expression.getReferenceTable(); this.targetAlias = expression.aliasForTable(this.targetTable); this.sourceTable = expression.getSourceTable(); this.sourceAlias = expression.getBaseExpression().aliasForTable(this.sourceTable); } else if (this.joinExpression != null) { this.sourceTable = ((ObjectExpression)this.joinExpression.getJoinSource()).getDescriptor().getTables().get(0);
private String getQualifiedName(DatasourcePlatform platform, boolean allowDelimiters){ if (qualifiedName == null) { // Print nested table using the TABLE function. DatabaseMapping mapping = queryKeyExpression.getMapping(); DatabaseTable nestedTable = mapping.getDescriptor().getTables().firstElement(); DatabaseTable tableAlias = queryKeyExpression.getBaseExpression().aliasForTable(nestedTable); DatabaseTable nestedTableAlias = queryKeyExpression.aliasForTable(this); StringBuffer name = new StringBuffer(); name.append("TABLE("); if (allowDelimiters && useDelimiters){ name.append(platform.getStartDelimiter()); } name.append(tableAlias.getName()); if (allowDelimiters && useDelimiters){ name.append(platform.getEndDelimiter()); } name.append("."); if (allowDelimiters && useDelimiters){ name.append(platform.getStartDelimiter()); } name.append(mapping.getField().getNameDelimited(platform)); if (allowDelimiters && useDelimiters){ name.append(platform.getEndDelimiter()); } name.append(")"); qualifiedName = name.toString(); } return qualifiedName; }
&& (((QueryKeyExpression)first).getBaseExpression() != null) && ((QueryKeyExpression)first).getBaseExpression().isQueryKeyExpression()) { QueryKeyExpression mappingExpression = (QueryKeyExpression)((QueryKeyExpression)first).getBaseExpression(); if ((mappingExpression.getBaseExpression() != null) && mappingExpression.getBaseExpression().isObjectExpression() && (!mappingExpression.shouldUseOuterJoin())) { mappingExpression.getBaseExpression().normalize(normalizer); DatabaseMapping mapping = mappingExpression.getMapping(); if ((mapping != null) && mapping.isOneToOneMapping() && (!((OneToOneMapping)mapping).hasCustomSelectionQuery()) && ((OneToOneMapping)mapping).isForeignKeyRelationship() && (second.isConstantExpression() || second.isParameterExpression())) { DatabaseField targetField = ((QueryKeyExpression)first).getField(); DatabaseField sourceField = ((OneToOneMapping)mapping).getTargetToSourceKeyFields().get(targetField); if (sourceField != null) { Expression optimizedExpression = this.operator.expressionFor(mappingExpression.getBaseExpression().getField(sourceField), second);
/** * INTERNAL: * This expression is built on a different base than the one we want. Rebuild it and * return the root of the new tree */ @Override public Expression rebuildOn(Expression newBase) { Expression newLocalBase = this.baseExpression.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()); if (castClass != null){ result.setCastClass(castClass); } return result; }
/** * 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; }
DatabaseTable targetAlias = null; if (outerExpression != null) { targetTable = outerExpression.getReferenceTable(); sourceTable = outerExpression.getSourceTable(); sourceAlias = outerExpression.getBaseExpression().aliasForTable(sourceTable); targetAlias = outerExpression.aliasForTable(targetTable); } else { sourceTable = ((ClassDescriptor)getDescriptorsForMultitableInheritanceOnly().get(index)).getTables().firstElement(); printAdditionalJoins(printer, outerJoinedAliases, (ClassDescriptor)getDescriptorsForMultitableInheritanceOnly().get(index), (Map)getOuterJoinedAdditionalJoinCriteria().elementAt(index), aliasesOfTablesToBeLocked, shouldPrintUpdateClauseForAllTables); } else { DatabaseTable relationTable = outerExpression.getRelationTable(); if (relationTable == null) { if (outerExpression.isDirectCollection()) { printForUpdateClauseOnJoin(targetAlias, printer, shouldPrintUpdateClauseForAllTables, aliasesOfTablesToBeLocked, platform); if (hasAdditionalJoinExpressions && platform.supportsNestingOuterJoins()) { printAdditionalJoins(printer, outerJoinedAliases, outerExpression.getDescriptor(), tablesJoinExpression, aliasesOfTablesToBeLocked, shouldPrintUpdateClauseForAllTables); writer.write(")"); printOnClause(sourceToTargetJoin, printer, platform); if (hasAdditionalJoinExpressions && !platform.supportsNestingOuterJoins()) { printAdditionalJoins(printer, outerJoinedAliases, outerExpression.getDescriptor(), tablesJoinExpression, aliasesOfTablesToBeLocked, shouldPrintUpdateClauseForAllTables); printAdditionalJoins(printer, outerJoinedAliases, outerExpression.getDescriptor(), tablesJoinExpression, aliasesOfTablesToBeLocked, shouldPrintUpdateClauseForAllTables);
/** * 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: * 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() + "\")"); }
/** * INTERNAL: * Return if the expression if for a map key mapping where the key is a OneToOne. */ public boolean isMapKeyObjectRelationship() { if (getMapping() != null) { return getMapping().isCollectionMapping() && ((CollectionMapping)getMapping()).isMapKeyObjectRelationship(); } else { return false; } }
/** * INTERNAL: * Return all the fields */ public Vector getFields() { if (isAttribute()) { Vector result = new Vector(1); DatabaseField field = getField(); if (field != null) { result.addElement(field); } return result; } else { Vector result = new Vector(); result.addAll(super.getFields()); if (mapping.isCollectionMapping()){ List<DatabaseField> fields = mapping.getContainerPolicy().getAllFieldsForMapKey((CollectionMapping)mapping); if (fields != null){ result.addAll(fields); } } return result; } }