Implement "AS" syntax and some standard SQL functions #14

Merged
merged 3 commits into from Oct 5, 2013

Conversation

Projects
None yet
2 participants
@uranusjr
Contributor

uranusjr commented Oct 5, 2013

See function.py for implemented standard functions.

I'm not sure where the docs should go, so I didn't add any, sorry. :p
Might want to hint the user in the docs that they can import mosql.function as f to use f.sum or similar. Don't from mosql.function import * unless you know want you're doing because it will override the built-in min, max and sum.

uranusjr added some commits Oct 5, 2013

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.
Implement "AS" syntax
Also added two tests to build.select

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

Merge pull request #14 from uranusjr/usefuls
Implement "AS" syntax and some standard SQL functions

@moskytw moskytw merged commit c729d21 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 took a deeper look at your code, and I did two changes:

  1. Actually, "a AS x, b AS y, ..." can be treated as a pair list, so I integrated the as_ function into identifier. Now you can use AS feature via putting them in a 2-tuple:

    print select('person', select=(('name', 'n'), ))
    SELECT "name" AS "n" FROM "person"

    See 051e986 for detail.

  2. I use closure to implement the simple functions. See 48898d6 for detail.

:)

Owner

moskytw commented Oct 13, 2013

I took a deeper look at your code, and I did two changes:

  1. Actually, "a AS x, b AS y, ..." can be treated as a pair list, so I integrated the as_ function into identifier. Now you can use AS feature via putting them in a 2-tuple:

    print select('person', select=(('name', 'n'), ))
    SELECT "name" AS "n" FROM "person"

    See 051e986 for detail.

  2. I use closure to implement the simple functions. See 48898d6 for detail.

:)

@uranusjr

This comment has been minimized.

Show comment
Hide comment
@uranusjr

uranusjr Oct 13, 2013

Contributor

Love the tuple syntax in select, thx.

What I had in mind behind the class-based implementation was that the end user may wish to add some specific functions. By using a class, users can implement other functions more easily if they want to by subclassing Function. (That's also why Function was in util.py.)

But this implementation is certainly nicer in the current context. I'm fine with it, too.

Contributor

uranusjr commented Oct 13, 2013

Love the tuple syntax in select, thx.

What I had in mind behind the class-based implementation was that the end user may wish to add some specific functions. By using a class, users can implement other functions more easily if they want to by subclassing Function. (That's also why Function was in util.py.)

But this implementation is certainly nicer in the current context. I'm fine with it, too.

@moskytw

This comment has been minimized.

Show comment
Hide comment
@moskytw

moskytw Oct 13, 2013

Owner

Yes, it is enough to use closure to implement at this time. :)

Owner

moskytw commented Oct 13, 2013

Yes, it is enough to use closure to implement at this time. :)

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