Project

General

Profile

« Previous | Next » 

Revision 4209

sql_io.py: cleanup_table(): Refactored to use for loop with array constant, so that additional NULL-equivalent strings can easily be added

View differences:

lib/sql_io.py
18 18

  
19 19
##### Data cleanup
20 20

  
21
null_strs = ['', r'\N']
22

  
21 23
def cleanup_table(db, table, cols):
22 24
    table = sql_gen.as_Table(table)
23 25
    cols = map(sql_gen.as_Col, cols)
24 26
    
25
    expr = ('nullif(nullif(trim(both from %s), '+db.esc_value('')+'), '
26
        +db.esc_value(r'\N')+')')
27
    changes = [(v, sql_gen.CustomCode(expr % v.to_str(db)))
28
        for v in cols]
27
    expr = 'trim(both from %s)'
28
    for null in null_strs: expr = 'nullif('+expr+', '+db.esc_value(null)+')'
29
    changes = [(v, sql_gen.CustomCode(expr % v.to_str(db))) for v in cols]
29 30
    
30 31
    sql.update(db, table, changes, in_place=True)
31 32

  

Also available in: Unified diff