Revision 2942
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
1211 | 1211 |
log_debug('Inserting these input columns into '+strings.as_tt( |
1212 | 1212 |
out_table.to_str(db))+':\n'+strings.as_table(mapping, ustr=col_ustr)) |
1213 | 1213 |
|
1214 |
out_pkey = pkey(db, out_table, recover=True) |
|
1214 |
is_function = function_exists(db, out_table) |
|
1215 |
|
|
1216 |
if is_function: out_pkey = 'result' |
|
1217 |
else: out_pkey = pkey(db, out_table, recover=True) |
|
1215 | 1218 |
out_pkey_col = sql_gen.as_Col(out_pkey, out_table) |
1216 | 1219 |
|
1217 | 1220 |
if mapping == {}: # need at least one column for INSERT SELECT |
... | ... | |
1333 | 1336 |
main_select = mk_main_select(insert_joins, mapping.values()) |
1334 | 1337 |
|
1335 | 1338 |
def main_insert(): |
1336 |
lock_table(db, out_table, 'EXCLUSIVE') |
|
1337 |
return insert_select(db, out_table, mapping.keys(), main_select, |
|
1338 |
**insert_args) |
|
1339 |
if is_function: |
|
1340 |
log_debug('Calling function on input rows') |
|
1341 |
args = dict(((k.name, v) for k, v in mapping.iteritems())) |
|
1342 |
func_call = sql_gen.NamedCol(out_pkey, |
|
1343 |
sql_gen.FunctionCall(out_table, **args)) |
|
1344 |
insert_into_pkeys(input_joins, [in_pkey_col, func_call]) |
|
1345 |
return None |
|
1346 |
else: |
|
1347 |
lock_table(db, out_table, 'EXCLUSIVE') |
|
1348 |
return insert_select(db, out_table, mapping.keys(), main_select, |
|
1349 |
**insert_args) |
|
1339 | 1350 |
|
1340 | 1351 |
try: |
1341 | 1352 |
cur = with_savepoint(db, main_insert) |
... | ... | |
1384 | 1395 |
remove_all_rows() |
1385 | 1396 |
# after exception handled, rerun loop with additional constraints |
1386 | 1397 |
|
1387 |
if row_ct_ref != None and cur.rowcount >= 0: |
|
1398 |
if cur != None and row_ct_ref != None and cur.rowcount >= 0:
|
|
1388 | 1399 |
row_ct_ref[0] += cur.rowcount |
1389 | 1400 |
|
1390 |
if has_joins: |
|
1401 |
if is_function: pass # pkeys table already created |
|
1402 |
elif has_joins: |
|
1391 | 1403 |
select_joins = input_joins+[sql_gen.Join(out_table, join_cols)] |
1392 | 1404 |
log_debug('Getting output table pkeys of existing/inserted rows') |
1393 | 1405 |
insert_into_pkeys(select_joins, pkeys_cols) |
Also available in: Unified diff
sql.py: put_table(): Support plain SQL functions in addition to relational functions