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 SCHEMA DDL statements #179

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
28 changes: 28 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type Statement interface {
// - https://cloud.google.com/spanner/docs/reference/standard-sql/dml-syntax

func (QueryStatement) isStatement() {}
func (CreateSchema) isStatement() {}
func (DropSchema) isStatement() {}
func (CreateDatabase) isStatement() {}
func (AlterDatabase) isStatement() {}
func (CreateTable) isStatement() {}
Expand Down Expand Up @@ -274,6 +276,8 @@ type DDL interface {
//
// - https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language

func (CreateSchema) isDDL() {}
func (DropSchema) isDDL() {}
func (CreateDatabase) isDDL() {}
func (AlterDatabase) isDDL() {}
func (CreateTable) isDDL() {}
Expand Down Expand Up @@ -1641,6 +1645,30 @@ type OptionsDef struct {
Value Expr
}

// CreateSchema is CREATE SCHEMA statement node.
//
// CREATE SCHEMA {{.Name | sql}}
type CreateSchema struct {
// pos = Create
// end = Name.end

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

Name *Ident
}

// DropSchema is DROP SCHEMA statement node.
//
// DROP SCHEMA {{.Name | sql}}
type DropSchema struct {
// pos = Drop
// end = Name.end

Drop token.Pos // position of "DROP" keyword

Name *Ident
}

// CreateDatabase is CREATE DATABASE statement node.
//
// CREATE DATABASE {{.Name | sql}}
Expand Down
16 changes: 16 additions & 0 deletions ast/pos.go

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

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 (s *CreateSchema) SQL() string { return "CREATE SCHEMA " + s.Name.SQL() }

func (s *DropSchema) SQL() string { return "DROP SCHEMA " + s.Name.SQL() }

func (d *AlterDatabase) SQL() string {
return "ALTER DATABASE " + d.Name.SQL() + " SET " + d.Options.SQL()
}
Expand Down
22 changes: 22 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,8 @@ func (p *Parser) parseDDL() ast.DDL {
case p.Token.Kind == "CREATE":
p.nextToken()
switch {
case p.Token.IsKeywordLike("SCHEMA"):
return p.parseCreateSchema(pos)
case p.Token.IsKeywordLike("DATABASE"):
return p.parseCreateDatabase(pos)
case p.Token.IsKeywordLike("TABLE"):
Expand Down Expand Up @@ -2192,6 +2194,8 @@ func (p *Parser) parseDDL() ast.DDL {
case p.Token.IsKeywordLike("DROP"):
p.nextToken()
switch {
case p.Token.IsKeywordLike("SCHEMA"):
return p.parseDropSchema(pos)
case p.Token.IsKeywordLike("TABLE"):
return p.parseDropTable(pos)
case p.Token.IsKeywordLike("INDEX"):
Expand Down Expand Up @@ -2228,6 +2232,24 @@ func (p *Parser) parseDDL() ast.DDL {
panic(p.errorfAtToken(&p.Token, "expected pseudo keyword: ALTER, DROP, but: %s", p.Token.AsString))
}

func (p *Parser) parseCreateSchema(pos token.Pos) *ast.CreateSchema {
p.expectKeywordLike("SCHEMA")
name := p.parseIdent()
return &ast.CreateSchema{
Create: pos,
Name: name,
}
}

func (p *Parser) parseDropSchema(pos token.Pos) *ast.DropSchema {
p.expectKeywordLike("SCHEMA")
name := p.parseIdent()
return &ast.DropSchema{
Drop: pos,
Name: name,
}
}

func (p *Parser) parseCreateDatabase(pos token.Pos) *ast.CreateDatabase {
p.expectKeywordLike("DATABASE")
name := p.parseIdent()
Expand Down
1 change: 1 addition & 0 deletions testdata/input/ddl/create_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SCHEMA sch1
1 change: 1 addition & 0 deletions testdata/input/ddl/drop_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP SCHEMA sch1
14 changes: 14 additions & 0 deletions testdata/result/ddl/create_schema.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- create_schema.sql
CREATE SCHEMA sch1
--- AST
&ast.CreateSchema{
Create: 0,
Name: &ast.Ident{
NamePos: 14,
NameEnd: 18,
Name: "sch1",
},
}

--- SQL
CREATE SCHEMA sch1
14 changes: 14 additions & 0 deletions testdata/result/ddl/drop_schema.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- drop_schema.sql
DROP SCHEMA sch1
--- AST
&ast.DropSchema{
Drop: 0,
Name: &ast.Ident{
NamePos: 12,
NameEnd: 16,
Name: "sch1",
},
}

--- SQL
DROP SCHEMA sch1
14 changes: 14 additions & 0 deletions testdata/result/statement/create_schema.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- create_schema.sql
CREATE SCHEMA sch1
--- AST
&ast.CreateSchema{
Create: 0,
Name: &ast.Ident{
NamePos: 14,
NameEnd: 18,
Name: "sch1",
},
}

--- SQL
CREATE SCHEMA sch1
14 changes: 14 additions & 0 deletions testdata/result/statement/drop_schema.sql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- drop_schema.sql
DROP SCHEMA sch1
--- AST
&ast.DropSchema{
Drop: 0,
Name: &ast.Ident{
NamePos: 12,
NameEnd: 16,
Name: "sch1",
},
}

--- SQL
DROP SCHEMA sch1
Loading