Database.cpp 4.1 KB

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