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

add alter view #1375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
91 changes: 91 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
var (
_ DDLNode = &AlterTableStmt{}
_ DDLNode = &AlterSequenceStmt{}
_ DDLNode = &AlterViewStmt{}
_ DDLNode = &AlterPlacementPolicyStmt{}
_ DDLNode = &CreateDatabaseStmt{}
_ DDLNode = &CreateIndexStmt{}
Expand Down Expand Up @@ -1106,6 +1107,9 @@ func (n *CreateTableStmt) Accept(v Visitor) (Node, bool) {
n.Cols[i] = node.(*ColumnDef)
}
for i, val := range n.Constraints {
if val.Refer != nil && val.Refer.Table.Schema.String() == "" {
val.Refer.Table.Schema = n.Table.Schema
}
node, ok = val.Accept(v)
if !ok {
return n, false
Expand Down Expand Up @@ -4018,3 +4022,90 @@ func (n *AlterSequenceStmt) Accept(v Visitor) (Node, bool) {
n.Name = node.(*TableName)
return v.Leave(n)
}

type AlterViewStmt struct {
ddlNode

Algorithm model.ViewAlgorithm
Definer *auth.UserIdentity
Security model.ViewSecurity
ViewName *TableName
Cols []model.CIStr
Select StmtNode
SchemaCols []model.CIStr
CheckOption model.ViewCheckOption
}

// Restore implements Node interface.
func (n *AlterViewStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("ALTER ")
ctx.WriteKeyWord("ALGORITHM")
ctx.WritePlain(" = ")
ctx.WriteKeyWord(n.Algorithm.String())
ctx.WriteKeyWord(" DEFINER")
ctx.WritePlain(" = ")

// todo Use n.Definer.Restore(ctx) to replace this part
if n.Definer.CurrentUser {
ctx.WriteKeyWord("current_user")
} else {
ctx.WriteName(n.Definer.Username)
if n.Definer.Hostname != "" {
ctx.WritePlain("@")
ctx.WriteName(n.Definer.Hostname)
}
}

ctx.WriteKeyWord(" SQL SECURITY ")
ctx.WriteKeyWord(n.Security.String())
ctx.WriteKeyWord(" VIEW ")

if err := n.ViewName.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while create AlterViewStmt.ViewName")
}

for i, col := range n.Cols {
if i == 0 {
ctx.WritePlain(" (")
} else {
ctx.WritePlain(",")
}
ctx.WriteName(col.O)
if i == len(n.Cols)-1 {
ctx.WritePlain(")")
}
}

ctx.WriteKeyWord(" AS ")

if err := n.Select.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while create AlterViewStmt.Select")
}

if n.CheckOption != model.CheckOptionCascaded {
ctx.WriteKeyWord(" WITH ")
ctx.WriteKeyWord(n.CheckOption.String())
ctx.WriteKeyWord(" CHECK OPTION")
}
return nil
}

// Accept implements Node Accept interface.
func (n *AlterViewStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*AlterViewStmt)
node, ok := n.ViewName.Accept(v)
if !ok {
return n, false
}
n.ViewName = node.(*TableName)
selnode, ok := n.Select.Accept(v)
if !ok {
return n, false
}
n.Select = selnode.(StmtNode)
return v.Leave(n)
}
Loading