Project

General

Profile

« Previous | Next » 

Revision 3501

sql_io.py: data_exception_handler(): Require the caller to provide a statement to return a default value in case of error, rather than assuming the caller can accept a return value of NULL

View differences:

lib/sql_io.py
37 37
                cacheable=True, log_level=4)
38 38
        except sql.DuplicateKeyException: pass
39 39

  
40
def data_exception_handler(db, srcs=[], errors_table=None):
40
def data_exception_handler(db, return_, srcs=[], errors_table=None):
41 41
    '''Handles a data_exception by saving the error or converting it to a
42 42
    warning, and returning NULL.
43
    @param return_ Statement to return a default value in case of error
43 44
    @param srcs The column names for the errors table
44 45
    @param errors_table None|sql_gen.Table
45 46
    @pre The invalid value must be in a local variable "value" of type text.
46 47
    '''
48
    return_ = sql_gen.as_Code(return_)
49
    
47 50
    save_errors = errors_table != None and srcs
48 51
    handler = ''
49 52
    if save_errors:
......
63 66
'''+strings.indent(sql_gen.RowExcIgnore('text', col_names_query, insert_query,
64 67
    row_var=errors_table_cols[0]).to_str(db))+'''
65 68
END;
69

  
66 70
'''
67 71
    else:
68 72
        handler += '''\
69 73
RAISE WARNING '%', SQLERRM;
70 74
'''
71
    handler += '''\
72
RETURN NULL;
73
'''
75
    handler += return_.to_str(db)
74 76
    return sql_gen.ExcHandler('data_exception', handler)
75 77

  
76 78
def cast(db, type_, col, errors_table=None):
......
99 101
    # Create function definition
100 102
    modifiers = 'STRICT'
101 103
    if not save_errors: modifiers = 'IMMUTABLE '+modifiers
102
    handler = data_exception_handler(db, srcs, errors_table)
104
    handler = data_exception_handler(db, 'RETURN NULL;\n', srcs, errors_table)
103 105
    body = sql_gen.CustomCode(handler.to_str(db, '''\
104 106
/* The explicit cast to the return type is needed to make the cast happen
105 107
inside the try block. (Implicit casts to the return type happen at the end

Also available in: Unified diff