Created
January 11, 2017 11:28
-
-
Save tomatolog/8168cc326db10a717da44665a52eaabf to your computer and use it in GitHub Desktop.
luasql mysql issue #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local luasql = nil | |
local json = require('cjson') | |
local ENV = nil | |
local CONN = nil | |
local CUR = nil | |
local result = {} | |
local dbname = 'DB_NAME_HERE' | |
local dbuser = 'DB_USER_NAME_HERE' | |
local dbpass = 'DB_PASS_HERE' | |
local dbhost = 'DB_HOST_HERE' | |
local dbport = 3306 | |
function unrequire(m) | |
package.loaded[m] = nil; | |
_G[m] = nil; | |
-- Search for the shared library handle in the registry and erase it | |
local registry = debug.getregistry(); | |
local nMatches, mKey, mt = 0, nil, registry['_LOADLIB']; | |
for key, ud in pairs(registry) do | |
if type(key) == 'string' and type(ud) == 'userdata' and getmetatable(ud) == mt and string.find(key, "LOADLIB: .*" .. m) then | |
nMatches = nMatches + 1; | |
if nMatches > 1 then | |
return false, "More than one possible key for module '" .. m .. "'. Can't decide which one to erase."; | |
end | |
mKey = key; | |
end | |
end | |
if mKey then | |
registry[mKey] = nil; | |
end | |
return true | |
end | |
function closeConnection() | |
if CONN then CONN:close() end | |
if ENV then assert ( ENV:close() ) end | |
CONN = nil | |
ENV = nil | |
CUR = nil | |
package.loaded["luasql.mysql"] = nil | |
luasql = nil | |
unrequire("mysql.so") | |
end | |
function setEnvironment() | |
if ENV then closeConnection() end | |
package.loaded["luasql.mysql"] = nil | |
luasql = require "luasql.mysql" | |
ENV = assert(luasql.mysql()) | |
end | |
function createConnection() | |
if ENV then | |
CONN, err = ENV:connect(dbname, dbuser, dbpass, dbhost, dbport) | |
if err then | |
result.STATUS = false | |
result.MESSAGE = err | |
end | |
end | |
end | |
function testConnection() | |
if CONN then | |
CUR, err = CONN:execute("SELECT VERSION() AS 'VERSION', NOW() AS 'DATE'") | |
if err then | |
result.STATUS = false | |
result.MESSAGE = err | |
else | |
row = CUR:fetch ({}, "a") | |
while row do | |
result.VERSION = row.VERSION | |
result.DATE = row.DATE | |
result.STATUS = true | |
row = CUR:fetch (row, "a") | |
end | |
end | |
CUR:close() | |
end | |
end | |
local itCount = arg[1] | |
for i=1, itCount do | |
setEnvironment() | |
createConnection() | |
testConnection() | |
closeConnection() | |
print('iter=' .. i) | |
print(json.encode(result)) | |
collectgarbage("collect") | |
collectgarbage("collect") | |
collectgarbage("collect") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment