-
Notifications
You must be signed in to change notification settings - Fork 0
/
routines.qtpl
225 lines (209 loc) · 6.24 KB
/
routines.qtpl
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{% package _go %}
{% import (
"fmt"
"strings"
"github.com/iancoleman/strcase"
"github.com/ruslanBik4/dbEngine/dbEngine"
"github.com/ruslanBik4/dbEngine/dbEngine/psql"
)
%}
{%- func (c *Creator) CreateRoutinesInvoker(r *psql.Routine, name string) -%}
{%- code
camelName := strcase.ToCamel(name)
args := make([]any, len(r.Params()))
sql, _, _ := r.BuildSql(dbEngine.ArgsForSelect(args...))
-%}
{%- if r.Type == psql.ROUTINE_TYPE_PROC -%}
// {%s camelName %} call procedure '{%s name %}'
{%- if r.Comment > "" -%}// DB comment: '{%s r.Comment %}'
{% endif %}
func (d *Database) {%s camelName %}(
ctx context.Context,{%= c.paramsTitle(r) %}
) error {
return d.Conn.ExecDDL(
ctx,
`{%s sql %}`,
{%= c.paramsArgs(r) %})
}
{%- else -%}
{%= c.CreateFunctionInvoker(r, name, camelName, sql) %}
{%- endif -%}
{%- endfunc -%} CreateRoutinesInvoker
{%- func (c *Creator) CreateFunctionInvoker(r *psql.Routine, name, camelName, sql string) -%}
{%- code
typeReturn, initReturn, needReference := "", "", true
-%}
{%- switch len(r.Columns()) -%}
{%- case 0 -%}
{%- code
typeReturn = c.udtToReturnType(r.UdtName)
-%}
{%- case 1 -%}
{%- code
param := r.Columns()[0]
typeReturn, _ = c.chkTypes(param, strcase.ToCamel(param.Name()))
if a, ok := strings.CutPrefix(typeReturn, "[]"); param.BasicType() < 0 && ok {
typeReturn = "WrapArray[*" + a + "]"
}
-%}
{%- default -%}
{%= c.CreateRowScanner(r, camelName) %}
{%- code
typeReturn = fmt.Sprintf("*%sRowScanner", camelName)
initReturn = fmt.Sprintf("res = &%sRowScanner{}", camelName)
needReference = false
-%}
{%- endswitch -%}
// {%s camelName %} run query with select DB function '{%s name %}: {%s r.UdtName%}'
{%- if r.Comment > "" -%}// DB comment: '{%s r.Comment %}'
{%- endif -%}
// ATTENTION! It returns only 1 row {%s typeReturn %}
// {%s= fmt.Sprintf("%+v", r) %}
func (d *Database) {%s camelName %}(
ctx context.Context,{%= c.paramsTitle(r) %}
) (res {%s typeReturn %}, err error) {
{%s= initReturn %}
err = d.Conn.SelectOneAndScan(ctx,
{% if needReference %}&{% endif %}res,
`{%s sql %}
FETCH FIRST 1 ROW ONLY`,
{%= c.paramsArgs(r) %})
return
}
{%- if r.ReturnType() == "json" || r.ReturnType() == "jsonb" -%}
// {%s camelName %}Out run query with select DB function '{%s name %}: {%s r.UdtName%}'
{%- if r.Comment > "" -%}// DB comment: '{%s r.Comment %}'
{%- endif -%}
// ATTENTION! It returns only 1 row {%s typeReturn %}
func (d *Database) {%s camelName %}Out(
ctx context.Context,
res any,{%= c.paramsTitle(r) %}
) error {
return d.Conn.SelectOneAndScan(ctx,
res,
`{%s sql %}
FETCH FIRST 1 ROW ONLY`,
{%= c.paramsArgs(r) %})
}
{%- endif -%}
{%- if r.ReturnType() == "record" -%}
// {%s camelName %}Out run query with select DB function '{%s name %}'
{%- if r.Comment > "" -%}// DB comment: '{%s r.Comment %}'
{%- endif -%}
// each will get every row from &{%s camelName %}sRowScanner
func (d *Database) {%s camelName %}Out(
ctx context.Context,
each func() error,
res dbEngine.RowScanner,{%= c.paramsTitle(r) %}
) error {
return d.Conn.SelectAndScanEach(ctx,
each,
res,
`{%s sql %}`,
{%= c.paramsArgs(r) %})
}
{%- code typeReturn = fmt.Sprintf("%sRowScanner", camelName) -%}
// {%s camelName %}Each run query with select DB function '{%s name %}'
{%- if r.Comment > "" -%}// DB comment: '{%s r.Comment %}'
{%- endif -%}
// each will get every row from &{%s camelName %}sRowScanner
func (d *Database) {%s camelName %}Each(
ctx context.Context,
each func(record *{%s typeReturn %}) error,{%= c.paramsTitle(r) %}
) error {
res := &{%s typeReturn %}{}
err := d.Conn.SelectAndScanEach(ctx,
func() error {
defer func() {
// create new record
*res = {%s typeReturn %}{}
}()
if each != nil {
return each(res)
}
return nil
},
res,
`{%s sql %}`,
{%= c.paramsArgs(r) %})
return err
}
// {%s camelName %}All run query with select DB function '{%s name %}'
{%- if r.Comment > "" -%}// DB comment: '{%s r.Comment %}'
{%- endif -%}
// WARNING! It return ALL rows as Slice of &{%s camelName %}sRowScanner
func (d *Database) {%s camelName %}All(
ctx context.Context,{%= c.paramsTitle(r) %}
) (res []{%s typeReturn %}, err error) {
buf := {%s typeReturn %}{}
err = d.Conn.SelectAndScanEach(ctx,
func() error {
res = append(res, buf)
// create new record
buf = {%s typeReturn %}{}
return nil
},
&buf,
`{%s sql %}`,
{%= c.paramsArgs(r) %})
return
}
{%- endif -%}{% comment %} ReturnType {% endcomment %}
{%- endfunc -%} CreateFunctionInvoker
{%- func (c *Creator) CreateRowScanner(r *psql.Routine, camelName string) -%}
// {%s camelName %}RowScanner run query with select
type {%s camelName %}RowScanner struct {
{%- code
maxLen := 0
for _, param := range r.Columns() {
maxLen = max(maxLen, len(param.Name()))
}
-%}
{%- for _, param := range r.Columns() -%}
{%- code
s := strcase.ToCamel(param.Name())
typeCol, _ := c.chkTypes(param, s)
if a, ok := strings.CutPrefix(typeCol, "[]"); param.BasicType() < 0 && ok {
typeCol = "WrapArray[*" + a + "]"
}
-%}
{%s fmt.Sprintf("%-[2]*[1]s ", s, maxLen) %}{%s typeCol %}{%s "\t\t"%}`json:"{%s param.Name()%}"`
{%- endfor -%}
}
// GetFields implement dbEngine.RowScanner interface
func (r *{%s camelName %}RowScanner) GetFields(columns []dbEngine.Column) []any {
v := make([]any, len(columns))
for i, col := range columns {
switch col.Name() {
{%- for _, param := range r.Columns() -%}
case "{%s param.Name() %}":
v[i] = &r.{%s= strcase.ToCamel(param.Name()) %}
{%- endfor -%}
}
}
return v
}
{%- endfunc -%}
{%- func (c *Creator) paramsTitle(r *psql.Routine) -%}
{%- code
maxLen := 0
for _, param := range r.Params() {
maxLen = max(maxLen, len(param.Name()))
}
-%}
{% for _, param := range r.Params() %}
{%- code
s := strcase.ToLowerCamel(param.Name())
typeCol, _ := c.chkTypes(param, s)
if param.Default() != nil && !strings.HasPrefix(typeCol, "[]") {
typeCol = "*"+typeCol
}
-%}
{%s fmt.Sprintf("%-[2]*[1]s ", s, maxLen) %}{%s typeCol %}, // {%s param.Comment() %} pg type: {%s param.Type() %} {%- if param.Default() != nil -%}, def: {%v param.Default() %}{%- endif -%}
{%- endfor -%}
{%- endfunc -%}
{%- func (c *Creator) paramsArgs(r *psql.Routine) -%}
{%- for _, param := range r.Params() -%}
{%s strcase.ToLowerCamel(param.Name()) %},
{%- endfor -%}
{%- endfunc -%}