Revision 3014
Added by Aaron Marcuse-Kubitza about 12 years ago
lib/sql.py | ||
---|---|---|
636 | 636 |
if returning != None: returning = sql_gen.as_Col(returning, table) |
637 | 637 |
|
638 | 638 |
first_line = 'INSERT INTO '+table.to_str(db) |
639 |
if cols != None: cols_str = ', '.join((c.to_str(db) for c in cols)) |
|
640 | 639 |
|
641 | 640 |
def mk_insert(select_query): |
642 | 641 |
query = first_line |
643 |
if cols != None: query += '\n('+cols_str+')' |
|
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: |
... | ... | |
655 | 655 |
lang = 'plpgsql' |
656 | 656 |
|
657 | 657 |
assert cols != None |
658 |
typed_cols = [sql_gen.TypedCol(c.name, db.col_info( |
|
659 |
sql_gen.with_default_table(c, table)).type) for c in cols] |
|
658 |
row = [sql_gen.Col(c.name, 'row') for c in cols] |
|
660 | 659 |
|
661 | 660 |
query = '''\ |
662 | 661 |
DECLARE |
663 |
''' |
|
664 |
for col in typed_cols: |
|
665 |
query += '''\ |
|
666 |
'''+col.to_str(db)+'''; |
|
667 |
''' |
|
668 |
query += '''\ |
|
662 |
row '''+table.to_str(db)+'''%ROWTYPE; |
|
669 | 663 |
cur CURSOR FOR |
670 | 664 |
'''+select_query+''' |
671 | 665 |
; |
672 | 666 |
BEGIN |
673 | 667 |
OPEN cur; |
674 |
FOUND := true; -- for initial check |
|
675 |
WHILE FOUND LOOP |
|
676 |
FETCH FROM cur INTO '''+cols_str+'''; |
|
677 |
'''+mk_insert(sql_gen.Values(cols).to_str(db))+''' |
|
668 |
WHILE true LOOP |
|
669 |
FETCH FROM cur INTO '''+(', '.join((c.to_str(db) for c in row)))+'''; |
|
670 |
EXIT WHEN NOT FOUND; |
|
671 |
|
|
672 |
'''+mk_insert(sql_gen.Values(row).to_str(db))+''' |
|
678 | 673 |
; |
679 | 674 |
END LOOP; |
680 | 675 |
CLOSE cur; |
Also available in: Unified diff
sql.py: mk_insert_select(): INSERT IGNORE: Fixed bug where user-defined types were not supported correctly, by referencing the column's type directly in PL/pgSQL instead of retrieving it from the information_schema. Use a row variable of the output table's row type to store the select query data, so that you don't need to create a separate local var for each output column.