ORM based on the list comprehension (really on generators)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> products = Table('product')
>>> components = Table('component')
Simple queries:
>>> LQ(p.name for p in products).sql()
'SELECT product.name FROM product;'
Queries on multiple columns:
>>> LQ((p.name, p.url) for p in products).sql()
'SELECT product.name, product.url FROM product;'
Queries with conditions:
>>> LQ(p.url for p in products if (p.price < 10) & (p.weight > 20)).sql()
'SELECT product.url FROM product WHERE ((product.price < 10) AND (product.weight > 20));'
Inner joins:
>>> LQ((p.name, c.path) \
for p, c in Join(products, components) \
if (p.guid == c.id) & (c.status == 'completed')).sql()
'SELECT product.name, component.path FROM product, component WHERE ((product.guid = component.id) AND (component.status = "completed"));'
GROUP BY and aggregate functions:
>>> LQ((group_by(c.status), count(c.id)) for c in components).sql()
'SELECT component.status, COUNT(component.id) FROM component GROUP BY component.status;'