You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (Jira)" <ji...@apache.org> on 2020/11/03 06:16:00 UTC

[jira] [Updated] (GROOVY-8258) [GEP] Create a LINQ-like DSL

     [ https://issues.apache.org/jira/browse/GROOVY-8258?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Daniel Sun updated GROOVY-8258:
-------------------------------
    Description: 
h2. *Ⅰ. Introduction*

GINQ DSL is an alternative solution of GINQ, it is a simplified version of GINQ(GROOVY-9159) and will be implemented as a module "groovy-ginq".

GINQ DSL is wrapped with:
{code:java}
GINQ {
   // GINQ code
}
{code}
*NOTE:* The simplest GINQ should consists of {{from}} and {{select}}, which looks like:
{code:java}
from n in [1, 2, 3]
select n
{code}
h2. *Ⅱ. Sample code related to GROOVY-9159*
h3. 1. Filtering
h4. 1.1
{code:java}
from p in persons
where p.age > 15 && p.age <= 35
select p.name
{code}
h4. 1.2
{code:java}
from p in persons
where p.age > 15 && p.age <= 35
select p
{code}
h4. 1.3
{code:java}
from t in numbers 
where t <= 2
select t
{code}
h3. 2. Joining
h4. 2.1
{code:java}
from p in persons
innerjoin c in cities on p.city.name == c.name // putting `on` clause in the same line with `innerjoin` is recommended code style
select p.name, c.name
{code}
h4. 2.2
{code:java}
from p in persons
innerjoin c in cities on p.city == c
select p.name
{code}
h4. 2.3
{code:java}
from p in persons
leftjoin c in cities on p.city.name == c.name //  same with left outer join
select p.name, c.name
{code}
h4. 2.4
{code:java}
from p in persons
rightjoin c in cities on p.city.name == c.name //  same with right outer join
select p.name, c.name
{code}
h3. 3. Projection
h4. 3.1
{code:java}
from p in persons
select p.name
{code}
h4. 3.2
{code:java}
from p in persons
select p.name, p.age
{code}
h4. 3.3
{code:java}
from p in persons
select [name: p.name, age: p.age]
{code}
h4. 3.4
{code:java}
from p in persons
select new Person(name: p.name, age: p.age)
{code}
h4. 3.5
{code:java}
from p in persons
select p
{code}
h3. 4. Grouping
h4. 4.1
{code:java}
from p in persons
groupby p.gender
select p.gender, max(p.age)
{code}
h3. 5. Having
h4. 5.1
{code:java}
from p in persons
groupby p.gender
having p.gender == 'Male'
select p.gender, max(p.age)
{code}
h3. 6. Sorting
h4. 6.1
{code:java}
from p in persons
orderby p.age
select p.name
{code}
h4. 6.2
{code:java}
from p in persons
orderby p.age in desc // "`in` order" means sorting
select p.name
{code}
h4. 6.3
{code:java}
from p in persons
orderby p.age in desc, p.name in asc // asc is optional
select p.name
{code}
h3. 7. Pagination
h4. 7.1
{code:java}
from n in numbers
limit 2, 5
select n
{code}
h4. 7.2
{code:java}
from n in numbers
limit 5
select n
{code}
h3. 8. Nested Queries
h4. 8.1
{code:java}
from v in (
    from n in numbers
    where n <= 5
    select n
)
limit 2, 5
select v
{code}


  was:
h2. *Ⅰ. Introduction*

GINQ DSL is an alternative solution of GINQ, it is a simplified version of GINQ(GROOVY-9159) and will be implemented as a module "groovy-ginq".

GINQ DSL is wrapped with:
{code:java}
GINQ {
   // GINQ code
}
{code}
*NOTE:* The simplest GINQ should consists of {{from}} and {{select}}, which looks like:
{code:java}
from n in [1, 2, 3]
select n
{code}
h2. *Ⅱ. Sample code related to GROOVY-9159*
h3. 1. Filtering
h4. 1.1
{code:java}
from p in persons
where p.age > 15 && p.age <= 35
select p.name
{code}
h4. 1.2
{code:java}
from p in persons
where p.age > 15 && p.age <= 35
select p
{code}
h4. 1.3
{code:java}
from t in numbers 
where t <= 2
select t
{code}
h3. 2. Joining
h4. 2.1
{code:java}
from p in persons
innerjoin c in cities on p.city.name == c.name // putting `on` clause in the same line with `innerjoin` is recommended code style
select p.name, c.name
{code}
h4. 2.2
{code:java}
from p in persons
innerjoin c in cities on p.city == c
select p.name
{code}
h4. 2.3
{code:java}
from p in persons
leftjoin c in cities on p.city.name == c.name //  same with left outer join
select p.name, c.name
{code}
h4. 2.4
{code:java}
from p in persons
rightjoin c in cities on p.city.name == c.name //  same with right outer join
select p.name, c.name
{code}
h3. 3. Projection
h4. 3.1
{code:java}
from p in persons
select p.name
{code}
h4. 3.2
{code:java}
from p in persons
select p.name, p.age
{code}
h4. 3.3
{code:java}
from p in persons
select [name: p.name, age: p.age]
{code}
h4. 3.4
{code:java}
from p in persons
select new Person(name: p.name, age: p.age)
{code}
h4. 3.5
{code:java}
from p in persons
select p
{code}
h3. 4. Grouping
h4. 4.1
{code:java}
from p in persons
groupby p.gender
select p.gender, max(p.age)
{code}
h3. 5. Having
h4. 5.1
{code:java}
from p in persons
groupby p.gender
having p.gender == 'Male'
select p.gender, max(p.age)
{code}
h3. 6. Sorting
h4. 6.1
{code:java}
from p in persons
orderby p.age
select p.name
{code}
h4. 6.2
{code:java}
from p in persons
orderby p.age in desc // "`in` order" means sorting
select p.name
{code}
h4. 6.3
{code:java}
from p in persons
orderby p.age in desc, p.name in asc // asc is optional
select p.name
{code}
h3. 7. Pagination
h4. 7.1
{code:java}
from n in numbers
limit 2, 5
select n
{code}
h4. 7.2
{code:java}
from n in numbers
limit 5
select n
{code}
h3. 8. Nested Queries
h4. 8.1
{code:java}
from v in (
    from n in numbers
    where n <= 5
    select n
)
limit 2, 5
select v
{code}
h3. 9. WITH-Clause
h4. 9.1
{code:java}
with v as (
    from m in numbers
    where m <= 5
    select m
)
from n in v
limit 2, 5
select n
{code}
h3. 10. Union
h4. 10.1
{code:java}
from n in numbers1
select n
unionall
from n in numbers2
select n
{code}
h4. 10.2
{code:java}
from n in numbers1
select n
union
from n in numbers2
select n
{code}


> [GEP] Create a LINQ-like DSL
> ----------------------------
>
>                 Key: GROOVY-8258
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8258
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Daniel Sun
>            Assignee: Daniel Sun
>            Priority: Major
>             Fix For: 4.0.0-alpha-2
>
>
> h2. *Ⅰ. Introduction*
> GINQ DSL is an alternative solution of GINQ, it is a simplified version of GINQ(GROOVY-9159) and will be implemented as a module "groovy-ginq".
> GINQ DSL is wrapped with:
> {code:java}
> GINQ {
>    // GINQ code
> }
> {code}
> *NOTE:* The simplest GINQ should consists of {{from}} and {{select}}, which looks like:
> {code:java}
> from n in [1, 2, 3]
> select n
> {code}
> h2. *Ⅱ. Sample code related to GROOVY-9159*
> h3. 1. Filtering
> h4. 1.1
> {code:java}
> from p in persons
> where p.age > 15 && p.age <= 35
> select p.name
> {code}
> h4. 1.2
> {code:java}
> from p in persons
> where p.age > 15 && p.age <= 35
> select p
> {code}
> h4. 1.3
> {code:java}
> from t in numbers 
> where t <= 2
> select t
> {code}
> h3. 2. Joining
> h4. 2.1
> {code:java}
> from p in persons
> innerjoin c in cities on p.city.name == c.name // putting `on` clause in the same line with `innerjoin` is recommended code style
> select p.name, c.name
> {code}
> h4. 2.2
> {code:java}
> from p in persons
> innerjoin c in cities on p.city == c
> select p.name
> {code}
> h4. 2.3
> {code:java}
> from p in persons
> leftjoin c in cities on p.city.name == c.name //  same with left outer join
> select p.name, c.name
> {code}
> h4. 2.4
> {code:java}
> from p in persons
> rightjoin c in cities on p.city.name == c.name //  same with right outer join
> select p.name, c.name
> {code}
> h3. 3. Projection
> h4. 3.1
> {code:java}
> from p in persons
> select p.name
> {code}
> h4. 3.2
> {code:java}
> from p in persons
> select p.name, p.age
> {code}
> h4. 3.3
> {code:java}
> from p in persons
> select [name: p.name, age: p.age]
> {code}
> h4. 3.4
> {code:java}
> from p in persons
> select new Person(name: p.name, age: p.age)
> {code}
> h4. 3.5
> {code:java}
> from p in persons
> select p
> {code}
> h3. 4. Grouping
> h4. 4.1
> {code:java}
> from p in persons
> groupby p.gender
> select p.gender, max(p.age)
> {code}
> h3. 5. Having
> h4. 5.1
> {code:java}
> from p in persons
> groupby p.gender
> having p.gender == 'Male'
> select p.gender, max(p.age)
> {code}
> h3. 6. Sorting
> h4. 6.1
> {code:java}
> from p in persons
> orderby p.age
> select p.name
> {code}
> h4. 6.2
> {code:java}
> from p in persons
> orderby p.age in desc // "`in` order" means sorting
> select p.name
> {code}
> h4. 6.3
> {code:java}
> from p in persons
> orderby p.age in desc, p.name in asc // asc is optional
> select p.name
> {code}
> h3. 7. Pagination
> h4. 7.1
> {code:java}
> from n in numbers
> limit 2, 5
> select n
> {code}
> h4. 7.2
> {code:java}
> from n in numbers
> limit 5
> select n
> {code}
> h3. 8. Nested Queries
> h4. 8.1
> {code:java}
> from v in (
>     from n in numbers
>     where n <= 5
>     select n
> )
> limit 2, 5
> select v
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)