Software / code / prosody
Comparison
plugins/mod_storage_sql.lua @ 5034:2dbb3bf74090
mod_storage_sql: Split out query handling logic from getsql() into a separate function
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 28 Jul 2012 21:26:33 +0200 |
| parent | 4318:44b131d7041b |
| child | 5035:874cab7b4b3e |
comparison
equal
deleted
inserted
replaced
| 5033:c64b5081bfa8 | 5034:2dbb3bf74090 |
|---|---|
| 173 elseif t == "json" then | 173 elseif t == "json" then |
| 174 return json.decode(value); | 174 return json.decode(value); |
| 175 end | 175 end |
| 176 end | 176 end |
| 177 | 177 |
| 178 local function getsql(sql, ...) | 178 local function dosql(sql, ...) |
| 179 if params.driver == "PostgreSQL" then | 179 if params.driver == "PostgreSQL" then |
| 180 sql = sql:gsub("`", "\""); | 180 sql = sql:gsub("`", "\""); |
| 181 end | 181 end |
| 182 -- do prepared statement stuff | 182 -- do prepared statement stuff |
| 183 local stmt, err = connection:prepare(sql); | 183 local stmt, err = connection:prepare(sql); |
| 184 if not stmt and not test_connection() then error("connection failed"); end | 184 if not stmt and not test_connection() then error("connection failed"); end |
| 185 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end | 185 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end |
| 186 -- run query | 186 -- run query |
| 187 local ok, err = stmt:execute(host or "", user or "", store or "", ...); | 187 local ok, err = stmt:execute(...); |
| 188 if not ok and not test_connection() then error("connection failed"); end | 188 if not ok and not test_connection() then error("connection failed"); end |
| 189 if not ok then return nil, err; end | 189 if not ok then return nil, err; end |
| 190 | 190 |
| 191 return stmt; | 191 return stmt; |
| 192 end | |
| 193 local function getsql(sql, ...) | |
| 194 return dosql(sql, host or "", user or "", store or "", ...); | |
| 192 end | 195 end |
| 193 local function setsql(sql, ...) | 196 local function setsql(sql, ...) |
| 194 local stmt, err = getsql(sql, ...); | 197 local stmt, err = getsql(sql, ...); |
| 195 if not stmt then return stmt, err; end | 198 if not stmt then return stmt, err; end |
| 196 return stmt:affected(); | 199 return stmt:affected(); |