Project

General

Profile

« Previous | Next » 

Revision 2085

sql.py: Added run_query_into() and use it in insert_select()

View differences:

lib/sql.py
254 254
##### Querying
255 255

  
256 256
def run_raw_query(db, *args, **kw_args):
257
    '''For args, see DbConn.run_query()'''
257
    '''For params, see DbConn.run_query()'''
258 258
    return db.run_query(*args, **kw_args)
259 259

  
260 260
def mogrify(db, query, params):
......
286 286

  
287 287
##### Basic queries
288 288

  
289
def run_query_into(db, query, params, into=None, *args, **kw_args):
290
    '''Outputs a query to a temp table.
291
    For params, see run_query().
292
    '''
293
    if into == None: return run_query(db, query, params, *args, **kw_args)
294
    else: # place rows in temp table
295
        check_name(into)
296
        
297
        run_query(db, 'DROP TABLE IF EXISTS '+into+' CASCADE', *args, **kw_args)
298
        return run_query(db, 'CREATE TEMP TABLE '+into+' AS '+query, params,
299
            *args, **kw_args) # CREATE TABLE sets rowcount to # rows in query
300

  
289 301
def mk_select(db, table, fields=None, conds=None, limit=None, start=None,
290 302
    table_is_esc=False):
291 303
    '''
......
390 402
    return (query, params)
391 403

  
392 404
def insert_select(db, *args, **kw_args):
393
    '''For params, see mk_insert_select() and run_query()
405
    '''For params, see mk_insert_select() and run_query_into()
394 406
    @param into Name of temp table to place RETURNING values in
395 407
    '''
396 408
    into = kw_args.pop('into', None)
......
399 411
    cacheable = kw_args.pop('cacheable', True)
400 412
    
401 413
    query, params = mk_insert_select(db, *args, **kw_args)
402
    if into == None: # return RETURNING values
403
        return run_query(db, query, params, recover, cacheable)
404
    else: # place RETURNING values in temp table
405
        run_query(db, 'DROP TABLE IF EXISTS '+into+' CASCADE', recover,
406
            cacheable)
407
        return run_query(db, 'CREATE TEMP TABLE '+into+' AS '+query, params,
408
            recover, cacheable) # CREATE TABLE sets rowcount to # inserts
414
    return run_query_into(db, query, params, into, recover, cacheable)
409 415

  
410 416
default = object() # tells insert() to use the default value for a column
411 417

  
412 418
def insert(db, table, row, *args, **kw_args):
413
    '''For args, see insert_select()'''
419
    '''For params, see insert_select()'''
414 420
    if lists.is_seq(row): cols = None
415 421
    else:
416 422
        cols = row.keys()

Also available in: Unified diff