Project

General

Profile

1
# SQL code generation
2

    
3
import operator
4
import re
5

    
6
import objects
7
import strings
8
import util
9

    
10
##### Escaping
11

    
12
def is_safe_name(name): return re.match(r'^[^\WA-Z]+$', name) # no uppercase
13

    
14
def esc_name(name, quote='"'):
15
    if is_safe_name(name): return name
16
    return quote + name.replace(quote, quote+quote) + quote
17
        # doubling an embedded quote escapes it in both PostgreSQL and MySQL
18

    
19
def clean_name(name): return name.replace('"', '').replace('`', '')
20

    
21
##### SQL code objects
22

    
23
class MockDb:
24
    def esc_value(self, value): return strings.repr_no_u(value)
25
    
26
    def esc_name(self, name): return esc_name(name)
27
mockDb = MockDb()
28

    
29
class BasicObject(objects.BasicObject):
30
    def __init__(self, value): self.value = value
31
    
32
    def __str__(self): return clean_name(strings.repr_no_u(self))
33

    
34
class Code(BasicObject):
35
    def to_str(self, db): raise NotImplemented()
36
    
37
    def __repr__(self): return self.to_str(mockDb)
38

    
39
class CustomCode(Code):
40
    def __init__(self, str_): self.str_ = str_
41
    
42
    def to_str(self, db): return self.str_
43

    
44
class Expr(Code):
45
    def __init__(self, expr): self.expr = expr
46
    
47
    def to_str(self, db): return '('+self.expr.to_str(db)+')'
48

    
49
##### Literal values
50

    
51
class Literal(Code):
52
    def __init__(self, value): self.value = value
53
    
54
    def to_str(self, db): return db.esc_value(self.value)
55

    
56
def as_Value(value):
57
    if isinstance(value, Code): return value
58
    else: return Literal(value)
59

    
60
def is_null(value): return isinstance(value, Literal) and value.value == None
61

    
62
##### Tables
63

    
64
class Table(Code):
65
    def __init__(self, name, schema=None):
66
        '''
67
        @param schema str|None (for no schema)
68
        '''
69
        self.name = name
70
        self.schema = schema
71
    
72
    def to_str(self, db):
73
        str_ = ''
74
        if self.schema != None: str_ += db.esc_name(self.schema)+'.'
75
        str_ += db.esc_name(self.name)
76
        return str_
77
    
78
    def to_Table(self): return self
79

    
80
def as_Table(table):
81
    if table == None or isinstance(table, Code): return table
82
    else: return Table(table)
83

    
84
class NamedTable(Table):
85
    def __init__(self, name, code, cols=None):
86
        Table.__init__(self, name)
87
        
88
        if not isinstance(code, Code): code = Table(code)
89
        
90
        self.code = code
91
        self.cols = cols
92
    
93
    def to_str(self, db):
94
        str_ = self.code.to_str(db)+'\nAS '+Table.to_str(self, db)
95
        if self.cols != None: str_ += ' ('+(', '.join(self.cols))+')'
96
        return str_
97
    
98
    def to_Table(self): return Table(self.name)
99

    
100
##### Columns
101

    
102
class Col(Code):
103
    def __init__(self, name, table=None):
104
        '''
105
        @param table Table|None (for no table)
106
        '''
107
        if util.is_str(table): table = Table(table)
108
        assert table == None or isinstance(table, Table)
109
        
110
        self.name = name
111
        self.table = table
112
    
113
    def to_str(self, db):
114
        str_ = ''
115
        if self.table != None: str_ += self.table.to_str(db)+'.'
116
        str_ += db.esc_name(self.name)
117
        return str_
118
    
119
    def to_Col(self): return self
120

    
121
def is_table_col(col): return col.table != None
122

    
123
def as_Col(col, table=None, name=None):
124
    '''
125
    @param name If not None, any non-Col input will be renamed using NamedCol.
126
    '''
127
    if name != None:
128
        col = as_Value(col)
129
        if not isinstance(col, Col): col = NamedCol(name, col)
130
    
131
    if isinstance(col, Code): return col
132
    else: return Col(col, table)
133

    
134
def to_name_only_col(col, check_table=None):
135
    col = as_Col(col)
136
    
137
    if check_table != None:
138
        table = col.table
139
        assert table == None or table == check_table
140
    return Col(col.name)
141

    
142
class NamedCol(Col):
143
    def __init__(self, name, code):
144
        Col.__init__(self, name)
145
        
146
        if not isinstance(code, Code): code = Literal(code)
147
        
148
        self.code = code
149
    
150
    def to_str(self, db):
151
        return self.code.to_str(db)+' AS '+Col.to_str(self, db)
152
    
153
    def to_Col(self): return Col(self.name)
154

    
155
def remove_col_rename(col):
156
    if isinstance(col, NamedCol): col = col.code
157
    return col
158

    
159
class ColDict(dict):
160
    '''A dict that automatically makes inserted entries Col objects'''
161
    
162
    def __setitem__(self, key, value):
163
        return dict.__setitem__(self, key, as_Col(value, name=key))
164
    
165
    def update(self, dict_):
166
        for key, value in dict_.iteritems(): self[key] = value
167

    
168
##### Functions
169

    
170
class Function(Table): pass
171

    
172
class FunctionCall(Code):
173
    def __init__(self, function, *args):
174
        '''
175
        @param args [Code...] The function's arguments
176
        '''
177
        if not isinstance(function, Code): function = Function(function)
178
        args = map(remove_col_rename, args)
179
        
180
        self.function = function
181
        self.args = args
182
    
183
    def to_str(self, db):
184
        args_str = ', '.join((v.to_str(db) for v in self.args))
185
        return self.function.to_str(db)+'('+args_str+')'
186

    
187
def wrap_in_func(function, value):
188
    '''Wraps a value inside a function call.
189
    Propagates any column renaming to the returned value.
190
    '''
191
    name = None
192
    if isinstance(value, NamedCol): name = value.name
193
    value = FunctionCall(function, value)
194
    if name != None: value = NamedCol(name, value)
195
    return value
196

    
197
def unwrap_func_call(func_call, check_name=None):
198
    '''Unwraps any function call to its first argument.
199
    Also removes any column renaming.
200
    '''
201
    func_call = remove_col_rename(func_call)
202
    if not isinstance(func_call, FunctionCall): return func_call
203
    
204
    if check_name != None:
205
        name = func_call.function.name
206
        assert name == None or name == check_name
207
    return func_call.args[0]
208

    
209
##### Conditions
210

    
211
class ColValueCond(Code):
212
    def __init__(self, col, value):
213
        value = as_ValueCond(value)
214
        
215
        self.col = col
216
        self.value = value
217
    
218
    def to_str(self, db): return self.value.to_str(db, self.col)
219

    
220
##### Condition column comparisons
221

    
222
class ValueCond(BasicObject):
223
    def __init__(self, value):
224
        if not isinstance(value, Code): value = Literal(value)
225
        value = remove_col_rename(value)
226
        
227
        self.value = value
228
    
229
    def to_str(self, db, left_value):
230
        '''
231
        @param left_value The Code object that the condition is being applied on
232
        '''
233
        raise NotImplemented()
234
    
235
    def __repr__(self): return self.to_str(mockDb, '<left_value>')
236

    
237
class CompareCond(ValueCond):
238
    def __init__(self, value, operator='='):
239
        '''
240
        @param operator By default, compares NULL values literally. Use '~=' or
241
            '~!=' to pass NULLs through.
242
        '''
243
        ValueCond.__init__(self, value)
244
        self.operator = operator
245
    
246
    def to_str(self, db, left_value):
247
        if not isinstance(left_value, Code): left_value = Col(left_value)
248
        left_value = remove_col_rename(left_value)
249
        
250
        right_value = self.value
251
        left = left_value.to_str(db)
252
        right = right_value.to_str(db)
253
        
254
        # Parse operator
255
        operator = self.operator
256
        passthru_null_ref = [False]
257
        operator = strings.remove_prefix('~', operator, passthru_null_ref)
258
        neg_ref = [False]
259
        operator = strings.remove_prefix('!', operator, neg_ref)
260
        equals = operator.endswith('=')
261
        if equals and is_null(self.value): operator = 'IS'
262
        
263
        # Create str
264
        str_ = left+' '+operator+' '+right
265
        if equals and not passthru_null_ref[0] and isinstance(right_value, Col):
266
            str_ += ' OR ('+left+' IS NULL AND '+right+' IS NULL)'
267
        if neg_ref[0]: str_ = 'NOT ('+str_+')'
268
        return str_
269

    
270
# Tells as_ValueCond() to assume a non-ValueCond is a literal value
271
assume_literal = object()
272

    
273
def as_ValueCond(value, default_table=assume_literal):
274
    if not isinstance(value, ValueCond):
275
        if default_table is not assume_literal:
276
            value = as_Col(value, default_table)
277
        return CompareCond(value)
278
    else: return value
279

    
280
##### Joins
281

    
282
join_same = object() # tells Join the left and right columns have the same name
283

    
284
# Tells Join the left and right columns have the same name and are never NULL
285
join_same_not_null = object()
286

    
287
filter_out = object() # tells Join to filter out rows that match the join
288

    
289
class Join(BasicObject):
290
    def __init__(self, table, mapping, type_=None):
291
        '''
292
        @param mapping dict(right_table_col=left_table_col, ...)
293
            * if left_table_col is join_same: left_table_col = right_table_col
294
              * Note that right_table_col must be a string
295
            * if left_table_col is join_same_not_null:
296
              left_table_col = right_table_col and both have NOT NULL constraint
297
              * Note that right_table_col must be a string
298
        @param type_ None (for plain join)|str (e.g. 'LEFT')|filter_out
299
            * filter_out: equivalent to 'LEFT' with the query filtered by
300
              `table_pkey IS NULL` (indicating no match)
301
        '''
302
        if util.is_str(table): table = Table(table)
303
        assert type_ == None or util.is_str(type_) or type_ is filter_out
304
        
305
        self.table = table
306
        self.mapping = mapping
307
        self.type_ = type_
308
    
309
    def to_str(self, db, left_table):
310
        # Switch order (left_table is on the right in the comparison)
311
        right_table = left_table
312
        left_table = self.table # note left_table is reassigned
313
        
314
        def join(entry):
315
            '''Parses non-USING joins'''
316
            right_table_col, left_table_col = entry
317
            
318
            # Switch order (right_table_col is on the left in the comparison)
319
            left = right_table_col
320
            right = left_table_col
321
            
322
            # Parse special values
323
            if right is join_same: right = left
324
            elif right is join_same_not_null:
325
                right = CompareCond(as_Col(left, right_table), '~=')
326
            
327
            right = as_ValueCond(right, right_table)
328
            return '('+right.to_str(db, as_Col(left, left_table))+')'
329
        
330
        # Create join condition
331
        type_ = self.type_
332
        joins = self.mapping
333
        if type_ is not filter_out and reduce(operator.and_,
334
            (v is join_same_not_null for v in joins.itervalues())):
335
            # all cols w/ USING, so can use simpler USING syntax
336
            cols = (as_Col(v).to_str(db) for v in joins.iterkeys())
337
            join_cond = 'USING ('+(', '.join(cols))+')'
338
        else: join_cond = 'ON\n'+('\nAND\n'.join(map(join, joins.iteritems())))
339
        
340
        # Create join
341
        if type_ is filter_out: type_ = 'LEFT'
342
        str_ = ''
343
        if type_ != None: str_ += type_+' '
344
        str_ += 'JOIN '+left_table.to_str(db)+' '+join_cond
345
        return str_
346
    
347
    def __repr__(self): return self.to_str(mockDb, '<left_table>')
348

    
349
##### Value exprs
350

    
351
row_count = CustomCode('count(*)')
(25-25/36)