github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/alter.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tree
    16  
    17  import "fmt"
    18  
    19  type AlterUser struct {
    20  	statementImpl
    21  	IfExists bool
    22  	Users    []*User
    23  	Role     *Role
    24  	MiscOpt  UserMiscOption
    25  	// comment or attribute
    26  	CommentOrAttribute AccountCommentOrAttribute
    27  }
    28  
    29  func (node *AlterUser) Format(ctx *FmtCtx) {
    30  	ctx.WriteString("alter user")
    31  	if node.IfExists {
    32  		ctx.WriteString(" if exists")
    33  	}
    34  	if node.Users != nil {
    35  		prefix := " "
    36  		for _, u := range node.Users {
    37  			ctx.WriteString(prefix)
    38  			u.Format(ctx)
    39  			prefix = ", "
    40  		}
    41  	}
    42  	if node.Role != nil {
    43  		ctx.WriteString(" default role ")
    44  		node.Role.Format(ctx)
    45  	}
    46  	if node.MiscOpt != nil {
    47  		prefix := " "
    48  		ctx.WriteString(prefix)
    49  		node.MiscOpt.Format(ctx)
    50  	}
    51  	node.CommentOrAttribute.Format(ctx)
    52  }
    53  
    54  func (node *AlterUser) GetStatementType() string { return "Alter User" }
    55  func (node *AlterUser) GetQueryType() string     { return QueryTypeDCL }
    56  
    57  func NewAlterUser(ife bool, u []*User, r *Role, m UserMiscOption) *AlterUser {
    58  	return &AlterUser{
    59  		IfExists: ife,
    60  		Users:    u,
    61  		Role:     r,
    62  		MiscOpt:  m,
    63  	}
    64  }
    65  
    66  type AlterAccountAuthOption struct {
    67  	Exist          bool
    68  	Equal          string
    69  	AdminName      string
    70  	IdentifiedType AccountIdentified
    71  }
    72  
    73  func (node *AlterAccountAuthOption) Format(ctx *FmtCtx) {
    74  	if node.Exist {
    75  		ctx.WriteString(" admin_name")
    76  		if len(node.Equal) != 0 {
    77  			ctx.WriteString(" ")
    78  			ctx.WriteString(node.Equal)
    79  		}
    80  
    81  		ctx.WriteString(fmt.Sprintf(" '%s'", node.AdminName))
    82  		node.IdentifiedType.Format(ctx)
    83  	}
    84  }
    85  
    86  type AlterAccount struct {
    87  	statementImpl
    88  	IfExists   bool
    89  	Name       string
    90  	AuthOption AlterAccountAuthOption
    91  	//status_option or not
    92  	StatusOption AccountStatus
    93  	//comment or not
    94  	Comment AccountComment
    95  }
    96  
    97  func (ca *AlterAccount) Format(ctx *FmtCtx) {
    98  	ctx.WriteString("alter account ")
    99  	if ca.IfExists {
   100  		ctx.WriteString("if exists ")
   101  	}
   102  	ctx.WriteString(ca.Name)
   103  	ca.AuthOption.Format(ctx)
   104  	ca.StatusOption.Format(ctx)
   105  	ca.Comment.Format(ctx)
   106  }
   107  
   108  func (ca *AlterAccount) GetStatementType() string { return "Alter Account" }
   109  func (ca *AlterAccount) GetQueryType() string     { return QueryTypeDCL }
   110  
   111  type AlterView struct {
   112  	statementImpl
   113  	IfExists bool
   114  	Name     *TableName
   115  	ColNames IdentifierList
   116  	AsSource *Select
   117  }
   118  
   119  func (node *AlterView) Format(ctx *FmtCtx) {
   120  	ctx.WriteString("alter ")
   121  
   122  	ctx.WriteString("view ")
   123  
   124  	if node.IfExists {
   125  		ctx.WriteString("if exists ")
   126  	}
   127  
   128  	node.Name.Format(ctx)
   129  	if len(node.ColNames) > 0 {
   130  		ctx.WriteString(" (")
   131  		node.ColNames.Format(ctx)
   132  		ctx.WriteByte(')')
   133  	}
   134  	ctx.WriteString(" as ")
   135  	node.AsSource.Format(ctx)
   136  }
   137  
   138  func (node *AlterView) GetStatementType() string { return "Alter View" }
   139  func (node *AlterView) GetQueryType() string     { return QueryTypeDDL }
   140  
   141  // alter configuration for mo_mysql_compatbility_mode
   142  type AlterDataBaseConfig struct {
   143  	statementImpl
   144  	AccountName    string
   145  	DbName         string
   146  	IsAccountLevel bool
   147  	UpdateConfig   string
   148  }
   149  
   150  func (node *AlterDataBaseConfig) Format(ctx *FmtCtx) {
   151  
   152  	if node.IsAccountLevel {
   153  		ctx.WriteString("alter ")
   154  		ctx.WriteString("account configuration ")
   155  
   156  		ctx.WriteString("for ")
   157  		ctx.WriteString(fmt.Sprintf("%s ", node.AccountName))
   158  	} else {
   159  		ctx.WriteString("alter ")
   160  		ctx.WriteString("database configuration ")
   161  
   162  		ctx.WriteString("for ")
   163  		ctx.WriteString(fmt.Sprintf("%s ", node.DbName))
   164  	}
   165  
   166  	ctx.WriteString("as ")
   167  	ctx.WriteString(fmt.Sprintf("%s ", node.UpdateConfig))
   168  }
   169  
   170  func (node *AlterDataBaseConfig) GetStatementType() string { return "Alter DataBase config" }
   171  func (node *AlterDataBaseConfig) GetQueryType() string     { return QueryTypeDDL }