Guest User

Untitled

a guest
Apr 17th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. pcall(require, "mysqloo")
  2.  
  3. local mysqloo_loaded = mysqloo ~= nil -- Checking the result of "require" doesn't seem to work
  4.  
  5. local DATABASE = {}
  6.  
  7. DATABASE.KEYWORD_AUTOINCREMENT = "auto_increment"
  8. DATABASE.QUERY_LISTTABLES = "show tables;"
  9.  
  10. function DATABASE.IsAvailable() -- Can this database type be used at all?
  11. return mysqloo_loaded
  12. end
  13.  
  14. function DATABASE:IsConnected()
  15. return self.db ~= nil
  16. end
  17.  
  18. function DATABASE:RawDB()
  19. if not self:IsConnected() then
  20. self:Connect(self.Config)
  21. return self.db
  22. end
  23. return self.db
  24. end
  25.  
  26. function DATABASE:Connect(details)
  27. if not details.user or not details.password or not details.database then
  28. return false, "MysqlOO connection requires username, password and database"
  29. end
  30. local db = mysqloo.connect( details.host or "localhost",
  31. details.user,
  32. details.password,
  33. details.database,
  34. details.port or 3306,
  35. details.socket or "")
  36. self.db = db
  37.  
  38. function db:onConnected()
  39. self.db = db
  40. end
  41.  
  42. local toerr
  43. function db:onConnectionFailed( err )
  44. toerr = err
  45. self.db = nil
  46. end
  47.  
  48. db:connect()
  49. db:wait()
  50.  
  51. return self:IsConnected(), toerr
  52. end
  53.  
  54. function DATABASE:RawQuery(onSuccess, onError, fquery)
  55. local db = self:RawDB()
  56. if not db then
  57. FDB.Error("RawDB not available!")
  58. end
  59.  
  60. local fdbself = self -- store self here, because we cant access 'self' from onSuccess
  61.  
  62. local query = db:query(fquery)
  63. function query:onSuccess(data)
  64. fdbself.LastAffectedRows = query:affectedRows()
  65. fdbself.LastAutoIncrement = query:lastInsert()
  66. fdbself.LastRowCount = #data
  67.  
  68. if FDB.IsDebug() then -- We double check for debug mode here because string operations are expensive-ish
  69. FDB.Debug("Query succeeded! AffectedRows " .. tostring(fdbself:GetAffectedRows()) .. " InsertedId " .. tostring(fdbself:GetInsertedId()) ..
  70. " RowCount " .. tostring(fdbself:GetRowCount()))
  71. end
  72. if onSuccess then
  73. onSuccess(data)
  74. end
  75. end
  76.  
  77. function query:onError(err, sql)
  78. -- TODO check for "Mysql server has gone away" error, in which case a reconnect is needed
  79. FDB.Warn("Query failed! SQL: " .. sql .. ". Err: " .. err)
  80. if onError then
  81. onError(err, sql)
  82. end
  83. end
  84.  
  85. query:start()
  86.  
  87. return query
  88. end
  89.  
  90. function DATABASE:Wait(queryobj)
  91. if not queryobj then return false end
  92.  
  93. queryobj:wait()
  94. return true
  95. end
  96.  
  97. function DATABASE:GetInsertedId()
  98. return self.LastAutoIncrement
  99. end
  100.  
  101. function DATABASE:GetAffectedRows()
  102. return self.LastAffectedRows or 0
  103. end
  104.  
  105. function DATABASE:GetRowCount()
  106. return self.LastRowCount
  107. end
  108.  
  109. -- Transaction stuff
  110.  
  111. -- TODO, we could fake transactions by adding queries to a table until commit/fallback
  112. function DATABASE:StartTransaction()
  113. return self:BlockingQuery("START TRANSACTION;") ~= false
  114. end
  115.  
  116. function DATABASE:Commit()
  117. return self:BlockingQuery("COMMIT;") ~= false
  118. end
  119.  
  120. function DATABASE:Rollback()
  121. return self:BlockingQuery("ROLLBACK;") ~= false
  122. end
  123.  
  124. FDB.RegisterDatabase("mysqloo", DATABASE)
Advertisement
Add Comment
Please, Sign In to add comment