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 ddl for schema and database #106

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
332 changes: 263 additions & 69 deletions postgres/parser/parser/sql.y

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions postgres/parser/sem/tree/alter_default_privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ var _ Statement = &AlterDefaultPrivileges{}

// AlterDefaultPrivileges represents a ALTER DEFAULT PRIVILEGES statement.
type AlterDefaultPrivileges struct {
ForRole bool
TargetRoles []string
Privileges privilege.List
Target TargetList
Grantees []string
GrantOption bool
Restrict bool
Grant bool
ForRole bool
TargetRoles []string
Privileges privilege.List
Target TargetList
Grantees []string
GrantOption bool
DropBehavior DropBehavior
Grant bool
}

// Format implements the NodeFormatter interface.
Expand Down Expand Up @@ -77,10 +77,9 @@ func (node *AlterDefaultPrivileges) Format(ctx *FmtCtx) {
node.Privileges.Format(&ctx.Buffer)
ctx.WriteString(fmt.Sprintf(" ON %sS FROM ", strings.ToUpper(string(node.Target.TargetType))))
ctx.WriteString(strings.Join(node.Grantees, ", "))
if node.Restrict {
ctx.WriteString(" RESTRICT")
} else {
ctx.WriteString(" CASCADE")
if node.DropBehavior.String() != "" {
ctx.WriteByte(' ')
ctx.WriteString(node.DropBehavior.String())
}
}
}
86 changes: 77 additions & 9 deletions postgres/parser/sem/tree/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,24 @@ import (

// CreateDatabase represents a CREATE DATABASE statement.
type CreateDatabase struct {
IfNotExists bool
Name Name
Template string
Encoding string
Collate string
CType string
IfNotExists bool
Name Name
Owner string
Template string
Encoding string
Strategy string
Locale string
Collate string
CType string
IcuLocale string
IcuRules string
LocaleProvider string
CollationVersion string
Tablespace string
AllowConnections Expr // default is true
ConnectionLimit Expr // default is -1
IsTemplate Expr // default is false
Oid Expr
}

// Format implements the NodeFormatter interface.
Expand All @@ -65,6 +77,10 @@ func (node *CreateDatabase) Format(ctx *FmtCtx) {
ctx.WriteString("IF NOT EXISTS ")
}
ctx.FormatNode(&node.Name)
if node.Owner != "" {
ctx.WriteString(" OWNER = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Owner, ctx.flags.EncodeFlags())
}
if node.Template != "" {
ctx.WriteString(" TEMPLATE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Template, ctx.flags.EncodeFlags())
Expand All @@ -73,6 +89,14 @@ func (node *CreateDatabase) Format(ctx *FmtCtx) {
ctx.WriteString(" ENCODING = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Encoding, ctx.flags.EncodeFlags())
}
if node.Strategy != "" {
ctx.WriteString(" STRATEGY = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Strategy, ctx.flags.EncodeFlags())
}
if node.Locale != "" {
ctx.WriteString(" LOCALE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Locale, ctx.flags.EncodeFlags())
}
if node.Collate != "" {
ctx.WriteString(" LC_COLLATE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Collate, ctx.flags.EncodeFlags())
Expand All @@ -81,6 +105,42 @@ func (node *CreateDatabase) Format(ctx *FmtCtx) {
ctx.WriteString(" LC_CTYPE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.CType, ctx.flags.EncodeFlags())
}
if node.IcuLocale != "" {
ctx.WriteString(" ICU_LOCALE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.IcuLocale, ctx.flags.EncodeFlags())
}
if node.IcuRules != "" {
ctx.WriteString(" ICU_RULES = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.IcuRules, ctx.flags.EncodeFlags())
}
if node.LocaleProvider != "" {
ctx.WriteString(" LOCALE_PROVIDER = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.LocaleProvider, ctx.flags.EncodeFlags())
}
if node.CollationVersion != "" {
ctx.WriteString(" COLLATION_VERSION = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.CollationVersion, ctx.flags.EncodeFlags())
}
if node.Tablespace != "" {
ctx.WriteString(" TABLESPACE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Tablespace, ctx.flags.EncodeFlags())
}
if node.AllowConnections != nil {
ctx.WriteString(" ALLOW_CONNECTIONS = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.AllowConnections.String(), ctx.flags.EncodeFlags())
}
if node.ConnectionLimit != nil {
ctx.WriteString(" CONNECTION LIMIT = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.ConnectionLimit.String(), ctx.flags.EncodeFlags())
}
if node.IsTemplate != nil {
ctx.WriteString(" IS_TEMPLATE = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.IsTemplate.String(), ctx.flags.EncodeFlags())
}
if node.Oid != nil {
ctx.WriteString(" OID = ")
lex.EncodeSQLStringWithFlags(&ctx.Buffer, node.Oid.String(), ctx.flags.EncodeFlags())
}
}

// IndexElem represents a column with a direction in a CREATE INDEX statement.
Expand Down Expand Up @@ -1227,9 +1287,10 @@ func (node *CreateTable) HoistConstraints() {

// CreateSchema represents a CREATE SCHEMA statement.
type CreateSchema struct {
IfNotExists bool
Schema string
AuthRole string
IfNotExists bool
Schema string
AuthRole string
SchemaElements []Statement
}

// Format implements the NodeFormatter interface.
Expand All @@ -1249,6 +1310,13 @@ func (node *CreateSchema) Format(ctx *FmtCtx) {
ctx.WriteString(" AUTHORIZATION ")
ctx.WriteString(node.AuthRole)
}

if node.SchemaElements != nil {
for _, se := range node.SchemaElements {
ctx.WriteByte(' ')
ctx.FormatNode(se)
}
}
}

// CreateSequence represents a CREATE SEQUENCE statement.
Expand Down
11 changes: 5 additions & 6 deletions postgres/parser/sem/tree/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func (d DropBehavior) String() string {

// DropDatabase represents a DROP DATABASE statement.
type DropDatabase struct {
Name Name
IfExists bool
DropBehavior DropBehavior
Name Name
IfExists bool
Force bool
}

// Format implements the NodeFormatter interface.
Expand All @@ -67,9 +67,8 @@ func (node *DropDatabase) Format(ctx *FmtCtx) {
ctx.WriteString("IF EXISTS ")
}
ctx.FormatNode(&node.Name)
if node.DropBehavior != DropDefault {
ctx.WriteByte(' ')
ctx.WriteString(node.DropBehavior.String())
if node.Force {
ctx.WriteString(" WITH ( FORCE )")
}
}

Expand Down
26 changes: 12 additions & 14 deletions postgres/parser/sem/tree/revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Revoke struct {
Grantees []string
GrantOptionFor bool
GrantedBy string
Restrict bool
DropBehavior DropBehavior

// only used for table target with column names defined
PrivsWithCols []PrivForCols
Expand Down Expand Up @@ -80,20 +80,19 @@ func (node *Revoke) Format(ctx *FmtCtx) {
ctx.WriteString(" GRANTED BY ")
ctx.WriteString(node.GrantedBy)
}
if node.Restrict {
ctx.WriteString(" RESTRICT")
} else {
ctx.WriteString(" CASCADE")
if node.DropBehavior.String() != "" {
ctx.WriteByte(' ')
ctx.WriteString(node.DropBehavior.String())
}
}

// RevokeRole represents a REVOKE <role> statement.
type RevokeRole struct {
Roles NameList
Members []string
Option string
GrantedBy string
Restrict bool
Roles NameList
Members []string
Option string
GrantedBy string
DropBehavior DropBehavior
}

// Format implements the NodeFormatter interface.
Expand All @@ -110,9 +109,8 @@ func (node *RevokeRole) Format(ctx *FmtCtx) {
ctx.WriteString(" GRANTED BY ")
ctx.WriteString(node.GrantedBy)
}
if node.Restrict {
ctx.WriteString(" RESTRICT")
} else {
ctx.WriteString(" CASCADE")
if node.DropBehavior.String() != "" {
ctx.WriteByte(' ')
ctx.WriteString(node.DropBehavior.String())
}
}
45 changes: 41 additions & 4 deletions server/ast/create_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,55 @@ import (

// nodeCreateDatabase handles *tree.CreateDatabase nodes.
func nodeCreateDatabase(node *tree.CreateDatabase) (*vitess.DBDDL, error) {
if len(node.Owner) > 0 {
return nil, fmt.Errorf("OWNER clause is not yet supported")
}
if len(node.Template) > 0 {
return nil, fmt.Errorf("templates are not yet supported")
return nil, fmt.Errorf("TEMPLATE clause is not yet supported")
}
if len(node.Encoding) > 0 {
return nil, fmt.Errorf("encodings are not yet supported")
return nil, fmt.Errorf("ENCODING clause is not yet supported")
}
if len(node.Strategy) > 0 {
return nil, fmt.Errorf("STRATEGY clause is not yet supported")
}
if len(node.Locale) > 0 {
return nil, fmt.Errorf("LOCALE clause is not yet supported")
}
if len(node.Collate) > 0 {
return nil, fmt.Errorf("collations are not yet supported")
return nil, fmt.Errorf("LC_COLLATE clause is not yet supported")
}
if len(node.CType) > 0 {
return nil, fmt.Errorf("ctypes are not yet supported")
return nil, fmt.Errorf("LC_CTYPE clause is not yet supported")
}
if len(node.IcuLocale) > 0 {
return nil, fmt.Errorf("ICU_LOCALE clause is not yet supported")
}
if len(node.IcuRules) > 0 {
return nil, fmt.Errorf("TEMPLATE clause is not yet supported")
}
if len(node.LocaleProvider) > 0 {
return nil, fmt.Errorf("LOCALE_PROVIDER clause is not yet supported")
}
if len(node.CollationVersion) > 0 {
return nil, fmt.Errorf("COLLATION_VERSION clause is not yet supported")
}
if len(node.Tablespace) > 0 {
return nil, fmt.Errorf("TABLESPACE clause is not yet supported")
}
if node.AllowConnections != nil {
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("ALLOW_CONNECTIONS clause is not yet supported")
}
if node.ConnectionLimit != nil {
return nil, fmt.Errorf("CONNECTION LIMIT clause is not yet supported")
}
if node.IsTemplate != nil {
return nil, fmt.Errorf("IS_TEMPLATE clause is not yet supported")
}
if node.Oid != nil {
return nil, fmt.Errorf("OID clause is not yet supported")
}

return &vitess.DBDDL{
Action: vitess.CreateStr,
DBName: node.Name.String(),
Expand Down
9 changes: 2 additions & 7 deletions server/ast/drop_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@ func nodeDropDatabase(node *tree.DropDatabase) (*vitess.DBDDL, error) {
if node == nil {
return nil, nil
}
switch node.DropBehavior {
case tree.DropDefault:
// Default behavior, nothing to do
case tree.DropRestrict:
return nil, fmt.Errorf("RESTRICT is not yet supported")
case tree.DropCascade:
return nil, fmt.Errorf("CASCADE is not yet supported")
if node.Force {
return nil, fmt.Errorf("WITH ( FORCE ) is not yet supported")
}
return &vitess.DBDDL{
Action: vitess.DropStr,
Expand Down
Loading
Loading