openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r940492 - in /openjpa/branches/1.3.x/openjpa-jdbc/src/main: java/org/apache/openjpa/jdbc/kernel/exps/ java/org/apache/openjpa/jdbc/sql/ resources/org/apache/openjpa/jdbc/kernel/exps/
Date Mon, 03 May 2010 16:00:05 GMT
Author: mikedd
Date: Mon May  3 16:00:04 2010
New Revision: 940492

URL: http://svn.apache.org/viewvc?rev=940492&view=rev
Log:
OPENJPA-1483: Support count distinct compound key
> Submitted By: Heath Thomann, merged from Fay Wang's changes in trunk

Modified:
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Count.java
Mon May  3 16:00:04 2010
@@ -18,6 +18,7 @@
  */
 package org.apache.openjpa.jdbc.kernel.exps;
 
+import org.apache.openjpa.jdbc.schema.Column;
 import org.apache.openjpa.jdbc.sql.SQLBuffer;
 import org.apache.openjpa.jdbc.sql.Select;
 
@@ -28,17 +29,33 @@ import org.apache.openjpa.jdbc.sql.Selec
  */
 class Count
     extends UnaryOp {
+	
+    private boolean isCountMultiColumns = false;
+    private boolean isCountDistinct = false;
 
     /**
      * Constructor. Provide the value to operate on.
      */
     public Count(Val val) {
         super(val);
+		if (val instanceof Distinct){
+			isCountDistinct = true;
+		}        
     }
 
     public ExpState initialize(Select sel, ExpContext ctx, int flags) {
         // join into related object if present
-        return initializeValue(sel, ctx, JOIN_REL);
+		ExpState expState = initializeValue(sel, ctx, JOIN_REL);
+		Val val = isCountDistinct ? ((Distinct) getValue()).getValue()
+				: getValue();
+		if (val instanceof PCPath) {
+			Column[] cols = ((PCPath) val).getColumns(expState);
+			if (cols.length > 1) {
+				isCountMultiColumns = true;
+			}
+		}
+
+		return expState;
     }
 
     protected Class getType(Class c) {
@@ -52,6 +69,10 @@ class Count
     public boolean isAggregate() {
         return true;
     }
+
+	public boolean isCountDistinctMultiCols() {
+		return isCountDistinct && isCountMultiColumns;
+	}
     
     /**
      * Overrides SQL formation by replacing COUNT(column) by COUNT(*) when specific conditions
are met and
@@ -59,13 +80,19 @@ class Count
      */
     @Override
     public void appendTo(Select sel, ExpContext ctx, ExpState state, SQLBuffer sql, int index)
{
-        super.appendTo(sel, ctx, state, sql, index);
-        if (ctx.store.getDBDictionary().useWildCardForCount && state.joins.isEmpty())
{
-            String s = sql.getSQL();
-            if (s.startsWith("COUNT(") && s.endsWith(")")) {
-                sql.replaceSqlString("COUNT(".length(), s.length()-1, "*");
-            }
-        }
+		if (isCountDistinctMultiCols()) {
+			getValue().appendTo(sel, ctx, state, sql, 0);
+			sql.addCastForParam(getOperator(), getValue());
+		} else
+			super.appendTo(sel, ctx, state, sql, index);
+		if ((ctx.store.getDBDictionary().useWildCardForCount && state.joins
+				.isEmpty())
+				|| !isCountDistinct && isCountMultiColumns) {
+			String s = sql.getSQL();
+			if (s.startsWith("COUNT(") && s.endsWith(")")) {
+				sql.replaceSqlString("COUNT(".length(), s.length() - 1, "*");
+			}
+		}
     }
 }
 

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Distinct.java
Mon May  3 16:00:04 2010
@@ -41,4 +41,22 @@ class Distinct
     protected String getOperator() {
         return "DISTINCT";
     }
+
+    @Override
+    public void appendTo(Select sel, ExpContext ctx, ExpState state, 
+        SQLBuffer sql, int index) {
+        Val val = getValue();
+        if (val instanceof PCPath) {
+            boolean noParen = getNoParen();
+            sql.append(getOperator());
+            sql.append(noParen ? " " : "(");
+            ((PCPath)val).appendTo(sel, ctx, state, sql); 
+            sql.addCastForParam(getOperator(), val);
+            if (!noParen)
+                sql.append(")");
+            
+        } else
+            super.appendTo(sel, ctx, state, sql, index);
+    }
+    
 }

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/PCPath.java
Mon May  3 16:00:04 2010
@@ -892,8 +892,22 @@ public class PCPath
     }
 
     public void appendTo(Select sel, ExpContext ctx, ExpState state, 
+        SQLBuffer sql) {
+        Column[] cols = getColumns(state);
+        for (int i = 0; i < cols.length; i++) {
+            appendTo(sel, state, sql, cols[i]);
+            if (i < cols.length -1)
+            sql.append(", ");
+        }
+    }
+    
+    public void appendTo(Select sel, ExpContext ctx, ExpState state, 
         SQLBuffer sql, int index) {
         Column col = getColumns(state)[index];
+        appendTo(sel, state, sql, col);
+    }
+    
+    public void appendTo(Select sel, ExpState state, SQLBuffer sql, Column col) {
         if (sel != null)
             sel.setSchemaAlias(_schemaAlias);
 

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/SelectConstructor.java
Mon May  3 16:00:04 2010
@@ -36,6 +36,8 @@ import org.apache.openjpa.kernel.exps.Ex
 import org.apache.openjpa.kernel.exps.QueryExpressions;
 import org.apache.openjpa.kernel.exps.Subquery;
 import org.apache.openjpa.kernel.exps.Value;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.util.UnsupportedException;
 
 /**
  * Turns parsed queries into selects.
@@ -48,6 +50,7 @@ public class SelectConstructor
 
     private boolean _extent = false;
     private Select _subselect = null;
+    private static final Localizer _loc = Localizer.forPackage(SelectConstructor.class);
 
     /**
      * Return true if we know the select to have on criteria; to be an extent.
@@ -115,6 +118,18 @@ public class SelectConstructor
         }
         for (int i = 0; i < exps.grouping.length; i++)
             ((Val) exps.grouping[i]).groupBy(sel, ctx, state.grouping[i]);
+        
+        if (exps.projections.length == 1) {
+            Val val = (Val) exps.projections[0];
+            if (val instanceof Count && ((Count)val).isCountDistinctMultiCols())
{
+                Select newSel = ctx.store.getSQLFactory().newSelect();
+                newSel.select("COUNT(*)", val);
+                newSel.setExpectedResultCount(1, true);
+                newSel.setFromSelect(sel);
+                sel.setExpectedResultCount(0, true);
+                sel = newSel;
+            }
+        }        
         return sel;
     }
 
@@ -233,6 +248,10 @@ public class SelectConstructor
                 // projections; this ensures that we have all our joins cached
                 state.projections[i] = resultVal.initialize(sel, ctx, 
                     Val.JOIN_REL | Val.FORCE_OUTER);
+                if (exps.projections.length > 1 && resultVal instanceof Count)
{
+                    if (((Count)resultVal).isCountDistinctMultiCols())
+                        throw new UnsupportedException(_loc.get("count-distinct-multi-col-only"));
+                }
                 joins = sel.and(joins, state.projections[i].joins);
             }
         }
@@ -288,6 +307,8 @@ public class SelectConstructor
         Select inner = sel.getFromSelect();
         Val val;
         Joins joins = null;
+        boolean isCountDistinctMultiCols = false;
+
         if (sel.getSubselectPath() != null)
             joins = sel.newJoins().setSubselect(sel.getSubselectPath());
 
@@ -305,9 +326,18 @@ public class SelectConstructor
             // subselect for objects; we really just need the primary key values
             sel.select(mapping.getPrimaryKeyColumns(), joins);
         } else {
+            if (exps.projections.length == 1) {
+                val = (Val) exps.projections[0];
+                if (val instanceof Count && ((Count)val).isCountDistinctMultiCols())
{
+                    isCountDistinctMultiCols = true;
+                    if (sel.getParent() != null)
+                        throw new UnsupportedException(_loc.get("count-distinct-multi-col-subselect-unsupported"));
+                }
+            }            
+
             // if we have an inner select, we need to select the candidate
             // class' pk columns to guarantee unique instances
-            if (inner != null)
+            if (inner != null && !isCountDistinctMultiCols)
                 inner.select(mapping.getPrimaryKeyColumns(), joins);
 
             // select each result value; no need to pass on the eager mode since
@@ -315,9 +345,13 @@ public class SelectConstructor
             boolean pks = sel.getParent() != null;
             for (int i = 0; i < exps.projections.length; i++) {
                 val = (Val) exps.projections[i];
-                if (inner != null)
-                    val.selectColumns(inner, ctx, state.projections[i], pks);
-                val.select(sel, ctx, state.projections[i], pks);
+                if (inner != null) {
+                    if (!isCountDistinctMultiCols)
+                        val.selectColumns(inner, ctx, state.projections[i], pks);
+                    else
+                        val.select(inner, ctx, state.projections[i], pks);
+                } else
+                    val.select(sel, ctx, state.projections[i], pks);
             }
 
             // make sure having columns are selected since it is required by 

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java
Mon May  3 16:00:04 2010
@@ -74,6 +74,10 @@ abstract class UnaryOp
     public void setImplicitType(Class type) {
         _cast = type;
     }
+    
+    public boolean getNoParen() {
+        return _noParen;
+    }
 
     public ExpState initialize(Select sel, ExpContext ctx, int flags) {
         return initializeValue(sel, ctx, flags);

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SelectImpl.java
Mon May  3 16:00:04 2010
@@ -3151,9 +3151,14 @@ public class SelectImpl
                         
                     String as = null;
                     if (inner)
-                        as = ((String) alias).replace('.', '_');
-                    else if (_selectAs != null)
-                        as = (String) _selectAs.get(id);
+						if (inner) {
+							if (alias instanceof String){
+								as = ((String) alias).replace('.', '_');
+							}
+						} 
+						else if (_selectAs != null){
+							as = (String) _selectAs.get(id);
+						}
 
                     if (as != null) {
                         if (ident && _idents != null)

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties?rev=940492&r1=940491&r2=940492&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties
(original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/exps/localizer.properties
Mon May  3 16:00:04 2010
@@ -26,4 +26,9 @@ path-only: The target for filter listene
 no-col: The column "{0}" given to filter "{1}" does not exist in the table \
 	of the specified target.
 cant-convert: Attempt to compare incompatible types "{0}" and "{1}".
-invalid-unbound-var: Invalid unbound variable "{0}" in query. 
\ No newline at end of file
+invalid-unbound-var: Invalid unbound variable "{0}" in query. 
+empty-collection-parm: Input parameter "{0}" is empty.
+count-distinct-multi-col-only: Count distinct compound primary key is not \
+    supported when there are other projection items.
+count-distinct-multi-col-subselect-unsupported: Count distinct multiple columns \
+	in the subselect is not supported.
\ No newline at end of file



Mime
View raw message