forked from pingcap/tidb-tools
/
util.go
130 lines (113 loc) · 3.16 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package diff
import (
"fmt"
"strings"
"github.com/pingcap/log"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb-tools/pkg/dbutil"
"github.com/pingcap/tidb-tools/pkg/utils"
"github.com/pingcap/tidb/types"
"go.uber.org/zap"
)
func equalStrings(str1, str2 []string) bool {
if len(str1) != len(str2) {
return false
}
for i := 0; i < len(str1); i++ {
if str1[i] != str2[i] {
return false
}
}
return true
}
func removeColumns(tableInfo *model.TableInfo, columns []string) *model.TableInfo {
if len(columns) == 0 {
return tableInfo
}
removeColMap := utils.SliceToMap(columns)
for i := 0; i < len(tableInfo.Indices); i++ {
index := tableInfo.Indices[i]
for j := 0; j < len(index.Columns); j++ {
col := index.Columns[j]
if _, ok := removeColMap[col.Name.O]; ok {
index.Columns = append(index.Columns[:j], index.Columns[j+1:]...)
j--
if len(index.Columns) == 0 {
tableInfo.Indices = append(tableInfo.Indices[:i], tableInfo.Indices[i+1:]...)
i--
}
}
}
}
for j := 0; j < len(tableInfo.Columns); j++ {
col := tableInfo.Columns[j]
if _, ok := removeColMap[col.Name.O]; ok {
tableInfo.Columns = append(tableInfo.Columns[:j], tableInfo.Columns[j+1:]...)
j--
}
}
// calculate column offset
colMap := make(map[string]int, len(tableInfo.Columns))
for i, col := range tableInfo.Columns {
col.Offset = i
colMap[col.Name.O] = i
}
for _, index := range tableInfo.Indices {
for _, col := range index.Columns {
offset, ok := colMap[col.Name.O]
if !ok {
// this should never happened
log.Fatal("column not exists", zap.String("column", col.Name.O))
}
col.Offset = offset
}
}
return tableInfo
}
func getColumnsFromIndex(index *model.IndexInfo, tableInfo *model.TableInfo) []*model.ColumnInfo {
indexColumns := make([]*model.ColumnInfo, 0, len(index.Columns))
for _, indexColumn := range index.Columns {
for _, column := range tableInfo.Columns {
if column.Name.O == indexColumn.Name.O {
indexColumns = append(indexColumns, column)
}
}
}
return indexColumns
}
func needQuotes(ft types.FieldType) bool {
return !(dbutil.IsNumberType(ft.Tp) || dbutil.IsFloatType(ft.Tp))
}
func rowContainsCols(row map[string]*dbutil.ColumnData, cols []*model.ColumnInfo) bool {
for _, col := range cols {
if _, ok := row[col.Name.O]; !ok {
return false
}
}
return true
}
func rowToString(row map[string]*dbutil.ColumnData) string {
var s strings.Builder
s.WriteString("{ ")
for key, val := range row {
if val.IsNull {
s.WriteString(fmt.Sprintf("%s: IsNull, ", key))
} else {
s.WriteString(fmt.Sprintf("%s: %s, ", key, val.Data))
}
}
s.WriteString(" }")
return s.String()
}