Diff

plugins/mod_storage_sql.lua @ 3854:5a1551d604b1

mod_storage_sql: Auto-initialize SQLite3 database.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 12 Dec 2010 05:39:24 +0500
parent 3853:1a58bbe19c60
child 3743:5adfb8d0444d
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Sun Dec 12 05:38:48 2010 +0500
+++ b/plugins/mod_storage_sql.lua	Sun Dec 12 05:39:24 2010 +0500
@@ -47,6 +47,19 @@
 
 	dbh:autocommit(false); -- don't commit automatically
 	connection = dbh;
+	
+	if params.driver == "SQLite3" then -- auto initialize
+		local stmt = assert(connection:prepare("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='Prosody';"));
+		local ok = assert(stmt:execute());
+		local count = stmt:fetch()[1];
+		if count == 0 then
+			local stmt = assert(connection:prepare("CREATE TABLE Prosody (host TEXT, user TEXT, store TEXT, key TEXT, subkey TEXT, type TEXT, value TEXT);"));
+			assert(stmt:execute());
+			assert(connection:commit());
+			module:log("debug", "Initialized new SQLite3 database");
+		end
+		--print("===", json.stringify())
+	end
 end
 
 local function serialize(value)