Skip to content

Commit ff4c881

Browse files
authored
Fix ALTER PROTO BUNDLE to accept combination of INSERT/UPDATE/DELETE (#202)
1 parent bdde761 commit ff4c881

16 files changed

+282
-62
lines changed

ast/ast.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -1871,15 +1871,6 @@ type CreatePlacement struct {
18711871
//
18721872
// ================================================================================
18731873

1874-
type ProtoBundleAlteration interface {
1875-
Node
1876-
isProtoBundleAlteration()
1877-
}
1878-
1879-
func (AlterProtoBundleInsert) isProtoBundleAlteration() {}
1880-
func (AlterProtoBundleUpdate) isProtoBundleAlteration() {}
1881-
func (AlterProtoBundleDelete) isProtoBundleAlteration() {}
1882-
18831874
// ProtoBundleTypes is parenthesized Protocol Buffers type names node IN CREATE/ALTER PROTO BUNDLE statement.
18841875
//
18851876
// ({{.Types | sqlJoin ", "}})
@@ -1905,14 +1896,17 @@ type CreateProtoBundle struct {
19051896

19061897
// AlterProtoBundle is ALTER PROTO BUNDLE statement node.
19071898
//
1908-
// ALTER PROTO BUNDLE {{.Alteration | sql}}
1899+
// ALTER PROTO BUNDLE {{.Insert | sqlOpt}} {{.Update | sqlOpt}} {{.Delete | sqlOpt}}
19091900
type AlterProtoBundle struct {
19101901
// pos = Alter
1911-
// end = Alteration.end
1902+
// end = (Delete ?? Update ?? Insert).end || Bundle + 6
19121903

1913-
Alter token.Pos // position of "ALTER" keyword
1904+
Alter token.Pos // position of "ALTER" keyword
1905+
Bundle token.Pos
19141906

1915-
Alteration ProtoBundleAlteration
1907+
Insert *AlterProtoBundleInsert // optional
1908+
Update *AlterProtoBundleUpdate // optional
1909+
Delete *AlterProtoBundleDelete // optional
19161910
}
19171911

19181912
// AlterProtoBundleInsert is INSERT (proto_type_name, ...) node in ALTER PROTO BUNDLE.

ast/pos.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ast/sql.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,12 @@ func (p *ProtoBundleTypes) SQL() string { return "(" + sqlJoin(p.Types, ", ") +
806806

807807
func (b *CreateProtoBundle) SQL() string { return "CREATE PROTO BUNDLE " + b.Types.SQL() }
808808

809-
func (a *AlterProtoBundle) SQL() string { return "ALTER PROTO BUNDLE " + a.Alteration.SQL() }
809+
func (a *AlterProtoBundle) SQL() string {
810+
return "ALTER PROTO BUNDLE" +
811+
sqlOpt(" ", a.Insert, "") +
812+
sqlOpt(" ", a.Update, "") +
813+
sqlOpt(" ", a.Delete, "")
814+
}
810815

811816
func (a *AlterProtoBundleInsert) SQL() string { return "INSERT " + a.Types.SQL() }
812817

parser.go

+52-35
Original file line numberDiff line numberDiff line change
@@ -2505,11 +2505,59 @@ func (p *Parser) parseCreateProtoBundle(pos token.Pos) *ast.CreateProtoBundle {
25052505

25062506
func (p *Parser) parseAlterProtoBundle(pos token.Pos) *ast.AlterProtoBundle {
25072507
p.expect("PROTO")
2508-
p.expectKeywordLike("BUNDLE")
2509-
alteration := p.parseProtoBundleAlteration()
2508+
bundle := p.expectKeywordLike("BUNDLE").Pos
2509+
insert := p.tryParseAlterProtoBundleInsert()
2510+
update := p.tryParseAlterProtoBundleUpdate()
2511+
delete := p.tryParseAlterProtoBundleDelete()
2512+
25102513
return &ast.AlterProtoBundle{
2511-
Alter: pos,
2512-
Alteration: alteration,
2514+
Alter: pos,
2515+
Bundle: bundle,
2516+
Insert: insert,
2517+
Update: update,
2518+
Delete: delete,
2519+
}
2520+
}
2521+
2522+
func (p *Parser) tryParseAlterProtoBundleInsert() *ast.AlterProtoBundleInsert {
2523+
if !p.Token.IsKeywordLike("INSERT") {
2524+
return nil
2525+
}
2526+
2527+
pos := p.expectKeywordLike("INSERT").Pos
2528+
types := p.parseProtoBundleTypes()
2529+
2530+
return &ast.AlterProtoBundleInsert{
2531+
Insert: pos,
2532+
Types: types,
2533+
}
2534+
}
2535+
2536+
func (p *Parser) tryParseAlterProtoBundleUpdate() *ast.AlterProtoBundleUpdate {
2537+
if !p.Token.IsKeywordLike("UPDATE") {
2538+
return nil
2539+
}
2540+
2541+
pos := p.expectKeywordLike("UPDATE").Pos
2542+
types := p.parseProtoBundleTypes()
2543+
2544+
return &ast.AlterProtoBundleUpdate{
2545+
Update: pos,
2546+
Types: types,
2547+
}
2548+
}
2549+
2550+
func (p *Parser) tryParseAlterProtoBundleDelete() *ast.AlterProtoBundleDelete {
2551+
if !p.Token.IsKeywordLike("DELETE") {
2552+
return nil
2553+
}
2554+
2555+
pos := p.expectKeywordLike("DELETE").Pos
2556+
types := p.parseProtoBundleTypes()
2557+
2558+
return &ast.AlterProtoBundleDelete{
2559+
Delete: pos,
2560+
Types: types,
25132561
}
25142562
}
25152563

@@ -4379,34 +4427,3 @@ func (p *Parser) parseRenameTable(pos token.Pos) *ast.RenameTable {
43794427
}
43804428

43814429
}
4382-
4383-
func (p *Parser) parseProtoBundleAlteration() ast.ProtoBundleAlteration {
4384-
switch {
4385-
case p.Token.IsKeywordLike("INSERT"):
4386-
insert := p.expectKeywordLike("INSERT").Pos
4387-
types := p.parseProtoBundleTypes()
4388-
4389-
return &ast.AlterProtoBundleInsert{
4390-
Insert: insert,
4391-
Types: types,
4392-
}
4393-
case p.Token.IsKeywordLike("UPDATE"):
4394-
update := p.expectKeywordLike("UPDATE").Pos
4395-
types := p.parseProtoBundleTypes()
4396-
4397-
return &ast.AlterProtoBundleUpdate{
4398-
Update: update,
4399-
Types: types,
4400-
}
4401-
case p.Token.IsKeywordLike("DELETE"):
4402-
delete := p.expectKeywordLike("DELETE").Pos
4403-
types := p.parseProtoBundleTypes()
4404-
4405-
return &ast.AlterProtoBundleDelete{
4406-
Delete: delete,
4407-
Types: types,
4408-
}
4409-
default:
4410-
panic(p.errorfAtToken(&p.Token, `expected INSERT, UPDATE or DELETE, but: %v`, p.Token.AsString))
4411-
}
4412-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER PROTO BUNDLE INSERT (package.Inserted) UPDATE (package.Updated) DELETE (package.Deleted)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER PROTO BUNDLE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--- alter_proto_bundle_all.sql
2+
ALTER PROTO BUNDLE INSERT (package.Inserted) UPDATE (package.Updated) DELETE (package.Deleted)
3+
--- AST
4+
&ast.AlterProtoBundle{
5+
Alter: 0,
6+
Bundle: 12,
7+
Insert: &ast.AlterProtoBundleInsert{
8+
Insert: 19,
9+
Types: &ast.ProtoBundleTypes{
10+
Lparen: 26,
11+
Rparen: 43,
12+
Types: []*ast.NamedType{
13+
&ast.NamedType{
14+
Path: []*ast.Ident{
15+
&ast.Ident{
16+
NamePos: 27,
17+
NameEnd: 34,
18+
Name: "package",
19+
},
20+
&ast.Ident{
21+
NamePos: 35,
22+
NameEnd: 43,
23+
Name: "Inserted",
24+
},
25+
},
26+
},
27+
},
28+
},
29+
},
30+
Update: &ast.AlterProtoBundleUpdate{
31+
Update: 45,
32+
Types: &ast.ProtoBundleTypes{
33+
Lparen: 52,
34+
Rparen: 68,
35+
Types: []*ast.NamedType{
36+
&ast.NamedType{
37+
Path: []*ast.Ident{
38+
&ast.Ident{
39+
NamePos: 53,
40+
NameEnd: 60,
41+
Name: "package",
42+
},
43+
&ast.Ident{
44+
NamePos: 61,
45+
NameEnd: 68,
46+
Name: "Updated",
47+
},
48+
},
49+
},
50+
},
51+
},
52+
},
53+
Delete: &ast.AlterProtoBundleDelete{
54+
Delete: 70,
55+
Types: &ast.ProtoBundleTypes{
56+
Lparen: 77,
57+
Rparen: 93,
58+
Types: []*ast.NamedType{
59+
&ast.NamedType{
60+
Path: []*ast.Ident{
61+
&ast.Ident{
62+
NamePos: 78,
63+
NameEnd: 85,
64+
Name: "package",
65+
},
66+
&ast.Ident{
67+
NamePos: 86,
68+
NameEnd: 93,
69+
Name: "Deleted",
70+
},
71+
},
72+
},
73+
},
74+
},
75+
},
76+
}
77+
78+
--- SQL
79+
ALTER PROTO BUNDLE INSERT (package.Inserted) UPDATE (package.Updated) DELETE (package.Deleted)

testdata/result/ddl/alter_proto_bundle_delete.sql.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
ALTER PROTO BUNDLE DELETE(`examples.shipping.OrderHistory`)
33
--- AST
44
&ast.AlterProtoBundle{
5-
Alter: 0,
6-
Alteration: &ast.AlterProtoBundleDelete{
5+
Alter: 0,
6+
Bundle: 12,
7+
Insert: (*ast.AlterProtoBundleInsert)(nil),
8+
Update: (*ast.AlterProtoBundleUpdate)(nil),
9+
Delete: &ast.AlterProtoBundleDelete{
710
Delete: 19,
811
Types: &ast.ProtoBundleTypes{
912
Lparen: 25,

testdata/result/ddl/alter_proto_bundle_insert.sql.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ ALTER PROTO BUNDLE INSERT (
44
)
55
--- AST
66
&ast.AlterProtoBundle{
7-
Alter: 0,
8-
Alteration: &ast.AlterProtoBundleInsert{
7+
Alter: 0,
8+
Bundle: 12,
9+
Insert: &ast.AlterProtoBundleInsert{
910
Insert: 19,
1011
Types: &ast.ProtoBundleTypes{
1112
Lparen: 26,
@@ -33,6 +34,8 @@ ALTER PROTO BUNDLE INSERT (
3334
},
3435
},
3536
},
37+
Update: (*ast.AlterProtoBundleUpdate)(nil),
38+
Delete: (*ast.AlterProtoBundleDelete)(nil),
3639
}
3740

3841
--- SQL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--- alter_proto_bundle_noop.sql
2+
ALTER PROTO BUNDLE
3+
--- AST
4+
&ast.AlterProtoBundle{
5+
Alter: 0,
6+
Bundle: 12,
7+
Insert: (*ast.AlterProtoBundleInsert)(nil),
8+
Update: (*ast.AlterProtoBundleUpdate)(nil),
9+
Delete: (*ast.AlterProtoBundleDelete)(nil),
10+
}
11+
12+
--- SQL
13+
ALTER PROTO BUNDLE

testdata/result/ddl/alter_proto_bundle_update.sql.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
ALTER PROTO BUNDLE UPDATE(`examples.shipping.Order`)
33
--- AST
44
&ast.AlterProtoBundle{
5-
Alter: 0,
6-
Alteration: &ast.AlterProtoBundleUpdate{
5+
Alter: 0,
6+
Bundle: 12,
7+
Insert: (*ast.AlterProtoBundleInsert)(nil),
8+
Update: &ast.AlterProtoBundleUpdate{
79
Update: 19,
810
Types: &ast.ProtoBundleTypes{
911
Lparen: 25,
@@ -21,6 +23,7 @@ ALTER PROTO BUNDLE UPDATE(`examples.shipping.Order`)
2123
},
2224
},
2325
},
26+
Delete: (*ast.AlterProtoBundleDelete)(nil),
2427
}
2528

2629
--- SQL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--- alter_proto_bundle_all.sql
2+
ALTER PROTO BUNDLE INSERT (package.Inserted) UPDATE (package.Updated) DELETE (package.Deleted)
3+
--- AST
4+
&ast.AlterProtoBundle{
5+
Alter: 0,
6+
Bundle: 12,
7+
Insert: &ast.AlterProtoBundleInsert{
8+
Insert: 19,
9+
Types: &ast.ProtoBundleTypes{
10+
Lparen: 26,
11+
Rparen: 43,
12+
Types: []*ast.NamedType{
13+
&ast.NamedType{
14+
Path: []*ast.Ident{
15+
&ast.Ident{
16+
NamePos: 27,
17+
NameEnd: 34,
18+
Name: "package",
19+
},
20+
&ast.Ident{
21+
NamePos: 35,
22+
NameEnd: 43,
23+
Name: "Inserted",
24+
},
25+
},
26+
},
27+
},
28+
},
29+
},
30+
Update: &ast.AlterProtoBundleUpdate{
31+
Update: 45,
32+
Types: &ast.ProtoBundleTypes{
33+
Lparen: 52,
34+
Rparen: 68,
35+
Types: []*ast.NamedType{
36+
&ast.NamedType{
37+
Path: []*ast.Ident{
38+
&ast.Ident{
39+
NamePos: 53,
40+
NameEnd: 60,
41+
Name: "package",
42+
},
43+
&ast.Ident{
44+
NamePos: 61,
45+
NameEnd: 68,
46+
Name: "Updated",
47+
},
48+
},
49+
},
50+
},
51+
},
52+
},
53+
Delete: &ast.AlterProtoBundleDelete{
54+
Delete: 70,
55+
Types: &ast.ProtoBundleTypes{
56+
Lparen: 77,
57+
Rparen: 93,
58+
Types: []*ast.NamedType{
59+
&ast.NamedType{
60+
Path: []*ast.Ident{
61+
&ast.Ident{
62+
NamePos: 78,
63+
NameEnd: 85,
64+
Name: "package",
65+
},
66+
&ast.Ident{
67+
NamePos: 86,
68+
NameEnd: 93,
69+
Name: "Deleted",
70+
},
71+
},
72+
},
73+
},
74+
},
75+
},
76+
}
77+
78+
--- SQL
79+
ALTER PROTO BUNDLE INSERT (package.Inserted) UPDATE (package.Updated) DELETE (package.Deleted)

0 commit comments

Comments
 (0)