github.com/vedadiyan/sqlparser@v1.0.0/pkg/sqlparser/encodable.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/vedadiyan/sqlparser/pkg/sqltypes"
    23  )
    24  
    25  // This file contains types that are 'Encodable'.
    26  
    27  // Encodable defines the interface for types that can
    28  // be custom-encoded into SQL.
    29  type Encodable interface {
    30  	EncodeSQL(buf *strings.Builder)
    31  }
    32  
    33  // InsertValues is a custom SQL encoder for the values of
    34  // an insert statement.
    35  type InsertValues [][]sqltypes.Value
    36  
    37  // EncodeSQL performs the SQL encoding for InsertValues.
    38  func (iv InsertValues) EncodeSQL(buf *strings.Builder) {
    39  	for i, rows := range iv {
    40  		if i != 0 {
    41  			buf.WriteString(", ")
    42  		}
    43  		buf.WriteByte('(')
    44  		for j, bv := range rows {
    45  			if j != 0 {
    46  				buf.WriteString(", ")
    47  			}
    48  			bv.EncodeSQL(buf)
    49  		}
    50  		buf.WriteByte(')')
    51  	}
    52  }
    53  
    54  // TupleEqualityList is for generating equality constraints
    55  // for tables that have composite primary keys.
    56  type TupleEqualityList struct {
    57  	Columns []IdentifierCI
    58  	Rows    [][]sqltypes.Value
    59  }
    60  
    61  // EncodeSQL generates the where clause constraints for the tuple
    62  // equality.
    63  func (tpl *TupleEqualityList) EncodeSQL(buf *strings.Builder) {
    64  	if len(tpl.Columns) == 1 {
    65  		tpl.encodeAsIn(buf)
    66  		return
    67  	}
    68  	tpl.encodeAsEquality(buf)
    69  }
    70  
    71  func (tpl *TupleEqualityList) encodeAsIn(buf *strings.Builder) {
    72  	Append(buf, tpl.Columns[0])
    73  	buf.WriteString(" in (")
    74  	for i, r := range tpl.Rows {
    75  		if i != 0 {
    76  			buf.WriteString(", ")
    77  		}
    78  		r[0].EncodeSQL(buf)
    79  	}
    80  	buf.WriteByte(')')
    81  }
    82  
    83  func (tpl *TupleEqualityList) encodeAsEquality(buf *strings.Builder) {
    84  	for i, r := range tpl.Rows {
    85  		if i != 0 {
    86  			buf.WriteString(" or ")
    87  		}
    88  		buf.WriteString("(")
    89  		for j, c := range tpl.Columns {
    90  			if j != 0 {
    91  				buf.WriteString(" and ")
    92  			}
    93  			Append(buf, c)
    94  			buf.WriteString(" = ")
    95  			r[j].EncodeSQL(buf)
    96  		}
    97  		buf.WriteByte(')')
    98  	}
    99  }