Search in sources :

Example 1 with TableExpression

use of org.eclipse.persistence.internal.expressions.TableExpression in project eclipselink by eclipse-ee4j.

the class HistoryPolicy method additionalHistoryExpression.

/**
 * INTERNAL:
 * Add any temporal querying conditions to this object expression.
 * @param tableIndex not null indicates that only expression for a single table should be returned.
 */
public Expression additionalHistoryExpression(Expression context, Expression base, Integer tableIndex) {
    // 
    AsOfClause clause = base.getAsOfClause();
    Object value = clause.getValue();
    Expression join = null;
    Expression subJoin = null;
    Expression start = null;
    Expression end = null;
    if (value == null) {
        return null;
    // for now nothing as assume mirroring historical tables.
    } else {
        if (value instanceof Expression) {
            // Print AS OF TIMESTAMP (SYSDATE - 1000*60*10) not AS OF ('SYSDATE - 1000*60*10').
            if ((value instanceof ConstantExpression) && (((ConstantExpression) value).getValue() instanceof String)) {
                value = (((ConstantExpression) value).getValue());
            }
        } else {
            ConversionManager converter = ConversionManager.getDefaultManager();
            value = converter.convertObject(value, ClassConstants.TIMESTAMP);
        }
        if (getMapping() != null) {
            if (tableIndex != null && tableIndex > 0) {
                return null;
            }
            TableExpression tableExp = null;
            DatabaseTable historicalTable = getHistoricalTables().get(0);
            tableExp = (TableExpression) ((ObjectExpression) base).existingDerivedTable(historicalTable);
            start = tableExp.getField(getStart());
            end = tableExp.getField(getEnd());
            join = start.lessThanEqual(value).and(end.isNull().or(end.greaterThan(value)));
            // We also need to do step two here in advance.
            tableExp.setTable(historicalTable);
            return join;
        }
        int iFirst, iLast;
        if (tableIndex == null) {
            // loop through all history tables
            iFirst = 0;
            iLast = getHistoricalTables().size() - 1;
        } else {
            // only return expression for the specified table
            iFirst = tableIndex;
            iLast = iFirst;
        }
        for (int i = iFirst; i <= iLast; i++) {
            start = base.getField(getStart(i));
            end = base.getField(getEnd(i));
            subJoin = start.lessThanEqual(value).and(end.isNull().or(end.greaterThan(value)));
            join = ((join == null) ? subJoin : join.and(subJoin));
        }
        return join;
    }
}
Also used : ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) HistoricalDatabaseTable(org.eclipse.persistence.internal.history.HistoricalDatabaseTable) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression) ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression)

Example 2 with TableExpression

use of org.eclipse.persistence.internal.expressions.TableExpression in project eclipselink by eclipse-ee4j.

the class DirectCollectionMapping method initializeSelectionCriteria.

protected void initializeSelectionCriteria(AbstractSession session) {
    Expression criteria = null;
    ExpressionBuilder base = new ExpressionBuilder();
    TableExpression table = (TableExpression) base.getTable(getReferenceTable());
    Iterator<DatabaseField> referenceKeys = getReferenceKeyFields().iterator();
    Iterator<DatabaseField> sourceKeys = getSourceKeyFields().iterator();
    while (referenceKeys.hasNext()) {
        DatabaseField referenceKey = referenceKeys.next();
        DatabaseField sourceKey = sourceKeys.next();
        Expression expression = table.getField(referenceKey).equal(base.getParameter(sourceKey));
        if (criteria == null) {
            criteria = expression;
        } else {
            criteria = expression.and(criteria);
        }
    }
    setSelectionCriteria(criteria);
}
Also used : ObjectExpression(org.eclipse.persistence.internal.expressions.ObjectExpression) Expression(org.eclipse.persistence.expressions.Expression) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression)

Example 3 with TableExpression

use of org.eclipse.persistence.internal.expressions.TableExpression in project eclipselink by eclipse-ee4j.

the class ForeignReferenceQueryKey method getRelationTable.

/**
 * PUBLIC:
 * Returns the relation table.
 * Currently only ManyToMany and OneToOne may have relation table.
 * The method is overridden to return null for other subclasses.
 * The returned relationTable still could be null.
 */
public DatabaseTable getRelationTable(ClassDescriptor referenceDescriptor) {
    ExpressionIterator expIterator = new ExpressionIterator() {

        @Override
        public void iterate(Expression each) {
            if (each.isTableExpression()) {
                ((Collection) this.getResult()).add(((TableExpression) each).getTable());
            } else if (each.isDataExpression()) {
                DatabaseField field = ((DataExpression) each).getField();
                if (field != null && field.hasTableName()) {
                    ((Collection) this.getResult()).add(field.getTable());
                }
            } else if (each.isParameterExpression()) {
                DatabaseField field = ((ParameterExpression) each).getField();
                if (field != null && field.hasTableName()) {
                    ((Collection) this.getResult()).add(field.getTable());
                }
            }
        }
    };
    expIterator.setResult(new HashSet());
    expIterator.iterateOn(this.joinCriteria);
    HashSet<DatabaseTable> tables = (HashSet) expIterator.getResult();
    DatabaseTable relationTable = null;
    Iterator<DatabaseTable> it = tables.iterator();
    while (it.hasNext()) {
        DatabaseTable table = it.next();
        // neither source nor reference descriptor contains table - must be relationTable
        if (!descriptor.getTables().contains(table) && !referenceDescriptor.getTables().contains(table)) {
            relationTable = table;
            break;
        }
    }
    return relationTable;
}
Also used : ExpressionIterator(org.eclipse.persistence.internal.expressions.ExpressionIterator) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) DataExpression(org.eclipse.persistence.internal.expressions.DataExpression) TableExpression(org.eclipse.persistence.internal.expressions.TableExpression) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ParameterExpression(org.eclipse.persistence.internal.expressions.ParameterExpression) Collection(java.util.Collection) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) HashSet(java.util.HashSet)

Aggregations

TableExpression (org.eclipse.persistence.internal.expressions.TableExpression)3 Expression (org.eclipse.persistence.expressions.Expression)2 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 DataExpression (org.eclipse.persistence.internal.expressions.DataExpression)1 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)1 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)1 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)1 HistoricalDatabaseTable (org.eclipse.persistence.internal.history.HistoricalDatabaseTable)1