Project

General

Profile

« Previous | Next » 

Revision 2402

sql.py: Added mk_update() and update()

View differences:

sql.py
601 601
    
602 602
    return insert_select(db, table, cols, query, values, *args, **kw_args)
603 603

  
604
def mk_update(db, table, changes=None, cond=None):
605
    '''
606
    @param changes [(col, new_value),...]
607
        * container can be any iterable type
608
        * col: sql_gen.Code|str (for col name)
609
        * new_value: sql_gen.Code|literal value
610
    @param cond sql_gen.Code WHERE condition. e.g. use sql_gen.*Cond objects.
611
    @return str query
612
    '''
613
    query = 'UPDATE '+sql_gen.as_Table(table).to_str(db)+'\nSET\n'
614
    query += ',\n'.join((sql_gen.to_name_only_col(col).to_str(db)+' = '
615
        +sql_gen.as_Value(new_value).to_str(db) for col, new_value in changes))
616
    if cond != None: query += ' WHERE '+cond.to_str(db)
617
    
618
    return query
619

  
620
def update(db, *args, **kw_args):
621
    '''For params, see mk_update() and run_query()'''
622
    recover = kw_args.pop('recover', None)
623
    
624
    return run_query(db, mk_update(db, *args, **kw_args), [], recover)
625

  
604 626
def last_insert_id(db):
605 627
    module = util.root_module(db.db)
606 628
    if module == 'psycopg2': return value(run_query(db, 'SELECT lastval()'))

Also available in: Unified diff