Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. MySQL = {
  2. Async = {},
  3. Sync = {},
  4. }
  5.  
  6. local function safeParameters(params)
  7. if nil == params then
  8. return {[''] = ''}
  9. end
  10.  
  11. assert(type(params) == "table", "A table is expected")
  12. assert(params[1] == nil, "Parameters should not be an array, but a map (key / value pair) instead")
  13.  
  14. if next(params) == nil then
  15. return {[''] = ''}
  16. end
  17.  
  18. return params
  19. end
  20.  
  21. ---
  22. -- Execute a query with no result required, sync version
  23. --
  24. -- @param query
  25. -- @param params
  26. --
  27. -- @return int Number of rows updated
  28. --
  29. function MySQL.Sync.execute(query, params)
  30. assert(type(query) == "string", "The SQL Query must be a string")
  31.  
  32. local res = 0
  33. local finishedQuery = false
  34. exports['mysql-async']:mysql_execute(query, safeParameters(params), function (result)
  35. res = result
  36. finishedQuery = true
  37. end)
  38. repeat Citizen.Wait(0) until finishedQuery == true
  39. return res
  40. end
  41. ---
  42. -- Execute a query and fetch all results in an sync way
  43. --
  44. -- @param query
  45. -- @param params
  46. --
  47. -- @return table Query results
  48. --
  49. function MySQL.Sync.fetchAll(query, params)
  50. assert(type(query) == "string", "The SQL Query must be a string")
  51.  
  52. local res = {}
  53. local finishedQuery = false
  54. exports['mysql-async']:mysql_fetch_all(query, safeParameters(params), function (result)
  55. res = result
  56. finishedQuery = true
  57. end)
  58. repeat Citizen.Wait(0) until finishedQuery == true
  59. return res
  60. end
  61.  
  62. ---
  63. -- Execute a query and fetch the first column of the first row, sync version
  64. -- Useful for count function by example
  65. --
  66. -- @param query
  67. -- @param params
  68. --
  69. -- @return mixed Value of the first column in the first row
  70. --
  71. function MySQL.Sync.fetchScalar(query, params)
  72. assert(type(query) == "string", "The SQL Query must be a string")
  73.  
  74. local res = ''
  75. local finishedQuery = false
  76. exports['mysql-async']:mysql_fetch_scalar(query, safeParameters(params), function (result)
  77. res = result
  78. finishedQuery = true
  79. end)
  80. repeat Citizen.Wait(0) until finishedQuery == true
  81. return res
  82. end
  83.  
  84. ---
  85. -- Execute a query and retrieve the last id insert, sync version
  86. --
  87. -- @param query
  88. -- @param params
  89. --
  90. -- @return mixed Value of the last insert id
  91. --
  92. function MySQL.Sync.insert(query, params)
  93. assert(type(query) == "string", "The SQL Query must be a string")
  94.  
  95. local res = 0
  96. local finishedQuery = false
  97. exports['mysql-async']:mysql_insert(query, safeParameters(params), function (result)
  98. res = result
  99. finishedQuery = true
  100. end)
  101. repeat Citizen.Wait(0) until finishedQuery == true
  102. return res
  103. end
  104.  
  105. ---
  106. -- Execute a List of querys and returns bool true when all are executed successfully
  107. --
  108. -- @param querys
  109. -- @param params
  110. --
  111. -- @return bool if the transaction was successful
  112. --
  113. function MySQL.Sync.transaction(querys, params)
  114. local res = 0
  115. local finishedQuery = false
  116. exports['mysql-async']:mysql_transaction(query, params, function (result)
  117. res = result
  118. finishedQuery = true
  119. end)
  120. repeat Citizen.Wait(0) until finishedQuery == true
  121. return res
  122. end
  123.  
  124. ---
  125. -- Execute a query with no result required, async version
  126. --
  127. -- @param query
  128. -- @param params
  129. -- @param func(int)
  130. --
  131. function MySQL.Async.execute(query, params, func)
  132. assert(type(query) == "string", "The SQL Query must be a string")
  133.  
  134. exports['mysql-async']:mysql_execute(query, safeParameters(params), func)
  135. end
  136.  
  137. ---
  138. -- Execute a query and fetch all results in an async way
  139. --
  140. -- @param query
  141. -- @param params
  142. -- @param func(table)
  143. --
  144. function MySQL.Async.fetchAll(query, params, func)
  145. assert(type(query) == "string", "The SQL Query must be a string")
  146.  
  147. exports['mysql-async']:mysql_fetch_all(query, safeParameters(params), func)
  148. end
  149.  
  150. ---
  151. -- Execute a query and fetch the first column of the first row, async version
  152. -- Useful for count function by example
  153. --
  154. -- @param query
  155. -- @param params
  156. -- @param func(mixed)
  157. --
  158. function MySQL.Async.fetchScalar(query, params, func)
  159. assert(type(query) == "string", "The SQL Query must be a string")
  160.  
  161. exports['mysql-async']:mysql_fetch_scalar(query, safeParameters(params), func)
  162. end
  163.  
  164. ---
  165. -- Execute a query and retrieve the last id insert, async version
  166. --
  167. -- @param query
  168. -- @param params
  169. -- @param func(string)
  170. --
  171. function MySQL.Async.insert(query, params, func)
  172. assert(type(query) == "string", "The SQL Query must be a string")
  173.  
  174. exports['mysql-async']:mysql_insert(query, safeParameters(params), func)
  175. end
  176.  
  177. ---
  178. -- Execute a List of querys and returns bool true when all are executed successfully
  179. --
  180. -- @param querys
  181. -- @param params
  182. -- @param func(bool)
  183. --
  184. function MySQL.Async.transaction(querys, params, func)
  185. return exports['mysql-async']:mysql_transaction(querys, params, func)
  186. end
  187.  
  188. function MySQL.ready (callback)
  189. Citizen.CreateThread(function ()
  190. -- add some more error handling
  191. while GetResourceState('mysql-async') ~= 'started' do
  192. Citizen.Wait(0)
  193. end
  194. while not exports['mysql-async']:is_ready() do
  195. Citizen.Wait(0)
  196. end
  197. callback()
  198. end)
  199. end
  200.  
  201. set mysql_connection_string "server=localhost;uid=mysqluser;password=password;database=essentialmode"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement