forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
138 lines (118 loc) · 4.27 KB
/
table.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
131
132
133
134
135
136
137
138
// 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 dbutil
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/ddl"
_ "github.com/pingcap/tidb/planner/core" // to setup expression.EvalAstExpr. See: https://github.com/pingcap/tidb/blob/a94cff903cd1e7f3b050db782da84273ef5592f4/planner/core/optimizer.go#L202
_ "github.com/pingcap/tidb/types/parser_driver" // for parser driver
)
// GetTableInfo returns table information.
func GetTableInfo(ctx context.Context, db *sql.DB, schemaName string, tableName string) (*model.TableInfo, error) {
createTableSQL, err := GetCreateTableSQL(ctx, db, schemaName, tableName)
if err != nil {
return nil, errors.Trace(err)
}
parser2, err := GetParserForDB(db)
if err != nil {
return nil, errors.Trace(err)
}
return GetTableInfoBySQL(createTableSQL, parser2)
}
// GetTableInfoBySQL returns table information by given create table sql.
func GetTableInfoBySQL(createTableSQL string, parser2 *parser.Parser) (table *model.TableInfo, err error) {
stmt, err := parser2.ParseOneStmt(createTableSQL, "", "")
if err != nil {
return nil, errors.Trace(err)
}
s, ok := stmt.(*ast.CreateTableStmt)
if ok {
table, err := ddl.BuildTableInfoFromAST(s)
if err != nil {
return nil, errors.Trace(err)
}
// put primary key in indices
if table.PKIsHandle {
pkIndex := &model.IndexInfo{
Name: model.NewCIStr("PRIMARY"),
Primary: true,
State: model.StatePublic,
Unique: true,
Columns: []*model.IndexColumn{
{
Name: table.GetPkName(),
},
},
}
table.Indices = append(table.Indices, pkIndex)
}
return table, nil
}
return nil, errors.Errorf("get table info from sql %s failed!", createTableSQL)
}
// FindColumnByName finds column by name.
func FindColumnByName(cols []*model.ColumnInfo, name string) *model.ColumnInfo {
// column name don't distinguish capital and small letter
name = strings.ToLower(name)
for _, col := range cols {
if col.Name.L == name {
return col
}
}
return nil
}
// EqualTableInfo returns true if this two table info have same columns and indices
func EqualTableInfo(tableInfo1, tableInfo2 *model.TableInfo) (bool, string) {
// check columns
if len(tableInfo1.Columns) != len(tableInfo2.Columns) {
return false, fmt.Sprintf("column num not equal, one is %d another is %d", len(tableInfo1.Columns), len(tableInfo2.Columns))
}
for j, col := range tableInfo1.Columns {
if col.Name.O != tableInfo2.Columns[j].Name.O {
return false, fmt.Sprintf("column name not equal, one is %s another is %s", col.Name.O, tableInfo2.Columns[j].Name.O)
}
if col.Tp != tableInfo2.Columns[j].Tp {
return false, fmt.Sprintf("column %s's type not equal, one is %v another is %v", col.Name.O, col.Tp, tableInfo2.Columns[j].Tp)
}
}
// check index
if len(tableInfo1.Indices) != len(tableInfo2.Indices) {
return false, fmt.Sprintf("index num not equal, one is %d another is %d", len(tableInfo1.Indices), len(tableInfo2.Indices))
}
index2Map := make(map[string]*model.IndexInfo)
for _, index := range tableInfo2.Indices {
index2Map[index.Name.O] = index
}
for _, index1 := range tableInfo1.Indices {
index2, ok := index2Map[index1.Name.O]
if !ok {
return false, fmt.Sprintf("index %s not exists", index1.Name.O)
}
if len(index1.Columns) != len(index2.Columns) {
return false, fmt.Sprintf("index %s's columns num not equal, one is %d another is %d", index1.Name.O, len(index1.Columns), len(index2.Columns))
}
for j, col := range index1.Columns {
if col.Name.O != index2.Columns[j].Name.O {
return false, fmt.Sprintf("index %s's column not equal, one has %s another has %s", index1.Name.O, col.Name.O, index2.Columns[j].Name.O)
}
}
}
return true, ""
}