1 |
2211
|
aaronmk
|
# SQL code generation
|
2 |
|
|
|
3 |
2276
|
aaronmk
|
import operator
|
4 |
|
|
|
5 |
2360
|
aaronmk
|
import objects
|
6 |
2222
|
aaronmk
|
import strings
|
7 |
2227
|
aaronmk
|
import util
|
8 |
2211
|
aaronmk
|
|
9 |
2499
|
aaronmk
|
##### Escaping
|
10 |
|
|
|
11 |
|
|
def esc_name(name, quote='"'):
|
12 |
|
|
return quote + name.replace(quote, quote+quote) + quote
|
13 |
|
|
# doubling an embedded quote escapes it in both PostgreSQL and MySQL
|
14 |
|
|
|
15 |
2513
|
aaronmk
|
def clean_name(name): return name.replace('"', '').replace('`', '')
|
16 |
|
|
|
17 |
2219
|
aaronmk
|
##### SQL code objects
|
18 |
|
|
|
19 |
2349
|
aaronmk
|
class MockDb:
|
20 |
2503
|
aaronmk
|
def esc_value(self, value): return strings.repr_no_u(value)
|
21 |
2349
|
aaronmk
|
|
22 |
2499
|
aaronmk
|
def esc_name(self, name): return esc_name(name)
|
23 |
2349
|
aaronmk
|
mockDb = MockDb()
|
24 |
|
|
|
25 |
2514
|
aaronmk
|
class BasicObject(objects.BasicObject):
|
26 |
|
|
def __init__(self, value): self.value = value
|
27 |
|
|
|
28 |
|
|
def __str__(self): return clean_name(strings.repr_no_u(self))
|
29 |
|
|
|
30 |
|
|
class Code(BasicObject):
|
31 |
2211
|
aaronmk
|
def to_str(self, db): raise NotImplemented()
|
32 |
2349
|
aaronmk
|
|
33 |
2514
|
aaronmk
|
def __repr__(self): return self.to_str(mockDb)
|
34 |
2211
|
aaronmk
|
|
35 |
2269
|
aaronmk
|
class CustomCode(Code):
|
36 |
2256
|
aaronmk
|
def __init__(self, str_): self.str_ = str_
|
37 |
|
|
|
38 |
|
|
def to_str(self, db): return self.str_
|
39 |
|
|
|
40 |
2335
|
aaronmk
|
##### Literal values
|
41 |
|
|
|
42 |
2216
|
aaronmk
|
class Literal(Code):
|
43 |
2211
|
aaronmk
|
def __init__(self, value): self.value = value
|
44 |
2213
|
aaronmk
|
|
45 |
|
|
def to_str(self, db): return db.esc_value(self.value)
|
46 |
2211
|
aaronmk
|
|
47 |
2400
|
aaronmk
|
def as_Value(value):
|
48 |
|
|
if isinstance(value, Code): return value
|
49 |
|
|
else: return Literal(value)
|
50 |
|
|
|
51 |
2216
|
aaronmk
|
def is_null(value): return isinstance(value, Literal) and value.value == None
|
52 |
|
|
|
53 |
2335
|
aaronmk
|
##### Tables
|
54 |
|
|
|
55 |
2211
|
aaronmk
|
class Table(Code):
|
56 |
|
|
def __init__(self, name, schema=None):
|
57 |
|
|
'''
|
58 |
|
|
@param schema str|None (for no schema)
|
59 |
|
|
'''
|
60 |
|
|
self.name = name
|
61 |
|
|
self.schema = schema
|
62 |
|
|
|
63 |
2348
|
aaronmk
|
def to_str(self, db):
|
64 |
|
|
str_ = ''
|
65 |
|
|
if self.schema != None: str_ += db.esc_name(self.schema)+'.'
|
66 |
|
|
str_ += db.esc_name(self.name)
|
67 |
|
|
return str_
|
68 |
2336
|
aaronmk
|
|
69 |
|
|
def to_Table(self): return self
|
70 |
2211
|
aaronmk
|
|
71 |
2219
|
aaronmk
|
def as_Table(table):
|
72 |
2270
|
aaronmk
|
if table == None or isinstance(table, Code): return table
|
73 |
2219
|
aaronmk
|
else: return Table(table)
|
74 |
|
|
|
75 |
2336
|
aaronmk
|
class NamedTable(Table):
|
76 |
|
|
def __init__(self, name, code, cols=None):
|
77 |
|
|
Table.__init__(self, name)
|
78 |
|
|
|
79 |
|
|
if not isinstance(code, Code): code = Table(code)
|
80 |
|
|
|
81 |
|
|
self.code = code
|
82 |
|
|
self.cols = cols
|
83 |
|
|
|
84 |
|
|
def to_str(self, db):
|
85 |
2467
|
aaronmk
|
str_ = self.code.to_str(db)+'\nAS '+Table.to_str(self, db)
|
86 |
2336
|
aaronmk
|
if self.cols != None: str_ += ' ('+(', '.join(self.cols))+')'
|
87 |
|
|
return str_
|
88 |
|
|
|
89 |
|
|
def to_Table(self): return Table(self.name)
|
90 |
|
|
|
91 |
2335
|
aaronmk
|
##### Columns
|
92 |
|
|
|
93 |
2211
|
aaronmk
|
class Col(Code):
|
94 |
|
|
def __init__(self, name, table=None):
|
95 |
|
|
'''
|
96 |
|
|
@param table Table|None (for no table)
|
97 |
|
|
'''
|
98 |
2241
|
aaronmk
|
if util.is_str(table): table = Table(table)
|
99 |
2211
|
aaronmk
|
assert table == None or isinstance(table, Table)
|
100 |
|
|
|
101 |
|
|
self.name = name
|
102 |
|
|
self.table = table
|
103 |
|
|
|
104 |
|
|
def to_str(self, db):
|
105 |
|
|
str_ = ''
|
106 |
|
|
if self.table != None: str_ += self.table.to_str(db)+'.'
|
107 |
2348
|
aaronmk
|
str_ += db.esc_name(self.name)
|
108 |
2211
|
aaronmk
|
return str_
|
109 |
2314
|
aaronmk
|
|
110 |
|
|
def to_Col(self): return self
|
111 |
2211
|
aaronmk
|
|
112 |
2393
|
aaronmk
|
def is_table_col(col): return col.table != None
|
113 |
|
|
|
114 |
2260
|
aaronmk
|
def as_Col(col, table=None):
|
115 |
2333
|
aaronmk
|
assert col != None
|
116 |
|
|
|
117 |
|
|
if isinstance(col, Code): return col
|
118 |
2260
|
aaronmk
|
else: return Col(col, table)
|
119 |
|
|
|
120 |
2401
|
aaronmk
|
def to_name_only_col(col, check_table=None):
|
121 |
|
|
col = as_Col(col)
|
122 |
|
|
|
123 |
|
|
if check_table != None:
|
124 |
|
|
table = col.table
|
125 |
|
|
assert table == None or table == check_table
|
126 |
|
|
return Col(col.name)
|
127 |
|
|
|
128 |
2323
|
aaronmk
|
class NamedCol(Col):
|
129 |
2229
|
aaronmk
|
def __init__(self, name, code):
|
130 |
2310
|
aaronmk
|
Col.__init__(self, name)
|
131 |
|
|
|
132 |
2229
|
aaronmk
|
if not isinstance(code, Code): code = Literal(code)
|
133 |
|
|
|
134 |
|
|
self.code = code
|
135 |
|
|
|
136 |
|
|
def to_str(self, db):
|
137 |
2310
|
aaronmk
|
return self.code.to_str(db)+' AS '+Col.to_str(self, db)
|
138 |
2314
|
aaronmk
|
|
139 |
|
|
def to_Col(self): return Col(self.name)
|
140 |
2229
|
aaronmk
|
|
141 |
2462
|
aaronmk
|
def remove_col_rename(col):
|
142 |
|
|
if isinstance(col, NamedCol): col = col.code
|
143 |
|
|
return col
|
144 |
|
|
|
145 |
2335
|
aaronmk
|
##### Conditions
|
146 |
2259
|
aaronmk
|
|
147 |
2398
|
aaronmk
|
class ColValueCond(Code):
|
148 |
|
|
def __init__(self, col, value):
|
149 |
|
|
value = as_ValueCond(value)
|
150 |
|
|
|
151 |
|
|
self.col = col
|
152 |
|
|
self.value = value
|
153 |
|
|
|
154 |
|
|
def to_str(self, db): return self.value.to_str(db, self.col)
|
155 |
|
|
|
156 |
|
|
##### Condition column comparisons
|
157 |
|
|
|
158 |
2514
|
aaronmk
|
class ValueCond(BasicObject):
|
159 |
2213
|
aaronmk
|
def __init__(self, value):
|
160 |
2225
|
aaronmk
|
if not isinstance(value, Code): value = Literal(value)
|
161 |
2462
|
aaronmk
|
value = remove_col_rename(value)
|
162 |
2213
|
aaronmk
|
|
163 |
|
|
self.value = value
|
164 |
2214
|
aaronmk
|
|
165 |
2216
|
aaronmk
|
def to_str(self, db, left_value):
|
166 |
2214
|
aaronmk
|
'''
|
167 |
2216
|
aaronmk
|
@param left_value The Code object that the condition is being applied on
|
168 |
2214
|
aaronmk
|
'''
|
169 |
|
|
raise NotImplemented()
|
170 |
2228
|
aaronmk
|
|
171 |
2514
|
aaronmk
|
def __repr__(self): return self.to_str(mockDb, '<left_value>')
|
172 |
2211
|
aaronmk
|
|
173 |
|
|
class CompareCond(ValueCond):
|
174 |
|
|
def __init__(self, value, operator='='):
|
175 |
2222
|
aaronmk
|
'''
|
176 |
|
|
@param operator By default, compares NULL values literally. Use '~=' or
|
177 |
|
|
'~!=' to pass NULLs through.
|
178 |
|
|
'''
|
179 |
2211
|
aaronmk
|
ValueCond.__init__(self, value)
|
180 |
|
|
self.operator = operator
|
181 |
|
|
|
182 |
2216
|
aaronmk
|
def to_str(self, db, left_value):
|
183 |
|
|
if not isinstance(left_value, Code): left_value = Col(left_value)
|
184 |
2462
|
aaronmk
|
left_value = remove_col_rename(left_value)
|
185 |
2216
|
aaronmk
|
|
186 |
2222
|
aaronmk
|
right_value = self.value
|
187 |
|
|
left = left_value.to_str(db)
|
188 |
|
|
right = right_value.to_str(db)
|
189 |
|
|
|
190 |
|
|
# Parse operator
|
191 |
2216
|
aaronmk
|
operator = self.operator
|
192 |
2222
|
aaronmk
|
passthru_null_ref = [False]
|
193 |
|
|
operator = strings.remove_prefix('~', operator, passthru_null_ref)
|
194 |
|
|
neg_ref = [False]
|
195 |
|
|
operator = strings.remove_prefix('!', operator, neg_ref)
|
196 |
|
|
equals = operator.endswith('=')
|
197 |
|
|
if equals and is_null(self.value): operator = 'IS'
|
198 |
|
|
|
199 |
|
|
# Create str
|
200 |
|
|
str_ = left+' '+operator+' '+right
|
201 |
|
|
if equals and not passthru_null_ref[0] and isinstance(right_value, Col):
|
202 |
|
|
str_ += ' OR ('+left+' IS NULL AND '+right+' IS NULL)'
|
203 |
|
|
if neg_ref[0]: str_ = 'NOT ('+str_+')'
|
204 |
|
|
return str_
|
205 |
2216
|
aaronmk
|
|
206 |
2260
|
aaronmk
|
# Tells as_ValueCond() to assume a non-ValueCond is a literal value
|
207 |
|
|
assume_literal = object()
|
208 |
|
|
|
209 |
|
|
def as_ValueCond(value, default_table=assume_literal):
|
210 |
|
|
if not isinstance(value, ValueCond):
|
211 |
|
|
if default_table is not assume_literal:
|
212 |
|
|
value = as_Col(value, default_table)
|
213 |
|
|
return CompareCond(value)
|
214 |
2216
|
aaronmk
|
else: return value
|
215 |
2219
|
aaronmk
|
|
216 |
2335
|
aaronmk
|
##### Joins
|
217 |
|
|
|
218 |
2352
|
aaronmk
|
join_same = object() # tells Join the left and right columns have the same name
|
219 |
2260
|
aaronmk
|
|
220 |
2353
|
aaronmk
|
# Tells Join the left and right columns have the same name and are never NULL
|
221 |
|
|
join_same_not_null = object()
|
222 |
|
|
|
223 |
2260
|
aaronmk
|
filter_out = object() # tells Join to filter out rows that match the join
|
224 |
|
|
|
225 |
2514
|
aaronmk
|
class Join(BasicObject):
|
226 |
2260
|
aaronmk
|
def __init__(self, table, mapping, type_=None):
|
227 |
|
|
'''
|
228 |
|
|
@param mapping dict(right_table_col=left_table_col, ...)
|
229 |
2352
|
aaronmk
|
* if left_table_col is join_same: left_table_col = right_table_col
|
230 |
2353
|
aaronmk
|
* Note that right_table_col must be a string
|
231 |
|
|
* if left_table_col is join_same_not_null:
|
232 |
|
|
left_table_col = right_table_col and both have NOT NULL constraint
|
233 |
|
|
* Note that right_table_col must be a string
|
234 |
2260
|
aaronmk
|
@param type_ None (for plain join)|str (e.g. 'LEFT')|filter_out
|
235 |
|
|
* filter_out: equivalent to 'LEFT' with the query filtered by
|
236 |
|
|
`table_pkey IS NULL` (indicating no match)
|
237 |
|
|
'''
|
238 |
|
|
if util.is_str(table): table = Table(table)
|
239 |
|
|
assert type_ == None or util.is_str(type_) or type_ is filter_out
|
240 |
|
|
|
241 |
|
|
self.table = table
|
242 |
|
|
self.mapping = mapping
|
243 |
|
|
self.type_ = type_
|
244 |
|
|
|
245 |
|
|
def to_str(self, db, left_table):
|
246 |
2353
|
aaronmk
|
# Switch order (left_table is on the right in the comparison)
|
247 |
|
|
right_table = left_table
|
248 |
|
|
left_table = self.table # note left_table is reassigned
|
249 |
|
|
|
250 |
2260
|
aaronmk
|
def join(entry):
|
251 |
|
|
'''Parses non-USING joins'''
|
252 |
|
|
right_table_col, left_table_col = entry
|
253 |
|
|
|
254 |
2353
|
aaronmk
|
# Switch order (right_table_col is on the left in the comparison)
|
255 |
|
|
left = right_table_col
|
256 |
|
|
right = left_table_col
|
257 |
|
|
|
258 |
2260
|
aaronmk
|
# Parse special values
|
259 |
2353
|
aaronmk
|
if right is join_same: right = left
|
260 |
|
|
elif right is join_same_not_null:
|
261 |
|
|
right = CompareCond(as_Col(left, right_table), '~=')
|
262 |
2260
|
aaronmk
|
|
263 |
2353
|
aaronmk
|
right = as_ValueCond(right, right_table)
|
264 |
2410
|
aaronmk
|
return '('+right.to_str(db, as_Col(left, left_table))+')'
|
265 |
2260
|
aaronmk
|
|
266 |
2265
|
aaronmk
|
# Create join condition
|
267 |
|
|
type_ = self.type_
|
268 |
2276
|
aaronmk
|
joins = self.mapping
|
269 |
2265
|
aaronmk
|
if type_ is not filter_out and reduce(operator.and_,
|
270 |
2460
|
aaronmk
|
(v is join_same_not_null for v in joins.itervalues())):
|
271 |
2260
|
aaronmk
|
# all cols w/ USING, so can use simpler USING syntax
|
272 |
2298
|
aaronmk
|
cols = (as_Col(v).to_str(db) for v in joins.iterkeys())
|
273 |
|
|
join_cond = 'USING ('+(', '.join(cols))+')'
|
274 |
2467
|
aaronmk
|
else: join_cond = 'ON\n'+('\nAND\n'.join(map(join, joins.iteritems())))
|
275 |
2260
|
aaronmk
|
|
276 |
|
|
# Create join
|
277 |
|
|
if type_ is filter_out: type_ = 'LEFT'
|
278 |
2266
|
aaronmk
|
str_ = ''
|
279 |
|
|
if type_ != None: str_ += type_+' '
|
280 |
2353
|
aaronmk
|
str_ += 'JOIN '+left_table.to_str(db)+' '+join_cond
|
281 |
2266
|
aaronmk
|
return str_
|
282 |
2349
|
aaronmk
|
|
283 |
2514
|
aaronmk
|
def __repr__(self): return self.to_str(mockDb, '<left_table>')
|
284 |
2424
|
aaronmk
|
|
285 |
|
|
##### Value exprs
|
286 |
|
|
|
287 |
|
|
row_count = CustomCode('count(*)')
|