Skip to content

Commit

Permalink
modify watch to for
Browse files Browse the repository at this point in the history
  • Loading branch information
nktks committed Sep 18, 2023
1 parent bf32d74 commit 15d2d48
Show file tree
Hide file tree
Showing 28 changed files with 142 additions and 142 deletions.
44 changes: 22 additions & 22 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,14 @@ type InsertInput interface {
func (ValuesInput) isInsertInput() {}
func (SubQueryInput) isInsertInput() {}

// ChangeStreamWatch represents watch statement in CREATE/ALTER CHANGE STREAM statement.
type ChangeStreamWatch interface {
// ChangeStreamFor represents for statement in CREATE/ALTER CHANGE STREAM statement.
type ChangeStreamFor interface {
Node
isChangeStreamWatch()
isChangeStreamFor()
}

func (ChangeStreamWatchAll) isChangeStreamWatch() {}
func (ChangeStreamWatchTables) isChangeStreamWatch() {}
func (ChangeStreamForAll) isChangeStreamFor() {}
func (ChangeStreamForTables) isChangeStreamFor() {}

// ChangeStreamAlternation represents alter statement in ALTER CHANGE STREAM statement.
type ChangeStreamAlternation interface {
Expand Down Expand Up @@ -1804,39 +1804,39 @@ type CreateIndex struct {

// CreateChangeStream is CREATE CHANGE STREAM statement node.
//
// CREATE CHANGE STREAM {{.Name | sql}} {{.Watch | sql }}
// CREATE CHANGE STREAM {{.Name | sql}} {{.For | sql }}
// {{ if .Options }}OPTIONS {{ .Options.Exprs | sqlJoin ","}}{{end}}
type CreateChangeStream struct {
// pos = Create
// end = Options.Rparen + 1 || .Watch.end
// end = Options.Rparen + 1 || .For.end
Create token.Pos // position of "CREATE" keyword
Name *Ident
Watch ChangeStreamWatch
For ChangeStreamFor
Options *ChangeStreamOptions
}

// ChangeStreamWatchAll is FOR ALL node in CREATE CHANGE STREAM
type ChangeStreamWatchAll struct {
// ChangeStreamForAll is FOR ALL node in CREATE CHANGE STREAM
type ChangeStreamForAll struct {
// pos = For
// end = All
For token.Pos // position of "FOR" keyword
All token.Pos // position of "ALL" keyword
}

// ChangeStreamWatchTables is FOR tables node in CREATE CHANGE STREAM
// ChangeStreamForTables is FOR tables node in CREATE CHANGE STREAM
//
// FOR {{.WatchTables | sqlJoin ","}}
type ChangeStreamWatchTables struct {
// FOR {{.Tables | sqlJoin ","}}
type ChangeStreamForTables struct {
// pos = For
// end = WatchTables[$].end
For token.Pos // position of "FOR" keyword
WatchTables []*ChangeStreamWatchTable
// end = Tables[$].end
For token.Pos // position of "FOR" keyword
Tables []*ChangeStreamForTable
}

// ChangeStreamWatchTable table node in CREATE CHANGE STREAM SET FOR
// ChangeStreamForTable table node in CREATE CHANGE STREAM SET FOR
//
// .TableName{{if .Columns}}({{.Columns | sqlJoin ","}}){{end}}
type ChangeStreamWatchTable struct {
type ChangeStreamForTable struct {
// pos = TableName.pos
// end = TableName.end || Rparen + 1
TableName *Ident
Expand All @@ -1851,12 +1851,12 @@ type ChangeStreamOptions struct {

// ChangeStreamAlternationSetFor is SET FOR tables node in ALTER CHANGE STREAM
//
// SET FOR {{.Watch | sql }}
// SET FOR {{.For | sql }}
type ChangeStreamAlternationSetFor struct {
// pos = Set
// end = Watch.end
Set token.Pos // position of "SET" keyword
Watch ChangeStreamWatch
// end = For.end
Set token.Pos // position of "SET" keyword
For ChangeStreamFor
}

// ChangeStreamAlternationSetFor is DROP FOR ALL node in ALTER CHANGE STREAM
Expand Down
16 changes: 8 additions & 8 deletions ast/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,16 @@ func (c *CreateChangeStream) End() token.Pos {
if c.Options != nil {
return c.Options.Rparen + token.Pos(len(")"))
}
if c.Watch != nil {
return c.Watch.End()
if c.For != nil {
return c.For.End()
}
return c.Name.End()
}

func (c *ChangeStreamWatchAll) Pos() token.Pos { return c.For }
func (c *ChangeStreamWatchAll) End() token.Pos { return c.All }
func (c *ChangeStreamWatchTables) Pos() token.Pos { return c.For }
func (c *ChangeStreamWatchTables) End() token.Pos { return c.WatchTables[len(c.WatchTables)-1].End() }
func (c *ChangeStreamForAll) Pos() token.Pos { return c.For }
func (c *ChangeStreamForAll) End() token.Pos { return c.All }
func (c *ChangeStreamForTables) Pos() token.Pos { return c.For }
func (c *ChangeStreamForTables) End() token.Pos { return c.Tables[len(c.Tables)-1].End() }

func (s *Storing) Pos() token.Pos { return s.Storing }
func (s *Storing) End() token.Pos { return s.Rparen + 1 }
Expand All @@ -704,7 +704,7 @@ func (d *DropChangeStream) End() token.Pos { return d.Name.End() }
func (a *AlterChangeStream) Pos() token.Pos { return a.Alter }
func (a *AlterChangeStream) End() token.Pos { return a.ChangeStreamAlternation.End() }
func (a *ChangeStreamAlternationSetFor) Pos() token.Pos { return a.Set }
func (a *ChangeStreamAlternationSetFor) End() token.Pos { return a.Watch.End() }
func (a *ChangeStreamAlternationSetFor) End() token.Pos { return a.For.End() }
func (a *ChangeStreamAlternationDropForAll) Pos() token.Pos { return a.Drop }
func (a *ChangeStreamAlternationDropForAll) End() token.Pos { return a.All + token.Pos(len("ALL")) }
func (a *ChangeStreamAlternationSetOptions) Pos() token.Pos { return a.Set }
Expand Down Expand Up @@ -781,7 +781,7 @@ func (s *SizedSchemaType) End() token.Pos { return s.Rparen + 1 }
func (a *ArraySchemaType) Pos() token.Pos { return a.Array }
func (a *ArraySchemaType) End() token.Pos { return a.Gt + 1 }

func (c *ChangeStreamWatchTable) End() token.Pos {
func (c *ChangeStreamForTable) End() token.Pos {
if len(c.Columns) == 0 {
return c.TableName.End()
}
Expand Down
14 changes: 7 additions & 7 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,8 +972,8 @@ func (c *CreateIndex) SQL() string {

func (c *CreateChangeStream) SQL() string {
sql := "CREATE CHANGE STREAM " + c.Name.SQL()
if c.Watch != nil {
sql += fmt.Sprintf(" %s", c.Watch.SQL())
if c.For != nil {
sql += fmt.Sprintf(" %s", c.For.SQL())
}
if c.Options != nil {
sql += " OPTIONS ("
Expand All @@ -988,10 +988,10 @@ func (c *CreateChangeStream) SQL() string {
return sql
}

func (c *ChangeStreamWatchAll) SQL() string { return "FOR ALL" }
func (c *ChangeStreamWatchTables) SQL() string {
func (c *ChangeStreamForAll) SQL() string { return "FOR ALL" }
func (c *ChangeStreamForTables) SQL() string {
sql := "FOR "
for i, table := range c.WatchTables {
for i, table := range c.Tables {
if i > 0 {
sql += ", "
}
Expand All @@ -1005,7 +1005,7 @@ func (a *AlterChangeStream) SQL() string {
}

func (a ChangeStreamAlternationSetFor) SQL() string {
return fmt.Sprintf("SET %s", a.Watch.SQL())
return fmt.Sprintf("SET %s", a.For.SQL())
}
func (a ChangeStreamAlternationDropForAll) SQL() string { return "DROP FOR ALL" }
func (a ChangeStreamAlternationSetOptions) SQL() string {
Expand All @@ -1019,7 +1019,7 @@ func (a ChangeStreamAlternationSetOptions) SQL() string {
sql += ")"
return sql
}
func (c *ChangeStreamWatchTable) SQL() string {
func (c *ChangeStreamForTable) SQL() string {
sql := c.TableName.SQL()
if len(c.Columns) > 0 {
sql += "("
Expand Down
22 changes: 11 additions & 11 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2505,7 +2505,7 @@ func (p *Parser) parseCreateChangeStream(pos token.Pos) *ast.CreateChangeStream
Name: name,
}
if p.Token.Kind == "FOR" {
cs.Watch = p.parseChangeStreamWatch()
cs.For = p.parseChangeStreamFor()
}
if p.Token.IsKeywordLike("OPTIONS") {
p.nextToken()
Expand All @@ -2526,8 +2526,8 @@ func (p *Parser) parseAlterChangeStream(pos token.Pos) *ast.AlterChangeStream {
p.nextToken()
if p.Token.Kind == "FOR" {
csa := &ast.ChangeStreamAlternationSetFor{
Set: setpos,
Watch: p.parseChangeStreamWatch(),
Set: setpos,
For: p.parseChangeStreamFor(),
}
cs.ChangeStreamAlternation = csa
return cs
Expand Down Expand Up @@ -2564,37 +2564,37 @@ func (p *Parser) parseDropChangeStream(pos token.Pos) *ast.DropChangeStream {

}

func (p *Parser) parseChangeStreamWatch() ast.ChangeStreamWatch {
func (p *Parser) parseChangeStreamFor() ast.ChangeStreamFor {
pos := p.Token.Pos
p.nextToken()
if p.Token.Kind == "ALL" {
p.nextToken()
return &ast.ChangeStreamWatchAll{
return &ast.ChangeStreamForAll{
For: pos,
All: p.Token.Pos,
}

}
cswt := &ast.ChangeStreamWatchTables{
cswt := &ast.ChangeStreamForTables{
For: pos,
}
for {
tname := p.parseIdent()
watchTable := ast.ChangeStreamWatchTable{
forTable := ast.ChangeStreamForTable{
TableName: tname,
}

if p.Token.Kind == "(" {
p.nextToken()
watchTable.Columns = []*ast.Ident{p.parseIdent()}
forTable.Columns = []*ast.Ident{p.parseIdent()}
for p.Token.Kind == "," {
p.nextToken()
watchTable.Columns = append(watchTable.Columns, p.parseIdent())
forTable.Columns = append(forTable.Columns, p.parseIdent())
}
watchTable.Rparen = p.expect(")").Pos
forTable.Rparen = p.expect(")").Pos
}

cswt.WatchTables = append(cswt.WatchTables, &watchTable)
cswt.Tables = append(cswt.Tables, &forTable)
if p.Token.Kind == "," {
p.nextToken()
continue
Expand Down
4 changes: 2 additions & 2 deletions testdata/result/ddl/alter_change_stream_set_for_all.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ ALTER CHANGE STREAM change_stream_name SET FOR ALL
Name: "change_stream_name",
},
ChangeStreamAlternation: &ast.ChangeStreamAlternationSetFor{
Set: 39,
Watch: &ast.ChangeStreamWatchAll{
Set: 39,
For: &ast.ChangeStreamForAll{
For: 43,
All: 51,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ ALTER CHANGE STREAM change_stream_name SET FOR table_name1(column1, column2), ta
Name: "change_stream_name",
},
ChangeStreamAlternation: &ast.ChangeStreamAlternationSetFor{
Set: 39,
Watch: &ast.ChangeStreamWatchTables{
For: 43,
WatchTables: []*ast.ChangeStreamWatchTable{
&ast.ChangeStreamWatchTable{
Set: 39,
For: &ast.ChangeStreamForTables{
For: 43,
Tables: []*ast.ChangeStreamForTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 47,
NameEnd: 58,
Expand All @@ -34,7 +34,7 @@ ALTER CHANGE STREAM change_stream_name SET FOR table_name1(column1, column2), ta
},
Rparen: 75,
},
&ast.ChangeStreamWatchTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 78,
NameEnd: 89,
Expand Down
2 changes: 1 addition & 1 deletion testdata/result/ddl/create_change_stream.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CREATE CHANGE STREAM change_stream_name
NameEnd: 39,
Name: "change_stream_name",
},
Watch: nil,
For: nil,
Options: (*ast.ChangeStreamOptions)(nil),
}

Expand Down
2 changes: 1 addition & 1 deletion testdata/result/ddl/create_change_stream_for_all.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CREATE CHANGE STREAM change_stream_name FOR ALL
NameEnd: 39,
Name: "change_stream_name",
},
Watch: &ast.ChangeStreamWatchAll{
For: &ast.ChangeStreamForAll{
For: 40,
All: 48,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ CREATE CHANGE STREAM change_stream_name FOR table_name1(column1, column2), table
NameEnd: 39,
Name: "change_stream_name",
},
Watch: &ast.ChangeStreamWatchTables{
For: 40,
WatchTables: []*ast.ChangeStreamWatchTable{
&ast.ChangeStreamWatchTable{
For: &ast.ChangeStreamForTables{
For: 40,
Tables: []*ast.ChangeStreamForTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 44,
NameEnd: 55,
Expand All @@ -32,7 +32,7 @@ CREATE CHANGE STREAM change_stream_name FOR table_name1(column1, column2), table
},
Rparen: 72,
},
&ast.ChangeStreamWatchTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 75,
NameEnd: 86,
Expand Down
8 changes: 4 additions & 4 deletions testdata/result/ddl/create_change_stream_for_table.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ CREATE CHANGE STREAM change_stream_name FOR table_name
NameEnd: 39,
Name: "change_stream_name",
},
Watch: &ast.ChangeStreamWatchTables{
For: 40,
WatchTables: []*ast.ChangeStreamWatchTable{
&ast.ChangeStreamWatchTable{
For: &ast.ChangeStreamForTables{
For: 40,
Tables: []*ast.ChangeStreamForTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 44,
NameEnd: 54,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ CREATE CHANGE STREAM change_stream_name FOR table_name(column)
NameEnd: 39,
Name: "change_stream_name",
},
Watch: &ast.ChangeStreamWatchTables{
For: 40,
WatchTables: []*ast.ChangeStreamWatchTable{
&ast.ChangeStreamWatchTable{
For: &ast.ChangeStreamForTables{
For: 40,
Tables: []*ast.ChangeStreamForTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 44,
NameEnd: 54,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ CREATE CHANGE STREAM change_stream_name FOR table_name(column1, column2)
NameEnd: 39,
Name: "change_stream_name",
},
Watch: &ast.ChangeStreamWatchTables{
For: 40,
WatchTables: []*ast.ChangeStreamWatchTable{
&ast.ChangeStreamWatchTable{
For: &ast.ChangeStreamForTables{
For: 40,
Tables: []*ast.ChangeStreamForTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 44,
NameEnd: 54,
Expand Down
10 changes: 5 additions & 5 deletions testdata/result/ddl/create_change_stream_for_tables.sql.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ CREATE CHANGE STREAM change_stream_name FOR table_name1, table_name2
NameEnd: 39,
Name: "change_stream_name",
},
Watch: &ast.ChangeStreamWatchTables{
For: 40,
WatchTables: []*ast.ChangeStreamWatchTable{
&ast.ChangeStreamWatchTable{
For: &ast.ChangeStreamForTables{
For: 40,
Tables: []*ast.ChangeStreamForTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 44,
NameEnd: 55,
Expand All @@ -21,7 +21,7 @@ CREATE CHANGE STREAM change_stream_name FOR table_name1, table_name2
Columns: []*ast.Ident(nil),
Rparen: 0,
},
&ast.ChangeStreamWatchTable{
&ast.ChangeStreamForTable{
TableName: &ast.Ident{
NamePos: 57,
NameEnd: 68,
Expand Down
Loading

0 comments on commit 15d2d48

Please sign in to comment.