Project

General

Profile

« Previous | Next » 

Revision 1961

sql.py: insert(): Support using the default value for columns designated with the special value sql.default

View differences:

lib/sql.py
302 302
    
303 303
    return run_query(db, query, conds.values(), recover, cacheable)
304 304

  
305
default = object() # tells insert() to use the default value for a column
306

  
305 307
def insert(db, table, row, returning=None, recover=None, cacheable=True,
306 308
    table_is_esc=False):
307 309
    '''
......
316 318
        map(check_name, cols)
317 319
    row = list(row) # ensure that "!= []" works
318 320
    
321
    # Check for special values
322
    labels = []
323
    values = []
324
    for value in row:
325
        if value == default: labels.append('DEFAULT')
326
        else:
327
            labels.append('%s')
328
            values.append(value)
329
    
330
    # Build query
319 331
    query = 'INSERT INTO '+table
320
    if row != []:
332
    if values != []:
321 333
        if cols != None: query += ' ('+', '.join(cols)+')'
322
        query += ' VALUES ('+(', '.join(['%s']*len(row)))+')'
334
        query += ' VALUES ('+(', '.join(labels))+')'
323 335
    else: query += ' DEFAULT VALUES'
324 336
    
325 337
    if returning != None:
326 338
        check_name(returning)
327 339
        query += ' RETURNING '+returning
328 340
    
329
    return run_query(db, query, row, recover, cacheable)
341
    return run_query(db, query, values, recover, cacheable)
330 342

  
331 343
def last_insert_id(db):
332 344
    module = util.root_module(db.db)

Also available in: Unified diff