Database.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Created by Иван_Архипов on 17.11.2017.
  3. //
  4. #define UNICODE
  5. #define _UNICODE
  6. #include "Database.h"
  7. #include "DatException.h"
  8. #include "BinaryData.h"
  9. #include "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. ExecSql(CreateMetadataTableCommand_);
  43. sqlite3_prepare_v2(db_, InsertBinaryCommand_.c_str(), InsertBinaryCommand_.length(), &insert_binary_, nullptr);
  44. sqlite3_prepare_v2(db_, InsertTextCommand_.c_str(), InsertTextCommand_.length(), &insert_text_, nullptr);
  45. //sqlite3_prepare_v2(db_, GetTextCommand, 50ll * 1024ll * 1024ll, &get_text_, nullptr);
  46. //sqlite3_prepare_v2(db_, GetBinaryCommand, 50ll * 1024ll * 1024ll, &get_binary_, nullptr);
  47. }
  48. void Database::ExecSql(const std::string &sql) {
  49. char *error;
  50. if (sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &error) != SQLITE_OK) {
  51. fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_));
  52. throw DatException((std::string("Bad Database::ExecSql() - unable to perform request")
  53. + std::string(sql)).c_str(), DATABASE_EXCEPTION);
  54. }
  55. }
  56. void Database::PushTextFile(long long file_id, long long gossip_id, const char16_t *text, const char *args, int dat_id) {
  57. if (query_size_ == 0)
  58. ExecSql("BEGIN TRANSACTION");
  59. query_size_++;
  60. sqlite3_bind_int(insert_text_, 1, (int)file_id);
  61. sqlite3_bind_int(insert_text_, 2, (int)gossip_id);
  62. int a = sqlite3_bind_text16(insert_text_, 3, text, -1, SQLITE_TRANSIENT);
  63. int b = sqlite3_bind_text(insert_text_, 4, args, -1, SQLITE_TRANSIENT);
  64. if (sqlite3_step(insert_text_) != SQLITE_DONE) {
  65. fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_));
  66. throw DatException((std::string("Bad Database::PushTextFile() - unable to perform push operation")).c_str(),
  67. DATABASE_EXCEPTION);
  68. }
  69. sqlite3_reset(insert_text_);
  70. if (query_size_ >= QUERY_MAX_SIZE) {
  71. ExecSql("COMMIT");
  72. query_size_ = 0;
  73. }
  74. }
  75. //BinaryData &Database::GetTextFile(long long file_id) {}
  76. void Database::PushBinaryFile(long long file_id, const BinaryData &data) {
  77. if (query_size_ == 0)
  78. ExecSql("BEGIN TRANSACTION");
  79. query_size_++;
  80. sqlite3_bind_int(insert_binary_, 1, (int)file_id);
  81. sqlite3_bind_blob(insert_binary_, 2, data.data(), data.size(), SQLITE_TRANSIENT);
  82. if (sqlite3_step(insert_binary_) != SQLITE_DONE) {
  83. fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_));
  84. throw DatException(std::string("Bad Database::PushTextFile() - unable to perform operation").c_str()
  85. , DATABASE_EXCEPTION);
  86. }
  87. sqlite3_reset(insert_binary_);
  88. if (query_size_ >= QUERY_MAX_SIZE) {
  89. ExecSql("COMMIT");
  90. query_size_ = 0;
  91. }
  92. }
  93. //BinaryData &Database::GetBinaryFile(long long file_id) {
  94. //
  95. //}
  96. }