openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r810212 [2/2] - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/
Date Tue, 01 Sep 2009 20:16:21 GMT
Added: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OpenJPACriteriaQuery.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OpenJPACriteriaQuery.java?rev=810212&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OpenJPACriteriaQuery.java
(added)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OpenJPACriteriaQuery.java
Tue Sep  1 20:16:19 2009
@@ -0,0 +1,21 @@
+package org.apache.openjpa.persistence.criteria;
+
+import javax.persistence.criteria.CriteriaQuery;
+
+/**
+ * OpenJPA-specific extension to JPA 2.0 Criteria Query API.
+ * 
+ * @param <T> type of result returned by this query
+ * 
+ * @author Pinaki Poddar
+ * @since 2.0.0
+ */
+public interface OpenJPACriteriaQuery<T> extends CriteriaQuery<T> {
+    /**
+     * Convert the query to a JPQL-like string.
+     * The conversion of Criteria Query may not be an exact JPQL string.
+     *  
+     * @return a JPQL-like string.
+     */
+    public String toCQL();
+}

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

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OrderImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OrderImpl.java?rev=810212&r1=810211&r2=810212&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OrderImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/OrderImpl.java
Tue Sep  1 20:16:19 2009
@@ -27,7 +27,7 @@
  * @author Pinaki Poddar
  *
  */
-public class OrderImpl implements Order {
+public class OrderImpl implements Order, CriteriaExpression {
 	private boolean _ascending;
 	private final ExpressionImpl<?> e;
 	
@@ -52,4 +52,19 @@
 		_ascending = !_ascending;
 		return this;
 	}
+	
+    public void acceptVisit(CriteriaExpressionVisitor visitor) {
+        if (!visitor.isVisited(this)) {
+            visitor.enter(this);
+            visitor.exit(this);
+        }
+    }
+    
+    public StringBuilder asValue(CriteriaQueryImpl<?> q) {
+        return e.asValue(q).append(_ascending ? "" : " DESC");
+    }
+    
+    public StringBuilder asVariable(CriteriaQueryImpl<?> q) {
+        throw new IllegalStateException(this + " can not be rendered as variable");
+    }
 }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java?rev=810212&r1=810211&r2=810212&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/PathImpl.java
Tue Sep  1 20:16:19 2009
@@ -46,7 +46,7 @@
 /**
  * Path is an expression often representing a persistent attribute traversed from another
(parent) path.
  * The type of the path is the type of the persistent attribute.
- * If the persistent attribute is bindable, then further path can be travesered from this
path. 
+ * If the persistent attribute is bindable, then further path can be traversed from this
path. 
  * 
  * @author Pinaki Poddar
  * @author Fay Wang
@@ -58,7 +58,7 @@
     protected final PathImpl<?,Z> _parent;
     protected final Members.Member<? super Z,?> _member;
     private boolean isEmbedded = false;
-    protected PathImpl<?,?> _correlatedPath;
+    private PathImpl<?,?> _correlatedPath;
     
     /**
      * Protected constructor use by root path which neither represent a member nor has a
parent. 
@@ -100,7 +100,7 @@
     /**
      *  Return the parent "node" in the path or null if no parent.
      */
-    public Path<Z> getParentPath() {
+    public final Path<Z> getParentPath() {
         return _parent;
     }
     
@@ -126,21 +126,34 @@
         return member != null ? member : getInnermostMember(parent._parent,  parent._member);

     }
     
+    /**
+     * Makes this path correlated to the given path.  
+     */
     public void setCorrelatedPath(PathImpl<?,?> correlatedPath) {
         _correlatedPath = correlatedPath;
     }
     
+    /**
+     * Gets the path correlated to this path, if any.
+     */
     public PathImpl<?,?> getCorrelatedPath() {
         return _correlatedPath;
     }
     
     /**
+     * Affirms if this path is correlated to another path.
+     */
+    public boolean isCorrelated() {
+        return _correlatedPath != null;
+    }
+    
+    /**
      * Convert this path to a kernel path.
      */
     @Override
     public Value toValue(ExpressionFactory factory, MetamodelImpl model,  CriteriaQueryImpl<?>
q) {
         if (q.isRegistered(this))
-            return q.getValue(this);
+            return q.getRegisteredValue(this);
         org.apache.openjpa.kernel.exps.Path path = null;
         SubqueryImpl<?> subquery = q.getDelegator();
         boolean allowNull = _parent == null ? false : _parent instanceof Join 
@@ -265,4 +278,21 @@
     public Expression<Class<? extends X>> type() {
         return new Expressions.Type<Class<? extends X>>(this);
     }
+    
+    public StringBuilder asValue(CriteriaQueryImpl<?> q) {
+        StringBuilder buffer = new StringBuilder();
+        if (_parent != null) {
+            Value var = q.getRegisteredVariable(_parent);
+            buffer.append(var != null ? var.getName() : _parent.asValue(q)).append(".");
+        }
+        if (_member != null) {
+            buffer.append(_member.fmd.getName());
+        } 
+        return buffer;
+    }
+    
+    public StringBuilder asVariable(CriteriaQueryImpl<?> q) {
+        Value var = q.getRegisteredVariable(this);
+        return asValue(q).append(" ").append(var == null ? "?" : var.getName());
+    }
 }

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=810212&r1=810211&r2=810212&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
Tue Sep  1 20:16:19 2009
@@ -30,23 +30,32 @@
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
 
 public class PredicateImpl extends ExpressionImpl<Boolean> implements Predicate {
-    private static final ExpressionImpl<Integer> ONE = new Expressions.Constant<Integer>(1);
+    private static final ExpressionImpl<Integer> ONE  = new Expressions.Constant<Integer>(1);
     public static final ExpressionImpl<Boolean> TRUE  = new Expressions.Equal(ONE,ONE);
     public static final ExpressionImpl<Boolean> FALSE = new Expressions.Equal(ONE,ONE).negate();
     
     List<Expression<Boolean>> _exps;
-    BooleanOperator _op = BooleanOperator.AND;
-    boolean _negated = false;
+    protected final BooleanOperator _op;
+    private boolean _negated = false;
 
+    /**
+     * A predicate with empty name and AND operator.
+     */
     protected PredicateImpl() {
-        super(Boolean.class);
+        this(BooleanOperator.AND);
     }
-
+    
+    /**
+     * A predicate with given name and given operator.
+     */
     protected PredicateImpl(BooleanOperator op) {
-        this();
+        super(Boolean.class);
         _op = op;
     }
 
+    /**
+     * A predicate with given name, given operator with given arguments.
+     */
     protected PredicateImpl(BooleanOperator op, Predicate...restrictions) {
         this(op);
         if (restrictions != null) {
@@ -55,6 +64,9 @@
         }
     }
 
+    /**
+     * Adds the given predicate expression.
+     */
     public PredicateImpl add(Expression<Boolean> s) {
         if (_exps == null)
             _exps = new ArrayList<Expression<Boolean>>();
@@ -63,14 +75,14 @@
     }
 
     public List<Expression<Boolean>> getExpressions() {
-        return _exps == null ? Collections.EMPTY_LIST : new CopyOnWriteArrayList<Expression<Boolean>>(_exps);
+        return Expressions.returnCopy(_exps);
     }
 
-    public BooleanOperator getOperator() {
+    public final BooleanOperator getOperator() {
         return _op;
     }
 
-    public boolean isNegated() {
+    public final boolean isNegated() {
         return _negated;
     }
 
@@ -125,12 +137,14 @@
     }
 
     public void acceptVisit(CriteriaExpressionVisitor visitor) {
-        if (_exps == null)
-            return;
-        for (Expression<?> e : _exps) {
-            ((ExpressionImpl<?>)e).acceptVisit(visitor);
-        }
+        Expressions.acceptVisit(visitor, this, _exps == null ? null : _exps.toArray(new Expression<?>[_exps.size()]));
+    }
+    
+    public StringBuilder asValue(CriteriaQueryImpl<?> q) {
+        return Expressions.asValue(q, _exps == null ? null : _exps.toArray(new Expression<?>[_exps.size()]),

+            " " +_op + " ");
     }
+
     
     public static class And extends PredicateImpl {
         public And(Expression<Boolean> x, Expression<Boolean> y) {

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=810212&r1=810211&r2=810212&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
Tue Sep  1 20:16:19 2009
@@ -50,13 +50,11 @@
         return _entity;
     }
 
-    public void addToContext(ExpressionFactory factory, MetamodelImpl model,
-            CriteriaQueryImpl<?> q) {
+    public void addToContext(ExpressionFactory factory, MetamodelImpl model, CriteriaQueryImpl<?>
q) {
         String alias = q.getAlias(this);
-        Value var = factory.newBoundVariable(alias, 
-            AbstractExpressionBuilder.TYPE_OBJECT);
+        Value var = factory.newBoundVariable(alias, AbstractExpressionBuilder.TYPE_OBJECT);
         var.setMetaData(_entity.meta);
-        Context currContext = (Context) q.getContexts().peek();
+        Context currContext = q.ctx();
         currContext.addSchema(alias, _entity.meta);
         currContext.addVariable(alias, var);
         if (currContext.schemaAlias == null)
@@ -101,7 +99,20 @@
         return factory.bindVariable(var, path);
     }
     
-    public String toString() {
-        return _entity.toString();
+    public StringBuilder asValue(CriteriaQueryImpl<?> q) {
+        Value v = q.getRegisteredRootVariable(this);
+        if (v != null)
+            return new StringBuilder(v.getAlias());
+        v = q.getRegisteredValue(this);
+        if (v != null)
+            return new StringBuilder(v.getAlias());
+        if (q.isRegistered(this)) 
+            return new StringBuilder(q.getRegisteredValue(this).getName());
+        return new StringBuilder().append(Character.toLowerCase(_entity.getName().charAt(0)));
     }
+    
+    public StringBuilder asVariable(CriteriaQueryImpl<?> q) {
+        return new StringBuilder(_entity.getName()).append(" ").append(asValue(q));
+    }
+
 }

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=810212&r1=810211&r2=810212&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
Tue Sep  1 20:16:19 2009
@@ -20,6 +20,7 @@
 
 import java.util.List;
 
+import javax.persistence.criteria.Expression;
 import javax.persistence.criteria.Selection;
 
 import org.apache.openjpa.persistence.TupleElementImpl;
@@ -32,7 +33,7 @@
  * @param <X>
  */
 public class SelectionImpl<X> extends TupleElementImpl<X> 
-    implements Selection<X> {
+    implements Selection<X>, CriteriaExpression {
     
     public SelectionImpl(Class<X> cls) {
         super(cls);
@@ -50,4 +51,17 @@
     public boolean isCompoundSelection() {
         return false;
     }
+    
+    public StringBuilder asValue(CriteriaQueryImpl<?> q) {
+        throw new IllegalStateException(this.getClass().getSimpleName() + " can not be rendered
as value");
+    }
+    
+    public StringBuilder asVariable(CriteriaQueryImpl<?> q) {
+        throw new IllegalStateException(this.getClass().getSimpleName() + " can not be rendered
as variable");
+    }
+    
+    public void acceptVisit(CriteriaExpressionVisitor visitor) {
+        Expressions.acceptVisit(visitor, this, (Expression<?>[])null);
+    }
+
 }

Modified: 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=810212&r1=810211&r2=810212&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SubqueryImpl.java
Tue Sep  1 20:16:19 2009
@@ -19,7 +19,6 @@
 package org.apache.openjpa.persistence.criteria;
 
 import java.util.ArrayList;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.Stack;
@@ -43,7 +42,8 @@
 import org.apache.openjpa.kernel.exps.Value;
 import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder;
 import org.apache.openjpa.meta.ClassMetaData;
-import org.apache.openjpa.meta.JavaTypes;
+import org.apache.openjpa.meta.FieldMetaData;
+import org.apache.openjpa.meta.ValueMetaData;
 import org.apache.openjpa.persistence.meta.AbstractManagedType;
 import org.apache.openjpa.persistence.meta.Members;
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
@@ -51,7 +51,8 @@
 
 /**
  * Subquery is an expression which itself is a query and always appears in the
- * context of a parent query.
+ * context of a parent query. A subquery delegates to a captive query for most
+ * of the operations but also maintains its own joins and correlated joins.
  * 
  * @author Pinaki Poddar
  * @author Fay Wang
@@ -63,11 +64,16 @@
     private final CriteriaQueryImpl<T> _delegate;
     private final MetamodelImpl  _model;
     private java.util.Set<Join<?,?>> _joins;
-    private Expression<T> _select;
     private org.apache.openjpa.kernel.exps.Subquery _subq;
     private List<Join<?,?>> _corrJoins = null;
     
-    public SubqueryImpl(Class<T> cls, AbstractQuery<?> parent) {
+    /**
+     * Construct a subquery always in the context of a parent query.
+     * 
+     * @param cls the result type of this subquery
+     * @param parent the non-null parent query which itself can be a subquery.
+     */
+    SubqueryImpl(Class<T> cls, AbstractQuery<?> parent) {
         super(cls);
         _parent = parent;
         if (parent instanceof CriteriaQueryImpl) {
@@ -80,11 +86,18 @@
         _delegate = new CriteriaQueryImpl<T>(_model, this);
     }
     
+    /**
+     * Gets the parent query of this subquery.
+     * Can be a query or another subquery.
+     */
     public AbstractQuery<?> getParent() {
         return _parent;
     }
     
-    public CriteriaQueryImpl<T> getDelegate() {
+    /**
+     * Gets the captive query to which this subquery delegates.
+     */
+    CriteriaQueryImpl<T> getDelegate() {
         return _delegate;
     }
     
@@ -92,23 +105,25 @@
         return _model;
     }
     
-    public Stack<Context> getContexts() {
+    Stack<Context> getContexts() {
         return getInnermostParent().getContexts();
     }
     
+    /**
+     * Gets the 'root' query for this subquery.
+     */
     public CriteriaQueryImpl<?> getInnermostParent() {
         return (CriteriaQueryImpl<?>)(((_parent instanceof CriteriaQueryImpl)) ? 
             _parent : ((SubqueryImpl<?>)_parent).getInnermostParent());
     }
 
     public Subquery<T> select(Expression<T> expression) {
-        _select = expression;
         _delegate.select(expression);
         return this;
     }
     
     public Expression<T> getSelection() {
-        return _select;
+        return (Expression<T>)_delegate.getSelection();
     }
     
     public <X> Root<X> from(EntityType<X> entity) {
@@ -124,7 +139,7 @@
     }
     
     public Root<?> getRoot() {
-        return _delegate.getRoot();
+        return _delegate.getRoot(false);
     }    
 
     public Subquery<T> where(Expression<Boolean> restriction) {
@@ -174,9 +189,12 @@
     }
 
     public <U> Subquery<U> subquery(Class<U> type) {
-        return new SubqueryImpl<U>(type, _delegate);
+        return new SubqueryImpl<U>(type, this);
     }
     
+    /**
+     * Correlate this subquery with the given root.
+     */
     public <Y> Root<Y> correlate(Root<Y> root) {
         Types.Entity<Y> entity = (Types.Entity<Y>)root.getModel();
         RootImpl<Y> corrRoot = new RootImpl<Y>(entity);
@@ -189,13 +207,16 @@
         return _corrJoins;
     }
     
+    /**
+     * Correlate this subquery with the given join.
+     */
     public <X,Y> Join<X,Y> correlate(Join<X,Y> parentJoin) {
-        Join corrJoin = clone(parentJoin);
+        Join<?,?> corrJoin = clone(parentJoin);
         ((PathImpl<?,?>)corrJoin).setCorrelatedPath((PathImpl<?,?>)parentJoin);
         if (_corrJoins == null)
             _corrJoins = new ArrayList<Join<?,?>>();
         _corrJoins.add(corrJoin);
-        return corrJoin;
+        return (Join<X,Y>)corrJoin;
     }
     
     private Join<?,?> clone(Join<?,?> join) {
@@ -211,6 +232,13 @@
         return join1;
     }
     
+    /**
+     * Affirms if this is a correlated subquery.
+     */
+    public boolean isCorrelated() {
+        return _corrJoins != null;
+    }
+    
     private Join<?,?> makeJoin(FromImpl<?,?> parent, Members.SingularAttributeImpl<?,?>
member, JoinType jt) {
         return new Joins.SingularJoin(parent, member, jt);
     }
@@ -289,7 +317,7 @@
     // correlated parent, the candidate of the subquery
     // should be the class metadata of the collection element 
     private ClassMetaData getCandidate() {
-        if (_delegate.getRoots() == null && _corrJoins != null) {
+        if (getRoots().isEmpty() && _corrJoins != null) {
             FromImpl<?,?> corrJoin = (FromImpl<?,?>) _corrJoins.get(0);
             if (corrJoin.getJoins() != null) {
                 FromImpl<?,?> join = (FromImpl<?,?>)corrJoin.getJoins().iterator().next();
@@ -298,7 +326,7 @@
         }
          
         RootImpl<?> root = (RootImpl<?>)getRoot();
-        if (root.getCorrelatedPath() != null && root.getJoins() != null) {
+        if (root != null && root.getCorrelatedPath() != null && !root.getJoins().isEmpty())
{
             FromImpl<?,?> join = (FromImpl<?,?>) root.getJoins().iterator().next();
             return getInnermostCandidate(join);
         }
@@ -307,7 +335,7 @@
     }
     
     private ClassMetaData getInnermostCandidate(FromImpl<?,?> from) {
-        if (from.getJoins() != null) {
+        if (!from.getJoins().isEmpty()) {
             from = (FromImpl<?,?>) from.getJoins().iterator().next();
             return getInnermostCandidate(from);
         }
@@ -316,16 +344,43 @@
     
     
     private ClassMetaData getCandidate(FromImpl<?,?> from) {
-        if (from._member.fmd.getDeclaredTypeCode() == JavaTypes.COLLECTION || 
-            from._member.fmd.getDeclaredTypeCode() == JavaTypes.MAP)
-            return from._member.fmd.isElementCollection()
-                ? from._member.fmd.getElement().getEmbeddedMetaData()
-                : from._member.fmd.getElement().getDeclaredTypeMetaData();
-        return from._member.fmd.getDeclaredTypeMetaData();
-        
+        return getFieldType(from._member.fmd);
     }
     
+    private static ClassMetaData getFieldType(FieldMetaData fmd) {
+        if (fmd == null)
+            return null;
+
+        ClassMetaData cmd = null;
+        ValueMetaData vmd;
+
+        if ((vmd = fmd.getElement()) != null)
+            cmd = vmd.getDeclaredTypeMetaData();
+        else if ((vmd = fmd.getKey()) != null)
+            cmd = vmd.getDeclaredTypeMetaData();
+        else if ((vmd = fmd.getValue()) != null)
+            cmd = vmd.getDeclaredTypeMetaData();
+
+        if (cmd == null || cmd.getDescribedType() == Object.class)
+            cmd = fmd.getDeclaredTypeMetaData();
+        if (cmd == null && fmd.isElementCollection())
+            cmd = fmd.getDefiningMetaData();
+
+        return cmd;
+    }
+
+    
     public Class<T> getResultType() {
         return getJavaType();
     }
+    
+    public StringBuilder asValue(CriteriaQueryImpl<?> q) {
+        StringBuilder buffer = new StringBuilder();
+        _delegate.render(buffer, _delegate.getRoots(), _corrJoins);
+        return buffer;
+    }
+    
+    public StringBuilder asVariable(CriteriaQueryImpl<?> q) {
+        return asValue(q);
+    }
 }



Mime
View raw message