Advertisement
chase_lua

vrp_mysql/mysql.lua

Apr 18th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 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)
  89. local r = async()
  90.  
  91. -- TriggerEvent("vRP:MySQL:query", path, args)
  92. if not (type(args) == "table") then
  93. args = {}
  94. end
  95.  
  96. -- force args to be a C# dictionary
  97. args._none = " "
  98.  
  99. -- exports.vrp_mysql:query(path, args)
  100. -- print("[vRP] try to query "..path.." id "..task_id)
  101. TriggerEvent("vRP:MySQL_query", path, args, mode)
  102. if MySQL.debug then
  103. print("[vRP] MySQL begin query (m"..mode..") "..path.." ("..task_id..")")
  104. dpaths[task_id] = path
  105. end
  106.  
  107. tasks[task_id] = r
  108.  
  109. return r:wait()
  110. end
  111.  
  112. -- do a query (multiple rows)
  113. --- return rows, affected
  114. function MySQL.query(path, args)
  115. return MySQL._query(path, args, 2)
  116. end
  117.  
  118. -- do a scalar query (one row, one column)
  119. --- return scalar
  120. function MySQL.scalar(path, args)
  121. return MySQL._query(path, args, 1)
  122. end
  123.  
  124. -- do a execute query (no results)
  125. --- return affected
  126. function MySQL.execute(path, args)
  127. return MySQL._query(path, args, 0)
  128. end
  129.  
  130. -- return module
  131. return MySQL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement