You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/11/04 23:42:01 UTC

[groovy] branch GROOVY-8258 updated: GROOVY-8258: add GINQ tips in the doco

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch GROOVY-8258
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-8258 by this push:
     new 2b6a7e5  GROOVY-8258: add GINQ tips in the doco
2b6a7e5 is described below

commit 2b6a7e52a001ebb7b6e032a2f8d99dbb837f50ff
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Nov 5 07:40:16 2020 +0800

    GROOVY-8258: add GINQ tips in the doco
---
 .../groovy-ginq/src/spec/doc/ginq-userguide.adoc   | 49 ++++++++++++++++++----
 .../test/org/apache/groovy/ginq/GinqTest.groovy    | 33 +++++++++++++++
 2 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
index ea1ccf5..e6e661f 100644
--- a/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
+++ b/subprojects/groovy-ginq/src/spec/doc/ginq-userguide.adoc
@@ -27,7 +27,7 @@ As GORM and Hibernate are powerful enough to support DB populating, we will cove
 
 == GINQ a.k.a. Groovy-Integrated Query
 
-GINQ is a DSL, which consists of the following structure:
+GINQ is a DSL for querying, which consists of the following structure:
 ```
        GINQ
        |__ from
@@ -59,19 +59,21 @@ For example,
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_execution_01,indent=0]
 ----
 
-=== Projection
+=== GINQ Syntax
+
+==== Projection
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_projection_01,indent=0]
 ----
 
-=== Filtering
+==== Filtering
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_filtering_01,indent=0]
 ----
 
-=== Joining
+==== Joining
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_joining_01,indent=0]
@@ -97,7 +99,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_joining_04,ind
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_joining_05,indent=0]
 ----
 
-=== Grouping
+==== Grouping
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_01,indent=0]
@@ -139,7 +141,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_grouping_03,in
 GINQ supports some built-in aggregate functions, e.g.
 `count`, `min`, `max`, `sum` and the most powerful one `agg`
 
-=== Sorting
+==== Sorting
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_sorting_01,indent=0]
@@ -162,7 +164,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_sorting_02,ind
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_sorting_04,indent=0]
 ----
 
-=== Pagination
+==== Pagination
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_pagination_01,indent=0]
@@ -173,7 +175,7 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_pagination_01,
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_pagination_02,indent=0]
 ----
 
-=== Nested GINQ
+==== Nested GINQ
 [source, sql]
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_nested_01,indent=0]
@@ -183,3 +185,34 @@ include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_nested_01,inde
 ----
 include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_nested_02,indent=0]
 ----
+
+=== GINQ Tips
+
+==== List Comprehension
+
+List comprehension is an elegant way to define and create lists based on existing lists:
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_tips_01,indent=0]
+----
+
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_tips_02,indent=0]
+----
+
+GINQ as list comprehension used in loop directly:
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_tips_03,indent=0]
+----
+
+==== Customize GINQ
+
+For advanced users, you could customize GINQ behaviour by specifying your own target code generator.
+For example, we could specify the qualified class name `org.apache.groovy.ginq.provider.collection.GinqAstWalker` as the target code generator to generate GINQ method calls for querying collections,
+which is the default behaviour of GINQ:
+[source, sql]
+----
+include::../test/org/apache/groovy/ginq/GinqTest.groovy[tags=ginq_customize_01,indent=0]
+----
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 693c004..5f10048 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
@@ -2132,10 +2132,43 @@ class GinqTest {
     @Test
     void "testGinq - customize GINQ target - 0"() {
         assertScript '''
+// tag::ginq_customize_01[]
             assert [0, 1, 2] == GQ('org.apache.groovy.ginq.provider.collection.GinqAstWalker') {
                 from n in [0, 1, 2]
                 select n
             }.toList()
+// end::ginq_customize_01[]
+        '''
+    }
+
+    @Test
+    void "testGinq - GINQ tips - 0"() {
+        assertScript '''
+// tag::ginq_tips_01[]
+            assert [4, 16, 36, 64, 100] == GQ {from n in (1..<11) where n % 2 == 0 select n ** 2}.toList()
+// end::ginq_tips_01[]
+        '''
+    }
+
+    @Test
+    void "testGinq - GINQ tips - 1"() {
+        assertScript '''
+// tag::ginq_tips_02[]
+            assert [4, 16, 36, 64, 100] == GQ {from n in (1..<11) where n % 2 == 0 select n ** 2} as List
+// end::ginq_tips_02[]
+        '''
+    }
+
+    @Test
+    void "testGinq - GINQ tips - 2"() {
+        assertScript '''
+// tag::ginq_tips_03[]
+            def result = []
+            for (def x : GQ {from n in (1..<11) where n % 2 == 0 select n ** 2}) {
+                result << x
+            }
+            assert [4, 16, 36, 64, 100] == result
+// end::ginq_tips_03[]
         '''
     }
 }