/
templates_q.go
192 lines (156 loc) · 5.76 KB
/
templates_q.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2019 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package generate
import "text/template"
var poolQueryTemplate = template.Must(template.New("").Parse(`
// This file is autogenerated by hexya-generate
// DO NOT MODIFY THIS FILE - ANY CHANGES WILL BE OVERWRITTEN
package {{ .QueryPackageName }}
import (
"github.com/hexya-erp/hexya/src/models"
"github.com/hexya-erp/pool/{{ .QueryPackageName }}/{{ .SnakeName }}"
)
type {{ .Name }}Condition = {{ .SnakeName }}.Condition
// {{ .Name }} returns a {{ .SnakeName }}.ConditionStart for {{ .Name }}Model
func {{ .Name }}() {{ .SnakeName }}.ConditionStart {
return {{ .SnakeName }}.ConditionStart{
ConditionStart: &models.ConditionStart{},
}
}
`))
var poolModelsQueryTemplate = template.Must(template.New("").Parse(`
// This file is autogenerated by hexya-generate
// DO NOT MODIFY THIS FILE - ANY CHANGES WILL BE OVERWRITTEN
package {{ .SnakeName }}
import (
"github.com/hexya-erp/hexya/src/models/operator"
"github.com/hexya-erp/hexya/src/models"
{{ range .TypesDeps }} "{{ . }}"
{{ end }}
)
// ------- INTERFACES --------
{{ range .RelModels }}
type {{ . }}Condition interface {
models.Conditioner
{{ . }}ConditionHexyaFunc()
}
type {{ . }}Set interface {
models.RecordSet
{{ . }}SetHexyaFunc()
}
{{ end }}
// ------- CONDITION ---------
// A Condition is a type safe WHERE clause in an SQL query
type Condition struct {
*models.Condition
}
{{ range .ConditionFuncs }}
// {{ . }} completes the current condition with a simple {{ . }} clause : c.{{ . }}().nextCond => c {{ . }} nextCond
//
// No brackets are added so AND precedence over OR applies.
func (c Condition) {{ . }}() ConditionStart {
return ConditionStart{
ConditionStart: c.Condition.{{ . }}(),
}
}
// {{ . }}Cond completes the current condition with the given cond as an {{ . }} clause
// between brackets : c.{{ . }}(cond) => c {{ . }} (cond)
func (c Condition) {{ . }}Cond(cond Condition) Condition {
return Condition{
Condition: c.Condition.{{ . }}Cond(cond.Condition),
}
}
{{ end }}
// Underlying returns the underlying models.Condition instance
func (c Condition) Underlying() *models.Condition {
return c.Condition
}
// {{ $.Name }}ConditionHexyaFunc is a dummy function to uniquely match interfaces.
func (c Condition) {{ $.Name }}ConditionHexyaFunc() {}
var _ models.Conditioner = Condition{}
// ------- CONDITION START ---------
// A ConditionStart is an object representing a Condition when
// we just added a logical operator (AND, OR, ...) and we are
// about to add a predicate.
type ConditionStart struct {
*models.ConditionStart
}
// NewCondition returns a valid empty Condition
func (cs ConditionStart) NewCondition() Condition {
return Condition {
Condition: &models.Condition{},
}
}
{{ range .Fields }}
// {{ .Name }} adds the "{{ .Name }}" field to the Condition
func (cs ConditionStart) {{ .Name }}() p{{ .SanType }}ConditionField {
return p{{ .SanType }}ConditionField{
ConditionField: cs.Field(models.NewFieldName("{{ .Name }}", "{{ .JSON }}")),
}
}
{{ if .IsRS }}
// {{ .Name }}FilteredOn adds a condition with a table join on the given field and
// filters the result with the given condition
func (cs ConditionStart) {{ .Name }}FilteredOn(cond {{ .RelModel }}Condition) Condition {
return Condition{
Condition: cs.FilteredOn(models.NewFieldName("{{ .Name }}", "{{ .JSON }}"), cond.Underlying()),
}
}
{{ end }}
{{ end }}
// ------- CONDITION FIELDS ----------
{{ range $typ := .Types }}
// A p{{ $typ.SanType }}ConditionField is a partial Condition when
// we have selected a field of type {{ $typ.Type }} and expecting an operator.
type p{{ $typ.SanType }}ConditionField struct {
*models.ConditionField
}
{{ range $typ.Operators }}
// {{ .Name }} adds a condition value to the ConditionPath
func (c p{{ $typ.SanType }}ConditionField) {{ .Name }}(arg {{ if and .Multi (not $typ.IsRS) }}[]{{ end }}{{ $typ.Type }}) Condition {
return Condition{
Condition: c.ConditionField.{{ .Name }}(arg),
}
}
// {{ .Name }}Func adds a function value to the ConditionPath.
// The function will be evaluated when the query is performed and
// it will be given the RecordSet on which the query is made as parameter
func (c p{{ $typ.SanType }}ConditionField) {{ .Name }}Func(arg func (models.RecordSet) {{ if and .Multi (not $typ.IsRS) }}[]{{ end }}{{ if $typ.IsRS }}models.RecordSet{{ else }}{{ $typ.Type }}{{ end }}) Condition {
return Condition{
Condition: c.ConditionField.{{ .Name }}(arg),
}
}
// {{ .Name }}Eval adds an expression value to the ConditionPath.
// The expression value will be evaluated by the client with the
// corresponding execution context. The resulting Condition cannot
// be used server-side.
func (c p{{ $typ.SanType }}ConditionField) {{ .Name }}Eval(expression string) Condition {
return Condition{
Condition: c.ConditionField.{{ .Name }}(models.ClientEvaluatedString(expression)),
}
}
{{ end }}
// IsNull checks if the current condition field is null
func (c p{{ $typ.SanType }}ConditionField) IsNull() Condition {
return Condition{
Condition: c.ConditionField.IsNull(),
}
}
// IsNotNull checks if the current condition field is not null
func (c p{{ $typ.SanType }}ConditionField) IsNotNull() Condition {
return Condition{
Condition: c.ConditionField.IsNotNull(),
}
}
// AddOperator adds a condition value to the condition with the given operator and data
// If multi is true, a recordset will be converted into a slice of int64
// otherwise, it will return an int64 and panic if the recordset is not a singleton.
//
// This method is low level and should be avoided. Use operator methods such as Equals() instead.
func (c p{{ $typ.SanType }}ConditionField) AddOperator(op operator.Operator, data interface{}) Condition {
return Condition{
Condition: c.ConditionField.AddOperator(op, data),
}
}
{{ end }}
`))