diff src/memory/sqlite.py @ 448:17c7e48bf68f

core: - history management improved - better timestamp precision for history bridge + core: history signature change (now return a list instead of a dict, and timestamp is now float) quick_frontend: - use of new history API - removed deprecated keep_last argument in getHistory (was only used by Sortilège)
author Goffi <goffi@goffi.org>
date Sun, 04 Dec 2011 16:18:56 +0100
parents a6640bb8ee95
children cf005701624b
line wrap: on
line diff
--- a/src/memory/sqlite.py	Sun Dec 04 00:58:20 2011 +0100
+++ b/src/memory/sqlite.py	Sun Dec 04 16:18:56 2011 +0100
@@ -178,7 +178,7 @@
         """
         assert(profile!=None)
         d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, profile_id) VALUES (?,?,?,?,?,?,?)",
-                                (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or int(time.time()),
+                                (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(),
                                 message, self.profiles[profile]))
         d.addErrback(lambda ignore: error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" %
                                          {"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message})))
@@ -190,14 +190,15 @@
         @param to_jid: dest JID (full, or bare for catchall
         @param size: maximum number of messages to get, or 0 for unlimited
         """
-        def sqliteToDict(result):
-            result_dict = {}
-            for row in result:
+        def sqliteToDict(query_result):
+            query_result.reverse()
+            result = []
+            for row in query_result:
                 timestamp, source, source_res, dest, dest_res, message = row
-                result_dict[timestamp] = ("%s/%s" % (source, source_res) if source_res else source,
+                result.append((timestamp, "%s/%s" % (source, source_res) if source_res else source,
                                           "%s/%s" % (dest, dest_res) if dest_res else dest,
-                                          message)
-            return result_dict
+                                          message))
+            return result
         
         query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message FROM history WHERE"]
         values = []
@@ -214,6 +215,9 @@
         if to_jid.resource:
             query_parts.append("AND dest_res=?")
             values.append(to_jid.resource)
+
+        query_parts.append("ORDER BY timestamp DESC")
+
         if limit:
             query_parts.append("LIMIT ?")
             values.append(limit)