comparison idavoll/pgsql_storage.py @ 167:ef22e4150caa

Move protocol implementations (pubsub, disco, forms) to and depend on wokkel. Author: ralphm Fixes: #4
author Ralph Meijer <ralphm@ik.nu>
date Wed, 03 Oct 2007 12:41:43 +0000
parents 1701c0e2c707
children 958e69630e52
comparison
equal deleted inserted replaced
166:5abb017bd687 167:ef22e4150caa
1 # Copyright (c) 2003-2006 Ralph Meijer 1 # Copyright (c) 2003-2007 Ralph Meijer
2 # See LICENSE for details. 2 # See LICENSE for details.
3 3
4 import copy 4 import copy
5 import storage
6 from twisted.enterprise import adbapi 5 from twisted.enterprise import adbapi
7 from twisted.internet import defer
8 from twisted.words.protocols.jabber import jid 6 from twisted.words.protocols.jabber import jid
9 from zope.interface import implements 7 from zope.interface import implements
10 8
9 from idavoll import error, iidavoll
10
11 class Storage: 11 class Storage:
12 12
13 implements(storage.IStorage) 13 implements(iidavoll.IStorage)
14 14
15 def __init__(self, user, database, password = None): 15 def __init__(self, user, database, password = None):
16 self._dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL', 16 self._dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL',
17 user=user, 17 user=user,
18 password=password, 18 password=password,
30 (node_id,)) 30 (node_id,))
31 try: 31 try:
32 (configuration["pubsub#persist_items"], 32 (configuration["pubsub#persist_items"],
33 configuration["pubsub#deliver_payloads"]) = cursor.fetchone() 33 configuration["pubsub#deliver_payloads"]) = cursor.fetchone()
34 except TypeError: 34 except TypeError:
35 raise storage.NodeNotFound 35 raise error.NodeNotFound()
36 else: 36 else:
37 node = LeafNode(node_id, configuration) 37 node = LeafNode(node_id, configuration)
38 node._dbpool = self._dbpool 38 node._dbpool = self._dbpool
39 return node 39 return node
40 40
51 owner = owner.userhost() 51 owner = owner.userhost()
52 try: 52 try:
53 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""", 53 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""",
54 (node_id)) 54 (node_id))
55 except cursor._pool.dbapi.OperationalError: 55 except cursor._pool.dbapi.OperationalError:
56 raise storage.NodeExists 56 raise error.NodeExists()
57 57
58 cursor.execute("""SELECT 1 from entities where jid=%s""", 58 cursor.execute("""SELECT 1 from entities where jid=%s""",
59 (owner)) 59 (owner))
60 60
61 if not cursor.fetchone(): 61 if not cursor.fetchone():
62 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", 62 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
76 def _delete_node(self, cursor, node_id): 76 def _delete_node(self, cursor, node_id):
77 cursor.execute("""DELETE FROM nodes WHERE node=%s""", 77 cursor.execute("""DELETE FROM nodes WHERE node=%s""",
78 (node_id,)) 78 (node_id,))
79 79
80 if cursor.rowcount != 1: 80 if cursor.rowcount != 1:
81 raise storage.NodeNotFound 81 raise error.NodeNotFound()
82 82
83 def get_affiliations(self, entity): 83 def get_affiliations(self, entity):
84 d = self._dbpool.runQuery("""SELECT node, affiliation FROM entities 84 d = self._dbpool.runQuery("""SELECT node, affiliation FROM entities
85 JOIN affiliations ON 85 JOIN affiliations ON
86 (affiliations.entity_id=entities.id) 86 (affiliations.entity_id=entities.id)
108 subscription) 108 subscription)
109 for node, subscriber, resource, subscription in subscriptions] 109 for node, subscriber, resource, subscription in subscriptions]
110 110
111 class Node: 111 class Node:
112 112
113 implements(storage.INode) 113 implements(iidavoll.INode)
114 114
115 def __init__(self, node_id, config): 115 def __init__(self, node_id, config):
116 self.id = node_id 116 self.id = node_id
117 self._config = config 117 self._config = config
118 118
119 def _check_node_exists(self, cursor): 119 def _check_node_exists(self, cursor):
120 cursor.execute("""SELECT id FROM nodes WHERE node=%s""", 120 cursor.execute("""SELECT id FROM nodes WHERE node=%s""",
121 (self.id)) 121 (self.id))
122 if not cursor.fetchone(): 122 if not cursor.fetchone():
123 raise backend.NodeNotFound 123 raise error.NodeNotFound()
124 124
125 def get_type(self): 125 def get_type(self):
126 return self.type 126 return self.type
127 127
128 def get_configuration(self): 128 def get_configuration(self):
220 (resource, 220 (resource,
221 state, 221 state,
222 self.id, 222 self.id,
223 userhost)) 223 userhost))
224 except cursor._pool.dbapi.OperationalError: 224 except cursor._pool.dbapi.OperationalError:
225 raise storage.SubscriptionExists 225 raise error.SubscriptionExists()
226 226
227 def remove_subscription(self, subscriber): 227 def remove_subscription(self, subscriber):
228 return self._dbpool.runInteraction(self._remove_subscription, 228 return self._dbpool.runInteraction(self._remove_subscription,
229 subscriber) 229 subscriber)
230 230
240 AND resource=%s""", 240 AND resource=%s""",
241 (self.id, 241 (self.id,
242 userhost, 242 userhost,
243 resource)) 243 resource))
244 if cursor.rowcount != 1: 244 if cursor.rowcount != 1:
245 raise storage.SubscriptionNotFound 245 raise error.SubscriptionNotFound()
246 246
247 return None 247 return None
248 248
249 def get_subscribers(self): 249 def get_subscribers(self):
250 d = self._dbpool.runInteraction(self._get_subscribers) 250 d = self._dbpool.runInteraction(self._get_subscribers)
396 node_id=(SELECT id FROM nodes WHERE node=%s)""", 396 node_id=(SELECT id FROM nodes WHERE node=%s)""",
397 (self.id,)) 397 (self.id,))
398 398
399 class LeafNode(Node, LeafNodeMixin): 399 class LeafNode(Node, LeafNodeMixin):
400 400
401 implements(storage.ILeafNode) 401 implements(iidavoll.ILeafNode)