Skip to content

Commit

Permalink
patched except
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Nov 3, 2023
1 parent bed6153 commit d13eaba
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func parseSelectListItem(cursor *parsly.Cursor, list *query.List) error {
item.Comments = match.Text(cursor)
}
list.Append(item)
match := cursor.MatchAfterOptional(whitespaceMatcher, inlineCommentMatcher, commentBlockMatcher, binaryOperatorMatcher, logicalOperatorMatcher, nextMatcher)
match := cursor.MatchAfterOptional(whitespaceMatcher, exceptKeywordMatcher, inlineCommentMatcher, commentBlockMatcher, binaryOperatorMatcher, logicalOperatorMatcher, nextMatcher)
switch match.Code {
case commentBlock:
item.Comments = match.Text(cursor)
Expand Down Expand Up @@ -167,7 +167,7 @@ func parseGroupByList(cursor *parsly.Cursor, list *query.List) error {
return nil
}

//ParseList parses list
// ParseList parses list
func ParseList(raw string) (query.List, error) {
cursor := parsly.NewCursor("", []byte(raw), 0)
list := query.List{}
Expand Down
12 changes: 11 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,20 @@ func matchPostFrom(cursor *parsly.Cursor, dest *query.Select, match *parsly.Toke
}

func expectExpectIdentifiers(cursor *parsly.Cursor, expect *[]string) (bool, error) {
match := cursor.MatchAfterOptional(whitespaceMatcher, identifierMatcher)
pos := cursor.Pos
match := cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher, identifierMatcher)
switch match.Code {
case parenthesesCode:
block := match.Text(cursor)
for _, item := range strings.Split(block[1:len(block)-1], ",") {
*expect = append(*expect, strings.TrimSpace(item))
}
case identifierCode:
item := match.Text(cursor)
if cursor.Pos < len(cursor.Input) && cursor.Input[cursor.Pos] == '(' {
cursor.Pos = pos
return false, nil
}
*expect = append(*expect, item)
default:
return false, nil
Expand Down
19 changes: 15 additions & 4 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ func TestParseSelect(t *testing.T) {
options []Option
}{

{
description: "except",
SQL: "SELECT main.* EXCEPT(Id), cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
expect: "SELECT main.* EXCEPT Id, cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
},
{
description: "except group",
SQL: "SELECT main.* EXCEPT (Id,Name), cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
expect: "SELECT main.* EXCEPT (Id, Name), cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
},

{
description: "criteria with expr",
SQL: "SELECT Name FROM BAR WHERE ${predicate}",
Expand Down Expand Up @@ -96,13 +107,13 @@ func TestParseSelect(t *testing.T) {
{
description: "except select",
SQL: "SELECT * EXCEPT c1,c2 FROM x t",
expect: "SELECT * EXCEPT c1, c2 FROM x t",
expect: "SELECT * EXCEPT (c1, c2) FROM x t",
},

{
description: "except select",
SQL: "SELECT t1.* EXCEPT c1,c2, t2.* EXCEPT c3 FROM x t1 JOIN y AS t2 ON t1.ID=t2.ID",
expect: "SELECT t1.* EXCEPT c1, c2, t2.* EXCEPT c3 FROM x t1 JOIN y t2 ON t1.ID = t2.ID",
expect: "SELECT t1.* EXCEPT (c1, c2), t2.* EXCEPT c3 FROM x t1 JOIN y t2 ON t1.ID = t2.ID",
},

{
Expand Down Expand Up @@ -164,13 +175,13 @@ func TestParseSelect(t *testing.T) {
{
description: "except select",
SQL: "SELECT * EXCEPT c1,c2 FROM x t",
expect: "SELECT * EXCEPT c1, c2 FROM x t",
expect: "SELECT * EXCEPT (c1, c2) FROM x t",
},

{
description: "except select",
SQL: "SELECT t1.* EXCEPT c1,c2, t2.* EXCEPT c3 FROM x t1 JOIN y AS t2 ON t1.ID=t2.ID",
expect: "SELECT t1.* EXCEPT c1, c2, t2.* EXCEPT c3 FROM x t1 JOIN y t2 ON t1.ID = t2.ID",
expect: "SELECT t1.* EXCEPT (c1, c2), t2.* EXCEPT c3 FROM x t1 JOIN y t2 ON t1.ID = t2.ID",
},

{
Expand Down
6 changes: 6 additions & 0 deletions stringify.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,18 @@ func stringify(n node.Node, builder *bytes.Buffer) {
stringify(actual.X, builder)
if len(actual.Except) > 0 {
builder.WriteString(" EXCEPT ")
if len(actual.Except) > 1 {
builder.WriteString("(")
}
for i, item := range actual.Except {
if i > 0 {
builder.WriteString(", ")
}
builder.WriteString(item)
}
if len(actual.Except) > 1 {
builder.WriteString(")")
}
}
if actual.Comments != "" {
builder.WriteString(" ")
Expand Down

0 comments on commit d13eaba

Please sign in to comment.