Comparison

util/sql.lua @ 12872:a20923f7d5fd

mod_storage_sql: Record connection to database as module status Allows retrieving this in e.g. a health reporting module Thanks pfak
author Kim Alvefur <zash@zash.se>
date Mon, 30 Jan 2023 00:38:26 +0100
parent 10534:8a42fd6702e6
child 12975:d10957394a3c
comparison
equal deleted inserted replaced
12871:885323e2a1ce 12872:a20923f7d5fd
97 return true; 97 return true;
98 end 98 end
99 function engine:onconnect() -- luacheck: ignore 212/self 99 function engine:onconnect() -- luacheck: ignore 212/self
100 -- Override from create_engine() 100 -- Override from create_engine()
101 end 101 end
102 function engine:ondisconnect() -- luacheck: ignore 212/self
103 -- Override from create_engine()
104 end
102 105
103 function engine:prepquery(sql) 106 function engine:prepquery(sql)
104 if self.params.driver == "MySQL" then 107 if self.params.driver == "MySQL" then
105 sql = sql:gsub("\"", "`"); 108 sql = sql:gsub("\"", "`");
106 end 109 end
222 if not ok then 225 if not ok then
223 local conn = self.conn; 226 local conn = self.conn;
224 if not conn or not conn:ping() then 227 if not conn or not conn:ping() then
225 log("debug", "Database connection was closed. Will reconnect and retry."); 228 log("debug", "Database connection was closed. Will reconnect and retry.");
226 self.conn = nil; 229 self.conn = nil;
230 self:ondisconnect();
227 log("debug", "Retrying SQL transaction [%s]", (...)); 231 log("debug", "Retrying SQL transaction [%s]", (...));
228 ok, ret, b, c = self:_transaction(...); 232 ok, ret, b, c = self:_transaction(...);
229 log("debug", "SQL transaction retry %s", ok and "succeeded" or "failed"); 233 log("debug", "SQL transaction retry %s", ok and "succeeded" or "failed");
230 else 234 else
231 log("debug", "SQL connection is up, so not retrying"); 235 log("debug", "SQL connection is up, so not retrying");
363 port = params.port, 367 port = params.port,
364 path = params.database, 368 path = params.database,
365 }; 369 };
366 end 370 end
367 371
368 local function create_engine(_, params, onconnect) 372 local function create_engine(_, params, onconnect, ondisconnect)
369 return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt); 373 return setmetatable({ url = db2uri(params); params = params; onconnect = onconnect; ondisconnect = ondisconnect }, engine_mt);
370 end 374 end
371 375
372 return { 376 return {
373 is_column = is_column; 377 is_column = is_column;
374 is_index = is_index; 378 is_index = is_index;