annotate idavoll/pgsql_backend.py @ 117:bc872c33e88c

Added test cases for a few of the Node methods. Clean up more in the pgsql database.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 08 Apr 2005 21:00:22 +0000
parents 8d8946e67fcb
children
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
105
8d8946e67fcb Move to Twisted 2.0.
Ralph Meijer <ralphm@ik.nu>
parents: 98
diff changeset
3 from twisted.words.protocols.jabber import jid
28
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:
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
26 (configuration["pubsub#persist_items"],
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
27 configuration["pubsub#deliver_payloads"]) = cursor.fetchone()
41
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()
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
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
98
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
180 cursor.execute("""INSERT INTO affiliations
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
181 (node_id, entity_id, affiliation)
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
182 SELECT n.id, e.id, 'owner' FROM
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
183 (SELECT id FROM nodes WHERE node=%s) AS n
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
184 CROSS JOIN
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
185 (SELECT id FROM entities WHERE jid=%s) AS e""",
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
186 (node_id.encode('utf8'),
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
187 owner.full().encode('utf8')))
55
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
188
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
189 return None
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
190
62
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
191 def get_affiliations(self, entity):
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
192 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
193 JOIN affiliations ON
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
194 (affiliations.entity_id=entities.id)
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
195 JOIN nodes ON
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
196 (nodes.id=affiliations.node_id)
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
197 WHERE jid=%s""",
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
198 (entity.full().encode('utf8'),))
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
199
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
200 def get_subscriptions(self, entity):
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
201 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
202 FROM entities JOIN subscriptions ON
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
203 (subscriptions.entity_id=entities.id)
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
204 JOIN nodes ON
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
205 (nodes.id=subscriptions.node_id)
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
206 WHERE jid=%s""",
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
207 (entity.full().encode('utf8'),))
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
208 d.addCallback(self._convert_subscription_jids)
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
209 return d
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
210
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
211 def _convert_subscription_jids(self, subscriptions):
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
212 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
213 for node, subscriber, resource, subscription in subscriptions]
55
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
214
66
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
215 def get_node_type(self, node_id):
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
216 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
217
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
218 def _get_node_type(self, cursor, node_id):
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
219 self._check_node_exists(cursor, node_id)
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
220 return 'leaf'
13ced084aa69 Add get_node_type() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 62
diff changeset
221
71
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
222 def get_nodes(self):
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
223 d = self.dbpool.runQuery("""SELECT node from nodes""")
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
224 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
225 return d
a63bb34931ec Added get_nodes() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 66
diff changeset
226
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
227 def is_subscribed(self, node_id, subscriber):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
228 return self.dbpool.runInteraction(self._is_subscribed, node_id,
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
229 subscriber)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
230
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
231 def _is_subscribed(self, cursor, node_id, subscriber):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
232 self._check_node_exists(cursor, node_id)
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 userhost = subscriber.userhost()
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
235 resource = subscriber.resource or ''
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 cursor.execute("""SELECT 1 FROM entities
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
238 JOIN subscriptions ON
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
239 (entities.id=subscriptions.entity_id)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
240 JOIN nodes ON
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
241 (nodes.id=subscriptions.node_id)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
242 WHERE entities.jid=%s AND resource=%s
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
243 AND node=%s""",
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
244 (userhost.encode('utf8'),
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
245 resource.encode('utf8'),
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
246 node_id.encode('utf8')))
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
247
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
248 return cursor.fetchone() is not None
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
249
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
250 def get_items_by_ids(self, node_id, item_ids):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
251 return self.dbpool.runInteraction(self._get_items_by_ids, node_id,
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
252 item_ids)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
253
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
254 def _get_items_by_ids(self, cursor, node_id, item_ids):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
255 self._check_node_exists(cursor, node_id)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
256 items = []
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
257 for item_id in item_ids:
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
258 cursor.execute("""SELECT data FROM nodes JOIN items ON
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
259 (nodes.id=items.node_id)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
260 WHERE node=%s AND item=%s""",
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
261 (node_id.encode('utf8'),
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
262 item_id.encode('utf8')))
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
263 result = cursor.fetchone()
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
264 if result:
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
265 items.append(result[0])
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
266 return items
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
267
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
268 def get_items(self, node_id, max_items=None):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
269 return self.dbpool.runInteraction(self._get_items, node_id, max_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, cursor, node_id, max_items):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
272 self._check_node_exists(cursor, node_id)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
273 query = """SELECT data FROM nodes JOIN items ON
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
274 (nodes.id=items.node_id)
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
275 WHERE node=%s ORDER BY date DESC"""
98
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
276 if max_items:
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
277 cursor.execute(query + " LIMIT %s",
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
278 (node_id.encode('utf8'),
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
279 max_items))
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
280 else:
b9c449f4c167 Removed debugging junk
Ralph Meijer <ralphm@ik.nu>
parents: 95
diff changeset
281 cursor.execute(query, (node_id.encode('utf8')))
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
282
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
283 result = cursor.fetchall()
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
284 return [r[0] for r in result]
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
285
85
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
286 def remove_items(self, node_id, item_ids):
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
287 return self.dbpool.runInteraction(self._remove_items, node_id, item_ids)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
288
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
289 def _remove_items(self, cursor, node_id, item_ids):
90
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
290 self._check_node_exists(cursor, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
291
85
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
292 deleted = []
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
293
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
294 for item_id in item_ids:
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
295 cursor.execute("""DELETE FROM items WHERE
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
296 node_id=(SELECT id FROM nodes WHERE node=%s) AND
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
297 item=%s""",
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
298 (node_id.encode('utf-8'),
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
299 item_id.encode('utf-8')))
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
300
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
301 if cursor.rowcount:
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
302 deleted.append(item_id)
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
303
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
304 return deleted
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
305
90
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
306 def purge_node(self, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
307 return self.dbpool.runInteraction(self._purge_node, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
308
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
309 def _purge_node(self, cursor, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
310 self._check_node_exists(cursor, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
311
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
312 cursor.execute("""DELETE FROM items WHERE
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
313 node_id=(SELECT id FROM nodes WHERE node=%s)""",
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
314 (node_id.encode('utf-8'),))
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
315
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
316 def delete_node(self, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
317 return self.dbpool.runInteraction(self._delete_node, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
318
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
319 def _delete_node(self, cursor, node_id):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
320 self._check_node_exists(cursor, node_id)
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
321
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
322 cursor.execute("""DELETE FROM nodes WHERE node=%s""",
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
323 (node_id.encode('utf-8'),))
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
324
95
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
325 def set_node_configuration(self, node_id, options):
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
326 return self.dbpool.runInteraction(self._set_node_configuration,
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
327 node_id,
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
328 options)
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
329
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
330 def _set_node_configuration(self, cursor, node_id, options):
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
331 cursor.execute("""UPDATE nodes SET persistent=%s, deliver_payload=%s
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
332 WHERE node=%s""",
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
333 (options["pubsub#persist_items"].encode('utf8'),
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
334 options["pubsub#deliver_payloads"].encode('utf8'),
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
335 node_id.encode('utf-8')))
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
336 if cursor.rowcount != 1:
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
337 raise backend.Error
3ad74552bbc7 Merge from RELENG_0: Implemented node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 90
diff changeset
338
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
339 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
340 """ 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
341
55
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
342 class NodeCreationService(backend.NodeCreationService):
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
343 pass
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
344
45
4447b3c5b857 Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
345 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
346 pass
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
347
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
348 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
349 pass
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
350
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
351 class SubscriptionService(backend.SubscriptionService):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
352 pass
62
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
353
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
354 class AffiliationsService(backend.AffiliationsService):
8cdbc27c467a Add get_affiliations() and get_subscriptions() to Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 59
diff changeset
355 pass
80
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
356
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
357 class ItemRetrievalService(backend.ItemRetrievalService):
ec354aab3949 Implemented ItemRetrievalService.
Ralph Meijer <ralphm@ik.nu>
parents: 77
diff changeset
358 pass
85
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
359
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
360 class RetractionService(backend.RetractionService):
ec557449d1aa Implement node retraction, with storage support for pgsql.
Ralph Meijer <ralphm@ik.nu>
parents: 80
diff changeset
361 pass
90
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
362
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
363 class NodeDeletionService(backend.NodeDeletionService):
59378610b16e Implement node purging and node deletion.
Ralph Meijer <ralphm@ik.nu>
parents: 85
diff changeset
364 pass