Skip to content

Commit

Permalink
add support for sub-query, sdispater#309
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanpralo committed Aug 22, 2019
1 parent e5d48d6 commit 6607cb4
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion orator/query/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, connection, grammar, processor):
self._processor = processor
self._connection = connection
self._bindings = OrderedDict()
for type in ["select", "join", "where", "having", "order"]:
for type in ["select", "from", "join", "where", "having", "order"]:
self._bindings[type] = []

self.aggregate_ = None
Expand Down Expand Up @@ -188,8 +188,31 @@ def from_(self, table):
:return: The current QueryBuilder instance
:rtype: QueryBuilder
"""
self.set_bindings([], "from")
self.from__ = table
return self

def from_sub(self, query, as_):
"""
Set the query target table to a subquery
:param query: The QueryBuilder to set as from expression
:type query: QueryBuilder
:param as_: The alias name for the subquery
:type as_: str
:return: The current QueryBuilder instance
:rtype: QueryBuilder
"""
if not isinstance(query, QueryBuilder):
raise ArgumentError("From expression must be a QueryBuilder")

bindings = query.get_bindings()
query = "(%s) AS %s" % (query.to_sql(), self._grammar.wrap(as_))

self.set_bindings(bindings, "from")
self.from__ = QueryExpression(query)
return self

def join(self, table, one=None, operator=None, two=None, type="inner", where=False):
Expand Down

0 comments on commit 6607cb4

Please sign in to comment.