@@ -13883,6 +13883,7 @@ namespace sqlite_orm {
13883
13883
#ifdef SQLITE_ORM_CPP20_SEMAPHORE_SUPPORTED
13884
13884
#include <semaphore>
13885
13885
#endif
13886
+ #include <functional> // std::function
13886
13887
#include <string> // std::string
13887
13888
#endif
13888
13889
@@ -13919,7 +13920,15 @@ namespace sqlite_orm {
13919
13920
std::binary_semaphore& sync;
13920
13921
};
13921
13922
13922
- connection_holder(std::string filename) : filename(std::move(filename)) {}
13923
+ connection_holder(std::string filename, bool openedForeverHint, std::function<void(sqlite3*)> onAfterOpen) :
13924
+ filename(std::move(filename)), _openedForeverHint{openedForeverHint},
13925
+ _onAfterOpen{std::move(onAfterOpen)} {}
13926
+
13927
+ connection_holder(const connection_holder&) = delete;
13928
+
13929
+ connection_holder(const connection_holder& other, std::function<void(sqlite3*)> onAfterOpen) :
13930
+ filename{other.filename}, _openedForeverHint{other._openedForeverHint},
13931
+ _onAfterOpen{std::move(onAfterOpen)} {}
13923
13932
13924
13933
void retain() {
13925
13934
const maybe_lock maybeLock{this->_sync, !this->_openedForeverHint};
@@ -13931,12 +13940,16 @@ namespace sqlite_orm {
13931
13940
return;
13932
13941
}
13933
13942
13934
- // first one opens the connection.
13943
+ // first one opens and sets up the connection.
13935
13944
13936
13945
if (int rc = sqlite3_open(this->filename.c_str(), &this->db); rc != SQLITE_OK)
13937
13946
[[unlikely]] /*possible, but unexpected*/ {
13938
13947
throw_translated_sqlite_error(this->db);
13939
13948
}
13949
+
13950
+ if (this->_onAfterOpen) {
13951
+ this->_onAfterOpen(this->db);
13952
+ }
13940
13953
}
13941
13954
13942
13955
void release() {
@@ -13980,13 +13993,21 @@ namespace sqlite_orm {
13980
13993
sqlite3* db = nullptr;
13981
13994
13982
13995
private:
13983
- std::binary_semaphore _sync{1};
13984
13996
const bool _openedForeverHint = false;
13997
+ const std::function<void(sqlite3* db)> _onAfterOpen;
13998
+ std::binary_semaphore _sync{1};
13985
13999
std::atomic_int _retain_count{};
13986
14000
};
13987
14001
#else
13988
14002
struct connection_holder {
13989
- connection_holder(std::string filename) : filename(std::move(filename)) {}
14003
+ connection_holder(std::string filename,
14004
+ bool /*openedForeverHint*/,
14005
+ std::function<void(sqlite3*)> onAfterOpen) :
14006
+ filename(std::move(filename)), _onAfterOpen{std::move(onAfterOpen)} {}
14007
+
14008
+ connection_holder(const connection_holder&) = delete;
14009
+ connection_holder(const connection_holder& other, std::function<void(sqlite3*)> onAfterOpen) :
14010
+ filename{other.filename}, _onAfterOpen{std::move(onAfterOpen)} {}
13990
14011
13991
14012
void retain() {
13992
14013
// first one opens the connection.
@@ -13998,6 +14019,10 @@ namespace sqlite_orm {
13998
14019
throw_translated_sqlite_error(this->db);
13999
14020
}
14000
14021
}
14022
+
14023
+ if (this->_onAfterOpen) {
14024
+ this->_onAfterOpen(this->db);
14025
+ }
14001
14026
}
14002
14027
14003
14028
void release() {
@@ -14031,6 +14056,7 @@ namespace sqlite_orm {
14031
14056
sqlite3* db = nullptr;
14032
14057
14033
14058
private:
14059
+ const std::function<void(sqlite3* db)> _onAfterOpen;
14034
14060
std::atomic_int _retain_count{};
14035
14061
};
14036
14062
#endif
@@ -17051,11 +17077,12 @@ namespace sqlite_orm {
17051
17077
}
17052
17078
17053
17079
void set_pragma_impl(const std::string& query, sqlite3* db = nullptr) {
17054
- auto con = this->get_connection();
17055
- if (db == nullptr) {
17056
- db = con.get();
17080
+ if (db) {
17081
+ perform_void_exec(db, query);
17082
+ } else {
17083
+ auto con = this->get_connection();
17084
+ perform_void_exec(con.get(), query);
17057
17085
}
17058
- perform_void_exec(db, query);
17059
17086
}
17060
17087
};
17061
17088
}
@@ -18069,9 +18096,6 @@ namespace sqlite_orm {
18069
18096
void open_forever() {
18070
18097
this->isOpenedForever = true;
18071
18098
this->connection->retain();
18072
- if (1 == this->connection->retain_count()) {
18073
- this->on_open_internal(this->connection->get());
18074
- }
18075
18099
}
18076
18100
18077
18101
/**
@@ -18384,7 +18408,7 @@ namespace sqlite_orm {
18384
18408
}
18385
18409
18386
18410
backup_t make_backup_to(const std::string& filename) {
18387
- auto holder = std::make_unique<connection_holder>(filename);
18411
+ auto holder = std::make_unique<connection_holder>(filename, true, nullptr );
18388
18412
connection_ref conRef{*holder};
18389
18413
return {conRef, "main", this->get_connection(), "main", std::move(holder)};
18390
18414
}
@@ -18394,7 +18418,7 @@ namespace sqlite_orm {
18394
18418
}
18395
18419
18396
18420
backup_t make_backup_from(const std::string& filename) {
18397
- auto holder = std::make_unique<connection_holder>(filename);
18421
+ auto holder = std::make_unique<connection_holder>(filename, true, nullptr );
18398
18422
connection_ref conRef{*holder};
18399
18423
return {this->get_connection(), "main", conRef, "main", std::move(holder)};
18400
18424
}
@@ -18442,22 +18466,25 @@ namespace sqlite_orm {
18442
18466
pragma(std::bind(&storage_base::get_connection, this)),
18443
18467
limit(std::bind(&storage_base::get_connection, this)),
18444
18468
inMemory(filename.empty() || filename == ":memory:"),
18445
- connection(std::make_unique<connection_holder>(std::move(filename))),
18469
+ connection(std::make_unique<connection_holder>(
18470
+ std::move(filename),
18471
+ inMemory,
18472
+ std::bind(&storage_base::on_open_internal, this, std::placeholders::_1))),
18446
18473
cachedForeignKeysCount(foreignKeysCount) {
18447
18474
if (this->inMemory) {
18448
18475
this->connection->retain();
18449
- this->on_open_internal(this->connection->get());
18450
18476
}
18451
18477
}
18452
18478
18453
18479
storage_base(const storage_base& other) :
18454
18480
on_open(other.on_open), pragma(std::bind(&storage_base::get_connection, this)),
18455
18481
limit(std::bind(&storage_base::get_connection, this)), inMemory(other.inMemory),
18456
- connection(std::make_unique<connection_holder>(other.connection->filename)),
18482
+ connection(std::make_unique<connection_holder>(
18483
+ *other.connection,
18484
+ std::bind(&storage_base::on_open_internal, this, std::placeholders::_1))),
18457
18485
cachedForeignKeysCount(other.cachedForeignKeysCount) {
18458
18486
if (this->inMemory) {
18459
18487
this->connection->retain();
18460
- this->on_open_internal(this->connection->get());
18461
18488
}
18462
18489
}
18463
18490
@@ -18472,18 +18499,12 @@ namespace sqlite_orm {
18472
18499
18473
18500
void begin_transaction_internal(const std::string& query) {
18474
18501
this->connection->retain();
18475
- if (1 == this->connection->retain_count()) {
18476
- this->on_open_internal(this->connection->get());
18477
- }
18478
18502
sqlite3* db = this->connection->get();
18479
18503
perform_void_exec(db, query);
18480
18504
}
18481
18505
18482
18506
connection_ref get_connection() {
18483
18507
connection_ref res{*this->connection};
18484
- if (1 == this->connection->retain_count()) {
18485
- this->on_open_internal(this->connection->get());
18486
- }
18487
18508
return res;
18488
18509
}
18489
18510
@@ -18509,7 +18530,7 @@ namespace sqlite_orm {
18509
18530
}
18510
18531
#endif
18511
18532
if (this->pragma.synchronous_ != -1) {
18512
- this->pragma.synchronous( this->pragma.synchronous_);
18533
+ this->pragma.set_pragma("synchronous", this->pragma.synchronous_, db );
18513
18534
}
18514
18535
18515
18536
if (this->pragma.journal_mode_ != -1) {
0 commit comments