Guest User

Untitled

a guest
Jun 3rd, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.11 KB | None | 0 0
  1.  
  2. local connection = nil
  3. local connection = nil
  4. local null = nil
  5. local results = { }
  6. local max_results = 128
  7.  
  8. -- connection functions
  9. local function connect( )
  10.     -- retrieve the settings
  11.     local server = get( "server" ) or "00.00.00.0"
  12.     local user = get( "user" ) or "User"
  13.     local password = get( "password" ) or "pass"
  14.     local db = get( "database" ) or "database"
  15.     local port = get( "port" ) or 3306
  16.     local socket = get( "socket" ) or nil
  17.    
  18.     -- connect
  19.     connection = mysql_connect ( server, user, password, db, port, socket )
  20.     if connection then
  21.         if user == "root" then
  22.             setTimer( outputDebugString, 100, 1, "Connecting to your MySQL as 'root' is strongly discouraged.", 2 )
  23.         end
  24.         return true
  25.     else
  26.         outputDebugString ( "Connection to MySQL Failed.", 1 )
  27.         return false
  28.     end
  29. end
  30.  
  31. local function disconnect( )
  32.     if connection and mysql_ping( connection ) then
  33.         mysql_close( connection )
  34.     end
  35. end
  36.  
  37. local function checkConnection( )
  38.     if not connection or not mysql_ping( connection ) then
  39.         return connect( )
  40.     end
  41.     return true
  42. end
  43.  
  44. addEventHandler( "onResourceStart", resourceRoot,
  45.     function( )
  46.         if not mysql_connect then
  47.             if hasObjectPermissionTo( resource, "function.shutdown" ) then
  48.                 shutdown( "MySQL module missing." )
  49.             end
  50.             cancelEvent( true, "MySQL module missing." )
  51.         elseif not hasObjectPermissionTo( resource, "function.mysql_connect" ) then
  52.             if hasObjectPermissionTo( resource, "function.shutdown" ) then
  53.                 shutdown( "Insufficient ACL rights for mysql resource." )
  54.             end
  55.             cancelEvent( true, "Insufficient ACL rights for mysql resource." )
  56.         elseif not connect( ) then
  57.             if connection then
  58.                 outputDebugString( mysql_error( connection ), 1 )
  59.             end
  60.            
  61.             if hasObjectPermissionTo( resource, "function.shutdown" ) then
  62.                 shutdown( "MySQL failed to connect." )
  63.             end
  64.             cancelEvent( true, "MySQL failed to connect." )
  65.         else
  66.             null = mysql_null( )
  67.         end
  68.     end
  69. )
  70.  
  71. addEventHandler( "onResourceStop", resourceRoot,
  72.     function( )
  73.         for key, value in pairs( results ) do
  74.             mysql_free_result( value.r )
  75.             outputDebugString( "Query not free()'d: " .. value.q, 2 )
  76.         end
  77.        
  78.         disconnect( )
  79.     end
  80. )
  81.  
  82. --
  83.  
  84. function escape_string( str )
  85.     if type( str ) == "string" then
  86.         return mysql_escape_string( connection, str )
  87.     elseif type( str ) == "number" then
  88.         return tostring( str )
  89.     end
  90. end
  91.  
  92. local function query( str, ... )
  93.     checkConnection( )
  94.    
  95.     if ( ... ) then
  96.         local t = { ... }
  97.         for k, v in ipairs( t ) do
  98.             t[ k ] = escape_string( tostring( v ) ) or ""
  99.         end
  100.         str = str:format( unpack( t ) )
  101.     end
  102.    
  103.     local result = mysql_query( connection, str )
  104.     if result then
  105.         for num = 1, max_results do
  106.             if not results[ num ] then
  107.                 results[ num ] = { r = result, q = str }
  108.                 return num
  109.             end
  110.         end
  111.         mysql_free_result( result )
  112.         return false, "Unable to allocate result in pool"
  113.     end
  114.     return false, mysql_error( connection )
  115. end
  116.  
  117. function query_free( str, ... )
  118.     if sourceResource == getResourceFromName( "runcode" ) then
  119.         return false
  120.     end
  121.    
  122.     checkConnection( )
  123.    
  124.     if ( ... ) then
  125.         local t = { ... }
  126.         for k, v in ipairs( t ) do
  127.             t[ k ] = escape_string( tostring( v ) ) or ""
  128.         end
  129.         str = str:format( unpack( t ) )
  130.     end
  131.    
  132.     local result = mysql_query( connection, str )
  133.     if result then
  134.         mysql_free_result( result )
  135.         return true
  136.     end
  137.     return false, mysql_error( connection )
  138. end
  139.  
  140. function free_result( result )
  141.     if results[ result ] then
  142.         mysql_free_result( results[ result ].r )
  143.         results[ result ] = nil
  144.     end
  145. end
  146.  
  147. function query_assoc( str, ... )
  148.     if sourceResource == getResourceFromName( "runcode" ) then
  149.         return false
  150.     end
  151.    
  152.     local t = { }
  153.     local result, error = query( str, ... )
  154.     if result then
  155.         for result, row in mysql_rows_assoc( results[ result ].r ) do
  156.             local num = #t + 1
  157.             t[ num ] = { }
  158.             for key, value in pairs( row ) do
  159.                 if value ~= null then
  160.                     t[ num ][ key ] = tonumber( value ) or value
  161.                 end
  162.             end
  163.         end
  164.         free_result( result )
  165.         return t
  166.     end
  167.     return false, error
  168. end
  169.  
  170. function query_assoc_single( str, ... )
  171.     if sourceResource == getResourceFromName( "runcode" ) then
  172.         return false
  173.     end
  174.    
  175.     local t = { }
  176.     local result, error = query( str, ... )
  177.     if result then
  178.         local row = mysql_fetch_assoc( results[ result ].r )
  179.         if row then
  180.             for key, value in pairs( row ) do
  181.                 if value ~= null then
  182.                     t[ key ] = tonumber( value ) or value
  183.                 end
  184.             end
  185.             free_result( result )
  186.             return t
  187.         end
  188.         free_result( result )
  189.         return false
  190.     end
  191.     return false, error
  192. end
  193.  
  194. function query_insertid( str, ... )
  195.     if sourceResource == getResourceFromName( "runcode" ) then
  196.         return false
  197.     end
  198.    
  199.     local result, error = query( str, ... )
  200.     if result then
  201.         local id = mysql_insert_id( connection )
  202.         free_result( result )
  203.         return id
  204.     end
  205.     return false, error
  206. end
  207.  
  208. function query_affected_rows( str, ... )
  209.     if sourceResource == getResourceFromName( "runcode" ) then
  210.         return false
  211.     end
  212.    
  213.     local result, error = query( str, ... )
  214.     if result then
  215.         local rows = mysql_affected_rows( connection )
  216.         free_result( result )
  217.         return rows
  218.     end
  219.     return false, error
  220. end
Add Comment
Please, Sign In to add comment