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.CompoundSelection;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import javax.persistence.criteria.Predicate.BooleanOperator;
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
 *
 */
@SuppressWarnings("serial")
public class CriteriaBuilderImpl implements OpenJPACriteriaBuilder, ExpressionParser {

    private MetamodelImpl _model;

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

    public QueryExpressions eval(Object parsed, ExpressionStoreQuery query,
        ExpressionFactory factory, ClassMetaData candidate) {
        CriteriaQueryImpl c = (CriteriaQueryImpl) parsed;
        return c.getQueryExpressions(factory);
    }
    
    public Value[] eval(String[] vals, ExpressionStoreQuery query,
        ExpressionFactory factory, ClassMetaData candidate) {
        return null;
    }

    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
     */
    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
     */
    public OpenJPACriteriaQuery createTupleQuery() {
        return new CriteriaQueryImpl(_model, Tuple.class);
    }

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

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

    public  Expression abs(Expression x) {
        return new Expressions.Abs(x);
    }

    public  Expression all(Subquery subquery) {
        return new Expressions.All(subquery);
    }

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

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

    public  Expression any(Subquery subquery) {
        return new Expressions.Any(subquery);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public Expression