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

Support DMLs with named schema #262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3644,7 +3644,7 @@ type Insert struct {

InsertOrType InsertOrType

TableName *Ident
TableName *Path
Columns []*Ident
Input InsertInput
ThenReturn *ThenReturn // optional
Expand Down Expand Up @@ -3707,7 +3707,7 @@ type Delete struct {

Delete token.Pos // position of "DELETE" keyword

TableName *Ident
TableName *Path
As *AsAlias // optional
Where *Where
ThenReturn *ThenReturn // optional
Expand All @@ -3724,7 +3724,7 @@ type Update struct {

Update token.Pos // position of "UPDATE" keyword

TableName *Ident
TableName *Path
As *AsAlias // optional
Updates []*UpdateItem // len(Updates) > 0
Where *Where
Expand Down
6 changes: 3 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4754,7 +4754,7 @@ func (p *Parser) parseInsert(pos token.Pos) *ast.Insert {
p.nextToken()
}

name := p.parseIdent()
name := p.parsePath()

p.expect("(")
var columns []*ast.Ident
Expand Down Expand Up @@ -4849,7 +4849,7 @@ func (p *Parser) parseDelete(pos token.Pos) *ast.Delete {
p.nextToken()
}

name := p.parseIdent()
name := p.parsePath()
as := p.tryParseAsAlias(withOptionalAs)
where := p.parseWhere()
thenReturn := p.tryParseThenReturn()
Expand All @@ -4864,7 +4864,7 @@ func (p *Parser) parseDelete(pos token.Pos) *ast.Delete {
}

func (p *Parser) parseUpdate(pos token.Pos) *ast.Update {
name := p.parseIdent()
name := p.parsePath()
as := p.tryParseAsAlias(withOptionalAs)

p.expect("SET")
Expand Down
1 change: 1 addition & 0 deletions testdata/input/dml/delete_fqn.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete sch1.foo where foo = 1 and bar = 2
3 changes: 3 additions & 0 deletions testdata/input/dml/insert_fqn_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
insert sch1.foo (foo, bar, baz)
values (1, 2, 3),
(4, 5, 6)
1 change: 1 addition & 0 deletions testdata/input/dml/update_fqn.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
update sch1.foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1
12 changes: 8 additions & 4 deletions testdata/result/dml/!bad_insert.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ syntax error: testdata/input/dml/!bad_insert.sql:2:1: expected beginning of simp

--- AST
&ast.Insert{
TableName: &ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
},
},
},
Columns: []*ast.Ident{
&ast.Ident{
Expand Down
12 changes: 8 additions & 4 deletions testdata/result/dml/delete _from.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
delete from foo where foo = 1 and bar = 2
--- AST
&ast.Delete{
TableName: &ast.Ident{
NamePos: 12,
NameEnd: 15,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 12,
NameEnd: 15,
Name: "foo",
},
},
},
Where: &ast.Where{
Where: 16,
Expand Down
12 changes: 8 additions & 4 deletions testdata/result/dml/delete.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
delete foo where foo = 1 and bar = 2
--- AST
&ast.Delete{
TableName: &ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
},
},
},
Where: &ast.Where{
Where: 11,
Expand Down
12 changes: 8 additions & 4 deletions testdata/result/dml/delete_as.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
delete foo as F where F.foo = 1
--- AST
&ast.Delete{
TableName: &ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
},
},
},
As: &ast.AsAlias{
As: 11,
Expand Down
56 changes: 56 additions & 0 deletions testdata/result/dml/delete_fqn.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--- delete_fqn.sql
delete sch1.foo where foo = 1 and bar = 2
--- AST
&ast.Delete{
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 11,
Name: "sch1",
},
&ast.Ident{
NamePos: 12,
NameEnd: 15,
Name: "foo",
},
},
},
Where: &ast.Where{
Where: 16,
Expr: &ast.BinaryExpr{
Op: "AND",
Left: &ast.BinaryExpr{
Op: "=",
Left: &ast.Ident{
NamePos: 22,
NameEnd: 25,
Name: "foo",
},
Right: &ast.IntLiteral{
ValuePos: 28,
ValueEnd: 29,
Base: 10,
Value: "1",
},
},
Right: &ast.BinaryExpr{
Op: "=",
Left: &ast.Ident{
NamePos: 34,
NameEnd: 37,
Name: "bar",
},
Right: &ast.IntLiteral{
ValuePos: 40,
ValueEnd: 41,
Base: 10,
Value: "2",
},
},
},
},
}

--- SQL
DELETE FROM sch1.foo WHERE foo = 1 AND bar = 2
12 changes: 8 additions & 4 deletions testdata/result/dml/delete_then_return.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
delete foo where foo = 1 and bar = 2 then return *
--- AST
&ast.Delete{
TableName: &ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
},
},
},
Where: &ast.Where{
Where: 11,
Expand Down
12 changes: 8 additions & 4 deletions testdata/result/dml/insert_cte_select.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ with cte AS (select 1 as foo, 2 as bar)
select *
--- AST
&ast.Insert{
TableName: &ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 10,
Name: "foo",
},
},
},
Columns: []*ast.Ident{
&ast.Ident{
Expand Down
112 changes: 112 additions & 0 deletions testdata/result/dml/insert_fqn_values.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
--- insert_fqn_values.sql
insert sch1.foo (foo, bar, baz)
values (1, 2, 3),
(4, 5, 6)
--- AST
&ast.Insert{
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 7,
NameEnd: 11,
Name: "sch1",
},
&ast.Ident{
NamePos: 12,
NameEnd: 15,
Name: "foo",
},
},
},
Columns: []*ast.Ident{
&ast.Ident{
NamePos: 17,
NameEnd: 20,
Name: "foo",
},
&ast.Ident{
NamePos: 22,
NameEnd: 25,
Name: "bar",
},
&ast.Ident{
NamePos: 27,
NameEnd: 30,
Name: "baz",
},
},
Input: &ast.ValuesInput{
Values: 32,
Rows: []*ast.ValuesRow{
&ast.ValuesRow{
Lparen: 39,
Rparen: 47,
Exprs: []*ast.DefaultExpr{
&ast.DefaultExpr{
DefaultPos: -1,
Expr: &ast.IntLiteral{
ValuePos: 40,
ValueEnd: 41,
Base: 10,
Value: "1",
},
},
&ast.DefaultExpr{
DefaultPos: -1,
Expr: &ast.IntLiteral{
ValuePos: 43,
ValueEnd: 44,
Base: 10,
Value: "2",
},
},
&ast.DefaultExpr{
DefaultPos: -1,
Expr: &ast.IntLiteral{
ValuePos: 46,
ValueEnd: 47,
Base: 10,
Value: "3",
},
},
},
},
&ast.ValuesRow{
Lparen: 57,
Rparen: 65,
Exprs: []*ast.DefaultExpr{
&ast.DefaultExpr{
DefaultPos: -1,
Expr: &ast.IntLiteral{
ValuePos: 58,
ValueEnd: 59,
Base: 10,
Value: "4",
},
},
&ast.DefaultExpr{
DefaultPos: -1,
Expr: &ast.IntLiteral{
ValuePos: 61,
ValueEnd: 62,
Base: 10,
Value: "5",
},
},
&ast.DefaultExpr{
DefaultPos: -1,
Expr: &ast.IntLiteral{
ValuePos: 64,
ValueEnd: 65,
Base: 10,
Value: "6",
},
},
},
},
},
},
}

--- SQL
INSERT INTO sch1.foo (foo, bar, baz) VALUES (1, 2, 3), (4, 5, 6)
12 changes: 8 additions & 4 deletions testdata/result/dml/insert_into_values.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ values (1, 2, 3),
(4, 5, 6)
--- AST
&ast.Insert{
TableName: &ast.Ident{
NamePos: 12,
NameEnd: 15,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 12,
NameEnd: 15,
Name: "foo",
},
},
},
Columns: []*ast.Ident{
&ast.Ident{
Expand Down
12 changes: 8 additions & 4 deletions testdata/result/dml/insert_or_ignore.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ INSERT OR IGNORE INTO foo
--- AST
&ast.Insert{
InsertOrType: "IGNORE",
TableName: &ast.Ident{
NamePos: 22,
NameEnd: 25,
Name: "foo",
TableName: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 22,
NameEnd: 25,
Name: "foo",
},
},
},
Columns: []*ast.Ident{
&ast.Ident{
Expand Down
Loading
Loading