Skip to content

Commit

Permalink
Create new API for selecting from a CTE.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Apr 24, 2018
1 parent baed4c3 commit e4ca73a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
5 changes: 3 additions & 2 deletions docs/peewee/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1039,9 +1039,10 @@ Query-builder
:param bool recursive: Whether the CTE is recursive.
:param list columns: Explicit list of columns produced by CTE (optional).

.. py:method:: select(*columns)
.. py:method:: select_from(*columns)
Create a SELECT query that utilizes the given common table expression.
Create a SELECT query that utilizes the given common table expression
as the source for a new query.

:param columns: One or more columns to select from the CTE.
:return: :py:class:`Select` query utilizing the common table expression
Expand Down
2 changes: 1 addition & 1 deletion peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def __init__(self, name, query, recursive=False, columns=None):
self._columns = columns
super(CTE, self).__init__(alias=name)

def select(self, *columns):
def select_from(self, *columns):
query = (Select((self,), columns)
.with_cte(self)
.bind(self._query._database))
Expand Down
29 changes: 19 additions & 10 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,16 +1680,23 @@ def test_simple_cte(self):
.select(Category.name, Category.parent)
.cte('catz', columns=('name', 'parent')))

query = (cte
cte_sql = ('WITH "catz" ("name", "parent") AS ('
'SELECT "t1"."name", "t1"."parent_id" '
'FROM "category" AS "t1") '
'SELECT "catz"."name", "catz"."parent" AS "pname" '
'FROM "catz" '
'ORDER BY "catz"."name"')

query = (Category
.select(cte.c.name, cte.c.parent.alias('pname'))
.order_by(cte.c.name))
self.assertSQL(query, (
'WITH "catz" ("name", "parent") AS ('
'SELECT "t1"."name", "t1"."parent_id" '
'FROM "category" AS "t1") '
'SELECT "catz"."name", "catz"."parent" AS "pname" '
'FROM "catz" '
'ORDER BY "catz"."name"'), [])
.from_(cte)
.order_by(cte.c.name)
.with_cte(cte))
self.assertSQL(query, cte_sql, [])

query2 = (cte.select_from(cte.c.name, cte.c.parent.alias('pname'))
.order_by(cte.c.name))
self.assertSQL(query2, cte_sql, [])

self.assertEqual([(row.name, row.pname) for row in query], [
('c11', 'p1'),
Expand All @@ -1699,6 +1706,8 @@ def test_simple_cte(self):
('p2', 'root'),
('p3', 'root'),
('root', None)])
self.assertEqual([(row.name, row.pname) for row in query],
[(row.name, row.pname) for row in query2])

@skip_if(IS_SQLITE_OLD or (IS_MYSQL and not IS_MYSQL_ADVANCED_FEATURES))
def test_cte_join(self):
Expand Down Expand Up @@ -1751,7 +1760,7 @@ def get_parents(cname):

cte = base + recursive
query = (cte
.select(cte.c.name, cte.c.level, cte.c.path)
.select_from(cte.c.name, cte.c.level, cte.c.path)
.order_by(cte.c.level))
self.assertSQL(query, (
'WITH RECURSIVE "parents" AS ('
Expand Down

0 comments on commit e4ca73a

Please sign in to comment.