def left_join(right, left, onclause): """ Function: Join columns using an inner join Input: 1. Right statement/table to be joined, 2. Left statement/table to be joined, 3. A SQL expression representing the ON clause of the join. If left at None, it attempts to join the two tables based on a foreign key relationship If more than 1 expression is listed, (expressions) have to use an '&' operator. Ex: (Table1.c.colname1 = Table2.c.colname1 & Table1.c.colname2 = Table2.c.colname2) Output: A joined object Example: Table1 >> to_statement >> left_join(Table2, onclause=(Table1.c.colname = Table2.c.colname):BooleanClauseList) """ left, right = left.alias(), table_check(right) print(left, right) l1, r1 = get_onclause_col(onclause) l2, r2 = [c.name for c in l1], [c.name for c in r1] l3, r3 = sorted([c for c in left.columns if c.name in l2], key=lambda x: x.name), sorted( [c for c in right.columns if c.name in r2], key=lambda x: x.name) col_dict = dict(zip(l3, r3)) expr_list = [(k == v) for k, v in col_dict.items()] clause = reduce(lambda x, y: operator.iand(x, y), expr_list) return select_sql([left.join_sql(right, onclause=clause, isouter=True)])
def maybe_wrap(stmt): if isinstance(stmt, Alias): return select_sql([col for col in stmt.c]).select_from(stmt) else: return stmt
def _maybe_wrap(stmt): if isinstance(stmt, Alias): return select_sql(everything(stmt)).select_from(stmt) else: return stmt
def to_statement(table_class): """Returns all columns from a table as a 'Select' object""" return select_sql(everything(table_class))
def to_statement(table_class): return select_sql(everything(table_class))