github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/use.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 SecondaryRoleType int
    18  
    19  const (
    20  	SecondaryRoleTypeAll SecondaryRoleType = iota
    21  	SecondaryRoleTypeNone
    22  )
    23  
    24  // Use statement
    25  type Use struct {
    26  	statementImpl
    27  	Name              string
    28  	SecondaryRole     bool
    29  	SecondaryRoleType SecondaryRoleType
    30  	Role              *Role
    31  }
    32  
    33  func (node *Use) Format(ctx *FmtCtx) {
    34  	ctx.WriteString("use")
    35  	if !node.SecondaryRole {
    36  		if node.Role != nil {
    37  			ctx.WriteString(" role ")
    38  			node.Role.Format(ctx)
    39  		} else if node.Name != "" {
    40  			ctx.WriteByte(' ')
    41  			ctx.WriteString(node.Name)
    42  		}
    43  	} else {
    44  		ctx.WriteString(" secondary role ")
    45  		switch node.SecondaryRoleType {
    46  		case SecondaryRoleTypeAll:
    47  			ctx.WriteString("all")
    48  		case SecondaryRoleTypeNone:
    49  			ctx.WriteString("none")
    50  		}
    51  	}
    52  }
    53  
    54  func (node *Use) GetStatementType() string { return "Use" }
    55  func (node *Use) GetQueryType() string     { return QueryTypeOth }
    56  
    57  // IsUseRole checks the statement is:
    58  //
    59  //	USE SECONDARY ROLE { ALL | NONE };
    60  //	USE ROLE role;
    61  func (node *Use) IsUseRole() bool {
    62  	return node.SecondaryRole || node.Role != nil
    63  }
    64  
    65  func NewUse(n string) *Use {
    66  	return &Use{Name: n}
    67  }