Support recusrive transactions in sqlite usage
GitOrigin-RevId: 1afd836ebd0c8b1cc79c86c9ef548deeb3f5adaa
This commit is contained in:
parent
ef5c2ea722
commit
cfb3d7bda4
@ -136,10 +136,18 @@ Status SqliteDb::set_user_version(int32 version) {
|
||||
}
|
||||
|
||||
Status SqliteDb::begin_transaction() {
|
||||
if (raw_->on_begin()) {
|
||||
return exec("BEGIN");
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status SqliteDb::commit_transaction() {
|
||||
TRY_RESULT(need_commit, raw_->on_commit());
|
||||
if (need_commit) {
|
||||
return exec("COMMIT");
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
bool SqliteDb::is_encrypted() {
|
||||
|
@ -44,9 +44,22 @@ class RawSqliteDb {
|
||||
Status last_error();
|
||||
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:
|
||||
sqlite3 *db_;
|
||||
std::string path_;
|
||||
size_t begin_cnt_{0};
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
Reference in New Issue
Block a user