Software /
code /
prosody
Annotate
plugins/mod_storage_sql2.lua @ 6955:c9cbe3cbe498
Merge 0.10->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 03 Dec 2015 15:43:02 +0000 |
parent | 6767:d01c29b62b16 |
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"; |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
3 local sql = require "util.sql"; |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
4 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
|
5 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
|
6 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
|
7 |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
8 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
|
9 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
|
10 local t_concat = table.concat; |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
11 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
|
12 |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
13 local noop = function() end |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
14 local unpack = unpack |
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
15 local function iterator(result) |
6754
c72b00dade25
mod_storage_sql2: Rename variable to avoid name clash
Matthew Wild <mwild1@gmail.com>
parents:
6746
diff
changeset
|
16 return function(result_) |
c72b00dade25
mod_storage_sql2: Rename variable to avoid name clash
Matthew Wild <mwild1@gmail.com>
parents:
6746
diff
changeset
|
17 local row = result_(); |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
18 if row ~= nil then |
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
19 return unpack(row); |
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
20 end |
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
21 end, result, nil; |
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
22 end |
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
23 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
24 local default_params = { driver = "SQLite3" }; |
5883
39b187e7e892
mod_storage_sql2, util.sql: Move code for setting encoding to util.sql
Kim Alvefur <zash@zash.se>
parents:
5882
diff
changeset
|
25 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
26 local engine; |
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
|
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 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
|
29 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
|
30 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
|
31 return t, tostring(value); |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
32 elseif is_stanza(value) then |
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 return json.decode(value); |
5735
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
49 elseif t == "xml" then |
24f4e73645fe
mod_storage_sql2: Support XML serialization for stanzas.
Kim Alvefur <zash@zash.se>
parents:
5734
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 |
9916f0a2d178
mod_storage_sql2 (temporary name), sql.lib, util.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 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
|
55 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
|
56 |
9916f0a2d178
mod_storage_sql2 (temporary name), sql.lib, util.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 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
|
58 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
|
59 local result = {}; |
5732
4aa1d6f5083a
mod_storage_sql2: Make sure the user field is not NULL
Kim Alvefur <zash@zash.se>
parents:
5711
diff
changeset
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
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 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
|
77 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
|
78 |
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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 |
6736
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
99 --- Key/value store API (default store type) |
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
100 |
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 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
|
102 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
|
103 function keyval_store:get(username) |
6737
9f932a31eeba
mod_storage_sql2: Some reformatting and variable name improvements
Matthew Wild <mwild1@gmail.com>
parents:
6736
diff
changeset
|
104 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
|
105 local ok, result = engine:transaction(keyval_store_get); |
6738
845bc5ba306d
mod_storage_sql2: Improve logging when database read fails
Matthew Wild <mwild1@gmail.com>
parents:
6737
diff
changeset
|
106 if not ok then |
845bc5ba306d
mod_storage_sql2: Improve logging when database read fails
Matthew Wild <mwild1@gmail.com>
parents:
6737
diff
changeset
|
107 module:log("error", "Unable to read from database %s store for %s: %s", store, username or "<host>", result); |
845bc5ba306d
mod_storage_sql2: Improve logging when database read fails
Matthew Wild <mwild1@gmail.com>
parents:
6737
diff
changeset
|
108 return nil, result; |
845bc5ba306d
mod_storage_sql2: Improve logging when database read fails
Matthew Wild <mwild1@gmail.com>
parents:
6737
diff
changeset
|
109 end |
6282
bce801e40484
mod_storage_sql2: Don't ignore failure in keyval_store:get() (thanks daurnimator)
Kim Alvefur <zash@zash.se>
parents:
6165
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 function keyval_store:users() |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
119 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
|
120 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
|
121 end); |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
122 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
|
123 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
|
124 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
|
125 |
6736
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
126 --- Archive store API |
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
127 |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
128 local map_store = {}; |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
129 map_store.__index = map_store; |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
130 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
|
131 local ok, result = engine:transaction(function() |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
132 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
|
133 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
|
134 return deserialize(row[1], row[2]); |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
135 end |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
136 else |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
137 error("TODO: non-string keys"); |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
138 end |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
139 end); |
6335
eaf6e7986934
plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents:
6331
diff
changeset
|
140 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
|
141 return result; |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
142 end |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 if data ~= nil then |
0cee68dd35f8
mod_storage_sql2: DELETE then INSERT in map stores
Kim Alvefur <zash@zash.se>
parents:
6335
diff
changeset
|
149 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
|
150 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
|
151 end |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
152 else |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
153 error("TODO: non-string keys"); |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
154 end |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
155 return true; |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
156 end); |
6335
eaf6e7986934
plugins/mod_storage_sql2: Return correct arguments from map_store operations
daurnimator <quae@daurnimator.com>
parents:
6331
diff
changeset
|
157 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
|
158 return result; |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
159 end |
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
160 |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
161 local archive_store = {} |
6726
e6e80ec50030
mod_storage_sql2: Add a 'caps' table for indicating support for optional features
Kim Alvefur <zash@zash.se>
parents:
6725
diff
changeset
|
162 archive_store.caps = { |
e6e80ec50030
mod_storage_sql2: Add a 'caps' table for indicating support for optional features
Kim Alvefur <zash@zash.se>
parents:
6725
diff
changeset
|
163 total = true; |
e6e80ec50030
mod_storage_sql2: Add a 'caps' table for indicating support for optional features
Kim Alvefur <zash@zash.se>
parents:
6725
diff
changeset
|
164 }; |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
165 archive_store.__index = archive_store |
6725
41725f3df3cc
mod_storage_sql2: Change order of arguments to :append to be the same as return values from :find iterator
Kim Alvefur <zash@zash.se>
parents:
6724
diff
changeset
|
166 function archive_store:append(username, key, value, when, with) |
41725f3df3cc
mod_storage_sql2: Change order of arguments to :append to be the same as return values from :find iterator
Kim Alvefur <zash@zash.se>
parents:
6724
diff
changeset
|
167 if type(when) ~= "number" then |
6728
db28b8639737
mod_storage_sql2: Fix argument compat thing from 41725f3df3cc, it was backwards
Kim Alvefur <zash@zash.se>
parents:
6726
diff
changeset
|
168 when, with, value = value, when, with; |
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
|
169 end |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 end |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
177 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
|
178 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
|
179 return key; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
180 end); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
181 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
|
182 |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
183 -- 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
|
184 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
|
185 -- 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
|
186 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
|
187 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
|
188 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
|
189 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
190 |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
191 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
|
192 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
|
193 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
|
194 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
|
195 else |
5903
0e0aab930e10
mod_storage_sql2: Fix backwards comparison of timestamp
Kim Alvefur <zash@zash.se>
parents:
5894
diff
changeset
|
196 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
|
197 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
198 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
199 |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
200 -- 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
|
201 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
|
202 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
|
203 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
|
204 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
205 |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
206 -- 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
|
207 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
|
208 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
|
209 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
|
210 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
211 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
212 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
|
213 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
|
214 -- 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
220 if query.before then |
5985
98ed9fe368ac
mod_storage_sql2: Fix SQL syntax
Kim Alvefur <zash@zash.se>
parents:
5979
diff
changeset
|
221 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
|
222 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
|
223 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
224 end |
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
225 |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
226 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
|
227 query = query or {}; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
228 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
|
229 local total; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
230 local ok, result = engine:transaction(function() |
6724
a8dbfa14e1a8
mod_storage_sql2: Include 'with' field from iterator like some 3rd party archive-capable storage modules
Kim Alvefur <zash@zash.se>
parents:
6532
diff
changeset
|
231 local sql_query = "SELECT `key`, `type`, `value`, `when`, `with` FROM `prosodyarchive` WHERE %s ORDER BY `sort_id` %s%s;"; |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
232 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
|
233 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
|
234 |
5847
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
235 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
|
236 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
237 -- Total matching |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
238 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
|
239 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
|
240 if stats then |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
241 local _total = stats() |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
242 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
|
243 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
244 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
|
245 return noop, total; |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
246 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
247 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
248 |
5847
6aaa7ad4463c
mod_storage_sql2: Split out code for building WHERE clauses into separate functions
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
249 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
|
250 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
251 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
|
252 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
|
253 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
254 |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 end); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
259 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
|
260 return function() |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
261 local row = result(); |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
262 if row ~= nil then |
6724
a8dbfa14e1a8
mod_storage_sql2: Include 'with' field from iterator like some 3rd party archive-capable storage modules
Kim Alvefur <zash@zash.se>
parents:
6532
diff
changeset
|
263 return row[1], deserialize(row[2], row[3]), row[4], row[5]; |
5741
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
264 end |
c7a664e496b3
mod_storage_sql2: Add archive store with append and find methods
Kim Alvefur <zash@zash.se>
parents:
5740
diff
changeset
|
265 end, total; |
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 |
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
|
268 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 |
5734
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
287 local stores = { |
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
288 keyval = keyval_store; |
6331
fc5113a4540e
plugins/mod_storage_sql2: Add map store support
daurnimator <quae@daurnimator.com>
parents:
6282
diff
changeset
|
289 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
|
290 archive = archive_store; |
5734
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
291 }; |
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
292 |
6736
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
293 --- Implement storage driver API |
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
294 |
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
295 -- FIXME: Some of these operations need to operate on the archive store(s) too |
4aee55c0cc5c
mod_storage_sql2: Add some comments
Matthew Wild <mwild1@gmail.com>
parents:
6532
diff
changeset
|
296 |
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
|
297 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
|
298 |
9916f0a2d178
mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 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
|
300 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
|
301 if store_mt then |
49f1fed6e25e
mod_storage_sql2: Keep available store types in a table
Kim Alvefur <zash@zash.se>
parents:
5733
diff
changeset
|
302 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
|
303 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
|
304 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
|
305 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
|
306 |
9916f0a2d178
mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 function driver:stores(username) |
6737
9f932a31eeba
mod_storage_sql2: Some reformatting and variable name improvements
Matthew Wild <mwild1@gmail.com>
parents:
6736
diff
changeset
|
308 local query = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" .. |
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
|
309 (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
|
310 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
|
311 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
|
312 end |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
313 local ok, result = engine:transaction(function() |
6737
9f932a31eeba
mod_storage_sql2: Some reformatting and variable name improvements
Matthew Wild <mwild1@gmail.com>
parents:
6736
diff
changeset
|
314 return engine:select(query, host, username); |
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
|
315 end); |
5733
aeeced7b0149
mod_storage_sql2: Fix iteration over users and stores
Kim Alvefur <zash@zash.se>
parents:
5732
diff
changeset
|
316 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
|
317 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
|
318 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
|
319 |
9916f0a2d178
mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
320 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
|
321 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
|
322 local stmt,err = engine:delete("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username); |
6737
9f932a31eeba
mod_storage_sql2: Some reformatting and variable name improvements
Matthew Wild <mwild1@gmail.com>
parents:
6736
diff
changeset
|
323 return true, err; |
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
|
324 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
|
325 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
|
326 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
327 --- Initialization |
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
|
328 |
9916f0a2d178
mod_storage_sql2 (temporary name), sql.lib, util.sql: New SQL API supporting cross-module connection sharing, transactions and Things - a work in progress
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
329 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
330 local function create_table(name) |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
331 local Table, Column, Index = sql.Table, sql.Column, sql.Index; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
332 |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
333 local ProsodyTable = Table { |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
334 name= name or "prosody"; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
335 Column { name="host", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
336 Column { name="user", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
337 Column { name="store", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
338 Column { name="key", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
339 Column { name="type", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
340 Column { name="value", type="MEDIUMTEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
341 Index { name="prosody_index", "host", "user", "store", "key" }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
342 }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
343 engine:transaction(function() |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
344 ProsodyTable:create(engine); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
345 end); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
346 |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
347 local ProsodyArchiveTable = Table { |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
348 name="prosodyarchive"; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
349 Column { name="sort_id", type="INTEGER", primary_key=true, auto_increment=true }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
350 Column { name="host", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
351 Column { name="user", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
352 Column { name="store", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
353 Column { name="key", type="TEXT", nullable=false }; -- item id |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
354 Column { name="when", type="INTEGER", nullable=false }; -- timestamp |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
355 Column { name="with", type="TEXT", nullable=false }; -- related id |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
356 Column { name="type", type="TEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
357 Column { name="value", type="MEDIUMTEXT", nullable=false }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
358 Index { name="prosodyarchive_index", unique = true, "host", "user", "store", "key" }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
359 }; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
360 engine:transaction(function() |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
361 ProsodyArchiveTable:create(engine); |
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
|
362 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
|
363 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
|
364 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
365 local function upgrade_table(params, apply_changes) |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
366 local changes = false; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
367 if params.driver == "MySQL" then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
368 local success,err = engine:transaction(function() |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
369 local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
370 if result:rowcount() > 0 then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
371 changes = true; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
372 if apply_changes then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
373 module:log("info", "Upgrading database schema..."); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
374 engine:execute("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
375 module:log("info", "Database table automatically upgraded"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
376 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
377 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
378 return true; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
379 end); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
380 if not success then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
381 module:log("error", "Failed to check/upgrade database schema (%s), please see " |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
382 .."http://prosody.im/doc/mysql for help", |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
383 err or "unknown error"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
384 return false; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
385 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
|
386 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
387 -- COMPAT w/pre-0.10: Upgrade table to UTF-8 if not already |
6761
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
388 local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE`,`TABLE_NAME` FROM `information_schema`.`columns` WHERE `TABLE_NAME` LIKE 'prosody%%' AND ( `CHARACTER_SET_NAME`!='%s' OR `COLLATION_NAME`!='%s_bin' );"; |
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
389 check_encoding_query = check_encoding_query:format(engine.charset, engine.charset); |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
390 success,err = engine:transaction(function() |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
391 local result = engine:execute(check_encoding_query); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
392 local n_bad_columns = result:rowcount(); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
393 if n_bad_columns > 0 then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
394 changes = true; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
395 if apply_changes then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
396 module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns); |
6761
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
397 local fix_column_query1 = "ALTER TABLE `%s` CHANGE `%s` `%s` BLOB;"; |
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
398 local fix_column_query2 = "ALTER TABLE `%s` CHANGE `%s` `%s` %s CHARACTER SET '%s' COLLATE '%s_bin';"; |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
399 for row in result:rows() do |
6761
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
400 local column_name, column_type, table_name = unpack(row); |
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
401 module:log("debug", "Fixing column %s in table %s", column_name, table_name); |
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
402 engine:execute(fix_column_query1:format(table_name, column_name, column_name)); |
b20efae224c9
mod_storage_sql2: Upgrade table if its charset does not match our connection's charset (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
6757
diff
changeset
|
403 engine:execute(fix_column_query2:format(table_name, column_name, column_name, column_type, engine.charset, engine.charset)); |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
404 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
405 module:log("info", "Database encoding upgrade complete!"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
406 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
407 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
408 end); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
409 success,err = engine:transaction(function() return engine:execute(check_encoding_query); end); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
410 if not success then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
411 module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
412 return false; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
413 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
414 end |
6755
b80202ad47e8
mod_storage_sql2: Add missing return to upgrade_table(), so we know if an upgrade is required
Matthew Wild <mwild1@gmail.com>
parents:
6754
diff
changeset
|
415 return changes; |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
416 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
|
417 |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
418 local function normalize_params(params) |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
419 if params.driver == "SQLite3" then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
420 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
421 end |
6746
5e8a8319699d
mod_storage_sql2: Validate configuration after normalizing SQLite3 database path (fixes traceback with default config)
Kim Alvefur <zash@zash.se>
parents:
6740
diff
changeset
|
422 assert(params.driver and params.database, "Configuration error: Both the SQL driver and the database need to be specified"); |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
423 return params; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
424 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
425 |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
426 function module.load() |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
427 if prosody.prosodyctl then return; end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
428 local params = normalize_params(module:get_option("sql", default_params)); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
429 engine = sql:create_engine(params, function (engine) |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
430 if module:get_option("sql_manage_tables", true) then |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
431 -- Automatically create table, ignore failure (table probably already exists) |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
432 -- FIXME: we should check in information_schema, etc. |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
433 create_table(); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
434 -- Check whether the table needs upgrading |
6757
fc9c1a566a19
mod_storage_sql2: Don't upgrade tables without admin intervention
Matthew Wild <mwild1@gmail.com>
parents:
6756
diff
changeset
|
435 if upgrade_table(params, false) then |
fc9c1a566a19
mod_storage_sql2: Don't upgrade tables without admin intervention
Matthew Wild <mwild1@gmail.com>
parents:
6756
diff
changeset
|
436 module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name); |
6739
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
437 return false, "database upgrade needed"; |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
438 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
439 end |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
440 end); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
441 |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
442 module:provides("storage", driver); |
6216743c188c
mod_storage_sql2: Break up monolithic code into functions, theoretically no functionality changes.
Matthew Wild <mwild1@gmail.com>
parents:
6738
diff
changeset
|
443 end |
6756
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
444 |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
445 function module.command(arg) |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
446 local config = require "core.configmanager"; |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
447 local prosodyctl = require "util.prosodyctl"; |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
448 local command = table.remove(arg, 1); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
449 if command == "upgrade" then |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
450 -- We need to find every unique dburi in the config |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
451 local uris = {}; |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
452 for host in pairs(prosody.hosts) do |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
453 local params = config.get(host, "sql") or default_params; |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
454 uris[sql.db2uri(params)] = params; |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
455 end |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
456 print("We will check and upgrade the following databases:\n"); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
457 for _, params in pairs(uris) do |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
458 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or "")); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
459 end |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
460 print(""); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
461 print("Ensure you have working backups of the above databases before continuing! "); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
462 if not prosodyctl.show_yesno("Continue with the database upgrade? [yN]") then |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
463 print("Ok, no upgrade. But you do have backups, don't you? ...don't you?? :-)"); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
464 return; |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
465 end |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
466 -- Upgrade each one |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
467 for _, params in pairs(uris) do |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
468 print("Checking "..params.database.."..."); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
469 engine = sql:create_engine(params); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
470 upgrade_table(params, true); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
471 end |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
472 print("All done!"); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
473 else |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
474 print("Unknown command: "..command); |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
475 end |
2e75b94e3a1e
mod_storage_sql2: Add prosodyctl command to upgrade tables from the command-line
Matthew Wild <mwild1@gmail.com>
parents:
6755
diff
changeset
|
476 end |