database.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. -- Private function!
  2. -- Create db:exec wrapper for error reporting.
  3. local function db_exec(db, stmt)
  4. if db:exec(stmt) ~= email.sql.OK then
  5. local msg = db:errmsg()
  6. minetest.log("error", "SQLite3 ERROR: " .. msg)
  7. local admin = utility.get_first_available_admin()
  8. if admin then
  9. minetest.chat_send_player(admin:get_player_name(), "# Server: Error from SQL! " .. msg)
  10. end
  11. end
  12. end
  13. function email.db_exec(stmt)
  14. assert(email.db)
  15. db_exec(email.db, stmt)
  16. end
  17. -- Load all emails for player from database.
  18. function email.load_inbox(db, name)
  19. assert(name)
  20. assert(db)
  21. local stmt = db:prepare([[
  22. SELECT sender,subject,message,date,number FROM email WHERE name = ?;
  23. ]])
  24. assert(stmt, db:errmsg())
  25. stmt:bind(1, name)
  26. local r = stmt:step()
  27. while r == email.sql.ROW do
  28. r = stmt:step()
  29. end
  30. assert(r == email.sql.DONE)
  31. local mail = {}
  32. for row in stmt:nrows() do
  33. assert(type(row.sender) == "string")
  34. assert(type(row.date) == "string")
  35. assert(type(row.subject) == "string")
  36. assert(type(row.message) == "string")
  37. assert(type(row.number) == "number")
  38. mail[#mail+1] = {
  39. from = row.sender,
  40. date = row.date,
  41. sub = row.subject,
  42. msg = row.message,
  43. rng = row.number,
  44. }
  45. end
  46. local r4 = stmt:finalize()
  47. assert(r4 == email.sql.OK)
  48. return mail
  49. end
  50. -- Delete emails from player's inbox.
  51. function email.delete_mails(db, name, mails)
  52. for k, v in ipairs(mails) do
  53. local rng = v.rng
  54. assert(type(rng) == "number")
  55. local stmt = db:prepare([[
  56. DELETE FROM email WHERE name = ? AND number = ?;
  57. ]])
  58. local result = stmt:bind_values(name, rng)
  59. assert(result == email.sql.OK)
  60. local r3 = stmt:step()
  61. assert(r3 == email.sql.DONE)
  62. local r4 = stmt:finalize()
  63. assert(r4 == email.sql.OK)
  64. end
  65. end
  66. -- Store email in player's inbox.
  67. function email.store_mail(db, name, mail)
  68. local sender = mail.from -- Cannot be substituted.
  69. local subject = mail.sub
  70. local message = mail.msg
  71. local number = mail.rng -- Cannot be substituted.
  72. local date = mail.date -- Cannot be substituted.
  73. -- Don't transfer corrupt data.
  74. if name and sender and number and message and subject and date then
  75. assert(type(name) == "string")
  76. assert(type(sender) == "string")
  77. assert(type(number) == "number")
  78. assert(type(subject) == "string")
  79. assert(type(message) == "string")
  80. assert(type(date) == "string")
  81. local stmt = db:prepare([[
  82. INSERT INTO email
  83. (name, sender, date, subject, message, number)
  84. VALUES (?, ?, ?, ?, ?, ?);
  85. ]])
  86. local result = stmt:bind_values(
  87. name, sender, date, subject, message, number)
  88. assert(result == email.sql.OK)
  89. local r3 = stmt:step()
  90. assert(r3 == email.sql.DONE)
  91. local r4 = stmt:finalize()
  92. assert(r4 == email.sql.OK)
  93. end
  94. end
  95. -- For one-time database format conversion!
  96. --[===[
  97. function email.translate_database()
  98. local db = email.sql.open(email.database)
  99. if not db then return end
  100. db_exec(db, [[ DROP TABLE IF EXISTS email; ]])
  101. db_exec(db, [[ CREATE TABLE email (
  102. name TEXT,
  103. sender TEXT,
  104. date TEXT,
  105. subject TEXT,
  106. message TEXT,
  107. number INTEGER
  108. ); ]])
  109. db_exec(db, [[ BEGIN TRANSACTION; ]])
  110. if email.inboxes then
  111. for name, inbox in pairs(email.inboxes) do
  112. for _, mail in ipairs(inbox) do
  113. email.store_mail(db, name, mail)
  114. end
  115. end
  116. end
  117. db_exec(db, [[ COMMIT; ]])
  118. db:close()
  119. end
  120. --]===]