Support subqueries #12

Merged
merged 1 commit into from Oct 5, 2013

Conversation

Projects
None yet
2 participants
@uranusjr
Contributor

uranusjr commented Oct 5, 2013

Based on the discussion in #9.

I added an extra layer _query between str and raw to use internally. Statement-based callables and identifier now both return a _query object instead of str. This difference is used in identifier to avoid applying unintended processing to _query objects that represent subqueries.

Now you can do this:

subquery = select('person', select=('last_name',))
print select('person', where={'first_name': paren(subquery)})

And get:

SELECT * FROM "person" WHERE "first_name" = (SELECT "last_name" FROM "person")

The above test has been added to build.select.

Support subqueries
I added an extra layer "_query" between str and raw to use internally.
Statement-based callables and "identifier" now both return a _query
object
instead of str. This difference is used in "identifier" to avoid
applying
unintended processing to _query objects that represent subqueries.

Now you can do this:

>>> subquery = select('person', select=('last_name',))
>>> print select('person', where={'first_name': paren(subquery)})

And get:

SELECT * FROM "person" WHERE "first_name" = (SELECT "last_name" FROM
"person")

The above test has been added to build.select.

moskytw added a commit that referenced this pull request Oct 5, 2013

@moskytw moskytw merged commit 8f1da98 into moskytw:dev Oct 5, 2013

1 check passed

default The Travis CI build passed
Details
@moskytw

This comment has been minimized.

Show comment
Hide comment
@moskytw

moskytw Oct 13, 2013

Owner

I did some changes basing on your code:

  1. The mosql.build is deprecated, so I removed the doctests. I will re-added them in query.rst. (see 9141715)
  2. Actually, supporting subquery doesn't need so much code, so I removed some. (see 679f758)
  3. Finally, I use another approach instead of _query. It is simpler but slower I think, so I am not sure it is better or not. (see 36c1ae9)

Maybe you will want to make sure the functionality is still correct.

Owner

moskytw commented Oct 13, 2013

I did some changes basing on your code:

  1. The mosql.build is deprecated, so I removed the doctests. I will re-added them in query.rst. (see 9141715)
  2. Actually, supporting subquery doesn't need so much code, so I removed some. (see 679f758)
  3. Finally, I use another approach instead of _query. It is simpler but slower I think, so I am not sure it is better or not. (see 36c1ae9)

Maybe you will want to make sure the functionality is still correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment