github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sqlmodel/utils.go (about) 1 // Copyright 2022 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package sqlmodel 15 16 import ( 17 "fmt" 18 "strings" 19 20 timodel "github.com/pingcap/tidb/pkg/parser/model" 21 ) 22 23 func getColsAndValuesOfIdx( 24 columns []*timodel.ColumnInfo, 25 indexColumns *timodel.IndexInfo, 26 data []interface{}, 27 ) ([]*timodel.ColumnInfo, []interface{}) { 28 cols := make([]*timodel.ColumnInfo, 0, len(indexColumns.Columns)) 29 values := make([]interface{}, 0, len(indexColumns.Columns)) 30 for _, col := range indexColumns.Columns { 31 cols = append(cols, columns[col.Offset]) 32 values = append(values, data[col.Offset]) 33 } 34 35 return cols, values 36 } 37 38 // valuesHolder gens values holder like (?,?,?). 39 func valuesHolder(n int) string { 40 var builder strings.Builder 41 builder.Grow((n-1)*2 + 3) 42 builder.WriteByte('(') 43 for i := 0; i < n; i++ { 44 if i > 0 { 45 builder.WriteString(",") 46 } 47 builder.WriteString("?") 48 } 49 builder.WriteByte(')') 50 return builder.String() 51 } 52 53 // generatedColumnsNameSet returns a set of generated columns' name. 54 func generatedColumnsNameSet(columns []*timodel.ColumnInfo) map[string]struct{} { 55 m := make(map[string]struct{}) 56 for _, col := range columns { 57 if col.IsGenerated() { 58 m[col.Name.L] = struct{}{} 59 } 60 } 61 return m 62 } 63 64 // ColValAsStr convert column value as string 65 func ColValAsStr(v interface{}) string { 66 switch dv := v.(type) { 67 case []byte: 68 return string(dv) 69 case string: 70 return dv 71 } 72 return fmt.Sprintf("%v", v) 73 }