Mercurial > prosody-modules
comparison mod_log_messages_sql/mod_log_messages_sql.lua @ 2407:be08b65f2855
mod_log_messages_sql: In 0.10ish, use 'messages' as default table name, override using 'log_messages_sql_table' if needed
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 02 Dec 2016 19:06:37 +0000 |
parents | 398c4aaccf6d |
children | 9d132153d786 |
comparison
equal
deleted
inserted
replaced
2406:a216be9b1d6e | 2407:be08b65f2855 |
---|---|
9 | 9 |
10 local serialize = require"util.json".encode, require"util.json".decode; | 10 local serialize = require"util.json".encode, require"util.json".decode; |
11 local tostring = tostring; | 11 local tostring = tostring; |
12 local time_now = os.time; | 12 local time_now = os.time; |
13 | 13 |
14 | |
15 local table_name = module:get_option("message_log_sql_table", pcall(require, "util.cache") and "messages" or "prosodyarchive"); | |
16 | |
14 local sql, setsql, getsql = {}; | 17 local sql, setsql, getsql = {}; |
15 do -- SQL stuff | 18 do -- SQL stuff |
16 local connection; | 19 local connection; |
17 local resolve_relative_path = require "core.configmanager".resolve_relative_path; | 20 local resolve_relative_path = require "core.configmanager".resolve_relative_path; |
18 local params = module:get_option("message_log_sql", module:get_option("sql")); | 21 local params = module:get_option("message_log_sql", module:get_option("sql")); |
22 | |
19 | 23 |
20 local function test_connection() | 24 local function test_connection() |
21 if not connection then return nil; end | 25 if not connection then return nil; end |
22 if connection:ping() then | 26 if connection:ping() then |
23 return true; | 27 return true; |
44 connection = dbh; | 48 connection = dbh; |
45 | 49 |
46 end | 50 end |
47 return connection; | 51 return connection; |
48 end | 52 end |
53 | |
49 | 54 |
50 do -- process options to get a db connection | 55 do -- process options to get a db connection |
51 local ok; | 56 local ok; |
52 prosody.unlock_globals(); | 57 prosody.unlock_globals(); |
53 ok, DBI = pcall(require, "DBI"); | 58 ok, DBI = pcall(require, "DBI"); |
136 | 141 |
137 --local id = uuid(); | 142 --local id = uuid(); |
138 local when = time_now(); | 143 local when = time_now(); |
139 -- And stash it | 144 -- And stash it |
140 local ok, err = setsql([[ | 145 local ok, err = setsql([[ |
141 INSERT INTO `prosodyarchive` | 146 INSERT INTO `]]..table_name..[[` |
142 (`host`, `user`, `store`, `when`, `with`, `resource`, `stanza`) | 147 (`host`, `user`, `store`, `when`, `with`, `resource`, `stanza`) |
143 VALUES (?, ?, ?, ?, ?, ?, ?); | 148 VALUES (?, ?, ?, ?, ?, ?, ?); |
144 ]], store_host, store_user, "message_log", when, target_bare, target_resource, serialize(st.preserialize(stanza))) | 149 ]], store_host, store_user, "message_log", when, target_bare, target_resource, serialize(st.preserialize(stanza))) |
145 if ok then | 150 if ok then |
146 sql.commit(); | 151 sql.commit(); |
162 module:hook("message/full", message_handler, 2); | 167 module:hook("message/full", message_handler, 2); |
163 | 168 |
164 -- In the telnet console, run: | 169 -- In the telnet console, run: |
165 -- >hosts["this host"].modules.mam_sql.environment.create_sql() | 170 -- >hosts["this host"].modules.mam_sql.environment.create_sql() |
166 function create_sql() | 171 function create_sql() |
167 local stm = getsql[[ | 172 local stm = getsql([[ |
168 CREATE TABLE `prosodyarchive` ( | 173 CREATE TABLE `]]..table_name..[[` ( |
169 `host` TEXT, | 174 `host` TEXT, |
170 `user` TEXT, | 175 `user` TEXT, |
171 `store` TEXT, | 176 `store` TEXT, |
172 `id` INTEGER PRIMARY KEY AUTOINCREMENT, | 177 `id` INTEGER PRIMARY KEY AUTOINCREMENT, |
173 `when` INTEGER, | 178 `when` INTEGER, |
174 `with` TEXT, | 179 `with` TEXT, |
175 `resource` TEXT, | 180 `resource` TEXT, |
176 `stanza` TEXT | 181 `stanza` TEXT |
177 ); | 182 ); |
178 CREATE INDEX `hus` ON `prosodyarchive` (`host`, `user`, `store`); | 183 CREATE INDEX `hus` ON `]]..table_name..[[` (`host`, `user`, `store`); |
179 CREATE INDEX `with` ON `prosodyarchive` (`with`); | 184 CREATE INDEX `with` ON `]]..table_name..[[` (`with`); |
180 CREATE INDEX `thetime` ON `prosodyarchive` (`when`); | 185 CREATE INDEX `thetime` ON `]]..table_name..[[` (`when`); |
181 ]]; | 186 ]]); |
182 stm:execute(); | 187 stm:execute(); |
183 sql.commit(); | 188 sql.commit(); |
184 end | 189 end |