diff options
author | Catalin Badea <catalin.badea392@gmail.com> | 2012-06-27 12:02:47 (GMT) |
---|---|---|
committer | Catalin Badea <catalin.badea392@gmail.com> | 2012-06-27 12:02:47 (GMT) |
commit | 0998d6a8f138e0fd00245fc3c1efc8878fa7b888 (patch) | |
tree | d728ad825753ec227ee190a2953db4315cf85d88 /Swiften/History/SQLiteHistoryManager.cpp | |
parent | ad7d00c25b74d212457fa599861c5583e79e7c13 (diff) | |
download | swift-contrib-0998d6a8f138e0fd00245fc3c1efc8878fa7b888.zip swift-contrib-0998d6a8f138e0fd00245fc3c1efc8878fa7b888.tar.bz2 |
Log messages to the sqlite db and dump all messages in the history viewer
Diffstat (limited to 'Swiften/History/SQLiteHistoryManager.cpp')
-rw-r--r-- | Swiften/History/SQLiteHistoryManager.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/Swiften/History/SQLiteHistoryManager.cpp b/Swiften/History/SQLiteHistoryManager.cpp index a24dec2..e10de07 100644 --- a/Swiften/History/SQLiteHistoryManager.cpp +++ b/Swiften/History/SQLiteHistoryManager.cpp @@ -25,7 +25,7 @@ SQLiteHistoryManager::SQLiteHistoryManager(const std::string& file) : db_(0) { } char* errorMessage; - int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('from' INTEGER, 'to' INTEGER, 'message' STRING, 'time' INTEGER)", 0, 0, &errorMessage); + int result = sqlite3_exec(db_, "CREATE TABLE IF NOT EXISTS messages('baseID' INTEGER, 'from' INTEGER, 'nick' STRING, 'message' STRING, 'time' INTEGER)", 0, 0, &errorMessage); if (result != SQLITE_OK) { std::cerr << "SQL Error: " << errorMessage << std::endl; sqlite3_free(errorMessage); @@ -44,7 +44,12 @@ SQLiteHistoryManager::~SQLiteHistoryManager() { void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { int secondsSinceEpoch = (message.getTime() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds(); - std::string statement = std::string("INSERT INTO messages('from', 'to', 'message', 'time') VALUES(") + boost::lexical_cast<std::string>(getIDForJID(message.getFrom())) + ", " + boost::lexical_cast<std::string>(getIDForJID(message.getTo())) + ", '" + getEscapedString(message.getMessage()) + "', " + boost::lexical_cast<std::string>(secondsSinceEpoch) + ")"; + std::string statement = std::string("INSERT INTO messages('baseID', 'from', 'nick', 'message', 'time') VALUES(") + + boost::lexical_cast<std::string>(getIDForJID(message.getBaseJID())) + ", " + + boost::lexical_cast<std::string>(getIDForJID(message.getFromJID())) + ", '" + + message.getDisplayNick() + "', '" + + getEscapedString(message.getMessage()) + "', " + + boost::lexical_cast<std::string>(secondsSinceEpoch) + ")"; char* errorMessage; int result = sqlite3_exec(db_, statement.c_str(), 0, 0, &errorMessage); if (result != SQLITE_OK) { @@ -56,20 +61,21 @@ void SQLiteHistoryManager::addMessage(const HistoryMessage& message) { std::vector<HistoryMessage> SQLiteHistoryManager::getMessages() const { std::vector<HistoryMessage> result; sqlite3_stmt* selectStatement; - std::string selectQuery("SELECT messages.'from', messages.'to', messages.'message', messages.'time' FROM messages"); + std::string selectQuery("SELECT messages.'baseID', messages.'from', messages.'nick', messages.'message', messages.'time' FROM messages"); int r = sqlite3_prepare(db_, selectQuery.c_str(), selectQuery.size(), &selectStatement, NULL); if (r != SQLITE_OK) { std::cout << "Error: " << sqlite3_errmsg(db_) << std::endl; } r = sqlite3_step(selectStatement); while (r == SQLITE_ROW) { - boost::optional<JID> from(getJIDFromID(sqlite3_column_int(selectStatement, 0))); - boost::optional<JID> to(getJIDFromID(sqlite3_column_int(selectStatement, 1))); - std::string message(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 2))); - int secondsSinceEpoch(sqlite3_column_int(selectStatement, 3)); + boost::optional<JID> baseJID(getJIDFromID(sqlite3_column_int(selectStatement, 0))); + boost::optional<JID> from(getJIDFromID(sqlite3_column_int(selectStatement, 1))); + std::string nick(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 2))); + std::string message(reinterpret_cast<const char*>(sqlite3_column_text(selectStatement, 3))); + int secondsSinceEpoch(sqlite3_column_int(selectStatement, 4)); boost::posix_time::ptime time(boost::gregorian::date(1970, 1, 1), boost::posix_time::seconds(secondsSinceEpoch)); - result.push_back(HistoryMessage(message, (from ? *from : JID()), (to ? *to : JID()), time)); + result.push_back(HistoryMessage(message, (baseJID ? *baseJID : JID()), (from ? *from : JID()), nick, time)); r = sqlite3_step(selectStatement); } if (r != SQLITE_DONE) { |