github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/connector.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[DropConnector](
    21  		func() *DropConnector { return &DropConnector{} },
    22  		func(d *DropConnector) { d.reset() },
    23  		reuse.DefaultOptions[DropConnector](), //.
    24  	) //WithEnableChecker()
    25  
    26  	reuse.CreatePool[CreateConnector](
    27  		func() *CreateConnector { return &CreateConnector{} },
    28  		func(c *CreateConnector) { c.reset() },
    29  		reuse.DefaultOptions[CreateConnector](), //.
    30  	) //WithEnableChecker()
    31  
    32  	reuse.CreatePool[ConnectorOption](
    33  		func() *ConnectorOption { return &ConnectorOption{} },
    34  		func(c *ConnectorOption) { c.reset() },
    35  		reuse.DefaultOptions[ConnectorOption](), //.
    36  	) //WithEnableChecker()
    37  }
    38  
    39  type DropConnector struct {
    40  	statementImpl
    41  	IfExists bool
    42  	Names    TableNames
    43  }
    44  
    45  func (node *DropConnector) Format(ctx *FmtCtx) {
    46  	ctx.WriteString("drop connector")
    47  	if node.IfExists {
    48  		ctx.WriteString(" if exists")
    49  	}
    50  	ctx.WriteByte(' ')
    51  	node.Names.Format(ctx)
    52  }
    53  
    54  func (node *DropConnector) GetStatementType() string { return "Drop Connector" }
    55  func (node *DropConnector) GetQueryType() string     { return QueryTypeDDL }
    56  
    57  func (node *DropConnector) Free() {
    58  	reuse.Free[DropConnector](node, nil)
    59  }
    60  
    61  func (node DropConnector) TypeName() string { return "tree.DropConnector" }
    62  
    63  func (node *DropConnector) reset() {
    64  	*node = DropConnector{}
    65  }
    66  
    67  func NewDropConnector(i bool, n TableNames) *DropConnector {
    68  	dropView := reuse.Alloc[DropConnector](nil)
    69  	dropView.IfExists = i
    70  	dropView.Names = n
    71  	return dropView
    72  }
    73  
    74  type CreateConnector struct {
    75  	statementImpl
    76  	TableName *TableName
    77  	Options   []*ConnectorOption
    78  }
    79  
    80  func NewCreateConnector(t *TableName, o []*ConnectorOption) *CreateConnector {
    81  	createView := reuse.Alloc[CreateConnector](nil)
    82  	createView.TableName = t
    83  	createView.Options = o
    84  	return createView
    85  }
    86  
    87  func (node *CreateConnector) Format(ctx *FmtCtx) {
    88  	ctx.WriteString("create connector for ")
    89  	node.TableName.Format(ctx)
    90  	if node.Options != nil {
    91  		prefix := " with ("
    92  		for _, t := range node.Options {
    93  			ctx.WriteString(prefix)
    94  			t.Format(ctx)
    95  			prefix = ", "
    96  		}
    97  		ctx.WriteByte(')')
    98  	}
    99  }
   100  
   101  func (node *CreateConnector) GetStatementType() string { return "Create Connector" }
   102  func (node *CreateConnector) GetQueryType() string     { return QueryTypeDDL }
   103  
   104  func (node *CreateConnector) Free() {
   105  	reuse.Free[CreateConnector](node, nil)
   106  }
   107  
   108  func (node CreateConnector) TypeName() string { return "tree.CreateConnector" }
   109  
   110  func (node *CreateConnector) reset() {
   111  	// if node.TableName != nil {
   112  	// node.TableName.Free()
   113  	// }
   114  	// if node.Options != nil {
   115  	// 	for _, item := range node.Options {
   116  	// item.Free()
   117  	// 	}
   118  	// }
   119  	*node = CreateConnector{}
   120  }
   121  
   122  type ConnectorOption struct {
   123  	createOptionImpl
   124  	Key Identifier
   125  	Val Expr
   126  }
   127  
   128  func NewConnectorOption(k Identifier, v Expr) *ConnectorOption {
   129  	option := reuse.Alloc[ConnectorOption](nil)
   130  	option.Key = k
   131  	option.Val = v
   132  	return option
   133  }
   134  
   135  func (node *ConnectorOption) Format(ctx *FmtCtx) {
   136  	ctx.WriteString(string(node.Key))
   137  	ctx.WriteString(" = ")
   138  	node.Val.Format(ctx)
   139  }
   140  
   141  func (node *ConnectorOption) Free() {
   142  	reuse.Free[ConnectorOption](node, nil)
   143  }
   144  
   145  func (node ConnectorOption) TypeName() string { return "tree.ConnectorOption" }
   146  
   147  func (node *ConnectorOption) reset() {
   148  	*node = ConnectorOption{}
   149  }