Comparison

util/sqlite3.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 12848:ccb030d988ac
child 12975:d10957394a3c
comparison
equal deleted inserted replaced
12871:885323e2a1ce 12872:a20923f7d5fd
157 return true; 157 return true;
158 end 158 end
159 function engine:onconnect() 159 function engine:onconnect()
160 -- Override from create_engine() 160 -- Override from create_engine()
161 end 161 end
162 function engine:ondisconnect() -- luacheck: ignore 212/self
163 -- Override from create_engine()
164 end
162 function engine:execute(sql, ...) 165 function engine:execute(sql, ...)
163 local success, err = self:connect(); 166 local success, err = self:connect();
164 if not success then return success, err; end 167 if not success then return success, err; end
165 local prepared = self.prepared; 168 local prepared = self.prepared;
166 169
320 local ok, ret = self:_transaction(...); 323 local ok, ret = self:_transaction(...);
321 if not ok then 324 if not ok then
322 local conn = self.conn; 325 local conn = self.conn;
323 if not conn or not conn:isopen() then 326 if not conn or not conn:isopen() then
324 self.conn = nil; 327 self.conn = nil;
328 self:ondisconnect();
325 ok, ret = self:_transaction(...); 329 ok, ret = self:_transaction(...);
326 end 330 end
327 end 331 end
328 return ok, ret; 332 return ok, ret;
329 end 333 end
387 port = params.port, 391 port = params.port,
388 path = params.database, 392 path = params.database,
389 }; 393 };
390 end 394 end
391 395
392 local function create_engine(_, params, onconnect) 396 local function create_engine(_, params, onconnect, ondisconnect)
393 assert(params.driver == "SQLite3", "Only SQLite3 is supported without LuaDBI"); 397 assert(params.driver == "SQLite3", "Only SQLite3 is supported without LuaDBI");
394 return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt); 398 return setmetatable({ url = db2uri(params); params = params; onconnect = onconnect; ondisconnect = ondisconnect }, engine_mt);
395 end 399 end
396 400
397 return { 401 return {
398 is_column = is_column; 402 is_column = is_column;
399 is_index = is_index; 403 is_index = is_index;