Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.71 KB | None | 0 0
  1. local DATABASE_HOST = "137.74.9.117"
  2. local DATABASE_PORT = 3306
  3. local DATABASE_NAME = "realism"
  4. local DATABASE_USERNAME = "els"
  5. local DATABASE_PASSWORD = "Lsasbpb34"
  6.  
  7. RealEntrprsDB = {}
  8. require("mysqloo")
  9. RealEntrprsDB.db = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT )
  10.  
  11. function RealEntrprsDB_Init()
  12.     use_mysqloo = file.Exists("bin/gmsv_mysqloo_*.dll", "LUA")
  13.     if use_mysqloo then
  14.     RealEntrprsDB_ConnectToDatabase()
  15.     timer.Simple(60, RealEntrprsDB_Init )
  16.     else
  17.     ServerLog("RealEntrprs: Erreur modul mysqloo")
  18.     end
  19.    
  20. end
  21. hook.Add( "Initialize", "RealEntrprsDB_Init", RealEntrprsDB_Init )
  22.  
  23. function RealEntrprsDB_Format( str )
  24.     if not str then return "NULL" end
  25.     return string.format( "%q", str )
  26. end
  27.  
  28. function Escape( str )
  29.     if not RealEntrprsDB.db then
  30.         Msg( "Non connecté à la DB.\n" )
  31.         return
  32.     end
  33.    
  34.     if not str then return end
  35.  
  36.     local esc = RealEntrprsDB.db:escape( str )
  37.     if not esc then
  38.         return nil
  39.     end
  40.     return "'" .. esc .. "'"
  41. end
  42.  
  43. function RealEntrprsDB.db:onConnected(db)
  44. print("Connecté")
  45. end
  46.  
  47. function RealEntrprsDB.db:onConnectionFailed( db, err )
  48. print("Database connecion probleme")
  49. print(err)
  50. end
  51.  
  52. function RealEntrprsDB_ConnectToDatabase()
  53.     if not IsValid(RealEntrprsDB.db) then
  54.         RealEntrprsDB.db:connect()
  55.         return
  56.     end
  57.    
  58.     if RealEntrprsDB.db:status() ~= 0 then
  59.         RealEntrprsDB.db:connect()
  60.         return
  61.     end
  62.  
  63. end
  64.  
  65.  
  66.  
  67.  
  68. function RealEntrprs_tbl_exist()
  69.     print(RealEntrprsDB.db:status())
  70.  
  71.     local db = RealEntrprsDB.db
  72.    
  73.     print(db:status())
  74.    
  75.    
  76.     local query = "CREATE TABLE IF NOT EXISTS RealEntrprs_data ( unique_id VARCHAR(255), nom TEXT, description TEXT, total_ca INTEGER )"
  77.     local q = RealEntrprsDB.db:query( query )
  78.     function q:onSuccess( data )
  79.         ServerLog("RealEntrprs: Check de l'existance de la table RealEntrprs_data. Elle existe ou elle vient d'être créée!\n")
  80.     end
  81.     function q:onError( err )
  82.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  83.             ServerLog("RealEntrprs: DB non connectée, on recommence...\n")
  84.             timer.Simple( 3, function() RealEntrprsDB_ConnectToDatabase() RealEntrprs_tbl_exist() end )
  85.         end
  86.         ServerLog("RealEntrprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  87.     end
  88.     q:start()
  89.  
  90.     local query = "CREATE TABLE IF NOT EXISTS RealEntrprs_ventesdata ( unique_id VARCHAR(255), nom TEXT, produit TEXT, prix INTEGER )"
  91.     local q = RealEntrprsDB.db:query( query )
  92.     function q:onSuccess( data )
  93.         ServerLog("RealEntrprs: Check de l'existance de la table RealEntrprs_ventesdata. Elle existe ou elle vient d'être créée!\n")
  94.     end
  95.     function q:onError( err )
  96.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  97.             ServerLog("RealEntrprs: DB non connectée, on recommence...\n")
  98.             timer.Simple( 3, function() RealEntrprsDB_ConnectToDatabase() RealEntrprs_tbl_exist() end )
  99.         end
  100.         ServerLog("RealEntrprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  101.     end
  102.     q:start()
  103.  
  104.     local query = "CREATE TABLE IF NOT EXISTS RealEntrprs_pnjsdata ( nom TEXT, title_pnj TEXT, description_pnj TEXT, position_pnj VARCHAR(255), model_pnj TEXT )"
  105.     local q = RealEntrprsDB.db:query( query )
  106.     function q:onSuccess( data )
  107.         ServerLog("RealEntrprs: Check de l'existance de la table RealEntrprs_pnjsdata. Elle existe ou elle vient d'être créée!\n")
  108.     end
  109.     function q:onError( err )
  110.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  111.             ServerLog("RealEntrprs: DB non connectée, on recommence...\n")
  112.             timer.Simple( 3, function() RealEntrprsDB_ConnectToDatabase() RealEntrprs_tbl_exist() end )
  113.         end
  114.         ServerLog("RealEntrprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  115.     end
  116.     q:start()  
  117.  
  118.     local query = "CREATE TABLE IF NOT EXISTS RealEntrprs_produitsdata ( nom TEXT, title_pnj TEXT, nom_produit TEXT, description_produit TEXT, prix_produit INTEGER, stock_produit INTEGER )"
  119.     local q = RealEntrprsDB.db:query( query )
  120.     function q:onSuccess( data )
  121.         ServerLog("RealEntrprs: Check de l'existance de la table RealEntrprs_produitsdata. Elle existe ou elle vient d'être créée!\n")
  122.     end
  123.     function q:onError( err )
  124.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  125.             ServerLog("RealEntrprs: DB non connectée, on recommence...\n")
  126.             timer.Simple( 3, function() RealEntrprsDB_ConnectToDatabase() RealEntrprs_tbl_exist() end )
  127.         end
  128.         ServerLog("RealEntrprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  129.     end
  130.     q:start()      
  131. end
  132.  
  133. --ADD
  134. function RealEntrprs_addentreprise( uid, nom, description )
  135.     local query = RealEntrprsDB.db:query( "INSERT INTO RealEntrprs_data VALUES ('" ..uid.. "', '" ..nom.. "', '" ..description.. "', '0')" )
  136.     query:start()
  137. end
  138. function RealEntrprs_addpnj( nom, titre, description, position, model )
  139.     local query = RealEntrprsDB.db:query("INSERT INTO RealEntrprs_pnjsdata VALUES ('" ..nom.. "', '" ..titre.. "', '" ..description.. "', '" ..model.. "'')")
  140.     query:start()
  141. end
  142. function RealEntrprs_addproduit( nom, titre_pnj, nom_produit, description, prix )
  143.     local query = RealEntrprsDB.db:query("INSERT INTO RealEntrprs_produitsdata VALUES ('" ..nom.. "', '" ..titre_pnj.. "', '" ..nom_produit.. "', '" ..description.. "', '" .. prix.. "'')")
  144.     query:start()
  145. end
  146.  
  147. --GET
  148. function RealEntrprs_getentreprises( ply )
  149.     local uid = ply:SteamID64()
  150.     local query = RealEntrprsDB.db:query("SELECT * FROM RealEntrprs_data WHERE unique_id='" .. uid .. "'")
  151.     query:start()
  152.         function query:onSuccess( result )
  153.             if result then
  154.                 net.Start("SendPlayerEntreprises")
  155.                     net.WriteTable( result )
  156.                 net.Send( ply )
  157.             else
  158.                 result = {}
  159.                 net.Start("SendPlayerEntreprises")
  160.                     net.WriteTable( result )
  161.                 net.Send( ply )
  162.             end
  163.         end
  164. end
  165.  
  166.  
  167. --MODIFY
  168.  
  169.  
  170. --DELETE
  171. function RealEntrprs_delentreprise( nom )
  172.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_data WHERE nom='" ..nom.. "'")
  173.     querystart()
  174.  
  175.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_ventesdata WHERE nom='" ..nom.. "'")
  176.     query:start()
  177.  
  178.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_pnjsdata WHERE nom='" ..nom.. "'")
  179.     query:start()
  180.  
  181.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_produitsdata WHERE nom='" ..nom.. "'")
  182.     query:start()
  183.  
  184. end
  185. function RealEntrprs_delproduit( nom, titre_pnj, nom_produit )
  186.  
  187.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_produitsdata WHERE nom='" ..nom.. "' and titre_pnj='"..titre_pnj.. "' and nom_produit='"..nom_produit.."'")
  188.     query:start()
  189. end
  190. function RealEntrprs_delpnj( titre )
  191.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_pnjsdata WHERE titre_pnj='" ..titre.. "'")
  192.     query:start()
  193.  
  194.     local query = RealEntrprsDB.db:query("DELETE FROM RealEntrprs_produitsdata WHERE titre_pnj='" ..titre.. "'")
  195.     query:start()
  196.  
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement