-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
42 lines (36 loc) · 885 Bytes
/
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
package spoon
import (
"fmt"
"strings"
)
// Table is mapping struct info
type Table struct {
name string
columns []*Column
primaryKey *PrimaryKey
indexes Indexes
}
func newTable(name string, columns []*Column, pk *PrimaryKey, indexes Indexes) *Table {
return &Table{
name: name,
columns: columns,
primaryKey: pk,
indexes: indexes,
}
}
func (t *Table) Indexes() Indexes {
return t.indexes
}
func (t *Table) CreateTableSchema() string {
ss := make([]string, 0, len(t.columns)+2)
ss = append(ss, fmt.Sprintf("CREATE TABLE %s (", Quote(t.name)))
for i := range t.columns {
c := t.columns[i]
ss = append(ss, fmt.Sprintf(" %s,", c.ToSQL()))
}
ss = append(ss, fmt.Sprintf(") %s", t.primaryKey.ToSQL()))
return strings.Join(ss, "\n")
}
func (t *Table) DropTableSchema() string {
return fmt.Sprintf("DROP TABLE %s", Quote(t.name))
}