2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/SchedulerLocalStorage.h"
|
|
|
|
|
|
|
|
#include "td/db/SqliteDb.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class SqliteConnectionSafe {
|
|
|
|
public:
|
|
|
|
SqliteConnectionSafe() = default;
|
|
|
|
explicit SqliteConnectionSafe(string name, DbKey key = DbKey::empty())
|
|
|
|
: lsls_connection_([name = name, key = std::move(key)] {
|
2018-04-02 22:11:45 +02:00
|
|
|
auto r_db = SqliteDb::open_with_key(name, key);
|
|
|
|
if (r_db.is_error()) {
|
|
|
|
LOG(FATAL) << "Can't open database " << name << ": " << r_db.error();
|
|
|
|
}
|
|
|
|
auto db = r_db.move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
db.exec("PRAGMA synchronous=NORMAL").ensure();
|
|
|
|
db.exec("PRAGMA temp_store=MEMORY").ensure();
|
|
|
|
db.exec("PRAGMA secure_delete=1").ensure();
|
|
|
|
db.exec("PRAGMA recursive_triggers=1").ensure();
|
|
|
|
return db;
|
|
|
|
})
|
|
|
|
, name_(std::move(name)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SqliteDb &get() {
|
|
|
|
return lsls_connection_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
2018-12-13 23:48:36 +01:00
|
|
|
LOG(INFO) << "Close SQLite database " << tag("path", name_);
|
2018-12-31 20:04:05 +01:00
|
|
|
lsls_connection_.clear_values();
|
|
|
|
}
|
|
|
|
void close_and_destroy() {
|
|
|
|
close();
|
2018-12-13 23:48:36 +01:00
|
|
|
LOG(INFO) << "Destroy SQLite database " << tag("path", name_);
|
2018-12-31 20:04:05 +01:00
|
|
|
SqliteDb::destroy(name_).ignore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LazySchedulerLocalStorage<SqliteDb> lsls_connection_;
|
|
|
|
string name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|