Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. require('mysqloo')
  2. require('dprint')
  3. local tostring, string, unpack, type
  4. do
  5. local _obj_0 = _G
  6. tostring, string, unpack, type = _obj_0.tostring, _obj_0.string, _obj_0.unpack, _obj_0.type
  7. end
  8. local databases = { }
  9. local Db
  10. do
  11. local _base_0 = {
  12. connect_new = function(self, host, username, password, database, port)
  13. if port == nil then
  14. port = '3306'
  15. end
  16. if not (host) then
  17. error('must provide host')
  18. end
  19. if not (username) then
  20. error('must provide username')
  21. end
  22. if not (password) then
  23. error('must provide password')
  24. end
  25. if not (database) then
  26. error('must provide database')
  27. end
  28. self.host = host
  29. self.username = username
  30. self.datbase = database
  31. self.port = port
  32. self.hash = string.format('%s:%s@%X:%s', host, port, util.CRC(username .. '-' .. password), database)
  33. if databases[self.hash] then
  34. self.db = databases[self.hash]
  35. return dprint('recycled database connection with hashid: ' .. self.hash)
  36. else
  37. print(host, type(host))
  38. print(username, type(username))
  39. print(password, type(password))
  40. print(database, type(database))
  41. print(port, type(port))
  42. self.db = mysqloo.connect(host, username, password, database, port)
  43. databases[self.hash] = self.db
  44. self.db.onConnected = function(self)
  45. return MsgC(Color(0, 255, 0), 'pMySQL connected successfully.\n')
  46. end
  47. self.db.onConnectionFailed = function(self, err)
  48. MsgC(Color(255, 0, 0), 'pMySQL connection failed\n')
  49. return error(err)
  50. end
  51. dprint('started new db connection with hash: ' .. self.hash)
  52. return self:connect()
  53. end
  54. end,
  55. connect_resume = function(self, db)
  56. self.hash = db.hash
  57. self.host = db.host
  58. self.username = db.username
  59. self.database = db.database
  60. self.port = db.port
  61. self.db = db.db
  62. end,
  63. connect = function(self)
  64. MsgC(Color(0, 255, 0), 'pMySQL connecting to database\n')
  65. local start = SysTime()
  66. self.db:connect()
  67. self.db:wait()
  68. return MsgC(Color(155, 155, 155), 'pMySQL connect operation complete. took: ' .. (SysTime() - start) .. ' seconds\n')
  69. end,
  70. query = function(self, sqlstr, callback)
  71. local query = self.db:query(sqlstr)
  72. query.onSuccess = function(self, data)
  73. if callback then
  74. return callback(data)
  75. end
  76. end
  77. query.onError = function(_, err)
  78. if self.db:status() == mysqloo.DATABASE_NOT_CONNECTED then
  79. self:connect()
  80. end
  81. dprint('QUERY FAILED!')
  82. dprint('SQL: ' .. sqlstr)
  83. dprint('ERR: ' .. err)
  84. if callback then
  85. return callback(nil, err)
  86. end
  87. end
  88. query:setOption(mysqloo.OPTION_INTERPRET_DATA)
  89. query:start()
  90. return query
  91. end,
  92. query_ex = function(self, sqlstr, options, callback)
  93. local query_buffer = { }
  94. local last = 0
  95. local count = 1
  96. local mysql = self.db
  97. while true do
  98. local next = sqlstr:find('?', last + 1)
  99. if not next then
  100. break
  101. end
  102. query_buffer[#query_buffer + 1] = sqlstr:sub(last + 1, next - 1)
  103. query_buffer[#query_buffer + 1] = options[count] ~= nil and self:escape(options[count]) or error('option ' .. count .. ' is nil, expected value')
  104. count = count + 1
  105. last = next
  106. end
  107. query_buffer[#query_buffer + 1] = sqlstr:sub(last + 1)
  108. local query_str = table.concat(query_buffer)
  109. return self:query(query_str, callback)
  110. end,
  111. query_sync = function(self, sqlstr, options)
  112. if options == nil then
  113. options = { }
  114. end
  115. local _data, _err
  116. local query = self:query_ex(sqlstr, options, function(data, err)
  117. _data, _err = data, err
  118. end)
  119. query:wait()
  120. return _data, _err
  121. end,
  122. escape = function(self, str)
  123. if type(str) == 'string' then
  124. return self.db:escape(str)
  125. else
  126. return self.db:escape(tostring(str))
  127. end
  128. end
  129. }
  130. _base_0.__index = _base_0
  131. local _class_0 = setmetatable({
  132. __init = function(self, host, username, password, database, port)
  133. if type(host) == 'string' then
  134. return self:connect_new(host, username, password, database, port, socket, flags)
  135. elseif type(host) == 'table' and host.db and tostring(host.db):find('Database') then
  136. return self:connect_resume(host)
  137. else
  138. return error('could not initialize database object')
  139. end
  140. end,
  141. __base = _base_0,
  142. __name = "Db"
  143. }, {
  144. __index = _base_0,
  145. __call = function(cls, ...)
  146. local _self_0 = setmetatable({}, _base_0)
  147. cls.__init(_self_0, ...)
  148. return _self_0
  149. end
  150. })
  151. _base_0.__class = _class_0
  152. Db = _class_0
  153. end
  154. pmysql = {
  155. newdb = function(...)
  156. return Db(...)
  157. end,
  158. Db = Db
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement