aboutsummaryrefslogtreecommitdiffhomepage
path: root/tddb/td/db/detail/RawSqliteDb.h
blob: f60eb64e65df0f01f83a57f830bc1307d001f821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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/optional.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"

struct tdsqlite3;

namespace td {
namespace detail {

class RawSqliteDb {
 public:
  RawSqliteDb(tdsqlite3 *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");
    f(PSLICE() << main_path << "-shm");
  }
  static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;

  tdsqlite3 *db() {
    return db_;
  }
  CSlice path() const {
    return path_;
  }

  Status last_error();
  static Status last_error(tdsqlite3 *db, CSlice path);

  static bool was_any_database_destroyed();

  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;
  }

  void set_cipher_version(int32 cipher_version) {
    cipher_version_ = cipher_version;
  }

  optional<int32> get_cipher_version() const {
    return cipher_version_.copy();
  }

 private:
  tdsqlite3 *db_;
  std::string path_;
  size_t begin_cnt_{0};
  optional<int32> cipher_version_;
};

}  // namespace detail
}  // namespace td