This repository has been archived on 2020-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
tdlib-fork/tddb/td/db/SqliteConnectionSafe.cpp
levlam 811c4ed95c Add database path to most database errors.
GitOrigin-RevId: 469033593d39a542d517d328de546d24c76c77b1
2020-02-19 04:34:55 +03:00

52 lines
1.6 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// 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)
//
#include "td/db/SqliteConnectionSafe.h"
#include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/port/Stat.h"
namespace td {
SqliteConnectionSafe::SqliteConnectionSafe(string path, DbKey key)
: path_(std::move(path)), lsls_connection_([path = path_, key = std::move(key)] {
auto r_db = SqliteDb::open_with_key(path, key);
if (r_db.is_error()) {
auto r_stat = stat(path);
if (r_stat.is_error()) {
LOG(FATAL) << "Can't open database (" << r_stat.error() << "): " << r_db.error();
} else {
LOG(FATAL) << "Can't open database of size " << r_stat.ok().size_ << ": " << r_db.error();
}
}
auto db = r_db.move_as_ok();
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;
}) {
}
SqliteDb &SqliteConnectionSafe::get() {
return lsls_connection_.get();
}
void SqliteConnectionSafe::close() {
LOG(INFO) << "Close SQLite database " << tag("path", path_);
lsls_connection_.clear_values();
}
void SqliteConnectionSafe::close_and_destroy() {
close();
LOG(INFO) << "Destroy SQLite database " << tag("path", path_);
SqliteDb::destroy(path_).ignore();
}
} // namespace td