@@ -32,6 +32,19 @@ function convertToFastApiPath(path) {
32
32
return path .replace (/ :([_a-zA-Z ] + )/ g , ' {$1}' )
33
33
}
34
34
35
+ // Differs from formatQuery() as it doesn't flatten query (preserve original formatting)
36
+ // and also use """ for multiline strings
37
+ function formatQueryForPython (query , indentLevel ) {
38
+ const sql = removePlaceholders (removeComments (query))
39
+ const isMultilineSql = sql .indexOf (' \n ' ) >= 0
40
+ if (isMultilineSql) {
41
+ const indent = ' ' .repeat (indentLevel)
42
+ const indentedSql = sql .replace (/ \n / g , ' \n ' + indent)
43
+ return ` \n ${ indent} """\n ${ indent}${ indentedSql} \n ${ indent} """` ;
44
+ }
45
+ return ` "${ sql} "`
46
+ }
47
+
35
48
36
49
endpoints .forEach (function (endpoint ) {
37
50
const path = convertToFastApiPath (endpoint .path )
@@ -58,7 +71,7 @@ endpoints.forEach(function(endpoint) {
58
71
const queries = []
59
72
queriesWithNames .forEach (queryWithName => {
60
73
for (const [name , query ] of Object .entries (queryWithName)) {
61
- const sql = convertToPsycopgNamedArguments (formatQuery (query))
74
+ const sql = convertToPsycopgNamedArguments (formatQueryForPython (query, 20 ))
62
75
const params = extractParamsFromQuery (query);
63
76
const formattedParams = params .length > 0
64
77
// [ "p.categoryId" ] => [ '"categoryId": categoryId' ]
@@ -81,7 +94,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
81
94
< % queries .forEach (queryInfo => {
82
95
for (const [name , query ] of Object .entries (queryInfo)) {
83
96
-% >
84
- cur .execute (" <%- query.sql %>" < %- query .formattedParams % > )
97
+ cur .execute (< %- query .sql % >< %- query .formattedParams % > )
85
98
result[' <%- name %>' ] = cur .fetchone ()[0 ]
86
99
< % }
87
100
})
@@ -92,7 +105,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
92
105
const query = queries[0 ].result
93
106
-% >
94
107
with conn .cursor (cursor_factory = psycopg2 .extras .RealDictCursor ) as cur:
95
- cur .execute (" <%- query.sql %>" < %- query .formattedParams % > )
108
+ cur .execute (< %- query .sql % >< %- query .formattedParams % > )
96
109
result = cur .fetchone ()
97
110
if result is None:
98
111
raise HTTPException (status_code= 404 )
@@ -102,7 +115,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
102
115
const query = queries[0 ].result
103
116
-% >
104
117
with conn .cursor (cursor_factory = psycopg2 .extras .RealDictCursor ) as cur:
105
- cur .execute (" <%- query.sql %>" < %- query .formattedParams % > )
118
+ cur .execute (< %- query .sql % >< %- query .formattedParams % > )
106
119
return cur .fetchall ()
107
120
< %
108
121
}
0 commit comments