-
Notifications
You must be signed in to change notification settings - Fork 371
/
table.go
258 lines (234 loc) · 6.76 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gorp
import (
"bytes"
"fmt"
"reflect"
"strings"
)
// TableMap represents a mapping between a Go struct and a database table
// Use dbmap.AddTable() or dbmap.AddTableWithName() to create these
type TableMap struct {
// Name of database table.
TableName string
SchemaName string
gotype reflect.Type
Columns []*ColumnMap
keys []*ColumnMap
indexes []*IndexMap
uniqueTogether [][]string
version *ColumnMap
insertPlan bindPlan
updatePlan bindPlan
deletePlan bindPlan
getPlan bindPlan
dbmap *DbMap
}
// ResetSql removes cached insert/update/select/delete SQL strings
// associated with this TableMap. Call this if you've modified
// any column names or the table name itself.
func (t *TableMap) ResetSql() {
t.insertPlan = bindPlan{}
t.updatePlan = bindPlan{}
t.deletePlan = bindPlan{}
t.getPlan = bindPlan{}
}
// SetKeys lets you specify the fields on a struct that map to primary
// key columns on the table. If isAutoIncr is set, result.LastInsertId()
// will be used after INSERT to bind the generated id to the Go struct.
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
//
// Panics if isAutoIncr is true, and fieldNames length != 1
//
func (t *TableMap) SetKeys(isAutoIncr bool, fieldNames ...string) *TableMap {
if isAutoIncr && len(fieldNames) != 1 {
panic(fmt.Sprintf(
"gorp: SetKeys: fieldNames length must be 1 if key is auto-increment. (Saw %v fieldNames)",
len(fieldNames)))
}
t.keys = make([]*ColumnMap, 0)
for _, name := range fieldNames {
colmap := t.ColMap(name)
colmap.isPK = true
colmap.isAutoIncr = isAutoIncr
t.keys = append(t.keys, colmap)
}
t.ResetSql()
return t
}
// SetUniqueTogether lets you specify uniqueness constraints across multiple
// columns on the table. Each call adds an additional constraint for the
// specified columns.
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
//
// Panics if fieldNames length < 2.
//
func (t *TableMap) SetUniqueTogether(fieldNames ...string) *TableMap {
if len(fieldNames) < 2 {
panic(fmt.Sprintf(
"gorp: SetUniqueTogether: must provide at least two fieldNames to set uniqueness constraint."))
}
columns := make([]string, 0, len(fieldNames))
for _, name := range fieldNames {
columns = append(columns, name)
}
for _, existingColumns := range t.uniqueTogether {
if equal(existingColumns, columns) {
return t
}
}
t.uniqueTogether = append(t.uniqueTogether, columns)
t.ResetSql()
return t
}
// ColMap returns the ColumnMap pointer matching the given struct field
// name. It panics if the struct does not contain a field matching this
// name.
func (t *TableMap) ColMap(field string) *ColumnMap {
col := colMapOrNil(t, field)
if col == nil {
e := fmt.Sprintf("No ColumnMap in table %s type %s with field %s",
t.TableName, t.gotype.Name(), field)
panic(e)
}
return col
}
func colMapOrNil(t *TableMap, field string) *ColumnMap {
for _, col := range t.Columns {
if col.fieldName == field || col.ColumnName == field {
return col
}
}
return nil
}
// IdxMap returns the IndexMap pointer matching the given index name.
func (t *TableMap) IdxMap(field string) *IndexMap {
for _, idx := range t.indexes {
if idx.IndexName == field {
return idx
}
}
return nil
}
// AddIndex registers the index with gorp for specified table with given parameters.
// This operation is idempotent. If index is already mapped, the
// existing *IndexMap is returned
// Function will panic if one of the given for index columns does not exists
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
//
func (t *TableMap) AddIndex(name string, idxtype string, columns []string) *IndexMap {
// check if we have a index with this name already
for _, idx := range t.indexes {
if idx.IndexName == name {
return idx
}
}
for _, icol := range columns {
if res := t.ColMap(icol); res == nil {
e := fmt.Sprintf("No ColumnName in table %s to create index on", t.TableName)
panic(e)
}
}
idx := &IndexMap{IndexName: name, Unique: false, IndexType: idxtype, columns: columns}
t.indexes = append(t.indexes, idx)
t.ResetSql()
return idx
}
// SetVersionCol sets the column to use as the Version field. By default
// the "Version" field is used. Returns the column found, or panics
// if the struct does not contain a field matching this name.
//
// Automatically calls ResetSql() to ensure SQL statements are regenerated.
func (t *TableMap) SetVersionCol(field string) *ColumnMap {
c := t.ColMap(field)
t.version = c
t.ResetSql()
return c
}
// SqlForCreateTable gets a sequence of SQL commands that will create
// the specified table and any associated schema
func (t *TableMap) SqlForCreate(ifNotExists bool) string {
s := bytes.Buffer{}
dialect := t.dbmap.Dialect
if strings.TrimSpace(t.SchemaName) != "" {
schemaCreate := "create schema"
if ifNotExists {
s.WriteString(dialect.IfSchemaNotExists(schemaCreate, t.SchemaName))
} else {
s.WriteString(schemaCreate)
}
s.WriteString(fmt.Sprintf(" %s;", t.SchemaName))
}
tableCreate := "create table"
if ifNotExists {
s.WriteString(dialect.IfTableNotExists(tableCreate, t.SchemaName, t.TableName))
} else {
s.WriteString(tableCreate)
}
s.WriteString(fmt.Sprintf(" %s (", dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
x := 0
for _, col := range t.Columns {
if !col.Transient {
if x > 0 {
s.WriteString(", ")
}
stype := dialect.ToSqlType(col.gotype, col.MaxSize, col.isAutoIncr)
s.WriteString(fmt.Sprintf("%s %s", dialect.QuoteField(col.ColumnName), stype))
if col.isPK || col.isNotNull {
s.WriteString(" not null")
}
if col.isPK && len(t.keys) == 1 {
s.WriteString(" primary key")
}
if col.Unique {
s.WriteString(" unique")
}
if col.isAutoIncr {
s.WriteString(fmt.Sprintf(" %s", dialect.AutoIncrStr()))
}
x++
}
}
if len(t.keys) > 1 {
s.WriteString(", primary key (")
for x := range t.keys {
if x > 0 {
s.WriteString(", ")
}
s.WriteString(dialect.QuoteField(t.keys[x].ColumnName))
}
s.WriteString(")")
}
if len(t.uniqueTogether) > 0 {
for _, columns := range t.uniqueTogether {
s.WriteString(", unique (")
for i, column := range columns {
if i > 0 {
s.WriteString(", ")
}
s.WriteString(dialect.QuoteField(column))
}
s.WriteString(")")
}
}
s.WriteString(") ")
s.WriteString(dialect.CreateTableSuffix())
s.WriteString(dialect.QuerySuffix())
return s.String()
}
func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}