github.com/matrixorigin/matrixone@v1.2.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  import "github.com/matrixorigin/matrixone/pkg/common/reuse"
    18  
    19  func init() {
    20  	reuse.CreatePool[Use](
    21  		func() *Use { return &Use{} },
    22  		func(u *Use) { u.reset() },
    23  		reuse.DefaultOptions[Use](), //.
    24  	) //WithEnableChecker()
    25  }
    26  
    27  type SecondaryRoleType int
    28  
    29  const (
    30  	SecondaryRoleTypeAll SecondaryRoleType = iota
    31  	SecondaryRoleTypeNone
    32  )
    33  
    34  // Use statement
    35  type Use struct {
    36  	statementImpl
    37  	Name              *CStr
    38  	SecondaryRole     bool
    39  	SecondaryRoleType SecondaryRoleType
    40  	Role              *Role
    41  }
    42  
    43  func NewUse(name *CStr, secondaryRole bool, secondaryRoleType SecondaryRoleType, role *Role) *Use {
    44  	use := reuse.Alloc[Use](nil)
    45  	use.Name = name
    46  	use.SecondaryRole = secondaryRole
    47  	use.SecondaryRoleType = secondaryRoleType
    48  	use.Role = role
    49  	return use
    50  }
    51  
    52  func (node *Use) Format(ctx *FmtCtx) {
    53  	ctx.WriteString("use")
    54  	if !node.SecondaryRole {
    55  		if node.Role != nil {
    56  			ctx.WriteString(" role ")
    57  			node.Role.Format(ctx)
    58  		} else if node.Name != nil && !node.Name.Empty() {
    59  			ctx.WriteByte(' ')
    60  			ctx.WriteString(node.Name.ToLower())
    61  		}
    62  	} else {
    63  		ctx.WriteString(" secondary role ")
    64  		switch node.SecondaryRoleType {
    65  		case SecondaryRoleTypeAll:
    66  			ctx.WriteString("all")
    67  		case SecondaryRoleTypeNone:
    68  			ctx.WriteString("none")
    69  		}
    70  	}
    71  }
    72  
    73  func (node *Use) GetStatementType() string { return "Use" }
    74  func (node *Use) GetQueryType() string     { return QueryTypeOth }
    75  
    76  // IsUseRole checks the statement is:
    77  //
    78  //	USE SECONDARY ROLE { ALL | NONE };
    79  //	USE ROLE role;
    80  func (node *Use) IsUseRole() bool {
    81  	return node.SecondaryRole || node.Role != nil
    82  }
    83  
    84  func (node *Use) Free() {
    85  	reuse.Free[Use](node, nil)
    86  }
    87  
    88  func (node *Use) reset() {
    89  	// if node.Name != nil {
    90  	// node.Free()
    91  	// }
    92  	if node.Role != nil {
    93  		node.Role.Free()
    94  	}
    95  	*node = Use{}
    96  }
    97  
    98  func (node Use) TypeName() string { return "tree.Use" }