File: sql2extension.py

package info (click to toggle)
postgresql-pgmp 1.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 584 kB
  • sloc: ansic: 2,053; sql: 853; python: 589; makefile: 100; sh: 15
file content (189 lines) | stat: -rwxr-xr-x 6,161 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
"""Generate "ALTER EXTENSION" statements to package a list of SQL definitions.

The script doesn't try to be a robust parser: it relies on the input file
being regular enough.

The script is also incomplete, but it complains loudly if it meets elements
it doesn't know how to deal with.
"""

# Copyright (c) 2011-2020, Daniele Varrazzo <daniele.varrazzo@gmail.com>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# * Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
# * The name of Daniele Varrazzo may not be used to endorse or promote
#   products derived from this software without specific prior written
#   permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import re
import sys

re_stmt = re.compile(
    r'CREATE\s+(?:OR\s+REPLACE\s+)?(\w+)\b([^;]+);',
    re.MULTILINE | re.IGNORECASE)

def process_file(f, opt):
    data = f.read()
    # Clean up parts we don't care about and that make parsing more complex
    data = strip_comments(data)
    data = strip_strings(data)

    for m in re_stmt.finditer(data):
        try:
            f = globals()['process_' + m.group(1).lower()]
        except:
            # TODO: all the missing statements
            raise KeyError("can't process statement 'CREATE %s'" %
                (m.group(1).upper(),))

        f(m.group(2), opt.extname)

def process_aggregate(body, extname):
    # TODO: parse the "old syntax"
    name = _find_name(body)
    args = _find_args(body)
    print("ALTER EXTENSION %s ADD AGGREGATE %s %s;" % (extname, name, args))

def process_cast(body, extname):
    args = _find_args(body)
    print("ALTER EXTENSION %s ADD CAST %s;" % (extname, args))

def process_function(body, extname):
    name = _find_name(body)
    args = _find_args(body)
    print("ALTER EXTENSION %s ADD FUNCTION %s %s;" % (extname, name, args))

def process_operator(body, extname):
    if body.lstrip().lower().startswith('class'):
        return process_operator_class(body, extname)

    m = re.match(r'^\s*([^\s\(]+)\s*\(', body)
    if m is None:
        raise ValueError("can't find operator:\n%s" % body)

    op = m.group(1)
    m = re.search(r'LEFTARG\s*=\s*([^,\)]+)', body, re.IGNORECASE)
    larg = m and m.group(1).strip()

    m = re.search(r'RIGHTARG\s*=\s*([^,\)]+)', body, re.IGNORECASE)
    rarg = m and m.group(1).strip()

    if not (larg or rarg):
        raise ValueError("can't find operator arguments:\n%s" % body)

    print("ALTER EXTENSION %s ADD OPERATOR %s (%s, %s);" % (
        extname, op, larg or 'NONE', rarg or 'NONE'))

def process_operator_class(body, extname):
    m = re.match(r'^\s*CLASS\s*(\w+)\b.*?USING\s+(\w+)\b',
        body, re.IGNORECASE | re.DOTALL)

    if m is None:
        raise ValueError("can't parse operator class:\n%s" % body)

    print("ALTER EXTENSION %s ADD OPERATOR CLASS %s USING %s;" % (
        extname, m.group(1), m.group(2)))

def process_type(body, extname):
    name = _find_name(body)
    print("ALTER EXTENSION %s ADD TYPE %s;" % (
        extname, name))

re_name = re.compile(r'^\s*(\w+)\b')

def _find_name(body):
    m = re_name.match(body)
    if m is None:
        raise ValueError("can't find name:\n%s" % body)

    return m.group(1)

def _find_args(body):
    # find the closing brace of the arguments list
    # count the braces to avoid getting fooled by type modifiers
    # e.g. varchar(10)
    count = 0
    for i, c in enumerate(body):
        if c == '(':
            count += 1
        elif c == ')':
            count -= 1
            if count == 0:
                break
    else:
        raise ValueError("failed to parse arguments list:\n%s")

    astart = body.index('(')
    aend = i + 1

    return ' '.join(body[astart:aend].split())


re_comment_single = re.compile(r'--.*?$', re.MULTILINE)
re_comment_multi = re.compile(r'/\*.*?\*/', re.DOTALL)

def strip_comments(s):
    """Remove SQL comments from a string.
    
    TODO: doesn't handle nested comments.
    """
    s = re_comment_single.sub("''", s)
    s = re_comment_multi.sub("''", s)
    return s


re_string_quote = re.compile(r"'(''|[^'])*'")
re_string_dollar = re.compile(r'\$([^$]*)\$.*?\$\1\$')

def strip_strings(s):
    """Replace all the SQL literal strings with the empty string."""
    s = re_string_quote.sub('', s)
    s = re_string_dollar.sub('', s)
    return s


def main():
    opt = parse_options()
    print("-- This file was automatically generated")
    print("-- by the script '%s'" % __file__)
    print("-- from input files:", ", ".join(opt.filenames))
    print()
    for fn in opt.filenames:
        f = fn == '-' and sys.stdin or open(fn)
        process_file(f, opt)

def parse_options():
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option('--extname')
    opt, args = parser.parse_args()
    if not opt.extname:
        parser.error("extension name must be specified")
    opt.filenames = args or ['-']
    return opt

if __name__ == '__main__':
    main()