Revision 1960
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/sql.py | ||
---|---|---|
7 | 7 |
import exc |
8 | 8 |
import dicts |
9 | 9 |
import iters |
10 |
import lists |
|
10 | 11 |
from Proxy import Proxy |
11 | 12 |
import rand |
12 | 13 |
import strings |
... | ... | |
301 | 302 |
|
302 | 303 |
return run_query(db, query, conds.values(), recover, cacheable) |
303 | 304 |
|
304 |
def insert(db, table, row, returning=None, recover=None, cacheable=True): |
|
305 |
'''@param returning str|None An inserted column (such as pkey) to return''' |
|
306 |
check_name(table) |
|
307 |
cols = row.keys() |
|
308 |
map(check_name, cols) |
|
305 |
def insert(db, table, row, returning=None, recover=None, cacheable=True, |
|
306 |
table_is_esc=False): |
|
307 |
''' |
|
308 |
@param returning str|None An inserted column (such as pkey) to return |
|
309 |
@param table_is_esc Whether the table name has already been escaped |
|
310 |
''' |
|
311 |
if not table_is_esc: check_name(table) |
|
312 |
if lists.is_seq(row): cols = None |
|
313 |
else: |
|
314 |
cols = row.keys() |
|
315 |
row = row.values() |
|
316 |
map(check_name, cols) |
|
317 |
row = list(row) # ensure that "!= []" works |
|
318 |
|
|
309 | 319 |
query = 'INSERT INTO '+table |
310 |
|
|
311 |
if row != {}: query += ' ('+', '.join(cols)+') VALUES ('\
|
|
312 |
+', '.join(['%s']*len(cols))+')'
|
|
320 |
if row != []: |
|
321 |
if cols != None: query += ' ('+', '.join(cols)+')'
|
|
322 |
query += ' VALUES ('+(', '.join(['%s']*len(row)))+')'
|
|
313 | 323 |
else: query += ' DEFAULT VALUES' |
314 | 324 |
|
315 | 325 |
if returning != None: |
316 | 326 |
check_name(returning) |
317 | 327 |
query += ' RETURNING '+returning |
318 | 328 |
|
319 |
return run_query(db, query, row.values(), recover, cacheable)
|
|
329 |
return run_query(db, query, row, recover, cacheable) |
|
320 | 330 |
|
321 | 331 |
def last_insert_id(db): |
322 | 332 |
module = util.root_module(db.db) |
Also available in: Unified diff
sql.py: insert(): Support rows that are just a list of values, with no columns. Support already-escaped table names.