github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/insert.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  // the INSERT statement.
    18  type Insert struct {
    19  	statementImpl
    20  	Table             TableExpr
    21  	Accounts          IdentifierList
    22  	PartitionNames    IdentifierList
    23  	Columns           IdentifierList
    24  	Rows              *Select
    25  	OnDuplicateUpdate UpdateExprs
    26  }
    27  
    28  func (node *Insert) Format(ctx *FmtCtx) {
    29  	ctx.WriteString("insert into ")
    30  	node.Table.Format(ctx)
    31  
    32  	if node.PartitionNames != nil {
    33  		ctx.WriteString(" partition(")
    34  		node.PartitionNames.Format(ctx)
    35  		ctx.WriteByte(')')
    36  	}
    37  
    38  	if node.Columns != nil {
    39  		ctx.WriteString(" (")
    40  		node.Columns.Format(ctx)
    41  		ctx.WriteByte(')')
    42  	}
    43  	if node.Accounts != nil {
    44  		ctx.WriteString(" accounts(")
    45  		node.Accounts.Format(ctx)
    46  		ctx.WriteByte(')')
    47  	}
    48  	if node.Rows != nil {
    49  		ctx.WriteByte(' ')
    50  		node.Rows.Format(ctx)
    51  	}
    52  	if len(node.OnDuplicateUpdate) > 0 {
    53  		ctx.WriteString(" on duplicate key update ")
    54  		node.OnDuplicateUpdate.Format(ctx)
    55  	}
    56  }
    57  
    58  func (node *Insert) GetStatementType() string { return "Insert" }
    59  func (node *Insert) GetQueryType() string     { return QueryTypeDML }
    60  
    61  func NewInsert(t TableExpr, c IdentifierList, r *Select, p IdentifierList) *Insert {
    62  	return &Insert{
    63  		Table:          t,
    64  		Columns:        c,
    65  		Rows:           r,
    66  		PartitionNames: p,
    67  	}
    68  }
    69  
    70  type Assignment struct {
    71  	Column Identifier
    72  	Expr   Expr
    73  }