Revision 3112
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql_io.py | ||
---|---|---|
43 | 43 |
@param errors_table None|sql_gen.Table|str |
44 | 44 |
''' |
45 | 45 |
col = sql_gen.as_Col(col) |
46 |
save_errors = (errors_table != None and isinstance(col, sql_gen.Col) |
|
47 |
and col.srcs != ()) |
|
48 |
if not save_errors: return sql_gen.Cast(type_, col) # can't save errors |
|
49 | 46 |
|
47 |
# Don't convert exceptions to warnings for user-supplied constants |
|
48 |
if isinstance(col, sql_gen.Literal): return sql_gen.Cast(type_, col) |
|
49 |
|
|
50 | 50 |
assert not isinstance(col, sql_gen.NamedCol) |
51 | 51 |
|
52 |
errors_table = sql_gen.as_Table(errors_table) |
|
53 |
srcs = map(sql_gen.to_name_only_col, col.srcs) |
|
54 |
function_name = str(sql_gen.FunctionCall(type_, *srcs)) |
|
52 |
save_errors = (errors_table != None and isinstance(col, sql_gen.Col) |
|
53 |
and col.srcs != ()) |
|
54 |
function_name = type_ |
|
55 |
if save_errors: |
|
56 |
errors_table = sql_gen.as_Table(errors_table) |
|
57 |
|
|
58 |
srcs = map(sql_gen.to_name_only_col, col.srcs) |
|
59 |
function_name = str(sql_gen.FunctionCall(function_name, *srcs)) |
|
55 | 60 |
function = db.TempFunction(function_name) |
56 | 61 |
|
57 | 62 |
while True: |
58 | 63 |
# Create function definition |
59 |
errors_table_cols = map(sql_gen.Col, |
|
60 |
['column', 'value', 'error_code', 'error']) |
|
61 | 64 |
query = '''\ |
62 | 65 |
CREATE FUNCTION '''+function.to_str(db)+'''(value text) |
63 | 66 |
RETURNS '''+type_+''' |
64 | 67 |
LANGUAGE plpgsql |
68 |
''' |
|
69 |
if not save_errors: query += 'IMMUTABLE ' |
|
70 |
query += '''\ |
|
65 | 71 |
STRICT |
66 | 72 |
AS $$ |
67 | 73 |
BEGIN |
... | ... | |
71 | 77 |
RETURN value::'''+type_+'''; |
72 | 78 |
EXCEPTION |
73 | 79 |
WHEN data_exception THEN |
80 |
''' |
|
81 |
if save_errors: |
|
82 |
errors_table_cols = map(sql_gen.Col, |
|
83 |
['column', 'value', 'error_code', 'error']) |
|
84 |
query += '''\ |
|
74 | 85 |
-- Save error in errors table. |
75 | 86 |
DECLARE |
76 | 87 |
error_code text := SQLSTATE; |
... | ... | |
91 | 102 |
END LOOP; |
92 | 103 |
END; |
93 | 104 |
|
105 |
''' |
|
106 |
query += '''\ |
|
94 | 107 |
RAISE WARNING '%', SQLERRM; |
95 | 108 |
RETURN NULL; |
96 | 109 |
END; |
Also available in: Unified diff
sql_io.py: cast(): Always convert exceptions to warnings if the input is a column or expression, even if there is no place to save the errors, so that invalid data does not need to be handled by the caller in a (much slower) extra exception-handling loop