summaryrefslogtreecommitdiff
path: root/sqlplain/doc/strip_blanks.py
blob: 25ad41e189cc201d067db2cab69ffae5d06b9c98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Take a table with a primary key and some nullable text fields.
Strip leading spaces in all text fields and
replace the fields which are all blanks with None.

usage:

$ python %s <db-uri> <table>

"""

import os, sys
from sqlplain import connect, util
from sqlplain.table import KTable

def strip(value):
    "value is None or a string"
    if value is None: # do nothing
        return None
    return value.strip() or None    
            
def get_text_fields(conn, table, exclude=()):
    'Returns the text fields of a table, possibly excluding some of them'
    names = []
    for row in util.get_descr(conn, table):
        if row.type_code == 1 and row.name not in exclude:
                names.append(row.name)
    return names

def replace_blanks(conn, tablename):
    # conn.chatty = True
    pkeys = util.get_kfields(conn, tablename)
    textfields = get_text_fields(conn, tablename, exclude=pkeys)
    tbl = KTable.type(tablename, pkeys, textfields)(conn)
    for row in tbl.select():
        kw = dict((tf, strip(getattr(row, tf))) for tf in textfields)
        newrow = row._replace(**kw)
        if newrow != row:
            tbl.update_row(newrow)
            print newrow
        else:
            sys.stdout.write('.')
            sys.stdout.flush()

if __name__ == '__main__':
    try:
        uri, tablename = sys.argv[1:]
    except ValueError:
        sys.exit(__doc__ % sys.argv[0])
    replace_blanks(connect(uri), tablename)