Audacity 3.2.0
SafeConnection.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: SafeConnection.cpp
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#include "SafeConnection.h"
8
9namespace audacity::sqlite
10{
12 : mConnection(std::move(connection))
13{
14}
15
16std::shared_ptr<SafeConnection> SafeConnection::Open(
17 std::string_view path, OpenMode mode, ThreadMode threadMode,
18 Error* openError)
19{
20 auto connection = Connection::Open(path, mode, threadMode);
21
22 if (!connection)
23 {
24 if (openError)
25 *openError = connection.GetError();
26
27 return {};
28 }
29
30 return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
31}
32
33std::shared_ptr<SafeConnection> SafeConnection::Reopen(
34 const Connection& prevConnection, OpenMode mode, ThreadMode threadMode,
35 Error* openError)
36{
37 auto connection = Connection::Reopen(prevConnection, mode, threadMode);
38
39 if (!connection)
40 {
41 if (openError)
42 *openError = connection.GetError();
43
44 return {};
45 }
46
47 return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
48}
49
50std::shared_ptr<SafeConnection> SafeConnection::Reopen(
51 sqlite3* prevConnection, OpenMode mode, ThreadMode threadMode,
52 Error* openError)
53{
54 auto connection = Connection::Reopen(prevConnection, mode, threadMode);
55
56 if (!connection)
57 {
58 if (openError)
59 *openError = connection.GetError();
60
61 return {};
62 }
63
64 return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
65}
66
67std::shared_ptr<SafeConnection> SafeConnection::Reopen(
68 SafeConnection& prevConnection, OpenMode mode, ThreadMode threadMode,
69 Error* openError)
70{
71 auto connection =
72 Connection::Reopen(prevConnection.mConnection, mode, threadMode);
73
74 if (!connection)
75 {
76 if (openError)
77 *openError = connection.GetError();
78
79 return {};
80 }
81
82 return std::make_shared<SafeConnection>(Tag {}, std::move(*connection));
83}
84
86{
87 return Lock { shared_from_this() };
88}
89
90SafeConnection::Lock::Lock(std::shared_ptr<SafeConnection> connection)
91 : mSafeConnection(std::move(connection))
92{
94 mLock = std::unique_lock { mSafeConnection->mConnectionMutex };
95}
96
98{
99 return &mSafeConnection->mConnection;
100}
101
103{
104 return &mSafeConnection->mConnection;
105}
106
108{
109 return mSafeConnection->mConnection;
110}
111
113{
114 return mSafeConnection->mConnection;
115}
116
117SafeConnection::Lock::operator bool() const noexcept
118{
119 return IsValid();
120}
121
122bool SafeConnection::Lock::IsValid() const noexcept
123{
124 return mSafeConnection != nullptr;
125}
126
127} // namespace audacity::sqlite
A class representing a connection to a SQLite database.
Definition: Connection.h:48
static Result< Connection > Reopen(const Connection &connection, OpenMode mode=OpenMode::ReadWriteCreate, ThreadMode threadMode=ThreadMode::Serialized)
Definition: Connection.cpp:82
static Result< Connection > Open(std::string_view path, OpenMode mode=OpenMode::ReadWriteCreate, ThreadMode threadMode=ThreadMode::Serialized)
Opens a connection to a database.
Definition: Connection.cpp:21
A class representing an error in SQLite.
Definition: Error.h:17
A class representing a safe connection to SQLite.
static std::shared_ptr< SafeConnection > Reopen(const Connection &connection, OpenMode mode=OpenMode::ReadWriteCreate, ThreadMode threadMode=ThreadMode::Serialized, Error *openError=nullptr)
SafeConnection(Tag, Connection connection)
static std::shared_ptr< SafeConnection > Open(std::string_view path, OpenMode mode=OpenMode::ReadWriteCreate, ThreadMode threadMode=ThreadMode::Serialized, Error *openError=nullptr)
OpenMode
The mode in which the database should be opened.
Definition: Connection.h:26
ThreadMode
The mode in which the database should be accessed.
Definition: Connection.h:39
STL namespace.
std::shared_ptr< SafeConnection > mSafeConnection
Lock(std::shared_ptr< SafeConnection > connection)
std::unique_lock< MutexType > mLock