123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //
- // 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 <cstring>
- namespace LOTRO_DAT {
- Database::Database() {
- db_ = nullptr;
- }
- Database::Database(const std::string &filename) {
- InitDatabase(filename.c_str());
- }
- Database::~Database() {
- ExecSql("COMMIT TRANSACTION");
- if (db_ != nullptr) {
- sqlite3_finalize(insert_request_);
- 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(CreateTableCommand_);
- sqlite3_prepare_v2(db_, InsertFileCommand_.c_str(), InsertFileCommand_.length(), &insert_request_, nullptr);
- ExecSql("BEGIN TRANSACTION");
- }
- 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::PushFile(const BinaryData &binary_data, const std::u16string &text_data, const std::string &options) {
- sqlite3_bind_blob(insert_request_, 1, binary_data.data(), binary_data.size(), SQLITE_TRANSIENT);
- sqlite3_bind_text16(insert_request_, 2, text_data.c_str(), -1, SQLITE_TRANSIENT);
- sqlite3_bind_text(insert_request_, 3, options.c_str(), -1, SQLITE_TRANSIENT);
- if (sqlite3_step(insert_request_) != 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_request_);
- }
- }
|