2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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)
|
|
|
|
//
|
|
|
|
#include "td/db/detail/RawSqliteDb.h"
|
|
|
|
|
|
|
|
#include "sqlite/sqlite3.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/port/path.h"
|
2018-06-07 21:56:03 +02:00
|
|
|
#include "td/utils/port/Stat.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
2018-06-26 01:43:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status RawSqliteDb::last_error(sqlite3 *db) {
|
|
|
|
return Status::Error(Slice(sqlite3_errmsg(db)));
|
|
|
|
}
|
2018-06-26 01:43:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status RawSqliteDb::destroy(Slice path) {
|
2018-06-07 21:56:03 +02:00
|
|
|
Status error;
|
|
|
|
with_db_path(path, [&](auto path) {
|
|
|
|
unlink(path).ignore();
|
|
|
|
if (!stat(path).is_error()) {
|
|
|
|
error = Status::Error(PSLICE() << "Failed to delete " << tag("path", path));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return error;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-26 01:43:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status RawSqliteDb::last_error() {
|
|
|
|
//If database was corrupted, try to delete it.
|
|
|
|
auto code = sqlite3_errcode(db_);
|
|
|
|
if (code == SQLITE_CORRUPT) {
|
|
|
|
destroy(path_).ignore();
|
|
|
|
}
|
|
|
|
|
|
|
|
return last_error(db_);
|
|
|
|
}
|
2018-06-26 01:43:11 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
RawSqliteDb::~RawSqliteDb() {
|
|
|
|
auto rc = sqlite3_close(db_);
|
|
|
|
LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace td
|