Skip to content

Commit 76309d1

Browse files
committed
Less connection reference counting for transactions
1 parent 84e5f09 commit 76309d1

File tree

2 files changed

+64
-52
lines changed

2 files changed

+64
-52
lines changed

dev/storage_base.h

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,39 @@ namespace sqlite_orm {
4646
limit_accessor limit;
4747

4848
transaction_guard_t transaction_guard() {
49-
this->begin_transaction();
50-
return {this->get_connection(),
51-
std::bind(&storage_base::commit, this),
52-
std::bind(&storage_base::rollback, this)};
49+
auto connection = this->get_connection();
50+
sqlite3* db = connection.get();
51+
this->executor.perform_void_exec(db, "BEGIN TRANSACTION");
52+
return {std::move(connection),
53+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
54+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
5355
}
5456

5557
transaction_guard_t deferred_transaction_guard() {
56-
this->begin_deferred_transaction();
57-
return {this->get_connection(),
58-
std::bind(&storage_base::commit, this),
59-
std::bind(&storage_base::rollback, this)};
58+
auto connection = this->get_connection();
59+
sqlite3* db = connection.get();
60+
this->executor.perform_void_exec(db, "BEGIN DEFERRED TRANSACTION");
61+
return {std::move(connection),
62+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
63+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
6064
}
6165

6266
transaction_guard_t immediate_transaction_guard() {
63-
this->begin_immediate_transaction();
64-
return {this->get_connection(),
65-
std::bind(&storage_base::commit, this),
66-
std::bind(&storage_base::rollback, this)};
67+
auto connection = this->get_connection();
68+
sqlite3* db = connection.get();
69+
this->executor.perform_void_exec(db, "BEGIN IMMEDIATE TRANSACTION");
70+
return {std::move(connection),
71+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
72+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
6773
}
6874

6975
transaction_guard_t exclusive_transaction_guard() {
70-
this->begin_exclusive_transaction();
71-
return {this->get_connection(),
72-
std::bind(&storage_base::commit, this),
73-
std::bind(&storage_base::rollback, this)};
76+
auto connection = this->get_connection();
77+
sqlite3* db = connection.get();
78+
this->executor.perform_void_exec(db, "BEGIN EXCLUSIVE TRANSACTION");
79+
return {std::move(connection),
80+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
81+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
7482
}
7583

7684
/**
@@ -594,19 +602,23 @@ namespace sqlite_orm {
594602
}
595603

596604
void begin_transaction() {
597-
this->begin_transaction_internal("BEGIN TRANSACTION");
605+
sqlite3* db = this->connection->retain();
606+
this->executor.perform_void_exec(db, "BEGIN TRANSACTION");
598607
}
599608

600609
void begin_deferred_transaction() {
601-
this->begin_transaction_internal("BEGIN DEFERRED TRANSACTION");
610+
sqlite3* db = this->connection->retain();
611+
this->executor.perform_void_exec(db, "BEGIN DEFERRED TRANSACTION");
602612
}
603613

604614
void begin_immediate_transaction() {
605-
this->begin_transaction_internal("BEGIN IMMEDIATE TRANSACTION");
615+
sqlite3* db = this->connection->retain();
616+
this->executor.perform_void_exec(db, "BEGIN IMMEDIATE TRANSACTION");
606617
}
607618

608619
void begin_exclusive_transaction() {
609-
this->begin_transaction_internal("BEGIN EXCLUSIVE TRANSACTION");
620+
sqlite3* db = this->connection->retain();
621+
this->executor.perform_void_exec(db, "BEGIN EXCLUSIVE TRANSACTION");
610622
}
611623

612624
void commit() {
@@ -768,12 +780,6 @@ namespace sqlite_orm {
768780
}
769781
}
770782

771-
void begin_transaction_internal(const std::string& sql) {
772-
this->connection->retain();
773-
sqlite3* db = this->connection->get();
774-
this->executor.perform_void_exec(db, sql.c_str());
775-
}
776-
777783
connection_ref get_connection() {
778784
return {*this->connection};
779785
}

include/sqlite_orm/sqlite_orm.h

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18780,31 +18780,39 @@ namespace sqlite_orm {
1878018780
limit_accessor limit;
1878118781

1878218782
transaction_guard_t transaction_guard() {
18783-
this->begin_transaction();
18784-
return {this->get_connection(),
18785-
std::bind(&storage_base::commit, this),
18786-
std::bind(&storage_base::rollback, this)};
18783+
auto connection = this->get_connection();
18784+
sqlite3* db = connection.get();
18785+
this->executor.perform_void_exec(db, "BEGIN TRANSACTION");
18786+
return {std::move(connection),
18787+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
18788+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
1878718789
}
1878818790

1878918791
transaction_guard_t deferred_transaction_guard() {
18790-
this->begin_deferred_transaction();
18791-
return {this->get_connection(),
18792-
std::bind(&storage_base::commit, this),
18793-
std::bind(&storage_base::rollback, this)};
18792+
auto connection = this->get_connection();
18793+
sqlite3* db = connection.get();
18794+
this->executor.perform_void_exec(db, "BEGIN DEFERRED TRANSACTION");
18795+
return {std::move(connection),
18796+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
18797+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
1879418798
}
1879518799

1879618800
transaction_guard_t immediate_transaction_guard() {
18797-
this->begin_immediate_transaction();
18798-
return {this->get_connection(),
18799-
std::bind(&storage_base::commit, this),
18800-
std::bind(&storage_base::rollback, this)};
18801+
auto connection = this->get_connection();
18802+
sqlite3* db = connection.get();
18803+
this->executor.perform_void_exec(db, "BEGIN IMMEDIATE TRANSACTION");
18804+
return {std::move(connection),
18805+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
18806+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
1880118807
}
1880218808

1880318809
transaction_guard_t exclusive_transaction_guard() {
18804-
this->begin_exclusive_transaction();
18805-
return {this->get_connection(),
18806-
std::bind(&storage_base::commit, this),
18807-
std::bind(&storage_base::rollback, this)};
18810+
auto connection = this->get_connection();
18811+
sqlite3* db = connection.get();
18812+
this->executor.perform_void_exec(db, "BEGIN EXCLUSIVE TRANSACTION");
18813+
return {std::move(connection),
18814+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "COMMIT"),
18815+
std::bind(&sqlite_executor::perform_void_exec, &executor, db, "ROLLBACK")};
1880818816
}
1880918817

1881018818
/**
@@ -19328,19 +19336,23 @@ namespace sqlite_orm {
1932819336
}
1932919337

1933019338
void begin_transaction() {
19331-
this->begin_transaction_internal("BEGIN TRANSACTION");
19339+
sqlite3* db = this->connection->retain();
19340+
this->executor.perform_void_exec(db, "BEGIN TRANSACTION");
1933219341
}
1933319342

1933419343
void begin_deferred_transaction() {
19335-
this->begin_transaction_internal("BEGIN DEFERRED TRANSACTION");
19344+
sqlite3* db = this->connection->retain();
19345+
this->executor.perform_void_exec(db, "BEGIN DEFERRED TRANSACTION");
1933619346
}
1933719347

1933819348
void begin_immediate_transaction() {
19339-
this->begin_transaction_internal("BEGIN IMMEDIATE TRANSACTION");
19349+
sqlite3* db = this->connection->retain();
19350+
this->executor.perform_void_exec(db, "BEGIN IMMEDIATE TRANSACTION");
1934019351
}
1934119352

1934219353
void begin_exclusive_transaction() {
19343-
this->begin_transaction_internal("BEGIN EXCLUSIVE TRANSACTION");
19354+
sqlite3* db = this->connection->retain();
19355+
this->executor.perform_void_exec(db, "BEGIN EXCLUSIVE TRANSACTION");
1934419356
}
1934519357

1934619358
void commit() {
@@ -19502,12 +19514,6 @@ namespace sqlite_orm {
1950219514
}
1950319515
}
1950419516

19505-
void begin_transaction_internal(const std::string& sql) {
19506-
this->connection->retain();
19507-
sqlite3* db = this->connection->get();
19508-
this->executor.perform_void_exec(db, sql.c_str());
19509-
}
19510-
1951119517
connection_ref get_connection() {
1951219518
return {*this->connection};
1951319519
}

0 commit comments

Comments
 (0)