Audacity 3.2.0
SQLiteUtils.cpp
Go to the documentation of this file.
1
2/*
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 * SPDX-FileName: SQLiteUtils.cpp
5 * SPDX-FileContributor: Dmitry Vedenko
6 */
7
8#include "SQLiteUtils.h"
9
10#include "sqlite3.h"
11
12namespace audacity::sqlite
13{
14namespace
15{
16// This singleton handles initialization/shutdown of the SQLite library.
17// It is needed because our local SQLite is built with SQLITE_OMIT_AUTOINIT
18// defined.
19//
20// It's safe to use even if a system version of SQLite is used that didn't
21// have SQLITE_OMIT_AUTOINIT defined.
23{
24public:
26 {
27 // Enable URI filenames for all connections
28 mError = Error(sqlite3_config(SQLITE_CONFIG_URI, 1));
29
30 if (mError.IsError())
31 return;
32
33 mError =
34 Error(sqlite3_config(SQLITE_CONFIG_LOG, SQLiteLogCallback, this));
35
36 if (mError.IsError())
37 return;
38
39 mError = Error(sqlite3_initialize());
40
41#ifdef NO_SHM
42 if (mError.IsError())
43 return;
44
45 // Use the "unix-excl" VFS to make access to the DB exclusive. This
46 // gets rid of the "<database name>-shm" shared memory file.
47 //
48 // Though it shouldn't, it doesn't matter if this fails.
49 auto vfs = sqlite3_vfs_find("unix-excl");
50 if (vfs)
51 sqlite3_vfs_register(vfs, 1);
52#endif
53 }
54
56 {
57 // This function must be called single-threaded only
58 // It returns a value, but there's nothing we can do with it
59 (void)sqlite3_shutdown();
60 }
61
62 static void SQLiteLogCallback(void* initer, int code, const char* msg)
63 {
64 SQLiteIniter* self = static_cast<SQLiteIniter*>(initer);
65
66 if (self->mLogCallback)
67 self->mLogCallback(code, msg);
68 }
69
70 Error GetError() const noexcept
71 {
72 return mError;
73 }
74
76 {
77 mLogCallback = std::move(callback);
78 }
79
80private:
83};
84
86{
87 static SQLiteIniter sIniter;
88 return sIniter;
89}
90} // namespace
91
92Error Initialize() noexcept
93{
94 return GetIniter().GetError();
95}
96
98{
99 GetIniter().SetLogCallback(std::move(callback));
100}
101
102} // namespace audacity::sqlite
A class representing an error in SQLite.
Definition: Error.h:17
static void SQLiteLogCallback(void *initer, int code, const char *msg)
Definition: SQLiteUtils.cpp:62
std::function< void(int, std::string_view)> LogCallback
Definition: SQLiteUtils.h:19
void SetLogCallback(LogCallback callback)
Definition: SQLiteUtils.cpp:97
Error Initialize() noexcept
Definition: SQLiteUtils.cpp:92