Revision 3010
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
631 | 631 |
''' |
632 | 632 |
table = sql_gen.remove_table_rename(sql_gen.as_Table(table)) |
633 | 633 |
if cols == []: cols = None # no cols (all defaults) = unknown col names |
634 |
if cols != None: |
|
635 |
cols = [sql_gen.to_name_only_col(v, table).to_str(db) for v in cols] |
|
634 |
if cols != None: cols = [sql_gen.to_name_only_col(c, table) for c in cols] |
|
636 | 635 |
if select_query == None: select_query = 'DEFAULT VALUES' |
637 | 636 |
if returning != None: returning = sql_gen.as_Col(returning, table) |
638 | 637 |
|
... | ... | |
640 | 639 |
|
641 | 640 |
def mk_insert(select_query): |
642 | 641 |
query = first_line |
643 |
if cols != None: query += '\n('+', '.join(cols)+')' |
|
642 |
if cols != None: |
|
643 |
query += '\n('+(', '.join((c.to_str(db) for c in cols)))+')' |
|
644 | 644 |
query += '\n'+select_query |
645 | 645 |
|
646 | 646 |
if returning != None: |
... | ... | |
654 | 654 |
embeddable = True # must use function |
655 | 655 |
lang = 'plpgsql' |
656 | 656 |
|
657 |
assert cols != None |
|
658 |
row_cols = [sql_gen.Col(c.name, 'row') for c in cols] |
|
659 |
row_query = mk_insert(sql_gen.Values(row_cols).to_str(db)) |
|
660 |
|
|
657 | 661 |
query = '''\ |
662 |
DECLARE |
|
663 |
row record; |
|
664 |
cur CURSOR FOR |
|
665 |
'''+select_query+''' |
|
666 |
; |
|
658 | 667 |
BEGIN |
659 |
END; |
|
668 |
OPEN cur; |
|
669 |
FOUND := true; -- for initial check |
|
670 |
WHILE FOUND LOOP |
|
671 |
FETCH FROM cur INTO row; |
|
672 |
'''+row_query+''' |
|
673 |
; |
|
674 |
END LOOP; |
|
675 |
CLOSE cur; |
|
676 |
END;\ |
|
660 | 677 |
''' |
661 | 678 |
else: query = mk_insert(select_query) |
662 | 679 |
|
Also available in: Unified diff
sql.py: mk_insert_select(): INSERT IGNORE: Implemented simple cursor loop that just inserts each row, without ignoring duplicate keys