github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/values.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 VALUES clause 18 type ValuesClause struct { 19 SelectStatement 20 RowWord bool 21 Rows []Exprs 22 } 23 24 func (node *ValuesClause) Format(ctx *FmtCtx) { 25 ctx.WriteString("values ") 26 comma := "" 27 for i := range node.Rows { 28 ctx.WriteString(comma) 29 if node.RowWord { 30 ctx.WriteString("row") 31 } 32 ctx.WriteByte('(') 33 node.Rows[i].Format(ctx) 34 ctx.WriteByte(')') 35 comma = ", " 36 } 37 } 38 39 func NewValuesClause(r []Exprs) *ValuesClause { 40 return &ValuesClause{ 41 Rows: r, 42 } 43 } 44 45 // ValuesStatement the VALUES Statement 46 type ValuesStatement struct { 47 statementImpl 48 NodeFormatter 49 Rows []Exprs 50 OrderBy OrderBy 51 Limit *Limit 52 } 53 54 func (node *ValuesStatement) Format(ctx *FmtCtx) { 55 ctx.WriteString("values ") 56 57 if len(node.Rows) > 0 { 58 prefix := "" 59 for _, row := range node.Rows { 60 ctx.WriteString(prefix + "row(") 61 comma := "" 62 for i := range row { 63 ctx.WriteString(comma) 64 row[i].Format(ctx) 65 comma = ", " 66 } 67 prefix = "), " 68 } 69 ctx.WriteString(")") 70 } 71 72 if len(node.OrderBy) > 0 { 73 ctx.WriteByte(' ') 74 node.OrderBy.Format(ctx) 75 } 76 if node.Limit != nil { 77 ctx.WriteByte(' ') 78 node.Limit.Format(ctx) 79 } 80 } 81 func (node *ValuesStatement) GetStatementType() string { return "Values" } 82 func (node *ValuesStatement) GetQueryType() string { return QueryTypeDQL }