openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r781621 [3/3] - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence/src/main/java/org/apache/openjpa/persiste...
Date Wed, 03 Jun 2009 23:44:59 GMT
Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java?rev=781621&r1=781620&r2=781621&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PredicateImpl.java
Wed Jun  3 23:44:58 2009
@@ -21,7 +21,6 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Expression;
 import javax.persistence.criteria.Predicate;
 
@@ -29,27 +28,27 @@
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
 
 public class PredicateImpl extends ExpressionImpl<Boolean> 
-    implements Predicate {
+implements Predicate {
     List<Expression<Boolean>> _exps;
     BooleanOperator _op;
     boolean _negated = false;
-    
+
     protected PredicateImpl() {
-    	super(Boolean.class);
+        super(Boolean.class);
     }
-                               
+
     protected PredicateImpl(BooleanOperator op) {
         this();
         _op = op;
     }
-    
+
     protected PredicateImpl(BooleanOperator op, Predicate...restrictions) {
-		this(op);
-		if (restrictions != null) {
-			for (Predicate p : restrictions)
-				add((PredicateImpl)p);
-		}
-	}
+        this(op);
+        if (restrictions != null) {
+            for (Predicate p : restrictions)
+                add((PredicateImpl)p);
+        }
+    }
 
     public PredicateImpl add(Expression<Boolean> s) {
         if (_exps == null)
@@ -71,60 +70,64 @@
     }
 
     public PredicateImpl negate() {
-        PredicateImpl not = new PredicateImpl(_op);
+        PredicateImpl not = clone();
         not._negated = true;
-        if (_exps != null)
-        	not._exps = new ArrayList<Expression<Boolean>>(this._exps);
-        not._op = this._op;
         return not;
     }
-    
+
+    public PredicateImpl clone() {
+        PredicateImpl clone = new PredicateImpl(_op);
+        if (_exps != null)
+            clone._exps = new ArrayList<Expression<Boolean>>(this._exps);
+        return clone;
+    }
+
     @Override
     org.apache.openjpa.kernel.exps.Expression toKernelExpression(
-        ExpressionFactory factory, MetamodelImpl model, CriteriaQuery q) {
-    		if (_exps == null || _exps.isEmpty())
-    			return factory.emptyExpression();
-    		if (_exps.size() == 1)
-    			return ((ExpressionImpl<?>)_exps.get(0))
-    			   .toKernelExpression(factory, model, q);
-    		ExpressionImpl<?> e1 = (ExpressionImpl<?>)_exps.get(0);
-    		ExpressionImpl<?> e2 = (ExpressionImpl<?>)_exps.get(1);
-    		org.apache.openjpa.kernel.exps.Expression ke1 = 
-    			e1.toKernelExpression(factory, model, q);
-    		org.apache.openjpa.kernel.exps.Expression ke2 = 
-    			e2.toKernelExpression(factory, model, q);
-    		org.apache.openjpa.kernel.exps.Expression result = 
-    			_op == BooleanOperator.AND 
-    			? factory.and(ke1,ke2) : factory.or(ke1, ke2);
-
-    		for (int i = 2; i < _exps.size(); i++) {
-    			ExpressionImpl<?> e = (ExpressionImpl<?>)_exps.get(i);
-    			result = _op == BooleanOperator.AND 
+            ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl q) {
+        if (_exps == null || _exps.isEmpty())
+            return factory.emptyExpression();
+        if (_exps.size() == 1)
+            return ((ExpressionImpl<?>)_exps.get(0))
+            .toKernelExpression(factory, model, q);
+        ExpressionImpl<?> e1 = (ExpressionImpl<?>)_exps.get(0);
+        ExpressionImpl<?> e2 = (ExpressionImpl<?>)_exps.get(1);
+        org.apache.openjpa.kernel.exps.Expression ke1 = 
+            e1.toKernelExpression(factory, model, q);
+        org.apache.openjpa.kernel.exps.Expression ke2 = 
+            e2.toKernelExpression(factory, model, q);
+        org.apache.openjpa.kernel.exps.Expression result = 
+            _op == BooleanOperator.AND 
+            ? factory.and(ke1,ke2) : factory.or(ke1, ke2);
+
+            for (int i = 2; i < _exps.size(); i++) {
+                ExpressionImpl<?> e = (ExpressionImpl<?>)_exps.get(i);
+                result = _op == BooleanOperator.AND 
                 ? factory.and(result, e.toKernelExpression(factory, model, q))
-    		    : factory.or(result, e.toKernelExpression(factory,model,q));
-    		}
-    		return _negated ? factory.not(result) : result;
+                        : factory.or(result, e.toKernelExpression(factory,model,q));
+            }
+            return _negated ? factory.not(result) : result;
     }
-        
+
     public static class And extends PredicateImpl {
-    	public And(Expression<Boolean> x, Expression<Boolean> y) {
-    		super(BooleanOperator.AND);
-    		add(x).add(y);
-    	}
-    	
-    	public And(Predicate...restrictions) {
-    		super(BooleanOperator.AND, restrictions);
-    	}
-   }
-    
+        public And(Expression<Boolean> x, Expression<Boolean> y) {
+            super(BooleanOperator.AND);
+            add(x).add(y);
+        }
+
+        public And(Predicate...restrictions) {
+            super(BooleanOperator.AND, restrictions);
+        }
+    }
+
     public static class Or extends PredicateImpl {
-    	public Or(Expression<Boolean> x, Expression<Boolean> y) {
-    		super(BooleanOperator.OR);
-    		add(x).add(y);
-    	}
-    	
-    	public Or(Predicate...restrictions) {
-    		super(BooleanOperator.OR, restrictions);
-    	}
+        public Or(Expression<Boolean> x, Expression<Boolean> y) {
+            super(BooleanOperator.OR);
+            add(x).add(y);
+        }
+
+        public Or(Predicate...restrictions) {
+            super(BooleanOperator.OR, restrictions);
+        }
     }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/RootImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/RootImpl.java?rev=781621&r1=781620&r2=781621&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/RootImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/RootImpl.java
Wed Jun  3 23:44:58 2009
@@ -19,13 +19,11 @@
 
 package org.apache.openjpa.persistence.criteria;
 
-import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Root;
 import javax.persistence.metamodel.Entity;
 
 import org.apache.openjpa.kernel.exps.ExpressionFactory;
 import org.apache.openjpa.kernel.exps.Value;
-import org.apache.openjpa.persistence.criteria.FromImpl;
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
 import org.apache.openjpa.persistence.meta.Types;
 
@@ -37,8 +35,9 @@
  * @param <X>
  */
 public class RootImpl<X> extends FromImpl<X,X> implements Root<X> {
-	private final Types.Entity<X> _entity;
-	
+    private final Types.Entity<X> _entity;
+    private RootImpl<X> _correlatedParent;
+        
     public RootImpl(Types.Entity<X> type) {
         super(type);
         _entity = type;
@@ -48,13 +47,28 @@
         return _entity;
     }
     
+    public void setCorrelatedParent(RootImpl<X> correlatedParent) {
+        _correlatedParent = correlatedParent;
+    }
+    
+    public RootImpl<X> getCorrelatedParent() {
+        return _correlatedParent;
+    }
+    
     /**
      * Convert this path to a kernel path value.
      */
     @Override
     public Value toValue(ExpressionFactory factory, MetamodelImpl model, 
-        CriteriaQuery c) {
-        Value var = factory.newPath();
+        CriteriaQueryImpl c) {
+        SubqueryImpl subquery = c.getContext();
+        Value var = null;
+        if (subquery != null && PathImpl.inSubquery(this, subquery)) {
+            org.apache.openjpa.kernel.exps.Subquery subQ = 
+                subquery.getSubQ();
+            var = factory.newPath(subQ);
+        } else 
+            var = factory.newPath();
         var.setMetaData(_entity.meta);
         return var;
     }
@@ -65,7 +79,7 @@
      */
     @Override
     public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
-        ExpressionFactory factory, MetamodelImpl model, CriteriaQuery c) {
+        ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl c) {
         org.apache.openjpa.kernel.exps.Value path = toValue(factory, model, c);
         
         Value var = factory.newBoundVariable(getAlias(), 
@@ -73,6 +87,14 @@
         org.apache.openjpa.kernel.exps.Expression exp = 
             factory.bindVariable(var, path);
         
-        return exp;
+        if (_correlatedParent == null) 
+            return exp;
+        org.apache.openjpa.kernel.exps.Value path1 = 
+            _correlatedParent.toValue(factory, model, c);
+        org.apache.openjpa.kernel.exps.Expression equal = 
+            factory.equal(path1, path);
+        //return factory.and(exp, equal);
+        return equal;
+        
     }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java?rev=781621&r1=781620&r2=781621&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java
Wed Jun  3 23:44:58 2009
@@ -21,13 +21,9 @@
 import java.util.Arrays;
 import java.util.List;
 
-import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Selection;
 
-import org.apache.openjpa.kernel.exps.ExpressionFactory;
-import org.apache.openjpa.kernel.exps.Value;
 import org.apache.openjpa.persistence.ResultItemImpl;
-import org.apache.openjpa.persistence.meta.MetamodelImpl;
 
 /**
  * An item selected in the projection clause of  Criteria query.
@@ -53,17 +49,4 @@
     public List<Selection<?>> getSelections() {
         return _sels;
     }
-    
-    Value toValue(ExpressionFactory factory, MetamodelImpl model,
-        CriteriaQuery q) {
-        ((CriteriaQueryImpl)q).setResultClass(getJavaType());
-        Value[] result = new Value[_sels.size()];
-        int i = 0;
-        for (Selection<?> s : _sels) {
-            result[i++] = ((ExpressionImpl<?>)s).toValue(factory, model, 
-                q);
-        }
-        ((CriteriaQueryImpl)q).setProjections(result);
-        return null;
-    }
 }

Added: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java?rev=781621&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
(added)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
Wed Jun  3 23:44:58 2009
@@ -0,0 +1,180 @@
+package org.apache.openjpa.persistence.criteria;
+
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.persistence.criteria.AbstractQuery;
+import javax.persistence.criteria.CollectionJoin;
+import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.Join;
+import javax.persistence.criteria.ListJoin;
+import javax.persistence.criteria.MapJoin;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import javax.persistence.criteria.SetJoin;
+import javax.persistence.criteria.Subquery;
+import javax.persistence.metamodel.Entity;
+
+import org.apache.openjpa.kernel.exps.ExpressionFactory;
+import org.apache.openjpa.kernel.exps.Value;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.persistence.meta.MetamodelImpl;
+import org.apache.openjpa.persistence.meta.Types;
+import org.apache.openjpa.kernel.exps.QueryExpressions;
+import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder;
+
+
+public class SubqueryImpl<T> extends ExpressionImpl<T> implements Subquery<T>
{
+    private final CriteriaQueryImpl _parent;
+    private final CriteriaQueryImpl _delegate;
+    private java.util.Set<Join<?,?>> _joins;
+    private Expression<T> _select;
+    private org.apache.openjpa.kernel.exps.Subquery _subq;
+    
+    public SubqueryImpl(Class<T> cls, CriteriaQueryImpl parent) {
+        super(cls);
+        _parent = parent;
+        _delegate = new CriteriaQueryImpl(parent.getMetamodel());
+        _delegate.setContext(this);
+    }
+    
+    public AbstractQuery getParent() {
+        return _parent;
+    }
+    
+    public Subquery<T> select(Expression<T> expression) {
+        _select = expression;
+        _delegate.select(expression);
+        return this;
+    }
+    
+    public Expression<T> getSelection() {
+        return _select;
+    }
+    
+    public <X> Root<X> from(Entity<X> entity) {
+        return _delegate.from(entity);
+    }
+
+    public <X> Root<X> from(Class<X> entityClass) {
+        return _delegate.from(entityClass);
+    }
+
+    public Set<Root<?>> getRoots() {
+        return _delegate.getRoots();
+    }
+    
+    public Root<?> getRoot() {
+        return _delegate.getRoot();
+    }    
+
+    public Subquery<T> where(Expression<Boolean> restriction) {
+        _delegate.where(restriction);
+        return this;
+    }
+
+    public Subquery<T> where(Predicate... restrictions) {
+        _delegate.where(restrictions);
+        return this;
+    }
+
+    public Subquery<T> groupBy(Expression<?>... grouping) {
+        _delegate.groupBy(grouping);
+        return this;
+    }
+
+    public Subquery<T> having(Expression<Boolean> restriction) {
+        _delegate.having(restriction);
+        return this;
+    }
+
+    public Subquery<T> having(Predicate... restrictions) {
+        _delegate.having(restrictions);
+        return this;
+    }
+
+    public Subquery<T> distinct(boolean distinct) {
+        _delegate.distinct(distinct);
+        return this;
+    }
+
+    public List<Expression<?>> getGroupList() {
+        return _delegate.getGroupList();
+    }
+
+    public Predicate getRestriction() {
+        return _delegate.getRestriction();
+    }
+
+    public Predicate getGroupRestriction() {
+        return _delegate.getGroupRestriction();
+    }
+
+    public boolean isDistinct() {
+        return _delegate.isDistinct();
+    }
+
+    public <U> Subquery<U> subquery(Class<U> type) {
+        return new SubqueryImpl<U>(type, _delegate);
+    }
+    
+    public <Y> Root<Y> correlate(Root<Y> root) {
+        Types.Entity<Y> entity = 
+            (Types.Entity<Y>)((RootImpl<Y>)root).getModel();
+        RootImpl<Y> corrRoot = new RootImpl<Y>(entity);
+        corrRoot.setCorrelatedParent((RootImpl<Y>)root);
+        Set<Root<?>> roots = getRoots();
+        if (roots == null) {
+            roots = new LinkedHashSet<Root<?>>();
+            _delegate.setRoots(roots);
+        }
+        roots.add(corrRoot);
+        return corrRoot;
+    }
+    
+    public <X,Y> Join<X,Y> correlate(Join<X,Y> join) {
+        return join;
+    }
+    public <X,Y> CollectionJoin<X,Y> correlate(CollectionJoin<X,Y> join)
{
+        return join;
+    }
+    public <X,Y> SetJoin<X,Y> correlate(SetJoin<X,Y> join) {
+        return join;
+    }
+    public <X,Y> ListJoin<X,Y> correlate(ListJoin<X,Y> join) {
+        return join;
+    }
+    public <X,K,V> MapJoin<X,K,V> correlate(MapJoin<X,K,V> join) {
+        return join;
+    }
+    
+    public java.util.Set<Join<?, ?>> getJoins() {
+        return _joins;
+    }
+    
+    public org.apache.openjpa.kernel.exps.Subquery getSubQ() {
+        return _subq;
+    }
+
+    /**
+     * Convert this path to a kernel path value.
+     */
+    @Override
+    public Value toValue(ExpressionFactory factory, MetamodelImpl model,
+        CriteriaQueryImpl q) {
+        final boolean subclasses = true;
+        CriteriaExpressionBuilder queryEval = q.getExprBuilder();
+        String alias = queryEval.nextAlias();
+        ClassMetaData candidate =  
+            ((Types.Managed<?>)getRoot().getModel()).meta;
+        _subq = factory.newSubquery(candidate, subclasses, alias);
+        _subq.setMetaData(candidate);
+        QueryExpressions subexp = q.getExprBuilder().
+            getQueryExpressions(factory, _delegate);
+        _subq.setQueryExpressions(subexp);
+        if (subexp.projections.length > 0)
+            JPQLExpressionBuilder.checkEmbeddable(subexp.projections[0], null);
+        return _subq;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message