cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From and...@apache.org
Subject svn commit: r931910 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/ejbql/parser/ test/java/org/apache/cayenne/query/
Date Thu, 08 Apr 2010 12:35:06 GMT
Author: andrey
Date: Thu Apr  8 12:35:05 2010
New Revision: 931910

URL: http://svn.apache.org/viewvc?rev=931910&view=rev
Log:
CAY-1417 EJBQL doesn't support null numeric parameters.
fix and adding logic as in CAY-1365

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java?rev=931910&r1=931909&r2=931910&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
Thu Apr  8 12:35:05 2010
@@ -43,6 +43,7 @@ import org.apache.cayenne.ejbql.parser.E
 import org.apache.cayenne.ejbql.parser.EJBQLTrimBoth;
 import org.apache.cayenne.ejbql.parser.EJBQLTrimSpecification;
 import org.apache.cayenne.ejbql.parser.Node;
+import org.apache.cayenne.ejbql.parser.SimpleNode;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbJoin;
@@ -383,26 +384,46 @@ public class EJBQLConditionTranslator ex
         visitConditional((AggregateConditionNode) expression, " OR", finishedChildIndex);
         return true;
     }
+    
+    /**
+     * Checks expression for containing null imput parameter.
+     * For that, we'll append IS NULL or IS NOT NULL instead of =null or <>null
+     * @return whether replacement was done and there's no need for normal expression processing
+     */
+    boolean checkNullParameter(EJBQLExpression expression, String toAppend) {
+        if (expression.getChildrenCount() == 2) {
+            // We rewrite expression "parameter = :x" where x=null
+            // as "parameter IS NULL"
+            // BUT in such as ":x = parameter" (where x=null) we don't do anything
+            // as a result it can be unsupported in some DB
+            if (expression.getChild(1) instanceof EJBQLNamedInputParameter) {
+                EJBQLNamedInputParameter par = (EJBQLNamedInputParameter) expression
+                        .getChild(1);
+                if (context.namedParameters.containsKey(par.getText())
+                        && context.namedParameters.get(par.getText()) == null) {
+                    context.append(toAppend);
+                    return true;
+                }
+            }
+            else if (expression.getChild(1) instanceof EJBQLPositionalInputParameter) {
+                EJBQLPositionalInputParameter par = (EJBQLPositionalInputParameter) expression
+                        .getChild(1);
+                if (context.positionalParameters.containsKey(par.getPosition())
+                        && context.positionalParameters.get(par.getPosition()) ==
null) {
+                    context.append(toAppend);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 
     @Override
     public boolean visitEquals(EJBQLExpression expression, int finishedChildIndex) {
         switch (finishedChildIndex) {
             case 0:
-                if (expression.getChildrenCount() == 2) {
-
-                    // We rewrite expression "parameter = :x" where x=null
-                    // as "parameter IS NULL"
-                    // BUT in such as ":x = parameter" (where x=null) we don't do anything
-                    // as a result it can be unsupported in some DB
-                    if (expression.getChild(1) instanceof EJBQLNamedInputParameter) {
-                        EJBQLNamedInputParameter par = (EJBQLNamedInputParameter) expression
-                                .getChild(1);
-                        if (context.namedParameters.containsKey(par.getText())
-                                && context.namedParameters.get(par.getText()) ==
null) {
-                            context.append(" IS NULL");
-                            return false;
-                        }
-                    }
+                if (checkNullParameter(expression, " IS NULL")) {
+                    return false;
                 }
                 context.append(" =");
                 break;
@@ -461,6 +482,9 @@ public class EJBQLConditionTranslator ex
     public boolean visitNotEquals(EJBQLExpression expression, int finishedChildIndex) {
         switch (finishedChildIndex) {
             case 0:
+                if (checkNullParameter(expression, " IS NOT NULL")) {
+                    return false;
+                }
                 context.append(" <>");
                 break;
             case 1:
@@ -818,7 +842,7 @@ public class EJBQLConditionTranslator ex
         else {
 
             String type = null;
-            EJBQLEquals parent = ((EJBQLNamedInputParameter) expression).getParent();
+            Node parent = ((SimpleNode) expression).jjtGetParent();
 
             context.pushMarker("@processParameter", true);
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java?rev=931910&r1=931909&r2=931910&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
Thu Apr  8 12:35:05 2010
@@ -33,8 +33,4 @@ public class EJBQLNamedInputParameter ex
     protected boolean visitNode(EJBQLExpressionVisitor visitor) {
         return visitor.visitNamedInputParameter(this);
     }
-   
-    public EJBQLEquals getParent() {
-        return (EJBQLEquals) parent;
-    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java?rev=931910&r1=931909&r2=931910&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
Thu Apr  8 12:35:05 2010
@@ -272,6 +272,18 @@ public class EJBQLQueryTest extends Caye
         createDataContext().performQuery(query);
     }
     
+    public void testNullNotEqualsParameter() {
+        EJBQLQuery query = new EJBQLQuery("select p from Painting p WHERE p.toArtist<>:x");
+        query.setParameter("x", null);
+        createDataContext().performQuery(query);
+    }
+    
+    public void testNullPositionalParameter() {
+        EJBQLQuery query = new EJBQLQuery("select p from Painting p WHERE p.toArtist=?1");
+        query.setParameter(1, null);
+        createDataContext().performQuery(query);
+    }
+    
     public void testNullAndNotNullParameter() {
         EJBQLQuery query = new EJBQLQuery("select p from Painting p WHERE p.toArtist=:x OR
p.toArtist.artistName=:b");
         query.setParameter("x", null);



Mime
View raw message