comparison idavoll/pgsql_storage.py @ 127:d3689da18ed2

Don't use encode('utf-8') on serialized XML. Return unicode strings for get_items() Rename get_items_by_ids() to get_items_by_id() as in the interface. Make purge() work. Don't return item ids when removing items.
author Ralph Meijer <ralphm@ik.nu>
date Sun, 24 Apr 2005 17:21:43 +0000
parents c4ee16bc48e5
children 327de183f48d
comparison
equal deleted inserted replaced
126:ffc3c706b0fb 127:d3689da18ed2
296 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s 296 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s
297 FROM nodes 297 FROM nodes
298 WHERE nodes.id = items.node_id AND 298 WHERE nodes.id = items.node_id AND
299 nodes.node = %s and items.item=%s""", 299 nodes.node = %s and items.item=%s""",
300 (publisher.full().encode('utf8'), 300 (publisher.full().encode('utf8'),
301 data.encode('utf8'), 301 data,
302 self.id.encode('utf8'), 302 self.id.encode('utf8'),
303 item["id"].encode('utf8'))) 303 item["id"].encode('utf8')))
304 if cursor.rowcount == 1: 304 if cursor.rowcount == 1:
305 return 305 return
306 306
307 cursor.execute("""INSERT INTO items (node_id, item, publisher, data) 307 cursor.execute("""INSERT INTO items (node_id, item, publisher, data)
308 SELECT id, %s, %s, %s FROM nodes WHERE node=%s""", 308 SELECT id, %s, %s, %s FROM nodes WHERE node=%s""",
309 (item["id"].encode('utf8'), 309 (item["id"].encode('utf8'),
310 publisher.full().encode('utf8'), 310 publisher.full().encode('utf8'),
311 data.encode('utf8'), 311 data,
312 self.id.encode('utf8'))) 312 self.id.encode('utf8')))
313 313
314 def remove_items(self, item_ids): 314 def remove_items(self, item_ids):
315 return self._dbpool.runInteraction(self._remove_items, item_ids) 315 return self._dbpool.runInteraction(self._remove_items, item_ids)
316 316
324 node_id=(SELECT id FROM nodes WHERE node=%s) AND 324 node_id=(SELECT id FROM nodes WHERE node=%s) AND
325 item=%s""", 325 item=%s""",
326 (self.id.encode('utf-8'), 326 (self.id.encode('utf-8'),
327 item_id.encode('utf-8'))) 327 item_id.encode('utf-8')))
328 328
329 if cursor.rowcount: 329 if not cursor.rowcount:
330 deleted.append(item_id) 330 raise storage.ItemNotFound
331
332 return deleted
333 331
334 def get_items(self, max_items=None): 332 def get_items(self, max_items=None):
335 return self._dbpool.runInteraction(self._get_items, max_items) 333 return self._dbpool.runInteraction(self._get_items, max_items)
336 334
337 def _get_items(self, cursor, max_items): 335 def _get_items(self, cursor, max_items):
345 max_items)) 343 max_items))
346 else: 344 else:
347 cursor.execute(query, (self.id.encode('utf8'))) 345 cursor.execute(query, (self.id.encode('utf8')))
348 346
349 result = cursor.fetchall() 347 result = cursor.fetchall()
350 return [r[0] for r in result] 348 return [unicode(r[0], 'utf8') for r in result]
351 349
352 def get_items_by_ids(self, item_ids): 350 def get_items_by_id(self, item_ids):
353 return self._dbpool.runInteraction(self._get_items_by_ids, item_ids) 351 return self._dbpool.runInteraction(self._get_items_by_id, item_ids)
354 352
355 def _get_items_by_ids(self, cursor, item_ids): 353 def _get_items_by_id(self, cursor, item_ids):
356 self._check_node_exists(cursor) 354 self._check_node_exists(cursor)
357 items = [] 355 items = []
358 for item_id in item_ids: 356 for item_id in item_ids:
359 cursor.execute("""SELECT data FROM nodes JOIN items ON 357 cursor.execute("""SELECT data FROM nodes JOIN items ON
360 (nodes.id=items.node_id) 358 (nodes.id=items.node_id)
361 WHERE node=%s AND item=%s""", 359 WHERE node=%s AND item=%s""",
362 (self.id.encode('utf8'), 360 (self.id.encode('utf8'),
363 item_id.encode('utf8'))) 361 item_id.encode('utf8')))
364 result = cursor.fetchone() 362 result = cursor.fetchone()
365 if result: 363 if result:
366 items.append(result[0]) 364 items.append(unicode(result[0], 'utf8'))
367 return items 365 return items
368 366
369 def purge(self): 367 def purge(self):
370 return self._dbpool.runInteraction(self._purge) 368 return self._dbpool.runInteraction(self._purge)
371 369
372 def _purge_node(self, cursor): 370 def _purge(self, cursor):
373 self._check_node_exists(cursor) 371 self._check_node_exists(cursor)
374 372
375 cursor.execute("""DELETE FROM items WHERE 373 cursor.execute("""DELETE FROM items WHERE
376 node_id=(SELECT id FROM nodes WHERE node=%s)""", 374 node_id=(SELECT id FROM nodes WHERE node=%s)""",
377 (self.id.encode('utf-8'),)) 375 (self.id.encode('utf-8'),))