Project

General

Profile

« Previous | Next » 

Revision 3464

sql_io.py: cast(): Refactored to use sql_gen.FunctionDef

View differences:

lib/sql_io.py
101 101
        function_name = str(sql_gen.FunctionCall(function_name, *srcs))
102 102
    function = db.TempFunction(function_name)
103 103
    
104
    while True:
105
        # Create function definition
106
        query = '''\
107
CREATE FUNCTION '''+function.to_str(db)+'''(value text)
108
RETURNS '''+type_+'''
109
LANGUAGE plpgsql
110
'''
111
        if not save_errors: query += 'IMMUTABLE '
112
        query += '''\
113
STRICT
114
AS $$
115
'''
116
        query += data_exception_handler(db, srcs, errors_table).to_str(db, '''\
104
    # Create function definition
105
    modifiers = 'STRICT'
106
    if not save_errors: modifiers = 'IMMUTABLE '+modifiers
107
    handler = data_exception_handler(db, srcs, errors_table)
108
    body = sql_gen.CustomCode(handler.to_str(db, '''\
117 109
    /* The explicit cast to the return type is needed to make the cast happen
118 110
    inside the try block. (Implicit casts to the return type happen at the end
119 111
    of the function, outside any block.) */
120 112
    RETURN value::'''+type_+''';
121
''')
122
        query += '''\
123
$$;
124
'''
125
        
126
        # Create function
113
'''))
114
    body.lang='plpgsql'
115
    def_ = sql_gen.FunctionDef(function, type_, body, ['value text'], modifiers)
116
    
117
    # Create function
118
    while True:
127 119
        try:
128
            sql.run_query(db, query, recover=True, cacheable=True,
120
            sql.run_query(db, def_.to_str(db), recover=True, cacheable=True,
129 121
                log_ignore_excs=(sql.DuplicateException,))
130 122
            break # successful
131 123
        except sql.DuplicateException:

Also available in: Unified diff