def __call__(self, where=None, join=(), outer=(), rename=(), keep=None, calc=(), groupBy=()): cols = Graph(self.columns) rename = ~Graph(rename) groupBy = Set(groupBy) join = [self] + list(join) outer = list(outer) if where is None: where = EMPTY for rv in join + outer: cols += rv.attributes() if groupBy or keep is not None: cols = Graph.fromkeys( Set(keep or ()) | Set((~rename).keys()) | groupBy) * cols if rename: cols = rename * cols + (cols - cols.restrict(~rename)) rv = BasicJoin(where, join, outer, cols + calc) if groupBy: return GroupBy(rv, groupBy) return rv
def sqlSelect(self, writer): writer.assignAliasesFor(self) writer.write('SELECT ') sep = writer.separator(', ') remainingColumns = self.columns for tbl in self.relvars: tblCols = tbl.attributes() outputSubset = remainingColumns * Graph.fromkeys( Set(tblCols.values())) remainingColumns = remainingColumns - outputSubset if outputSubset == tblCols: # All names in table are kept as-is, so just use '*' sep() writer.writeAlias(tbl) writer.write(".*") continue # For all output columns that are in this table... for name, col in outputSubset.items(): sep() writer.writeExpr(col) if name <> col.name: # XXX!!! writer.write(' AS ') writer.write(name) for name, col in remainingColumns.items(): sep() writer.writeExpr(col) writer.write(' AS ') writer.write(name) writer.write(' FROM ') sep = writer.separator(', ') for tbl in self.relvars: sep() writer.writeTable(tbl) writer.prepender(' WHERE ').writeCond(self.condition) return writer.data()
def ordered_tuple(set): values = (Graph.fromkeys(set) * order.items()).values() values.sort() return tuple([v for (k,v) in values])