Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1.  
  2. RealEntprsDB = {}
  3.  
  4. function RealEntprsDB_Init()
  5.     use_mysqloo = file.Exists("bin/gmsv_mysqloo_*.dll", "LUA")
  6.     if use_mysqloo then
  7.     require("mysqloo")
  8.     RealEntprsDB_ConnectToDatabase()
  9.     timer.Simple(60, RealEntprsDB_Init )
  10. else
  11.     ServerLog("RealEntprs: Erreur modul mysqloo")
  12.     end
  13.    
  14. end
  15. hook.Add( "Initialize", "RealEntprsDB_Init", RealEntprsDB_Init )
  16.  
  17. function RealEntprsDB_Format( str )
  18.     if not str then return "NULL" end
  19.     return string.format( "%q", str )
  20. end
  21.  
  22. function Escape( str )
  23.     if not RealEntprsDB.db then
  24.         Msg( "Non connecté à la DB.\n" )
  25.         return
  26.     end
  27.    
  28.     if not str then return end
  29.  
  30.     local esc = RealEntprsDB.db:escape( str )
  31.     if not esc then
  32.         return nil
  33.     end
  34.     return "'" .. esc .. "'"
  35. end
  36.  
  37.  
  38. function RealEntprsDB_ConnectToDatabase()
  39.     if not IsValid(RealEntprsDB.db) then
  40.         RealEntprsDB.db = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT )
  41.         RealEntprsDB.db:connect()
  42.         return
  43.     end
  44.    
  45.     if RealEntprsDB.db:status() ~= 0 then
  46.         RealEntprsDB.db = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT )
  47.         RealEntprsDB.db:connect()
  48.         return
  49.     end
  50. end
  51.  
  52.  
  53. function RealEntprs_tbl_exist()
  54.     print(RealEntprsDB.db:status())
  55.     if RealEntprsDB.db:status() ~= 0 then
  56.         ServerLog("RealEntprs: Database non initialisée. ...\n")
  57.         timer.Simple(0.5, function() RealEntprs_tbl_exist() end )
  58.         return
  59.     end
  60.  
  61.     local db = RealEntprsDB.db
  62.    
  63.     print(db:status())
  64.    
  65.    
  66.     local query = "CREATE TABLE IF NOT EXISTS RealEntprs_data ( unique_id VARCHAR(255), nom TEXT, description TEXT, date_creation VARCHAR(255), total_ca INTEGER )"
  67.     local q = RealEntprsDB.db:query( query )
  68.     function q:onSuccess( data )
  69.         ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_data. Elle existe ou elle vient d'être créée!\n")
  70.     end
  71.     function q:onError( err )
  72.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  73.             ServerLog("RealEntprs: DB non connectée, on recommence...\n")
  74.             timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
  75.         end
  76.         ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  77.     end
  78.     q:start()
  79.  
  80.     local query = "CREATE TABLE IF NOT EXISTS RealEntprs_ventesdata ( unique_id VARCHAR(255), nom TEXT, produit TEXT, prix INTEGER )"
  81.     local q = RealEntprsDB.db:query( query )
  82.     function q:onSuccess( data )
  83.         ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_ventesdata. Elle existe ou elle vient d'être créée!\n")
  84.     end
  85.     function q:onError( err )
  86.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  87.             ServerLog("RealEntprs: DB non connectée, on recommence...\n")
  88.             timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
  89.         end
  90.         ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  91.     end
  92.     q:start()
  93.  
  94.     local query = "CREATE TABLE IF NOT EXISTS RealEntprs_pnjsdata ( nom TEXT, title_pnj TEXT, description_pnj TEXT, position_pnj VARCHAR(255), model_pnj TEXT )"
  95.     local q = RealEntprsDB.db:query( query )
  96.     function q:onSuccess( data )
  97.         ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_pnjsdata. Elle existe ou elle vient d'être créée!\n")
  98.     end
  99.     function q:onError( err )
  100.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  101.             ServerLog("RealEntprs: DB non connectée, on recommence...\n")
  102.             timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
  103.         end
  104.         ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  105.     end
  106.     q:start()  
  107.  
  108.     local query = "CREATE TABLE IF NOT EXISTS RealEntprs_produitsdata ( nom TEXT, title_pnj TEXT, nom_produit TEXT, description_produit TEXT, prix_produit INTEGER, stock_produit INTEGER )"
  109.     local q = RealEntprsDB.db:query( query )
  110.     function q:onSuccess( data )
  111.         ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_produitsdata. Elle existe ou elle vient d'être créée!\n")
  112.     end
  113.     function q:onError( err )
  114.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  115.             ServerLog("RealEntprs: DB non connectée, on recommence...\n")
  116.             timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
  117.         end
  118.         ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
  119.     end
  120.     q:start()      
  121. end
  122.  
  123. --Gestion des données
  124.  
  125. --ADD
  126. function RealEntprs_addentreprise( uid, nom, description )
  127.     nom = sql.SQLStr( nom )
  128.     date = os.date()
  129.     local query = "INSERT INTO RealEntprs_data VALUES (NULL, '" ..uid.. "', " ..nom.. ", " ..description.. ",'" ..date.. "', '0')"
  130.     result = sql.Query(query)
  131. end
  132. function RealEntprs_addpnj( nom, titre, description, position, model )
  133.     nom = sql.SQLStr( nom )
  134.     date = os.date()
  135.     local query = "INSERT INTO RealEntprs_pnjsdata VALUES (NULL, " ..nom.. ", " ..titre.. ", " ..description.. ", " ..model.. ")"
  136.     result = sql.Query(query)
  137. end
  138. function RealEntprs_addproduit( nom, titre_pnj, nom_produit, description, prix )
  139.     nom = sql.SQLStr( nom )
  140.     date = os.date()
  141.     local query = "INSERT INTO RealEntprs_produitsdata VALUES (NULL, " ..nom.. ", " ..titre_pnj.. ", " ..nom_produit.. ", " ..description.. ", '" .. prix.. "'')"
  142.     result = sql.Query(query)
  143. end
  144.  
  145. --GET
  146. function RealEntprs_getentreprises( uid )
  147.     local uid = target_ply:SteamID64()
  148.    
  149.     local query = "SELECT * FROM RealEntprs_data WHERE unique_id='" .. uid .. "'"
  150.     result = sql.Query(query)
  151.  
  152.     if result then
  153.         net.Start("SendPlayerEntreprises")
  154.             net.WriteTable( result )
  155.         net.Send( ply )
  156.     else
  157.         result = {}
  158.         net.Start("SendPlayerEntreprises")
  159.             net.WriteTable( result )
  160.         net.Send( ply )
  161.     end
  162. end
  163.  
  164.  
  165. --MODIFY
  166.  
  167.  
  168. --DELETE
  169. function RealEntprs_delentreprise( nom )
  170.     local query = "DELETE FROM RealEntprs_data WHERE nom='" ..nom.. "'"
  171.     local q = db:query( query )
  172.     function q:onError( err )
  173.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  174.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  175.         end
  176.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  177.     end
  178.     q:start()
  179.  
  180.     local query = "DELETE FROM RealEntprs_ventesdata WHERE nom='" ..nom.. "'"
  181.     local q = db:query( query )
  182.     function q:onError( err )
  183.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  184.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  185.         end
  186.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  187.     end
  188.     q:start()
  189.  
  190.     local query = "DELETE FROM RealEntprs_pnjsdata WHERE nom='" ..nom.. "'"
  191.     local q = db:query( query )
  192.     function q:onError( err )
  193.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  194.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  195.         end
  196.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  197.     end
  198.     q:start()
  199.  
  200.     local query = "DELETE FROM RealEntprs_produitsdata WHERE nom='" ..nom.. "'"
  201.     local q = db:query( query )
  202.     function q:onError( err )
  203.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  204.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  205.         end
  206.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  207.     end
  208.     q:start()
  209.  
  210. end
  211. function RealEntprs_delproduit( nom )
  212.     local query = "DELETE FROM RealEntprs_data WHERE nom='" ..nom.. "'"
  213.     local q = db:query( query )
  214.     function q:onError( err )
  215.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  216.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  217.         end
  218.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  219.     end
  220.     q:start()
  221.  
  222.     local query = "DELETE FROM RealEntprs_ventesdata WHERE nom='" ..nom.. "'"
  223.     local q = db:query( query )
  224.     function q:onError( err )
  225.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  226.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  227.         end
  228.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  229.     end
  230.     q:start()
  231.  
  232.     local query = "DELETE FROM RealEntprs_pnjsdata WHERE nom='" ..nom.. "'"
  233.     local q = db:query( query )
  234.     function q:onError( err )
  235.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  236.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  237.         end
  238.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  239.     end
  240.     q:start()
  241.  
  242.     local query = "DELETE FROM RealEntprs_produitsdata WHERE nom='" ..nom.. "'"
  243.     local q = db:query( query )
  244.     function q:onError( err )
  245.         if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  246.             timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
  247.         end
  248.         ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
  249.     end
  250.     q:start()
  251.  
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement