Advertisement
Guest User

Untitled

a guest
Jun 5th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. -- refractionservers.net, Chewgum - chewgumtj@gmail.com --
  2.  
  3. atlaschat.sql = {}
  4. atlaschat.sql.remote = {}
  5.  
  6. local queue = {}
  7. local firstTime = true
  8. local tableName = "atlaschat_remote"
  9.  
  10. ----------------------------------------------------------------------
  11. -- Purpose:
  12. -- Initializes the database object.
  13. ----------------------------------------------------------------------
  14.  
  15. function atlaschat.sql.Initialize(callback_success, callback_failed)
  16. local exists = sql.TableExists(tableName)
  17.  
  18. if (!exists) then
  19. sql.Query("CREATE TABLE " .. tableName .. "(address TEXT DEFAULT \"\" NOT NULL UNIQUE, port INTEGER DEFAULT 3306 NOT NULL UNIQUE, user TEXT DEFAULT \"\" NOT NULL UNIQUE, password TEXT DEFAULT \"\" NOT NULL UNIQUE, database TEXT DEFAULT \"atlaschat\" NOT NULL UNIQUE)")
  20. else
  21. -- Compatibility operations for sql tables are done here.
  22. if (!ATLASCHAT_VERSION_PREVIOUS or ATLASCHAT_VERSION_PREVIOUS != ATLASCHAT_VERSION) then
  23.  
  24. if (ATLASCHAT_VERSION_PREVIOUS) then
  25.  
  26. -- Insert the "database" field if we're upgrading from version 2.2.0
  27. if (ATLASCHAT_VERSION_PREVIOUS == 220) then
  28. sql.Query("ALTER TABLE " .. tableName .. " ADD database TEXT DEFAULT \"atlaschat\" NOT NULL")
  29. sql.Query("UPDATE " .. tableName .. " SET database = 'atlaschat'")
  30. end
  31. end
  32. end
  33. end
  34.  
  35. local data = sql.Query("SELECT address, port, user, password, database FROM " .. tableName)
  36.  
  37. if (data) then
  38. data = data[1]
  39.  
  40. atlaschat.sql.remote = data
  41.  
  42. if (!mysqloo) then
  43. local success, message = pcall(require, "mysqloo")
  44.  
  45. if (!success) then
  46. ErrorNoHalt("[atlaschat] Could not find the mysqloo module: " .. tostring(message) .. "\n")
  47. end
  48. end
  49.  
  50. if (mysqloo) then
  51. if (atlaschat.sql.remote.object) then atlaschat.sql.remote.object = nil end
  52.  
  53. local database = mysqloo.connect(data.address, data.user, data.password, data.database or "atlaschat", tonumber(data.port))
  54.  
  55. ServerLog("[atlaschat] Connecting to database...\n")
  56.  
  57. function database:onConnected()
  58. ServerLog("[atlaschat] Connection to database established.\n")
  59.  
  60. atlaschat.sql.remote.object = self
  61.  
  62. hook.Call("atlaschat.DatabaseConnected", nil, true, firstTime)
  63.  
  64. firstTime = false
  65.  
  66. if (callback_success) then
  67. callback_success()
  68. end
  69.  
  70. for k, info in pairs(queue) do
  71. local queryObject = atlaschat.sql.remote.object:query(info.query)
  72.  
  73. function queryObject:onSuccess(data)
  74. if (info.callback_success) then
  75. info.callback_success(data, self)
  76. end
  77. end
  78.  
  79. function queryObject:onError(message, queryString)
  80. ServerLog("[atlaschat] The query \"" .. queryString .. "\" failed: " .. message .. "\n")
  81.  
  82. if (info.callback_failed) then
  83. info.callback_failed(self, message, queryString)
  84. end
  85. end
  86.  
  87. queryObject:start()
  88. end
  89.  
  90. queue = {}
  91. end
  92.  
  93. function database:onConnectionFailed(message)
  94. ServerLog("[atlaschat] MySQL connection failed: " .. tostring(message) .. "\n")
  95.  
  96. if (callback_failed) then
  97. callback_failed(message)
  98. end
  99.  
  100. -- Fallback to SQLite.
  101. hook.Call("atlaschat.DatabaseConnected", nil, false, firstTime)
  102.  
  103. firstTime = false
  104. end
  105.  
  106. database:connect()
  107. else
  108. atlaschat.sql.remote = {}
  109.  
  110. ServerLog("[atlaschat] The module \"gmsv_mysqloo\" was not found. ( Failed to load? )\n")
  111. ServerLog("[atlaschat] Download and install the module: http://goo.gl/4A4ekH\n")
  112. end
  113. else
  114. hook.Call("atlaschat.DatabaseConnected", nil, false, firstTime)
  115.  
  116. firstTime = false
  117. end
  118. end
  119.  
  120. ----------------------------------------------------------------------
  121. -- Purpose:
  122. -- Initializes a query to the database.
  123. ----------------------------------------------------------------------
  124.  
  125. function atlaschat.sql.Query(query, callback_success, callback_failed)
  126. if (atlaschat.sql.remote.object) then
  127. local queryObject = atlaschat.sql.remote.object:query(query)
  128.  
  129. function queryObject:onSuccess(data)
  130. if (callback_success) then
  131. callback_success(data, self)
  132. end
  133. end
  134.  
  135. function queryObject:onError(message, queryString)
  136. ServerLog("[atlaschat] The query \"" .. queryString .. "\" failed: " .. message .. "\n")
  137.  
  138. local status = atlaschat.sql.remote.object:status()
  139.  
  140. if (status == mysqloo.DATABASE_NOT_CONNECTED) then
  141. table.insert(queue, {query = query, callback_success = callback_success, callback_failed = callback_failed})
  142.  
  143. atlaschat.sql.Initialize()
  144. end
  145.  
  146. if (callback_failed) then
  147. callback_failed(self, message, queryString)
  148. end
  149. end
  150.  
  151. queryObject:start()
  152. else
  153. local data = sql.Query(query)
  154.  
  155. if (data == false) then
  156. ServerLog("[atlaschat] The query \"" .. query .. "\" failed: " .. sql.LastError() .. "\n")
  157.  
  158. if (callback_failed) then
  159. callback_failed()
  160. end
  161. else
  162. if (callback_success) then
  163. callback_success(data)
  164. end
  165. end
  166. end
  167. end
  168.  
  169. ----------------------------------------------------------------------
  170. -- Purpose:
  171. -- Returns true if we're using mysql.
  172. ----------------------------------------------------------------------
  173.  
  174. function atlaschat.sql.IsRemote()
  175. return atlaschat.sql.remote.object != nil
  176. end
  177.  
  178. ----------------------------------------------------------------------
  179. -- Purpose:
  180. -- Sends & changes information for mysql.
  181. ----------------------------------------------------------------------
  182.  
  183. util.AddNetworkString("atlaschat.myin")
  184.  
  185. net.Receive("atlaschat.myin", function(bits, player)
  186. local isAdmin = player:IsSuperAdmin()
  187.  
  188. if (isAdmin) then
  189. local option = util.tobool(net.ReadBit())
  190.  
  191. if (option) then
  192. local status = atlaschat.sql.remote.object and atlaschat.sql.remote.object:status() or -1
  193. local address = atlaschat.sql.remote.address or ""
  194. local port = tonumber(atlaschat.sql.remote.port) or 3306
  195. local username = atlaschat.sql.remote.user or ""
  196. local database = atlaschat.sql.remote.database or "atlaschat"
  197.  
  198. net.Start("atlaschat.myin")
  199. net.WriteInt(status, 8)
  200. net.WriteString("")
  201. net.WriteString(address)
  202. net.WriteUInt(port, 16)
  203. net.WriteString(username)
  204. net.WriteString(database)
  205. net.Send(player)
  206. else
  207. local address = net.ReadString()
  208. local port = net.ReadUInt(16)
  209. local username = net.ReadString()
  210. local password = net.ReadString()
  211. local database = net.ReadString()
  212.  
  213. if (atlaschat.sql.remote.address) then
  214. sql.Query("UPDATE " .. tableName .. " SET address = " .. sql.SQLStr(address) .. ", port = " .. port .. ", user = " .. sql.SQLStr(username) .. ", password = " .. sql.SQLStr(password) .. ", database = " .. sql.SQLStr(database))
  215. else
  216. sql.Query("INSERT INTO " .. tableName .. "(address, port, user, password, database) VALUES(" .. sql.SQLStr(address) .. ", " .. port .. ", " .. sql.SQLStr(username) .. ", " .. sql.SQLStr(password) .. ", " .. sql.SQLStr(database) .. ")")
  217. end
  218.  
  219. if (!mysqloo) then
  220. local success, message = pcall(require, "mysqloo")
  221.  
  222. if (!success) then
  223. ErrorNoHalt("[atlaschat] Could not find the mysqloo module: " .. tostring(message) .. "\n")
  224. end
  225. end
  226.  
  227. net.Start("atlaschat.myin")
  228. net.WriteInt(mysqloo.DATABASE_CONNECTING, 8)
  229. net.WriteString("")
  230. net.WriteString(address)
  231. net.WriteUInt(port, 16)
  232. net.WriteString(username)
  233. net.WriteString(database)
  234. net.Send(player)
  235.  
  236. -- Reconnect with the new information.
  237. atlaschat.sql.Initialize(function()
  238. net.Start("atlaschat.myin")
  239. net.WriteInt(mysqloo.DATABASE_CONNECTED, 8)
  240. net.WriteString("")
  241. net.WriteString(address)
  242. net.WriteUInt(port, 16)
  243. net.WriteString(username)
  244. net.WriteString(database)
  245. net.Send(player)
  246. end,
  247.  
  248. function(message)
  249. net.Start("atlaschat.myin")
  250. net.WriteInt(mysqloo.DATABASE_NOT_CONNECTED, 8)
  251. net.WriteString(message)
  252. net.WriteString(address)
  253. net.WriteUInt(port, 16)
  254. net.WriteString(username)
  255. net.WriteString(database)
  256. net.Send(player)
  257. end)
  258. end
  259. end
  260. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement