Revision 2070
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
346 | 346 |
return run_query(db, query, params, recover, cacheable) |
347 | 347 |
|
348 | 348 |
def mk_insert_select(db, table, cols=None, select_query=None, params=None, |
349 |
returning=None, table_is_esc=False): |
|
349 |
returning=None, embeddable=False, table_is_esc=False):
|
|
350 | 350 |
''' |
351 | 351 |
@param returning str|None An inserted column (such as pkey) to return |
352 |
@param embeddable Whether the query should be embeddable as a nested SELECT. |
|
353 |
If you set this, you must ensure cacheable=False when the query is run. |
|
352 | 354 |
@param table_is_esc Whether the table name has already been escaped |
353 | 355 |
''' |
354 | 356 |
if select_query == None: select_query = 'DEFAULT VALUES' |
... | ... | |
366 | 368 |
check_name(returning) |
367 | 369 |
query += ' RETURNING '+returning |
368 | 370 |
|
371 |
if embeddable: |
|
372 |
# Create function |
|
373 |
function = 'pg_temp.'+('_'.join(['insert_returning', table] + cols)) |
|
374 |
return_type = 'SETOF '+table+'.'+returning+'%TYPE' |
|
375 |
function_query = '''\ |
|
376 |
CREATE OR REPLACE FUNCTION '''+function+'''() RETURNS '''+return_type+''' |
|
377 |
LANGUAGE sql |
|
378 |
AS $$'''+mogrify(db, query, params)+''';$$; |
|
379 |
''' |
|
380 |
run_query(db, function_query, cacheable=True) |
|
381 |
|
|
382 |
# Return query that uses function |
|
383 |
return mk_select(db, function+'()', table_is_esc=True) |
|
384 |
|
|
369 | 385 |
return (query, params) |
370 | 386 |
|
371 | 387 |
def insert_select(db, *args, **kw_args): |
Also available in: Unified diff
sql.py: mk_insert_select(): Support using an INSERT RETURNING statement as a nested SELECT