comparison idavoll/pgsql_storage.py @ 113:dfef919aaf1b

Fix interpretations of JIDs. Catch more specific exceptions only.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 08 Apr 2005 16:33:20 +0000
parents d252d793f0ed
children 4f0113adb7ed
comparison
equal deleted inserted replaced
112:72c8812fa1b5 113:dfef919aaf1b
38 38
39 def create_node(self, node_id, owner, type='leaf'): 39 def create_node(self, node_id, owner, type='leaf'):
40 return self._dbpool.runInteraction(self._create_node, node_id, owner) 40 return self._dbpool.runInteraction(self._create_node, node_id, owner)
41 41
42 def _create_node(self, cursor, node_id, owner): 42 def _create_node(self, cursor, node_id, owner):
43 node_id = node_id.encode('utf-8')
44 owner = owner.userhost().encode('utf-8')
43 try: 45 try:
44 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""", 46 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""",
45 (node_id.encode('utf8'))) 47 (node_id))
46 except: 48 except cursor._pool.dbapi.OperationalError:
47 raise storage.NodeExists 49 raise storage.NodeExists
48 50
49 cursor.execute("""SELECT 1 from entities where jid=%s""", 51 cursor.execute("""SELECT 1 from entities where jid=%s""",
50 (owner.full().encode('utf8'))) 52 (owner))
51 53
52 if not cursor.fetchone(): 54 if not cursor.fetchone():
53 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", 55 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
54 (owner.full().encode('utf8'))) 56 (owner))
55 57
56 cursor.execute("""INSERT INTO affiliations 58 cursor.execute("""INSERT INTO affiliations
57 (node_id, entity_id, affiliation) 59 (node_id, entity_id, affiliation)
58 SELECT n.id, e.id, 'owner' FROM 60 SELECT n.id, e.id, 'owner' FROM
59 (SELECT id FROM nodes WHERE node=%s) AS n 61 (SELECT id FROM nodes WHERE node=%s) AS n
60 CROSS JOIN 62 CROSS JOIN
61 (SELECT id FROM entities WHERE jid=%s) AS e""", 63 (SELECT id FROM entities WHERE jid=%s) AS e""",
62 (node_id.encode('utf8'), 64 (node_id, owner))
63 owner.full().encode('utf8')))
64 65
65 def delete_node(self, node_id): 66 def delete_node(self, node_id):
66 return self._dbpool.runInteraction(self._delete_node, node_id) 67 return self._dbpool.runInteraction(self._delete_node, node_id)
67 68
68 def _delete_node(self, cursor, node_id): 69 def _delete_node(self, cursor, node_id):
77 JOIN affiliations ON 78 JOIN affiliations ON
78 (affiliations.entity_id=entities.id) 79 (affiliations.entity_id=entities.id)
79 JOIN nodes ON 80 JOIN nodes ON
80 (nodes.id=affiliations.node_id) 81 (nodes.id=affiliations.node_id)
81 WHERE jid=%s""", 82 WHERE jid=%s""",
82 (entity.full().encode('utf8'),)) 83 (entity.userhost().encode('utf8'),))
83 d.addCallback(lambda results: [tuple(r) for r in results]) 84 d.addCallback(lambda results: [tuple(r) for r in results])
84 return d 85 return d
85 86
86 def get_subscriptions(self, entity): 87 def get_subscriptions(self, entity):
87 d = self._dbpool.runQuery("""SELECT node, jid, resource, subscription 88 d = self._dbpool.runQuery("""SELECT node, jid, resource, subscription
139 cursor.execute("""SELECT affiliation FROM affiliations 140 cursor.execute("""SELECT affiliation FROM affiliations
140 JOIN nodes ON (node_id=nodes.id) 141 JOIN nodes ON (node_id=nodes.id)
141 JOIN entities ON (entity_id=entities.id) 142 JOIN entities ON (entity_id=entities.id)
142 WHERE node=%s AND jid=%s""", 143 WHERE node=%s AND jid=%s""",
143 (self.id.encode('utf8'), 144 (self.id.encode('utf8'),
144 entity.full().encode('utf8'))) 145 entity.userhost().encode('utf8')))
145 146
146 try: 147 try:
147 return cursor.fetchone()[0] 148 return cursor.fetchone()[0]
148 except TypeError: 149 except TypeError:
149 return None 150 return None
157 resource = subscriber.resource or '' 158 resource = subscriber.resource or ''
158 159
159 try: 160 try:
160 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", 161 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
161 (userhost.encode('utf8'))) 162 (userhost.encode('utf8')))
162 except: 163 except cursor._pool.dbapi.OperationalError:
163 pass 164 pass
164 165
165 try: 166 try:
166 cursor.execute("""INSERT INTO subscriptions 167 cursor.execute("""INSERT INTO subscriptions
167 (node_id, entity_id, resource, subscription) 168 (node_id, entity_id, resource, subscription)
171 (SELECT id FROM entities WHERE jid=%s) AS e""", 172 (SELECT id FROM entities WHERE jid=%s) AS e""",
172 (resource.encode('utf8'), 173 (resource.encode('utf8'),
173 state.encode('utf8'), 174 state.encode('utf8'),
174 self.id.encode('utf8'), 175 self.id.encode('utf8'),
175 userhost.encode('utf8'))) 176 userhost.encode('utf8')))
176 except: 177 except cursor._pool.dbapi.OperationalError:
177 cursor.execute("""SELECT subscription FROM subscriptions 178 cursor.execute("""SELECT subscription FROM subscriptions
178 JOIN nodes ON (nodes.id=subscriptions.node_id) 179 JOIN nodes ON (nodes.id=subscriptions.node_id)
179 JOIN entities ON 180 JOIN entities ON
180 (entities.id=subscriptions.entity_id) 181 (entities.id=subscriptions.entity_id)
181 WHERE node=%s AND jid=%s AND resource=%s""", 182 WHERE node=%s AND jid=%s AND resource=%s""",