annotate idavoll/pgsql_backend.py @ 55:7c4dfef5d964

Implement create_node() in Storage. Add NodeCreationService.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 05 Nov 2004 17:06:08 +0000
parents 40ac06941edc
children 0fa161c00ed9
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""",
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
41 (node_id.encode('utf8'), entity.encode('utf8')))
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
42
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
43 try:
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
44 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
45 except TypeError:
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
46 return None
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
47
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
48 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
49 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
50 entity)
28
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
52 def get_subscribers(self, node_id):
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
53 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
54 d = self.dbpool.runQuery("""SELECT jid, resource FROM subscriptions
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
55 JOIN nodes ON (node_id=nodes.id)
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
56 JOIN entities ON (entity_id=entities.id)
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
57 WHERE node=%s AND
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
58 subscription='subscribed'""",
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
59 (node_id.encode('utf8'),))
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
60 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
61 return d
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
62
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
63 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
64 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
65
43
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
66 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
67 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
68 publisher)
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
69
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
70 def _store_items(self, cursor, node_id, items, publisher):
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
71 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
72 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
73 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
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_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
76 data = item.toXml()
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
77 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
78 FROM nodes
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
79 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
80 nodes.node = %s and items.item=%s""",
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
81 (publisher.encode('utf8'),
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
82 data.encode('utf8'),
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
83 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
84 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
85 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
86 return
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
87
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
88 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
89 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
90 (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
91 publisher.encode('utf8'),
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
92 data.encode('utf8'),
9685b7e291ef Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 41
diff changeset
93 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
94
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
95 def add_subscription(self, node_id, subscriber, state):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
96 return self.dbpool.runInteraction(self._add_subscription, node_id,
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
97 subscriber, state)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
98
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
99 def _add_subscription(self, cursor, node_id, subscriber, state):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
100 self._check_node_exists(cursor, node_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
101 subscriber = jid.JID(subscriber)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
102 userhost = subscriber.userhost()
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
103 resource = subscriber.resource or ''
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
104
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
105 try:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
106 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
107 (userhost.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
108 except:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
109 pass
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
110
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
111 try:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
112 cursor.execute("""INSERT INTO subscriptions
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
113 (node_id, entity_id, resource, subscription)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
114 SELECT n.id, e.id, %s, %s FROM
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
115 (SELECT id FROM nodes WHERE node=%s) AS n
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
116 CROSS JOIN
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
117 (SELECT id FROM entities WHERE jid=%s) AS e""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
118 (resource.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
119 state.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
120 node_id.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
121 userhost.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
122 except:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
123 cursor.execute("""SELECT subscription FROM subscriptions
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
124 JOIN nodes ON (nodes.id=subscriptions.node_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
125 JOIN entities ON
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
126 (entities.id=subscriptions.entity_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
127 WHERE node=%s AND jid=%s AND resource=%s""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
128 (node_id.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
129 userhost.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
130 resource.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
131 state = cursor.fetchone()[0]
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
132
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
133 return {'node': node_id,
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
134 'jid': subscriber.full(),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
135 'subscription': state}
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 def remove_subscription(self, node_id, subscriber):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
138 return self.dbpool.runInteraction(self._remove_subscription, node_id,
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
139 subscriber)
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, cursor, node_id, subscriber):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
142 self._check_node_exists(cursor, node_id)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
143 subscriber = jid.JID(subscriber)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
144 userhost = subscriber.userhost()
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
145 resource = subscriber.resource or ''
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
146
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
147 cursor.execute("""DELETE FROM subscriptions WHERE
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
148 node_id=(SELECT id FROM nodes WHERE node=%s) AND
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
149 entity_id=(SELECT id FROM entities WHERE jid=%s)
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
150 AND resource=%s""",
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
151 (node_id.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
152 userhost.encode('utf8'),
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
153 resource.encode('utf8')))
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
154 if cursor.rowcount != 1:
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
155 raise backend.NotSubscribed
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
156
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
157 return None
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
158
55
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
159 def create_node(self, node_id, owner):
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
160 return self.dbpool.runInteraction(self._create_node, node_id,
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
161 owner)
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
162
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
163 def _create_node(self, cursor, node_id, owner):
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
164 try:
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
165 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""",
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
166 (node_id.encode('utf8')))
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
167 except:
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
168 raise backend.NodeExists
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
169
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
170 cursor.execute("""SELECT 1 from entities where jid=%s""",
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
171 (owner.encode('utf8')))
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 if not cursor.fetchone():
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
174 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
175 (owner.encode('utf8')))
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
176
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
177 try:
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
178 cursor.execute("""INSERT INTO affiliations
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
179 (node_id, entity_id, affiliation)
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
180 SELECT n.id, e.id, 'owner' FROM
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
181 (SELECT id FROM nodes WHERE node=%s) AS n
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
182 CROSS JOIN
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
183 (SELECT id FROM entities WHERE jid=%s) AS e""",
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
184 (node_id.encode('utf8'),
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
185 owner.encode('utf8')))
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
186 except Exception, e:
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
187 print e
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
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
191
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
192 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
193 """ 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
194
55
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
195 class NodeCreationService(backend.NodeCreationService):
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
196 pass
7c4dfef5d964 Implement create_node() in Storage.
Ralph Meijer <ralphm@ik.nu>
parents: 51
diff changeset
197
45
4447b3c5b857 Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents: 43
diff changeset
198 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
199 pass
41
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
200
ea3d3544a52e Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents: 28
diff changeset
201 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
202 pass
51
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
203
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
204 class SubscriptionService(backend.SubscriptionService):
40ac06941edc Added node existance checks.
Ralph Meijer <ralphm@ik.nu>
parents: 45
diff changeset
205 pass