Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. --[[
  2.     Developers:
  3.         • .WhiteBlue (szymon.lua@gmail.com)
  4.  
  5.     Copyright (c) 2018 .WhiteBlue (szymon.lua@gmail.com)
  6.  
  7.     Note (EN): You can not copy and share this code without my permission.
  8.     Note (PL): Nie możesz kopiować i udostępniać tego kodu bez mojej zgody.
  9. ]]
  10.  
  11. -- Variables
  12. local connection = {
  13.     element = nil,
  14.  
  15.     -- Database
  16.     database = {
  17.         ['host'] = '',
  18.  
  19.         ['name'] = '',
  20.  
  21.         ['username'] = '',
  22.         ['password'] = '',
  23.     },
  24. }
  25.  
  26. -- Functions
  27. function database_get(type, ...)
  28.     if not connection['element'] or not {...} then return nil end
  29.  
  30.     local prepare = dbPrepareString(connection['element'], ...)
  31.  
  32.     if prepare then
  33.         local query = dbQuery(connection['element'], prepare)
  34.         local result, num_affected_rows, last_insert_id = dbPoll(query, -1)
  35.  
  36.         if result and num_affected_rows and last_insert_id then
  37.             if type then
  38.                 if type == 'one' then
  39.                     return result[1], num_affected_rows, last_insert_id
  40.  
  41.                 elseif type == 'more' then
  42.                     return result, num_affected_rows, last_insert_id
  43.                 end
  44.             end
  45.         end
  46.     end
  47. end
  48.  
  49. function database_set(...)
  50.     if not connection['element'] or not {...} then return nil end
  51.  
  52.     local prepare = dbPrepareString(connection['element'], ...)
  53.  
  54.     if prepare then
  55.         local query = dbExec(connection['element'], prepare)
  56.  
  57.         if query then
  58.             return true
  59.         else
  60.             return nil
  61.         end
  62.     end
  63. end
  64.  
  65. addEventHandler('onResourceStart', resourceRoot,
  66.     function()
  67.         if not connection['database'] then return nil end
  68.  
  69.         if not connection['database']['host'] or connection['database']['host'] == 0 then
  70.             outputDebugString('(og-connection/connection.lua) ✗ Nie znaleziono hosta bazy danych.')
  71.  
  72.             return nil
  73.         end
  74.  
  75.         if not connection['database']['name'] or connection['database']['name'] == 0 then
  76.             outputDebugString('(og-connection/connection.lua) ✗ Nie znaleziono nazwy bazy danych.')
  77.  
  78.             return nil
  79.         end
  80.  
  81.         if not connection['database']['username'] or connection['database']['username'] == 0 then
  82.             outputDebugString('(og-connection/connection.lua) ✗ Nie znaleziono użytkownika bazy danych.')
  83.  
  84.             return nil
  85.         end
  86.  
  87.         if not connection['database']['password'] or connection['database']['password'] == 0 then
  88.             outputDebugString('(og-connection/connection.lua) ✗ Nie znaleziono hasła użytkownika bazy danych.')
  89.  
  90.             return nil
  91.         end
  92.  
  93.         -- Connect
  94.         local connect = dbConnect('mysql', 'host='.. connection['database']['host'] ..';dbname='.. connection['database']['name'] ..';charset=utf8', connection['database']['username'], connection['database']['password'], 'share=1')
  95.  
  96.         if connect then
  97.             connection['element'] = connect
  98.  
  99.             outputDebugString('(og-connection/connection.lua) ✓ Nawiązano połączenie z bazą danych.')
  100.         else
  101.             connection['element'] = nil
  102.  
  103.             outputDebugString('(og-connection/connection.lua) ✗ Nie nawiązano połączenia z bazą danych.')
  104.         end
  105.     end
  106. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement