db = psycopg.connect("dbname=joel") curs = db.cursor() curs.execute("SELECT * FROM table") curs.fetchall()
$ createlang plpythonu *db_name*
joel=# CREATE FUNCTION circ (float) RETURNS float AS ' from math import pi return 2 * pi * args[0] ' LANGUAGE plpythonu; joel=# SELECT circ(10); circ --------------- 62.8318530718 (1 row)
rv = plpy.execute("SELECT * FROM table", 5)
plan = plpy.prepare( "SELECT * FROM users WHERE fname = $1", [ "text" ] ) rv = plpy.execute(plan, [ *"name"* ], 5)
<dtml-in people> INSERT INTO people VALUES ( <dtml-sqlvar fname type="string">, <dtml-sqlvar lname type="string"> ) <dtml-var sql_delimiter> </dtml-in>
CREATE OR REPLACE FUNCTION zstart() RETURNS TEXT AS ' if GD.has_key("zope_app"): GD["app"]._p_jar.sync() return import sys sys.path.insert(0, "/zope/lib/python") sys.path.insert(0, "/zope/lib/python/Products") import Zope sys.argv=["zope"] Zope.configure("/instance/etc/zope.conf") GD["zope_app"]=Zope.app() from ZODB.Transaction import get_transaction GD["zope_get_trans"]=get_transaction ' LANGUAGE 'plpythonu';
CREATE FUNCTION clear_cache() RETURNS TRIGGER AS ' plpy.execute("SELECT * FROM zstart()") GD["zope_app"].cache_api_call_here() plpy.execute("SELECT * FROM zcommit()") ' language 'plpythonu';
CREATE TRIGGER test_clear_cache_trig AFTER INSERT OR UPDATE OR DELETE ON test FOR EACH STATEMENT EXECUTE PROCEDURE clear_cache();
{ 'persons':'/folder/list_persons', 'animals':'/folder/list_animals' }
q = app.query_map() conn = psycopg.connect(DSN) conn.autocommit(1) curs = conn.cursor() for i in q.keys(): curs.execute("LISTEN %s" % i) while 1: select.select([curs],[],[])==([],[],[]) curs.execute("SELECT 1") notifies = curs.notifies() for n in notifies: # in real life, do something with each... print q[n[0]]
CREATE RULE person_upd AS ON UPDATE TO persons DO NOTIFY persons; CREATE RULE person_del AS ON DELETE TO persons DO NOTIFY persons; CREATE RULE person_ins AS ON INSERT TO persons DO NOTIFY persons;