openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r776136 - in /openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria: CriteriaBuilder.java CriteriaQueryImpl.java SelectionImpl.java
Date Mon, 18 May 2009 23:41:44 GMT
Author: faywang
Date: Mon May 18 23:41:43 2009
New Revision: 776136

URL: http://svn.apache.org/viewvc?rev=776136&view=rev
Log:
OPENJPA-1013: Constructor support in Criteria Query. Test case will be checked in later.

Modified:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/SelectionImpl.java

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java?rev=776136&r1=776135&r2=776136&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaBuilder.java
Mon May 18 23:41:43 2009
@@ -540,7 +540,7 @@
 
     public <Y> Selection<Y> select(Class<Y> result, 
         Selection<?>... selections) {
-        throw new AbstractMethodError();
+        return new SelectionImpl(result).setSelections(selections);
     }
 
     public <R> Case<R> selectCase() {

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java?rev=776136&r1=776135&r2=776136&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/criteria/CriteriaQueryImpl.java
Mon May 18 23:41:43 2009
@@ -62,6 +62,9 @@
     private PredicateImpl       _having;
     private Boolean             _distinct;
     private LinkedMap           _parameterTypes;
+    private Class               _resultClass;
+    private Value[]             _projections;
+    private int                 _aliasCount = 0;
     
     public CriteriaQueryImpl(MetamodelImpl model) {
         this._model = model;
@@ -177,6 +180,14 @@
         _parameterTypes = parameterTypes;
     }
     
+    public void setResultClass(Class resultClass) {
+        _resultClass = resultClass;
+    }
+
+    public void setProjections(Value[] projections) {
+        _projections = projections;
+    }
+
     /**
      * Populate kernel expressions.
      */
@@ -206,14 +217,13 @@
 	    evalOrdering(exps, factory);
 	//    exps.operation = QueryOperations.OP_SELECT;
 	    
-	//    exps.parameterTypes = null; // LinkedMap<>
-	//    exps.projectionAliases = null; // String[]
-	//    exps.projectionClauses = null; // String[]
-	      exps.projections = toValues(factory, getSelectionList());
+	    evalProjection(exps, factory);
+	    
 	//    exps.range = null; // Value[]
 	//    exps.resultClass = null; // Class
-	      if (_parameterTypes != null)
-	          exps.parameterTypes = _parameterTypes;
+	    if (_parameterTypes != null)
+	        exps.parameterTypes = _parameterTypes;
+	    exps.resultClass = _resultClass;
 	    return exps;
     }
 
@@ -249,19 +259,31 @@
                  (ExpressionImpl<?>)groupBy, factory, _model, this);;
         }
     }
-    
-    
-    
 
-    Value[] toValues(ExpressionFactory factory, List<Selection<?>> sels) {
+    void evalProjection(QueryExpressions exps, ExpressionFactory factory) {
+        Value [] projs = toValues(exps, factory, getSelectionList());
+        if (projs.length == 1 && projs[0] == null)
+            exps.projections = _projections;
+        else 
+            exps.projections = projs;
+        //exps.projectionClauses = String[];
+    }    
+
+    Value[] toValues(QueryExpressions exps, ExpressionFactory factory, 
+        List<Selection<?>> sels) {
     	if (sels == null || (sels.size() == 1 && sels.get(0) == getRoot()))
     			return new Value[0];
     	Value[] result = new Value[sels.size()];
+    	String[] aliases = new String[sels.size()];
     	int i = 0;
     	for (Selection<?> s : sels) {
-    		result[i++] = ((ExpressionImpl<?>)s).toValue(factory, _model, 
+            result[i] = ((SelectionImpl<?>)s).toValue(factory, _model, 
     		    this);
-    	}
+            aliases[i] = nextAlias();
+            i++;
+        }
+        exps.projectionAliases = aliases;
+    	
     	return result;
     }
 
@@ -323,4 +345,9 @@
         if (_parameterTypes.containsKey(paramKey))
             _parameterTypes.put(paramKey, type);
     }
+    
+    private String nextAlias() {
+        return "jpqlalias" + (++_aliasCount);
+    }
+    
 }

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=776136&r1=776135&r2=776136&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
Mon May 18 23:41:43 2009
@@ -18,9 +18,16 @@
  */
 package org.apache.openjpa.persistence.criteria;
 
+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.
@@ -31,8 +38,32 @@
  */
 public class SelectionImpl<X> extends ResultItemImpl<X> 
     implements Selection<X> {
-
+    
+    private List<Selection<?>>  _sels;
+    
     public SelectionImpl(Class<X> cls) {
         super(cls);
     }
+    
+    public SelectionImpl setSelections(Selection<?>... selections) {
+        _sels = Arrays.asList(selections);
+        return this;
+    }
+    
+    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;
+    }
 }



Mime
View raw message