forked from pingcap/tidb
/
opt.go
228 lines (204 loc) · 4.86 KB
/
opt.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2015 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 coldef
import (
"fmt"
"strings"
"github.com/pingcap/tidb/expression"
)
// FloatOpt is used for parsing floating-point type option from SQL.
// TODO: add reference doc.
type FloatOpt struct {
Flen int
Decimal int
}
// CharsetOpt is used for parsing charset option from SQL.
type CharsetOpt struct {
Chs string
Col string
}
// String implements fmt.Stringer interface.
func (o *CharsetOpt) String() string {
var ss []string
if o.Chs != "" {
ss = append(ss, "CHARACTER SET = "+o.Chs)
}
if o.Col != "" {
ss = append(ss, "COLLATE = "+o.Col)
}
return strings.Join(ss, " ")
}
// ConstraintOpt is used for parsing column constraint info from SQL.
type ConstraintOpt struct {
Tp int
Bvalue bool
Evalue expression.Expression
}
// String implements fmt.Stringer interface.
func (c *ConstraintOpt) String() string {
switch c.Tp {
case ConstrNotNull:
return "NOT NULL"
case ConstrNull:
return "NULL"
case ConstrAutoIncrement:
return "AUTO_INCREMENT"
case ConstrPrimaryKey:
return "PRIMARY KEY"
case ConstrUniq:
return "UNIQUE"
case ConstrUniqKey:
return "UNIQUE KEY"
case ConstrDefaultValue:
return "DEFAULT " + c.Evalue.String()
case ConstrOnUpdate:
return "ON UPDATE " + c.Evalue.String()
default:
return ""
}
}
// DB Options.
const (
DBOptNone = iota
DBOptCharset
DBOptCollate
)
// DatabaseOpt is used for parsing database option from SQL.
type DatabaseOpt struct {
Tp int
Value string
}
// Constraints.
const (
ConstrNoConstr = iota
ConstrPrimaryKey
ConstrForeignKey
ConstrNotNull
ConstrAutoIncrement
ConstrDefaultValue
ConstrUniq
ConstrIndex
ConstrUniqIndex
ConstrKey
ConstrUniqKey
ConstrNull
ConstrOnUpdate
ConstrFulltext
)
const (
elemColumnDef = iota
elemTableConstr
)
// LockType is select lock type.
type LockType int
// Select Lock Type.
const (
SelectLockNone LockType = iota
SelectLockForUpdate
SelectLockInShareMode
)
// Table Options.
const (
TblOptNone = iota
TblOptEngine
TblOptCharset
TblOptCollate
TblOptAutoIncrement
)
// TableOpt is used for parsing table option from SQL.
type TableOpt struct {
Tp int
StrValue string
UintValue uint64
}
// TableOption is the collection of table options.
// TODO: rename TableOpt or TableOption.
type TableOption struct {
Engine string
Charset string
Collate string
AutoIncrement uint64 // TODO: apply this value to autoid.Allocator.
}
// String implements fmt.Stringer interface.
func (o *TableOption) String() string {
strs := []string{}
if o.Engine != "" {
x := fmt.Sprintf("ENGINE=%s", o.Engine)
strs = append(strs, x)
}
if o.Charset != "" {
x := fmt.Sprintf("CHARSET=%s", o.Charset)
strs = append(strs, x)
}
if o.Collate != "" {
x := fmt.Sprintf("COLLATE=%s", o.Collate)
strs = append(strs, x)
}
return strings.Join(strs, " ")
}
// TableConstraint is constraint for table definition.
type TableConstraint struct {
Tp int
ConstrName string
// Used for PRIMARY KEY, UNIQUE, ......
Keys []*IndexColName
// Used for foreign key.
Refer *ReferenceDef
}
// Clone clones a new TableConstraint from old TableConstraint.
func (tc *TableConstraint) Clone() *TableConstraint {
keys := make([]*IndexColName, 0, len(tc.Keys))
for _, k := range tc.Keys {
keys = append(keys, k)
}
ntc := &TableConstraint{
Tp: tc.Tp,
ConstrName: tc.ConstrName,
Keys: keys,
}
if tc.Refer != nil {
ntc.Refer = tc.Refer.Clone()
}
return ntc
}
// String implements fmt.Stringer interface.
func (tc *TableConstraint) String() string {
tokens := []string{}
if tc.Tp == ConstrPrimaryKey {
tokens = append(tokens, "PRIMARY KEY")
} else {
if tc.Tp == ConstrKey {
tokens = append(tokens, "KEY")
} else if tc.Tp == ConstrIndex {
tokens = append(tokens, "INDEX")
} else if tc.Tp == ConstrUniq {
tokens = append(tokens, "UNIQUE")
} else if tc.Tp == ConstrUniqKey {
tokens = append(tokens, "UNIQUE KEY")
} else if tc.Tp == ConstrUniqIndex {
tokens = append(tokens, "UNIQUE INDEX")
} else if tc.Tp == ConstrForeignKey {
tokens = append(tokens, "FOREIGN KEY")
}
tokens = append(tokens, tc.ConstrName)
}
keysStr := make([]string, 0, len(tc.Keys))
for _, v := range tc.Keys {
keysStr = append(keysStr, v.String())
}
tokens = append(tokens, fmt.Sprintf("(%s)", strings.Join(keysStr, ", ")))
if tc.Refer != nil {
tokens = append(tokens, tc.Refer.String())
}
return strings.Join(tokens, " ")
}