cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r471539 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/exp/ main/java/org/apache/cayenne/exp/parser/ test/java/org/apache/cayenne/exp/ test/java/org/apache/cayenne/query/
Date Sun, 05 Nov 2006 21:47:39 GMT
Author: torehalset
Date: Sun Nov  5 13:47:38 2006
New Revision: 471539

URL: http://svn.apache.org/viewvc?view=rev&rev=471539
Log:
CAY-700: where in () with empty collection
implemented for ExpressionFactory.inExp & co and for 
Query.queryWithParameters

Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTIn.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTNotIn.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTst.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/query/SelectQueryTst.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java?view=diff&rev=471539&r1=471538&r2=471539
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/ExpressionFactory.java
Sun Nov  5 13:47:38 2006
@@ -364,65 +364,97 @@
     }
 
     /**
-     * A convenience shortcut for building IN expression.
+     * A convenience shortcut for building IN expression. Return ASTFalse for empty
+     * collection.
      */
     public static Expression inExp(String pathSpec, Object[] values) {
+        if (values.length == 0) {
+            return new ASTFalse();
+        }
         return new ASTIn(new ASTObjPath(pathSpec), new ASTList(values));
     }
 
     /**
-     * A convenience shortcut for building IN DB expression.
+     * A convenience shortcut for building IN DB expression. Return ASTFalse for empty
+     * collection.
      */
     public static Expression inDbExp(String pathSpec, Object[] values) {
+        if (values.length == 0) {
+            return new ASTFalse();
+        }
         return new ASTIn(new ASTDbPath(pathSpec), new ASTList(values));
     }
 
     /**
-     * A convenience shortcut for building IN expression.
+     * A convenience shortcut for building IN expression. Return ASTFalse for empty
+     * collection.
      */
     public static Expression inExp(String pathSpec, Collection values) {
+        if (values.isEmpty()) {
+            return new ASTFalse();
+        }
         return new ASTIn(new ASTObjPath(pathSpec), new ASTList(values));
     }
 
     /**
-     * A convenience shortcut for building IN DB expression.
+     * A convenience shortcut for building IN DB expression. Return ASTFalse for empty
+     * collection.
      */
     public static Expression inDbExp(String pathSpec, Collection values) {
+        if (values.isEmpty()) {
+            return new ASTFalse();
+        }
         return new ASTIn(new ASTDbPath(pathSpec), new ASTList(values));
     }
 
     /**
-     * A convenience shortcut for building NOT_IN expression.
+     * A convenience shortcut for building NOT_IN expression. Return ASTTrue for empty
+     * collection.
      */
     public static Expression notInExp(String pathSpec, Collection values) {
+        if (values.isEmpty()) {
+            return new ASTTrue();
+        }
         return new ASTNotIn(new ASTObjPath(pathSpec), new ASTList(values));
     }
     
     /**
-     * A convenience shortcut for building NOT_IN expression.
+     * A convenience shortcut for building NOT_IN expression. Return ASTTrue for empty
+     * collection.
      * 
      * @since 3.0
      */
     public static Expression notInDbExp(String pathSpec, Collection values) {
+        if (values.isEmpty()) {
+            return new ASTTrue();
+        }
         return new ASTNotIn(new ASTDbPath(pathSpec), new ASTList(values));
     }
 
 
     /**
-     * A convenience shortcut for building NOT_IN expression.
+     * A convenience shortcut for building NOT_IN expression. Return ASTTrue for empty
+     * collection.
      * 
      * @since 1.0.6
      */
     public static Expression notInExp(String pathSpec, Object[] values) {
+        if (values.length == 0) {
+            return new ASTTrue();
+        }
         return new ASTNotIn(new ASTObjPath(pathSpec), new ASTList(values));
     }
     
     /**
-     * A convenience shortcut for building NOT_IN expression.
+     * A convenience shortcut for building NOT_IN expression. Return ASTTrue for empty
+     * collection.
      * 
      * @since 3.0
      */
     public static Expression notInDbExp(String pathSpec, Object[] values) {
+        if (values.length == 0) {
+            return new ASTTrue();
+        }
         return new ASTNotIn(new ASTDbPath(pathSpec), new ASTList(values));
     }
 

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTIn.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTIn.java?view=diff&rev=471539&r1=471538&r2=471539
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTIn.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTIn.java
Sun Nov  5 13:47:38 2006
@@ -20,6 +20,7 @@
 package org.apache.cayenne.exp.parser;
 
 import org.apache.cayenne.exp.Expression;
+import org.apache.commons.collections.Transformer;
 
 /**
  * "In" expression.
@@ -85,4 +86,23 @@
     public int getType() {
         return Expression.IN;
     }
+
+    protected Object transformExpression(Transformer transformer) {
+        Object transformed = super.transformExpression(transformer);
+        
+        // transform empty ASTIn to ASTFalse
+        if (transformed instanceof ASTIn) {
+            ASTIn exp = (ASTIn) transformed;
+            if (exp.jjtGetNumChildren() == 2) {
+                ASTList list = (ASTList) exp.jjtGetChild(1);
+                Object[] objects = (Object[]) list.evaluate(null);
+                if (objects.length == 0) {
+                    transformed = new ASTFalse();
+                }
+            }
+        }
+
+        return transformed;
+    }
+
 }

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTNotIn.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTNotIn.java?view=diff&rev=471539&r1=471538&r2=471539
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTNotIn.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/exp/parser/ASTNotIn.java
Sun Nov  5 13:47:38 2006
@@ -21,6 +21,7 @@
 package org.apache.cayenne.exp.parser;
 
 import org.apache.cayenne.exp.Expression;
+import org.apache.commons.collections.Transformer;
 
 /**
  * "Not In" expression.
@@ -82,4 +83,23 @@
     public int getType() {
         return Expression.NOT_IN;
     }
+    
+    protected Object transformExpression(Transformer transformer) {
+        Object transformed = super.transformExpression(transformer);
+        
+        // transform empty ASTNotIn to ASTTrue
+        if (transformed instanceof ASTNotIn) {
+            ASTNotIn exp = (ASTNotIn) transformed;
+            if (exp.jjtGetNumChildren() == 2) {
+                ASTList list = (ASTList) exp.jjtGetChild(1);
+                Object[] objects = (Object[]) list.evaluate(null);
+                if (objects.length == 0) {
+                    transformed = new ASTTrue();
+                }
+            }
+        }
+
+        return transformed;
+    }
+
 }

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTst.java?view=diff&rev=471539&r1=471538&r2=471539
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/exp/ExpressionFactoryTst.java
Sun Nov  5 13:47:38 2006
@@ -152,10 +152,18 @@
 
     public void testInExp2() throws Exception {
         List v = new ArrayList();
+        v.add("a");
+        v.add("b");
         Expression exp = ExpressionFactory.inExp("abc", v);
         assertEquals(Expression.IN, exp.getType());
     }
 
+    public void testInExp3() throws Exception {
+        List v = new ArrayList();
+        Expression exp = ExpressionFactory.inExp("abc", v);
+        assertEquals(Expression.FALSE, exp.getType());
+    }
+    
     public void testLikeExp() throws Exception {
         String v = "abc";
         Expression exp = ExpressionFactory.likeExp("abc", v);

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/query/SelectQueryTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/query/SelectQueryTst.java?view=diff&rev=471539&r1=471538&r2=471539
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/query/SelectQueryTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/query/SelectQueryTst.java
Sun Nov  5 13:47:38 2006
@@ -218,6 +218,56 @@
         assertEquals(2, objects.size());
     }
 
+    public void testSelectParameterizedEmptyIn() throws Exception {
+        query.setRoot(Artist.class);
+        Expression qual = Expression.fromString("artistName in $list");
+        query.setQualifier(qual);
+        query = query.queryWithParameters(Collections.singletonMap(
+                "list",
+                new Object[] {}));
+        performQuery();
+
+        // check query results
+        List objects = opObserver.rowsForQuery(query);
+        assertEquals(0, objects.size());
+    }
+    
+    public void testSelectParameterizedEmptyNotIn() throws Exception {
+        query.setRoot(Artist.class);
+        Expression qual = Expression.fromString("artistName not in $list");
+        query.setQualifier(qual);
+        query = query.queryWithParameters(Collections.singletonMap(
+                "list",
+                new Object[] {}));
+        performQuery();
+
+        // check query results
+        List objects = opObserver.rowsForQuery(query);
+        assertEquals(20, objects.size());
+    }
+    
+    public void testSelectEmptyIn() throws Exception {
+        query.setRoot(Artist.class);
+        Expression qual = ExpressionFactory.inExp("artistName", new Object[]{});
+        query.setQualifier(qual);
+        performQuery();
+
+        // check query results
+        List objects = opObserver.rowsForQuery(query);
+        assertEquals(0, objects.size());
+    }
+    
+    public void testSelectEmptyNotIn() throws Exception {
+        query.setRoot(Artist.class);
+        Expression qual = ExpressionFactory.notInExp("artistName", new Object[]{});
+        query.setQualifier(qual);
+        performQuery();
+
+        // check query results
+        List objects = opObserver.rowsForQuery(query);
+        assertEquals(20, objects.size());
+    }
+    
     public void testSelectCustAttributes() throws Exception {
         query.setRoot(Artist.class);
         query.addCustomDbAttribute("ARTIST_NAME");



Mime
View raw message