plugins/mod_storage_sql2.lua
author Waqas Hussain <waqas20@gmail.com>
Tue, 16 Dec 2014 18:33:54 -0500
changeset 6537 b89406fa076c
parent 6349 0cee68dd35f8
parent 6535 a966efeb6cb3
child 6730 eb9c842b80fa
permissions -rw-r--r--
Merge 0.10 -> trunk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local json = require "util.json";
5735
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
     3
local xml_parse = require "util.xml".parse;
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
     4
local uuid = require "util.uuid";
6165
6a184b16b717 core.certmanager, core.moduleapi, mod_storage_sql, mod_storage_sql2: Import from util.paths
Kim Alvefur <zash@zash.se>
parents: 6060
diff changeset
     5
local resolve_relative_path = require "util.paths".resolve_relative_path;
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
5735
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
     7
local stanza_mt = require"util.stanza".stanza_mt;
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
     8
local getmetatable = getmetatable;
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
     9
local t_concat = table.concat;
5735
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
    10
local function is_stanza(x) return getmetatable(x) == stanza_mt; end
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
    11
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
    12
local noop = function() end
5733
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    13
local unpack = unpack
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    14
local function iterator(result)
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    15
	return function(result)
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    16
		local row = result();
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    17
		if row ~= nil then
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    18
			return unpack(row);
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    19
		end
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    20
	end, result, nil;
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    21
end
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
    22
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
local mod_sql = module:require("sql");
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
local params = module:get_option("sql");
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
local engine; -- TODO create engine
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
local function create_table()
5740
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    29
	local Table,Column,Index = mod_sql.Table,mod_sql.Column,mod_sql.Index;
5894
9e47ece9457c mod_storage_sql2: Switch to the util.sql table definition for the main table
Kim Alvefur <zash@zash.se>
parents: 5893
diff changeset
    30
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
	local ProsodyTable = Table {
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		name="prosody";
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		Column { name="host", type="TEXT", nullable=false };
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		Column { name="user", type="TEXT", nullable=false };
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
		Column { name="store", type="TEXT", nullable=false };
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
		Column { name="key", type="TEXT", nullable=false };
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		Column { name="type", type="TEXT", nullable=false };
5891
b6a56934338c mod_storage_sql2: Use MEDIUMTEXT fields for value columns (ie TEXT on non-MySQL)
Kim Alvefur <zash@zash.se>
parents: 5884
diff changeset
    38
		Column { name="value", type="MEDIUMTEXT", nullable=false };
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		Index { name="prosody_index", "host", "user", "store", "key" };
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	};
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
	engine:transaction(function()
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		ProsodyTable:create(engine);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
	end);
5882
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    44
5740
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    45
	local ProsodyArchiveTable = Table {
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    46
		name="prosodyarchive";
5913
6865eecaf5a5 mod_storage_sql2: Auto increment columns won't be NULL, so drop nullable=false
Kim Alvefur <zash@zash.se>
parents: 5903
diff changeset
    47
		Column { name="sort_id", type="INTEGER", primary_key=true, auto_increment=true };
5740
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    48
		Column { name="host", type="TEXT", nullable=false };
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    49
		Column { name="user", type="TEXT", nullable=false };
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    50
		Column { name="store", type="TEXT", nullable=false };
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    51
		Column { name="key", type="TEXT", nullable=false }; -- item id
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    52
		Column { name="when", type="INTEGER", nullable=false }; -- timestamp
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    53
		Column { name="with", type="TEXT", nullable=false }; -- related id
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    54
		Column { name="type", type="TEXT", nullable=false };
5891
b6a56934338c mod_storage_sql2: Use MEDIUMTEXT fields for value columns (ie TEXT on non-MySQL)
Kim Alvefur <zash@zash.se>
parents: 5884
diff changeset
    55
		Column { name="value", type="MEDIUMTEXT", nullable=false };
5893
62ce0328fdfe mod_storage_sql2: The prosodyarchive_index should be unique
Kim Alvefur <zash@zash.se>
parents: 5892
diff changeset
    56
		Index { name="prosodyarchive_index", unique = true, "host", "user", "store", "key" };
5740
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    57
	};
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    58
	engine:transaction(function()
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    59
		ProsodyArchiveTable:create(engine);
6fce00f61acf mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents: 5735
diff changeset
    60
	end);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
end
5883
39b187e7e892 mod_storage_sql2, util.sql: Move code for setting encoding to util.sql
Kim Alvefur <zash@zash.se>
parents: 5882
diff changeset
    62
5881
12d12bda4b8c mod_storage_sql2: Split up setting of encoding and table upgrade code
Kim Alvefur <zash@zash.se>
parents: 5848
diff changeset
    63
local function upgrade_table()
5765
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    64
	if params.driver == "MySQL" then
5882
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    65
		local success,err = engine:transaction(function()
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    66
			local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    67
			if result:rowcount() > 0 then
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    68
				module:log("info", "Upgrading database schema...");
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    69
				engine:execute("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT");
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    70
				module:log("info", "Database table automatically upgraded");
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    71
			end
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    72
			return true;
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    73
		end);
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    74
		if not success then
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    75
			module:log("error", "Failed to check/upgrade database schema (%s), please see "
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    76
				.."http://prosody.im/doc/mysql for help",
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    77
				err or "unknown error");
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    78
			return false;
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    79
		end
5765
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    80
		-- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    81
		local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE` FROM `information_schema`.`columns` WHERE `TABLE_NAME`='prosody' AND ( `CHARACTER_SET_NAME`!='utf8' OR `COLLATION_NAME`!='utf8_bin' );";
5882
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    82
		success,err = engine:transaction(function()
5765
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    83
			local result = engine:execute(check_encoding_query);
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    84
			local n_bad_columns = result:rowcount();
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    85
			if n_bad_columns > 0 then
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    86
				module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns);
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    87
				local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;";
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    88
				local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';";
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    89
				for row in result:rows() do
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    90
					local column_name, column_type = unpack(row);
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    91
					engine:execute(fix_column_query1:format(column_name, column_name));
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    92
					engine:execute(fix_column_query2:format(column_name, column_name, column_type));
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    93
				end
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    94
				module:log("info", "Database encoding upgrade complete!");
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    95
			end
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    96
		end);
5882
fbba2997aabb mod_storage_sql2: Move all schema upgrade code to the same place
Kim Alvefur <zash@zash.se>
parents: 5881
diff changeset
    97
		success,err = engine:transaction(function() return engine:execute(check_encoding_query); end);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
		if not success then
5765
d854c17a45fd mod_storage_sql2: Do an early return and drop an indentation level
Kim Alvefur <zash@zash.se>
parents: 5741
diff changeset
    99
			module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error");
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
		end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
do -- process options to get a db connection
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	params = params or { driver = "SQLite3" };
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5765
diff changeset
   106
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
	if params.driver == "SQLite3" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
		params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5765
diff changeset
   110
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
	assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
	--local dburi = db2uri(params);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
	engine = mod_sql:create_engine(params);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5765
diff changeset
   115
5884
9cb81a17ae23 mod_storage_sql2: Move checking of the sql_manage_tables option so it also includes table upgrades (again)
Kim Alvefur <zash@zash.se>
parents: 5883
diff changeset
   116
	if module:get_option("sql_manage_tables", true) then
9cb81a17ae23 mod_storage_sql2: Move checking of the sql_manage_tables option so it also includes table upgrades (again)
Kim Alvefur <zash@zash.se>
parents: 5883
diff changeset
   117
		-- Automatically create table, ignore failure (table probably already exists)
9cb81a17ae23 mod_storage_sql2: Move checking of the sql_manage_tables option so it also includes table upgrades (again)
Kim Alvefur <zash@zash.se>
parents: 5883
diff changeset
   118
		create_table();
9cb81a17ae23 mod_storage_sql2: Move checking of the sql_manage_tables option so it also includes table upgrades (again)
Kim Alvefur <zash@zash.se>
parents: 5883
diff changeset
   119
		-- Encoding mess
9cb81a17ae23 mod_storage_sql2: Move checking of the sql_manage_tables option so it also includes table upgrades (again)
Kim Alvefur <zash@zash.se>
parents: 5883
diff changeset
   120
		upgrade_table();
9cb81a17ae23 mod_storage_sql2: Move checking of the sql_manage_tables option so it also includes table upgrades (again)
Kim Alvefur <zash@zash.se>
parents: 5883
diff changeset
   121
	end
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
local function serialize(value)
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
	local t = type(value);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
	if t == "string" or t == "boolean" or t == "number" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
		return t, tostring(value);
5735
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
   128
	elseif is_stanza(value) then
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
   129
		return "xml", tostring(value);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
	elseif t == "table" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
		local value,err = json.encode(value);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
		if value then return "json", value; end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
		return nil, err;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
	return nil, "Unhandled value type: "..t;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
local function deserialize(t, value)
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
	if t == "string" then return value;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
	elseif t == "boolean" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
		if value == "true" then return true;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
		elseif value == "false" then return false; end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
	elseif t == "number" then return tonumber(value);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
	elseif t == "json" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
		return json.decode(value);
5735
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
   145
	elseif t == "xml" then
24f4e73645fe mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents: 5734
diff changeset
   146
		return xml_parse(value);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
local host = module.host;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
local user, store;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
local function keyval_store_get()
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
	local haveany;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
	local result = {};
5732
4aa1d6f5083a mod_storage_sql2: Make sure the user field is not NULL
Kim Alvefur <zash@zash.se>
parents: 5711
diff changeset
   156
	for row in engine:select("SELECT `key`,`type`,`value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?", host, user or "", store) do
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
		haveany = true;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
		local k = row[1];
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
		local v = deserialize(row[2], row[3]);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
		if k and v then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
			if k ~= "" then result[k] = v; elseif type(v) == "table" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   162
				for a,b in pairs(v) do
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   163
					result[a] = b;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   164
				end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   165
			end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
		end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   168
	if haveany then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   169
		return result;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
local function keyval_store_set(data)
5732
4aa1d6f5083a mod_storage_sql2: Make sure the user field is not NULL
Kim Alvefur <zash@zash.se>
parents: 5711
diff changeset
   173
	engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?", host, user or "", store);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5765
diff changeset
   174
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
	if data and next(data) ~= nil then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
		local extradata = {};
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
		for key, value in pairs(data) do
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   178
			if type(key) == "string" and key ~= "" then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   179
				local t, value = serialize(value);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   180
				assert(t, value);
5732
4aa1d6f5083a mod_storage_sql2: Make sure the user field is not NULL
Kim Alvefur <zash@zash.se>
parents: 5711
diff changeset
   181
				engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, key, t, value);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   182
			else
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
				extradata[key] = value;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   184
			end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   185
		end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
		if next(extradata) ~= nil then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
			local t, extradata = serialize(extradata);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
			assert(t, extradata);
5732
4aa1d6f5083a mod_storage_sql2: Make sure the user field is not NULL
Kim Alvefur <zash@zash.se>
parents: 5711
diff changeset
   189
			engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, user or "", store, "", t, extradata);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
		end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
	return true;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   194
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   195
local keyval_store = {};
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   196
keyval_store.__index = keyval_store;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   197
function keyval_store:get(username)
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   198
	user,store = username,self.store;
6282
bce801e40484 mod_storage_sql2: Don't ignore failure in keyval_store:get() (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 6165
diff changeset
   199
	local ok, result = engine:transaction(keyval_store_get);
bce801e40484 mod_storage_sql2: Don't ignore failure in keyval_store:get() (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 6165
diff changeset
   200
	if not ok then return ok, result; end
bce801e40484 mod_storage_sql2: Don't ignore failure in keyval_store:get() (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents: 6165
diff changeset
   201
	return result;
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   202
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   203
function keyval_store:set(username, data)
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
	user,store = username,self.store;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   205
	return engine:transaction(function()
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   206
		return keyval_store_set(data);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
	end);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   208
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   209
function keyval_store:users()
5733
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
   210
	local ok, result = engine:transaction(function()
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
		return engine:select("SELECT DISTINCT `user` FROM `prosody` WHERE `host`=? AND `store`=?", host, self.store);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   212
	end);
5733
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
   213
	if not ok then return ok, result end
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
   214
	return iterator(result);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   217
local map_store = {};
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   218
map_store.__index = map_store;
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   219
function map_store:get(username, key)
6335
eaf6e7986934 plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents: 6331
diff changeset
   220
	local ok, result = engine:transaction(function()
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   221
		if type(key) == "string" and key ~= "" then
6349
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   222
			for row in engine:select("SELECT `type`, `value` FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, username or "", self.store, key) do
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   223
				return deserialize(row[1], row[2]);
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   224
			end
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   225
		else
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   226
			error("TODO: non-string keys");
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   227
		end
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   228
	end);
6335
eaf6e7986934 plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents: 6331
diff changeset
   229
	if not ok then return nil, result; end
eaf6e7986934 plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents: 6331
diff changeset
   230
	return result;
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   231
end
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   232
function map_store:set(username, key, data)
6335
eaf6e7986934 plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents: 6331
diff changeset
   233
	local ok, result = engine:transaction(function()
6349
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   234
		if type(key) == "string" and key ~= "" then
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   235
			engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?",
6349
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   236
				host, username or "", self.store, key);
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   237
			if data ~= nil then
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   238
				local t, value = assert(serialize(data));
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   239
				engine:insert("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", host, username or "", self.store, key, t, value);
0cee68dd35f8 mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents: 6335
diff changeset
   240
			end
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   241
		else
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   242
			error("TODO: non-string keys");
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   243
		end
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   244
		return true;
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   245
	end);
6335
eaf6e7986934 plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents: 6331
diff changeset
   246
	if not ok then return nil, result; end
eaf6e7986934 plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents: 6331
diff changeset
   247
	return result;
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   248
end
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   249
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   250
local archive_store = {}
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   251
archive_store.__index = archive_store
5969
0a3b8003ac9c mod_storage_sql2: Expose the unique key argument, allowing arbitrary ids. Conflicting items are removed.
Kim Alvefur <zash@zash.se>
parents: 5968
diff changeset
   252
function archive_store:append(username, key, when, with, value)
0a3b8003ac9c mod_storage_sql2: Expose the unique key argument, allowing arbitrary ids. Conflicting items are removed.
Kim Alvefur <zash@zash.se>
parents: 5968
diff changeset
   253
	if value == nil then -- COMPAT early versions
0a3b8003ac9c mod_storage_sql2: Expose the unique key argument, allowing arbitrary ids. Conflicting items are removed.
Kim Alvefur <zash@zash.se>
parents: 5968
diff changeset
   254
		when, with, value, key = key, when, with, value
0a3b8003ac9c mod_storage_sql2: Expose the unique key argument, allowing arbitrary ids. Conflicting items are removed.
Kim Alvefur <zash@zash.se>
parents: 5968
diff changeset
   255
	end
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   256
	local user,store = username,self.store;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   257
	return engine:transaction(function()
5996
e7efa9703a3f mod_storage_sql2: Only attempt to delete conflicting items if an ID/key is given
Kim Alvefur <zash@zash.se>
parents: 5995
diff changeset
   258
		if key then
e7efa9703a3f mod_storage_sql2: Only attempt to delete conflicting items if an ID/key is given
Kim Alvefur <zash@zash.se>
parents: 5995
diff changeset
   259
			engine:delete("DELETE FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", host, user or "", store, key);
e7efa9703a3f mod_storage_sql2: Only attempt to delete conflicting items if an ID/key is given
Kim Alvefur <zash@zash.se>
parents: 5995
diff changeset
   260
		else
e7efa9703a3f mod_storage_sql2: Only attempt to delete conflicting items if an ID/key is given
Kim Alvefur <zash@zash.se>
parents: 5995
diff changeset
   261
			key = uuid.generate();
e7efa9703a3f mod_storage_sql2: Only attempt to delete conflicting items if an ID/key is given
Kim Alvefur <zash@zash.se>
parents: 5995
diff changeset
   262
		end
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   263
		local t, value = serialize(value);
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   264
		engine:insert("INSERT INTO `prosodyarchive` (`host`, `user`, `store`, `when`, `with`, `key`, `type`, `value`) VALUES (?,?,?,?,?,?,?,?)", host, user or "", store, when, with, key, t, value);
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   265
		return key;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   266
	end);
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   267
end
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   268
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   269
-- Helpers for building the WHERE clause
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   270
local function archive_where(query, args, where)
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   271
	-- Time range, inclusive
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   272
	if query.start then
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   273
		args[#args+1] = query.start
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   274
		where[#where+1] = "`when` >= ?"
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   275
	end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   276
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   277
	if query["end"] then
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   278
		args[#args+1] = query["end"];
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   279
		if query.start then
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   280
			where[#where] = "`when` BETWEEN ? AND ?" -- is this inclusive?
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   281
		else
5903
0e0aab930e10 mod_storage_sql2: Fix backwards comparison of timestamp
Kim Alvefur <zash@zash.se>
parents: 5894
diff changeset
   282
			where[#where+1] = "`when` <= ?"
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   283
		end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   284
	end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   285
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   286
	-- Related name
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   287
	if query.with then
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   288
		where[#where+1] = "`with` = ?";
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   289
		args[#args+1] = query.with
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   290
	end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   291
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   292
	-- Unique id
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   293
	if query.key then
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   294
		where[#where+1] = "`key` = ?";
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   295
		args[#args+1] = query.key
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   296
	end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   297
end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   298
local function archive_where_id_range(query, args, where)
5968
e1e9f1411442 mod_storage_sql2: Include user, host and store in id lookup
Kim Alvefur <zash@zash.se>
parents: 5913
diff changeset
   299
	local args_len = #args
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   300
	-- Before or after specific item, exclusive
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   301
	if query.after then  -- keys better be unique!
5985
98ed9fe368ac mod_storage_sql2: Fix SQL syntax
Kim Alvefur <zash@zash.se>
parents: 5979
diff changeset
   302
		where[#where+1] = "`sort_id` > (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1)"
5968
e1e9f1411442 mod_storage_sql2: Include user, host and store in id lookup
Kim Alvefur <zash@zash.se>
parents: 5913
diff changeset
   303
		args[args_len+1], args[args_len+2], args[args_len+3], args[args_len+4] = query.after, args[1], args[2], args[3];
e1e9f1411442 mod_storage_sql2: Include user, host and store in id lookup
Kim Alvefur <zash@zash.se>
parents: 5913
diff changeset
   304
		args_len = args_len + 4
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   305
	end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   306
	if query.before then
5985
98ed9fe368ac mod_storage_sql2: Fix SQL syntax
Kim Alvefur <zash@zash.se>
parents: 5979
diff changeset
   307
		where[#where+1] = "`sort_id` < (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1)"
5968
e1e9f1411442 mod_storage_sql2: Include user, host and store in id lookup
Kim Alvefur <zash@zash.se>
parents: 5913
diff changeset
   308
		args[args_len+1], args[args_len+2], args[args_len+3], args[args_len+4] = query.before, args[1], args[2], args[3];
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   309
	end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   310
end
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   311
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   312
function archive_store:find(username, query)
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   313
	query = query or {};
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   314
	local user,store = username,self.store;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   315
	local total;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   316
	local ok, result = engine:transaction(function()
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   317
		local sql_query = "SELECT `key`, `type`, `value`, `when` FROM `prosodyarchive` WHERE %s ORDER BY `sort_id` %s%s;";
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   318
		local args = { host, user or "", store, };
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   319
		local where = { "`host` = ?", "`user` = ?", "`store` = ?", };
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   320
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   321
		archive_where(query, args, where);
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   322
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   323
		-- Total matching
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   324
		if query.total then
6060
de4c83feb064 mod_storage_sql2: Build counter query without ORDER BY clause
Kim Alvefur <zash@zash.se>
parents: 6013
diff changeset
   325
			local stats = engine:select("SELECT COUNT(*) FROM `prosodyarchive` WHERE " .. t_concat(where, " AND "), unpack(args));
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   326
			if stats then
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   327
				local _total = stats()
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   328
				total = _total and _total[1];
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   329
			end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   330
			if query.limit == 0 then -- Skip the real query
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   331
				return noop, total;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   332
			end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   333
		end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   334
5847
6aaa7ad4463c mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
   335
		archive_where_id_range(query, args, where);
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   336
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   337
		if query.limit then
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   338
			args[#args+1] = query.limit;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   339
		end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   340
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   341
		sql_query = sql_query:format(t_concat(where, " AND "), query.reverse and "DESC" or "ASC", query.limit and " LIMIT ?" or "");
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   342
		module:log("debug", sql_query);
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   343
		return engine:select(sql_query, unpack(args));
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   344
	end);
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   345
	if not ok then return ok, result end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   346
	return function()
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   347
		local row = result();
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   348
		if row ~= nil then
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   349
			return row[1], deserialize(row[2], row[3]), row[4];
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   350
		end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   351
	end, total;
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   352
end
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   353
5848
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   354
function archive_store:delete(username, query)
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   355
	query = query or {};
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   356
	local user,store = username,self.store;
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   357
	return engine:transaction(function()
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   358
		local sql_query = "DELETE FROM `prosodyarchive` WHERE %s;";
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   359
		local args = { host, user or "", store, };
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   360
		local where = { "`host` = ?", "`user` = ?", "`store` = ?", };
6013
918ab89cb68d mod_storage_sql2: archive:delete() with username = true deletes for all users
Kim Alvefur <zash@zash.se>
parents: 5996
diff changeset
   361
		if user == true then
918ab89cb68d mod_storage_sql2: archive:delete() with username = true deletes for all users
Kim Alvefur <zash@zash.se>
parents: 5996
diff changeset
   362
			table.remove(args, 2);
918ab89cb68d mod_storage_sql2: archive:delete() with username = true deletes for all users
Kim Alvefur <zash@zash.se>
parents: 5996
diff changeset
   363
			table.remove(where, 2);
918ab89cb68d mod_storage_sql2: archive:delete() with username = true deletes for all users
Kim Alvefur <zash@zash.se>
parents: 5996
diff changeset
   364
		end
5848
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   365
		archive_where(query, args, where);
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   366
		archive_where_id_range(query, args, where);
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   367
		sql_query = sql_query:format(t_concat(where, " AND "));
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   368
		module:log("debug", sql_query);
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   369
		return engine:delete(sql_query, unpack(args));
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   370
	end);
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   371
end
06156bfd4eaf mod_storage_sql2: Add method for deleting items from archives with same syntax as :find()
Kim Alvefur <zash@zash.se>
parents: 5847
diff changeset
   372
5734
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   373
local stores = {
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   374
	keyval = keyval_store;
6331
fc5113a4540e plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents: 6282
diff changeset
   375
	map = map_store;
5741
c7a664e496b3 mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents: 5740
diff changeset
   376
	archive = archive_store;
5734
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   377
};
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   378
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
local driver = {};
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
function driver:open(store, typ)
5734
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   382
	local store_mt = stores[typ or "keyval"];
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   383
	if store_mt then
49f1fed6e25e mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents: 5733
diff changeset
   384
		return setmetatable({ store = store }, store_mt);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   385
	end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   386
	return nil, "unsupported-store";
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   387
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   388
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   389
function driver:stores(username)
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   390
	local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" ..
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   391
		(username == true and "!=?" or "=?");
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
	if username == true or not username then
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   393
		username = "";
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   394
	end
5733
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
   395
	local ok, result = engine:transaction(function()
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
		return engine:select(sql, host, username);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   397
	end);
5733
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
   398
	if not ok then return ok, result end
aeeced7b0149 mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents: 5732
diff changeset
   399
	return iterator(result);
5494
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   400
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   401
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   402
function driver:purge(username)
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   403
	return engine:transaction(function()
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   404
		local stmt,err = engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   405
		return true,err;
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   406
	end);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   407
end
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   408
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   409
module:provides("storage", driver);
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   410
9916f0a2d178 mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   411