@@ -44,6 +44,7 @@ function formatQueryForPython(query, indentLevel) {
44
44
return ` "${ sql} "`
45
45
}
46
46
47
+ // LATER: reduce duplication with routes.go.ejs
47
48
// {'values':
48
49
// [
49
50
// {
@@ -59,11 +60,30 @@ function extractInsertValues(queryAst) {
59
60
return Array .from (new Set (values));
60
61
}
61
62
63
+ // LATER: reduce duplication with routes.go.ejs
64
+ // {'set':
65
+ // [
66
+ // {
67
+ // column: 'updated_by',
68
+ // value: { type: 'param', value: 'user_id' },
69
+ // table: null
70
+ // }
71
+ // ]
72
+ // } => [ 'user_id' ]
73
+ function extractUpdateValues (queryAst ) {
74
+ // LATER: distinguish between b.param and q.param and extract only first
75
+ return queryAst .set .map (elem => elem .value .type === ' param' ? elem .value .value : null )
76
+ .filter (value => value) // filter out nulls
77
+ }
78
+
62
79
// LATER: reduce duplication with routes.go.ejs
63
80
function extractProperties (queryAst ) {
64
81
if (queryAst .type === ' insert' ) {
65
82
return extractInsertValues (queryAst);
66
83
}
84
+ if (queryAst .type === ' update' ) {
85
+ return extractUpdateValues (queryAst);
86
+ }
67
87
return [];
68
88
}
69
89
@@ -259,11 +279,27 @@ def <%- pythonMethodName %>(body: <%- model %>, conn=Depends(db_connection)):
259
279
260
280
}
261
281
if (method .name === ' put' ) {
282
+ // TODO: reduce duplication with POST
283
+ const dto = query2dto (sqlParser, method);
284
+ // LATER: do we really need signature and cache?
285
+ const cacheKey = dto ? dto .signature : null ;
286
+ const model = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
287
+
288
+ const methodArgs = [` body: ${ model} ` , ... argsFromPath, ' conn=Depends(db_connection)' ]
289
+
290
+ const sql = convertToPsycopgNamedArguments (formatQueryForPython (method .query , 20 ))
291
+ const params = extractParamsFromQuery (method .query );
292
+ const formattedParams = formatParamsAsPythonDict (params)
262
293
% >
263
294
264
295
@router .put (' <%- path %>' , status_code = status .HTTP_204_NO_CONTENT )
265
- def < %- pythonMethodName % > ():
266
- pass
296
+ def < %- pythonMethodName % > (< %- methodArgs .join (' , ' ) % > ):
297
+ try:
298
+ with conn:
299
+ with conn .cursor () as cur:
300
+ cur .execute (< %- sql % >< %- formattedParams % > )
301
+ finally:
302
+ conn .close ()
267
303
< %
268
304
269
305
}
0 commit comments