comparison idavoll/pgsql_backend.py @ 80:ec354aab3949

Implemented ItemRetrievalService.
author Ralph Meijer <ralphm@ik.nu>
date Tue, 09 Nov 2004 14:51:32 +0000
parents b8958967f508
children ec557449d1aa
comparison
equal deleted inserted replaced
79:c40ccc30dfb7 80:ec354aab3949
225 def get_nodes(self): 225 def get_nodes(self):
226 d = self.dbpool.runQuery("""SELECT node from nodes""") 226 d = self.dbpool.runQuery("""SELECT node from nodes""")
227 d.addCallback(lambda results: [r[0] for r in results]) 227 d.addCallback(lambda results: [r[0] for r in results])
228 return d 228 return d
229 229
230 def is_subscribed(self, node_id, subscriber):
231 return self.dbpool.runInteraction(self._is_subscribed, node_id,
232 subscriber)
233
234 def _is_subscribed(self, cursor, node_id, subscriber):
235 self._check_node_exists(cursor, node_id)
236
237 userhost = subscriber.userhost()
238 resource = subscriber.resource or ''
239
240 cursor.execute("""SELECT 1 FROM entities
241 JOIN subscriptions ON
242 (entities.id=subscriptions.entity_id)
243 JOIN nodes ON
244 (nodes.id=subscriptions.node_id)
245 WHERE entities.jid=%s AND resource=%s
246 AND node=%s""",
247 (userhost.encode('utf8'),
248 resource.encode('utf8'),
249 node_id.encode('utf8')))
250
251 return cursor.fetchone() is not None
252
253 def get_items_by_ids(self, node_id, item_ids):
254 return self.dbpool.runInteraction(self._get_items_by_ids, node_id,
255 item_ids)
256
257 def _get_items_by_ids(self, cursor, node_id, item_ids):
258 self._check_node_exists(cursor, node_id)
259 items = []
260 for item_id in item_ids:
261 cursor.execute("""SELECT data FROM nodes JOIN items ON
262 (nodes.id=items.node_id)
263 WHERE node=%s AND item=%s""",
264 (node_id.encode('utf8'),
265 item_id.encode('utf8')))
266 result = cursor.fetchone()
267 if result:
268 items.append(result[0])
269 return items
270
271 def get_items(self, node_id, max_items=None):
272 return self.dbpool.runInteraction(self._get_items, node_id, max_items)
273
274 def _get_items(self, cursor, node_id, max_items):
275 self._check_node_exists(cursor, node_id)
276 query = """SELECT data FROM nodes JOIN items ON
277 (nodes.id=items.node_id)
278 WHERE node=%s ORDER BY date DESC"""
279 try:
280 if max_items:
281 cursor.execute(query + " LIMIT %s",
282 (node_id.encode('utf8'),
283 max_items))
284 else:
285 cursor.execute(query, (node_id.encode('utf8')))
286 except Exception, e:
287 print e
288
289 result = cursor.fetchall()
290 return [r[0] for r in result]
291
230 class BackendService(backend.BackendService): 292 class BackendService(backend.BackendService):
231 """ PostgreSQL backend Service for a JEP-0060 pubsub service """ 293 """ PostgreSQL backend Service for a JEP-0060 pubsub service """
232 294
233 class NodeCreationService(backend.NodeCreationService): 295 class NodeCreationService(backend.NodeCreationService):
234 pass 296 pass
242 class SubscriptionService(backend.SubscriptionService): 304 class SubscriptionService(backend.SubscriptionService):
243 pass 305 pass
244 306
245 class AffiliationsService(backend.AffiliationsService): 307 class AffiliationsService(backend.AffiliationsService):
246 pass 308 pass
309
310 class ItemRetrievalService(backend.ItemRetrievalService):
311 pass