Audacity 3.2.0
Connection.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: Connection.h
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#pragma once
8
9#include <mutex>
10#include <optional>
11#include <string>
12#include <vector>
13
14#include "Blob.h"
15#include "Function.h"
16#include "Result.h"
17#include "Statement.h"
18#include "Transaction.h"
19
20struct sqlite3;
21
22namespace audacity::sqlite
23{
25enum class OpenMode
26{
30 Memory,
31};
32
34
38enum class ThreadMode
39{
42};
43
44class Transaction;
45
47class SQLITE_HELPERS_API Connection final
48{
49public:
51 static Result<Connection> Open(
52 std::string_view path, OpenMode mode = OpenMode::ReadWriteCreate,
54
57 static Result<Connection> Wrap(sqlite3* connection);
58
61 static Result<Connection> Reopen(
62 const Connection& connection, OpenMode mode = OpenMode::ReadWriteCreate,
64
67 static Result<Connection> Reopen(
68 sqlite3* connection, OpenMode mode = OpenMode::ReadWriteCreate,
70
71 Connection() = default;
72
73 Connection(const Connection&) = delete;
74 Connection(Connection&&) noexcept;
75
76 Connection& operator=(const Connection&) = delete;
77 Connection& operator=(Connection&&) noexcept;
78
79 ~Connection();
80
82 bool IsOpen() const noexcept;
83
85 bool CheckTableExists(std::string_view tableName) const;
86
88 Error Execute(std::string_view sql) noexcept;
89
91 Transaction BeginTransaction(std::string name);
92
94 Result<Statement> CreateStatement(std::string_view sql) const;
95
97 template<typename ScalarFunctionType>
99 CreateScalarFunction(std::string name, ScalarFunctionType function)
100 {
101 return ScalarFunction { mConnection, std::move(name),
102 std::move(function) };
103 }
104
106 template<typename StepFunctionType, typename FinalFunctionType>
108 std::string name, StepFunctionType stepFunction,
109 FinalFunctionType finalFunction)
110 {
111 return AggregateFunction { mConnection, std::move(name),
112 std::move(stepFunction),
113 std::move(finalFunction) };
114 }
115
117 Result<Blob> OpenBlob(
118 const std::string& tableName, const std::string& columnName,
119 int64_t rowId, bool readOnly,
120 const std::string& databaseName = "main") const;
121
123 explicit operator sqlite3*() const noexcept;
124
126 explicit operator bool() const noexcept;
127
129 Error Close(bool force) noexcept;
130
132 std::string_view GetPath(const char* dbName = {}) const noexcept;
133
134private:
135 Connection(sqlite3* connection, bool owned) noexcept;
136
137 static Error TransactionHandler(
138 Connection& connection, Transaction::TransactionOperation operation,
139 Transaction& name);
140
141 sqlite3* mConnection {};
142
143 std::vector<Transaction*> mPendingTransactions {};
144
145 bool mInDestructor {};
146 bool mIsOwned {};
147};
148
149} // namespace audacity::sqlite
std::unique_ptr< DBConnection > Connection
Definition: DBConnection.h:132
wxString name
Definition: TagsEditor.cpp:166
A class representing an aggregate function in a SQLite database.
Definition: Function.h:181
A class representing a connection to a SQLite database.
Definition: Connection.h:48
Connection(const Connection &)=delete
AggregateFunction CreateAggregateFunction(std::string name, StepFunctionType stepFunction, FinalFunctionType finalFunction)
Registers an aggregate function with the given name.
Definition: Connection.h:107
A class representing an error in SQLite.
Definition: Error.h:17
A class representing a result of an operation.
Definition: Result.h:19
A class representing a scalar function in a SQLite database.
Definition: Function.h:148
A class representing a compiled statement.
Definition: Statement.h:202
A class representing a transaction in SQLite.
Definition: Transaction.h:20
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.