Project

General

Profile

« Previous | Next » 

Revision 2153

sql.py: run_query_into(): If CREATE TABLE AS generates a DuplicateTableException, rename the table with a version # prepended

View differences:

lib/sql.py
326 326

  
327 327
##### Basic queries
328 328

  
329
def next_version(name):
330
    '''Prepends the version # so it won't be removed if the name is truncated'''
331
    version = 0
332
    match = re.match(r'^v(\d+)_(.*)$', name)
333
    if match:
334
        version = int(match.group(1))+1
335
        name = match.group(2)
336
    return 'v'+str(version)+'_'+name
337

  
329 338
def run_query_into(db, query, params, into_ref=None, *args, **kw_args):
330 339
    '''Outputs a query to a temp table.
331 340
    For params, see run_query().
......
333 342
    if into_ref == None: return run_query(db, query, params, *args, **kw_args)
334 343
    else: # place rows in temp table
335 344
        check_name(into_ref[0])
336
        return run_query(db, 'CREATE TEMP TABLE '+into_ref[0]+' AS '+query,
337
            params, *args, **kw_args) # CREATE TABLE AS sets rowcount to # rows
345
        kw_args['recover'] = True
346
        while True:
347
            try:
348
                return run_query(db, 'CREATE TEMP TABLE '+into_ref[0]+' AS '
349
                    +query, params, *args, **kw_args)
350
                    # CREATE TABLE AS sets rowcount to # rows in query
351
            except DuplicateTableException, e:
352
                into_ref[0] = next_version(into_ref[0])
353
                # try again with next version of name
338 354

  
339 355
order_by_pkey = object() # tells mk_select() to order by the pkey
340 356

  
......
483 499
    cacheable = kw_args.pop('cacheable', True)
484 500
    
485 501
    query, params = mk_insert_select(db, *args, **kw_args)
486
    return run_query_into(db, query, params, into_ref, recover, cacheable)
502
    return run_query_into(db, query, params, into_ref, recover=recover,
503
        cacheable=cacheable)
487 504

  
488 505
default = object() # tells insert() to use the default value for a column
489 506

  

Also available in: Unified diff