forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_info.go
157 lines (147 loc) · 4.35 KB
/
table_info.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"fmt"
"strconv"
"strings"
"sync"
log "github.com/golang/glog"
querypb "github.com/youtube/vitess/go/vt/proto/query"
"github.com/youtube/vitess/go/vt/schema"
"golang.org/x/net/context"
)
// TableInfo contains the tabletserver related info for a table.
// It's a superset of schema.Table.
type TableInfo struct {
*schema.Table
// Seq must be locked before accessing the sequence vars.
// If CurVal==LastVal, we have to cache new values.
Seq sync.Mutex
NextVal int64
Increment int64
LastVal int64
}
// NewTableInfo creates a new TableInfo.
func NewTableInfo(conn *DBConn, tableName string, tableType string, comment string) (ti *TableInfo, err error) {
ti, err = loadTableInfo(conn, tableName)
if err != nil {
return nil, err
}
if strings.Contains(comment, "vitess_sequence") {
ti.Type = schema.Sequence
}
return ti, nil
}
func loadTableInfo(conn *DBConn, tableName string) (ti *TableInfo, err error) {
ti = &TableInfo{Table: schema.NewTable(tableName)}
if err = ti.fetchColumns(conn); err != nil {
return nil, err
}
if err = ti.fetchIndexes(conn); err != nil {
return nil, err
}
return ti, nil
}
func (ti *TableInfo) fetchColumns(conn *DBConn) error {
qr, err := conn.Exec(context.Background(), fmt.Sprintf("select * from `%s` where 1 != 1", ti.Name), 10000, true)
if err != nil {
return err
}
fieldTypes := make(map[string]querypb.Type, len(qr.Fields))
for _, field := range qr.Fields {
fieldTypes[field.Name] = field.Type
}
columns, err := conn.Exec(context.Background(), fmt.Sprintf("describe `%s`", ti.Name), 10000, false)
if err != nil {
return err
}
for _, row := range columns.Rows {
name := row[0].String()
columnType, ok := fieldTypes[name]
if !ok {
log.Warningf("Table: %s, column %s not found in select list, skipping.", ti.Name, name)
continue
}
ti.AddColumn(name, columnType, row[4], row[5].String())
}
return nil
}
// SetPK sets the pk columns for a TableInfo.
func (ti *TableInfo) SetPK(colnames []string) error {
pkIndex := schema.NewIndex("PRIMARY")
colnums := make([]int, len(colnames))
for i, colname := range colnames {
colnums[i] = ti.FindColumn(colname)
if colnums[i] == -1 {
return fmt.Errorf("column %s not found", colname)
}
pkIndex.AddColumn(colname, 1)
}
for _, col := range ti.Columns {
pkIndex.DataColumns = append(pkIndex.DataColumns, col.Name)
}
if len(ti.Indexes) == 0 {
ti.Indexes = make([]*schema.Index, 1)
} else if ti.Indexes[0].Name.Lowered() != "primary" {
ti.Indexes = append(ti.Indexes, nil)
copy(ti.Indexes[1:], ti.Indexes[:len(ti.Indexes)-1])
} // else we replace the currunt primary key
ti.Indexes[0] = pkIndex
ti.PKColumns = colnums
return nil
}
func (ti *TableInfo) fetchIndexes(conn *DBConn) error {
indexes, err := conn.Exec(context.Background(), fmt.Sprintf("show index from `%s`", ti.Name), 10000, false)
if err != nil {
return err
}
var currentIndex *schema.Index
currentName := ""
for _, row := range indexes.Rows {
indexName := row[2].String()
if currentName != indexName {
currentIndex = ti.AddIndex(indexName)
currentName = indexName
}
var cardinality uint64
if !row[6].IsNull() {
cardinality, err = strconv.ParseUint(row[6].String(), 0, 64)
if err != nil {
log.Warningf("%s", err)
}
}
currentIndex.AddColumn(row[4].String(), cardinality)
}
if len(ti.Indexes) == 0 {
return nil
}
pkIndex := ti.Indexes[0]
if pkIndex.Name.Lowered() != "primary" {
return nil
}
ti.PKColumns = make([]int, len(pkIndex.Columns))
for i, pkCol := range pkIndex.Columns {
ti.PKColumns[i] = ti.FindColumn(pkCol.Original())
}
// Primary key contains all table columns
for _, col := range ti.Columns {
pkIndex.DataColumns = append(pkIndex.DataColumns, col.Name)
}
// Secondary indices contain all primary key columns
for i := 1; i < len(ti.Indexes); i++ {
for _, c := range ti.Indexes[i].Columns {
ti.Indexes[i].DataColumns = append(ti.Indexes[i].DataColumns, c)
}
for _, c := range pkIndex.Columns {
// pk columns may already be part of the index. So,
// check before adding.
if ti.Indexes[i].FindDataColumn(c.Original()) != -1 {
continue
}
ti.Indexes[i].DataColumns = append(ti.Indexes[i].DataColumns, c)
}
}
return nil
}