2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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/utils/logging.h"
|
2020-08-14 16:48:43 +02:00
|
|
|
#include "td/utils/optional.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
struct sqlite3;
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace detail {
|
2018-07-18 03:30:29 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class RawSqliteDb {
|
|
|
|
public:
|
|
|
|
RawSqliteDb(sqlite3 *db, std::string path) : db_(db), path_(std::move(path)) {
|
|
|
|
}
|
|
|
|
RawSqliteDb(const RawSqliteDb &) = delete;
|
|
|
|
RawSqliteDb(RawSqliteDb &&) = delete;
|
|
|
|
RawSqliteDb &operator=(const RawSqliteDb &) = delete;
|
|
|
|
RawSqliteDb &operator=(RawSqliteDb &&) = delete;
|
|
|
|
~RawSqliteDb();
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
static void with_db_path(Slice main_path, F &&f) {
|
|
|
|
f(PSLICE() << main_path);
|
|
|
|
f(PSLICE() << main_path << "-journal");
|
|
|
|
f(PSLICE() << main_path << "-wal");
|
2019-09-16 20:18:36 +02:00
|
|
|
f(PSLICE() << main_path << "-shm");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
sqlite3 *db() {
|
|
|
|
return db_;
|
|
|
|
}
|
|
|
|
CSlice path() const {
|
|
|
|
return path_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status last_error();
|
2020-02-19 02:34:55 +01:00
|
|
|
static Status last_error(sqlite3 *db, CSlice path);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-09 13:31:45 +02:00
|
|
|
bool on_begin() {
|
|
|
|
begin_cnt_++;
|
|
|
|
return begin_cnt_ == 1;
|
|
|
|
}
|
|
|
|
Result<bool> on_commit() {
|
|
|
|
if (begin_cnt_ == 0) {
|
|
|
|
return Status::Error("No matching begin for commit");
|
|
|
|
}
|
|
|
|
begin_cnt_--;
|
|
|
|
return begin_cnt_ == 0;
|
|
|
|
}
|
|
|
|
|
2020-08-14 16:48:43 +02:00
|
|
|
void set_cipher_version(int32 cipher_version) {
|
|
|
|
cipher_version_ = cipher_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<int32> get_cipher_version() const {
|
|
|
|
return cipher_version_.copy();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
sqlite3 *db_;
|
|
|
|
std::string path_;
|
2019-08-09 13:31:45 +02:00
|
|
|
size_t begin_cnt_{0};
|
2020-08-14 16:48:43 +02:00
|
|
|
optional<int32> cipher_version_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-07-18 03:30:29 +02:00
|
|
|
|
|
|
|
} // namespace detail
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|