Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated demo to use sqlalchemy

  • Loading branch information...
commit edb540ab65383f0599919a0c819386b9517c3126 1 parent 42d03ed
@lihaoyi authored
View
40 macropy/demo.py
@@ -1,6 +1,4 @@
-
-print (lambda x: x + x)(1 + 2)
"""
Demos
=====
@@ -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
-------
View
43 macropy/macros2/linq_test.py
@@ -9,7 +9,6 @@
-
db = generate_schema(engine)
@@ -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'
+ )
+ """
+ )
View
7 macropy/macros2/linq_test_dataset.sql
@@ -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)
);
@@ -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);
View
4 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
Please sign in to comment.
Something went wrong with that request. Please try again.