Search in sources :

Example 1 with ConstantExpression

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

the class SubQueryImpl method select.

/**
 * Specify the item that is to be returned in the query result.
 * Replaces the previously specified selection, if any.
 * @param selection  selection specifying the item that
 *        is to be returned in the query result
 * @return the modified query
 */
@Override
public Subquery<T> select(Expression<T> selection) {
    findRootAndParameters(selection);
    for (Iterator<Root<?>> iterator = this.getRoots().iterator(); iterator.hasNext(); ) {
        findJoins((FromImpl) iterator.next());
    }
    for (Iterator<Join<?, ?>> iterator = this.getCorrelatedJoins().iterator(); iterator.hasNext(); ) {
        findJoins((FromImpl) iterator.next());
    }
    this.selection = (SelectionImpl) selection;
    this.queryType = (Class<T>) selection.getJavaType();
    this.subQuery.getItems().clear();
    if (selection.isCompoundSelection()) {
        int count = 0;
        for (Selection select : selection.getCompoundSelectionItems()) {
            this.subQuery.addItem(String.valueOf(count), ((InternalSelection) select).getCurrentNode());
            ++count;
        }
        this.subQuery.setExpressionBuilder(((InternalSelection) selection.getCompoundSelectionItems().get(0)).getCurrentNode().getBuilder());
    } else {
        TypeImpl<? extends T> type = ((MetamodelImpl) this.metamodel).getType(selection.getJavaType());
        if (type != null && type.getPersistenceType().equals(PersistenceType.ENTITY)) {
            this.subQuery.addAttribute("", new ConstantExpression(1, ((InternalSelection) selection).getCurrentNode().getBuilder()));
            this.subQuery.addNonFetchJoinedAttribute(((InternalSelection) selection).getCurrentNode());
        } else {
            String itemName = selection.getAlias();
            if (itemName == null) {
                itemName = ((InternalSelection) selection).getCurrentNode().getName();
            }
            this.subQuery.addItem(itemName, ((InternalSelection) selection).getCurrentNode());
        }
        this.subQuery.setExpressionBuilder(((InternalSelection) selection).getCurrentNode().getBuilder());
    }
    return this;
}
Also used : MetamodelImpl(org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl) Root(jakarta.persistence.criteria.Root) Selection(jakarta.persistence.criteria.Selection) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) CollectionJoin(jakarta.persistence.criteria.CollectionJoin) ListJoin(jakarta.persistence.criteria.ListJoin) SetJoin(jakarta.persistence.criteria.SetJoin) MapJoin(jakarta.persistence.criteria.MapJoin) Join(jakarta.persistence.criteria.Join)

Example 2 with ConstantExpression

use of org.eclipse.persistence.internal.expressions.ConstantExpression 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 3 with ConstantExpression

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

the class ExistsNode method generateExpression.

/**
 * INTERNAL
 * Generate the EclipseLink expression for this node
 */
@Override
public Expression generateExpression(GenerationContext context) {
    SubqueryNode subqueryNode = (SubqueryNode) getLeft();
    ReportQuery reportQuery = subqueryNode.getReportQuery(context);
    // Replace the SELECT clause of the exists subquery by SELECT 1 to
    // avoid problems with databases not supporting multiple columns in the
    // subquery SELECT clause in SQL.
    // The original select clause expressions might include relationship
    // navigations which should result in FK joins in the generated SQL,
    // e.g. ... EXISTS (SELECT o.customer FROM Order o ...). Add the
    // select clause expressions as non fetch join attributes to the
    // ReportQuery representing the subquery. This make sure the FK joins
    // get generated.
    List<ReportItem> items = reportQuery.getItems();
    for (Iterator<ReportItem> i = items.iterator(); i.hasNext(); ) {
        ReportItem item = i.next();
        Expression expr = item.getAttributeExpression();
        reportQuery.addNonFetchJoinedAttribute(expr);
    }
    reportQuery.clearItems();
    Expression one = new ConstantExpression(1, new ExpressionBuilder());
    reportQuery.addItem("one", one);
    reportQuery.dontUseDistinct();
    Expression expr = context.getBaseExpression();
    return notIndicated() ? expr.notExists(reportQuery) : expr.exists(reportQuery);
}
Also used : ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Example 4 with ConstantExpression

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

the class ExpressionBuilderVisitor method visit.

@Override
public void visit(KeywordExpression expression) {
    String keyword = expression.getText();
    Object value;
    if (keyword == KeywordExpression.NULL) {
        value = null;
        type[0] = Object.class;
    } else if (keyword == KeywordExpression.TRUE) {
        value = Boolean.TRUE;
        type[0] = Boolean.class;
    } else {
        value = Boolean.FALSE;
        type[0] = Boolean.class;
    }
    queryExpression = queryContext.getBaseExpression();
    queryExpression = new ConstantExpression(value, queryExpression);
}
Also used : DateConstantExpression(org.eclipse.persistence.internal.expressions.DateConstantExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression)

Example 5 with ConstantExpression

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

the class ExpressionBuilderVisitor method visit.

@Override
public void visit(EntityTypeLiteral expression) {
    ClassDescriptor descriptor = queryContext.getDescriptor(expression.getEntityTypeName());
    type[0] = descriptor.getJavaClass();
    queryExpression = new ConstantExpression(type[0], queryContext.getBaseExpression());
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DateConstantExpression(org.eclipse.persistence.internal.expressions.DateConstantExpression) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression)

Aggregations

ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)19 Expression (org.eclipse.persistence.expressions.Expression)7 DateConstantExpression (org.eclipse.persistence.internal.expressions.DateConstantExpression)6 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)4 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)4 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)4 List (java.util.List)3 FieldExpression (org.eclipse.persistence.internal.expressions.FieldExpression)3 ObjectExpression (org.eclipse.persistence.internal.expressions.ObjectExpression)3 QueryKeyExpression (org.eclipse.persistence.internal.expressions.QueryKeyExpression)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Vector (java.util.Vector)2 ExpressionIterator (org.eclipse.persistence.internal.expressions.ExpressionIterator)2