400 |
400 |
distinct_on_all = object() # tells mk_select() to SELECT DISTINCT ON all columns
|
401 |
401 |
|
402 |
402 |
def mk_select(db, tables, fields=None, conds=None, distinct_on=None, limit=None,
|
403 |
|
start=None, order_by=order_by_pkey, table_is_esc=False):
|
|
403 |
start=None, order_by=order_by_pkey, table_is_esc=False, default_table=None):
|
404 |
404 |
'''
|
405 |
405 |
@param tables The single table to select from, or a list of tables to join
|
406 |
406 |
together: [table0, (table1, joins), ...]
|
... | ... | |
433 |
433 |
|
434 |
434 |
params = []
|
435 |
435 |
|
436 |
|
def parse_col(field, default_table=None):
|
|
436 |
def parse_col(field):
|
437 |
437 |
'''Parses fields'''
|
438 |
|
return sql_gen.value2sql_gen(field, default_table,
|
439 |
|
table_is_esc).to_str(db)
|
|
438 |
return sql_gen.value2sql_gen(field, default_table, table_is_esc,
|
|
439 |
assume_col=True).to_str(db)
|
440 |
440 |
def cond(entry):
|
441 |
441 |
'''Parses conditions'''
|
442 |
442 |
left, right = entry
|
... | ... | |
478 |
478 |
left_join_ref[0] = True
|
479 |
479 |
conds[(table, right_col)] = None # filter query by no match
|
480 |
480 |
|
481 |
|
return cond((right_col, left_col))
|
|
481 |
return cond(((table, right_col),
|
|
482 |
sql_gen.value2sql_gen(left_col, left_table,
|
|
483 |
table_is_esc=table_is_esc, assume_col=True)))
|
482 |
484 |
|
483 |
485 |
# Create join condition and determine join type
|
484 |
486 |
if reduce(operator.and_, (v == join_using for v in joins.itervalues())):
|
... | ... | |
497 |
499 |
query += ' WHERE '+(' AND '.join(map(cond, conds.iteritems())))
|
498 |
500 |
params += conds.values()
|
499 |
501 |
missing = False
|
500 |
|
if order_by != None: query += ' ORDER BY '+parse_col(order_by, table0)
|
|
502 |
if order_by != None:
|
|
503 |
query += ' ORDER BY '+sql_gen.col2sql_gen(order_by, table0,
|
|
504 |
table_is_esc).to_str(db)
|
501 |
505 |
if limit != None: query += ' LIMIT '+str(limit); missing = False
|
502 |
506 |
if start != None:
|
503 |
507 |
if start != 0: query += ' OFFSET '+str(start)
|
... | ... | |
772 |
776 |
distinct_on = None
|
773 |
777 |
def mk_main_select(cols):
|
774 |
778 |
return mk_select(db, insert_joins, cols, conds, distinct_on,
|
775 |
|
order_by=None, limit=limit, start=start, table_is_esc=table_is_esc)
|
|
779 |
order_by=None, limit=limit, start=start, table_is_esc=table_is_esc,
|
|
780 |
default_table=in_tables0)
|
776 |
781 |
|
777 |
782 |
# Do inserts and selects
|
778 |
783 |
out_pkeys_ref = ['out_pkeys_'+temp_suffix]
|
sql.py: mk_select() (and sql_gen.py): Fixed bugs where literal strings were treated as literal values when they should have been treated as column names. Take default_table param to determine default table to use if a column doesn't have an explicit table. put_table(): mk_main_select(): Pass in_tables0 as mk_select()'s default_table.