def join(self, table: Table, on: EXPRESSION_TYPE = None, using: Column = None, full_cartesian: bool = False): join = "join {table}".format(table=table.str()) # unsafe formatting assert on is None or using is None, "both 'on' and 'using' cannot be specified at the same time" if on is not None: join += " on {on}".format(on=ExpressionParser.parse(on).str()) elif using is not None: join += " using ({using})".format(using=using.name) elif not full_cartesian: raise Exception( "Trying to create a join without adding 'on' nor 'using' clauses. " "This results in a full cartesian product of both tables, " "and that is probably not what you want to achieve. " "To avoid it, set either an 'on' condition or a 'using' column. " "If you really want to perform the full cartesian product, " "set the 'full_cartesian' parameter to true.") if self._not_none(self._join): self._join += " " + join else: self._join = join return self
def limit(self, expr: EXPRESSION_TYPE): expr = ExpressionParser.parse(expr) self._limit = "limit {expr}".format( expr=expr.str()) # unsafe formatting return self
def group_by(self, *expr: EXPRESSION_TYPE): expr = ExpressionParser.parse(expr) self._group_by = "group by {expr}".format( expr=expr.str()) # unsafe formatting return self
def fields(self, *fields: EXPRESSION_TYPE): self._fields = ExpressionParser.parse(fields).str() return self
def where(self, expr: EXPRESSION_TYPE): expr = ExpressionParser.parse(expr) self._where = "where {expr}".format( expr=expr.str()) # unsafe formatting return self
def parse(expr): return ExpressionParser.parse(expr)
def order_by(self, expr: EXPRESSION_TYPE, mode: OrderMode = None): expr = ExpressionParser.parse(expr) self._order_by = "order by {expr}".format(expr=expr.str()) if mode is not None: self._order_by += " {mode}".format(mode=mode.str()) return self
def set(self, column: Column, expr: EXPRESSION_TYPE): expr = ExpressionParser.parse(expr) self._set = "set {column_name} = {expr}".format(column_name=column.name, expr=expr.str()) return self
def values(self, *values: EXPRESSION_TYPE): self._values = ExpressionParser.parse(values).str() return self