github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/set.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  type SetVar struct {
    18  	statementImpl
    19  	Assignments []*VarAssignmentExpr
    20  }
    21  
    22  func (node *SetVar) Format(ctx *FmtCtx) {
    23  	ctx.WriteString("set")
    24  	if node.Assignments != nil {
    25  		prefix := " "
    26  		for _, a := range node.Assignments {
    27  			ctx.WriteString(prefix)
    28  			a.Format(ctx)
    29  			prefix = ", "
    30  		}
    31  	}
    32  }
    33  
    34  func (node *SetVar) GetStatementType() string { return "Set Var" }
    35  func (node *SetVar) GetQueryType() string     { return QueryTypeOth }
    36  
    37  func NewSetVar(a []*VarAssignmentExpr) *SetVar {
    38  	return &SetVar{
    39  		Assignments: a,
    40  	}
    41  }
    42  
    43  // for variable = expr
    44  type VarAssignmentExpr struct {
    45  	NodeFormatter
    46  	System   bool
    47  	Global   bool
    48  	Name     string
    49  	Value    Expr
    50  	Reserved Expr
    51  }
    52  
    53  func (node *VarAssignmentExpr) Format(ctx *FmtCtx) {
    54  	if node.Global {
    55  		ctx.WriteString("global ")
    56  	}
    57  	ctx.WriteString(node.Name)
    58  	ctx.WriteString(" =")
    59  	if node.Value != nil {
    60  		ctx.WriteByte(' ')
    61  		node.Value.Format(ctx)
    62  	}
    63  	if node.Reserved != nil {
    64  		ctx.WriteByte(' ')
    65  		node.Reserved.Format(ctx)
    66  	}
    67  }
    68  
    69  func NewVarAssignmentExpr(s bool, g bool, n string, v Expr, r Expr) *VarAssignmentExpr {
    70  	return &VarAssignmentExpr{
    71  		System:   s,
    72  		Global:   g,
    73  		Name:     n,
    74  		Value:    v,
    75  		Reserved: r,
    76  	}
    77  }
    78  
    79  type SetDefaultRoleType int
    80  
    81  const (
    82  	SET_DEFAULT_ROLE_TYPE_NONE SetDefaultRoleType = iota
    83  	SET_DEFAULT_ROLE_TYPE_ALL
    84  	SET_DEFAULT_ROLE_TYPE_NORMAL
    85  )
    86  
    87  type SetDefaultRole struct {
    88  	statementImpl
    89  	Type  SetDefaultRoleType
    90  	Roles []*Role
    91  	Users []*User
    92  }
    93  
    94  func (node *SetDefaultRole) Format(ctx *FmtCtx) {
    95  	ctx.WriteString("set default role")
    96  	switch node.Type {
    97  	case SET_DEFAULT_ROLE_TYPE_NONE:
    98  		ctx.WriteString(" none")
    99  	case SET_DEFAULT_ROLE_TYPE_ALL:
   100  		ctx.WriteString(" all")
   101  	case SET_DEFAULT_ROLE_TYPE_NORMAL:
   102  		prefix := " "
   103  		for _, r := range node.Roles {
   104  			ctx.WriteString(prefix)
   105  			r.Format(ctx)
   106  			prefix = ", "
   107  		}
   108  	}
   109  	ctx.WriteString(" to")
   110  	prefix := " "
   111  	for _, u := range node.Users {
   112  		ctx.WriteString(prefix)
   113  		u.Format(ctx)
   114  		prefix = ", "
   115  	}
   116  }
   117  
   118  func (node *SetDefaultRole) GetStatementType() string { return "Set Role" }
   119  func (node *SetDefaultRole) GetQueryType() string     { return QueryTypeOth }
   120  
   121  func NewSetDefaultRole(t SetDefaultRoleType, r []*Role, u []*User) *SetDefaultRole {
   122  	return &SetDefaultRole{
   123  		Type:  t,
   124  		Roles: r,
   125  		Users: u,
   126  	}
   127  }
   128  
   129  type SetRoleType int
   130  
   131  const (
   132  	SET_ROLE_TYPE_NORMAL SetRoleType = iota
   133  	SET_ROLE_TYPE_DEFAULT
   134  	SET_ROLE_TYPE_NONE
   135  	SET_ROLE_TYPE_ALL
   136  	SET_ROLE_TYPE_ALL_EXCEPT
   137  )
   138  
   139  type SetRole struct {
   140  	statementImpl
   141  	SecondaryRole     bool
   142  	SecondaryRoleType SecondaryRoleType
   143  	Role              *Role
   144  }
   145  
   146  func (node *SetRole) Format(ctx *FmtCtx) {
   147  	ctx.WriteString("set")
   148  	if !node.SecondaryRole {
   149  		if node.Role != nil {
   150  			ctx.WriteString(" role ")
   151  			node.Role.Format(ctx)
   152  		}
   153  	} else {
   154  		ctx.WriteString(" secondary role ")
   155  		switch node.SecondaryRoleType {
   156  		case SecondaryRoleTypeAll:
   157  			ctx.WriteString("all")
   158  		case SecondaryRoleTypeNone:
   159  			ctx.WriteString("none")
   160  		}
   161  	}
   162  }
   163  
   164  func (node *SetRole) GetStatementType() string { return "Set Role" }
   165  func (node *SetRole) GetQueryType() string     { return QueryTypeOth }
   166  
   167  type SetPassword struct {
   168  	statementImpl
   169  	User     *User
   170  	Password string
   171  }
   172  
   173  func (node *SetPassword) Format(ctx *FmtCtx) {
   174  	ctx.WriteString("set password")
   175  	if node.User != nil {
   176  		ctx.WriteString(" for ")
   177  		node.User.Format(ctx)
   178  	}
   179  	ctx.WriteString(" = ")
   180  	ctx.WriteString(node.Password)
   181  }
   182  
   183  func NewSetPassword(u *User, p string) *SetPassword {
   184  	return &SetPassword{
   185  		User:     u,
   186  		Password: p,
   187  	}
   188  }
   189  
   190  func (node *SetPassword) GetStatementType() string { return "Set Password" }
   191  func (node *SetPassword) GetQueryType() string     { return QueryTypeOth }