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: Fix translation of NULL values
Date Tue, 14 May 2019 09:53:38 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 b0d2339  Fix translation of NULL values
b0d2339 is described below

commit b0d23395aaaac413b5864031c546480cdbda5e32
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Tue May 14 12:32:21 2019 +0300

    Fix translation of NULL values
---
 .../access/sqlbuilder/sqltree/EqualNode.java        |  2 +-
 .../access/sqlbuilder/sqltree/NotEqualNode.java     |  2 +-
 .../access/sqlbuilder/sqltree/ValueNode.java        |  5 +++++
 .../translator/select/QualifierTranslatorTest.java  | 11 +++++++++++
 .../java/org/apache/cayenne/exp/ExpressionIT.java   | 21 +++++++++++++++++++++
 5 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/EqualNode.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/EqualNode.java
index b158c63..c2444b1 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/EqualNode.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/EqualNode.java
@@ -34,7 +34,7 @@ public class EqualNode extends ExpressionNode {
     public void appendChildrenSeparator(QuotingAppendable buffer, int childIdx) {
         Node child = getChild(1);
         if (child.getType() == NodeType.VALUE && ((ValueNode) child).getValue() ==
null) {
-            buffer.append(" IS NULL");
+            buffer.append(" IS");
         } else {
             buffer.append(" =");
         }
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/NotEqualNode.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/NotEqualNode.java
index 76f1342..87cb3a0 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/NotEqualNode.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/NotEqualNode.java
@@ -35,7 +35,7 @@ public class NotEqualNode extends ExpressionNode {
         String expStr = " <> ";
         Node child = getChild(1);
         if (child.getType() == NodeType.VALUE && ((ValueNode) child).getValue() ==
null) {
-            expStr = " IS NOT NULL";
+            expStr = " IS NOT";
         }
         buffer.append(expStr);
     }
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/ValueNode.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/ValueNode.java
index 2911397..c14825c 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/ValueNode.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/sqlbuilder/sqltree/ValueNode.java
@@ -63,8 +63,13 @@ public class ValueNode extends Node {
         return buffer;
     }
 
+    protected void appendNullValue(QuotingAppendable buffer) {
+        buffer.append(" NULL");
+    }
+
     private void appendValue(Object val, QuotingAppendable buffer) {
         if(val == null) {
+            appendNullValue(buffer);
             return;
         }
 
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 be802c4..45e2439 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
@@ -484,6 +484,17 @@ public class QualifierTranslatorTest {
     }
 
     @Test
+    public void translateNullComparision() {
+        Node or = translate("a > null");
+        assertNotNull(or);
+        assertThat(or, instanceOf(OpExpressionNode.class));
+        assertEquals(">", ((OpExpressionNode)or).getOp());
+        assertEquals(2, or.getChildrenCount());
+        assertThat(or.getChild(0), instanceOf(ColumnNode.class));
+        assertThat(or.getChild(1), instanceOf(ValueNode.class));
+    }
+
+    @Test
     public void translateComplexExp() {
         Node result = translate("(a >= 1 + 2 / 3 << 4) and (db:b != true)");
 
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionIT.java b/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionIT.java
index b2d8749..1149b20 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/exp/ExpressionIT.java
@@ -23,6 +23,7 @@ import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.configuration.server.ServerRuntime;
 import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.query.ObjectSelect;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.apache.cayenne.testdo.testmap.Painting;
@@ -125,4 +126,24 @@ public class ExpressionIT extends ServerCase {
 		assertNull(e4.first(paintingList));
 	}
 
+	@Test
+	public void testLessThanNull() {
+		Artist a1 = context.newObject(Artist.class);
+		a1.setArtistName("Picasso");
+		context.commitChanges();
+
+		List<Artist> artists = ObjectSelect.query(Artist.class, Artist.ARTIST_NAME.lt((String)null)).select(context);
+		assertTrue("Less than 'NULL' never matches anything", artists.isEmpty());
+	}
+
+	@Test
+	public void testInNull() {
+		Artist a1 = context.newObject(Artist.class);
+		a1.setArtistName("Picasso");
+		context.commitChanges();
+
+		List<Artist> artists = ObjectSelect.query(Artist.class, Artist.ARTIST_NAME.in("Picasso",
(String)null)).select(context);
+		assertEquals(1, artists.size());
+	}
+
 }


Mime
View raw message