Diff

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
line wrap: on
line diff
--- a/util/sql.lua	Fri Nov 10 05:34:29 2017 +0100
+++ b/util/sql.lua	Fri Nov 10 05:42:32 2017 +0100
@@ -1,11 +1,10 @@
 
 local setmetatable, getmetatable = setmetatable, getmetatable;
 local ipairs, unpack, select = ipairs, table.unpack or unpack, select; --luacheck: ignore 113
-local tonumber, tostring = tonumber, tostring;
+local tostring = tostring;
 local type = type;
 local assert, pcall, xpcall, debug_traceback = assert, pcall, xpcall, debug.traceback;
 local t_concat = table.concat;
-local s_char = string.char;
 local log = require "util.logger".init("sql");
 
 local DBI = require "DBI";
@@ -58,9 +57,6 @@
 function table_mt.__index:create(engine)
 	return engine:_create_table(self);
 end
-function table_mt:__call(...)
-	-- TODO
-end
 function column_mt:__tostring()
 	return 'Column{ name="'..self.name..'", type="'..self.type..'" }'
 end
@@ -71,31 +67,6 @@
 --	return 'Index{ name="'..self.name..'", type="'..self.type..'" }'
 end
 
-local function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return s_char(tonumber(c,16)); end)); end
-local function parse_url(url)
-	local scheme, secondpart, database = url:match("^([%w%+]+)://([^/]*)/?(.*)");
-	assert(scheme, "Invalid URL format");
-	local username, password, host, port;
-	local authpart, hostpart = secondpart:match("([^@]+)@([^@+])");
-	if not authpart then hostpart = secondpart; end
-	if authpart then
-		username, password = authpart:match("([^:]*):(.*)");
-		username = username or authpart;
-		password = password and urldecode(password);
-	end
-	if hostpart then
-		host, port = hostpart:match("([^:]*):(.*)");
-		host = host or hostpart;
-		port = port and assert(tonumber(port), "Invalid URL format");
-	end
-	return {
-		scheme = scheme:lower();
-		username = username; password = password;
-		host = host; port = port;
-		database = #database > 0 and database or nil;
-	};
-end
-
 local engine = {};
 function engine:connect()
 	if self.conn then return true; end
@@ -123,7 +94,7 @@
 	end
 	return true;
 end
-function engine:onconnect()
+function engine:onconnect() -- luacheck: ignore 212/self
 	-- Override from create_engine()
 end
 
@@ -148,6 +119,7 @@
 		prepared[sql] = stmt;
 	end
 
+	-- luacheck: ignore 411/success
 	local success, err = stmt:execute(...);
 	if not success then return success, err; end
 	return stmt;
@@ -335,7 +307,12 @@
 	local charset = "utf8";
 	if driver == "MySQL" then
 		self:transaction(function()
-			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
+			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
 				charset = row and row[1] or charset;
 			end
 		end);
@@ -379,7 +356,7 @@
 	};
 end
 
-local function create_engine(self, params, onconnect)
+local function create_engine(_, params, onconnect)
 	return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt);
 end