Advertisement
Guest User

Untitled

a guest
Feb 1st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.42 KB | None | 0 0
  1. --[[
  2.     Purpose: Provides a wrapper for database interaction so switching from mysqloo or tmysql4
  3.     can be quickly done without much hassle. There are also some utility functions for the
  4.     database.
  5. --]]
  6.  
  7. nut.db = nut.db or {}
  8.  
  9. local dbModule = nut.config.dbModule
  10. require(nut.config.dbModule)
  11.  
  12. -- SQLite doesn't need a module!
  13. --if (dbModule != "sqlite") then
  14.    
  15. --else
  16. --  local SQL_CURR_VERSION = 0
  17. --  local SQL_CONFIRMING = 0
  18. --  SQL_LOCAL_VERSION = SQL_LOCAL_VERSION or tonumber(file.Read("nutscript/db.txt", "DATA") or 0)
  19. --
  20. --  -- But it does require the script to setup a table.
  21. --  local QUERY_CREATE = [[
  22. --  CREATE TABLE ]]..nut.config.dbTable..[[ (
  23. --      steamid int,
  24. --      charname varchar(60),
  25. --      DisplayColor varchar(60),
  26. --      description varchar(240),
  27. --      gender varchar(6),
  28. --      money int,
  29. --      inv mediumtext,
  30. --      faction int,
  31. --      id int,
  32. --      chardata mediumtext,
  33. --      rpschema varchar(16),
  34. --      model tinytext
  35. --  );
  36. --  ]]
  37. --  local QUERY_CREATE_PLAYERS = [[
  38. --  CREATE TABLE ]]..nut.config.dbPlyTable..[[ (
  39. --      steamid int,
  40. --      whitelists tinytext,
  41. --      plydata mediumtext,
  42. --      rpschema tinytext
  43. --  );
  44. --  ]]
  45. --
  46. --  local function initializeTables(recreate)
  47. --      local success = true
  48. --
  49. --      if (recreate) then
  50. --          sql.Query("DROP TABLE "..nut.config.dbTable)
  51. --          sql.Query("DROP TABLE "..nut.config.dbPlyTable)
  52. --      end
  53. --
  54. --      if (!sql.TableExists(nut.config.dbTable)) then
  55. --          local result = sql.Query(QUERY_CREATE)
  56. --
  57. --          if (result == false) then
  58. --              MsgC(Color(255, 0, 0), "NutScript could not create characters table!\n")
  59. --              print(sql.LastError())
  60. --              success = false
  61. --          else
  62. --              MsgC(Color(0, 255, 0), "NutScript has created characters table.\n")
  63. --          end
  64. --      else
  65. --          success = false
  66. --      end
  67. --
  68. --      if (!sql.TableExists(nut.config.dbPlyTable)) then
  69. --          local result = sql.Query(QUERY_CREATE_PLAYERS)
  70. --
  71. --          if (result == false) then
  72. --              MsgC(Color(255, 0, 0), "NutScript could not create players table!\n")
  73. --              print(sql.LastError())
  74. --              success = false
  75. --          else
  76. --              MsgC(Color(0, 255, 0), "NutScript has created players table.\n")
  77. --          end
  78. --      else
  79. --          success = false
  80. --      end
  81. --
  82. --      if (success) then
  83. --          MsgC(Color(0, 255, 0), "NutScript has created database tables correctly!\n")
  84. --          file.Write("nutscript/db.txt", SQL_CURR_VERSION)
  85. --          SQL_LOCAL_VERSION = SQL_CURR_VERSION
  86. --      end
  87. --  end
  88. --
  89. --  initializeTables()
  90. --
  91. --  if (SQL_LOCAL_VERSION != SQL_CURR_VERSION) then
  92. --      MsgC(Color(255, 255, 0), "\nNutScript has had its database tables updated.\n")
  93. --      MsgC(Color(255, 255, 0), "You will need to enter the command: "); MsgN("nut_recreatedb")
  94. --      MsgC(Color(255, 255, 0), "You will need to enter the command: "); MsgN("nut_recreatedb")
  95. --      MsgC(Color(255, 255, 0), "Updating the tables will remove ALL pre-existing data.\n")
  96. --      MsgC(Color(255, 255, 0), "Not updating the tables MAY CAUSE SAVING/LOADED ERRORS!\n\n")
  97. --  end
  98. --
  99. --  concommand.Add("nut_recreatedb", function(client, command, arguments)
  100. --      if (!IsValid(client) or client:IsListenServerHost()) then
  101. --          if ((!SQL_CONFIRMING or SQL_CONFIRMING < CurTime()) and SQL_LOCAL_VERSION == SQL_CURR_VERSION) then
  102. --              MsgN("NutScript has verified that the table versions match.")
  103. --              MsgN("If you would like to recreate the tables, type the command again within 10 seconds.")
  104. --              SQL_CONFIRMING = CurTime() + 10
  105. --
  106. --              return
  107. --          end
  108. --
  109. --          initializeTables(true)
  110. --          SQL_CONFIRMING = 0
  111. --      end
  112. --  end)
  113. --end
  114. if (!mysql.connection) then
  115.     mysql:Connect(nut.config.dbHost, nut.config.dbUser, nut.config.dbPassword, nut.config.dbDatabase, nut.config.dbPort)
  116. end
  117.  
  118. hook.Add("DatabaseConnected", "nut.db.connected", function()
  119.     print("Connected to mysql via mysqloo using Mr. Brightsides mysql wrapper.")
  120. end);
  121.  
  122. timer.Create("nut.db.think", 1, 0, function()
  123.     mysql:Think()
  124. end)
  125.  
  126. --[[
  127.     Purpose: Connects to the database using the configuration values from sv_config.lua
  128.     and connects using the defined modules from the config file.
  129. --]]
  130. function nut.db.Connect()
  131.     if (dbModule == "sqlite") then
  132.         if (nut.db.sqliteInit == true) then return end
  133.        
  134.         print("NutScript using SQLite for database.")
  135.         nut.db.sqliteInit = true
  136.  
  137.         return
  138.     end
  139.  
  140.     if (nut.db.object) then
  141.         return
  142.     end
  143.  
  144.     local hostname = nut.config.dbHost
  145.     local username = nut.config.dbUser
  146.     local password = nut.config.dbPassword
  147.     local database = nut.config.dbDatabase
  148.     local port = nut.config.dbPort
  149.  
  150.     if (dbModule == "tmysql4") then
  151.         local fault;
  152.  
  153.         nut.db.object, fault = tmysql.initialize(hostname, username, password, database, port)
  154.  
  155.         if (!fault) then
  156.             print("NutScript has connected to the database via tmysql4.");
  157.         else
  158.             print("NutScript could not connect to the database!")
  159.             print(fault)
  160.         end
  161.     else
  162.         nut.db.object = mysqloo.connect(hostname, username, password, database, port)
  163.         nut.db.object.onConnected = function()
  164.             print("NutScript has connected to the database via mysqloo.")
  165.         end
  166.         nut.db.object.onConnectionFailed = function(_, fault)
  167.             print("NutScript could not connect to the database!")
  168.             print(fault)
  169.         end
  170.         nut.db.object:connect()
  171.     end
  172. end
  173.  
  174. nut.db.Connect()
  175.  
  176. --[[
  177.     Purpose: An alias to the current module's function to escape a string.
  178. --]]
  179. function nut.db.Escape(value)
  180.     return mysql:Escape(value)
  181. end
  182.  
  183. --[[
  184.     Purpose: Makes a query using either tmysql4 or mysqloo and runs the callback
  185.     function passing the data.
  186. --]]
  187. function nut.db.Query(query, callback)
  188.     if (dbModule == "tmysql4") then
  189.         nut.db.object:Query(query, function(result, status, fault)
  190.             if (status == false) then
  191.                 print("Query Error: "..query)
  192.                 print(fault)
  193.             elseif (callback) then
  194.                 callback(result[1], result)
  195.             end
  196.         end, QUERY_FLAG_ASSOC)
  197.     elseif (dbModule == "mysqloo") then
  198.         local result = nut.db.object:query(query)
  199.  
  200.         if (result) then
  201.             if (callback) then
  202.                 result.onSuccess = function(_, data)
  203.                     callback(data[1], data)
  204.                 end
  205.             end
  206.             result.onError = function(_, fault)
  207.                 print("Query Error: "..query);
  208.                 print(fault)
  209.             end
  210.             result:start()
  211.         end
  212.     else
  213.         local data = sql.Query(query)
  214.  
  215.         if (data == false) then
  216.             print("Query Error: "..query)
  217.             print(sql.LastError())
  218.         else
  219.             local value = {}
  220.  
  221.             if (data and data[1]) then
  222.                 value = data[1]
  223.  
  224.                 value.faction = tonumber(value.faction)
  225.                 value.id = tonumber(value.id)
  226.             end
  227.  
  228.             if (callback) then
  229.                 callback(value, data or {})
  230.             end
  231.         end
  232.     end
  233.     mysql:Queue(query, callback)
  234. end
  235.  
  236. --[[
  237.     Purpose: Inserts a table matching the key with a field in the database and a value
  238.     as the value for the field.
  239. --]]
  240. function nut.db.InsertTable(data, callback, dbTable)
  241.     local query = "INSERT INTO "..(dbTable or nut.config.dbTable).." ("
  242.  
  243.     for k, v in pairs(data) do
  244.         query = query..k..", "
  245.     end
  246.  
  247.     query = string.sub(query, 1, -3)..") VALUES ("
  248.  
  249.     for k, v in pairs(data) do
  250.         if (type(k) == "string" and k != "steamid") then
  251.             if (type(v) == "table") then
  252.                 v = von.serialize(v)
  253.             end
  254.  
  255.             -- SQLite doesn't play nice with quotations.
  256.             if (type(v) == "string") then
  257.                 if (dbModule == "sqlite") then
  258.                     v = nut.db.Escape(v)
  259.                 else
  260.                     v = "'"..nut.db.Escape(v).."'"
  261.                 end
  262.             end
  263.         end
  264.  
  265.         query = query..v..", "
  266.     end
  267.  
  268.     query = string.sub(query, 1, -3)..")"
  269.     nut.db.Query(query, callback)
  270. end
  271.  
  272. --[[
  273.     Purpose: Similar to insert table, it will update the values that are passed through
  274.     the second argument given the condition is true.
  275. --]]
  276. function nut.db.UpdateTable(condition, data, dbTable)
  277.     local query = "UPDATE "..(dbTable or nut.config.dbTable).." SET "
  278.  
  279.     for k, v in pairs(data) do
  280.         query = query..nut.db.Escape(k).." = "
  281.  
  282.         if (type(k) == "string" and k != "steamid") then
  283.             if (type(v) == "table") then
  284.                 v = von.serialize(v)
  285.             end
  286.  
  287.             -- SQLite doesn't play nice with quotations.
  288.             if (type(v) == "string") then
  289.                 if (dbModule == "sqlite") then
  290.                     v = nut.db.Escape(v)
  291.                 else
  292.                     v = "'"..nut.db.Escape(v).."'"
  293.                 end
  294.             end
  295.         end
  296.  
  297.         query = query..v..", "
  298.     end
  299.  
  300.     query = string.sub(query, 1, -3).." WHERE "..condition
  301.     nut.db.Query(query, callback)
  302. end
  303.  
  304. --[[
  305.     Purpose: Returns the values of given fields that are seperated by a comma and passes
  306.     them into the callback function given the condition provided is true.
  307. --]]
  308. function nut.db.FetchTable(condition, tables, callback, dbTable)
  309.     local query = "SELECT "..tables.." FROM "..(dbTable or nut.config.dbTable).." WHERE "..condition
  310.  
  311.     nut.db.Query(query, callback)
  312. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement