Advertisement
Guest User

Untitled

a guest
May 30th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. -- D3A MySQL Controls
  2.  
  3. require("mysql")
  4.  
  5. local ip = "localhost"
  6. local user = "root"
  7. local pass = "********"
  8. local db = "d3a 4"
  9.  
  10. D3A.MySQL.Connection = nil
  11.  
  12. local function WriteError(e, q)
  13. local fn = "D3A/Logs/MySQLError.txt"
  14. local entr
  15.  
  16. if (!q) then
  17. entr = Format("Time of error: %s\n%s\n\n", os.date(), e)
  18. else
  19. entr = Format("Time of error: %s\nQuery: %s\n%s\n\n", os.date(), q, e)
  20. end
  21.  
  22. if (file.Exists(fn)) then
  23. file.Write(fn, entr .. file.Read(fn))
  24. else
  25. file.Write(fn, entr)
  26. end
  27. end
  28.  
  29. function D3A.MySQL.Query(q)
  30. if (!D3A.MySQL.Connection) then
  31. print("D3A MYSQL :: Connecting to database")
  32. local db, error = mysql.connect(ip, user, pass, db)
  33.  
  34. if (db == 0) then
  35. print("D3A MYSQL :: " .. error)
  36. WriteError(error)
  37.  
  38. D3A.MySQL.Connection = false
  39. else
  40. print("D3A MYSQL :: Database connection successful")
  41. D3A.MySQL.Connection = db
  42.  
  43. timer.Create("D3A.MySQL.TimeOut", 45, 1, function()
  44. if (D3A.MySQL.Connection) then
  45. print("D3A MYSQL :: Killing the database connection (inactivity)")
  46.  
  47. mysql.disconnect(D3A.MySQL.Connection)
  48. D3A.MySQL.Connection = nil
  49. end
  50. end)
  51. end
  52. end
  53.  
  54. if (!q) then
  55. print("D3A MYSQL :: Cannot run query - No query given.")
  56. return {}
  57. end
  58.  
  59. if (D3A.MySQL.Connection) then
  60. local res, success, error = mysql.query(D3A.MySQL.Connection, mysql.escape(D3A.MySQL.Connection, q))
  61.  
  62. if (!success) then
  63. print("D3A MYSQL :: " .. error)
  64. print("Given query: " .. q)
  65. WriteError(error, q)
  66. return {}
  67. end
  68.  
  69. return res
  70. else
  71. print("D3A MYSQL :: Cannot run query - No database connection.")
  72. print("Given query: " .. q)
  73. return {}
  74. end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement