This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 23befea Support `!in` in GINQ
23befea is described below
commit 23befea38100fdab097628ad8427eccd74616e1b
Author: Daniel Sun <[email protected]>
AuthorDate: Sat Dec 12 21:54:19 2020 +0800
Support `!in` in GINQ
---
.../org/apache/groovy/ginq/dsl/GinqAstBuilder.java | 3 +-
.../ginq/provider/collection/GinqAstWalker.groovy | 2 +-
.../groovy-ginq/src/spec/doc/ginq-userguide.adoc | 31 ++++++++
.../test/org/apache/groovy/ginq/GinqTest.groovy | 92 ++++++++++++++++++++++
4 files changed, 126 insertions(+), 2 deletions(-)
diff --git
a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
index 80f71af..68d5cb7 100644
---
a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
+++
b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
@@ -301,7 +301,8 @@ public class GinqAstBuilder extends CodeVisitorSupport
implements SyntaxErrorRep
public void visitBinaryExpression(BinaryExpression expression) {
super.visitBinaryExpression(expression);
- if (expression.getOperation().getType() == Types.KEYWORD_IN) {
+ final int opType = expression.getOperation().getType();
+ if (opType == Types.KEYWORD_IN || opType == Types.COMPARE_NOT_IN) {
if (null != latestGinqExpression &&
isSelectMethodCallExpression(expression.getRightExpression())) {
// use the nested ginq and clear it
expression.setRightExpression(latestGinqExpression);
diff --git
a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
index 47662d5..2d0819e 100644
---
a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
+++
b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/GinqAstWalker.groovy
@@ -473,7 +473,7 @@ class GinqAstWalker implements GinqAstVisitor<Expression>,
SyntaxErrorReportable
}
if (expression instanceof BinaryExpression) {
- if (expression.operation.type == Types.KEYWORD_IN)
{
+ if (expression.operation.type in
[Types.KEYWORD_IN, Types.COMPARE_NOT_IN]) {
if (expression.rightExpression instanceof
AbstractGinqExpression) {
expression.rightExpression =
callX(GinqAstWalker.this.visit((AbstractGinqExpression)
expression.rightExpression),
diff --git a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
index c43089f..3d140e8 100644
--- a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
+++ b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
@@ -147,6 +147,37 @@
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_projection_02,
include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_01,indent=0]
----
+===== In
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_08,indent=0]
+----
+
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_07,indent=0]
+----
+
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_09,indent=0]
+----
+
+===== Not In
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_05,indent=0]
+----
+
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_06,indent=0]
+----
+
+[source, groovy]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_10,indent=0]
+----
===== Exists
[source, sql]
----
diff --git
a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 40b28ef..2b555b3 100644
---
a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++
b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -257,6 +257,32 @@ class GinqTest {
}
@Test
+ void "testGinq - from where select - 4"() {
+ assertGinqScript '''
+ assert [0] == GQ {
+// tag::ginq_filtering_05[]
+ from n in [0, 1, 2]
+ where n !in [1, 2]
+ select n
+// end::ginq_filtering_05[]
+ }.toList()
+ '''
+ }
+
+ @Test
+ void "testGinq - from where select - 5"() {
+ assertGinqScript '''
+ assert [1, 2] == GQ {
+// tag::ginq_filtering_08[]
+ from n in [0, 1, 2]
+ where n in [1, 2]
+ select n
+// end::ginq_filtering_08[]
+ }.toList()
+ '''
+ }
+
+ @Test
void "testGinq - from smartinnerjoin select - 1"() {
assertGinqScript '''
assert [[1, 1], [3, 3]] == GQ {
@@ -1084,6 +1110,72 @@ class GinqTest {
}
@Test
+ void "testGinq - nested from select - 26"() {
+ assertGinqScript '''
+ assert [0] == GQ {
+// tag::ginq_filtering_06[]
+ from n in [0, 1, 2]
+ where n !in (
+ from m in [1, 2]
+ select m
+ )
+ select n
+// end::ginq_filtering_06[]
+ }.toList()
+ '''
+ }
+
+ @Test
+ void "testGinq - nested from select - 27"() {
+ assertGinqScript '''
+ assert [1, 2] == GQ {
+// tag::ginq_filtering_07[]
+ from n in [0, 1, 2]
+ where n in (
+ from m in [1, 2]
+ select m
+ )
+ select n
+// end::ginq_filtering_07[]
+ }.toList()
+ '''
+ }
+
+ @Test
+ void "testGinq - nested from select - 28"() {
+ assertGinqScript '''
+// tag::ginq_filtering_09[]
+ import static groovy.lang.Tuple.tuple
+ assert [0, 1] == GQ {
+ from n in [0, 1, 2]
+ where tuple(n, n + 1) in (
+ from m in [1, 2]
+ select m - 1, m
+ )
+ select n
+ }.toList()
+// end::ginq_filtering_09[]
+ '''
+ }
+
+ @Test
+ void "testGinq - nested from select - 29"() {
+ assertGinqScript '''
+// tag::ginq_filtering_10[]
+ import static groovy.lang.Tuple.tuple
+ assert [2] == GQ {
+ from n in [0, 1, 2]
+ where tuple(n, n + 1) !in (
+ from m in [1, 2]
+ select m - 1, m
+ )
+ select n
+ }.toList()
+// end::ginq_filtering_10[]
+ '''
+ }
+
+ @Test
void "testGinq - from leftjoin select - 1"() {
assertGinqScript '''
def nums1 = [1, 2, 3]