Project

General

Profile

« Previous | Next » 

Revision 2120

sql.py: mk_select(): Support ORDER BY clause. By default, order by the pkey, since PostgreSQL apparently doesn't do this automatically (and this was causing some staging table tests to fail).

View differences:

sql.py
304 304
        return run_query(db, 'CREATE TEMP TABLE '+into+' AS '+query, params,
305 305
            *args, **kw_args) # CREATE TABLE sets rowcount to # rows in query
306 306

  
307
order_by_pkey = object() # tells mk_select() to order by the pkey
308

  
307 309
def mk_select(db, table, fields=None, conds=None, limit=None, start=None,
308
    table_is_esc=False):
310
    order_by=order_by_pkey, table_is_esc=False):
309 311
    '''
310 312
    @param fields Use None to select all fields in the table
311 313
    @param table_is_esc Whether the table name has already been escaped
......
316 318
    if conds == None: conds = {}
317 319
    assert limit == None or type(limit) == int
318 320
    assert start == None or type(start) == int
321
    if order_by == order_by_pkey:
322
        order_by = pkey(db, table, recover=True, table_is_esc=table_is_esc)
319 323
    if not table_is_esc: table = esc_name_(table)
320 324
    
321 325
    params = []
......
348 352
        query += ' WHERE '+' AND '.join(map(cond, conds.iteritems()))
349 353
        params += conds.values()
350 354
        missing = False
355
    if order_by != None: query += ' ORDER BY '+esc_name_(order_by)
351 356
    if limit != None: query += ' LIMIT '+str(limit); missing = False
352 357
    if start != None:
353 358
        if start != 0: query += ' OFFSET '+str(start)
......
457 462

  
458 463
def pkey(db, table, recover=None, table_is_esc=False):
459 464
    '''Assumed to be first column in table'''
460
    return col_names(select(db, table, limit=0, recover=recover,
465
    return col_names(select(db, table, limit=0, order_by=None, recover=recover,
461 466
        table_is_esc=table_is_esc)).next()
462 467

  
463 468
def index_cols(db, table, index):

Also available in: Unified diff