Advertisement
Guest User

connection.lua

a guest
Mar 9th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. function getSQLData()
  2. return "dbname=mtadev;host=127.0.0.1", "root", "********", "share=0"
  3. end
  4.  
  5. -- connection settings
  6. local hostname = "127.0.0.1"
  7. local username ="root"
  8. local password = "*******"
  9. local database = "mtadev"
  10. local port = 3306
  11.  
  12. -- global things.
  13. local MySQLConnection = nil
  14. local resultPool = { }
  15. local sqllog = false
  16. local countqueries = 0
  17.  
  18. -- connectToDatabase - Internal function, to spawn a DB connection
  19. function connectToDatabase(res)
  20. MySQLConnection = mysql_connect(hostname, username, password, database, port)
  21.  
  22. if (not MySQLConnection) then
  23. if (res == getThisResource()) then
  24. cancelEvent(true, "Cannot connect to the database.")
  25. end
  26. return nil
  27. end
  28.  
  29. return nil
  30. end
  31. addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), connectToDatabase, false)
  32.  
  33. -- destroyDatabaseConnection - Internal function, kill the connection if theres one.
  34. function destroyDatabaseConnection()
  35. if (not MySQLConnection) then
  36. return nil
  37. end
  38. mysql_close(MySQLConnection)
  39. return nil
  40. end
  41. addEventHandler("onResourceStop", getResourceRootElement(getThisResource()), destroyDatabaseConnection, false)
  42.  
  43. -- do something usefull here
  44. function logSQLError(str)
  45. local message = str or 'N/A'
  46. outputDebugString("MYSQL ERROR "..mysql_errno(MySQLConnection) .. ": " .. mysql_error(MySQLConnection))
  47. exports['logs']:logMessage("MYSQL ERROR :O! [QUERY] " .. message .. " [ERROR] " .. mysql_errno(MySQLConnection) .. ": " .. mysql_error(MySQLConnection), 24)
  48. end
  49.  
  50. function getFreeResultPoolID()
  51. local size = #resultPool
  52. if (size == 0) then
  53. return 1
  54. end
  55. for index, query in ipairs(resultPool) do
  56. if (query == nil) then
  57. return index
  58. end
  59. end
  60. return (size + 1)
  61. end
  62.  
  63. ------------ EXPORTED FUNCTIONS ---------------
  64.  
  65. function ping()
  66. if (mysql_ping(MySQLConnection) == false) then
  67. -- FUU, NO MOAR CONNECTION
  68. destroyDatabaseConnection()
  69. connectToDatabase(nil)
  70. if (mysql_ping(MySQLConnection) == false) then
  71. logSQLError()
  72. return false
  73. end
  74. return true
  75. end
  76.  
  77. return true
  78. end
  79.  
  80. function escape_string(str)
  81. if (ping()) then
  82. return mysql_escape_string(MySQLConnection, str)
  83. end
  84. return false
  85. end
  86.  
  87. function query(str)
  88. if sqllog then
  89. exports['logs']:logMessage(str, 24)
  90. end
  91. countqueries = countqueries + 1
  92.  
  93. if (ping()) then
  94. local result = mysql_query(MySQLConnection, str)
  95. if (not result) then
  96. logSQLError(str)
  97. return false
  98. end
  99.  
  100. local resultid = getFreeResultPoolID()
  101. resultPool[resultid] = result
  102. return resultid
  103. end
  104. return false
  105. end
  106.  
  107. function unbuffered_query(str)
  108. if sqllog then
  109. exports['logs']:logMessage(str, 24)
  110. end
  111. countqueries = countqueries + 1
  112.  
  113. if (ping()) then
  114. local result = mysql_unbuffered_query(MySQLConnection, str)
  115. if (not result) then
  116. logSQLError(str)
  117. return false
  118. end
  119.  
  120. local resultid = getFreeResultPoolID()
  121. resultPool[resultid] = result
  122. return resultid
  123. end
  124. return false
  125. end
  126.  
  127. function query_free(str)
  128. local queryresult = query(str)
  129. if not (queryresult == false) then
  130. free_result(queryresult)
  131. return true
  132. end
  133. return false
  134. end
  135.  
  136. function rows_assoc(resultid)
  137. if (not resultPool[resultid]) then
  138. return false
  139. end
  140. return mysql_rows_assoc(resultPool[resultid])
  141. end
  142.  
  143. function fetch_assoc(resultid)
  144. if (not resultPool[resultid]) then
  145. return false
  146. end
  147. return mysql_fetch_assoc(resultPool[resultid])
  148. end
  149.  
  150. function free_result(resultid)
  151. if (not resultPool[resultid]) then
  152. return false
  153. end
  154. mysql_free_result(resultPool[resultid])
  155. table.remove(resultPool, resultid)
  156. return nil
  157. end
  158.  
  159. -- incase a nub wants to use it, FINE
  160. function result(resultid, row_offset, field_offset)
  161. if (not resultPool[resultid]) then
  162. return false
  163. end
  164. return mysql_result(resultPool[resultid], row_offset, field_offset)
  165. end
  166.  
  167. function num_rows(resultid)
  168. if (not resultPool[resultid]) then
  169. return false
  170. end
  171. return mysql_num_rows(resultPool[resultid])
  172.  
  173. end
  174.  
  175. function insert_id()
  176. return mysql_insert_id(MySQLConnection) or false
  177. end
  178.  
  179. function query_fetch_assoc(str)
  180. local queryresult = query(str)
  181. if not (queryresult == false) then
  182. local result = fetch_assoc(queryresult)
  183. free_result(queryresult)
  184. return result
  185. end
  186. return false
  187. end
  188.  
  189. function query_rows_assoc(str)
  190. local queryresult = query(str)
  191. if not (queryresult == false) then
  192. local result = rows_assoc(queryresult)
  193. free_result(queryresult)
  194. return result
  195. end
  196. return false
  197. end
  198.  
  199. function query_insert_free(str)
  200. local queryresult = query(str)
  201. if not (queryresult == false) then
  202. local result = insert_id()
  203. free_result(queryresult)
  204. return result
  205. end
  206. return false
  207. end
  208.  
  209. function escape_string(str)
  210. return mysql_escape_string(MySQLConnection, str)
  211. end
  212.  
  213. function debugMode()
  214. if (sqllog) then
  215. sqllog = false
  216. else
  217. sqllog = true
  218. end
  219. return sqllog
  220. end
  221.  
  222. function returnQueryStats()
  223. return countqueries
  224. -- maybe later more
  225. end
  226.  
  227. function createConnect()
  228. if(MySQLConnection) then
  229. return MySQLConnection
  230. else
  231. return nil
  232. end
  233. end
  234.  
  235. --------------------------------------------
  236. -- Kapcsolódási adatok
  237. local host = hostname
  238. local felhasznalonev = username
  239. local jelszo = password
  240. local adatbazis = database
  241. --local port = 3306
  242.  
  243. local MySqlCsatlakozas = nil
  244. local QueryLogolas = false
  245.  
  246. function csatlakozasAzAdatbazishoz(res)
  247. MySqlCsatlakozas = dbConnect( "mysql", "dbname="..adatbazis..";host="..host, felhasznalonev, jelszo, "charset=utf8" )
  248.  
  249. if (not MySqlCsatlakozas) then
  250. if (res == getThisResource()) then
  251. cancelEvent(true, "Nem sikerült elérni az adatbázist")
  252. end
  253. return nil
  254. end
  255.  
  256. return nil
  257. end
  258. addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), csatlakozasAzAdatbazishoz, false)
  259.  
  260. -- destroyDatabaseConnection - Internal function, kill the connection if theres one.
  261. function lecsatlakozasAzAdatbazisrol()
  262. if (not MySqlCsatlakozas) then
  263. return nil
  264. end
  265. MySqlCsatlakozas = nil
  266. return nil
  267. end
  268. addEventHandler("onResourceStop", getResourceRootElement(getThisResource()), lecsatlakozasAzAdatbazisrol, false)
  269.  
  270. function kapcsolatCallback()
  271. if(MySqlCsatlakozas) then
  272. return MySqlCsatlakozas
  273. else
  274. return nil
  275. end
  276. end
  277.  
  278. ------------------------------------------------------
  279.  
  280. local sqlDatas = {
  281. ["host"] = "127.0.0.1",
  282. ["user"] = "root",
  283. ["pw"] = "*******",
  284. ["database"] = "mtadev",
  285. }
  286.  
  287. function getSQLDatas()
  288. return sqlDatas
  289. end
  290.  
  291. addEventHandler("onResourceStart", resourceRoot, function()
  292. dbHandler = dbConnect("mysql","dbname=".. getSQLDatas()["database"] ..";host="..getSQLDatas()["host"], getSQLDatas()["user"], getSQLDatas()["pw"], "autoreconnect=1")
  293. if not dbHandler then
  294. outputChatBox("Mysql kapcsolódás meghiúsult")
  295. cancelEvent(true)
  296. end
  297. end)
  298.  
  299. function getConnection()
  300. return dbHandler
  301. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement