35
35
#include " serializing_util.h"
36
36
#include " table_info.h"
37
37
38
- namespace sqlite_orm {
38
+ SQLITE_ORM_EXPORT namespace sqlite_orm {
39
+ struct on_open {
40
+ using storage_prop_tag = int ;
41
+
42
+ #ifndef SQLITE_ORM_AGGREGATE_PAREN_INIT_SUPPORTED
43
+ on_open () = default;
44
+ on_open (std::function<void (sqlite3*)> onOpen) : onOpen{std::move (onOpen)} {}
45
+ #endif
39
46
47
+ std::function<void (sqlite3*)> onOpen;
48
+ };
49
+
50
+ struct connection_control {
51
+ using storage_prop_tag = int ;
52
+
53
+ bool openForever = false ;
54
+ };
55
+ }
56
+
57
+ namespace sqlite_orm {
40
58
namespace internal {
41
59
42
60
struct storage_base {
@@ -289,17 +307,19 @@ namespace sqlite_orm {
289
307
* needed and closes when it is not needed. This function breaks this rule. In memory storage always
290
308
* keeps connection opened so calling this for in-memory storage changes nothing.
291
309
* Note about multithreading: in multithreading context avoiding using this function for not in-memory
292
- * storage may lead to data races. If you have data races in such a configuration try to call `open_forever`
310
+ * storage may lead to data races. If you have data races in such a configuration try to call `open_forever() `
293
311
* before accessing your storage - it may fix data races.
294
312
*/
295
313
void open_forever () {
296
- this ->isOpenedForever = true ;
297
- this ->connection ->retain ();
314
+ if (!this ->isOpenedForever ) {
315
+ this ->isOpenedForever = true ;
316
+ this ->connection ->retain ();
317
+ }
298
318
}
299
319
300
320
/* *
301
321
* Create an application-defined scalar SQL function.
302
- * Can be called at any time no matter whether the database connection is opened or not.
322
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is opened or not.
303
323
*
304
324
* Note: `create_scalar_function()` merely creates a closure to generate an instance of the scalar function object,
305
325
* together with a copy of the passed initialization arguments.
@@ -342,7 +362,7 @@ namespace sqlite_orm {
342
362
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
343
363
/* *
344
364
* Create an application-defined scalar function.
345
- * Can be called at any time no matter whether the database connection is opened or not.
365
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is opened or not.
346
366
*
347
367
* Note: `create_scalar_function()` merely creates a closure to generate an instance of the scalar function object,
348
368
* together with a copy of the passed initialization arguments.
@@ -357,7 +377,7 @@ namespace sqlite_orm {
357
377
358
378
/* *
359
379
* Create an application-defined scalar function.
360
- * Can be called at any time no matter whether the database connection is opened or not.
380
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is opened or not.
361
381
*
362
382
* If `quotedF` contains a freestanding function, stateless lambda or stateless function object,
363
383
* `quoted_scalar_function::callable()` uses the original function object, assuming it is free of side effects;
@@ -398,7 +418,7 @@ namespace sqlite_orm {
398
418
399
419
/* *
400
420
* Create an application-defined aggregate SQL function.
401
- * Can be called at any time no matter whether the database connection is opened or not.
421
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is opened or not.
402
422
*
403
423
* Note: `create_aggregate_function()` merely creates a closure to generate an instance of the aggregate function object,
404
424
* together with a copy of the passed initialization arguments.
@@ -447,7 +467,7 @@ namespace sqlite_orm {
447
467
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
448
468
/* *
449
469
* Create an application-defined aggregate function.
450
- * Can be called at any time no matter whether the database connection is opened or not.
470
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is opened or not.
451
471
*
452
472
* Note: `create_aggregate_function()` merely creates a closure to generate an instance of the aggregate function object,
453
473
* together with a copy of the passed initialization arguments.
@@ -462,7 +482,7 @@ namespace sqlite_orm {
462
482
463
483
/* *
464
484
* Delete a scalar function you created before.
465
- * Can be called at any time no matter whether the database connection is open or not.
485
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is open or not.
466
486
*/
467
487
template <class F >
468
488
void delete_scalar_function () {
@@ -474,7 +494,7 @@ namespace sqlite_orm {
474
494
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
475
495
/* *
476
496
* Delete a scalar function you created before.
477
- * Can be called at any time no matter whether the database connection is open or not.
497
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is open or not.
478
498
*/
479
499
template <orm_scalar_function auto f>
480
500
void delete_scalar_function () {
@@ -483,7 +503,7 @@ namespace sqlite_orm {
483
503
484
504
/* *
485
505
* Delete a quoted scalar function you created before.
486
- * Can be called at any time no matter whether the database connection is open or not.
506
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is open or not.
487
507
*/
488
508
template <orm_quoted_scalar_function auto quotedF>
489
509
void delete_scalar_function () {
@@ -493,7 +513,7 @@ namespace sqlite_orm {
493
513
494
514
/* *
495
515
* Delete aggregate function you created before.
496
- * Can be called at any time no matter whether the database connection is open or not.
516
+ * Can be called at any time (in a single-threaded context) no matter whether the database connection is open or not.
497
517
*/
498
518
template <class F >
499
519
void delete_aggregate_function () {
@@ -661,30 +681,41 @@ namespace sqlite_orm {
661
681
}
662
682
663
683
protected:
664
- storage_base (std::string filename, int foreignKeysCount) :
684
+ storage_base (std::string filename,
685
+ sqlite_orm::on_open onOpenSpec,
686
+ connection_control connectionCtrl,
687
+ int foreignKeysCount) :
688
+ on_open{std::move (onOpenSpec.onOpen )}, isOpenedForever{connectionCtrl.openForever },
665
689
pragma (std::bind (&storage_base::get_connection, this )),
666
690
limit (std::bind (&storage_base::get_connection, this )),
667
691
inMemory (filename.empty () || filename == " :memory:" ),
668
692
connection (std::make_unique<connection_holder>(
669
693
std::move (filename),
670
- inMemory,
694
+ inMemory || isOpenedForever ,
671
695
std::bind (&storage_base::on_open_internal, this , std::placeholders::_1))),
672
696
cachedForeignKeysCount (foreignKeysCount) {
673
697
if (this ->inMemory ) {
674
698
this ->connection ->retain ();
675
699
}
700
+ if (this ->isOpenedForever ) {
701
+ this ->connection ->retain ();
702
+ }
676
703
}
677
704
678
705
storage_base (const storage_base& other) :
679
706
on_open (other.on_open ), pragma (std::bind (&storage_base::get_connection, this )),
680
707
limit (std::bind (&storage_base::get_connection, this )), inMemory (other.inMemory ),
708
+ isOpenedForever{other.isOpenedForever },
681
709
connection (std::make_unique<connection_holder>(
682
710
*other.connection ,
683
711
std::bind (&storage_base::on_open_internal, this , std::placeholders::_1))),
684
712
cachedForeignKeysCount (other.cachedForeignKeysCount ) {
685
713
if (this ->inMemory ) {
686
714
this ->connection ->retain ();
687
715
}
716
+ if (this ->isOpenedForever ) {
717
+ this ->connection ->retain ();
718
+ }
688
719
}
689
720
690
721
~storage_base () {
@@ -719,15 +750,15 @@ namespace sqlite_orm {
719
750
perform_exec (db, " PRAGMA foreign_keys" , extract_single_value<bool >, &result);
720
751
return result;
721
752
}
722
-
723
753
#endif
724
- void on_open_internal (sqlite3* db) {
725
754
755
+ void on_open_internal (sqlite3* db) {
726
756
#if SQLITE_VERSION_NUMBER >= 3006019
727
757
if (this ->cachedForeignKeysCount ) {
728
758
this ->foreign_keys (db, true );
729
759
}
730
760
#endif
761
+
731
762
if (this ->pragma .synchronous_ != -1 ) {
732
763
this ->pragma .set_pragma (" synchronous" , this ->pragma .synchronous_ , db);
733
764
}
0 commit comments