Giter Site home page Giter Site logo

Comments (4)

moskytw avatar moskytw commented on May 27, 2024

It indeed is a problem. I will try to fix it. Thanks for reporting it. :)

from mosql.

moskytw avatar moskytw commented on May 27, 2024

It is super crazy if you build subquery with the current stuff:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from mosql.util import raw, paren, identifier
from mosql.query import select

def as_(a, b):
    return '%s AS %s' % (identifier(a), identifier(b))

print select(
    select = 'name',
    table = raw(as_(
        raw(paren(
            select('person', select='name')
        )),
        'name'
    ))
)
# -> SELECT "name" FROM (SELECT "name" FROM "person") AS "name"

I will find the best way to integrate this feature into MoSQL.

from mosql.

uranusjr avatar uranusjr commented on May 27, 2024

With #12 and #14, the "person" example can now be written with a slightly different (simpler) syntax:

print select(select='name', table=as_(paren(select('person', select='name')), 'name'))

And the funky example I provided becomes

x = select('fruits', select=('type', as_(min('price'), 'minprice')), group_by=('type',))
join_x = join(as_(paren(x), 'x'), {'fruits.type': 'x.type', 'fruits.price': 'x.minprice'})
select('fruits', select=('fruits.type', 'fruits.variety', 'fruits.price'), joins=(join_x,))

resulting

SELECT "fruits"."type", "fruits"."variety", "fruits"."price" FROM "fruits"
    INNER JOIN (SELECT "type", MIN("price") AS "minprice" FROM "fruits" GROUP BY "type") AS "x" 
    ON "fruits"."price" = "x"."minprice" AND "fruits"."type" = "x"."type"

Notice that you'll need to provide the paren calls for yourself. I'll look into how we can simplify these calls. At least the as_ functions should have the ability to add parentheses automatically.

from mosql.

moskytw avatar moskytw commented on May 27, 2024

Thanks @uranusjr. The commits are merged in dev branch now. :D

from mosql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.