Mercurial > libervia-backend
diff src/tools/sqlite.py @ 425:e4e9187e3b5b
backend, bridge: asynchronous history
quick_frontend: use of asynchronous history
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 08 Nov 2011 01:08:11 +0100 |
parents | 6c20c76abdcc |
children |
line wrap: on
line diff
--- a/src/tools/sqlite.py Mon Nov 07 22:27:07 2011 +0100 +++ b/src/tools/sqlite.py Tue Nov 08 01:08:11 2011 +0100 @@ -24,6 +24,7 @@ from twisted.enterprise import adbapi from twisted.internet import defer import os.path +import time class SqliteStorage(): """This class manage storage with Sqlite database""" @@ -44,7 +45,7 @@ info(_("The database is new, creating the tables")) database_creation = [ "CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))", - "CREATE TABLE historic (id INTEGER PRIMARY KEY ASC, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME, message TEXT, FOREIGN KEY(profile_id) REFERENCES profiles(id))", + "CREATE TABLE history (id INTEGER PRIMARY KEY ASC, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME, message TEXT, FOREIGN KEY(profile_id) REFERENCES profiles(id))", "CREATE TABLE param_gen (category TEXT, name TEXT, value TEXT, PRIMARY KEY (category,name))", "CREATE TABLE param_ind (category TEXT, name TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (category,name,profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id))"] for op in database_creation: @@ -162,6 +163,59 @@ d.addErrback(lambda ignore: error(_("Can't set individual parameter (%(category)s/%(name)s) for [%(profile)s] in database" % {"category":category, "name":name, "profile":profile}))) return d + #History + def addToHistory(self, from_jid, to_jid, message, timestamp=None, profile=None): + """Store a new message in history + @param from_jid: full source JID + @param to_jid: full dest JID + @param message: message + @param timestamp: timestamp in seconds since epoch, or None to use current time + """ + 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()), + 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}))) + return d + + def getHistory(self, from_jid, to_jid, limit=0, between=True): + """Store a new message in history + @param from_jid: source JID (full, or bare for catchall + @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: + timestamp, source, source_res, dest, dest_res, message = row + result_dict[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 + + query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message FROM history WHERE"] + values = [] + + if between: + query_parts.append("(source=? OR source=?) AND (dest=? or dest=?)") + values.extend([from_jid.userhost(), to_jid.userhost(), to_jid.userhost(), from_jid.userhost()]) + else: + query_parts.append("source=? AND dest=?") + values.extend([from_jid.userhost(), to_jid.userhost()]) + if from_jid.resource: + query_parts.append("AND source_res=?") + values.append(from_jid.resource) + if to_jid.resource: + query_parts.append("AND dest_res=?") + values.append(to_jid.resource) + if limit: + query_parts.append("LIMIT ?") + values.append(limit) + d = self.dbpool.runQuery(" ".join(query_parts), values) + return d.addCallback(sqliteToDict) + + ##Helper methods## def __getFirstResult(self, result):