Revision 2718
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
775 | 775 |
else: function_name = type_ |
776 | 776 |
function = sql_gen.TempFunction(function_name, db.autocommit) |
777 | 777 |
|
778 |
query = '''\ |
|
778 |
while True: |
|
779 |
# Create function definition |
|
780 |
query = '''\ |
|
779 | 781 |
CREATE FUNCTION '''+function.to_str(db)+'''(value text) |
780 | 782 |
RETURNS '''+type_+''' |
781 | 783 |
LANGUAGE plpgsql |
782 | 784 |
''' |
783 |
if not save_errors: query += 'IMMUTABLE\n' |
|
784 |
query += '''\ |
|
785 |
if not save_errors: query += 'IMMUTABLE\n'
|
|
786 |
query += '''\
|
|
785 | 787 |
STRICT |
786 | 788 |
AS $$ |
787 | 789 |
BEGIN |
... | ... | |
792 | 794 |
EXCEPTION |
793 | 795 |
WHEN data_exception THEN |
794 | 796 |
''' |
795 |
if save_errors: |
|
796 |
cols = [sql_gen.Literal(c.name) for c in srcs] |
|
797 |
query += '''\ |
|
797 |
if save_errors:
|
|
798 |
cols = [sql_gen.Literal(c.name) for c in srcs]
|
|
799 |
query += '''\
|
|
798 | 800 |
-- Save error in errors table. |
799 | 801 |
BEGIN |
800 | 802 |
INSERT INTO '''+errors_table.to_str(db)+''' |
... | ... | |
809 | 811 |
END; |
810 | 812 |
|
811 | 813 |
''' |
812 |
query += '''\ |
|
814 |
query += '''\
|
|
813 | 815 |
RAISE WARNING '%', SQLERRM; |
814 | 816 |
RETURN NULL; |
815 | 817 |
END; |
816 | 818 |
$$; |
817 | 819 |
''' |
818 |
try: |
|
819 |
run_query(db, query, recover=True, cacheable=True, |
|
820 |
log_ignore_excs=(DuplicateFunctionException,)) |
|
821 |
except DuplicateFunctionException: pass # function already existed |
|
820 |
|
|
821 |
# Create function |
|
822 |
try: |
|
823 |
run_query(db, query, recover=True, cacheable=True, |
|
824 |
log_ignore_excs=(DuplicateFunctionException,)) |
|
825 |
break # successful |
|
826 |
except DuplicateFunctionException: |
|
827 |
if save_errors: function.name = next_version(function.name) |
|
828 |
# try again with next version of name |
|
829 |
else: break # plain cast function, so only need one version |
|
822 | 830 |
|
823 | 831 |
return sql_gen.FunctionCall(function, col) |
824 | 832 |
|
Also available in: Unified diff
sql.py: cast(): Version the function name if using an errors table, to avoid collisions with other cast functions when the function name is truncated (or, more rarely, collisions with casts to the same type and on the same input columns but of a different table)