Skip to content

Commit

Permalink
updated demo to use sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi committed May 7, 2013
1 parent 42d03ed commit edb540a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
40 changes: 24 additions & 16 deletions macropy/demo.py
@@ -1,6 +1,4 @@


print (lambda x: x + x)(1 + 2)
"""
Demos
=====
Expand Down Expand Up @@ -44,21 +42,31 @@ class Point(x, y): pass
LINQ
----
import sqlite3
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
for line in open("macros2/linq_test_dataset.sql").read().split(";"):
cursor.execute(line.strip())
from macropy.macros2.linq import macros, sql
string = sql%(x.name for x in bbc if x.population > 100000000)
print string
for line in cursor.execute(string).fetchall():
print line
from sqlalchemy import *
from macropy.macros2.linq import macros, sql, generate_schema
engine = create_engine("sqlite://")
for line in open("macros2/linq_test_dataset.sql").read().split(";"):
engine.execute(line.strip())
db = generate_schema(engine)
results = engine.execute(
sql%((x.name, x.area) for x in db.bbc if x.area > 10000000)
).fetchall()
results = engine.execute(
sql%(
x.name for x in db.bbc
if x.gdp / x.population > (
y.gdp / y.population for y in db.bbc
if y.name == 'United Kingdom'
)
if (x.region == 'Europe')
)
).fetchall()
for line in results: print line
peg
-------
Expand Down
43 changes: 42 additions & 1 deletion macropy/macros2/linq_test.py
Expand Up @@ -9,7 +9,6 @@




db = generate_schema(engine)


Expand Down Expand Up @@ -144,3 +143,45 @@ def test_aliased(self):
) > 100000000
)
)

def test_join(self):
compare_queries(
"""
SELECT name
FROM movie m
JOIN actor a
JOIN casting c
WHERE m.title = 'Casablanca'
AND m.id = c.movieid
AND a.id = c.actorid
""",
sql%(
a.name
for m in db.movie
for a in db.actor
for c in db.casting
if m.title == 'Casablanca'
if m.id == c.movieid
if a.id == c.actorid
)
)

(
"""
SELECT mm.title
FROM movie mm
JOIN actor aa
JOIN casting cc
WHERE mm.id = cc.movieid
AND aa.id = cc.actorid
AND mm.title IN (
SELECT m.title
FROM movie m
JOIN actor a
JOIN casting c
WHERE m.id = c.movieid
AND a.id = c.actorid
AND a.name = 'Julie Andrews'
)
"""
)
7 changes: 4 additions & 3 deletions macropy/macros2/linq_test_dataset.sql
Expand Up @@ -3,9 +3,9 @@
CREATE TABLE bbc(
name VARCHAR(50) NOT NULL
,region VARCHAR(60)
,area DECIMAL(10)
,population DECIMAL(11)
,gdp DECIMAL(14)
,area INTEGER(10)
,population INTEGER(11)
,gdp INTEGER(14)
,PRIMARY KEY (name)
);

Expand Down Expand Up @@ -379,6 +379,7 @@ insert into casting values (10003, 43, 10);
insert into casting values (10003, 44, 13);
insert into casting values (10003, 45, 14);
insert into casting values (10003, 46, 15);

insert into casting values (10003, 47, 16);
insert into casting values (10003, 48, 17);
insert into casting values (10004, 50, 1);
Expand Down
4 changes: 0 additions & 4 deletions macropy/rundemo.py
@@ -1,6 +1,2 @@
print "A"
import macropy.core.macros
print "B"
from macropy.macros2.linq import macros, sql
print "C"
import demo

0 comments on commit edb540a

Please sign in to comment.