cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From and...@apache.org
Subject svn commit: r931916 - in /cayenne/main/branches/STABLE-3.0/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:49:21 GMT
Author: andrey
Date: Thu Apr  8 12:49:21 2010
New Revision: 931916

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

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

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java?rev=931916&r1=931915&r2=931916&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
(original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
Thu Apr  8 12:49:21 2010
@@ -383,26 +383,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 +481,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:

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java?rev=931916&r1=931915&r2=931916&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
(original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLNamedInputParameter.java
Thu Apr  8 12:49:21 2010
@@ -34,7 +34,11 @@ public class EJBQLNamedInputParameter ex
         return visitor.visitNamedInputParameter(this);
     }
    
-    public EJBQLEquals getParent() {
+    /**
+     * @deprecated since 3.0.1 this method is no longer used
+     */
+    @Deprecated
+	public EJBQLEquals getParent() {
         return (EJBQLEquals) parent;
     }
 }

Modified: cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java?rev=931916&r1=931915&r2=931916&view=diff
==============================================================================
--- cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
(original)
+++ cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/EJBQLQueryTest.java
Thu Apr  8 12:49:21 2010
@@ -273,6 +273,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