Project

General

Profile

« Previous | Next » 

Revision 2936

sql.py: Added cast_temp_col()

View differences:

sql.py
1018 1018
    be the primary key.'''
1019 1019
    add_col(db, table, row_num_typed_col, log_level=3)
1020 1020

  
1021
def cast_temp_col(db, type_, col, errors_table=None):
1022
    '''Like cast(), but creates a new column with the cast values if the input
1023
    is a column.
1024
    @return The new column or cast value
1025
    '''
1026
    def cast_(col): return cast(db, type_, col, errors_table)
1027
    
1028
    try: col = sql_gen.underlying_col(col)
1029
    except sql_gen.NoUnderlyingTableException: return sql_gen.wrap(cast_, col)
1030
    
1031
    table = col.table
1032
    new_col = sql_gen.Col(sql_gen.concat(col.name, '::'+type_), table, col.srcs)
1033
    expr = cast_(col)
1034
    add_col(db, table, sql_gen.TypedCol(new_col.name, type_))
1035
    update(db, table, [(new_col, expr)])
1036
    
1037
    return new_col
1038

  
1021 1039
def drop_table(db, table):
1022 1040
    table = sql_gen.as_Table(table)
1023 1041
    return run_query(db, 'DROP TABLE IF EXISTS '+table.to_str(db)+' CASCADE')

Also available in: Unified diff