Software /
code /
prosody
Annotate
plugins/mod_storage_sql.lua @ 4007:062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 05 Jan 2011 06:56:36 +0500 |
parent | 4004:c1b3ecbed6c0 |
child | 4096:3b991ceb228e |
rev | line source |
---|---|
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 --[[ |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 DB Tables: |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 Prosody - key-value, map |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
6 | host | user | store | key | type | value | |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 ProsodyArchive - list |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 | host | user | store | key | time | stanzatype | jsonvalue | |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 Mapping: |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 Roster - Prosody |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
12 | host | user | "roster" | "contactjid" | type | value | |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
13 | host | user | "roster" | NULL | "json" | roster[false] data | |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 Account - Prosody |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
15 | host | user | "accounts" | "username" | type | value | |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 Offline - ProsodyArchive |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 | host | user | "offline" | "contactjid" | time | "message" | json|XML | |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 ]] |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local type = type; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 local tostring = tostring; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local tonumber = tonumber; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local pairs = pairs; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local next = next; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 local setmetatable = setmetatable; |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
28 local xpcall = xpcall; |
3980
6b2fac6602b3
mod_storage_sql: Use util.json instead of util.serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
3978
diff
changeset
|
29 local json = require "util.json"; |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
4004
c1b3ecbed6c0
mod_storage_sql: Removed unnecessary initialization of a variable.
Waqas Hussain <waqas20@gmail.com>
parents:
3980
diff
changeset
|
31 local connection; |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 local host,user,store = module.host; |
3976
16170a66e140
mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
Waqas Hussain <waqas20@gmail.com>
parents:
3975
diff
changeset
|
33 local params = module:get_option("sql"); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 do -- process options to get a db connection |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 local DBI = require "DBI"; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 |
3976
16170a66e140
mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
Waqas Hussain <waqas20@gmail.com>
parents:
3975
diff
changeset
|
38 params = params or { driver = "SQLite3", database = "prosody.sqlite" }; |
16170a66e140
mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
Waqas Hussain <waqas20@gmail.com>
parents:
3975
diff
changeset
|
39 assert(params.driver and params.database, "invalid params"); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 prosody.unlock_globals(); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 local dbh, err = DBI.Connect( |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 params.driver, params.database, |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 params.username, params.password, |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 params.host, params.port |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 ); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 prosody.lock_globals(); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 assert(dbh, err); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 dbh:autocommit(false); -- don't commit automatically |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 connection = dbh; |
3732
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
52 |
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
53 if params.driver == "SQLite3" then -- auto initialize |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
54 local stmt = assert(connection:prepare("SELECT COUNT(*) FROM `sqlite_master` WHERE `type`='table' AND `name`='Prosody';")); |
3732
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
55 local ok = assert(stmt:execute()); |
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
56 local count = stmt:fetch()[1]; |
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
57 if count == 0 then |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
58 local stmt = assert(connection:prepare("CREATE TABLE `Prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);")); |
3732
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
59 assert(stmt:execute()); |
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
60 module:log("debug", "Initialized new SQLite3 database"); |
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
61 end |
3975
aa5e93e61760
mod_storage_sql: Call commit() after all SQL statements, including SELECT, to get SQLite to drop its locks.
Waqas Hussain <waqas20@gmail.com>
parents:
3974
diff
changeset
|
62 assert(connection:commit()); |
3980
6b2fac6602b3
mod_storage_sql: Use util.json instead of util.serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
3978
diff
changeset
|
63 --print("===", json.encode()) |
3732
bf449ecf2144
mod_storage_sql: Auto-initialize SQLite3 database.
Waqas Hussain <waqas20@gmail.com>
parents:
3731
diff
changeset
|
64 end |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 local function serialize(value) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 local t = type(value); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 if t == "string" or t == "boolean" or t == "number" then |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 return t, tostring(value); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 elseif t == "table" then |
3980
6b2fac6602b3
mod_storage_sql: Use util.json instead of util.serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
3978
diff
changeset
|
72 local value,err = json.encode(value); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 if value then return "json", value; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 return nil, err; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 return nil, "Unhandled value type: "..t; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 local function deserialize(t, value) |
3743
5adfb8d0444d
mod_storage_sql: Fixed the deserialization of string-typed values.
Waqas Hussain <waqas20@gmail.com>
parents:
3732
diff
changeset
|
79 if t == "string" then return value; |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 elseif t == "boolean" then |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 if value == "true" then return true; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 elseif value == "false" then return false; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 elseif t == "number" then return tonumber(value); |
3772
e1f6fe098404
mod_storage_sql: Fix a couple of bugs in "JSON" decoding
Matthew Wild <mwild1@gmail.com>
parents:
3744
diff
changeset
|
84 elseif t == "json" then |
3980
6b2fac6602b3
mod_storage_sql: Use util.json instead of util.serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
3978
diff
changeset
|
85 return json.decode(value); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 local function getsql(sql, ...) |
3976
16170a66e140
mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
Waqas Hussain <waqas20@gmail.com>
parents:
3975
diff
changeset
|
90 if params.driver == "PostgreSQL" then |
16170a66e140
mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
Waqas Hussain <waqas20@gmail.com>
parents:
3975
diff
changeset
|
91 sql = sql:gsub("`", "\""); |
16170a66e140
mod_storage_sql: Dynamically replace backquotes with double quotes when connecting to PostgreSQL...
Waqas Hussain <waqas20@gmail.com>
parents:
3975
diff
changeset
|
92 end |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 -- do prepared statement stuff |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 local stmt, err = connection:prepare(sql); |
3978
13ee740b1f89
mod_storage_sql: Log an error on query failure.
Waqas Hussain <waqas20@gmail.com>
parents:
3977
diff
changeset
|
95 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 -- run query |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
97 local ok, err = stmt:execute(host or "", user or "", store or "", ...); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 if not ok then return nil, err; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 return stmt; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 local function setsql(sql, ...) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 local stmt, err = getsql(sql, ...); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 if not stmt then return stmt, err; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 return stmt:affected(); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 local function transact(...) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 -- ... |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 local function rollback(...) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 connection:rollback(); -- FIXME check for rollback error? |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 return ...; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 local function commit(...) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 if not connection:commit() then return nil, "SQL commit failed"; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 return ...; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
119 local function keyval_store_get() |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
120 local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 if not stmt then return nil, err; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 local haveany; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 local result = {}; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 for row in stmt:rows(true) do |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 haveany = true; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 local k = row.key; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 local v = deserialize(row.type, row.value); |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
129 if k and v then |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
130 if k ~= "" then result[k] = v; elseif type(v) == "table" then |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 for a,b in pairs(v) do |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 result[a] = b; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 end |
3975
aa5e93e61760
mod_storage_sql: Call commit() after all SQL statements, including SELECT, to get SQLite to drop its locks.
Waqas Hussain <waqas20@gmail.com>
parents:
3974
diff
changeset
|
137 return commit(haveany and result or nil); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 end |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
139 local function keyval_store_set(data) |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
140 local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=?"); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 if data and next(data) ~= nil then |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 local extradata = {}; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 for key, value in pairs(data) do |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
145 if type(key) == "string" and key ~= "" then |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 local t, value = serialize(value); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 if not t then return rollback(t, value); end |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
148 local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 if not ok then return rollback(ok, err); end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 else |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 extradata[key] = value; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 if next(extradata) ~= nil then |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 local t, extradata = serialize(extradata); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 if not t then return rollback(t, extradata); end |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
157 local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", "", t, extradata); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 if not ok then return rollback(ok, err); end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 return commit(true); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
164 local keyval_store = {}; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
165 keyval_store.__index = keyval_store; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
166 function keyval_store:get(username) |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 user,store = username,self.store; |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
168 local success, ret, err = xpcall(keyval_store_get, debug.traceback); |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
169 if success then return ret, err; else return rollback(nil, ret); end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
170 end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
171 function keyval_store:set(username, data) |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
172 user,store = username,self.store; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
173 local success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback); |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
174 if success then return ret, err; else return rollback(nil, ret); end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
175 end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
176 |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
177 local function map_store_get(key) |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
178 local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 if not stmt then return nil, err; end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 local haveany; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 local result = {}; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 for row in stmt:rows(true) do |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 haveany = true; |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
185 local k = row.key; |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 local v = deserialize(row.type, row.value); |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
187 if k and v then |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
188 if k ~= "" then result[k] = v; elseif type(v) == "table" then |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 for a,b in pairs(v) do |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 result[a] = b; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 end |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
195 return commit(haveany and result[key] or nil); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 end |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
197 local function map_store_set(key, data) |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
198 local affected, err = setsql("DELETE FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 if data and next(data) ~= nil then |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
201 if type(key) == "string" and key ~= "" then |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
202 local t, value = serialize(data); |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
203 if not t then return rollback(t, value); end |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
204 local ok, err = setsql("INSERT INTO `Prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 if not ok then return rollback(ok, err); end |
3977
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
206 else |
6724853adb80
mod_storage_sql: Remove the subkey column from the Prosody table, and make the map store compatible with the key-value store.
Waqas Hussain <waqas20@gmail.com>
parents:
3976
diff
changeset
|
207 -- TODO non-string keys |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 return commit(true); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 |
4007
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
213 local map_store = {}; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
214 map_store.__index = map_store; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
215 function map_store:get(username, key) |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
216 user,store = username,self.store; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
217 local success, ret, err = xpcall(function() return map_store_get(key); end, debug.traceback); |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
218 if success then return ret, err; else return rollback(nil, ret); end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
219 end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
220 function map_store:set(username, key, data) |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
221 user,store = username,self.store; |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
222 local success, ret, err = xpcall(function() return map_store_set(key, data); end, debug.traceback); |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
223 if success then return ret, err; else return rollback(nil, ret); end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
224 end |
062b849ca088
mod_storage_sql: Catch Lua errors during SQL transactions, and rollback.
Waqas Hussain <waqas20@gmail.com>
parents:
4004
diff
changeset
|
225 |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 local list_store = {}; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 list_store.__index = list_store; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 function list_store:scan(username, from, to, jid, typ) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 user,store = username,self.store; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 local cols = {"from", "to", "jid", "typ"}; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 local vals = { from , to , jid , typ }; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
233 local stmt, err; |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
234 local query = "SELECT * FROM `ProsodyArchive` WHERE `host`=? AND `user`=? AND `store`=?"; |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
235 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
236 query = query.." ORDER BY time"; |
3974
af40a7ce4f77
mod_storage_sql: Quote identifiers in SQL with backquotes, and use the empty string for NULL, and '=' instead of 'IS' for comparison, to work with MySQL's limitations...
Waqas Hussain <waqas20@gmail.com>
parents:
3772
diff
changeset
|
237 --local stmt, err = getsql("SELECT * FROM `Prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or ""); |
3729
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
238 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
239 return nil, "not-implemented" |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
240 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
241 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
242 local driver = { name = "sql" }; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
243 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 function driver:open(store, typ) |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 if not typ then -- default key-value store |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
246 return setmetatable({ store = store }, keyval_store); |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
248 return nil, "unsupported-store"; |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 end |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
250 |
b351fa47a743
mod_storage_sql: Initial commit of new SQL data driver.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
251 module:add_item("data-driver", driver); |