/
test6.py
163 lines (127 loc) · 4.36 KB
/
test6.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import simplejson, json, sys
from decimal import Decimal
import psycopg2
import psycopg2.extras
import psycopg2.extensions
from psycopg2.extensions import adapt
from pprint import pprint
password = sys.argv[1]
conn = psycopg2.connect(host = "127.0.0.1", port = 5432, database = "test", user = "test", password = password)
conn.autocommit = True
cur = conn.cursor()
#psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
cur.execute("""DROP TYPE IF EXISTS t_station CASCADE""")
cur.execute("""DROP TYPE IF EXISTS t_employee CASCADE""")
cur.execute("""DROP TYPE IF EXISTS t_address CASCADE""")
cur.execute("""CREATE TYPE t_station AS (x INT, y INT, label VARCHAR)""")
cur.execute("""CREATE TYPE t_address AS (city VARCHAR, street VARCHAR, no INT, stations t_station[])""")
cur.execute("""CREATE TYPE t_employee AS (name VARCHAR, age INT, coins INT[], notes VARCHAR, address t_address)""")
cur.execute("""
CREATE OR REPLACE FUNCTION test_employee(e t_employee, roundtrip BOOLEAN DEFAULT TRUE)
RETURNS t_employee AS
$$
DECLARE
l_adr t_address;
l_stations t_station[];
l_station t_station;
BEGIN
IF roundtrip THEN
RETURN e;
END IF;
e.notes := 'something';
l_adr := e.address;
l_adr.street := 'sdfsd';
l_adr.no := 23;
l_adr.stations := l_stations;
e.address := l_adr;
RETURN e;
END
$$ LANGUAGE plpgsql;
""")
cur.execute("""
CREATE OR REPLACE FUNCTION test_json(e json)
RETURNS json AS
$$
BEGIN
RETURN e;
END
$$ LANGUAGE plpgsql;
""")
from pgutil import register_composite_as_dict
dcasters = {}
casters = {}
acasters = {}
stationCaster = register_composite_as_dict('t_station', conn)
dcasters['t_station'] = stationCaster
stationCaster.casters = casters
stationCaster.acasters = acasters
casters[stationCaster.oid] = stationCaster
acasters[stationCaster.array_oid] = stationCaster
empCaster = register_composite_as_dict('t_employee', conn)
dcasters['t_employee'] = empCaster
empCaster.casters = casters
empCaster.acasters = acasters
casters[empCaster.oid] = empCaster
acasters[empCaster.array_oid] = empCaster
adrCaster = register_composite_as_dict('t_address', conn)
dcasters['t_address'] = adrCaster
adrCaster.casters = casters
adrCaster.acasters = acasters
casters[adrCaster.oid] = adrCaster
acasters[adrCaster.array_oid] = adrCaster
class DictComposite(psycopg2.extras.CompositeCaster):
def make(self, attrs):
return dict(zip(self.attnames, attrs))
class DictAdapter(object):
def __init__(self, adapted):
self.adapted = adapted
def prepare(self, conn):
self._conn = conn
def getquoted(self):
if self.adapted.has_key('record') and dcasters.has_key(self.adapted['record']):
c = dcasters[self.adapted['record']]
v = []
for n in c.attnames:
v.append(self.adapted.get(n, None))
a = psycopg2.extensions.adapt(tuple(v))
a.prepare(self._conn)
return a.getquoted() + '::' + self.adapted['record']
else:
return adapt(None).getquoted()
psycopg2.extensions.register_adapter(dict, DictAdapter)
v1 = ('foo', 44, [1, 2, 3], None, ('Duckhausen', None, 18, [(10, None, 'blub'), (None, 5, None)]))
v1 = ('foo', 44, [1, 2, 3], None, ('Duckhausen', None, 18, [{'record': 't_station', 'x': 10}]))
print cur.mogrify("SELECT test_employee(%s, %s)", [v1, True])
cur.execute("SELECT test_employee(%s, %s)", [v1, True])
print cur.fetchone()
sys.exit(0)
e0 = {'name': 'foo', 'age': 44, 'ignored': None, 'coins': [1,2,3]}
e1 = {'name': 'foo',
'age': 44,
'address': {'no': 18,
'city': 'Duckhausen'},
'ignored': None,
'coins': [1,2,3]}
e2 = {'name': 'foo',
'age': 44,
'address': {'no': 18,
'city': 'Duckhausen',
'stations': [{'x': 10, 'label': 'blub'},
{'y': 5, 'ignored': False}]},
'ignored': None,
'coins': [1,2,3]}
for e in [e0, e1, e2]:
print "-"*40
try:
mangled = empCaster.totuple(e)
print mangled
#cur.execute("SELECT test_employee(%s, %s)", [empCaster.totuple(e), False])
#print cur.fetchone()
except Exception, e:
print e
#print "*"*40
#try:
# cur.execute("SELECT test_json(%s)", [e])
# print cur.fetchone()
#except Exception, e:
# print e