diff --git a/ast/ast.go b/ast/ast.go index a9634a91..38b7b499 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -2608,11 +2608,11 @@ type Update struct { // UpdateItem is SET clause items in UPDATE. // -// {{.Path | sqlJoin "."}} = {{.Expr | sql}} +// {{.Path | sqlJoin "."}} = {{.DefaultExpr | sql}} type UpdateItem struct { // pos = Path[0].pos - // end = Expr.end + // end = DefaultExpr.end - Path []*Ident // len(Path) > 0 - Expr Expr + Path []*Ident // len(Path) > 0 + DefaultExpr *DefaultExpr } diff --git a/ast/pos.go b/ast/pos.go index a2f1e925..04213755 100644 --- a/ast/pos.go +++ b/ast/pos.go @@ -929,4 +929,4 @@ func (u *Update) Pos() token.Pos { return u.Update } func (u *Update) End() token.Pos { return u.Where.End() } func (u *UpdateItem) Pos() token.Pos { return u.Path[0].Pos() } -func (u *UpdateItem) End() token.Pos { return u.Expr.End() } +func (u *UpdateItem) End() token.Pos { return u.DefaultExpr.End() } diff --git a/ast/sql.go b/ast/sql.go index a74c7775..56a647f3 100644 --- a/ast/sql.go +++ b/ast/sql.go @@ -1343,10 +1343,5 @@ func (u *Update) SQL() string { } func (u *UpdateItem) SQL() string { - sql := u.Path[0].SQL() - for _, id := range u.Path[1:] { - sql += "." + id.SQL() - } - sql += " = " + u.Expr.SQL() - return sql + return sqlJoin(u.Path, ".") + " = " + u.DefaultExpr.SQL() } diff --git a/parser.go b/parser.go index 149be710..eaebe944 100644 --- a/parser.go +++ b/parser.go @@ -3542,11 +3542,11 @@ func (p *Parser) parseUpdate(pos token.Pos) *ast.Update { func (p *Parser) parseUpdateItem() *ast.UpdateItem { path := p.parseIdentOrPath() p.expect("=") - expr := p.parseExpr() + defaultExpr := p.parseDefaultExpr() return &ast.UpdateItem{ - Path: path, - Expr: expr, + Path: path, + DefaultExpr: defaultExpr, } } diff --git a/testdata/input/dml/update.sql b/testdata/input/dml/update.sql index 85c816df..7379f523 100644 --- a/testdata/input/dml/update.sql +++ b/testdata/input/dml/update.sql @@ -1 +1 @@ -update foo set foo = bar, bar = foo where foo = 1 \ No newline at end of file +update foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1 \ No newline at end of file diff --git a/testdata/result/dml/update.sql.txt b/testdata/result/dml/update.sql.txt index 0e484ff4..8f218958 100644 --- a/testdata/result/dml/update.sql.txt +++ b/testdata/result/dml/update.sql.txt @@ -1,5 +1,5 @@ --- update.sql -update foo set foo = bar, bar = foo where foo = 1 +update foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1 --- AST &ast.Update{ Update: 0, @@ -18,10 +18,14 @@ update foo set foo = bar, bar = foo where foo = 1 Name: "foo", }, }, - Expr: &ast.Ident{ - NamePos: 21, - NameEnd: 24, - Name: "bar", + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: -1, + Default: false, + Expr: &ast.Ident{ + NamePos: 21, + NameEnd: 24, + Name: "bar", + }, }, }, &ast.UpdateItem{ @@ -32,25 +36,43 @@ update foo set foo = bar, bar = foo where foo = 1 Name: "bar", }, }, - Expr: &ast.Ident{ - NamePos: 32, - NameEnd: 35, - Name: "foo", + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: -1, + Default: false, + Expr: &ast.Ident{ + NamePos: 32, + NameEnd: 35, + Name: "foo", + }, + }, + }, + &ast.UpdateItem{ + Path: []*ast.Ident{ + &ast.Ident{ + NamePos: 37, + NameEnd: 40, + Name: "baz", + }, + }, + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: 43, + Default: true, + Expr: nil, }, }, }, Where: &ast.Where{ - Where: 36, + Where: 51, Expr: &ast.BinaryExpr{ Op: "=", Left: &ast.Ident{ - NamePos: 42, - NameEnd: 45, + NamePos: 57, + NameEnd: 60, Name: "foo", }, Right: &ast.IntLiteral{ - ValuePos: 48, - ValueEnd: 49, + ValuePos: 63, + ValueEnd: 64, Base: 10, Value: "1", }, @@ -59,4 +81,4 @@ update foo set foo = bar, bar = foo where foo = 1 } --- SQL -UPDATE foo SET foo = bar, bar = foo WHERE foo = 1 +UPDATE foo SET foo = bar, bar = foo, baz = DEFAULT WHERE foo = 1 diff --git a/testdata/result/dml/update_as.sql.txt b/testdata/result/dml/update_as.sql.txt index 36d5d92d..6f269aff 100644 --- a/testdata/result/dml/update_as.sql.txt +++ b/testdata/result/dml/update_as.sql.txt @@ -30,27 +30,31 @@ update foo as F set F.foo = F.bar + 1 where foo = F.bar Name: "foo", }, }, - Expr: &ast.BinaryExpr{ - Op: "+", - Left: &ast.Path{ - Idents: []*ast.Ident{ - &ast.Ident{ - NamePos: 28, - NameEnd: 29, - Name: "F", - }, - &ast.Ident{ - NamePos: 30, - NameEnd: 33, - Name: "bar", + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: -1, + Default: false, + Expr: &ast.BinaryExpr{ + Op: "+", + Left: &ast.Path{ + Idents: []*ast.Ident{ + &ast.Ident{ + NamePos: 28, + NameEnd: 29, + Name: "F", + }, + &ast.Ident{ + NamePos: 30, + NameEnd: 33, + Name: "bar", + }, }, }, - }, - Right: &ast.IntLiteral{ - ValuePos: 36, - ValueEnd: 37, - Base: 10, - Value: "1", + Right: &ast.IntLiteral{ + ValuePos: 36, + ValueEnd: 37, + Base: 10, + Value: "1", + }, }, }, }, diff --git a/testdata/result/statement/update.sql.txt b/testdata/result/statement/update.sql.txt index 0e484ff4..8f218958 100644 --- a/testdata/result/statement/update.sql.txt +++ b/testdata/result/statement/update.sql.txt @@ -1,5 +1,5 @@ --- update.sql -update foo set foo = bar, bar = foo where foo = 1 +update foo set foo = bar, bar = foo, baz = DEFAULT where foo = 1 --- AST &ast.Update{ Update: 0, @@ -18,10 +18,14 @@ update foo set foo = bar, bar = foo where foo = 1 Name: "foo", }, }, - Expr: &ast.Ident{ - NamePos: 21, - NameEnd: 24, - Name: "bar", + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: -1, + Default: false, + Expr: &ast.Ident{ + NamePos: 21, + NameEnd: 24, + Name: "bar", + }, }, }, &ast.UpdateItem{ @@ -32,25 +36,43 @@ update foo set foo = bar, bar = foo where foo = 1 Name: "bar", }, }, - Expr: &ast.Ident{ - NamePos: 32, - NameEnd: 35, - Name: "foo", + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: -1, + Default: false, + Expr: &ast.Ident{ + NamePos: 32, + NameEnd: 35, + Name: "foo", + }, + }, + }, + &ast.UpdateItem{ + Path: []*ast.Ident{ + &ast.Ident{ + NamePos: 37, + NameEnd: 40, + Name: "baz", + }, + }, + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: 43, + Default: true, + Expr: nil, }, }, }, Where: &ast.Where{ - Where: 36, + Where: 51, Expr: &ast.BinaryExpr{ Op: "=", Left: &ast.Ident{ - NamePos: 42, - NameEnd: 45, + NamePos: 57, + NameEnd: 60, Name: "foo", }, Right: &ast.IntLiteral{ - ValuePos: 48, - ValueEnd: 49, + ValuePos: 63, + ValueEnd: 64, Base: 10, Value: "1", }, @@ -59,4 +81,4 @@ update foo set foo = bar, bar = foo where foo = 1 } --- SQL -UPDATE foo SET foo = bar, bar = foo WHERE foo = 1 +UPDATE foo SET foo = bar, bar = foo, baz = DEFAULT WHERE foo = 1 diff --git a/testdata/result/statement/update_as.sql.txt b/testdata/result/statement/update_as.sql.txt index 36d5d92d..6f269aff 100644 --- a/testdata/result/statement/update_as.sql.txt +++ b/testdata/result/statement/update_as.sql.txt @@ -30,27 +30,31 @@ update foo as F set F.foo = F.bar + 1 where foo = F.bar Name: "foo", }, }, - Expr: &ast.BinaryExpr{ - Op: "+", - Left: &ast.Path{ - Idents: []*ast.Ident{ - &ast.Ident{ - NamePos: 28, - NameEnd: 29, - Name: "F", - }, - &ast.Ident{ - NamePos: 30, - NameEnd: 33, - Name: "bar", + DefaultExpr: &ast.DefaultExpr{ + DefaultPos: -1, + Default: false, + Expr: &ast.BinaryExpr{ + Op: "+", + Left: &ast.Path{ + Idents: []*ast.Ident{ + &ast.Ident{ + NamePos: 28, + NameEnd: 29, + Name: "F", + }, + &ast.Ident{ + NamePos: 30, + NameEnd: 33, + Name: "bar", + }, }, }, - }, - Right: &ast.IntLiteral{ - ValuePos: 36, - ValueEnd: 37, - Base: 10, - Value: "1", + Right: &ast.IntLiteral{ + ValuePos: 36, + ValueEnd: 37, + Base: 10, + Value: "1", + }, }, }, },