-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
schema.go
166 lines (146 loc) · 3.94 KB
/
schema.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
158
159
160
161
162
163
164
165
166
// 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 schema
// Yes, this sucks. It's a tiny tiny package that needs to be on its own
// It contains a data structure that's shared between sqlparser & tabletserver
import (
"strings"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/sync2"
)
// Column categories
const (
CAT_OTHER = iota
CAT_NUMBER
CAT_VARBINARY
)
// Cache types
const (
CACHE_NONE = 0
CACHE_RW = 1
CACHE_W = 2
)
// TableColumn contains info about a table's column.
type TableColumn struct {
Name string
Category int
IsAuto bool
Default sqltypes.Value
}
// Table contains info about a table.
type Table struct {
Name string
Columns []TableColumn
Indexes []*Index
PKColumns []int
CacheType int
// These vars can be accessed concurrently.
TableRows sync2.AtomicInt64
DataLength sync2.AtomicInt64
IndexLength sync2.AtomicInt64
DataFree sync2.AtomicInt64
}
// NewTable creates a new Table.
func NewTable(name string) *Table {
return &Table{
Name: name,
Columns: make([]TableColumn, 0, 16),
Indexes: make([]*Index, 0, 8),
}
}
// AddColumn adds a column to the Table.
func (ta *Table) AddColumn(name string, columnType string, defval sqltypes.Value, extra string) {
index := len(ta.Columns)
ta.Columns = append(ta.Columns, TableColumn{Name: name})
if strings.Contains(columnType, "int") {
ta.Columns[index].Category = CAT_NUMBER
} else if strings.HasPrefix(columnType, "varbinary") {
ta.Columns[index].Category = CAT_VARBINARY
} else {
ta.Columns[index].Category = CAT_OTHER
}
if extra == "auto_increment" {
ta.Columns[index].IsAuto = true
// Ignore default value, if any
return
}
if defval.IsNull() {
return
}
if ta.Columns[index].Category == CAT_NUMBER {
ta.Columns[index].Default = sqltypes.MakeNumeric(defval.Raw())
} else {
ta.Columns[index].Default = sqltypes.MakeString(defval.Raw())
}
}
// FindColumn finds a column in the table. It returns the index if found.
// Otherwise, it returns -1.
func (ta *Table) FindColumn(name string) int {
for i, col := range ta.Columns {
if col.Name == name {
return i
}
}
return -1
}
// GetPKColumn returns the pk column specified by the index.
func (ta *Table) GetPKColumn(index int) *TableColumn {
return &ta.Columns[ta.PKColumns[index]]
}
// AddIndex adds an index to the table.
func (ta *Table) AddIndex(name string) (index *Index) {
index = NewIndex(name)
ta.Indexes = append(ta.Indexes, index)
return index
}
// SetMysqlStats receives the values found in the mysql information_schema.tables table
func (ta *Table) SetMysqlStats(tr, dl, il, df sqltypes.Value) {
v, _ := tr.ParseInt64()
ta.TableRows.Set(v)
v, _ = dl.ParseInt64()
ta.DataLength.Set(v)
v, _ = il.ParseInt64()
ta.IndexLength.Set(v)
v, _ = df.ParseInt64()
ta.DataFree.Set(v)
}
// Index contains info about a table index.
type Index struct {
Name string
Columns []string
Cardinality []uint64
DataColumns []string
}
// NewIndex creates a new Index.
func NewIndex(name string) *Index {
return &Index{name, make([]string, 0, 8), make([]uint64, 0, 8), nil}
}
// AddColumn adds a column to the index.
func (idx *Index) AddColumn(name string, cardinality uint64) {
idx.Columns = append(idx.Columns, name)
if cardinality == 0 {
cardinality = uint64(len(idx.Cardinality) + 1)
}
idx.Cardinality = append(idx.Cardinality, cardinality)
}
// FindColumn finds a column in the index. It returns the index if found.
// Otherwise, it returns -1.
func (idx *Index) FindColumn(name string) int {
for i, colName := range idx.Columns {
if name == colName {
return i
}
}
return -1
}
// FindDataColumn finds a data column in the index. It returns the index if found.
// Otherwise, it returns -1.
func (idx *Index) FindDataColumn(name string) int {
for i, colName := range idx.DataColumns {
if name == colName {
return i
}
}
return -1
}