public void addAdditionalExpression(Expression theExpression) { // This change puts a null check into every call, but is printing additional // expressions in a meaningfull order worth it? additionalExpression = (additionalExpression == null) ? theExpression : additionalExpression.and(theExpression); }
/** * INTERNAL * Returns left.and(right) if both are defined. */ public Expression appendExpression(Expression left, Expression right) { Expression expr = null; if (left == null) { expr = right; } else if (right == null) { expr = left; } else { expr = left.and(right); } return expr; }
/** * INTERNAL: * childrenTablesJoinExpressions, childrenTables, allTables and childrenJoinExpression * are created simultaneously and kept in sync. */ protected void addChildTableJoinExpression(DatabaseTable table, Expression expression) { if(childrenTablesJoinExpressions == null) { childrenTablesJoinExpressions = new HashMap(); // childrenTables should've been null, too childrenTables = new ArrayList(); // allTables should've been null, too allTables = new Vector(getDescriptor().getTables()); } childrenTables.add(table); allTables.add(table); childrenTablesJoinExpressions.put(table, expression); childrenJoinExpression = expression.and(childrenJoinExpression); }
/** * PUBLIC: * Return an expression that performs a key word search. * <p>Example: * <pre><blockquote> * TopLink: project.get("description").containsAllKeyWords("TopLink rdbms java") * </blockquote></pre> */ public Expression containsAllKeyWords(String spaceSeperatedKeyWords) { StringTokenizer tokenizer = new StringTokenizer(spaceSeperatedKeyWords); Expression expression = null; while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (expression == null) { expression = containsSubstringIgnoringCase(token); } else { expression = expression.and(containsSubstringIgnoringCase(token)); } } if (expression == null) { return like("%"); } else { return expression; } }
/** * Delete all criteria is created with target foreign keys and source keys. * This criteria is then used to delete target records from the table. */ protected Expression getDeleteAllCriteria(AbstractSession session) { Expression expression; Expression criteria = null; Expression builder = new ExpressionBuilder(); for (Iterator keys = getTargetForeignKeyToSourceKeys().keySet().iterator(); keys.hasNext();) { DatabaseField targetForeignKey = (DatabaseField)keys.next(); DatabaseField sourceKey = (DatabaseField)getTargetForeignKeyToSourceKeys().get(targetForeignKey); expression = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); criteria = expression.and(criteria); } return criteria; }
/** * INTERNAL: * When given an expression, this method will return a new expression * with the optimistic locking values included. The values are taken * from the passed in database row. This expression will be used in * an update call. */ public Expression buildUpdateExpression(DatabaseTable table, Expression mainExpression, AbstractRecord row, AbstractRecord row2) { if (cachedExpression == null) { cachedExpression = buildExpression(); } if (getWriteLockField().getTableName().equals(table.getName())) { return mainExpression.and(cachedExpression); } return mainExpression; }
/** * The selection criteria are created with target foreign keys and source "primary" keys. * These criteria are then used to read the target records from the table. * These criteria are also used as the default "delete all" criteria. * * CR#3922 - This method is almost the same as buildSelectionCriteria() the difference * is that TargetForeignKeysToSourceKeys contains more information after login then SourceKeyFields * contains before login. */ protected Expression buildDefaultSelectionCriteria() { Expression selectionCriteria = null; Expression builder = new ExpressionBuilder(); for (Iterator keys = this.getTargetForeignKeysToSourceKeys().keySet().iterator(); keys.hasNext();) { DatabaseField targetForeignKey = (DatabaseField)keys.next(); DatabaseField sourceKey = (DatabaseField)this.getTargetForeignKeysToSourceKeys().get(targetForeignKey); Expression partialSelectionCriteria = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); selectionCriteria = partialSelectionCriteria.and(selectionCriteria); } return selectionCriteria; }
/** * This method would allow customers to get the potential selection criteria for a mapping * prior to initialization. This would allow them to more easily create an ammendment method * that would ammend the SQL for the join. * * CR#3922 - This method is almost the same as buildDefaultSelectionCriteria() the difference * is that TargetForeignKeysToSourceKeys contains more information after login then SourceKeyFields * contains before login. */ public Expression buildSelectionCriteria() { //CR3922 Expression selectionCriteria = null; Expression builder = new ExpressionBuilder(); Enumeration sourceKeys = this.getSourceKeyFields().elements(); for (Enumeration targetForeignKeys = this.getTargetForeignKeyFields().elements(); targetForeignKeys.hasMoreElements();) { DatabaseField targetForeignKey = (DatabaseField)targetForeignKeys.nextElement(); DatabaseField sourceKey = (DatabaseField)sourceKeys.nextElement(); Expression partialSelectionCriteria = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); selectionCriteria = partialSelectionCriteria.and(selectionCriteria); } return selectionCriteria; }
/** * Build the primary key expression from the specified primary key values. */ public Expression buildPrimaryKeyExpressionFromKeys(Vector primaryKeyValues, AbstractSession session) { Expression expression = null; Expression subExpression; Expression builder = new ExpressionBuilder(); List primaryKeyFields = getDescriptor().getPrimaryKeyFields(); for (int index = 0; index < primaryKeyFields.size(); index++) { Object value = primaryKeyValues.get(index); DatabaseField field = (DatabaseField)primaryKeyFields.get(index); if (value != null) { subExpression = builder.getField(field).equal(value); expression = subExpression.and(expression); } } return expression; }
/** * INTERNAL: * Selection criteria is created with target foreign keys and source keys. * This criteria is then used to read records from the target table. */ protected void initializeSelectionCriteria(AbstractSession session) { Expression expression; Expression criteria; Expression builder = new ExpressionBuilder(); for (Iterator keys = getTargetForeignKeyToSourceKeys().keySet().iterator(); keys.hasNext();) { DatabaseField targetForeignKey = (DatabaseField)keys.next(); DatabaseField sourceKey = (DatabaseField)getTargetForeignKeyToSourceKeys().get(targetForeignKey); expression = builder.getField(targetForeignKey).equal(builder.getParameter(sourceKey)); criteria = expression.and(getSelectionCriteria()); setSelectionCriteria(criteria); } }
/** * INTERNAL: * Selection criteria is created with source foreign keys and target keys. * This criteria is then used to read target records from the table. * * CR#3922 - This method is almost the same as buildSelectionCriteria() the difference * is that getSelectionCriteria() is called */ protected void initializeSelectionCriteria(AbstractSession session) { if (getSourceToTargetKeyFields().isEmpty()) { throw DescriptorException.noForeignKeysAreSpecified(this); } Expression criteria; Expression builder = new ExpressionBuilder(); for (Iterator entries = getSourceToTargetKeyFields().entrySet().iterator(); entries.hasNext();) { Map.Entry entry = (Map.Entry) entries.next(); DatabaseField foreignKey = (DatabaseField)entry.getKey(); DatabaseField targetKey = (DatabaseField)entry.getValue(); Expression expression = builder.getField(targetKey).equal(builder.getParameter(foreignKey)); criteria = expression.and(getSelectionCriteria()); setSelectionCriteria(criteria); } }
/** * INTERNAL: * Used to allow object level comparisons. */ public Expression buildObjectJoinExpression(Expression expression, Expression argument, AbstractSession session) { Expression attributeByAttributeComparison = null; //Enumeration mappingsEnum = getSourceToTargetKeyFields().elements(); Enumeration mappingsEnum = getReferenceDescriptor().getMappings().elements(); for (; mappingsEnum.hasMoreElements();) { DatabaseMapping mapping = (DatabaseMapping)mappingsEnum.nextElement(); String attributeName = mapping.getAttributeName(); Expression join = expression.get(attributeName).equal(argument.get(attributeName)); if (attributeByAttributeComparison == null) { attributeByAttributeComparison = join; } else { attributeByAttributeComparison = attributeByAttributeComparison.and(join); } } return attributeByAttributeComparison; }
/** * INTERNAL * Return a TopLink expression by 'AND'ing the expressions from the left and right nodes */ public Expression generateExpression(GenerationContext context) { // Get the left expression Expression whereClause = getLeft().generateExpression(context); // Or it with whatever the right expression is whereClause = whereClause.and(getRight().generateExpression(context)); // and return the expression... return whereClause; } }
protected void initializeSelectionCriteria(AbstractSession session) { Expression exp1; Expression exp2; Expression expression; Expression criteria = null; Enumeration referenceKeysEnum; Enumeration sourceKeysEnum; ExpressionBuilder base = new ExpressionBuilder(); TableExpression table = (TableExpression)base.getTable(getReferenceTable()); referenceKeysEnum = getReferenceKeyFields().elements(); sourceKeysEnum = getSourceKeyFields().elements(); for (; referenceKeysEnum.hasMoreElements();) { DatabaseField referenceKey = (DatabaseField)referenceKeysEnum.nextElement(); DatabaseField sourceKey = (DatabaseField)sourceKeysEnum.nextElement(); exp1 = table.getField(referenceKey); exp2 = base.getParameter(sourceKey); expression = exp1.equal(exp2); if (criteria == null) { criteria = expression; } else { criteria = expression.and(criteria); } } setSelectionCriteria(criteria); }
/** * Build the primary key expression for the secondary table. */ public Expression buildPrimaryKeyExpression(DatabaseTable table) throws DescriptorException { if (getDescriptor().getTables().firstElement().equals(table)) { return getPrimaryKeyExpression(); } Map keyMapping = (Map)getDescriptor().getAdditionalTablePrimaryKeyFields().get(table); if (keyMapping == null) { throw DescriptorException.multipleTablePrimaryKeyNotSpecified(getDescriptor()); } ExpressionBuilder builder = new ExpressionBuilder(); Expression expression = null; for (Iterator primaryKeyEnum = keyMapping.values().iterator(); primaryKeyEnum.hasNext();) { DatabaseField field = (DatabaseField)primaryKeyEnum.next(); expression = (builder.getField(field).equal(builder.getParameter(field))).and(expression); } return expression; }
/** * INTERNAL * Build a context for the expression generation */ public Expression generateExpression(DatabaseQuery readQuery, GenerationContext generationContext) { Expression selectExpression = getQueryNode().generateExpression(generationContext); if (getWhereNode() == null) { return selectExpression; } Expression whereExpression = getWhereNode().generateExpression(generationContext); selectExpression = getQueryNode().generateExpression(generationContext); if (selectExpression != null) { whereExpression = selectExpression.and(whereExpression); } return whereExpression; }
/** * INTERNAL: * Append the branch with all subclasses expression to the statement. */ public void appendWithAllSubclassesExpression(SQLSelectStatement selectStatement) { if (getWithAllSubclassesExpression() != null) { // For Flashback: Must always rebuild with simple expression on right. if (selectStatement.getWhereClause() == null) { selectStatement.setWhereClause((Expression)getWithAllSubclassesExpression().clone()); } else { selectStatement.setWhereClause(selectStatement.getWhereClause().and(getWithAllSubclassesExpression())); } } }
/** * Return the appropriate select statement to perform a does exist check * @param fields - fields for does exist check. */ protected SQLSelectStatement buildSelectStatementForDoesExist(DatabaseField field) { // Build appropriate select statement SQLSelectStatement selectStatement; selectStatement = new SQLSelectStatement(); selectStatement.addField(field); selectStatement.setWhereClause(((Expression)getDescriptor().getObjectBuilder().getPrimaryKeyExpression().clone()).and(getDescriptor().getQueryManager().getAdditionalJoinExpression())); selectStatement.setTranslationRow(getTranslationRow()); selectStatement.normalize(getSession(), getQuery().getDescriptor()); return selectStatement; }
/** * 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 (criteria != null) { criteria = getBaseExpression().twist(criteria, this); if (shouldUseOuterJoin() && getSession().getPlatform().shouldPrintOuterJoinInWhereClause()) { criteria.convertToUseOuterJoin(); } } 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; }
/** * Return the appropriate select statement containing the fields in the table. * This is used as a second read to a concrete class with subclasses in an abstract-multiple table read. */ protected SQLSelectStatement buildConcreteSelectStatement() { // 2612538 - the default size of IdentityHashtable (32) is appropriate IdentityHashtable clonedExpressions = new IdentityHashtable(); SQLSelectStatement selectStatement = buildBaseSelectStatement(false, clonedExpressions); // Case of class with subclasses that also has instances on abstract-multiple table read. if (getDescriptor().hasInheritance() && getDescriptor().getInheritancePolicy().shouldReadSubclasses()) { Expression concrete = (Expression)getDescriptor().getInheritancePolicy().getOnlyInstancesExpression(); if ((concrete != null) && (selectStatement.getWhereClause() != null)) { selectStatement.setWhereClause(selectStatement.getWhereClause().and(concrete)); } else if (concrete != null) { selectStatement.setWhereClause((Expression)concrete.clone()); } } selectStatement.setFields(getSelectionFields(selectStatement, false)); selectStatement.normalize(getSession(), getDescriptor(), clonedExpressions); // Allow for joining indexes to be computed to ensure distinct rows ((ObjectLevelReadQuery)getQuery()).getJoinedAttributeManager().computeJoiningMappingIndexes(false, getSession(), 0); return selectStatement; }