File: checks.py

package info (click to toggle)
sql-editor 0.4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 116 kB
  • ctags: 140
  • sloc: python: 1,091; makefile: 46; sh: 28
file content (69 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download | duplicates (3)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import re
from copy import copy


#this performs a list of checks
def check_list_of_checks(checks,table_info,row):
    msgs=[]
    for ch in checks:
        ( row, m ) = ch(table_info,row)
        msgs=msgs+m
    #formatted version
    msg=""
    inter=""
    for m in msgs:
        msg=msg+inter+m
        if m[0]=="E":
            row=None
        inter="\n"
    return ( row, msgs,msg )


#these functions  will check the row before update/insert, and
#return a (newrow, msgs)
# where msgs is a list of strings , each starting by "W" or "E" (for warning or error)

def extra_spaces(table_info,row):
    msgs=[]
    newrow=copy(row)    
    for j in range(table_info.n_fields):
        f=table_info.fields[j]
        #columninfo=table_info.field_columninfo[f]
        #(Field, Type, Null, Key, Default, Extra) = columninfo
        e=row[j]
        if type(e) == type(""):
            n=re.sub('^[ \t]*','',e)
            n=re.sub('[ \t]*$','',n)
            if n != e:
                msgs.append("W:"+f+": deleted extra spaces")
            newrow[j]=n
            e=n
    return (newrow,msgs)

def empty_null(table_info,row):
    msgs=[]
    newrow=copy(row)    
    for j in range(table_info.n_fields):
        f=table_info.fields[j]
        #FIXME non portable
        columninfo=table_info.field_columninfo[f]
        (Field, Type, Null, Key, Default, Extra) = columninfo
        e=row[j]
        if e != None and e == "" and Null:
            msgs.append("W:"+f+": is empty: converted to NULL")
            newrow[j]=None
    return (newrow,msgs)

def not_null_not_empty(table_info,row):
    msgs=[]
    newrow=copy(row)    
    for j in range(table_info.n_fields):
        f=table_info.fields[j]
        #FIXME non portable
        columninfo=table_info.field_columninfo[f]
        (Field, Type, Null, Key, Default, Extra) = columninfo
        e=row[j]
        if e != None and e == "" and not Null and j != table_info.n_primary:
            msgs.append("E:"+f+": is empty")
    return (newrow,msgs)