Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. Location: ulx > lua > ulx > modules
  2. File name: sban_mysql.lua
  3. Code:
  4. /*
  5. * ULX Source Bans
  6. * Version: 0.2.3a
  7. */
  8. require ("mysqloo")
  9.  
  10. //Config
  11. local SBANDATABASE_HOSTNAME = "" //Database IP/Host
  12. local SBANDATABASE_HOSTPORT = 3306 //Database Port
  13. local SBANDATABASE_DATABASE = "" //Database Database/Schema
  14. local SBANDATABASE_USERNAME = "" //Database Username
  15. local SBANDATABASE_PASSWORD = "" //Database Password
  16.  
  17. SBAN_MYSQL = SBAN_MYSQL or {}
  18. /*
  19. Mysql Connection
  20. */
  21. //Connect
  22. function SBAN_MYSQL.Connect( first )
  23. if first then
  24. print("[SBAN_ULX][MYSQL] Connecting Database")
  25. sban_db:connect()
  26. sban_db:wait() //Forces the server to wait for the DB connection
  27. end
  28.  
  29. local dbstatus = sban_db:status()
  30. if dbstatus != mysqloo.DATABASE_CONNECTED && dbstatus != mysqloo.DATABASE_CONNECTING then
  31. print("[SBAN_ULX][MYSQL] Connection was lost, Trying to reconnect")
  32. sban_db:connect()
  33. else
  34. if first then
  35. //Creates WatchDog for the database
  36. if(timer.Exists("sbanulx_database_check")) then timer.Destroy("sbanulx_database_check")end
  37. timer.Create("sbanulx_database_check", 1,0, function() SBAN_MYSQL.Connect(false) end)
  38. end
  39. end
  40. end
  41.  
  42. //Checks if the connection is already made
  43. if(sban_db == nil) then
  44. sban_db = mysqloo.connect(SBANDATABASE_HOSTNAME, SBANDATABASE_USERNAME, SBANDATABASE_PASSWORD, SBANDATABASE_DATABASE, SBANDATABASE_HOSTPORT)
  45. SBAN_MYSQL.Connect(true)
  46. end
  47.  
  48. //Connection Made
  49. function sban_db:onConnected()
  50. print("[SBAN_ULX][MYSQL] Connected")
  51. end
  52.  
  53. //Connection Error
  54. function sban_db:onConnectionFailed( err )
  55. print("[SBAN_ULX][MYSQL] Connection to database failed")
  56. print("[SBAN_ULX][MYSQL] Error:"..err)
  57. end
  58.  
  59.  
  60.  
  61. /*
  62. Functions
  63. */
  64. //StringFormat
  65. function SBAN_MYSQL.StringFormat( str )
  66. return sban_db:escape( str )
  67. end
  68.  
  69. //ExistsCheck
  70. function SBAN_MYSQL.ExistsCheck( result )
  71. if result[1] != nil then
  72. if tonumber(result[1]["c"]) >= 1 then
  73. return true
  74. else
  75. return false
  76. end
  77. end
  78. end
  79.  
  80. //Check Nil
  81. function SBAN_MYSQL.NilCheck( var )
  82.  
  83. if var == nil then
  84. return "null"
  85. else
  86. return var
  87. end
  88. end
  89.  
  90.  
  91.  
  92. /*
  93. Query Methods
  94. */
  95. /*
  96. Database.Query( string, callback )
  97.  
  98. Return:
  99. status = If the query went well it's true
  100. data = Returns if the query went well
  101. */
  102. function SBAN_MYSQL.Query( sqlquery, callback )
  103. local q = sban_db:query( sqlquery )
  104. local status, result
  105.  
  106. function q:onSuccess( data )
  107. status = true
  108. result = data
  109.  
  110. if callback != false && callback != nil then callback( status, result ) end
  111. end
  112.  
  113. function q:onError( err, sql )
  114. print("[SBAN_ULX][MYSQL][Query][error] "..err)
  115. print("[SBAN_ULX][MYSQL][Query][error] "..sql)
  116.  
  117. status = false
  118. result = nil
  119.  
  120. if callback != false && callback != nil then callback( status, result ) end
  121. end
  122.  
  123. q:start()
  124.  
  125. if callback == nil || !callback then
  126. q:wait()
  127. return status, result
  128. end
  129. end
  130.  
  131. /*
  132. Database.QueryInsert( string, callback )
  133.  
  134. Return:
  135. status = If the query went well it's true
  136. id = The insert id from the insert query
  137. */
  138. function SBAN_MYSQL.QueryInsert( sqlquery, callback )
  139. local q = sban_db:query(sqlquery)
  140. local status, result
  141.  
  142. function q:onSuccess( data )
  143. local id = q:lastInsert()
  144.  
  145.  
  146. status = true
  147. result = id
  148.  
  149. if callback != false && callback != nil then callback( status, result ) end
  150. end
  151.  
  152. function q:onError( err, sql )
  153. print("[SBAN_ULX][MYSQL][QueryInsert][error] "..err)
  154. print("[SBAN_ULX][MYSQL][QueryInsert][error] "..sql)
  155.  
  156. status = false
  157. result = nil
  158.  
  159. if callback != false && callback != nil then callback( status, result ) end
  160. end
  161.  
  162. q:start()
  163.  
  164. if callback == nil || !callback then
  165. q:wait()
  166. return status, result
  167. end
  168. end
  169.  
  170. /*
  171. Database.QueryUpdate( string, callback )
  172.  
  173. Return:
  174. status = true and the query went well
  175. */
  176. function SBAN_MYSQL.QueryUpdate( sqlquery, callback )
  177. local q = sban_db:query(sqlquery)
  178. local status
  179.  
  180. function q:onSuccess( data )
  181. local id = q:lastInsert()
  182.  
  183. status = true
  184.  
  185. if callback != false && callback != nil then callback( status ) end
  186. end
  187.  
  188. function q:onError( err, sql )
  189. print("[SBAN_ULX][MYSQL][QueryUpdate][error] "..err)
  190. print("[SBAN_ULX][MYSQL][QueryUpdate][error] "..sql)
  191.  
  192. status = false
  193.  
  194. if callback != false && callback != nil then callback( status ) end
  195. end
  196.  
  197. q:start()
  198.  
  199. if callback == nil || !callback then
  200. q:wait()
  201. return status
  202. end
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement