Software / code / prosody
Comparison
util/sql.lua @ 8382:e5d00bf4a4d5
util: Various minor changes to please [luacheck]
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 10 Nov 2017 05:42:32 +0100 |
| parent | 8380:a597ff326758 |
| child | 8389:5d866eb8f18f |
comparison
equal
deleted
inserted
replaced
| 8381:7f6184474149 | 8382:e5d00bf4a4d5 |
|---|---|
| 1 | 1 |
| 2 local setmetatable, getmetatable = setmetatable, getmetatable; | 2 local setmetatable, getmetatable = setmetatable, getmetatable; |
| 3 local ipairs, unpack, select = ipairs, table.unpack or unpack, select; --luacheck: ignore 113 | 3 local ipairs, unpack, select = ipairs, table.unpack or unpack, select; --luacheck: ignore 113 |
| 4 local tonumber, tostring = tonumber, tostring; | 4 local tostring = tostring; |
| 5 local type = type; | 5 local type = type; |
| 6 local assert, pcall, xpcall, debug_traceback = assert, pcall, xpcall, debug.traceback; | 6 local assert, pcall, xpcall, debug_traceback = assert, pcall, xpcall, debug.traceback; |
| 7 local t_concat = table.concat; | 7 local t_concat = table.concat; |
| 8 local s_char = string.char; | |
| 9 local log = require "util.logger".init("sql"); | 8 local log = require "util.logger".init("sql"); |
| 10 | 9 |
| 11 local DBI = require "DBI"; | 10 local DBI = require "DBI"; |
| 12 -- This loads all available drivers while globals are unlocked | 11 -- This loads all available drivers while globals are unlocked |
| 13 -- LuaDBI should be fixed to not set globals. | 12 -- LuaDBI should be fixed to not set globals. |
| 56 end | 55 end |
| 57 table_mt.__index = {}; | 56 table_mt.__index = {}; |
| 58 function table_mt.__index:create(engine) | 57 function table_mt.__index:create(engine) |
| 59 return engine:_create_table(self); | 58 return engine:_create_table(self); |
| 60 end | 59 end |
| 61 function table_mt:__call(...) | |
| 62 -- TODO | |
| 63 end | |
| 64 function column_mt:__tostring() | 60 function column_mt:__tostring() |
| 65 return 'Column{ name="'..self.name..'", type="'..self.type..'" }' | 61 return 'Column{ name="'..self.name..'", type="'..self.type..'" }' |
| 66 end | 62 end |
| 67 function index_mt:__tostring() | 63 function index_mt:__tostring() |
| 68 local s = 'Index{ name="'..self.name..'"'; | 64 local s = 'Index{ name="'..self.name..'"'; |
| 69 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end | 65 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end |
| 70 return s..' }'; | 66 return s..' }'; |
| 71 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }' | 67 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }' |
| 72 end | |
| 73 | |
| 74 local function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return s_char(tonumber(c,16)); end)); end | |
| 75 local function parse_url(url) | |
| 76 local scheme, secondpart, database = url:match("^([%w%+]+)://([^/]*)/?(.*)"); | |
| 77 assert(scheme, "Invalid URL format"); | |
| 78 local username, password, host, port; | |
| 79 local authpart, hostpart = secondpart:match("([^@]+)@([^@+])"); | |
| 80 if not authpart then hostpart = secondpart; end | |
| 81 if authpart then | |
| 82 username, password = authpart:match("([^:]*):(.*)"); | |
| 83 username = username or authpart; | |
| 84 password = password and urldecode(password); | |
| 85 end | |
| 86 if hostpart then | |
| 87 host, port = hostpart:match("([^:]*):(.*)"); | |
| 88 host = host or hostpart; | |
| 89 port = port and assert(tonumber(port), "Invalid URL format"); | |
| 90 end | |
| 91 return { | |
| 92 scheme = scheme:lower(); | |
| 93 username = username; password = password; | |
| 94 host = host; port = port; | |
| 95 database = #database > 0 and database or nil; | |
| 96 }; | |
| 97 end | 68 end |
| 98 | 69 |
| 99 local engine = {}; | 70 local engine = {}; |
| 100 function engine:connect() | 71 function engine:connect() |
| 101 if self.conn then return true; end | 72 if self.conn then return true; end |
| 121 if ok == false then | 92 if ok == false then |
| 122 return ok, err; | 93 return ok, err; |
| 123 end | 94 end |
| 124 return true; | 95 return true; |
| 125 end | 96 end |
| 126 function engine:onconnect() | 97 function engine:onconnect() -- luacheck: ignore 212/self |
| 127 -- Override from create_engine() | 98 -- Override from create_engine() |
| 128 end | 99 end |
| 129 | 100 |
| 130 function engine:prepquery(sql) | 101 function engine:prepquery(sql) |
| 131 if self.params.driver == "MySQL" then | 102 if self.params.driver == "MySQL" then |
| 146 stmt, err = self.conn:prepare(sql); | 117 stmt, err = self.conn:prepare(sql); |
| 147 if not stmt then return stmt, err; end | 118 if not stmt then return stmt, err; end |
| 148 prepared[sql] = stmt; | 119 prepared[sql] = stmt; |
| 149 end | 120 end |
| 150 | 121 |
| 122 -- luacheck: ignore 411/success | |
| 151 local success, err = stmt:execute(...); | 123 local success, err = stmt:execute(...); |
| 152 if not success then return success, err; end | 124 if not success then return success, err; end |
| 153 return stmt; | 125 return stmt; |
| 154 end | 126 end |
| 155 | 127 |
| 333 end | 305 end |
| 334 local set_names_query = "SET NAMES '%s';" | 306 local set_names_query = "SET NAMES '%s';" |
| 335 local charset = "utf8"; | 307 local charset = "utf8"; |
| 336 if driver == "MySQL" then | 308 if driver == "MySQL" then |
| 337 self:transaction(function() | 309 self:transaction(function() |
| 338 for row in self:select"SELECT \"CHARACTER_SET_NAME\" FROM \"information_schema\".\"CHARACTER_SETS\" WHERE \"CHARACTER_SET_NAME\" LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;" do | 310 for row in self:select[[ |
| 311 SELECT "CHARACTER_SET_NAME" | |
| 312 FROM "information_schema"."CHARACTER_SETS" | |
| 313 WHERE "CHARACTER_SET_NAME" LIKE 'utf8%' | |
| 314 ORDER BY MAXLEN DESC LIMIT 1; | |
| 315 ]] do | |
| 339 charset = row and row[1] or charset; | 316 charset = row and row[1] or charset; |
| 340 end | 317 end |
| 341 end); | 318 end); |
| 342 set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin")); | 319 set_names_query = set_names_query:gsub(";$", (" COLLATE '%s';"):format(charset.."_bin")); |
| 343 end | 320 end |
| 377 port = params.port, | 354 port = params.port, |
| 378 path = params.database, | 355 path = params.database, |
| 379 }; | 356 }; |
| 380 end | 357 end |
| 381 | 358 |
| 382 local function create_engine(self, params, onconnect) | 359 local function create_engine(_, params, onconnect) |
| 383 return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt); | 360 return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt); |
| 384 end | 361 end |
| 385 | 362 |
| 386 return { | 363 return { |
| 387 is_column = is_column; | 364 is_column = is_column; |