Audacity 3.2.0
DBConnection.h
Go to the documentation of this file.
1/*!********************************************************************
2
3Audacity: A Digital Audio Editor
4
5@file DBConnection.h
6@brief Declare DBConnection, which maintains database connection and associated status and background thread
7
8Paul Licameli -- split from ProjectFileIO.h
9
10**********************************************************************/
11
12#ifndef __AUDACITY_DB_CONNECTION__
13#define __AUDACITY_DB_CONNECTION__
14
15#include <atomic>
16#include <condition_variable>
17#include <functional>
18#include <map>
19#include <memory>
20#include <mutex>
21#include <thread>
22
23#include "ClientData.h"
24#include "Identifier.h"
25
26struct sqlite3;
27struct sqlite3_stmt;
28class wxString;
29class AudacityProject;
30
32{
35 int mErrorCode { 0 };
36 wxString mLog;
37};
38
40{
41public:
42 // Type of function invoked in the main thread after detection of
43 // checkpoint failure, which might have been in a worker thread
44 using CheckpointFailureCallback = std::function<void()>;
45
47 const std::weak_ptr<AudacityProject> &pProject,
48 const std::shared_ptr<DBConnectionErrors> &pErrors,
51
52 int Open(const FilePath fileName);
53 bool Close();
54
56 [[noreturn]] void ThrowException(
57 bool write
58 ) const;
59
60 int SafeMode(const char *schema = "main");
61 int FastMode(const char* schema = "main");
62 int SetPageSize(const char* schema = "main");
63
64 bool Assign(sqlite3 *handle);
65 sqlite3 *Detach();
66
67 sqlite3 *DB();
68
69 int GetLastRC() const ;
70 const wxString GetLastMessage() const;
71
73 {
82 };
83 sqlite3_stmt *Prepare(enum StatementID id, const char *sql);
84
85 void SetBypass( bool bypass );
86 bool ShouldBypass();
87
89 void SetError(
90 const TranslatableString &msg,
91 const TranslatableString &libraryError = {},
92 int errorCode = {});
93
95 void SetDBError(
96 const TranslatableString &msg,
97 const TranslatableString& libraryError = {},
98 int errorCode = -1);
99
100private:
101 int OpenStepByStep(const FilePath fileName);
102 int ModeConfig(sqlite3 *db, const char *schema, const char *config);
103
104 void CheckpointThread(sqlite3 *db, const FilePath &fileName);
105 static int CheckpointHook(void *data, sqlite3 *db, const char *schema, int pages);
106
107private:
108 std::weak_ptr<AudacityProject> mpProject;
109 sqlite3 *mDB;
111
112 std::thread mCheckpointThread;
113 std::condition_variable mCheckpointCondition;
115 std::atomic_bool mCheckpointStop{ false };
116 std::atomic_bool mCheckpointPending{ false };
117 std::atomic_bool mCheckpointActive{ false };
118
119 std::mutex mStatementMutex;
120 using StatementIndex = std::pair<enum StatementID, std::thread::id>;
121 std::map<StatementIndex, sqlite3_stmt *> mStatements;
122
123 std::shared_ptr<DBConnectionErrors> mpErrors;
125
126 // Bypass transactions if database will be deleted after close
128};
129
130using Connection = std::unique_ptr<DBConnection>;
131
132// This object attached to the project simply holds the pointer to the
133// project's current database connection, which is initialized on demand,
134// and may be redirected, temporarily or permanently, to another connection
135// when backing the project up or saving or saving-as.
136class ConnectionPtr final
137 : public ClientData::Base
138 , public std::enable_shared_from_this< ConnectionPtr >
139{
140public:
141 static ConnectionPtr &Get( AudacityProject &project );
142 static const ConnectionPtr &Get( const AudacityProject &project );
143
144 ~ConnectionPtr() override;
145
147};
148
149#endif
Utility ClientData::Site to register hooks into a host class that attach client data.
std::unique_ptr< DBConnection > Connection
Definition: DBConnection.h:130
wxString FilePath
Definition: Project.h:20
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
Connection mpConnection
Definition: DBConnection.h:146
~ConnectionPtr() override
static ConnectionPtr & Get(AudacityProject &project)
void CheckpointThread(sqlite3 *db, const FilePath &fileName)
std::shared_ptr< DBConnectionErrors > mpErrors
Definition: DBConnection.h:123
static int CheckpointHook(void *data, sqlite3 *db, const char *schema, int pages)
std::mutex mCheckpointMutex
Definition: DBConnection.h:114
bool Assign(sqlite3 *handle)
void ThrowException(bool write) const
throw and show appropriate message box
int OpenStepByStep(const FilePath fileName)
void SetError(const TranslatableString &msg, const TranslatableString &libraryError={}, int errorCode={})
Just set stored errors.
@ GetAllSampleBlocksSize
Definition: DBConnection.h:81
const wxString GetLastMessage() const
int GetLastRC() const
std::atomic_bool mCheckpointStop
Definition: DBConnection.h:115
sqlite3 * mCheckpointDB
Definition: DBConnection.h:110
std::atomic_bool mCheckpointActive
Definition: DBConnection.h:117
sqlite3 * Detach()
std::mutex mStatementMutex
Definition: DBConnection.h:119
std::atomic_bool mCheckpointPending
Definition: DBConnection.h:116
int FastMode(const char *schema="main")
std::thread mCheckpointThread
Definition: DBConnection.h:112
void SetBypass(bool bypass)
sqlite3_stmt * Prepare(enum StatementID id, const char *sql)
sqlite3 * mDB
Definition: DBConnection.h:109
int SetPageSize(const char *schema="main")
DBConnection(const std::weak_ptr< AudacityProject > &pProject, const std::shared_ptr< DBConnectionErrors > &pErrors, CheckpointFailureCallback callback)
std::condition_variable mCheckpointCondition
Definition: DBConnection.h:113
sqlite3 * DB()
std::map< StatementIndex, sqlite3_stmt * > mStatements
Definition: DBConnection.h:121
void SetDBError(const TranslatableString &msg, const TranslatableString &libraryError={}, int errorCode=-1)
Set stored errors and write to log; and default libraryError to what database library reports.
int ModeConfig(sqlite3 *db, const char *schema, const char *config)
std::function< void()> CheckpointFailureCallback
Definition: DBConnection.h:44
std::weak_ptr< AudacityProject > mpProject
Definition: DBConnection.h:108
CheckpointFailureCallback mCallback
Definition: DBConnection.h:124
int Open(const FilePath fileName)
std::pair< enum StatementID, std::thread::id > StatementIndex
Definition: DBConnection.h:120
bool ShouldBypass()
int SafeMode(const char *schema="main")
Holds a msgid for the translation catalog; may also bind format arguments.
A convenient default parameter for class template Site.
Definition: ClientData.h:28
TranslatableString mLastError
Definition: DBConnection.h:33
TranslatableString mLibraryError
Definition: DBConnection.h:34