piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.32k stars 86 forks source link

Add more operators to `QueryString` #1013

Closed dantownsend closed 2 weeks ago

dantownsend commented 2 weeks ago

In recent releases we have been adding more SQL functions to Piccolo.

These functions return QueryString - the building block of queries in Piccolo.

By overriding magic methods in QueryString we can make a more intuitive API. For example:

await Ticket.select(Round(Ticket.price) * 2)

In the example above, Round returns a QueryString, and by overriding the __mul__ magic method on QueryString, we can make it so Round(Ticket.price) * 2 returns a new QueryString which compiles down to round(price) * 2 in SQL.