Advertisement
Guest User

Untitled

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