You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2020/06/11 11:05:32 UTC

[cayenne] branch master updated: CAY-2664 Add methods to EntityProperty to allow direct usage of primary key values

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 078ebf4  CAY-2664 Add methods to EntityProperty to allow direct usage of primary key values
078ebf4 is described below

commit 078ebf4bcd617f64cd6fafeb4d624fde48ebb246
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Thu Jun 11 14:05:22 2020 +0300

    CAY-2664 Add methods to EntityProperty to allow direct usage of primary key values
---
 RELEASE-NOTES.txt                                  |  1 +
 .../cayenne/exp/property/EntityProperty.java       | 33 ++++++++++++++++++
 .../cayenne/exp/property/EntityPropertyTest.java   | 39 ++++++++++++++++++++++
 3 files changed, 73 insertions(+)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index b6eac74..50d546d 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -17,6 +17,7 @@ CAY-2338 Support comments in cgen and default templates
 CAY-2656 Modeler: option to download required jars directly from maven central
 CAY-2659 Use new SQLBuilder utility to generate SQL for batch queries
 CAY-2662 Use custom interface for SQL tree processor istead of a Function<Node, Node>
+CAY-2664 Add methods to EntityProperty to allow direct usage of primary key values
 
 Bug Fixes:
 
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/exp/property/EntityProperty.java b/cayenne-server/src/main/java/org/apache/cayenne/exp/property/EntityProperty.java
index ee5ba2a..c5a61af 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/exp/property/EntityProperty.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/exp/property/EntityProperty.java
@@ -19,6 +19,8 @@
 
 package org.apache.cayenne.exp.property;
 
+import java.util.Collection;
+
 import org.apache.cayenne.Persistent;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
@@ -53,6 +55,37 @@ public class EntityProperty<E extends Persistent> extends BaseProperty<E> implem
         super(name, expression, type);
     }
 
+    public Expression eqId(Object id) {
+        return ExpressionFactory.matchExp(getExpression(), id);
+    }
+
+    public Expression inId(Collection<Object> ids) {
+        return ExpressionFactory.inExp(getExpression(), ids);
+    }
+
+    public Expression inId(Object firstId, Object... moreIds) {
+        Object[] ids = new Object[moreIds.length + 1];
+        ids[0] = firstId;
+        System.arraycopy(moreIds, 0, ids, 1, moreIds.length);
+        return ExpressionFactory.inExp(getExpression(), ids);
+    }
+
+    public Expression neqId(Object id) {
+        return ExpressionFactory.noMatchExp(getExpression(), id);
+    }
+
+    public Expression ninId(Collection<Object> ids) {
+        return ExpressionFactory.notInExp(getExpression(), ids);
+    }
+
+    public Expression ninId(Object firstId, Object... moreIds) {
+        Object[] ids = new Object[moreIds.length + 1];
+        ids[0] = firstId;
+        System.arraycopy(moreIds, 0, ids, 1, moreIds.length);
+        return ExpressionFactory.notInExp(getExpression(), ids);
+    }
+
+
     /**
      * {@inheritDoc}
      */
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/exp/property/EntityPropertyTest.java b/cayenne-server/src/test/java/org/apache/cayenne/exp/property/EntityPropertyTest.java
index 78a532d..3d548e4 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/exp/property/EntityPropertyTest.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/exp/property/EntityPropertyTest.java
@@ -19,6 +19,9 @@
 
 package org.apache.cayenne.exp.property;
 
+import java.util.Arrays;
+
+import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
 import org.apache.cayenne.testdo.testmap.Artist;
 import org.junit.Before;
@@ -76,4 +79,40 @@ public class EntityPropertyTest {
         assertEquals("path.other", other.getName());
         assertEquals(ExpressionFactory.pathExp("path.other"), other.getExpression());
     }
+
+    @Test
+    public void eqId() {
+        Expression exp = property.eqId(1);
+        assertEquals(ExpressionFactory.exp("path = 1"), exp);
+    }
+
+    @Test
+    public void inIdCollection() {
+        Expression exp = property.inId(Arrays.asList(1, 2, 3));
+        assertEquals(ExpressionFactory.exp("path in (1, 2, 3)"), exp);
+    }
+
+    @Test
+    public void inIdVararg() {
+        Expression exp = property.inId(1, 2, 3);
+        assertEquals(ExpressionFactory.exp("path in (1, 2, 3)"), exp);
+    }
+
+    @Test
+    public void neqId() {
+        Expression exp = property.neqId(1);
+        assertEquals(ExpressionFactory.exp("path <> 1"), exp);
+    }
+
+    @Test
+    public void ninIdCollection() {
+        Expression exp = property.ninId(Arrays.asList(1, 2, 3));
+        assertEquals(ExpressionFactory.exp("path not in (1, 2, 3)"), exp);
+    }
+
+    @Test
+    public void ninIdVararg() {
+        Expression exp = property.ninId(1, 2, 3);
+        assertEquals(ExpressionFactory.exp("path not in (1, 2, 3)"), exp);
+    }
 }
\ No newline at end of file