# HG changeset patch # User Matthew Wild # Date 1294719765 0 # Node ID 8792da53e5036c292fd99a967cfa4420a940ddff # Parent c9363102afd23da7c41420fd13d3481f1e645cec# Parent d26db1f936f80e295d9459d7facdc969dfe1855d Merge 0.8->trunk diff -r c9363102afd2 -r 8792da53e503 plugins/mod_storage_sql.lua --- a/plugins/mod_storage_sql.lua Mon Jan 10 16:55:14 2011 +0000 +++ b/plugins/mod_storage_sql.lua Tue Jan 11 04:22:45 2011 +0000 @@ -64,6 +64,37 @@ end end +local function create_table() + local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; + if params.driver == "PostgreSQL" then + create_sql = create_sql:gsub("`", "\""); + end + + local stmt = connection:prepare(create_sql); + if stmt then + local ok = stmt:execute(); + local commit_ok = connection:commit(); + if ok and commit_ok then + module:log("info", "Initialized new %s database with prosody table", params.driver); + local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)"; + if params.driver == "PostgreSQL" then + index_sql = index_sql:gsub("`", "\""); + elseif params.driver == "MySQL" then + index_sql = index_sql:gsub("`([,)])", "`(20)%1"); + end + local stmt, err = connection:prepare(index_sql); + local ok, commit_ok, commit_err; + if stmt then + ok, err = stmt:execute(); + commit_ok, commit_err = connection:commit(); + end + if not(ok and commit_ok) then + module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err); + end + end + end +end + do -- process options to get a db connection DBI = require "DBI"; @@ -78,19 +109,7 @@ assert(connect()); -- Automatically create table, ignore failure (table probably already exists) - local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; - if params.driver == "PostgreSQL" then - create_sql = create_sql:gsub("`", "\""); - end - - local stmt = connection:prepare(create_sql); - if stmt then - local ok = stmt:execute(); - local commit_ok = connection:commit(); - if ok and commit_ok then - module:log("info", "Initialized new %s database with prosody table", params.driver); - end - end + create_table(); end local function serialize(value) diff -r c9363102afd2 -r 8792da53e503 util/datamanager.lua --- a/util/datamanager.lua Mon Jan 10 16:55:14 2011 +0000 +++ b/util/datamanager.lua Tue Jan 11 04:22:45 2011 +0000 @@ -57,7 +57,7 @@ return path; end -local data_path = prosody.paths.data; +local data_path = (prosody and prosody.paths and prosody.paths.data) or "."; local callbacks = {}; ------- API -------------