Audacity 3.2.0
Function.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: Function.cpp
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#include "Function.h"
8
9#include <algorithm>
10
11#include "sqlite3.h"
12
13namespace audacity::sqlite
14{
15namespace details
16{
17void FromSQLiteValue(sqlite3_value& value, bool& result)
18{
19 result = sqlite3_value_int(&value) != 0;
20}
21
22void FromSQLiteValue(sqlite3_value& value, int& result)
23{
24 result = sqlite3_value_int(&value);
25}
26
27void FromSQLiteValue(sqlite3_value& value, unsigned int& result)
28{
29 result = std::max({}, sqlite3_value_int(&value));
30}
31
32void FromSQLiteValue(sqlite3_value& value, long& result)
33{
34 if (sizeof(long) == sizeof(int))
35 result = sqlite3_value_int(&value);
36 else
37 result = sqlite3_value_int64(&value);
38}
39
40void FromSQLiteValue(sqlite3_value& value, unsigned long& result)
41{
42 if (sizeof(unsigned long) == sizeof(int))
43 result = std::max({}, sqlite3_value_int(&value));
44 else
45 result = std::max({}, sqlite3_value_int64(&value));
46}
47
48void FromSQLiteValue(sqlite3_value& value, long long& result)
49{
50 result = sqlite3_value_int64(&value);
51}
52
53void FromSQLiteValue(sqlite3_value& value, unsigned long long& result)
54{
55 result = std::max({}, sqlite3_value_int64(&value));
56}
57
58void FromSQLiteValue(sqlite3_value& value, double& result)
59{
60 result = sqlite3_value_double(&value);
61}
62
63void FromSQLiteValue(sqlite3_value& value, float& result)
64{
65 result = static_cast<float>(sqlite3_value_double(&value));
66}
67
68void FromSQLiteValue(sqlite3_value& value, std::string& result)
69{
70 const auto* text = reinterpret_cast<const char*>(sqlite3_value_text(&value));
71 const auto length = sqlite3_value_bytes(&value);
72
73 result.assign(text, length);
74}
75
76void FromSQLiteValue(sqlite3_value& value, std::string_view& result)
77{
78 const auto* text = reinterpret_cast<const char*>(sqlite3_value_text(&value));
79 const auto length = sqlite3_value_bytes(&value);
80
81 result = std::string_view(text, length);
82}
83
84void SetSQLiteFunctionResult(sqlite3_context* context, bool value)
85{
86 sqlite3_result_int(context, value ? 1 : 0);
87}
88
89void SetSQLiteFunctionResult(sqlite3_context* context, int value)
90{
91 sqlite3_result_int(context, value);
92}
93
94void SetSQLiteFunctionResult(sqlite3_context* context, unsigned int value)
95{
96 sqlite3_result_int(context, value);
97}
98
99void SetSQLiteFunctionResult(sqlite3_context* context, long value)
100{
101 if (sizeof(long) == sizeof(int))
102 sqlite3_result_int(context, value);
103 else
104 sqlite3_result_int64(context, value);
105}
106
107void SetSQLiteFunctionResult(sqlite3_context* context, unsigned long value)
108{
109 if (sizeof(long) == sizeof(int))
110 sqlite3_result_int(context, value);
111 else
112 sqlite3_result_int64(context, value);
113}
114
115void SetSQLiteFunctionResult(sqlite3_context* context, long long value)
116{
117 sqlite3_result_int64(context, value);
118}
119
120void SetSQLiteFunctionResult(sqlite3_context* context, unsigned long long value)
121{
122 sqlite3_result_int64(context, value);
123}
124
125void SetSQLiteFunctionResult(sqlite3_context* context, double value)
126{
127 sqlite3_result_double(context, value);
128}
129
130void SetSQLiteFunctionResult(sqlite3_context* context, float value)
131{
132 sqlite3_result_double(context, value);
133}
134
135void SetSQLiteFunctionResult(sqlite3_context* context, const std::string& value)
136{
137 sqlite3_result_text(context, value.c_str(), value.size(), SQLITE_TRANSIENT);
138}
139
141 sqlite3_context* context, const std::string_view& value)
142{
143 sqlite3_result_text(context, value.data(), value.size(), SQLITE_TRANSIENT);
144}
145
147 sqlite3_context* context, const std::string_view& error)
148{
149 sqlite3_result_error(context, error.data(), error.size());
150}
151
152} // namespace details
153
155{
156 *this = std::move(rhs);
157}
158
160{
161 std::swap(mConnection, rhs.mConnection);
162 std::swap(mName, rhs.mName);
163 std::swap(mFunctor, rhs.mFunctor);
164
165 return *this;
166}
167
169{
170 if (mConnection)
171 {
172 sqlite3_create_function(
173 mConnection, mName.c_str(), 0, SQLITE_UTF8, nullptr,
174 nullptr, nullptr, nullptr);
175 }
176}
177
178void ScalarFunction::Register(std::size_t arity)
179{
180 sqlite3_create_function (
181 mConnection, mName.c_str(), arity, SQLITE_UTF8, this,
182 &CallFunction, nullptr, nullptr);
183}
184
186 sqlite3_context* context, int argc, sqlite3_value** argv)
187{
188 auto* function = static_cast<ScalarFunction*>(sqlite3_user_data(context));
189 function->mFunctor(context, argc, argv);
190}
191
192void AggregateFunction::Register(std::size_t arity)
193{
194 sqlite3_create_function (
195 mConnection, mName.c_str(), arity, SQLITE_UTF8, this,
197}
198
200 sqlite3_context* context, int argc, sqlite3_value** argv)
201{
202 auto* function = static_cast<AggregateFunction*>(sqlite3_user_data(context));
203 function->mStepFunctor(context, argc, argv);
204}
205
206void AggregateFunction::CallFinalFunction(sqlite3_context* context)
207{
208 auto* function = static_cast<AggregateFunction*>(sqlite3_user_data(context));
209 function->mFinalFunctor(context);
210}
211
213{
214 *this = std::move(rhs);
215}
216
218{
219 std::swap(mConnection, rhs.mConnection);
220 std::swap(mName, rhs.mName);
221 std::swap(mStepFunctor, rhs.mStepFunctor);
222 std::swap(mFinalFunctor, rhs.mFinalFunctor);
223
224 return *this;
225}
226
228{
229 if (mConnection)
230 {
231 sqlite3_create_function (
232 mConnection, mName.c_str(), 0, SQLITE_UTF8, nullptr,
233 nullptr, nullptr, nullptr);
234 }
235}
236
237} // namespace audacity::sqlite
238
A class representing an aggregate function in a SQLite database.
Definition: Function.h:181
details::SQLiteFunctor mFinalFunctor
Definition: Function.h:216
AggregateFunction & operator=(const AggregateFunction &)=delete
void Register(std::size_t arity)
Definition: Function.cpp:192
static void CallStepFunction(sqlite3_context *context, int argc, sqlite3_value **argv)
Definition: Function.cpp:199
details::SQLiteFunctorWithArgs mStepFunctor
Definition: Function.h:215
static void CallFinalFunction(sqlite3_context *context)
Definition: Function.cpp:206
A class representing a scalar function in a SQLite database.
Definition: Function.h:148
details::SQLiteFunctorWithArgs mFunctor
Definition: Function.h:175
static void CallFunction(sqlite3_context *context, int argc, sqlite3_value **argv)
Definition: Function.cpp:185
void Register(std::size_t arity)
Definition: Function.cpp:178
ScalarFunction & operator=(const ScalarFunction &)=delete
UTILITY_API const char *const * argv
A copy of argv; responsibility of application startup to assign it.
UTILITY_API int argc
A copy of argc; responsibility of application startup to assign it.
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:634
void FromSQLiteValue(sqlite3_value &value, bool &result)
Definition: Function.cpp:17
void SetSQLiteFunctionError(sqlite3_context *context, const std::string_view &error)
Definition: Function.cpp:146
void SetSQLiteFunctionResult(sqlite3_context *context, bool value)
Definition: Function.cpp:84