Advertisement
Guest User

vrp_mysql MySQL.lua

a guest
Apr 16th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. -- begin MySQL module
  2. local MySQL = {}
  3.  
  4. MySQL.debug = false
  5. local dpaths = {}
  6.  
  7. local tasks = {}
  8.  
  9. --[[
  10. local function tick()
  11. SetTimeout(1, function() -- protect errors from breaking the loop
  12. SetTimeout(1000, tick)
  13.  
  14. local rmtasks = {}
  15. for id,cb in pairs(tasks) do
  16. local r = exports.vrp_mysql:checkTask(id)
  17. if r.status == 1 then
  18. cb(r.rows,r.affected) -- rows, affected
  19. table.insert(rmtasks, id)
  20. elseif r.status == -1 then
  21. print("[vRP] task "..id.." failed.")
  22. table.insert(rmtasks, id)
  23. end
  24. end
  25.  
  26. -- remove done tasks
  27. for k,v in pairs(rmtasks) do
  28. tasks[v] = nil
  29. end
  30. end)
  31. end
  32. tick()
  33. --]]
  34.  
  35. AddEventHandler("vRP:MySQL_task", function(task_id, data)
  36. -- print("vRP:MySQL_task "..task_id)
  37. local cb = tasks[task_id]
  38. if data.status == 1 then
  39. if cb then
  40. if data.mode == 0 then
  41. cb(data.affected or 0)
  42. elseif data.mode == 1 then
  43. cb(data.scalar or 0)
  44. elseif data.mode == 2 then
  45. cb(data.rows or {}, data.affected or 0) -- rows, affected
  46. end
  47. end
  48. elseif data.status == -1 then
  49. print("[vRP] task "..task_id.." failed.")
  50. end
  51.  
  52. tasks[task_id] = nil
  53.  
  54. if MySQL.debug and dpaths[task_id] then
  55. print("[vRP] MySQL end query "..dpaths[task_id].." ("..task_id..")")
  56. dpaths[task_id] = nil
  57. end
  58. end)
  59.  
  60. local task_id = -1
  61. AddEventHandler("vRP:MySQL_taskid", function(_task_id)
  62. -- print("vRP:MySQL_taskid ".._task_id)
  63. task_id = _task_id
  64. end)
  65.  
  66. -- host can be "host" or "host:port"
  67. function MySQL.createConnection(name,host,user,password,db,debug)
  68. -- print("[vRP] try to create connection "..name)
  69. -- parse port in host as "ip:port"
  70. local host_parts = splitString(host,":")
  71. if #host_parts >= 2 then
  72. host = host_parts[1]..";port="..host_parts[2]
  73. end
  74.  
  75. local config = "server="..host..";uid="..user..";pwd="..password..";database="..db..";"
  76.  
  77. -- TriggerEvent("vRP:MySQL:createConnection", name, config)
  78. exports.vrp_mysql:createConnection(name, config)
  79. end
  80.  
  81. function MySQL.createCommand(path, query)
  82. -- print("[vRP] try to create command "..path)
  83. -- TriggerEvent("vRP:MySQL:createCommand", path, query)
  84. exports.vrp_mysql:createCommand(path, query)
  85. end
  86.  
  87. -- generic query
  88. function MySQL._query(path, args, mode, cb)
  89. -- TriggerEvent("vRP:MySQL:query", path, args)
  90. if not (type(args) == "table") then
  91. args = {}
  92. end
  93.  
  94. -- force args to be a C# dictionary
  95. args._none = " "
  96.  
  97. -- exports.vrp_mysql:query(path, args)
  98. -- print("[vRP] try to query "..path.." id "..task_id)
  99. TriggerEvent("vRP:MySQL_query", path, args, mode)
  100. if MySQL.debug then
  101. print("[vRP] MySQL begin query (m"..mode..") "..path.." ("..task_id..")")
  102. dpaths[task_id] = path
  103. end
  104.  
  105. tasks[task_id] = cb
  106. end
  107.  
  108. -- do a query (multiple rows)
  109. --- cb(rows, affected)
  110. function MySQL.query(path, args, cb)
  111. MySQL._query(path, args, 2, cb)
  112. end
  113.  
  114. -- do a scalar query (one row, one column)
  115. --- cb(scalar)
  116. function MySQL.scalar(path, args, cb)
  117. MySQL._query(path, args, 1, cb)
  118. end
  119.  
  120. -- do a execute query (no results)
  121. --- cb(affected)
  122. function MySQL.execute(path, args, cb)
  123. MySQL._query(path, args, 0, cb)
  124. end
  125.  
  126. -- return module
  127. return MySQL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement