-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolumnWrap.go
167 lines (140 loc) · 4 KB
/
columnWrap.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package preform
import (
preformShare "github.com/go-preform/preform/share"
"github.com/go-preform/squirrel"
"reflect"
)
type IColWrap interface {
preformShare.ICol
iTypedCol
initCol(ref reflect.StructField, dbName string, factory IFactory, pos int)
setFactory(factory IFactory)
properties() (isArray bool, isPtr bool, isPk bool, isAuto bool)
SetAlias(alias string) ICol
QueryFactory() IQuery
unwrapPtr(any) any
unwrapPtrForInsert(any) any //care default value
unwrapPtrForUpdate(any) any
getValueFromBody(body iModelBody) any
getValueFromBodyFlatten(body iModelBody) []any
getValueFromBodiesAndLess(iModelBody, iModelBody) bool
valueToString(value any) string
setValueToBody(body iModelBody, value any)
wrapScanner(any) any
flatten(any) []any
Asc() string
Desc() string
Aggregate(fn preformShare.Aggregator, params ...any) iAggregateCol
Sum() iAggregateCol
Avg() iAggregateCol
Max() iAggregateCol
Min() iAggregateCol
Count() iAggregateCol
Mean() iAggregateCol
Median() iAggregateCol
Mode() iAggregateCol
StdDev() iAggregateCol
CountDistinct() iAggregateCol
JsonAgg() iAggregateCol
ArrayAgg() iAggregateCol
GroupConcat(splitter string) iAggregateCol
IsSame(col ICol) bool
}
type ColumnWrap[C ICol] struct {
colConditioner
IColWrap
col C
alias string
altFactory IFactory
codeWithAlias string
code string
}
func newColWrap[C ICol](col C, altFactory IFactory, alias string) *ColumnWrap[C] {
cc := &ColumnWrap[C]{IColWrap: col, col: col, altFactory: altFactory, alias: alias}
cc.colConditioner = colConditioner{col: cc, dialect: altFactory.Db().dialect}
return cc
}
func (c *ColumnWrap[T]) GetCodeWithAlias() string {
if c.codeWithAlias == "" {
db := c.altFactory.Db()
if c.DbName() != c.Alias() {
c.codeWithAlias = db.
dialect.QuoteIdentifier(c.altFactory.Alias()) + "." + db.dialect.QuoteIdentifier(c.DbName()) + " AS " + db.dialect.QuoteIdentifier(c.alias)
} else {
c.codeWithAlias = db.dialect.QuoteIdentifier(c.altFactory.Alias()) + "." + db.dialect.QuoteIdentifier(c.DbName())
}
}
return c.codeWithAlias
}
func (c *ColumnWrap[T]) GetCode() string {
if c.code == "" {
c.code = c.altFactory.
Db().
dialect.
QuoteIdentifier(c.altFactory.Alias()) + "." + c.altFactory.
Db().
dialect.
QuoteIdentifier(c.DbName())
}
return c.code
}
func (c ColumnWrap[C]) Alias() string {
return c.alias
}
func (c ColumnWrap[C]) Factory() IFactory {
return c.altFactory
}
func (c ColumnWrap[C]) QueryFactory() IQuery {
return c.altFactory
}
func (c *ColumnWrap[C]) SetAlias(alias string) ICol {
return newColWrap(c.col, c.altFactory, alias)
}
func (c ColumnWrap[C]) clone(f IFactory) ICol {
return newColWrap(c.col, f, c.alias)
}
func (c ColumnWrap[C]) Sum() iAggregateCol {
return Aggr.Sum(&c)
}
func (c ColumnWrap[C]) Avg() iAggregateCol {
return Aggr.Avg(&c)
}
func (c ColumnWrap[C]) Max() iAggregateCol {
return Aggr.Max(&c)
}
func (c ColumnWrap[C]) Min() iAggregateCol {
return Aggr.Min(&c)
}
func (c ColumnWrap[C]) CountDistinct() iAggregateCol {
return Aggr.CountDistinct(&c)
}
func (c ColumnWrap[C]) Count() iAggregateCol {
return Aggr.Count(&c)
}
func (c ColumnWrap[C]) Mean() iAggregateCol {
return Aggr.Mean(&c)
}
func (c ColumnWrap[C]) Median() iAggregateCol {
return Aggr.Median(&c)
}
func (c ColumnWrap[C]) Mode() iAggregateCol {
return Aggr.Mode(&c)
}
func (c ColumnWrap[C]) StdDev() iAggregateCol {
return Aggr.StdDev(&c)
}
func (c ColumnWrap[C]) GroupConcat(splitter string) iAggregateCol {
return Aggr.GroupConcat(&c, splitter)
}
func (c ColumnWrap[C]) JsonAgg() iAggregateCol {
return Aggr.JsonAgg(&c)
}
func (c ColumnWrap[C]) ArrayAgg() iAggregateCol {
return Aggr.ArrayAgg(&c)
}
func (c ColumnWrap[T]) Aggregate(fn preformShare.Aggregator, params ...any) iAggregateCol {
return &AggregateCol[T]{ICol: &c, Aggregator: fn, body: c.GetCode(), params: params, alias: c.alias, dialect: c.altFactory.Db().dialect}
}
func (c *ColumnWrap[T]) CaseStmt(pkName string) *CaseStmt {
return &CaseStmt{col: c, builder: squirrel.Case(pkName)}
}