summaryrefslogtreecommitdiff
path: root/scripts/find_sql_functions.py
blob: 55718cd86546c6047dc790a3022f87acf7a1d03d (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/env python

"""Find and print out function signatures from .sql file.

Usage:
    find_sql_functions.py [-h] [-s] [-p PREFIX] FILE ...

Switches:
    -h         Show help
    -p PREFIX  Prefix each line with string
    -s         Check whether function is SECURITY DEFINER
"""

import sys, re, getopt

rx = r"""
^
create \s+ (?: or \s+ replace \s+ )?
function ( [^(]+ )
[(] ( [^)]* ) [)]
"""

rx_secdef = r"""security\s+definer"""


rc = re.compile(rx, re.I | re.M | re.X)
sc = re.compile(r"\s+")
rc_sec = re.compile(rx_secdef, re.I | re.X)

def grep_file(fn, cf_prefix, cf_secdef):
    sql = open(fn).read()
    pos = 0
    while 1:
        m = rc.search(sql, pos)
        if not m:
            break
        pos = m.end()

        m2 = rc.search(sql, pos)
        if m2:
            xpos = m2.end()
        else:
            xpos = len(sql)
        secdef = False
        m2 = rc_sec.search(sql, pos, xpos)
        if m2:
            secdef = True

        fname = m.group(1).strip()
        fargs = m.group(2)

        alist = fargs.split(',')
        tlist = []
        for a in alist:
            a = a.strip()
            toks = sc.split(a.lower())
            if toks[0] == "out":
                continue
            if toks[0] in ("in", "inout"):
                toks = toks[1:]
            # just take last item
            tlist.append(toks[-1])

        sig = "%s(%s)" % (fname, ", ".join(tlist))

        if cf_prefix:
            ln = "%s %s;" % (cf_prefix, sig)
        else:
            ln = "    %s(%s)," % (fname, ", ".join(tlist))

        if cf_secdef and secdef:
            ln = "%-72s -- SECDEF" % (ln)

        print ln

def main(argv):
    cf_secdef = 0
    cf_prefix = ''

    try:
        opts, args = getopt.getopt(argv, "hsp:")
    except getopt.error, d:
        print 'getopt:', d
        sys.exit(1)

    for o, a in opts:
        if o == '-h':
            print __doc__
            sys.exit(0)
        elif o == '-s':
            cf_secdef = 1
        elif o == '-p':
            cf_prefix = a
        else:
            print __doc__
            sys.exit(1)

    for fn in args:
        grep_file(fn, cf_prefix, cf_secdef)

if __name__ == '__main__':
    main(sys.argv[1:])