Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print SQL #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Pretty print SQL #78

wants to merge 2 commits into from

Conversation

DavidGregory084
Copy link
Contributor

This is a POC to see what the output would look like. Samples from the tests:

with subquery as (
    select 1 as a, sum(cast(2 as integer)) as b,
        (3 + 4) as c,
        row_number() over (order by 13, 14 desc) as rownum
    from one
    inner join two
    on ((5 = 6) and (7 <> 8))
    where ((9 = 10) and (11 <> 12))
)
select a, b, c
from subquery
where rownum between 241 and 256

update one
set col1 = 'a', col2 = 'b'
where (('c' = 'd') and ('e' = 'f'))

select one.col1 as one_col1,
    one.col2 as one_col2,
    two.col2 as two_col2,
    two.col3 as two_col3,
    three.col3 as three_col3,
    three.col4 as three_col4
from one
inner join two
on (one.col2 = two.col2)
inner join three
on (two.col3 = three.col3)

select
    case
        when (mytable.col1 = 1) then 2
        when (mytable.col2 = 2) then 3
    end as case
from mytable

select mytable.col1 as mytable_col1,
    mytable.col2 as mytable_col2
from mytable
where not exists (
    select mytable_col1, mytable_col2
    from (
        select mytable.col1 as mytable_col1,
            mytable.col2 as mytable_col2
        from mytable
    ) as subselect2
)

insert into one (col1, col2, col1, col2, col1,
    col2)
values ('a', 'b', 'a', 'b', 'a', 'b'),
       ('c', 'd', 'c', 'd', 'c', 'd'),
       ('e', 'f', 'e', 'f', 'e', 'f'),
       ('g', 'h', 'g', 'h', 'g', 'h')

I'm not sure how comfortable I am with these changes being in the StatementBuilder itself, but couldn't find a self-contained SQL formatting lib or ANTLR grammar that supports ANSI SQL syntax.

@codecov-io
Copy link

Codecov Report

Merging #78 into master will increase coverage by 1.68%.
The diff coverage is 95.94%.

@@            Coverage Diff             @@
##           master      #78      +/-   ##
==========================================
+ Coverage   81.69%   83.37%   +1.68%     
==========================================
  Files          43       43              
  Lines        1147     1281     +134     
  Branches      105      120      +15     
==========================================
+ Hits          937     1068     +131     
- Misses        210      213       +3
Impacted Files Coverage Δ
...scala/sqlest/sql/base/DeleteStatementBuilder.scala 100% <100%> (ø)
.../main/scala/sqlest/sql/base/StatementBuilder.scala 95.45% <100%> (+6.56%)
...src/main/scala/sqlest/sql/H2StatementBuilder.scala 100% <100%> (ø)
...scala/sqlest/sql/base/UpdateStatementBuilder.scala 100% <100%> (ø)
...rc/main/scala/sqlest/sql/DB2StatementBuilder.scala 85.89% <100%> (+4.86%)
...scala/sqlest/sql/base/InsertStatementBuilder.scala 100% <100%> (ø)
...lest/src/main/scala/sqlest/executor/Database.scala 65.03% <60%> (-0.46%)
...scala/sqlest/sql/base/SelectStatementBuilder.scala 94.84% <92%> (-2.17%)
...n/scala/sqlest/sql/base/BaseStatementBuilder.scala 94.11% <96.47%> (+3.2%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 204f978...5e5a275. Read the comment docs.

@DavidGregory084 DavidGregory084 requested a review from p14n March 10, 2017 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants