/
table.go
109 lines (92 loc) · 1.86 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
package dac
import (
"strings"
)
type TableInfoOptions struct {
schema string
conflicts []string
}
type TableInfoOption func(options *TableInfoOptions)
func Schema(schema string) TableInfoOption {
return func(options *TableInfoOptions) {
options.schema = strings.TrimSpace(schema)
}
}
// Conflicts
// params are field not column
func Conflicts(conflicts ...string) TableInfoOption {
return func(options *TableInfoOptions) {
for _, conflict := range conflicts {
conflict = strings.TrimSpace(conflict)
if conflict == "" {
continue
}
options.conflicts = append(options.conflicts, conflict)
}
}
}
func Info(name string, options ...TableInfoOption) TableInfo {
opt := TableInfoOptions{}
for _, option := range options {
option(&opt)
}
return TableInfo{
name: strings.TrimSpace(name),
schema: opt.schema,
conflicts: opt.conflicts,
}
}
type TableInfo struct {
name string
schema string
conflicts []string
}
func (info TableInfo) Schema() string {
return info.schema
}
func (info TableInfo) Name() string {
return info.name
}
func (info TableInfo) Conflicts() []string {
return info.conflicts
}
// Table
// the recv of TableInfo method must be value, can not be ptr
type Table interface {
TableInfo() TableInfo
}
type ViewInfo struct {
pure bool
name string
schema string
base Table
}
func (info ViewInfo) Pure() (string, string, bool) {
return info.schema, info.name, info.pure
}
func (info ViewInfo) Base() Table {
return info.base
}
func TableView(table Table) ViewInfo {
return ViewInfo{
pure: false,
name: "",
schema: "",
base: table,
}
}
func PureView(name string, schema ...string) ViewInfo {
s := ""
if len(schema) > 0 {
s = schema[0]
}
return ViewInfo{
pure: true,
name: strings.TrimSpace(name),
schema: s,
base: nil,
}
}
type View interface {
ViewInfo() ViewInfo
}