Mercurial > libervia-pubsub
annotate idavoll/pgsql_backend.py @ 80:ec354aab3949
Implemented ItemRetrievalService.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 09 Nov 2004 14:51:32 +0000 |
parents | b8958967f508 |
children | ec557449d1aa |
rev | line source |
---|---|
28 | 1 from twisted.application import service |
2 from twisted.internet import defer | |
3 from twisted.protocols.jabber import jid | |
4 from twisted.enterprise import adbapi | |
5 import backend | |
6 | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
7 class Storage: |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
8 def __init__(self, user, database): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
9 self.dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL', user=user, |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
10 database=database) |
28 | 11 |
51 | 12 def _check_node_exists(self, cursor, node_id): |
13 cursor.execute("""SELECT id FROM nodes WHERE node=%s""", | |
14 (node_id.encode('utf8'))) | |
15 if not cursor.fetchone(): | |
16 raise backend.NodeNotFound | |
17 else: | |
18 return | |
19 | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
20 def _get_node_configuration(self, cursor, node_id): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
21 configuration = {} |
28 | 22 cursor.execute("""SELECT persistent, deliver_payload FROM nodes |
23 WHERE node=%s""", | |
24 (node_id,)) | |
25 try: | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
26 (configuration["persist_items"], |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
27 configuration["deliver_payloads"]) = cursor.fetchone() |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
28 return configuration |
28 | 29 except TypeError: |
30 raise backend.NodeNotFound | |
31 | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
32 def get_node_configuration(self, node_id): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
33 return self.dbpool.runInteraction(self._get_node_configuration, node_id) |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
34 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
35 def _get_affiliation(self, cursor, node_id, entity): |
51 | 36 self._check_node_exists(cursor, node_id) |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
37 cursor.execute("""SELECT affiliation FROM affiliations |
28 | 38 JOIN nodes ON (node_id=nodes.id) |
39 JOIN entities ON (entity_id=entities.id) | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
40 WHERE node=%s AND jid=%s""", |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
41 (node_id.encode('utf8'), |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
42 entity.full().encode('utf8'))) |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
43 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
44 try: |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
45 return cursor.fetchone()[0] |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
46 except TypeError: |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
47 return None |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
48 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
49 def get_affiliation(self, node_id, entity): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
50 return self.dbpool.runInteraction(self._get_affiliation, node_id, |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
51 entity) |
28 | 52 |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
53 def get_subscribers(self, node_id): |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
54 d = self.dbpool.runInteraction(self._get_subscribers, node_id) |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
55 d.addCallback(self._convert_to_jids) |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
56 return d |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
57 |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
58 def _get_subscribers(self, cursor,node_id): |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
59 self._check_node_exists(cursor, node_id) |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
60 cursor.execute("""SELECT jid, resource FROM subscriptions |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
61 JOIN nodes ON (node_id=nodes.id) |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
62 JOIN entities ON (entity_id=entities.id) |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
63 WHERE node=%s AND |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
64 subscription='subscribed'""", |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
65 (node_id.encode('utf8'),)) |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
66 return cursor.fetchall() |
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
67 |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
68 def _convert_to_jids(self, list): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
69 return [jid.JID("%s/%s" % (l[0], l[1])).full() for l in list] |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
70 |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
71 def store_items(self, node_id, items, publisher): |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
72 return self.dbpool.runInteraction(self._store_items, node_id, items, |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
73 publisher) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
74 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
75 def _store_items(self, cursor, node_id, items, publisher): |
51 | 76 self._check_node_exists(cursor, node_id) |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
77 for item in items: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
78 self._store_item(cursor, node_id, item, publisher) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
79 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
80 def _store_item(self, cursor, node_id, item, publisher): |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
81 data = item.toXml() |
77
b8958967f508
Introduced date field in items table.
Ralph Meijer <ralphm@ik.nu>
parents:
71
diff
changeset
|
82 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
83 FROM nodes |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
84 WHERE nodes.id = items.node_id AND |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
85 nodes.node = %s and items.item=%s""", |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
86 (publisher.full().encode('utf8'), |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
87 data.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
88 node_id.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
89 item["id"].encode('utf8'))) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
90 if cursor.rowcount == 1: |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
91 return |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
92 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
93 cursor.execute("""INSERT INTO items (node_id, item, publisher, data) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
94 SELECT id, %s, %s, %s FROM nodes WHERE node=%s""", |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
95 (item["id"].encode('utf8'), |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
96 publisher.full().encode('utf8'), |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
97 data.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
98 node_id.encode('utf8'))) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
99 |
51 | 100 def add_subscription(self, node_id, subscriber, state): |
101 return self.dbpool.runInteraction(self._add_subscription, node_id, | |
102 subscriber, state) | |
103 | |
104 def _add_subscription(self, cursor, node_id, subscriber, state): | |
105 self._check_node_exists(cursor, node_id) | |
106 userhost = subscriber.userhost() | |
107 resource = subscriber.resource or '' | |
108 | |
109 try: | |
110 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", | |
111 (userhost.encode('utf8'))) | |
112 except: | |
113 pass | |
114 | |
115 try: | |
116 cursor.execute("""INSERT INTO subscriptions | |
117 (node_id, entity_id, resource, subscription) | |
118 SELECT n.id, e.id, %s, %s FROM | |
119 (SELECT id FROM nodes WHERE node=%s) AS n | |
120 CROSS JOIN | |
121 (SELECT id FROM entities WHERE jid=%s) AS e""", | |
122 (resource.encode('utf8'), | |
123 state.encode('utf8'), | |
124 node_id.encode('utf8'), | |
125 userhost.encode('utf8'))) | |
126 except: | |
127 cursor.execute("""SELECT subscription FROM subscriptions | |
128 JOIN nodes ON (nodes.id=subscriptions.node_id) | |
129 JOIN entities ON | |
130 (entities.id=subscriptions.entity_id) | |
131 WHERE node=%s AND jid=%s AND resource=%s""", | |
132 (node_id.encode('utf8'), | |
133 userhost.encode('utf8'), | |
134 resource.encode('utf8'))) | |
135 state = cursor.fetchone()[0] | |
136 | |
137 return {'node': node_id, | |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
138 'jid': subscriber, |
51 | 139 'subscription': state} |
140 | |
141 def remove_subscription(self, node_id, subscriber): | |
142 return self.dbpool.runInteraction(self._remove_subscription, node_id, | |
143 subscriber) | |
144 | |
145 def _remove_subscription(self, cursor, node_id, subscriber): | |
146 self._check_node_exists(cursor, node_id) | |
147 userhost = subscriber.userhost() | |
148 resource = subscriber.resource or '' | |
149 | |
150 cursor.execute("""DELETE FROM subscriptions WHERE | |
151 node_id=(SELECT id FROM nodes WHERE node=%s) AND | |
152 entity_id=(SELECT id FROM entities WHERE jid=%s) | |
153 AND resource=%s""", | |
154 (node_id.encode('utf8'), | |
155 userhost.encode('utf8'), | |
156 resource.encode('utf8'))) | |
157 if cursor.rowcount != 1: | |
158 raise backend.NotSubscribed | |
159 | |
160 return None | |
161 | |
55
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
162 def create_node(self, node_id, owner): |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
163 return self.dbpool.runInteraction(self._create_node, node_id, |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
164 owner) |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
165 |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
166 def _create_node(self, cursor, node_id, owner): |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
167 try: |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
168 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""", |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
169 (node_id.encode('utf8'))) |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
170 except: |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
171 raise backend.NodeExists |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
172 |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
173 cursor.execute("""SELECT 1 from entities where jid=%s""", |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
174 (owner.full().encode('utf8'))) |
55
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
175 |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
176 if not cursor.fetchone(): |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
177 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
178 (owner.full().encode('utf8'))) |
55
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
179 |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
180 try: |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
181 cursor.execute("""INSERT INTO affiliations |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
182 (node_id, entity_id, affiliation) |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
183 SELECT n.id, e.id, 'owner' FROM |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
184 (SELECT id FROM nodes WHERE node=%s) AS n |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
185 CROSS JOIN |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
186 (SELECT id FROM entities WHERE jid=%s) AS e""", |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
187 (node_id.encode('utf8'), |
59
0fa161c00ed9
Use jid.JIDs everywhere in the backend.
Ralph Meijer <ralphm@ik.nu>
parents:
55
diff
changeset
|
188 owner.full().encode('utf8'))) |
55
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
189 except Exception, e: |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
190 print e |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
191 |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
192 return None |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
193 |
62
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
194 def get_affiliations(self, entity): |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
195 return self.dbpool.runQuery("""SELECT node, affiliation FROM entities |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
196 JOIN affiliations ON |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
197 (affiliations.entity_id=entities.id) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
198 JOIN nodes ON |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
199 (nodes.id=affiliations.node_id) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
200 WHERE jid=%s""", |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
201 (entity.full().encode('utf8'),)) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
202 |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
203 def get_subscriptions(self, entity): |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
204 d = self.dbpool.runQuery("""SELECT node, jid, resource, subscription |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
205 FROM entities JOIN subscriptions ON |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
206 (subscriptions.entity_id=entities.id) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
207 JOIN nodes ON |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
208 (nodes.id=subscriptions.node_id) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
209 WHERE jid=%s""", |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
210 (entity.full().encode('utf8'),)) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
211 d.addCallback(self._convert_subscription_jids) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
212 return d |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
213 |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
214 def _convert_subscription_jids(self, subscriptions): |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
215 return [(node, jid.JID('%s/%s' % (subscriber, resource)), subscription) |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
216 for node, subscriber, resource, subscription in subscriptions] |
55
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
217 |
66 | 218 def get_node_type(self, node_id): |
219 return self.dbpool.runInteraction(self._get_node_type, node_id) | |
220 | |
221 def _get_node_type(self, cursor, node_id): | |
222 self._check_node_exists(cursor, node_id) | |
223 return 'leaf' | |
224 | |
71 | 225 def get_nodes(self): |
226 d = self.dbpool.runQuery("""SELECT node from nodes""") | |
227 d.addCallback(lambda results: [r[0] for r in results]) | |
228 return d | |
229 | |
80
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
230 def is_subscribed(self, node_id, subscriber): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
231 return self.dbpool.runInteraction(self._is_subscribed, node_id, |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
232 subscriber) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
233 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
234 def _is_subscribed(self, cursor, node_id, subscriber): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
235 self._check_node_exists(cursor, node_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
236 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
237 userhost = subscriber.userhost() |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
238 resource = subscriber.resource or '' |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
239 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
240 cursor.execute("""SELECT 1 FROM entities |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
241 JOIN subscriptions ON |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
242 (entities.id=subscriptions.entity_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
243 JOIN nodes ON |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
244 (nodes.id=subscriptions.node_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
245 WHERE entities.jid=%s AND resource=%s |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
246 AND node=%s""", |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
247 (userhost.encode('utf8'), |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
248 resource.encode('utf8'), |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
249 node_id.encode('utf8'))) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
250 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
251 return cursor.fetchone() is not None |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
252 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
253 def get_items_by_ids(self, node_id, item_ids): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
254 return self.dbpool.runInteraction(self._get_items_by_ids, node_id, |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
255 item_ids) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
256 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
257 def _get_items_by_ids(self, cursor, node_id, item_ids): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
258 self._check_node_exists(cursor, node_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
259 items = [] |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
260 for item_id in item_ids: |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
261 cursor.execute("""SELECT data FROM nodes JOIN items ON |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
262 (nodes.id=items.node_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
263 WHERE node=%s AND item=%s""", |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
264 (node_id.encode('utf8'), |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
265 item_id.encode('utf8'))) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
266 result = cursor.fetchone() |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
267 if result: |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
268 items.append(result[0]) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
269 return items |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
270 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
271 def get_items(self, node_id, max_items=None): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
272 return self.dbpool.runInteraction(self._get_items, node_id, max_items) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
273 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
274 def _get_items(self, cursor, node_id, max_items): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
275 self._check_node_exists(cursor, node_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
276 query = """SELECT data FROM nodes JOIN items ON |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
277 (nodes.id=items.node_id) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
278 WHERE node=%s ORDER BY date DESC""" |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
279 try: |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
280 if max_items: |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
281 cursor.execute(query + " LIMIT %s", |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
282 (node_id.encode('utf8'), |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
283 max_items)) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
284 else: |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
285 cursor.execute(query, (node_id.encode('utf8'))) |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
286 except Exception, e: |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
287 print e |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
288 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
289 result = cursor.fetchall() |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
290 return [r[0] for r in result] |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
291 |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
292 class BackendService(backend.BackendService): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
293 """ PostgreSQL backend Service for a JEP-0060 pubsub service """ |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
294 |
55
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
295 class NodeCreationService(backend.NodeCreationService): |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
296 pass |
7c4dfef5d964
Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
51
diff
changeset
|
297 |
45
4447b3c5b857
Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
298 class PublishService(backend.PublishService): |
4447b3c5b857
Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
299 pass |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
300 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
301 class NotificationService(backend.NotificationService): |
45
4447b3c5b857
Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
302 pass |
51 | 303 |
304 class SubscriptionService(backend.SubscriptionService): | |
305 pass | |
62
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
306 |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
307 class AffiliationsService(backend.AffiliationsService): |
8cdbc27c467a
Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents:
59
diff
changeset
|
308 pass |
80
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
309 |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
310 class ItemRetrievalService(backend.ItemRetrievalService): |
ec354aab3949
Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents:
77
diff
changeset
|
311 pass |