Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8258

[GEP] Create a LINQ-like DSL (basic cases)

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-alpha-2
    • None
    • None

    Description

      Ⅰ. 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:

      GQ {
         // GINQ code
      }
      

      NOTE:

      • The simplest GINQ MUST consist of ONE from clause and ONE select clause, which looks like:
      from n in [1, 2, 3]
      select n
      
      • Currently supports the following clauses:
             ginq
             |__ from
             |__ [innerjoin/leftjoin/rightjoin/fulljoin/crossjoin]*
             |   |__ on
             |__ [where]
             |__ [groupby]
             |   |__ [having]
             |__ [orderby]
             |__ [limit]
             |__ select
         
      (Note: [ ] means optional)
      

      Ⅱ. Sample code related to GROOVY-9159

      1. Filtering

      1.1

      from p in persons
      where p.age > 15 && p.age <= 35
      select p.name
      

      1.2

      from p in persons
      where p.age > 15 && p.age <= 35
      select p
      

      1.3

      from t in numbers 
      where t <= 2
      select t
      

      2. Joining

      2.1

      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
      

      2.2

      from p in persons
      innerjoin c in cities on p.city == c
      select p.name
      

      2.3

      from p in persons
      leftjoin c in cities on p.city.name == c.name //  same with left outer join
      select p.name, c.name
      

      2.4

      from p in persons
      rightjoin c in cities on p.city.name == c.name //  same with right outer join
      select p.name, c.name
      

      3. Projection

      3.1

      from p in persons
      select p.name
      

      3.2

      from p in persons
      select p.name, p.age
      

      3.3

      from p in persons
      select [name: p.name, age: p.age]
      

      3.4

      from p in persons
      select new Person(name: p.name, age: p.age)
      

      3.5

      from p in persons
      select p
      

      4. Grouping

      4.1

      from p in persons
      groupby p.gender
      select p.gender, max(p.age)
      

      5. Having

      5.1

      from p in persons
      groupby p.gender
      having p.gender == 'Male'
      select p.gender, max(p.age)
      

      6. Sorting

      6.1

      from p in persons
      orderby p.age
      select p.name
      

      6.2

      from p in persons
      orderby p.age in desc // "`in` order" means sorting
      select p.name
      

      6.3

      from p in persons
      orderby p.age in desc, p.name in asc // asc is optional
      select p.name
      

      7. Pagination

      7.1

      from n in numbers
      limit 2, 5
      select n
      

      7.2

      from n in numbers
      limit 5
      select n
      

      8. Nested Queries

      8.1

      from v in (
          from n in numbers
          where n <= 5
          select n
      )
      limit 2, 5
      select v
      

      Advanced cases could be found at GROOVY-9806

      Attachments

        Issue Links

          Activity

            People

              daniel_sun Daniel Sun
              daniel_sun Daniel Sun
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h