-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
79 lines (64 loc) · 1.29 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
package q
import (
"github.com/wxy365/basal/opt"
)
type ITable interface {
named
aliasable[ITable]
StatementProvider
iTableIdentity()
}
type iTableAdapter struct {
}
func (i *iTableAdapter) iTableIdentity() {}
type Table struct {
iTableAdapter
namedImpl
alias string
}
func (t *Table) Alias() opt.Opt[string] {
return opt.Of(t.alias)
}
func (t *Table) As(alias string) ITable {
t.alias = alias
return t
}
func (t *Table) Col(columnName string) *Column {
col := &Column{}
col.name = columnName
return col
}
func (t *Table) GetStatement(ctx *RenderCtx) *Statement {
escaper := ctx.dbType.escaper()
nameAlias := opt.Map(t.Alias(), func(alias string) string {
return escaper(t.Name()) + " " + escaper(alias)
}).OrElse(escaper(t.Name()))
return &Statement{
stm: nameAlias,
}
}
type SubQuery struct {
iTableAdapter
selection *Selection
alias string
}
func (s *SubQuery) Name() string {
return ""
}
func (s *SubQuery) Alias() opt.Opt[string] {
return opt.Of(s.alias)
}
func (s *SubQuery) As(alias string) ITable {
s.alias = alias
return s
}
func (s *SubQuery) GetStatement(ctx *RenderCtx) *Statement {
stm := s.selection.GetStatement(ctx)
return &Statement{
stm: "(" + stm.stm + ") " + s.Alias().Get(),
params: stm.params,
}
}
type DO interface {
TableName() string
}