Database.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Created by Иван_Архипов on 17.11.2017.
  3. //
  4. #define UNICODE
  5. #define _UNICODE
  6. #include "Database.h"
  7. #include "Common/DatException.h"
  8. #include "BinaryData.h"
  9. #include "Common/CommonFunctions.h"
  10. #include <cstring>
  11. namespace LOTRO_DAT {
  12. Database::Database() {
  13. db_ = nullptr;
  14. query_size_ = 0;
  15. }
  16. Database::Database(const std::string &filename) {
  17. query_size_ = 0;
  18. InitDatabase(filename.c_str());
  19. }
  20. Database::~Database() {
  21. if (query_size_ != 0)
  22. ExecSql("COMMIT");
  23. if (db_ != nullptr) {
  24. sqlite3_finalize(insert_text_);
  25. sqlite3_finalize(insert_binary_);
  26. sqlite3_close(db_);
  27. }
  28. }
  29. void Database::InitDatabase(const std::string &filename) {
  30. if (sqlite3_open(filename.c_str(), &db_) != SQLITE_OK) {
  31. sqlite3_close(db_);
  32. throw DatException("Bad Database::InitDatabase() - sqlite3_open returned an error..."
  33. , DATABASE_EXCEPTION);
  34. }
  35. ExecSql("PRAGMA synchronous = OFF");
  36. ExecSql("PRAGMA count_changes = OFF");
  37. ExecSql("PRAGMA journal_mode = MEMORY");
  38. ExecSql("PRAGMA temp_store = MEMORY");
  39. ExecSql("PRAGMA encoding = \"UTF-8\";");
  40. ExecSql(CreateBinaryTableCommand_);
  41. ExecSql(CreateTextTableCommand_);
  42. sqlite3_prepare_v2(db_, InsertBinaryCommand_.c_str(), InsertBinaryCommand_.length(), &insert_binary_, nullptr);
  43. sqlite3_prepare_v2(db_, InsertTextCommand_.c_str(), InsertTextCommand_.length(), &insert_text_, nullptr);
  44. //sqlite3_prepare_v2(db_, GetTextCommand, 50ll * 1024ll * 1024ll, &get_text_, nullptr);
  45. //sqlite3_prepare_v2(db_, GetBinaryCommand, 50ll * 1024ll * 1024ll, &get_binary_, nullptr);
  46. }
  47. void Database::ExecSql(const std::string &sql) {
  48. char *error;
  49. if (sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &error) != SQLITE_OK) {
  50. fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_));
  51. throw DatException((std::string("Bad Database::ExecSql() - unable to perform request")
  52. + std::string(sql)).c_str(), DATABASE_EXCEPTION);
  53. }
  54. }
  55. void Database::PushTextFile(long long file_id, long long gossip_id, const char16_t *text, const char *args, int dat_id) {
  56. if (query_size_ == 0)
  57. ExecSql("BEGIN TRANSACTION");
  58. query_size_++;
  59. sqlite3_bind_int(insert_text_, 1, (int)file_id);
  60. sqlite3_bind_int(insert_text_, 2, (int)gossip_id);
  61. sqlite3_bind_text16(insert_text_, 3, text, -1, SQLITE_TRANSIENT);
  62. sqlite3_bind_text(insert_text_, 4, args, -1, SQLITE_TRANSIENT);
  63. if (sqlite3_step(insert_text_) != SQLITE_DONE) {
  64. fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_));
  65. throw DatException((std::string("Bad Database::PushTextFile() - unable to perform push operation")).c_str(),
  66. DATABASE_EXCEPTION);
  67. }
  68. sqlite3_reset(insert_text_);
  69. if (query_size_ >= QUERY_MAX_SIZE) {
  70. ExecSql("COMMIT");
  71. query_size_ = 0;
  72. }
  73. }
  74. //BinaryData &Database::GetTextFile(long long file_id) {}
  75. void Database::PushBinaryFile(long long file_id, const BinaryData &data) {
  76. if (query_size_ == 0)
  77. ExecSql("BEGIN TRANSACTION");
  78. query_size_++;
  79. sqlite3_bind_int(insert_binary_, 1, (int)file_id);
  80. sqlite3_bind_blob(insert_binary_, 2, data.data(), data.size(), SQLITE_TRANSIENT);
  81. if (sqlite3_step(insert_binary_) != SQLITE_DONE) {
  82. fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_));
  83. throw DatException(std::string("Bad Database::PushTextFile() - unable to perform operation").c_str()
  84. , DATABASE_EXCEPTION);
  85. }
  86. sqlite3_reset(insert_binary_);
  87. if (query_size_ >= QUERY_MAX_SIZE) {
  88. ExecSql("COMMIT");
  89. query_size_ = 0;
  90. }
  91. }
  92. //BinaryData &Database::GetBinaryFile(long long file_id) {
  93. //
  94. //}
  95. }