github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/procedure.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[DropProcedure](
    21  		func() *DropProcedure { return &DropProcedure{} },
    22  		func(d *DropProcedure) { d.reset() },
    23  		reuse.DefaultOptions[DropProcedure](), //.
    24  	) //WithEnableChecker()
    25  
    26  	reuse.CreatePool[ProcedureArgDecl](
    27  		func() *ProcedureArgDecl { return &ProcedureArgDecl{} },
    28  		func(p *ProcedureArgDecl) { p.reset() },
    29  		reuse.DefaultOptions[ProcedureArgDecl](), //.
    30  	) //WithEnableChecker()
    31  
    32  	reuse.CreatePool[ProcedureName](
    33  		func() *ProcedureName { return &ProcedureName{} },
    34  		func(p *ProcedureName) { p.reset() },
    35  		reuse.DefaultOptions[ProcedureName](), //.
    36  	) //WithEnableChecker()
    37  
    38  	reuse.CreatePool[CreateProcedure](
    39  		func() *CreateProcedure { return &CreateProcedure{} },
    40  		func(c *CreateProcedure) { c.reset() },
    41  		reuse.DefaultOptions[CreateProcedure](), //.
    42  	) //WithEnableChecker()
    43  
    44  	reuse.CreatePool[CallStmt](
    45  		func() *CallStmt { return &CallStmt{} },
    46  		func(c *CallStmt) { c.reset() },
    47  		reuse.DefaultOptions[CallStmt](), //.
    48  	) //WithEnableChecker()
    49  }
    50  
    51  type InOutArgType int
    52  
    53  const (
    54  	TYPE_IN InOutArgType = iota
    55  	TYPE_OUT
    56  	TYPE_INOUT
    57  )
    58  
    59  type ProcedureArgType struct {
    60  	Type InOutArgType
    61  }
    62  
    63  type ProcedureArg interface {
    64  	NodeFormatter
    65  	GetName(ctx *FmtCtx) string
    66  	GetType() int
    67  }
    68  
    69  type ProcedureArgImpl struct {
    70  	ProcedureArg
    71  }
    72  
    73  // container holding list of arguments in udf
    74  type ProcedureArgs []ProcedureArg
    75  
    76  type ProcedureArgDecl struct {
    77  	ProcedureArgImpl
    78  	Name      *UnresolvedName
    79  	Type      ResolvableTypeReference
    80  	InOutType InOutArgType
    81  }
    82  
    83  func (node *ProcedureArgDecl) Format(ctx *FmtCtx) {
    84  	// in out type
    85  	switch node.InOutType {
    86  	case TYPE_IN:
    87  		ctx.WriteString("in ")
    88  	case TYPE_OUT:
    89  		ctx.WriteString("out ")
    90  	case TYPE_INOUT:
    91  		ctx.WriteString("inout ")
    92  	}
    93  	if node.Name != nil {
    94  		node.Name.Format(ctx)
    95  		ctx.WriteByte(' ')
    96  	}
    97  	node.Type.(*T).InternalType.Format(ctx)
    98  }
    99  
   100  func (node *ProcedureArgDecl) GetName(ctx *FmtCtx) string {
   101  	node.Name.Format(ctx)
   102  	return ctx.String()
   103  }
   104  
   105  func (node *ProcedureArgDecl) GetType() int {
   106  	return int(node.InOutType)
   107  }
   108  
   109  func (node *ProcedureArgDecl) reset() {
   110  	// if node.Name != nil {
   111  	// node.Name.Free()
   112  	// }
   113  	*node = ProcedureArgDecl{}
   114  }
   115  
   116  func (node ProcedureArgDecl) TypeName() string { return "tree.ProcedureArgDecl" }
   117  
   118  func (node *ProcedureArgDecl) Free() {
   119  	reuse.Free[ProcedureArgDecl](node, nil)
   120  }
   121  
   122  type ProcedureArgForMarshal struct {
   123  	Name      *UnresolvedName
   124  	Type      ResolvableTypeReference
   125  	InOutType InOutArgType
   126  }
   127  
   128  type ProcedureName struct {
   129  	Name objName
   130  }
   131  
   132  func (node *ProcedureName) Format(ctx *FmtCtx) {
   133  	if node.Name.ExplicitCatalog {
   134  		ctx.WriteString(string(node.Name.CatalogName))
   135  		ctx.WriteByte('.')
   136  	}
   137  	if node.Name.ExplicitSchema {
   138  		ctx.WriteString(string(node.Name.SchemaName))
   139  		ctx.WriteByte('.')
   140  	}
   141  	ctx.WriteString(string(node.Name.ObjectName))
   142  }
   143  
   144  func (node *ProcedureName) HasNoNameQualifier() bool {
   145  	return !node.Name.ExplicitCatalog && !node.Name.ExplicitSchema
   146  }
   147  
   148  func (node *ProcedureName) reset() {
   149  	*node = ProcedureName{}
   150  }
   151  
   152  func (node ProcedureName) TypeName() string { return "tree.ProcedureName" }
   153  
   154  func (node *ProcedureName) Free() {
   155  	reuse.Free[ProcedureName](node, nil)
   156  }
   157  
   158  func NewProcedureName(name Identifier, prefix ObjectNamePrefix) *ProcedureName {
   159  	return &ProcedureName{
   160  		Name: objName{
   161  			ObjectName:       name,
   162  			ObjectNamePrefix: prefix,
   163  		},
   164  	}
   165  }
   166  
   167  func NewProcedureArgDecl(f InOutArgType, n *UnresolvedName, t ResolvableTypeReference) *ProcedureArgDecl {
   168  	return &ProcedureArgDecl{
   169  		Name:      n,
   170  		Type:      t,
   171  		InOutType: f,
   172  	}
   173  }
   174  
   175  type CreateProcedure struct {
   176  	statementImpl
   177  	Name *ProcedureName
   178  	Args ProcedureArgs
   179  	Body string
   180  }
   181  
   182  func NewCreateProcedure(n *ProcedureName, a ProcedureArgs, b string) *CreateProcedure {
   183  	createProcedure := reuse.Alloc[CreateProcedure](nil)
   184  	createProcedure.Name = n
   185  	createProcedure.Args = a
   186  	createProcedure.Body = b
   187  	return createProcedure
   188  }
   189  
   190  func (node *CreateProcedure) Format(ctx *FmtCtx) {
   191  	ctx.WriteString("create procedure ")
   192  
   193  	node.Name.Format(ctx)
   194  
   195  	ctx.WriteString(" (")
   196  	for i, def := range node.Args {
   197  		if i != 0 {
   198  			ctx.WriteString(",")
   199  			ctx.WriteByte(' ')
   200  		}
   201  		def.Format(ctx)
   202  	}
   203  	ctx.WriteString(") '")
   204  
   205  	ctx.WriteString(node.Body)
   206  	ctx.WriteString("'")
   207  }
   208  
   209  func (node *CreateProcedure) GetStatementType() string { return "Create Procedure" }
   210  
   211  func (node *CreateProcedure) GetQueryType() string { return QueryTypeOth }
   212  
   213  func (node *CreateProcedure) reset() {
   214  	if node.Name != nil {
   215  		node.Name.Free()
   216  	}
   217  	*node = CreateProcedure{}
   218  }
   219  
   220  func (node CreateProcedure) TypeName() string { return "tree.CreateProcedure" }
   221  
   222  func (node *CreateProcedure) Free() {
   223  	reuse.Free[CreateProcedure](node, nil)
   224  }
   225  
   226  type DropProcedure struct {
   227  	statementImpl
   228  	Name     *ProcedureName
   229  	IfExists bool
   230  }
   231  
   232  func (node *DropProcedure) Free() {
   233  	reuse.Free[DropProcedure](node, nil)
   234  }
   235  
   236  func (node DropProcedure) TypeName() string { return "tree.DropProcedure" }
   237  
   238  func (node *DropProcedure) reset() {
   239  	if node.Name != nil {
   240  		node.Name.Free()
   241  	}
   242  	*node = DropProcedure{}
   243  }
   244  
   245  func (node *DropProcedure) Format(ctx *FmtCtx) {
   246  	ctx.WriteString("drop procedure ")
   247  	if node.IfExists {
   248  		ctx.WriteString("if exists ")
   249  	}
   250  	node.Name.Format(ctx)
   251  }
   252  
   253  func (node *DropProcedure) GetStatementType() string { return "Create Procedure" }
   254  
   255  func (node *DropProcedure) GetQueryType() string { return QueryTypeOth }
   256  
   257  func NewDropProcedure(n *ProcedureName, i bool) *DropProcedure {
   258  	dropProcedure := reuse.Alloc[DropProcedure](nil)
   259  	dropProcedure.Name = n
   260  	dropProcedure.IfExists = i
   261  	return dropProcedure
   262  }
   263  
   264  type CallStmt struct {
   265  	statementImpl
   266  	Name *ProcedureName
   267  	Args Exprs
   268  }
   269  
   270  func (node *CallStmt) Format(ctx *FmtCtx) {
   271  	ctx.WriteString("call ")
   272  	node.Name.Format(ctx)
   273  	ctx.WriteString("(")
   274  	if len(node.Args) != 0 {
   275  		node.Args.Format(ctx)
   276  	}
   277  	ctx.WriteString(")")
   278  }
   279  
   280  func (node *CallStmt) GetStatementType() string { return "Call" }
   281  
   282  func (node *CallStmt) GetQueryType() string { return QueryTypeOth }
   283  
   284  func (node *CallStmt) reset() {
   285  	if node.Name != nil {
   286  		node.Name.Free()
   287  	}
   288  	*node = CallStmt{}
   289  }
   290  
   291  func (node CallStmt) TypeName() string { return "tree.CallStmt" }
   292  
   293  func (node *CallStmt) Free() {
   294  	reuse.Free[CallStmt](node, nil)
   295  }