Mercurial > libervia-pubsub
annotate idavoll/pgsql_backend.py @ 45:4447b3c5b857
Redefine backend classes as subclass of the implementations in backend.py.
Removed PersistenceService.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Wed, 03 Nov 2004 14:05:12 +0000 |
parents | 9685b7e291ef |
children | 40ac06941edc |
rev | line source |
---|---|
28 | 1 from twisted.application import service |
2 from twisted.internet import defer | |
3 from twisted.protocols.jabber import jid | |
4 from twisted.enterprise import adbapi | |
5 import backend | |
6 | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
7 class Storage: |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
8 def __init__(self, user, database): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
9 self.dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL', user=user, |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
10 database=database) |
28 | 11 |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
12 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
|
13 configuration = {} |
28 | 14 cursor.execute("""SELECT persistent, deliver_payload FROM nodes |
15 WHERE node=%s""", | |
16 (node_id,)) | |
17 try: | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
18 (configuration["persist_items"], |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
19 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
|
20 return configuration |
28 | 21 except TypeError: |
22 raise backend.NodeNotFound | |
23 | |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
24 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
|
25 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
|
26 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
27 def _get_affiliation(self, cursor, node_id, entity): |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
28 cursor.execute("""SELECT affiliation FROM affiliations |
28 | 29 JOIN nodes ON (node_id=nodes.id) |
30 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
|
31 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
|
32 (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
|
33 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
34 try: |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
35 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
|
36 except TypeError: |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
37 return None |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
38 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
39 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
|
40 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
|
41 entity) |
28 | 42 |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
43 def get_subscribers(self, node_id): |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 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
|
48 subscription='subscribed'""", |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
49 (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
|
50 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
|
51 return d |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
52 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
53 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
|
54 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
|
55 |
43
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
56 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
|
57 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
|
58 publisher) |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
59 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
60 def _store_items(self, cursor, 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
|
61 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
|
62 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
|
63 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
64 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
|
65 data = item.toXml() |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
66 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
|
67 FROM nodes |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
68 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
|
69 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
|
70 (publisher.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
71 data.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 return |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
76 |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
77 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
|
78 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
|
79 (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
|
80 publisher.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
81 data.encode('utf8'), |
9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
41
diff
changeset
|
82 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
|
83 |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
84 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
|
85 """ 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
|
86 |
45
4447b3c5b857
Redefine backend classes as subclass of the implementations in backend.py.
Ralph Meijer <ralphm@ik.nu>
parents:
43
diff
changeset
|
87 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
|
88 pass |
41
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
89 |
ea3d3544a52e
Rewrite using separated backend interfaces. The backend also uses a separate
Ralph Meijer <ralphm@ik.nu>
parents:
28
diff
changeset
|
90 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
|
91 pass |