Revision 2127
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/sql.py | ||
---|---|---|
1 | 1 |
# Database access |
2 | 2 |
|
3 | 3 |
import copy |
4 |
import operator |
|
4 | 5 |
import re |
5 | 6 |
import warnings |
6 | 7 |
|
... | ... | |
306 | 307 |
|
307 | 308 |
order_by_pkey = object() # tells mk_select() to order by the pkey |
308 | 309 |
|
310 |
join_using = object() # tells mk_select() to join the column with USING |
|
311 |
|
|
309 | 312 |
def mk_select(db, tables, fields=None, conds=None, limit=None, start=None, |
310 | 313 |
order_by=order_by_pkey, table_is_esc=False): |
311 | 314 |
''' |
... | ... | |
357 | 360 |
left_table = table0 |
358 | 361 |
for table, joins in tables: |
359 | 362 |
if not table_is_esc: table = esc_name_(table) |
363 |
query += ' JOIN '+table |
|
360 | 364 |
|
361 | 365 |
def join(entry): |
362 |
'''Parses joins''' |
|
366 |
'''Parses non-USING joins'''
|
|
363 | 367 |
right_col, left_col = entry |
364 | 368 |
right_col = table+'.'+esc_name_(right_col) |
365 | 369 |
left_col = left_table+'.'+esc_name_(left_col) |
366 | 370 |
return (right_col+' = '+left_col |
367 | 371 |
+' OR ('+right_col+' IS NULL AND '+left_col+' IS NULL)') |
368 | 372 |
|
369 |
query += ' JOIN '+table+' ON '+( |
|
370 |
' AND '.join(map(join, joins.iteritems()))) |
|
373 |
if reduce(operator.and_, (v == join_using for v in joins.itervalues())): |
|
374 |
# all cols w/ USING |
|
375 |
query += ' USING('+(', '.join(joins.iterkeys())) |
|
376 |
else: query += ' ON '+(' AND '.join(map(join, joins.iteritems()))) |
|
377 |
|
|
371 | 378 |
left_table = table |
372 | 379 |
|
373 | 380 |
missing = True |
Also available in: Unified diff
sql.py: mk_select(): Support joins with USING, which can be used to merge multiple input cols into the same output col