// // Created by Иван_Архипов on 17.11.2017. // #define UNICODE #define _UNICODE #include "Database.h" #include "Common/DatException.h" #include "BinaryData.h" #include "Common/CommonFunctions.h" #include namespace LOTRO_DAT { Database::Database() { db_ = nullptr; query_size_ = 0; } Database::Database(const std::string &filename) { query_size_ = 0; InitDatabase(filename.c_str()); } Database::~Database() { if (query_size_ != 0) ExecSql("COMMIT"); if (db_ != nullptr) { sqlite3_finalize(insert_text_); sqlite3_finalize(insert_binary_); sqlite3_close(db_); } } void Database::InitDatabase(const std::string &filename) { if (sqlite3_open(filename.c_str(), &db_) != SQLITE_OK) { sqlite3_close(db_); throw DatException("Bad Database::InitDatabase() - sqlite3_open returned an error..." , DATABASE_EXCEPTION); } ExecSql("PRAGMA synchronous = OFF"); ExecSql("PRAGMA count_changes = OFF"); ExecSql("PRAGMA journal_mode = MEMORY"); ExecSql("PRAGMA temp_store = MEMORY"); ExecSql("PRAGMA encoding = \"UTF-8\";"); ExecSql(CreateBinaryTableCommand_); ExecSql(CreateTextTableCommand_); sqlite3_prepare_v2(db_, InsertBinaryCommand_.c_str(), InsertBinaryCommand_.length(), &insert_binary_, nullptr); sqlite3_prepare_v2(db_, InsertTextCommand_.c_str(), InsertTextCommand_.length(), &insert_text_, nullptr); //sqlite3_prepare_v2(db_, GetTextCommand, 50ll * 1024ll * 1024ll, &get_text_, nullptr); //sqlite3_prepare_v2(db_, GetBinaryCommand, 50ll * 1024ll * 1024ll, &get_binary_, nullptr); } void Database::ExecSql(const std::string &sql) { char *error; if (sqlite3_exec(db_, sql.c_str(), nullptr, nullptr, &error) != SQLITE_OK) { fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_)); throw DatException((std::string("Bad Database::ExecSql() - unable to perform request") + std::string(sql)).c_str(), DATABASE_EXCEPTION); } } void Database::PushTextFile(long long file_id, long long gossip_id, const char16_t *text, const char *args, int dat_id) { if (query_size_ == 0) ExecSql("BEGIN TRANSACTION"); query_size_++; sqlite3_bind_int(insert_text_, 1, (int)file_id); sqlite3_bind_int(insert_text_, 2, (int)gossip_id); sqlite3_bind_text16(insert_text_, 3, text, -1, SQLITE_TRANSIENT); sqlite3_bind_text(insert_text_, 4, args, -1, SQLITE_TRANSIENT); if (sqlite3_step(insert_text_) != SQLITE_DONE) { fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_)); throw DatException((std::string("Bad Database::PushTextFile() - unable to perform push operation")).c_str(), DATABASE_EXCEPTION); } sqlite3_reset(insert_text_); if (query_size_ >= QUERY_MAX_SIZE) { ExecSql("COMMIT"); query_size_ = 0; } } //BinaryData &Database::GetTextFile(long long file_id) {} void Database::PushBinaryFile(long long file_id, const BinaryData &data) { if (query_size_ == 0) ExecSql("BEGIN TRANSACTION"); query_size_++; sqlite3_bind_int(insert_binary_, 1, (int)file_id); sqlite3_bind_blob(insert_binary_, 2, data.data(), data.size(), SQLITE_TRANSIENT); if (sqlite3_step(insert_binary_) != SQLITE_DONE) { fprintf(stderr, "SQLite3 error: %s\n", sqlite3_errmsg(db_)); throw DatException(std::string("Bad Database::PushTextFile() - unable to perform operation").c_str() , DATABASE_EXCEPTION); } sqlite3_reset(insert_binary_); if (query_size_ >= QUERY_MAX_SIZE) { ExecSql("COMMIT"); query_size_ = 0; } } //BinaryData &Database::GetBinaryFile(long long file_id) { // //} }