File: demo2b.py

package info (click to toggle)
python-pgsql 2.4.0-5sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 984 kB
  • ctags: 1,291
  • sloc: ansic: 5,913; python: 4,334; sh: 73; makefile: 71
file content (69 lines) | stat: -rwxr-xr-x 1,801 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/local/bin/python
#ident "$Id: demo2b.py,v 1.3 2001/10/18 03:17:08 ballie01 Exp $"
#-----------------------------------------------------------------------+
# demo2b.py - Print out the results of a select (similar to pgsql).	|
#-----------------------------------------------------------------------+
import time
from pyPgSQL import PgSQL

c = PgSQL.connect()

c.autocommit = 1 # Auto commit must be on to drop the tables!

c1 = c.cursor()

# Make sure that the tables and rules don't exist.
try:
    c1.execute("DROP RULE demo2r1")
except StandardError:
    pass

try:
    c1.execute("DROP TABLE demo2a")
except StandardError:
    pass

try:
    c1.execute("DROP TABLE demo2b")
except StandardError:
    pass

# Create the tables and rule.
c1.execute("""
CREATE TABLE demo2a (
	updated timestamp default now(),
	name varchar(30)
        )""")
c1.execute("""
CREATE TABLE demo2b (
	updated timestamp default now(),
	name varchar(30)
        )""")
c1.execute("""
CREATE RULE demo2r1 AS ON INSERT TO demo2b DO
	(INSERT INTO demo2a (name) values (new.name); NOTIFY demo2a)""")

c1.close()
del c1

c.autocommit = 0

c1 = c.cursor()

# Update the table (which kicks off the rule which sends the notify
# Note: The notification does not occur until a commit() happens.  Multiple
#	updates to demo2b with a single commit() will only send one notice.

c1.execute("INSERT INTO demo2b (name) VALUES ('This is a test(1)')")
c1.execute("INSERT INTO demo2b (name) VALUES ('This is another test(2)')")
c.commit()	# Note: 2 updates, 1 notice.
time.sleep(5)
c1.execute("INSERT INTO demo2b (name) VALUES ('And another test(3)')")
c.commit()
time.sleep(10)
c1.execute("INSERT INTO demo2b (name) VALUES ('The final insert(4)')")
c.commit()

print "\nThere should of been 3 notices sent and recieved.\n"

del c1, c