forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
142 lines (124 loc) · 3.02 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
package sqlchemy
import (
"fmt"
"reflect"
"strings"
"yunion.io/x/log"
"yunion.io/x/pkg/utils"
)
type STableSpec struct {
structType reflect.Type
name string
columns []IColumnSpec
indexes []STableIndex
contraints []STableConstraint
}
type STable struct {
spec *STableSpec
alias string
}
type STableField struct {
table *STable
spec IColumnSpec
alias string
}
func NewTableSpecFromStruct(s interface{}, name string) *STableSpec {
val := reflect.Indirect(reflect.ValueOf(s))
st := val.Type()
if st.Kind() != reflect.Struct {
panic("expect Struct kind")
}
table := &STableSpec{
columns: []IColumnSpec{},
name: name,
structType: st,
}
log.Infof("struct2TableSpec for table %s", name)
struct2TableSpec(val, table)
return table
}
func (ts *STableSpec) Name() string {
return ts.name
}
func (ts *STableSpec) Columns() []IColumnSpec {
return ts.columns
}
func (ts *STableSpec) DataType() reflect.Type {
return ts.structType
}
func (ts *STableSpec) CreateSQL() string {
cols := make([]string, 0)
primaries := make([]string, 0)
indexes := make([]string, 0)
for _, c := range ts.columns {
cols = append(cols, c.DefinitionString())
if c.IsPrimary() {
primaries = append(primaries, fmt.Sprintf("`%s`", c.Name()))
}
if c.IsIndex() {
indexes = append(indexes, fmt.Sprintf("KEY `ix_%s_%s` (`%s`)", ts.name, c.Name(), c.Name()))
}
}
if len(primaries) > 0 {
cols = append(cols, fmt.Sprintf("PRIMARY KEY (%s)", strings.Join(primaries, ", ")))
}
if len(indexes) > 0 {
cols = append(cols, indexes...)
}
return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (\n%s\n) ENGINE=InnoDB DEFAULT CHARSET=utf8", ts.name, strings.Join(cols, ",\n"))
}
func (ts *STableSpec) Instance() *STable {
table := STable{spec: ts, alias: getTableAliasName()}
return &table
}
func (ts *STableSpec) ColumnSpec(name string) IColumnSpec {
for _, c := range ts.Columns() {
if c.Name() == name {
return c
}
}
return nil
}
func (tbl *STable) Field(name string, alias ...string) IQueryField {
// name = reflectutils.StructFieldName(name)
name = utils.CamelSplit(name, "_")
spec := tbl.spec.ColumnSpec(name)
if spec == nil {
panic("column not found: " + name)
}
col := STableField{table: tbl, spec: spec}
if len(alias) > 0 {
col.Label(alias[0])
}
return &col
}
func (tbl *STable) Fields() []IQueryField {
ret := make([]IQueryField, 0)
for _, c := range tbl.spec.Columns() {
ret = append(ret, tbl.Field(c.Name()))
}
return ret
}
func (c *STableField) Expression() string {
if len(c.alias) > 0 {
return fmt.Sprintf("%s.%s as `%s`", c.table.Alias(), c.spec.Name(), c.alias)
} else {
return fmt.Sprintf("%s.%s", c.table.Alias(), c.spec.Name())
}
}
func (c *STableField) Name() string {
if len(c.alias) > 0 {
return c.alias
} else {
return c.spec.Name()
}
}
func (c *STableField) Reference() string {
return fmt.Sprintf("`%s`.`%s`", c.table.Alias(), c.Name())
}
func (c *STableField) Label(label string) IQueryField {
if len(label) > 0 && label != c.spec.Name() {
c.alias = label
}
return c
}