@@ -201,6 +201,21 @@ class Database
201
201
202
202
#endif // c++17
203
203
204
+ /* *
205
+ * @brief Wrap an existing sqlite3* connection opened by other means.
206
+ *
207
+ * When the Database object is constructed as a wrapper, its destruction does NOT automatically
208
+ * sqlite3_close() the connection. In this case (only), Statement objects may outlive the Database object with
209
+ * which they were constructed, so long as the underlying connection remains open.
210
+ *
211
+ * @param[in] apSQLite Existing sqlite3* connection to be wrapped
212
+ * @param[in] aBusyTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY (see setBusyTimeout())
213
+ *
214
+ * @throw SQLite::Exception in case of error
215
+ */
216
+ Database (sqlite3* apSQLite,
217
+ const int aBusyTimeoutMs = 0 );
218
+
204
219
// Database is non-copyable
205
220
Database (const Database&) = delete ;
206
221
Database& operator =(const Database&) = delete ;
@@ -217,7 +232,12 @@ class Database
217
232
*
218
233
* @warning assert in case of error
219
234
*/
220
- ~Database () = default ;
235
+ ~Database ()
236
+ {
237
+ if (!mCloseOnDestruct ) {
238
+ mSQLitePtr .release (); // prevent Deleter
239
+ }
240
+ }
221
241
222
242
// Deleter functor to use with smart pointers to close the SQLite database connection in an RAII fashion.
223
243
struct Deleter
@@ -414,7 +434,7 @@ class Database
414
434
// / Return UTF-8 encoded English language explanation of the most recent failed API call (if any).
415
435
const char * getErrorMsg () const noexcept ;
416
436
417
- // / Return the filename used to open the database.
437
+ // / Return the filename used to open the database; empty if the Database wrapped existing sqlite3*
418
438
const std::string& getFilename () const noexcept
419
439
{
420
440
return mFilename ;
@@ -536,10 +556,7 @@ class Database
536
556
static Header getHeaderInfo (const std::string& aFilename);
537
557
538
558
// Parse SQLite header data from a database file.
539
- Header getHeaderInfo ()
540
- {
541
- return getHeaderInfo (mFilename );
542
- }
559
+ Header getHeaderInfo ();
543
560
544
561
/* *
545
562
* @brief BackupType for the backup() method
@@ -571,6 +588,7 @@ class Database
571
588
private:
572
589
// TODO: perhaps switch to having Statement sharing a pointer to the Connexion
573
590
std::unique_ptr<sqlite3, Deleter> mSQLitePtr ; // /< Pointer to SQLite Database Connection Handle
591
+ bool mCloseOnDestruct ; // /< true iff ~Database() is to use sqlite3_close() on mSQLitePtr
574
592
std::string mFilename ; // /< UTF-8 filename used to open the database
575
593
};
576
594
0 commit comments