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 DDLs for Protocol Buffers #176

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
105 changes: 105 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (CreateSchema) isStatement() {}
func (DropSchema) isStatement() {}
func (CreateDatabase) isStatement() {}
func (AlterDatabase) isStatement() {}
func (CreateProtoBundle) isStatement() {}
func (AlterProtoBundle) isStatement() {}
func (DropProtoBundle) isStatement() {}
func (CreateTable) isStatement() {}
func (AlterTable) isStatement() {}
func (DropTable) isStatement() {}
Expand Down Expand Up @@ -303,6 +306,9 @@ func (CreateSchema) isDDL() {}
func (DropSchema) isDDL() {}
func (CreateDatabase) isDDL() {}
func (AlterDatabase) isDDL() {}
func (CreateProtoBundle) isDDL() {}
func (AlterProtoBundle) isDDL() {}
func (DropProtoBundle) isDDL() {}
func (CreateTable) isDDL() {}
func (AlterTable) isDDL() {}
func (DropTable) isDDL() {}
Expand Down Expand Up @@ -1819,6 +1825,105 @@ type AlterDatabase struct {
Options *Options
}

// ================================================================================
//
// PROTO BUNDLE statements
//
// ================================================================================

type ProtoBundleAlteration interface {
Node
isProtoBundleAlteration()
}

func (AlterProtoBundleInsert) isProtoBundleAlteration() {}
func (AlterProtoBundleUpdate) isProtoBundleAlteration() {}
func (AlterProtoBundleDelete) isProtoBundleAlteration() {}

// ProtoBundleTypes is parenthesized Protocol Buffers type names node IN CREATE/ALTER PROTO BUNDLE statement.
//
// ({{.Types | sqlJoin ", "}})
type ProtoBundleTypes struct {
// pos = Lparen
// end = Rparen + 1

Lparen, Rparen token.Pos
Types []*NamedType
}

// CreateProtoBundle is CREATE PROTO BUNDLE statement node.
//
// CREATE PROTO BUNDLE {{.Types, | sql}}
type CreateProtoBundle struct {
// pos = Create
// end = Types.end

Create token.Pos // position of "CREATE" keyword

Types *ProtoBundleTypes
}

// AlterProtoBundle is ALTER PROTO BUNDLE statement node.
//
// ALTER PROTO BUNDLE {{.Alteration | sql}}
type AlterProtoBundle struct {
// pos = Alter
// end = Alteration.end

Alter token.Pos // position of "ALTER" keyword

Alteration ProtoBundleAlteration
}

// AlterProtoBundleInsert is INSERT (proto_type_name, ...) node in ALTER PROTO BUNDLE.
//
// INSERT {{.Types | sql}}
type AlterProtoBundleInsert struct {
// pos = Insert
// end = Types.end

Insert token.Pos // position of "INSERT" pseudo keyword

Types *ProtoBundleTypes
}

// AlterProtoBundleUpdate is UPDATE (proto_type_name, ...) node in ALTER PROTO BUNDLE.
//
// UPDATE {{.Types | sql}}
type AlterProtoBundleUpdate struct {
// pos = Update
// end = Types.end

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

Types *ProtoBundleTypes
}

// AlterProtoBundleDelete is DELETE (proto_type_name, ...) node in ALTER PROTO BUNDLE.
//
// DELETE {{.Types | sql}}
type AlterProtoBundleDelete struct {
// pos = Delete
// end = Types.end

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

Types *ProtoBundleTypes
}

// DropProtoBundle is DROP PROTO BUNDLE statement node.
//
// DROP PROTO BUNDLE
type DropProtoBundle struct {
// pos = Drop
// end = Bundle + 6

Drop token.Pos // position of "DROP" pseudo keyword
Bundle token.Pos // position of "BUNDLE" pseudo keyword
}

// end of PROTO BUNDLE statements

// CreateTable is CREATE TABLE statement node.
//
// CREATE TABLE {{if .IfNotExists}}IF NOT EXISTS{{end}} {{.Name | sql}} (
Expand Down
56 changes: 56 additions & 0 deletions ast/pos.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,20 @@ func (d *AlterDatabase) SQL() string {
return "ALTER DATABASE " + d.Name.SQL() + " SET " + d.Options.SQL()
}

func (p *ProtoBundleTypes) SQL() string { return "(" + sqlJoin(p.Types, ", ") + ")" }

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

func (a *AlterProtoBundle) SQL() string { return "ALTER PROTO BUNDLE " + a.Alteration.SQL() }

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

func (a *AlterProtoBundleUpdate) SQL() string { return "UPDATE " + a.Types.SQL() }

func (a *AlterProtoBundleDelete) SQL() string { return "DELETE " + a.Types.SQL() }

func (d *DropProtoBundle) SQL() string { return "DROP PROTO BUNDLE" }

func (c *CreateTable) SQL() string {
return "CREATE TABLE " +
strOpt(c.IfNotExists, "IF NOT EXISTS ") +
Expand Down
78 changes: 78 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,8 @@ func (p *Parser) parseDDL() ast.DDL {
return p.parseCreateSchema(pos)
case p.Token.IsKeywordLike("DATABASE"):
return p.parseCreateDatabase(pos)
case p.Token.Kind == "PROTO":
return p.parseCreateProtoBundle(pos)
case p.Token.IsKeywordLike("TABLE"):
return p.parseCreateTable(pos)
case p.Token.IsKeywordLike("SEQUENCE"):
Expand All @@ -2328,6 +2330,8 @@ func (p *Parser) parseDDL() ast.DDL {
return p.parseAlterTable(pos)
case p.Token.IsKeywordLike("DATABASE"):
return p.parseAlterDatabase(pos)
case p.Token.Kind == "PROTO":
return p.parseAlterProtoBundle(pos)
case p.Token.IsKeywordLike("INDEX"):
return p.parseAlterIndex(pos)
case p.Token.IsKeywordLike("SEARCH"):
Expand All @@ -2345,6 +2349,8 @@ func (p *Parser) parseDDL() ast.DDL {
switch {
case p.Token.IsKeywordLike("SCHEMA"):
return p.parseDropSchema(pos)
case p.Token.Kind == "PROTO":
return p.parseDropProtoBundle(pos)
case p.Token.IsKeywordLike("TABLE"):
return p.parseDropTable(pos)
case p.Token.IsKeywordLike("INDEX"):
Expand Down Expand Up @@ -2421,6 +2427,47 @@ func (p *Parser) parseAlterDatabase(pos token.Pos) *ast.AlterDatabase {
}
}

func (p *Parser) parseProtoBundleTypes() *ast.ProtoBundleTypes {
lparen := p.expect("(").Pos
types := parseCommaSeparatedList(p, p.parseNamedType)
rparen := p.expect(")").Pos
return &ast.ProtoBundleTypes{
Lparen: lparen,
Rparen: rparen,
Types: types,
}
}

func (p *Parser) parseCreateProtoBundle(pos token.Pos) *ast.CreateProtoBundle {
p.expect("PROTO")
p.expectKeywordLike("BUNDLE")
types := p.parseProtoBundleTypes()

return &ast.CreateProtoBundle{
Create: pos,
Types: types,
}
}

func (p *Parser) parseAlterProtoBundle(pos token.Pos) *ast.AlterProtoBundle {
p.expect("PROTO")
p.expectKeywordLike("BUNDLE")
alteration := p.parseProtoBundleAlteration()
return &ast.AlterProtoBundle{
Alter: pos,
Alteration: alteration,
}
}

func (p *Parser) parseDropProtoBundle(pos token.Pos) *ast.DropProtoBundle {
p.expect("PROTO")
bundle := p.expectKeywordLike("BUNDLE").Pos

return &ast.DropProtoBundle{
Drop: pos,
Bundle: bundle,
}
}
func (p *Parser) parseCreateTable(pos token.Pos) *ast.CreateTable {
p.expectKeywordLike("TABLE")
ifNotExists := p.parseIfNotExists()
Expand Down Expand Up @@ -4194,3 +4241,34 @@ func (p *Parser) parseRenameTable(pos token.Pos) *ast.RenameTable {
}

}

func (p *Parser) parseProtoBundleAlteration() ast.ProtoBundleAlteration {
switch {
case p.Token.IsKeywordLike("INSERT"):
insert := p.expectKeywordLike("INSERT").Pos
types := p.parseProtoBundleTypes()

return &ast.AlterProtoBundleInsert{
Insert: insert,
Types: types,
}
case p.Token.IsKeywordLike("UPDATE"):
update := p.expectKeywordLike("UPDATE").Pos
types := p.parseProtoBundleTypes()

return &ast.AlterProtoBundleUpdate{
Update: update,
Types: types,
}
case p.Token.IsKeywordLike("DELETE"):
delete := p.expectKeywordLike("DELETE").Pos
types := p.parseProtoBundleTypes()

return &ast.AlterProtoBundleDelete{
Delete: delete,
Types: types,
}
default:
panic(p.errorfAtToken(&p.Token, `expected INSERT, UPDATE or DELETE, but: %v`, p.Token.AsString))
}
}
1 change: 1 addition & 0 deletions testdata/input/ddl/alter_proto_bundle_delete.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER PROTO BUNDLE DELETE(`examples.shipping.OrderHistory`)
3 changes: 3 additions & 0 deletions testdata/input/ddl/alter_proto_bundle_insert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER PROTO BUNDLE INSERT (
examples.shipping.OrderHistory
)
1 change: 1 addition & 0 deletions testdata/input/ddl/alter_proto_bundle_update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER PROTO BUNDLE UPDATE(`examples.shipping.Order`)
6 changes: 6 additions & 0 deletions testdata/input/ddl/create_proto_bundle_backquoted.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- If you're using a protocol buffer type and any part of the type name is a Spanner reserved keyword,
-- enclose the entire protocol buffer type name in backticks.
CREATE PROTO BUNDLE (
`examples.shipping.Order`,
`examples.shipping.Order.Address`,
`examples.shipping.Order.Item`)
1 change: 1 addition & 0 deletions testdata/input/ddl/drop_proto_bunldle.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP PROTO BUNDLE
27 changes: 27 additions & 0 deletions testdata/result/ddl/alter_proto_bundle_delete.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--- alter_proto_bundle_delete.sql
ALTER PROTO BUNDLE DELETE(`examples.shipping.OrderHistory`)
--- AST
&ast.AlterProtoBundle{
Alter: 0,
Alteration: &ast.AlterProtoBundleDelete{
Delete: 19,
Types: &ast.ProtoBundleTypes{
Lparen: 25,
Rparen: 58,
Types: []*ast.NamedType{
&ast.NamedType{
Path: []*ast.Ident{
&ast.Ident{
NamePos: 26,
NameEnd: 58,
Name: "examples.shipping.OrderHistory",
},
},
},
},
},
},
}

--- SQL
ALTER PROTO BUNDLE DELETE (`examples.shipping.OrderHistory`)
Loading
Loading