Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. local luasql = nil
  2. local json = require('cjson')
  3.  
  4. local ENV = nil
  5. local CONN = nil
  6. local CUR = nil
  7. local result = {}
  8.  
  9. local dbname = 'DB_NAME_HERE'
  10. local dbuser = 'DB_USER_NAME_HERE'
  11. local dbpass = 'DB_PASS_HERE'
  12. local dbhost = 'DB_HOST_HERE'
  13. local dbport = 3306
  14.  
  15. function unrequire(m)
  16. package.loaded[m] = nil;
  17. _G[m] = nil;
  18.  
  19. -- Search for the shared library handle in the registry and erase it
  20. local registry = debug.getregistry();
  21. local nMatches, mKey, mt = 0, nil, registry['_LOADLIB'];
  22.  
  23. for key, ud in pairs(registry) do
  24. if type(key) == 'string' and type(ud) == 'userdata' and getmetatable(ud) == mt and string.find(key, "LOADLIB: .*" .. m) then
  25. nMatches = nMatches + 1;
  26. if nMatches > 1 then
  27. return false, "More than one possible key for module '" .. m .. "'. Can't decide which one to erase.";
  28. end
  29. mKey = key;
  30. end
  31. end
  32.  
  33. if mKey then
  34. registry[mKey] = nil;
  35. end
  36.  
  37. return true
  38. end
  39.  
  40. function closeConnection()
  41. if CONN then CONN:close() end
  42. if ENV then assert ( ENV:close() ) end
  43. CONN = nil
  44. ENV = nil
  45. CUR = nil
  46. package.loaded["luasql.mysql"] = nil
  47. luasql = nil
  48. unrequire("mysql.so")
  49. end
  50.  
  51. function setEnvironment()
  52. if ENV then closeConnection() end
  53. package.loaded["luasql.mysql"] = nil
  54. luasql = require "luasql.mysql"
  55. ENV = assert(luasql.mysql())
  56. end
  57.  
  58. function createConnection()
  59. if ENV then
  60. CONN, err = ENV:connect(dbname, dbuser, dbpass, dbhost, dbport)
  61. if err then
  62. result.STATUS = false
  63. result.MESSAGE = err
  64. end
  65. end
  66. end
  67.  
  68. function testConnection()
  69. if CONN then
  70. CUR, err = CONN:execute("SELECT VERSION() AS 'VERSION', NOW() AS 'DATE'")
  71. if err then
  72. result.STATUS = false
  73. result.MESSAGE = err
  74. else
  75. row = CUR:fetch ({}, "a")
  76. while row do
  77. result.VERSION = row.VERSION
  78. result.DATE = row.DATE
  79. result.STATUS = true
  80. row = CUR:fetch (row, "a")
  81. end
  82. end
  83. CUR:close()
  84. end
  85. end
  86.  
  87. local itCount = arg[1]
  88. for i=1, itCount do
  89. setEnvironment()
  90. createConnection()
  91. testConnection()
  92. closeConnection()
  93.  
  94. print('iter=' .. i)
  95. print(json.encode(result))
  96.  
  97. collectgarbage("collect")
  98. collectgarbage("collect")
  99. collectgarbage("collect")
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement