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
39class PROJECT_FILE_IO_API DBConnection final
40{
41public:
42 using CheckpointFailureCallback = std::function<void()>;
43
45 const std::weak_ptr<AudacityProject> &pProject,
46 const std::shared_ptr<DBConnectionErrors> &pErrors,
51 );
53
54 int Open(const FilePath fileName);
55 bool Close();
56
58 [[noreturn]] void ThrowException(
59 bool write
60 ) const;
61
62 int SafeMode(const char *schema = "main");
63 int FastMode(const char* schema = "main");
64 int SetPageSize(const char* schema = "main");
65
66 bool Assign(sqlite3 *handle);
67 sqlite3 *Detach();
68
69 sqlite3 *DB();
70
71 int GetLastRC() const ;
72 const wxString GetLastMessage() const;
73
75 {
83 GetAllSampleBlocksSize
84 };
85 sqlite3_stmt *Prepare(enum StatementID id, const char *sql);
86
87 void SetBypass( bool bypass );
88 bool ShouldBypass();
89
91 void SetError(
92 const TranslatableString &msg,
93 const TranslatableString &libraryError = {},
94 int errorCode = {});
95
97 void SetDBError(
98 const TranslatableString &msg,
99 const TranslatableString& libraryError = {},
100 int errorCode = -1);
101
102private:
103 int OpenStepByStep(const FilePath fileName);
104 int ModeConfig(sqlite3 *db, const char *schema, const char *config);
105
106 void CheckpointThread(sqlite3 *db, const FilePath &fileName);
107 static int CheckpointHook(void *data, sqlite3 *db, const char *schema, int pages);
108
109private:
110 std::weak_ptr<AudacityProject> mpProject;
111 sqlite3 *mDB;
113
114 std::thread mCheckpointThread;
115 std::condition_variable mCheckpointCondition;
117 std::atomic_bool mCheckpointStop{ false };
118 std::atomic_bool mCheckpointPending{ false };
119 std::atomic_bool mCheckpointActive{ false };
120
121 std::mutex mStatementMutex;
122 using StatementIndex = std::pair<enum StatementID, std::thread::id>;
123 std::map<StatementIndex, sqlite3_stmt *> mStatements;
124
125 std::shared_ptr<DBConnectionErrors> mpErrors;
127
128 // Bypass transactions if database will be deleted after close
130};
131
132using Connection = std::unique_ptr<DBConnection>;
133
134// This object attached to the project simply holds the pointer to the
135// project's current database connection, which is initialized on demand,
136// and may be redirected, temporarily or permanently, to another connection
137// when backing the project up or saving or saving-as.
138class ConnectionPtr final
139 : public ClientData::Base
140 , public std::enable_shared_from_this< ConnectionPtr >
141{
142public:
144 static const ConnectionPtr &Get( const AudacityProject &project );
145
146 ~ConnectionPtr() override;
147
149};
150
151#endif
Utility ClientData::Site to register hooks into a host class that attach client data.
std::unique_ptr< DBConnection > Connection
Definition: DBConnection.h:132
wxString FilePath
Definition: Project.h:21
const auto project
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Connection mpConnection
Definition: DBConnection.h:148
~ConnectionPtr() override
static ConnectionPtr & Get(AudacityProject &project)
std::shared_ptr< DBConnectionErrors > mpErrors
Definition: DBConnection.h:125
std::mutex mCheckpointMutex
Definition: DBConnection.h:116
bool Assign(sqlite3 *handle)
sqlite3 * mCheckpointDB
Definition: DBConnection.h:112
sqlite3 * Detach()
std::mutex mStatementMutex
Definition: DBConnection.h:121
std::thread mCheckpointThread
Definition: DBConnection.h:114
sqlite3 * mDB
Definition: DBConnection.h:111
std::condition_variable mCheckpointCondition
Definition: DBConnection.h:115
std::map< StatementIndex, sqlite3_stmt * > mStatements
Definition: DBConnection.h:123
std::function< void()> CheckpointFailureCallback
Definition: DBConnection.h:42
std::weak_ptr< AudacityProject > mpProject
Definition: DBConnection.h:110
CheckpointFailureCallback mCallback
Definition: DBConnection.h:126
std::pair< enum StatementID, std::thread::id > StatementIndex
Definition: DBConnection.h:122
Holds a msgid for the translation catalog; may also bind format arguments.
void SetError()
A convenient default parameter for class template Site.
Definition: ClientData.h:29
TranslatableString mLastError
Definition: DBConnection.h:33
TranslatableString mLibraryError
Definition: DBConnection.h:34