cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] branch master updated: CAY-2575 Select translator: Wrong translation of IN Expression
Date Thu, 02 May 2019 13:42:30 GMT
This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new 90c19b0  CAY-2575 Select translator: Wrong translation of IN Expression
90c19b0 is described below

commit 90c19b0c18614346eb110265da2780caaa84dc09
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Thu May 2 16:42:16 2019 +0300

    CAY-2575 Select translator: Wrong translation of IN Expression
---
 RELEASE-NOTES.txt                                  |  1 +
 .../translator/select/QualifierTranslator.java     | 22 ++++++++++++++++++++--
 .../translator/select/QualifierTranslatorTest.java | 22 ++++++++++++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index bf53690..586d743 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -51,6 +51,7 @@ CAY-2553 Wrong disjoint prefetch query qualifier
 CAY-2559 Modeler: Warning dialog shows wrong information after changing target entity in
dbRelationship
 CAY-2561 Modeler: cgen type combobox doesn't set templates
 CAY-2572 Queries are not sorted by name in data map XML
+CAY-2575 Select translator: Wrong translation of IN Expression
 
 ----------------------------------
 Release: 4.1.B1
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/QualifierTranslator.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/QualifierTranslator.java
index d48a53f..206996b 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/QualifierTranslator.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/translator/select/QualifierTranslator.java
@@ -297,13 +297,31 @@ class QualifierTranslator implements TraversalHandler {
         return expressionNodeBuilder.build();
     }
 
+    private boolean nodeProcessed(Expression node) {
+        // must be in sync with expressionNodeToSqlNode() method
+        switch (node.getType()) {
+            case NOT_IN: case IN: case NOT_BETWEEN: case BETWEEN: case NOT:
+            case BITWISE_NOT: case EQUAL_TO: case NOT_EQUAL_TO: case LIKE: case NOT_LIKE:
+            case LIKE_IGNORE_CASE: case NOT_LIKE_IGNORE_CASE: case OBJ_PATH: case DB_PATH:
+            case FUNCTION_CALL: case ADD: case SUBTRACT: case MULTIPLY: case DIVIDE: case
NEGATIVE:
+            case BITWISE_AND: case BITWISE_LEFT_SHIFT: case BITWISE_OR: case BITWISE_RIGHT_SHIFT:
case BITWISE_XOR:
+            case OR: case AND: case LESS_THAN: case LESS_THAN_EQUAL_TO: case GREATER_THAN:
case GREATER_THAN_EQUAL_TO:
+            case TRUE: case FALSE: case ASTERISK: case EXISTS: case SUBQUERY: case ENCLOSING_OBJECT:
case FULL_OBJECT:
+                return true;
+        }
+        return false;
+    }
+
     @Override
     public void endNode(Expression node, Expression parentNode) {
         if(expressionsToSkip.contains(node) || expressionsToSkip.contains(parentNode)) {
             return;
         }
-        if(currentNode.getParent() != null) {
-            currentNode = currentNode.getParent();
+
+        if(nodeProcessed(node)) {
+            if (currentNode.getParent() != null) {
+                currentNode = currentNode.getParent();
+            }
         }
     }
 
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/QualifierTranslatorTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/QualifierTranslatorTest.java
index 45116cf..be802c4 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/QualifierTranslatorTest.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/translator/select/QualifierTranslatorTest.java
@@ -22,6 +22,8 @@ package org.apache.cayenne.access.translator.select;
 import java.util.Arrays;
 
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.access.sqlbuilder.SQLGenerationVisitor;
+import org.apache.cayenne.access.sqlbuilder.StringBuilderAppendable;
 import org.apache.cayenne.access.sqlbuilder.sqltree.BetweenNode;
 import org.apache.cayenne.access.sqlbuilder.sqltree.BitwiseNotNode;
 import org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode;
@@ -80,6 +82,12 @@ public class QualifierTranslatorTest {
         attribute.setName("a");
         attribute.setDbAttributePath("a");
         entity.addAttribute(attribute);
+
+        ObjAttribute attribute2 = new ObjAttribute();
+        attribute2.setName("b");
+        attribute2.setDbAttributePath("b");
+        entity.addAttribute(attribute2);
+
         entity.setDbEntity(dbEntity);
 
         DataMap dataMap = new DataMap();
@@ -451,6 +459,20 @@ public class QualifierTranslatorTest {
     }
 
     @Test
+    public void translateComplexAnd() {
+        Node and = translate("a < 2 and b in (5,6) and b = 7");
+        assertNotNull(and);
+
+        assertThat(and, instanceOf(OpExpressionNode.class));
+        assertEquals("AND", ((OpExpressionNode)and).getOp());
+        assertEquals(3, and.getChildrenCount());
+
+        SQLGenerationVisitor visitor = new SQLGenerationVisitor(new StringBuilderAppendable());
+        and.visit(visitor);
+        assertEquals(" ( t0.a < 2 ) AND t0.b IN ( 5, 6) AND ( t0.b = 7 )", visitor.getSQLString());
+    }
+
+    @Test
     public void translateOr() {
         Node or = translate("true or false");
         assertNotNull(or);


Mime
View raw message