@@ -32,6 +32,19 @@ function convertToFastApiPath(path) {
3232 return path .replace (/ :([_a-zA-Z ] + )/ g , ' {$1}' )
3333}
3434
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+
3548
3649endpoints .forEach (function (endpoint ) {
3750 const path = convertToFastApiPath (endpoint .path )
@@ -58,7 +71,7 @@ endpoints.forEach(function(endpoint) {
5871 const queries = []
5972 queriesWithNames .forEach (queryWithName => {
6073 for (const [name , query ] of Object .entries (queryWithName)) {
61- const sql = convertToPsycopgNamedArguments (formatQuery (query))
74+ const sql = convertToPsycopgNamedArguments (formatQueryForPython (query, 20 ))
6275 const params = extractParamsFromQuery (query);
6376 const formattedParams = params .length > 0
6477 // [ "p.categoryId" ] => [ '"categoryId": categoryId' ]
@@ -81,7 +94,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
8194< % queries .forEach (queryInfo => {
8295 for (const [name , query ] of Object .entries (queryInfo)) {
8396-% >
84- cur .execute (" <%- query.sql %>" < %- query .formattedParams % > )
97+ cur .execute (< %- query .sql % >< %- query .formattedParams % > )
8598 result[' <%- name %>' ] = cur .fetchone ()[0 ]
8699< % }
87100 })
@@ -92,7 +105,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
92105 const query = queries[0 ].result
93106-% >
94107 with conn .cursor (cursor_factory = psycopg2 .extras .RealDictCursor ) as cur:
95- cur .execute (" <%- query.sql %>" < %- query .formattedParams % > )
108+ cur .execute (< %- query .sql % >< %- query .formattedParams % > )
96109 result = cur .fetchone ()
97110 if result is None:
98111 raise HTTPException (status_code= 404 )
@@ -102,7 +115,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
102115 const query = queries[0 ].result
103116-% >
104117 with conn .cursor (cursor_factory = psycopg2 .extras .RealDictCursor ) as cur:
105- cur .execute (" <%- query.sql %>" < %- query .formattedParams % > )
118+ cur .execute (< %- query .sql % >< %- query .formattedParams % > )
106119 return cur .fetchall ()
107120< %
108121 }
0 commit comments