All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openjpa.persistence.criteria.CriteriaBuilderImpl Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.openjpa.persistence.criteria;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import javax.persistence.Tuple;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.CompoundSelection;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Predicate.BooleanOperator;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Metamodel;

import org.apache.openjpa.kernel.ExpressionStoreQuery;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.ExpressionParser;
import org.apache.openjpa.kernel.exps.QueryExpressions;
import org.apache.openjpa.kernel.exps.Value;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.meta.MetamodelImpl;

/**
 * Factory for Criteria query expressions.
 *
 * Acts as an adapter to OpenJPA ExpressionFactory.
 *
 * @author Pinaki Poddar
 * @author Fay Wang
 *
 * @since 2.0.0
 *
 */
public class CriteriaBuilderImpl implements OpenJPACriteriaBuilder, ExpressionParser {
    private static final long serialVersionUID = 1L;
    private MetamodelImpl _model;

    public OpenJPACriteriaBuilder setMetaModel(MetamodelImpl model) {
        _model = model;
        return this;
    }

    @Override
    public Metamodel getMetamodel() {
        return _model;
    }

    @Override
    public QueryExpressions eval(Object parsed, ExpressionStoreQuery query,
        ExpressionFactory factory, ClassMetaData candidate) {
        CriteriaQueryImpl c = (CriteriaQueryImpl) parsed;
        return c.getQueryExpressions(factory);
    }

    @Override
    public Value[] eval(String[] vals, ExpressionStoreQuery query,
        ExpressionFactory factory, ClassMetaData candidate) {
        return null;
    }

    @Override
    public String getLanguage() {
        return LANG_CRITERIA;
    }

    /**
     *  Create a Criteria query object with the specified result type.
     *  @param resultClass  type of the query result
     *  @return query object
     */
    @Override
    public  OpenJPACriteriaQuery createQuery(Class resultClass) {
        return new CriteriaQueryImpl<>(_model, resultClass);
    }

    /**
     *  Create a Criteria query object that returns a tuple of
     *  objects as its result.
     *  @return query object
     */
    @Override
    public OpenJPACriteriaQuery createTupleQuery() {
        return new CriteriaQueryImpl<>(_model, Tuple.class);
    }

    @Override
    public  CriteriaUpdate createCriteriaUpdate(Class targetEntity) {
        throw new UnsupportedOperationException("JPA 2.1");
    }

    @Override
    public  CriteriaDelete createCriteriaDelete(Class targetEntity) {
        throw new UnsupportedOperationException("JPA 2.1");
    }

    @Override
    public Object parse(String ql, ExpressionStoreQuery query) {
        throw new AbstractMethodError();
    }

    @Override
    public void populate(Object parsed, ExpressionStoreQuery query) {
        CriteriaQueryImpl c = (CriteriaQueryImpl) parsed;
        query.invalidateCompilation();
        query.getContext().setCandidateType(c.getRoot().getJavaType(), true);
        query.setQuery(parsed);
    }

    @Override
    public  Expression abs(Expression x) {
        return new Expressions.Abs<>(x);
    }

    @Override
    public  Expression all(Subquery subquery) {
        return new Expressions.All<>(subquery);
    }

    @Override
    public Predicate and(Predicate... restrictions) {
    	return new PredicateImpl.And(restrictions);
    }

    @Override
    public Predicate and(Expression x, Expression y) {
    	return new PredicateImpl.And(x,y);
    }

    @Override
    public  Expression any(Subquery subquery) {
        return new Expressions.Any<>(subquery);
    }

    @Override
    public Order asc(Expression x) {
        return new OrderImpl(x, true);
    }

    @Override
    public  Expression avg(Expression x) {
        return new Expressions.Avg(x);
    }

    @Override
    public > Predicate between(Expression v, Expression x,
        Expression y) {
        return new Expressions.Between(v,x,y);
    }

    @Override
    public > Predicate between(Expression v, Y x, Y y) {
        return new Expressions.Between(v,x,y);
    }

    @Override
    public  Coalesce coalesce() {
        return new Expressions.Coalesce(Object.class);
    }

    @Override
    public  Expression coalesce(Expression x, Expression y) {
    	return new Expressions.Coalesce(x.getJavaType()).value(x).value(y);
    }

    @Override
    public  Expression coalesce(Expression x, Y y) {
    	return new Expressions.Coalesce(x.getJavaType()).value(x).value(y);
   }

    @Override
    public Expression concat(Expression x, Expression y) {
    	return new Expressions.Concat(x, y);
    }

    @Override
    public Expression concat(Expression x, String y) {
    	return new Expressions.Concat(x, y);
    }

    @Override
    public Expression concat(String x, Expression y) {
    	return new Expressions.Concat(x, y);
    }

    @Override
    public Predicate conjunction() {
        return new PredicateImpl.And();
    }

    @Override
    public Expression count(Expression x) {
        return new Expressions.Count(x);
    }

    @Override
    public Expression countDistinct(Expression x) {
        return new Expressions.Count(x, true);
    }

    @Override
    public OpenJPACriteriaQuery createQuery() {
        return new CriteriaQueryImpl<>(_model, Object.class);
    }

    @Override
    public Expression currentDate() {
    	return new Expressions.CurrentDate();
    }

    @Override
    public Expression