Skip to content

Commit 01591a3

Browse files
committed
refactor(golang): extract variable "sql"
1 parent 58211b6 commit 01591a3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/templates/routes.go.ejs

+7-5
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ endpoints.forEach(function(endpoint) {
197197
return
198198
}
199199
200+
const sql = formatQuery(method.query)
201+
200202
// define before "if", to make it available later
201203
let dataType
202204
if (method.name !== 'delete') {
@@ -221,7 +223,7 @@ endpoints.forEach(function(endpoint) {
221223
<%
222224
if (params.length > 0) {
223225
-%>
224-
stmt, err := db.PrepareNamed("<%- formatQuery(method.query) %>")
226+
stmt, err := db.PrepareNamed("<%- sql %>")
225227
if err != nil {
226228
fmt.Fprintf(os.Stderr, "PrepareNamed failed: %v\n", err)
227229
internalServerError(w)
@@ -235,7 +237,7 @@ endpoints.forEach(function(endpoint) {
235237
err = stmt.<%- queryFunction %>(&result, args)
236238
<% } else { -%>
237239
<%- resultVariableDeclaration %>
238-
err := db.<%- queryFunction %>(&result, "<%- formatQuery(method.query) %>")
240+
err := db.<%- queryFunction %>(&result, "<%- sql %>")
239241
<% } -%>
240242
switch err {
241243
case sql.ErrNoRows:
@@ -260,7 +262,7 @@ endpoints.forEach(function(endpoint) {
260262
<%- formatParamsAsGolangMap(params, method) %>
261263
}
262264
_, err := db.NamedExec(
263-
"<%- formatQuery(method.query) %>",
265+
"<%- sql %>",
264266
args,
265267
)
266268
if err != nil {
@@ -283,7 +285,7 @@ endpoints.forEach(function(endpoint) {
283285
<%- formatParamsAsGolangMap(params, method) %>
284286
}
285287
_, err := db.NamedExec(
286-
"<%- formatQuery(method.query) %>",
288+
"<%- sql %>",
287289
args,
288290
)
289291
if err != nil {
@@ -303,7 +305,7 @@ endpoints.forEach(function(endpoint) {
303305
<%- formatParamsAsGolangMap(params, method) %>
304306
}
305307
_, err := db.NamedExec(
306-
"<%- formatQuery(method.query) %>",
308+
"<%- sql %>",
307309
args,
308310
)
309311
if err != nil {

0 commit comments

Comments
 (0)