Software /
code /
prosody
Annotate
plugins/mod_storage_sql2.lua @ 5741:c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 12 Jul 2013 02:53:24 +0200 |
parent | 5740:6fce00f61acf |
child | 5765:d854c17a45fd |
rev | line source |
---|---|
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"; |
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
|
5 local resolve_relative_path = require "core.configmanager".resolve_relative_path; |
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; |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
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 }; |
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
|
38 Column { name="value", 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
|
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);]] |
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
|
44 if not module:get_option("sql_manage_tables", true) 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
|
45 return; |
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
|
46 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
|
47 |
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
|
48 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; |
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
|
49 if params.driver == "PostgreSQL" 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
|
50 create_sql = create_sql:gsub("`", "\""); |
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
|
51 elseif params.driver == "MySQL" 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
|
52 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT") |
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
|
53 :gsub(";$", " CHARACTER SET 'utf8' COLLATE 'utf8_bin';"); |
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
|
54 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
|
55 |
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
|
56 local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`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
|
57 if params.driver == "PostgreSQL" 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
|
58 index_sql = index_sql:gsub("`", "\""); |
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
|
59 elseif params.driver == "MySQL" 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
|
60 index_sql = index_sql:gsub("`([,)])", "`(20)%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
|
61 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
|
62 |
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
|
63 local success,err = 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
|
64 engine:execute(create_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
|
65 engine:execute(index_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
|
66 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
|
67 if not success then -- so we failed to create |
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
|
68 if params.driver == "MySQL" 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
|
69 success,err = 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
|
70 local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); |
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
|
71 if result:rowcount() > 0 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
|
72 module:log("info", "Upgrading database schema..."); |
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
|
73 engine:execute("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT"); |
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
|
74 module:log("info", "Database table automatically upgraded"); |
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
|
75 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
|
76 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
|
77 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
|
78 if not success 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
|
79 module:log("error", "Failed to check/upgrade database schema (%s), please see " |
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
|
80 .."http://prosody.im/doc/mysql for help", |
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
|
81 err or "unknown error"); |
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
|
82 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
|
83 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
|
84 end |
5740
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
85 local ProsodyArchiveTable = Table { |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
86 name="prosodyarchive"; |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
87 Column { name="sort_id", type="INTEGER PRIMARY KEY AUTOINCREMENT", nullable=false }; |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 Column { name="type", 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
|
95 Column { name="value", type=params.driver == "MySQL" and "MEDIUMTEXT" or "TEXT", nullable=false }; |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
96 Index { name="prosodyarchive_index", "host", "user", "store", "key" }; |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
97 }; |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
98 engine:transaction(function() |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
99 ProsodyArchiveTable:create(engine); |
6fce00f61acf
mod_storage_sql2: Create an additional table `prosodyarchive` for chronological collections
Kim Alvefur <zash@zash.se>
parents:
5735
diff
changeset
|
100 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
|
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 local function set_encoding() |
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 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
|
104 local set_names_query = "SET NAMES 'utf8';"; |
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 if params.driver == "MySQL" 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
|
106 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';"); |
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 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
|
108 local success,err = engine:transaction(function() return engine:execute(set_names_query); 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
|
109 if not success 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
|
110 module:log("error", "Failed to set database connection encoding to UTF8: %s", 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
|
111 return; |
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 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
|
113 if params.driver == "MySQL" 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
|
114 -- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already |
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
|
115 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' );"; |
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
|
116 local success,err = 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
|
117 local result = engine:execute(check_encoding_query); |
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
|
118 local n_bad_columns = result:rowcount(); |
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
|
119 if n_bad_columns > 0 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
|
120 module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns); |
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
|
121 local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;"; |
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 local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';"; |
5711
254a9420e53d
mod_storage_sql2: Use correct variable (Thanks SkyBlue and Florob)
Kim Alvefur <zash@zash.se>
parents:
5494
diff
changeset
|
123 for row in result:rows() 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
|
124 local column_name, column_type = unpack(row); |
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 engine:execute(fix_column_query1:format(column_name, column_name)); |
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 engine:execute(fix_column_query2:format(column_name, column_name, column_type)); |
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 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
|
128 module:log("info", "Database encoding upgrade complete!"); |
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
|
129 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
|
130 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
|
131 local success,err = engine:transaction(function() return engine:execute(check_encoding_query); 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
|
132 if not success 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
|
133 module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error"); |
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 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
|
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 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
|
138 |
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 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
|
140 params = params or { driver = "SQLite3" }; |
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 |
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 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
|
143 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
|
144 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
|
145 |
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
|
146 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
|
147 |
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 --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
|
149 engine = mod_sql:create_engine(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
|
150 |
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 -- Encoding mess |
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 set_encoding(); |
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 |
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 -- Automatically create table, ignore failure (table probably already exists) |
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 create_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
|
156 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
|
157 |
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 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
|
159 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
|
160 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
|
161 return t, tostring(value); |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
162 elseif is_stanza(value) then |
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
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 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 return json.decode(value); |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
179 elseif t == "xml" then |
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
180 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
|
181 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
|
182 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
|
183 |
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 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
|
185 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
|
186 |
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 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
|
188 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
|
189 local result = {}; |
5732
4aa1d6f5083a
mod_storage_sql2: Make sure the user field is not NULL
Kim Alvefur <zash@zash.se>
parents:
5711
diff
changeset
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?", host, user or "", store); |
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
|
208 |
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 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 |
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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 return select(2, engine:transaction(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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 function keyval_store:users() |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
242 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
|
243 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
|
244 end); |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
245 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
|
246 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
|
247 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
|
248 |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
249 local archive_store = {} |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
250 archive_store.__index = archive_store |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
251 function archive_store:append(username, when, with, value) |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
252 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
|
253 return engine:transaction(function() |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
254 local key = uuid.generate(); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
255 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
|
256 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
|
257 return key; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
258 end); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
259 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
260 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
|
261 query = query or {}; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
262 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
|
263 local total; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 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
|
268 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
269 -- Time range, inclusive |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
270 if query.start then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
271 args[#args+1] = query.start |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
272 where[#where+1] = "`when` >= ?" |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
273 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
274 if query["end"] then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
275 args[#args+1] = query["end"]; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
276 if query.start then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
277 where[#where] = "`when` BETWEEN ? AND ?" -- is this inclusive? |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
278 else |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
279 where[#where+1] = "`when` >= ?" |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
280 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
281 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
282 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
283 -- Related name |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
284 if query.with then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
285 where[#where+1] = "`with` = ?"; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
286 args[#args+1] = query.with |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
287 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
288 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
289 -- Unique id |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
290 if query.key then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
291 where[#where+1] = "`key` = ?"; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
292 args[#args+1] = query.key |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
293 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
294 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
295 -- Total matching |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
296 if query.total then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
297 local stats = engine:select(sql_query:gsub("^(SELECT).-(FROM)", "%1 COUNT(*) %2"):format(t_concat(where, " AND "), "DESC", ""), unpack(args)); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
298 if stats then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
299 local _total = stats() |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
300 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
|
301 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
302 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
|
303 return noop, total; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
304 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
305 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
306 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
307 -- Before or after specific item, exclusive |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
308 if query.after then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
309 where[#where+1] = "`sort_id` > (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? LIMIT 1)" |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
310 args[#args+1] = query.after |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
311 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
312 if query.before then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
313 where[#where+1] = "`sort_id` < (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? LIMIT 1)" |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
314 args[#args+1] = query.before |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
315 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
316 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
317 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
|
318 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
|
319 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
320 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
321 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
|
322 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
|
323 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
|
324 end); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
325 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
|
326 return function() |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
327 local row = result(); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
328 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
|
329 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
|
330 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
331 end, 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 |
5734
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
334 local stores = { |
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
335 keyval = keyval_store; |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
336 archive = archive_store; |
5734
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
337 }; |
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
338 |
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
|
339 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
|
340 |
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
|
341 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
|
342 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
|
343 if store_mt then |
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
344 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
|
345 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
|
346 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
|
347 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
|
348 |
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
|
349 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
|
350 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
|
351 (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
|
352 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
|
353 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
|
354 end |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
355 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
|
356 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
|
357 end); |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
358 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
|
359 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
|
360 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
|
361 |
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
|
362 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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 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
|
368 |
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
|
369 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
|
370 |
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
|
371 |