Advertisement
Guest User

Untitled

a guest
Sep 4th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. _GM = GM or GAMEMODE
  2.  
  3. require("tmysql4")
  4.  
  5. _GM.MYSQL = {}
  6. _GM.MYSQL.Host = "removed cus i can"
  7. _GM.MYSQL.Port = blah
  8. _GM.MYSQL.User = "blah"
  9. _GM.MYSQL.Pass = "no"
  10. _GM.MYSQL.DB = "blah"
  11.  
  12.  
  13. if CLIENT_MULTI_STATEMENTS then
  14. _GM.ErrorHandler:DebugPrint( 1, "TMYSQL4 MODULE LOADED SUCCESSFULLY!", Color( 0, 255, 0 ) )
  15. else
  16. _GM.ErrorHandler:DebugPrint( 1, "TMYSQL4 DID NOT LOAD PROPERLY! IS THE MODULE INSTALLED?", Color( 255, 0, 0 ) )
  17. return
  18. end
  19.  
  20. function _GM.MYSQL:Connect()
  21. DB, ERR = tmysql.Create( _GM.MYSQL.Host, _GM.MYSQL.User, _GM.MYSQL.Pass, _GM.MYSQL.DB, _GM.MYSQL.Port )
  22.  
  23. _cur_status, _cur_err = DB:Connect()
  24.  
  25. if _cur_err or ERR then
  26. _GM.ErrorHandler:DebugPrint( 1, _cur_err or ERR, Color( 255, 0, 0 ) )
  27. end
  28.  
  29. if _cur_status then
  30. _GM.ErrorHandler:DebugPrint( 1, "Gamemode Database successfully connected: " .. tostring( _GM.MYSQL.Host ), Color( 0, 255, 0 ) )
  31. else
  32. _GM.ErrorHandler:DebugPrint( 1, "Gamemode Database failed to connect to MySQL.", Color( 255, 0, 0 ) )
  33. end
  34. end
  35.  
  36. _GM.MYSQL:Connect()
  37.  
  38. function _GM.MYSQL:Disconnect()
  39. DB:Disconnect()
  40. _GM.ErrorHandler:DebugPrint( 1, "Database has been disconnected", Color( 255, 255, 0 ) )
  41. -- ¯\_(ツ)_/¯
  42. end
  43.  
  44. function _GM.MYSQL:Query( _String, _Callback )
  45.  
  46. if DB then
  47. DB:Query( _String, _Callback )
  48. else
  49. _GM.ErrorHandler:DebugPrint( 1, "No database detected. wtf?", Color( 255, 0, 0 ) )
  50. end
  51. end
  52.  
  53. function _GM.MYSQL:DebugQuery( _String, _Callback )
  54.  
  55. local _NiceString = string.format( _String )
  56. local _NiceString = ''
  57.  
  58. if DB then
  59. DB:Query( _String, onCompleted )
  60. else
  61. _GM.ErrorHandler:DebugPrint( 1, "No database detected. wtf?", Color( 255, 0, 0 ) )
  62. end
  63.  
  64. function onCompleted( _Results )
  65.  
  66. if _Callback then
  67. _Callback( _Results )
  68. end
  69.  
  70. if _Results[1].status then
  71. _GM.ErrorHandler:DebugPrint( 1, "Query " .. _NiceString .. " ran successfully", Color( 255, 255, 0 ) )
  72. else
  73. _GM.ErrorHandler:DebugPrint( 1, "Query " .. _NiceString .. " failed!\n" .. _Results[1].error, Color( 255, 0, 0 ) )
  74. return
  75. end
  76.  
  77. PrintTable( _Results[1].data )
  78. end
  79. end
  80.  
  81. function _GM.MYSQL:GetActiveConnections()
  82. return tmysql.GetTable()
  83. end
  84.  
  85. function _GM.MYSQL:Escape( _String )
  86. if not DB then return end
  87.  
  88. return DB:Escape( _String )
  89. end
  90.  
  91. function _GM.MYSQL:CreateTable( _Name, _Arguments )
  92. --let's make sure we don't accidently write over a pre-existing table... ouch.
  93. _GM.MYSQL:DebugQuery(" CREATE TABLE IF NOT EXISTS `" .. _Name .. "` ( " .. _Arguments .. " ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB;", function( _Returned ) end)
  94. end
  95.  
  96. function _GM.MYSQL:CreateRPTables()
  97.  
  98. _GM.MYSQL:Query( "SELECT COUNT(*) FROM _players", function( _return )
  99.  
  100. if not _return[1].data or table.Count( _return[1].data ) == 0 then
  101. _GM.ErrorHandler:DebugPrint( 1, "Creating RP tables (They don't seem to exist yet)", Color( 255, 255, 0 ) )
  102.  
  103. _GM.MYSQL:CreateTable( '_players', [[
  104. `_steamid` CHAR(32) NOT NULL,
  105. `_first_name` VARCHAR(32) NOT NULL,
  106. `_last_name` VARCHAR(32) NOT NULL,
  107. `_cash` BIGINT NOT NULL,
  108. `_model` TEXT(32) NOT NULL,
  109. `_xp` SMALLINT NOT NULL,
  110. `_rank` TEXT(32) NOT NULL,
  111. `_color` VARCHAR(32) NOT NULL DEFAULT '1;1;1',
  112. `_physgun` VARCHAR(32) NOT NULL DEFAULT '1;1;1',
  113. UNIQUE INDEX `steamid` (`_steamid`)
  114. ]])
  115.  
  116. _GM.ErrorHandler:DebugPrint( 1, "Query finished! -- Player RP tables created", Color( 255, 0, 0 ) )
  117. end
  118. end)
  119. end
  120.  
  121. _GM.MYSQL:CreateRPTables()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement