From 6f34b37cca627374931c3477cd22432ecade4e8b Mon Sep 17 00:00:00 2001 From: GRMrGecko Date: Sun, 1 Jul 2012 06:05:21 -0500 Subject: [PATCH] Added time as primary key for SQLite as it makes it take less than a second to order by time when you have over 1,000,000 entires in your log. MySQL seems fine as it has a cache. If someone who uses MySQL finds this wrong, please ether add in the primary key system yourself and submit a patch or let me know in a bug report. --- logsqlite.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/logsqlite.cpp b/logsqlite.cpp index 398a451..6a13dde 100755 --- a/logsqlite.cpp +++ b/logsqlite.cpp @@ -239,7 +239,7 @@ public: SetSetting("replayAll","0"); SetSetting("logLimit","1"); SetSetting("logLevel","1"); - SetSetting("version","1"); + SetSetting("version","2"); } } } @@ -251,7 +251,7 @@ public: } sqlite3_finalize(result); if (status!=SQLITE_ROW) { - status = sqlite3_prepare(database, "CREATE TABLE `messages` (`target` text, `nick` text, `type` text, `message` text, `time` real(20,5))", -1, &result, NULL); + status = sqlite3_prepare(database, "CREATE TABLE `messages` (`target` text, `nick` text, `type` text, `message` text, `time` real(20,5) PRIMARY KEY)", -1, &result, NULL); if (status==SQLITE_OK) { sqlite3_step(result); sqlite3_finalize(result); @@ -278,9 +278,55 @@ public: logLevel = atoi(GetSetting("logLevel").c_str()); unsigned long version = strtoul(GetSetting("version").c_str(), NULL, 10); - if (version==0) + if (version==0) { SetSetting("version","1"); - + version = 1; + } + if (version==1) { + status = sqlite3_prepare(database, "BEGIN TRANSACTION", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "CREATE TEMPORARY TABLE `messages_backup` (`target` text, `nick` text, `type` text, `message` text, `time` real(20,5))", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "INSERT INTO `messages_backup` SELECT * FROM `messages`", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "DROP TABLE `messages`", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "CREATE TABLE `messages` (`target` text, `nick` text, `type` text, `message` text, `time` real(20,5) PRIMARY KEY)", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "INSERT INTO `messages` SELECT * FROM `messages_backup`", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "DROP TABLE `messages_backup`", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + status = sqlite3_prepare(database, "COMMIT", -1, &result, NULL); + if (status==SQLITE_OK) { + sqlite3_step(result); + sqlite3_finalize(result); + } + SetSetting("version","2"); + version = 2; + } + UpdateIgnoreLists(); return true;