Advertisement
Guest User

Untitled

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