Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2010 MTA: Paradise
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ]]
  17.  
  18. local connection = nil
  19. local connection = nil
  20. local null = nil
  21. local results = { }
  22. local max_results = 128
  23.  
  24. -- connection functions
  25. local function connect( )
  26. -- retrieve the settings
  27. local server = get( "mysqlgame.clans.hu" ) or "mysqlgame.clans.hu"
  28. local user = get( "zerolifemta225" ) or "zerolifemta225"
  29. local password = get( "pUsevurajYdAQE4" ) or "pUsevurajYdAQE4"
  30. local db = get( "zerolifemta225" ) or "zerolifemta225"
  31. local port = get( "3306" ) or 3306
  32. local socket = get( "socket" ) or nil
  33.  
  34. -- connect
  35. connection = mysql_connect ( server, user, password, db, port, socket )
  36. if connection then
  37. if user == "root" then
  38. setTimer( outputDebugString, 100, 1, "Connecting to your MySQL as 'root' is strongly discouraged.", 2 )
  39. end
  40. return true
  41. else
  42. outputDebugString ( "Connection to MySQL Failed.", 1 )
  43. return false
  44. end
  45. end
  46.  
  47. local function disconnect( )
  48. if connection and mysql_ping( connection ) then
  49. mysql_close( connection )
  50. end
  51. end
  52.  
  53. local function checkConnection( )
  54. if not connection or not mysql_ping( connection ) then
  55. return connect( )
  56. end
  57. return true
  58. end
  59.  
  60. addEventHandler( "onResourceStart", resourceRoot,
  61. function( )
  62. if not mysql_connect then
  63. if hasObjectPermissionTo( resource, "function.shutdown" ) then
  64. shutdown( "MySQL module missing." )
  65. end
  66. cancelEvent( true, "MySQL module missing." )
  67. elseif not hasObjectPermissionTo( resource, "function.mysql_connect" ) then
  68. if hasObjectPermissionTo( resource, "function.shutdown" ) then
  69. shutdown( "Insufficient ACL rights for mysql resource." )
  70. end
  71. cancelEvent( true, "Insufficient ACL rights for mysql resource." )
  72. elseif not connect( ) then
  73. if connection then
  74. outputDebugString( mysql_error( connection ), 1 )
  75. end
  76.  
  77. if hasObjectPermissionTo( resource, "function.shutdown" ) then
  78. shutdown( "MySQL failed to connect." )
  79. end
  80. cancelEvent( true, "MySQL failed to connect." )
  81. else
  82. null = mysql_null( )
  83. end
  84. end
  85. )
  86.  
  87. addEventHandler( "onResourceStop", resourceRoot,
  88. function( )
  89. for key, value in pairs( results ) do
  90. mysql_free_result( value.r )
  91. outputDebugString( "Query not free()'d: " .. value.q, 2 )
  92. end
  93.  
  94. disconnect( )
  95. end
  96. )
  97.  
  98. --
  99.  
  100. function escape_string( str )
  101. if type( str ) == "string" then
  102. return mysql_escape_string( connection, str )
  103. elseif type( str ) == "number" then
  104. return tostring( str )
  105. end
  106. end
  107.  
  108. local function query( str, ... )
  109. checkConnection( )
  110.  
  111. if ( ... ) then
  112. local t = { ... }
  113. for k, v in ipairs( t ) do
  114. t[ k ] = escape_string( tostring( v ) ) or ""
  115. end
  116. str = str:format( unpack( t ) )
  117. end
  118.  
  119. local result = mysql_query( connection, str )
  120. if result then
  121. for num = 1, max_results do
  122. if not results[ num ] then
  123. results[ num ] = { r = result, q = str }
  124. return num
  125. end
  126. end
  127. mysql_free_result( result )
  128. return false, "Unable to allocate result in pool"
  129. end
  130. return false, mysql_error( connection )
  131. end
  132.  
  133. function query_free( str, ... )
  134. if sourceResource == getResourceFromName( "runcode" ) then
  135. return false
  136. end
  137.  
  138. checkConnection( )
  139.  
  140. if ( ... ) then
  141. local t = { ... }
  142. for k, v in ipairs( t ) do
  143. t[ k ] = escape_string( tostring( v ) ) or ""
  144. end
  145. str = str:format( unpack( t ) )
  146. end
  147.  
  148. local result = mysql_query( connection, str )
  149. if result then
  150. mysql_free_result( result )
  151. return true
  152. end
  153. return false, mysql_error( connection )
  154. end
  155.  
  156. function free_result( result )
  157. if results[ result ] then
  158. mysql_free_result( results[ result ].r )
  159. results[ result ] = nil
  160. end
  161. end
  162.  
  163. function query_assoc( str, ... )
  164. if sourceResource == getResourceFromName( "runcode" ) then
  165. return false
  166. end
  167.  
  168. local t = { }
  169. local result, error = query( str, ... )
  170. if result then
  171. for result, row in mysql_rows_assoc( results[ result ].r ) do
  172. local num = #t + 1
  173. t[ num ] = { }
  174. for key, value in pairs( row ) do
  175. if value ~= null then
  176. t[ num ][ key ] = tonumber( value ) or value
  177. end
  178. end
  179. end
  180. free_result( result )
  181. return t
  182. end
  183. return false, error
  184. end
  185.  
  186. function query_assoc_single( str, ... )
  187. if sourceResource == getResourceFromName( "runcode" ) then
  188. return false
  189. end
  190.  
  191. local t = { }
  192. local result, error = query( str, ... )
  193. if result then
  194. local row = mysql_fetch_assoc( results[ result ].r )
  195. if row then
  196. for key, value in pairs( row ) do
  197. if value ~= null then
  198. t[ key ] = tonumber( value ) or value
  199. end
  200. end
  201. free_result( result )
  202. return t
  203. end
  204. free_result( result )
  205. return false
  206. end
  207. return false, error
  208. end
  209.  
  210. function query_insertid( str, ... )
  211. if sourceResource == getResourceFromName( "runcode" ) then
  212. return false
  213. end
  214.  
  215. local result, error = query( str, ... )
  216. if result then
  217. local id = mysql_insert_id( connection )
  218. free_result( result )
  219. return id
  220. end
  221. return false, error
  222. end
  223.  
  224. function query_affected_rows( str, ... )
  225. if sourceResource == getResourceFromName( "runcode" ) then
  226. return false
  227. end
  228.  
  229. local result, error = query( str, ... )
  230. if result then
  231. local rows = mysql_affected_rows( connection )
  232. free_result( result )
  233. return rows
  234. end
  235. return false, error
  236. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement