github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/validator_cond.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 syncer
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/pingcap/tidb/pkg/parser/model"
    20  )
    21  
    22  type Cond struct {
    23  	TargetTbl string
    24  	Columns   []*model.ColumnInfo
    25  	PK        *model.IndexInfo
    26  	PkValues  [][]string
    27  }
    28  
    29  func (c *Cond) GetArgs() []interface{} {
    30  	var res []interface{}
    31  	for _, v := range c.PkValues {
    32  		for _, val := range v {
    33  			res = append(res, val)
    34  		}
    35  	}
    36  	return res
    37  }
    38  
    39  func (c *Cond) GetWhere() string {
    40  	var b strings.Builder
    41  	isOneKey := len(c.PK.Columns) == 1
    42  	if isOneKey {
    43  		b.WriteString(c.PK.Columns[0].Name.O)
    44  	} else {
    45  		b.WriteString("(")
    46  		for i := 0; i < len(c.PK.Columns); i++ {
    47  			if i != 0 {
    48  				b.WriteString(",")
    49  			}
    50  			b.WriteString(c.PK.Columns[i].Name.O)
    51  		}
    52  		b.WriteString(")")
    53  	}
    54  	b.WriteString(" in (")
    55  	for i := range c.PkValues {
    56  		if i != 0 {
    57  			b.WriteString(",")
    58  		}
    59  		if !isOneKey {
    60  			b.WriteString("(")
    61  			for j := 0; j < len(c.PK.Columns); j++ {
    62  				if j != 0 {
    63  					b.WriteString(",")
    64  				}
    65  				b.WriteString("?")
    66  			}
    67  			b.WriteString(")")
    68  		} else {
    69  			b.WriteString("?")
    70  		}
    71  	}
    72  	b.WriteString(")")
    73  	return b.String()
    74  }