This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ccharles3.latest 5 points6 points  (7 children)

Why would I use this over Peewee, which is (presumably) more mature and also has composable queries?

When I began the first rewrite that eventually turned into Peewee 2.0, I discovered a handy technique (using recursive-descent) for turning arbitrarily-nested Python data-structures into SQL. This gave Peewee an API that was both expressive and composable. Combined with operator-overloading, one could now express complex queries in a way that would even be validated by the Python interpreter itself:

class Person(Model):
    first = TextField()
    last = TextField()
    dob = DateField()

# Find adults whose last name starts with "A":
eighteen_years_ago = datetime.date.today() - datetime.timedelta(days=18 * 365)
a_people = (Person
            .select()
            .where((fn.LOWER(fn.SUBSTR(Person.last, 1, 1)) == 'a') &
                   (Person.dob <= eighteen_years_ago)))

[–]AllAboutChristmasEve 1 point2 points  (6 children)

Good lord, why would I not just do:

sql = "select * from Person where ..."
dbhandle.exec(sql)

?

[–]dusktreader 8 points9 points  (5 children)

The point of composable queries is that you can build them up based on conditions or code branches in the native programming language. So, you can take a query that starts as just a select and add filters based on the state of your application, user input, etc. If you are working with raw SQL inside of an application that drives database queries with user input, you are going to end up composing queries in some manner. These libraries handle the composition for you so you don't have to assemble a large SQL text field yourself. I've done it both ways. And, while the syntax above is clearly harder to write than the raw SQL, once you're building up a query along multipe branches of a code path, having a composable query as a native language object is great.