Project

General

Profile

1 2211 aaronmk
# SQL code generation
2
3 2276 aaronmk
import operator
4 2568 aaronmk
import re
5 2276 aaronmk
6 2360 aaronmk
import objects
7 2222 aaronmk
import strings
8 2227 aaronmk
import util
9 2211 aaronmk
10 2587 aaronmk
##### Names
11 2499 aaronmk
12 2608 aaronmk
identifier_max_len = 63 # works for both PostgreSQL and MySQL
13 2587 aaronmk
14
def add_suffix(str_, suffix):
15 2609 aaronmk
    '''Preserves version so that it won't be truncated off the string, leading
16
    to collisions.'''
17
    if len(str_) == identifier_max_len: # preserve version
18
        str_, sep, version = str_.partition('#')
19
        suffix = sep+version+suffix
20 2587 aaronmk
    return strings.add_suffix(str_, suffix, identifier_max_len)
21
22 2575 aaronmk
def is_safe_name(name):
23 2583 aaronmk
    '''A name is safe *and unambiguous* if it:
24
    * contains only *lowercase* word (\w) characters
25
    * doesn't start with a digit
26
    * contains "_", so that it's not a keyword
27
    '''
28
    return re.match(r'^(?=.*_)(?!\d)[^\WA-Z]+$', name)
29 2568 aaronmk
30 2499 aaronmk
def esc_name(name, quote='"'):
31
    return quote + name.replace(quote, quote+quote) + quote
32
        # doubling an embedded quote escapes it in both PostgreSQL and MySQL
33
34 2513 aaronmk
def clean_name(name): return name.replace('"', '').replace('`', '')
35
36 2219 aaronmk
##### SQL code objects
37
38 2349 aaronmk
class MockDb:
39 2503 aaronmk
    def esc_value(self, value): return strings.repr_no_u(value)
40 2349 aaronmk
41 2499 aaronmk
    def esc_name(self, name): return esc_name(name)
42 2349 aaronmk
mockDb = MockDb()
43
44 2514 aaronmk
class BasicObject(objects.BasicObject):
45
    def __init__(self, value): self.value = value
46
47
    def __str__(self): return clean_name(strings.repr_no_u(self))
48
49
class Code(BasicObject):
50 2211 aaronmk
    def to_str(self, db): raise NotImplemented()
51 2349 aaronmk
52 2514 aaronmk
    def __repr__(self): return self.to_str(mockDb)
53 2211 aaronmk
54 2269 aaronmk
class CustomCode(Code):
55 2256 aaronmk
    def __init__(self, str_): self.str_ = str_
56
57
    def to_str(self, db): return self.str_
58
59 2540 aaronmk
class Expr(Code):
60
    def __init__(self, expr): self.expr = expr
61
62
    def to_str(self, db): return '('+self.expr.to_str(db)+')'
63
64 2335 aaronmk
##### Literal values
65
66 2216 aaronmk
class Literal(Code):
67 2211 aaronmk
    def __init__(self, value): self.value = value
68 2213 aaronmk
69
    def to_str(self, db): return db.esc_value(self.value)
70 2211 aaronmk
71 2400 aaronmk
def as_Value(value):
72
    if isinstance(value, Code): return value
73
    else: return Literal(value)
74
75 2216 aaronmk
def is_null(value): return isinstance(value, Literal) and value.value == None
76
77 2335 aaronmk
##### Tables
78
79 2211 aaronmk
class Table(Code):
80
    def __init__(self, name, schema=None):
81
        '''
82
        @param schema str|None (for no schema)
83
        '''
84
        self.name = name
85
        self.schema = schema
86
87 2348 aaronmk
    def to_str(self, db):
88
        str_ = ''
89
        if self.schema != None: str_ += db.esc_name(self.schema)+'.'
90
        str_ += db.esc_name(self.name)
91
        return str_
92 2336 aaronmk
93
    def to_Table(self): return self
94 2211 aaronmk
95 2219 aaronmk
def as_Table(table):
96 2270 aaronmk
    if table == None or isinstance(table, Code): return table
97 2219 aaronmk
    else: return Table(table)
98
99 2336 aaronmk
class NamedTable(Table):
100
    def __init__(self, name, code, cols=None):
101
        Table.__init__(self, name)
102
103
        if not isinstance(code, Code): code = Table(code)
104
105
        self.code = code
106
        self.cols = cols
107
108
    def to_str(self, db):
109 2467 aaronmk
        str_ = self.code.to_str(db)+'\nAS '+Table.to_str(self, db)
110 2336 aaronmk
        if self.cols != None: str_ += ' ('+(', '.join(self.cols))+')'
111
        return str_
112
113
    def to_Table(self): return Table(self.name)
114
115 2335 aaronmk
##### Columns
116
117 2211 aaronmk
class Col(Code):
118
    def __init__(self, name, table=None):
119
        '''
120
        @param table Table|None (for no table)
121
        '''
122 2241 aaronmk
        if util.is_str(table): table = Table(table)
123 2211 aaronmk
        assert table == None or isinstance(table, Table)
124
125
        self.name = name
126
        self.table = table
127
128
    def to_str(self, db):
129
        str_ = ''
130
        if self.table != None: str_ += self.table.to_str(db)+'.'
131 2348 aaronmk
        str_ += db.esc_name(self.name)
132 2211 aaronmk
        return str_
133 2314 aaronmk
134
    def to_Col(self): return self
135 2211 aaronmk
136 2393 aaronmk
def is_table_col(col): return col.table != None
137
138 2563 aaronmk
def as_Col(col, table=None, name=None):
139
    '''
140
    @param name If not None, any non-Col input will be renamed using NamedCol.
141
    '''
142
    if name != None:
143
        col = as_Value(col)
144
        if not isinstance(col, Col): col = NamedCol(name, col)
145 2333 aaronmk
146
    if isinstance(col, Code): return col
147 2260 aaronmk
    else: return Col(col, table)
148
149 2401 aaronmk
def to_name_only_col(col, check_table=None):
150
    col = as_Col(col)
151 2579 aaronmk
    if not isinstance(col, Col): return col
152 2401 aaronmk
153
    if check_table != None:
154
        table = col.table
155
        assert table == None or table == check_table
156
    return Col(col.name)
157
158 2323 aaronmk
class NamedCol(Col):
159 2229 aaronmk
    def __init__(self, name, code):
160 2310 aaronmk
        Col.__init__(self, name)
161
162 2229 aaronmk
        if not isinstance(code, Code): code = Literal(code)
163
164
        self.code = code
165
166
    def to_str(self, db):
167 2310 aaronmk
        return self.code.to_str(db)+' AS '+Col.to_str(self, db)
168 2314 aaronmk
169
    def to_Col(self): return Col(self.name)
170 2229 aaronmk
171 2462 aaronmk
def remove_col_rename(col):
172
    if isinstance(col, NamedCol): col = col.code
173
    return col
174
175 2564 aaronmk
class ColDict(dict):
176
    '''A dict that automatically makes inserted entries Col objects'''
177
178
    def __setitem__(self, key, value):
179
        return dict.__setitem__(self, key, as_Col(value, name=key))
180
181
    def update(self, dict_):
182
        for key, value in dict_.iteritems(): self[key] = value
183
184 2524 aaronmk
##### Functions
185
186
class Function(Table): pass
187
188
class FunctionCall(Code):
189
    def __init__(self, function, *args):
190
        '''
191
        @param args [Code...] The function's arguments
192
        '''
193
        if not isinstance(function, Code): function = Function(function)
194 2532 aaronmk
        args = map(remove_col_rename, args)
195 2524 aaronmk
196
        self.function = function
197
        self.args = args
198
199
    def to_str(self, db):
200
        args_str = ', '.join((v.to_str(db) for v in self.args))
201
        return self.function.to_str(db)+'('+args_str+')'
202
203 2533 aaronmk
def wrap_in_func(function, value):
204
    '''Wraps a value inside a function call.
205
    Propagates any column renaming to the returned value.
206
    '''
207
    name = None
208
    if isinstance(value, NamedCol): name = value.name
209
    value = FunctionCall(function, value)
210
    if name != None: value = NamedCol(name, value)
211
    return value
212
213 2561 aaronmk
def unwrap_func_call(func_call, check_name=None):
214
    '''Unwraps any function call to its first argument.
215
    Also removes any column renaming.
216
    '''
217
    func_call = remove_col_rename(func_call)
218
    if not isinstance(func_call, FunctionCall): return func_call
219
220
    if check_name != None:
221
        name = func_call.function.name
222
        assert name == None or name == check_name
223
    return func_call.args[0]
224
225 2335 aaronmk
##### Conditions
226 2259 aaronmk
227 2398 aaronmk
class ColValueCond(Code):
228
    def __init__(self, col, value):
229
        value = as_ValueCond(value)
230
231
        self.col = col
232
        self.value = value
233
234
    def to_str(self, db): return self.value.to_str(db, self.col)
235
236 2577 aaronmk
def combine_conds(conds, keyword=None):
237
    '''
238
    @param keyword The keyword to add before the conditions, if any
239
    '''
240
    str_ = ''
241
    if keyword != None:
242
        if conds == []: whitespace = ''
243
        elif len(conds) == 1: whitespace = ' '
244
        else: whitespace = '\n'
245
        str_ += keyword+whitespace
246
247
    str_ += '\nAND '.join(conds)
248
    return str_
249
250 2398 aaronmk
##### Condition column comparisons
251
252 2514 aaronmk
class ValueCond(BasicObject):
253 2213 aaronmk
    def __init__(self, value):
254 2225 aaronmk
        if not isinstance(value, Code): value = Literal(value)
255 2462 aaronmk
        value = remove_col_rename(value)
256 2213 aaronmk
257
        self.value = value
258 2214 aaronmk
259 2216 aaronmk
    def to_str(self, db, left_value):
260 2214 aaronmk
        '''
261 2216 aaronmk
        @param left_value The Code object that the condition is being applied on
262 2214 aaronmk
        '''
263
        raise NotImplemented()
264 2228 aaronmk
265 2514 aaronmk
    def __repr__(self): return self.to_str(mockDb, '<left_value>')
266 2211 aaronmk
267
class CompareCond(ValueCond):
268
    def __init__(self, value, operator='='):
269 2222 aaronmk
        '''
270
        @param operator By default, compares NULL values literally. Use '~=' or
271
            '~!=' to pass NULLs through.
272
        '''
273 2211 aaronmk
        ValueCond.__init__(self, value)
274
        self.operator = operator
275
276 2216 aaronmk
    def to_str(self, db, left_value):
277
        if not isinstance(left_value, Code): left_value = Col(left_value)
278 2462 aaronmk
        left_value = remove_col_rename(left_value)
279 2216 aaronmk
280 2222 aaronmk
        right_value = self.value
281
        left = left_value.to_str(db)
282
        right = right_value.to_str(db)
283
284
        # Parse operator
285 2216 aaronmk
        operator = self.operator
286 2222 aaronmk
        passthru_null_ref = [False]
287
        operator = strings.remove_prefix('~', operator, passthru_null_ref)
288
        neg_ref = [False]
289
        operator = strings.remove_prefix('!', operator, neg_ref)
290
        equals = operator.endswith('=')
291
        if equals and is_null(self.value): operator = 'IS'
292
293
        # Create str
294
        str_ = left+' '+operator+' '+right
295
        if equals and not passthru_null_ref[0] and isinstance(right_value, Col):
296 2578 aaronmk
            str_ = '('+str_+' OR ('+left+' IS NULL AND '+right+' IS NULL))'
297
        if neg_ref[0]: str_ = 'NOT '+str_
298 2222 aaronmk
        return str_
299 2216 aaronmk
300 2260 aaronmk
# Tells as_ValueCond() to assume a non-ValueCond is a literal value
301
assume_literal = object()
302
303
def as_ValueCond(value, default_table=assume_literal):
304
    if not isinstance(value, ValueCond):
305
        if default_table is not assume_literal:
306
            value = as_Col(value, default_table)
307
        return CompareCond(value)
308 2216 aaronmk
    else: return value
309 2219 aaronmk
310 2335 aaronmk
##### Joins
311
312 2352 aaronmk
join_same = object() # tells Join the left and right columns have the same name
313 2260 aaronmk
314 2353 aaronmk
# Tells Join the left and right columns have the same name and are never NULL
315
join_same_not_null = object()
316
317 2260 aaronmk
filter_out = object() # tells Join to filter out rows that match the join
318
319 2514 aaronmk
class Join(BasicObject):
320 2260 aaronmk
    def __init__(self, table, mapping, type_=None):
321
        '''
322
        @param mapping dict(right_table_col=left_table_col, ...)
323 2352 aaronmk
            * if left_table_col is join_same: left_table_col = right_table_col
324 2353 aaronmk
              * Note that right_table_col must be a string
325
            * if left_table_col is join_same_not_null:
326
              left_table_col = right_table_col and both have NOT NULL constraint
327
              * Note that right_table_col must be a string
328 2260 aaronmk
        @param type_ None (for plain join)|str (e.g. 'LEFT')|filter_out
329
            * filter_out: equivalent to 'LEFT' with the query filtered by
330
              `table_pkey IS NULL` (indicating no match)
331
        '''
332
        if util.is_str(table): table = Table(table)
333
        assert type_ == None or util.is_str(type_) or type_ is filter_out
334
335
        self.table = table
336
        self.mapping = mapping
337
        self.type_ = type_
338
339
    def to_str(self, db, left_table):
340 2353 aaronmk
        # Switch order (left_table is on the right in the comparison)
341
        right_table = left_table
342
        left_table = self.table # note left_table is reassigned
343
344 2260 aaronmk
        def join(entry):
345
            '''Parses non-USING joins'''
346
            right_table_col, left_table_col = entry
347
348 2353 aaronmk
            # Switch order (right_table_col is on the left in the comparison)
349
            left = right_table_col
350
            right = left_table_col
351
352 2260 aaronmk
            # Parse special values
353 2353 aaronmk
            if right is join_same: right = left
354
            elif right is join_same_not_null:
355
                right = CompareCond(as_Col(left, right_table), '~=')
356 2260 aaronmk
357 2353 aaronmk
            right = as_ValueCond(right, right_table)
358 2578 aaronmk
            return right.to_str(db, as_Col(left, left_table))
359 2260 aaronmk
360 2265 aaronmk
        # Create join condition
361
        type_ = self.type_
362 2276 aaronmk
        joins = self.mapping
363 2265 aaronmk
        if type_ is not filter_out and reduce(operator.and_,
364 2460 aaronmk
            (v is join_same_not_null for v in joins.itervalues())):
365 2260 aaronmk
            # all cols w/ USING, so can use simpler USING syntax
366 2298 aaronmk
            cols = (as_Col(v).to_str(db) for v in joins.iterkeys())
367
            join_cond = 'USING ('+(', '.join(cols))+')'
368 2576 aaronmk
        else:
369
            if len(joins) == 1: whitespace = ' '
370
            else: whitespace = '\n'
371 2577 aaronmk
            join_cond = combine_conds(map(join, joins.iteritems()), 'ON')
372 2260 aaronmk
373
        # Create join
374
        if type_ is filter_out: type_ = 'LEFT'
375 2266 aaronmk
        str_ = ''
376
        if type_ != None: str_ += type_+' '
377 2353 aaronmk
        str_ += 'JOIN '+left_table.to_str(db)+' '+join_cond
378 2266 aaronmk
        return str_
379 2349 aaronmk
380 2514 aaronmk
    def __repr__(self): return self.to_str(mockDb, '<left_table>')
381 2424 aaronmk
382
##### Value exprs
383
384
row_count = CustomCode('count(*)')