2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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/db/DbKey.h"
|
|
|
|
#include "td/db/SqliteStatement.h"
|
|
|
|
|
|
|
|
#include "td/db/detail/RawSqliteDb.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"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
struct sqlite3;
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class SqliteDb {
|
|
|
|
public:
|
|
|
|
SqliteDb() = default;
|
|
|
|
SqliteDb(SqliteDb &&) = default;
|
|
|
|
SqliteDb &operator=(SqliteDb &&) = default;
|
|
|
|
SqliteDb(const SqliteDb &) = delete;
|
|
|
|
SqliteDb &operator=(const SqliteDb &) = delete;
|
|
|
|
~SqliteDb();
|
|
|
|
|
|
|
|
// dangerous
|
|
|
|
SqliteDb clone() const {
|
|
|
|
return SqliteDb(raw_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return !raw_;
|
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
*this = SqliteDb();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status exec(CSlice cmd) TD_WARN_UNUSED_RESULT;
|
|
|
|
Result<bool> has_table(Slice table);
|
|
|
|
Result<string> get_pragma(Slice name);
|
2020-08-12 21:36:46 +02:00
|
|
|
Result<string> get_pragma_string(Slice name);
|
2021-10-07 12:18:00 +02:00
|
|
|
|
|
|
|
Status begin_read_transaction() TD_WARN_UNUSED_RESULT;
|
|
|
|
Status begin_write_transaction() TD_WARN_UNUSED_RESULT;
|
2018-10-26 16:11:20 +02:00
|
|
|
Status commit_transaction() TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Result<int32> user_version();
|
2018-10-26 16:11:20 +02:00
|
|
|
Status set_user_version(int32 version) TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
void trace(bool flag);
|
|
|
|
|
|
|
|
static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2021-09-22 16:05:52 +02:00
|
|
|
// we can't change the key on the fly, so static functions are more than enough
|
2021-09-22 18:04:56 +02:00
|
|
|
static Result<SqliteDb> open_with_key(CSlice path, bool allow_creation, const DbKey &db_key,
|
|
|
|
optional<int32> cipher_version = {});
|
|
|
|
static Result<SqliteDb> change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
|
|
|
|
const DbKey &old_db_key);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status last_error();
|
|
|
|
|
|
|
|
sqlite3 *get_native() const {
|
|
|
|
return raw_->db();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<SqliteStatement> get_statement(CSlice statement) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
static void with_db_path(Slice main_path, F &&f) {
|
|
|
|
detail::RawSqliteDb::with_db_path(main_path, f);
|
|
|
|
}
|
|
|
|
|
2020-08-14 18:53:26 +02:00
|
|
|
optional<int32> get_cipher_version() const;
|
2020-08-14 16:48:43 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
explicit SqliteDb(std::shared_ptr<detail::RawSqliteDb> raw) : raw_(std::move(raw)) {
|
|
|
|
}
|
|
|
|
std::shared_ptr<detail::RawSqliteDb> raw_;
|
2020-03-28 22:12:51 +01:00
|
|
|
bool enable_logging_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-22 18:04:56 +02:00
|
|
|
Status init(CSlice path, bool allow_creation) TD_WARN_UNUSED_RESULT;
|
2021-09-22 16:05:52 +02:00
|
|
|
|
2020-01-27 13:12:22 +01:00
|
|
|
Status check_encryption();
|
2021-09-22 18:04:56 +02:00
|
|
|
static Result<SqliteDb> do_open_with_key(CSlice path, bool allow_creation, const DbKey &db_key, int32 cipher_version);
|
2020-08-14 16:48:43 +02:00
|
|
|
void set_cipher_version(int32 cipher_version);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-04-26 00:47:25 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|