forked from jirfag/go-queryset
/
methodsbuilder.go
159 lines (132 loc) · 4.35 KB
/
methodsbuilder.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
package generator
import (
"github.com/tchssk/go-queryset/internal/parser"
"github.com/tchssk/go-queryset/internal/queryset/field"
"github.com/tchssk/go-queryset/internal/queryset/methods"
)
type methodsBuilder struct {
fields []field.Info
s parser.ParsedStruct
ret []methods.Method
sctx methods.QsStructContext
}
func (b *methodsBuilder) qsTypeName() string {
return b.s.TypeName + "QuerySet"
}
func newMethodsBuilder(s parser.ParsedStruct, fields []field.Info) *methodsBuilder {
return &methodsBuilder{
s: s,
sctx: methods.NewQsStructContext(s),
fields: fields,
}
}
func (b *methodsBuilder) getQuerySetMethodsForField(f field.Info) []methods.Method {
fctx := b.sctx.FieldCtx(f)
basicTypeMethods := []methods.Method{
methods.NewBinaryFilterMethod(fctx.WithOperationName("eq")),
methods.NewBinaryFilterMethod(fctx.WithOperationName("ne")),
methods.NewOrderAscByMethod(fctx),
methods.NewOrderDescByMethod(fctx),
}
if !f.IsTime {
inMethod := methods.NewInFilterMethod(fctx)
notInMethod := methods.NewNotInFilterMethod(fctx)
basicTypeMethods = append(basicTypeMethods, inMethod, notInMethod)
}
numericMethods := []methods.Method{
methods.NewBinaryFilterMethod(fctx.WithOperationName("lt")),
methods.NewBinaryFilterMethod(fctx.WithOperationName("gt")),
methods.NewBinaryFilterMethod(fctx.WithOperationName("lte")),
methods.NewBinaryFilterMethod(fctx.WithOperationName("gte")),
}
if f.IsString {
likeMethod := methods.NewBinaryFilterMethod(fctx.WithOperationName("like"))
notLikeMethod := methods.NewBinaryFilterMethod(fctx.WithOperationName("notlike"))
methods := append(basicTypeMethods, likeMethod, notLikeMethod)
return append(methods, numericMethods...)
}
if f.IsNumeric {
return append(basicTypeMethods, numericMethods...)
}
if f.IsStruct {
// Association was found (any struct or struct pointer)
return []methods.Method{methods.NewPreloadMethod(fctx)}
}
if f.IsPointer {
ptrMethods := b.getQuerySetMethodsForField(f.GetPointed())
return append(ptrMethods,
methods.NewIsNullMethod(fctx),
methods.NewIsNotNullMethod(fctx))
}
// it's a string
return basicTypeMethods
}
func (b *methodsBuilder) buildQuerySetFieldMethods(f field.Info) *methodsBuilder {
methods := b.getQuerySetMethodsForField(f)
b.ret = append(b.ret, methods...)
return b
}
func getUpdaterTypeName(structTypeName string) string {
return structTypeName + "Updater"
}
func (b *methodsBuilder) buildUpdaterStructMethods() {
updaterTypeName := getUpdaterTypeName(b.s.TypeName)
b.ret = append(b.ret,
methods.NewUpdaterUpdateMethod(updaterTypeName),
methods.NewUpdaterUpdateNumMethod(updaterTypeName),
)
}
func (b *methodsBuilder) buildUpdaterFieldMethods(f field.Info) {
if f.IsSlice {
return
}
if f.IsPointer {
p := f.GetPointed()
if p.IsStruct {
// TODO
return
}
// It's a pointer to simple field (string, int).
// Developer used pointer to distinguish between NULL and not NULL values.
}
dbSchemaTypeName := b.s.TypeName + "DBSchema"
updaterTypeName := getUpdaterTypeName(b.s.TypeName)
b.ret = append(b.ret,
methods.NewUpdaterSetMethod(f.Name, f.TypeName, updaterTypeName,
dbSchemaTypeName))
}
func (b *methodsBuilder) buildStructSelectMethods() *methodsBuilder {
b.ret = append(b.ret,
methods.NewAllMethod(b.s.TypeName, b.qsTypeName()),
methods.NewOneMethod(b.s.TypeName, b.qsTypeName()),
methods.NewLimitMethod(b.qsTypeName()),
methods.NewOffsetMethod(b.qsTypeName()))
return b
}
func (b *methodsBuilder) buildAggrMethods() *methodsBuilder {
b.ret = append(b.ret,
methods.NewCountMethod(b.qsTypeName()))
return b
}
func (b *methodsBuilder) buildCRUDMethods() *methodsBuilder {
b.ret = append(b.ret,
methods.NewGetUpdaterMethod(b.qsTypeName(), getUpdaterTypeName(b.s.TypeName)),
methods.NewDeleteMethod(b.qsTypeName(), b.s.TypeName),
methods.NewStructModifierMethod("Create", b.s.TypeName),
methods.NewStructModifierMethod("Delete", b.s.TypeName),
methods.NewDeleteNumMethod(b.qsTypeName(), b.s.TypeName),
methods.NewDeleteNumUnscopedMethod(b.qsTypeName(), b.s.TypeName),
methods.NewGetDBMethod(b.qsTypeName()),
)
return b
}
func (b methodsBuilder) Build() []methods.Method {
b.buildStructSelectMethods().
buildAggrMethods().
buildCRUDMethods().
buildUpdaterStructMethods()
for _, f := range b.fields {
b.buildQuerySetFieldMethods(f).buildUpdaterFieldMethods(f)
}
return b.ret
}