-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.go
111 lines (93 loc) · 2.17 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
package lorm
import (
"strconv"
"strings"
"unicode"
"github.com/tada-team/lorm/op"
)
var (
aliases = make(map[string]int)
defaultAlias = "t"
)
func nextAlias(className string) string {
var b strings.Builder
for _, r := range className {
if unicode.IsUpper(r) && unicode.IsLetter(r) {
b.WriteRune(r)
}
}
short := strings.ToLower(b.String())
if short == "" {
short = defaultAlias
}
aliases[short]++
if aliases[short] > 1 {
short += strconv.Itoa(aliases[short])
}
return short
}
type BaseTable struct {
name string
alias string
fields []string
cachedPk op.Column
cachedTableName string
cachedFields *[]op.Column
cachedFieldsExpr *op.Expr
cachedColumns map[string]op.Column
}
func NewBaseTable(name, aliasSeed string, fields ...string) BaseTable {
t := BaseTable{name: name, fields: fields}
t.SetAlias(nextAlias(aliasSeed))
return t
}
func (t BaseTable) Pk() op.Column { return t.cachedPk }
func (t BaseTable) String() string { return t.cachedTableName }
func (t BaseTable) GetAlias() string { return t.alias }
func (t BaseTable) AllFieldsExpr() op.Expr { return *t.cachedFieldsExpr }
func (t BaseTable) GetAllFields() []op.Column { return *t.cachedFields }
func (t *BaseTable) SetAlias(s string) {
n := len(t.fields)
t.alias = s
t.cachedColumns = make(map[string]op.Column, n)
var b strings.Builder
size := 0
for _, f := range t.fields {
size += len(f)
}
if t.alias != "" {
size = (len(t.alias) + 1 + 2) * n
} else {
size = (len(t.name) + 1 + 2) * n
}
b.Grow(size)
fields := make([]op.Column, n)
for i, f := range t.fields {
c := t.Column(f)
if i > 0 {
b.WriteString(", ")
}
b.WriteString(c.String())
fields[i] = c
}
expr := op.Raw(b.String())
t.cachedFieldsExpr = &expr
t.cachedFields = &fields
t.cachedPk = t.Column(t.fields[0])
t.cachedTableName = t.name
if t.alias != "" {
t.cachedTableName += " AS " + t.alias
}
}
func (t BaseTable) Column(v string) op.Column {
res := t.cachedColumns[v]
if res == "" {
if t.alias != "" {
res = op.Column(t.alias + "." + v)
} else {
res = op.Column(t.name + "." + v)
}
t.cachedColumns[v] = res
}
return res
}