Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RealEntprsDB = {}
- function RealEntprsDB_Init()
- use_mysqloo = file.Exists("bin/gmsv_mysqloo_*.dll", "LUA")
- if use_mysqloo then
- require("mysqloo")
- RealEntprsDB_ConnectToDatabase()
- timer.Simple(60, RealEntprsDB_Init )
- else
- ServerLog("RealEntprs: Erreur modul mysqloo")
- end
- end
- hook.Add( "Initialize", "RealEntprsDB_Init", RealEntprsDB_Init )
- function RealEntprsDB_Format( str )
- if not str then return "NULL" end
- return string.format( "%q", str )
- end
- function Escape( str )
- if not RealEntprsDB.db then
- Msg( "Non connecté à la DB.\n" )
- return
- end
- if not str then return end
- local esc = RealEntprsDB.db:escape( str )
- if not esc then
- return nil
- end
- return "'" .. esc .. "'"
- end
- function RealEntprsDB_ConnectToDatabase()
- if not IsValid(RealEntprsDB.db) then
- RealEntprsDB.db = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT )
- RealEntprsDB.db:connect()
- return
- end
- if RealEntprsDB.db:status() ~= 0 then
- RealEntprsDB.db = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT )
- RealEntprsDB.db:connect()
- return
- end
- end
- function RealEntprs_tbl_exist()
- print(RealEntprsDB.db:status())
- if RealEntprsDB.db:status() ~= 0 then
- ServerLog("RealEntprs: Database non initialisée. ...\n")
- timer.Simple(0.5, function() RealEntprs_tbl_exist() end )
- return
- end
- local db = RealEntprsDB.db
- print(db:status())
- local query = "CREATE TABLE IF NOT EXISTS RealEntprs_data ( unique_id VARCHAR(255), nom TEXT, description TEXT, date_creation VARCHAR(255), total_ca INTEGER )"
- local q = RealEntprsDB.db:query( query )
- function q:onSuccess( data )
- ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_data. Elle existe ou elle vient d'être créée!\n")
- end
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- ServerLog("RealEntprs: DB non connectée, on recommence...\n")
- timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
- end
- ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
- end
- q:start()
- local query = "CREATE TABLE IF NOT EXISTS RealEntprs_ventesdata ( unique_id VARCHAR(255), nom TEXT, produit TEXT, prix INTEGER )"
- local q = RealEntprsDB.db:query( query )
- function q:onSuccess( data )
- ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_ventesdata. Elle existe ou elle vient d'être créée!\n")
- end
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- ServerLog("RealEntprs: DB non connectée, on recommence...\n")
- timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
- end
- ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
- end
- q:start()
- local query = "CREATE TABLE IF NOT EXISTS RealEntprs_pnjsdata ( nom TEXT, title_pnj TEXT, description_pnj TEXT, position_pnj VARCHAR(255), model_pnj TEXT )"
- local q = RealEntprsDB.db:query( query )
- function q:onSuccess( data )
- ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_pnjsdata. Elle existe ou elle vient d'être créée!\n")
- end
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- ServerLog("RealEntprs: DB non connectée, on recommence...\n")
- timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
- end
- ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
- end
- q:start()
- 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 )"
- local q = RealEntprsDB.db:query( query )
- function q:onSuccess( data )
- ServerLog("RealEntprs: Check de l'existance de la table RealEntprs_produitsdata. Elle existe ou elle vient d'être créée!\n")
- end
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- ServerLog("RealEntprs: DB non connectée, on recommence...\n")
- timer.Simple( 3, function() RealEntprsDB_ConnectToDatabase() RealEntprs_tbl_exist() end )
- end
- ServerLog("RealEntprs: SQL Erreur - " .. err .. " sur le query: " .. query .. "\n")
- end
- q:start()
- end
- --Gestion des données
- --ADD
- function RealEntprs_addentreprise( uid, nom, description )
- nom = sql.SQLStr( nom )
- date = os.date()
- local query = "INSERT INTO RealEntprs_data VALUES (NULL, '" ..uid.. "', " ..nom.. ", " ..description.. ",'" ..date.. "', '0')"
- result = sql.Query(query)
- end
- function RealEntprs_addpnj( nom, titre, description, position, model )
- nom = sql.SQLStr( nom )
- date = os.date()
- local query = "INSERT INTO RealEntprs_pnjsdata VALUES (NULL, " ..nom.. ", " ..titre.. ", " ..description.. ", " ..model.. ")"
- result = sql.Query(query)
- end
- function RealEntprs_addproduit( nom, titre_pnj, nom_produit, description, prix )
- nom = sql.SQLStr( nom )
- date = os.date()
- local query = "INSERT INTO RealEntprs_produitsdata VALUES (NULL, " ..nom.. ", " ..titre_pnj.. ", " ..nom_produit.. ", " ..description.. ", '" .. prix.. "'')"
- result = sql.Query(query)
- end
- --GET
- function RealEntprs_getentreprises( uid )
- local uid = target_ply:SteamID64()
- local query = "SELECT * FROM RealEntprs_data WHERE unique_id='" .. uid .. "'"
- result = sql.Query(query)
- if result then
- net.Start("SendPlayerEntreprises")
- net.WriteTable( result )
- net.Send( ply )
- else
- result = {}
- net.Start("SendPlayerEntreprises")
- net.WriteTable( result )
- net.Send( ply )
- end
- end
- --MODIFY
- --DELETE
- function RealEntprs_delentreprise( nom )
- local query = "DELETE FROM RealEntprs_data WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- local query = "DELETE FROM RealEntprs_ventesdata WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- local query = "DELETE FROM RealEntprs_pnjsdata WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- local query = "DELETE FROM RealEntprs_produitsdata WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- end
- function RealEntprs_delproduit( nom )
- local query = "DELETE FROM RealEntprs_data WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- local query = "DELETE FROM RealEntprs_ventesdata WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- local query = "DELETE FROM RealEntprs_pnjsdata WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- local query = "DELETE FROM RealEntprs_produitsdata WHERE nom='" ..nom.. "'"
- local q = db:query( query )
- function q:onError( err )
- if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
- timer.Simple( 3, function() AWarnDB_ConnectToDatabase() end )
- end
- ServerLog("AWarn: SQL Erreur - " .. err .. " sur: " .. query .. "\n")
- end
- q:start()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement