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

Implement ALTER DATABASE statement #161

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
15 changes: 15 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Statement interface {

func (QueryStatement) isStatement() {}
func (CreateDatabase) isStatement() {}
func (AlterDatabase) isStatement() {}
func (CreateTable) isStatement() {}
func (AlterTable) isStatement() {}
func (DropTable) isStatement() {}
Expand Down Expand Up @@ -265,6 +266,7 @@ type DDL interface {
// - https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language

func (CreateDatabase) isDDL() {}
func (AlterDatabase) isDDL() {}
func (CreateTable) isDDL() {}
func (AlterTable) isDDL() {}
func (DropTable) isDDL() {}
Expand Down Expand Up @@ -1636,6 +1638,19 @@ type CreateDatabase struct {
Name *Ident
}

// AlterDatabase is ALTER DATABASE statement node.
//
// ALTER DATABASE {{.Name | sql}} SET {{.Options | sql}}
type AlterDatabase struct {
// pos = Alter
// end = Name.end

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

Name *Ident
Options *Options
}

// CreateTable is CREATE TABLE statement node.
//
// CREATE TABLE {{if .IfNotExists}}IF NOT EXISTS{{end}} {{.Name | sql}} (
Expand Down
2 changes: 2 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
func TestStatement(t *testing.T) {
Statement(&QueryStatement{}).isStatement()
Statement(&CreateDatabase{}).isStatement()
Statement(&AlterDatabase{}).isStatement()
Statement(&CreateTable{}).isStatement()
Statement(&AlterTable{}).isStatement()
Statement(&DropTable{}).isStatement()
Expand Down Expand Up @@ -136,6 +137,7 @@ func TestStringValue(t *testing.T) {

func TestDDL(t *testing.T) {
DDL(&CreateDatabase{}).isDDL()
DDL(&AlterDatabase{}).isDDL()
DDL(&CreateTable{}).isDDL()
DDL(&AlterTable{}).isDDL()
DDL(&DropTable{}).isDDL()
Expand Down
3 changes: 3 additions & 0 deletions ast/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,9 @@ func (g *OptionsDef) End() token.Pos { return g.Value.End() }
func (c *CreateDatabase) Pos() token.Pos { return c.Create }
func (c *CreateDatabase) End() token.Pos { return c.Name.End() }

func (d *AlterDatabase) Pos() token.Pos { return d.Alter }
func (d *AlterDatabase) End() token.Pos { return d.Options.End() }

func (c *CreateTable) Pos() token.Pos {
return c.Create
}
Expand Down
4 changes: 4 additions & 0 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,10 @@ func (c *CreateDatabase) SQL() string {
return "CREATE DATABASE " + c.Name.SQL()
}

func (d *AlterDatabase) SQL() string {
return "ALTER DATABASE " + d.Name.SQL() + " SET " + d.Options.SQL()
}

func (c *CreateTable) SQL() string {
return "CREATE TABLE " +
strOpt(c.IfNotExists, "IF NOT EXISTS ") +
Expand Down
15 changes: 15 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2171,6 +2171,8 @@ func (p *Parser) parseDDL() ast.DDL {
switch {
case p.Token.IsKeywordLike("TABLE"):
return p.parseAlterTable(pos)
case p.Token.IsKeywordLike("DATABASE"):
return p.parseAlterDatabase(pos)
case p.Token.IsKeywordLike("INDEX"):
return p.parseAlterIndex(pos)
case p.Token.IsKeywordLike("SEQUENCE"):
Expand Down Expand Up @@ -2222,6 +2224,19 @@ func (p *Parser) parseCreateDatabase(pos token.Pos) *ast.CreateDatabase {
}
}

func (p *Parser) parseAlterDatabase(pos token.Pos) *ast.AlterDatabase {
p.expectKeywordLike("DATABASE")
name := p.parseIdent()
p.expect("SET")
options := p.parseOptions()

return &ast.AlterDatabase{
Alter: pos,
Name: name,
Options: options,
}
}

func (p *Parser) parseCreateTable(pos token.Pos) *ast.CreateTable {
p.expectKeywordLike("TABLE")
ifNotExists := p.parseIfNotExists()
Expand Down
7 changes: 7 additions & 0 deletions testdata/input/ddl/alter_database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER DATABASE dbname SET OPTIONS (
optimizer_version=2,
optimizer_statistics_package='auto_20191128_14_47_22UTC',
version_retention_period='7d',
enable_key_visualizer=true,
default_leader='europe-west1'
)
86 changes: 86 additions & 0 deletions testdata/result/ddl/alter_database.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--- alter_database.sql
ALTER DATABASE dbname SET OPTIONS (
optimizer_version=2,
optimizer_statistics_package='auto_20191128_14_47_22UTC',
version_retention_period='7d',
enable_key_visualizer=true,
default_leader='europe-west1'
)
--- AST
&ast.AlterDatabase{
Alter: 0,
Name: &ast.Ident{
NamePos: 15,
NameEnd: 21,
Name: "dbname",
},
Options: &ast.Options{
Options: 26,
Rparen: 226,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 40,
NameEnd: 57,
Name: "optimizer_version",
},
Value: &ast.IntLiteral{
ValuePos: 58,
ValueEnd: 59,
Base: 10,
Value: "2",
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 65,
NameEnd: 93,
Name: "optimizer_statistics_package",
},
Value: &ast.StringLiteral{
ValuePos: 94,
ValueEnd: 121,
Value: "auto_20191128_14_47_22UTC",
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 127,
NameEnd: 151,
Name: "version_retention_period",
},
Value: &ast.StringLiteral{
ValuePos: 152,
ValueEnd: 156,
Value: "7d",
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 162,
NameEnd: 183,
Name: "enable_key_visualizer",
},
Value: &ast.BoolLiteral{
ValuePos: 184,
Value: true,
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 194,
NameEnd: 208,
Name: "default_leader",
},
Value: &ast.StringLiteral{
ValuePos: 209,
ValueEnd: 223,
Value: "europe-west1",
},
},
},
},
}

--- SQL
ALTER DATABASE dbname SET OPTIONS (optimizer_version = 2, optimizer_statistics_package = "auto_20191128_14_47_22UTC", version_retention_period = "7d", enable_key_visualizer = true, default_leader = "europe-west1")
86 changes: 86 additions & 0 deletions testdata/result/statement/alter_database.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
--- alter_database.sql
ALTER DATABASE dbname SET OPTIONS (
optimizer_version=2,
optimizer_statistics_package='auto_20191128_14_47_22UTC',
version_retention_period='7d',
enable_key_visualizer=true,
default_leader='europe-west1'
)
--- AST
&ast.AlterDatabase{
Alter: 0,
Name: &ast.Ident{
NamePos: 15,
NameEnd: 21,
Name: "dbname",
},
Options: &ast.Options{
Options: 26,
Rparen: 226,
Records: []*ast.OptionsDef{
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 40,
NameEnd: 57,
Name: "optimizer_version",
},
Value: &ast.IntLiteral{
ValuePos: 58,
ValueEnd: 59,
Base: 10,
Value: "2",
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 65,
NameEnd: 93,
Name: "optimizer_statistics_package",
},
Value: &ast.StringLiteral{
ValuePos: 94,
ValueEnd: 121,
Value: "auto_20191128_14_47_22UTC",
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 127,
NameEnd: 151,
Name: "version_retention_period",
},
Value: &ast.StringLiteral{
ValuePos: 152,
ValueEnd: 156,
Value: "7d",
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 162,
NameEnd: 183,
Name: "enable_key_visualizer",
},
Value: &ast.BoolLiteral{
ValuePos: 184,
Value: true,
},
},
&ast.OptionsDef{
Name: &ast.Ident{
NamePos: 194,
NameEnd: 208,
Name: "default_leader",
},
Value: &ast.StringLiteral{
ValuePos: 209,
ValueEnd: 223,
Value: "europe-west1",
},
},
},
},
}

--- SQL
ALTER DATABASE dbname SET OPTIONS (optimizer_version = 2, optimizer_statistics_package = "auto_20191128_14_47_22UTC", version_retention_period = "7d", enable_key_visualizer = true, default_leader = "europe-west1")
Loading