Support recusrive transactions in sqlite usage

GitOrigin-RevId: 1afd836ebd0c8b1cc79c86c9ef548deeb3f5adaa
This commit is contained in:
Arseny Smirnov 2019-08-09 14:31:45 +03:00
parent ef5c2ea722
commit cfb3d7bda4
2 changed files with 23 additions and 2 deletions

View File

@ -136,10 +136,18 @@ Status SqliteDb::set_user_version(int32 version) {
} }
Status SqliteDb::begin_transaction() { Status SqliteDb::begin_transaction() {
return exec("BEGIN"); if (raw_->on_begin()) {
return exec("BEGIN");
}
return Status::OK();
} }
Status SqliteDb::commit_transaction() { Status SqliteDb::commit_transaction() {
return exec("COMMIT"); TRY_RESULT(need_commit, raw_->on_commit());
if (need_commit) {
return exec("COMMIT");
}
return Status::OK();
} }
bool SqliteDb::is_encrypted() { bool SqliteDb::is_encrypted() {

View File

@ -44,9 +44,22 @@ class RawSqliteDb {
Status last_error(); Status last_error();
static Status last_error(sqlite3 *db); static Status last_error(sqlite3 *db);
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;
}
private: private:
sqlite3 *db_; sqlite3 *db_;
std::string path_; std::string path_;
size_t begin_cnt_{0};
}; };
} // namespace detail } // namespace detail