annotate idavoll/pgsql_backend.py @ 73:5d7a924ebddb

Reworked disco support. Actually return disco items.
author Ralph Meijer <ralphm@ik.nu>
date Sun, 07 Nov 2004 14:21:09 +0000
parents a63bb34931ec
children b8958967f508
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.application import service
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2 from twisted.internet import defer
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.protocols.jabber import jid
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 from twisted.enterprise import adbapi
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 import backend
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
12 def _check_node_exists(self, cursor, node_id):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
13 cursor.execute("""SELECT id FROM nodes WHERE node=%s""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
14 (node_id.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
15 if not cursor.fetchone():
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
16 raise backend.NodeNotFound
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
17 else:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
18 return
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
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
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 cursor.execute("""SELECT persistent, deliver_payload FROM nodes
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 WHERE node=%s""",
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24 (node_id,))
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 except TypeError:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 raise backend.NodeNotFound
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
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
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 JOIN nodes ON (node_id=nodes.id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
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()
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
82 cursor.execute("""UPDATE items SET publisher=%s, data=%s
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
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
100 def add_subscription(self, node_id, subscriber, state):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
101 return self.dbpool.runInteraction(self._add_subscription, node_id,
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
102 subscriber, state)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
103
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
104 def _add_subscription(self, cursor, node_id, subscriber, state):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
105 self._check_node_exists(cursor, node_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
106 userhost = subscriber.userhost()
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
107 resource = subscriber.resource or ''
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
108
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
109 try:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
110 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
111 (userhost.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
112 except:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
113 pass
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
114
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
115 try:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
116 cursor.execute("""INSERT INTO subscriptions
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
117 (node_id, entity_id, resource, subscription)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
118 SELECT n.id, e.id, %s, %s FROM
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
119 (SELECT id FROM nodes WHERE node=%s) AS n
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
120 CROSS JOIN
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
121 (SELECT id FROM entities WHERE jid=%s) AS e""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
122 (resource.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
123 state.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
124 node_id.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
125 userhost.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
126 except:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
127 cursor.execute("""SELECT subscription FROM subscriptions
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
128 JOIN nodes ON (nodes.id=subscriptions.node_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
129 JOIN entities ON
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
130 (entities.id=subscriptions.entity_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
131 WHERE node=%s AND jid=%s AND resource=%s""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
132 (node_id.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
133 userhost.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
134 resource.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
135 state = cursor.fetchone()[0]
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
136
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
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
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
139 'subscription': state}
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
140
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
141 def remove_subscription(self, node_id, subscriber):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
142 return self.dbpool.runInteraction(self._remove_subscription, node_id,
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
143 subscriber)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
144
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
145 def _remove_subscription(self, cursor, node_id, subscriber):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
146 self._check_node_exists(cursor, node_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
147 userhost = subscriber.userhost()
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
148 resource = subscriber.resource or ''
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
149
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
150 cursor.execute("""DELETE FROM subscriptions WHERE
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
151 node_id=(SELECT id FROM nodes WHERE node=%s) AND
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
152 entity_id=(SELECT id FROM entities WHERE jid=%s)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
153 AND resource=%s""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
154 (node_id.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
155 userhost.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
156 resource.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
157 if cursor.rowcount != 1:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
158 raise backend.NotSubscribed
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
159
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
160 return None
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
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
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
218 def get_node_type(self, node_id):
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
219 return self.dbpool.runInteraction(self._get_node_type, node_id)
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
220
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
221 def _get_node_type(self, cursor, node_id):
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
222 self._check_node_exists(cursor, node_id)
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
223 return 'leaf'
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
224
71
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
225 def get_nodes(self):
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
226 d = self.dbpool.runQuery("""SELECT node from nodes""")
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
227 d.addCallback(lambda results: [r[0] for r in results])
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
228 return d
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
229
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
230 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
231 """ 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
232
55
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
233 class NodeCreationService(backend.NodeCreationService):
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
234 pass
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
235
45
4447b3c5b857 Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
236 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
237 pass
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
238
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
239 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
240 pass
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
241
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
242 class SubscriptionService(backend.SubscriptionService):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
243 pass
62
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
244
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
245 class AffiliationsService(backend.AffiliationsService):
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
246 pass