ThinSQLite++
A thin, safe and convenient modern C++ wrapper for SQLite API.
Loading...
Searching...
No Matches
context_iface.hpp
1/*
2 Copyright 2019 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/main/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_CONTEXT_IFACE_INCLUDED
10#define HEADER_SQLITEPP_CONTEXT_IFACE_INCLUDED
11
12#include "handle.hpp"
13#include "value_iface.hpp"
14#include "meta.hpp"
15
16#include <memory>
17
18namespace thinsqlitepp
19{
20 class database;
21
36 class context final : public handle<sqlite3_context, context>
37 {
38 public:
40 ~context() noexcept = delete;
41
47 void * aggregate_context(int size) noexcept
48 { return sqlite3_aggregate_context(c_ptr(), size); }
49
55 class database & database() const noexcept
56 { return *(class database *)sqlite3_context_db_handle(c_ptr()); }
57
63 void error(const std::string_view & value) noexcept
64 { sqlite3_result_error(c_ptr(), value.size() ? &value[0] : "", int_size(value.size())); }
65 #if __cpp_char8_t >= 201811
67 void error(const std::u8string_view & value) noexcept
68 { sqlite3_result_error(c_ptr(), value.size() ? (const char *)&value[0] : "", int_size(value.size())); }
69 #endif
70
80 void error(int error_code) noexcept
81 { sqlite3_result_error_code(c_ptr(), error_code); }
82
88 void error_nomem() noexcept
90
98
104 void result(std::nullptr_t) noexcept
106
112 void result(int value) noexcept
114
120 void result(int64_t value) noexcept
122
128 void result(double value) noexcept
130
138 void result(const std::string_view & value) noexcept
139 {
140 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
141 if (auto data = value.data())
142 sqlite3_result_text(c_ptr(), data, int_size(value.size()), SQLITE_TRANSIENT);
143 else
145 }
146 #if __cpp_char8_t >= 201811
148 void result(const std::u8string_view & value) noexcept
149 {
150 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
151 if (auto data = value.data())
152 sqlite3_result_text(c_ptr(), (const char *)data, int_size(value.size()), SQLITE_TRANSIENT);
153 else
155 }
156 #endif
166 {
167 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
168 if (auto data = value.data())
169 sqlite3_result_text(c_ptr(), data, int_size(value.size()), SQLITE_STATIC);
170 else
172 }
173 #if __cpp_char8_t >= 201811
176 {
177 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
178 if (auto data = value.data())
179 sqlite3_result_text(c_ptr(), (const char *)data, int_size(value.size()), SQLITE_STATIC);
180 else
182 }
183 #endif
195 void result_reference(const std::string_view & value, void (*unref)(const char *)) noexcept
196 {
197 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
198 if (auto data = value.data())
199 {
200 sqlite3_result_text(c_ptr(), data, int_size(value.size()), (void (*)(void*))unref);
201 }
202 else
203 {
204 unref(nullptr);
206 }
207 }
208
209 #if __cpp_char8_t >= 201811
211 void result_reference(const std::u8string_view & value, void (*unref)(const char8_t *)) noexcept
212 {
213 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
214 if (auto data = value.data())
215 {
216 sqlite3_result_text(c_ptr(), (const char *)data, int_size(value.size()), (void (*)(void *))unref);
217 }
218 else
219 {
220 unref(nullptr);
222 }
223 }
224 #endif
225
233 void result(const blob_view & value) noexcept
234 {
235 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
236 if (auto data = value.data())
237 sqlite3_result_blob(c_ptr(), data, int_size(value.size()), SQLITE_TRANSIENT);
238 else
240 }
241
250 void result_reference(const blob_view & value) noexcept
251 {
252 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
253 if (auto data = value.data())
254 sqlite3_result_blob(c_ptr(), data, int_size(value.size()), SQLITE_STATIC);
255 else
257 }
258
270 void result_reference(const blob_view & value, void (*unref)(const std::byte *) noexcept) noexcept
271 {
272 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
273 if (auto data = value.data())
274 {
275 sqlite3_result_blob(c_ptr(), data, int_size(value.size()), (void (*)(void *))unref);
276 }
277 else
278 {
279 unref(nullptr);
281 }
282 }
283
284
291 void result(const zero_blob & value) noexcept
292 { sqlite3_result_zeroblob(c_ptr(), int_size(value.size())); }
293
295
302 template<class T>
303 void result(T * ptr, const char * type, void(*destroy)(T*)) noexcept
304 { sqlite3_result_pointer(this->c_ptr(), ptr, type, (void(*)(void*))destroy); }
305
316 template<class T>
317 void result(std::unique_ptr<T> ptr) noexcept
318 { this->result(ptr.release(), typeid(T).name(), [](T * p) { delete p;}); }
319
325 void result(const value & val) noexcept
326 { sqlite3_result_value(c_ptr(), val.c_ptr()); }
327
328#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 9, 0)
336 void result_subtype(unsigned value) noexcept
338#endif
339
347 template<class T>
348 T * get_auxdata(int arg) const noexcept
349 { return (T *)sqlite3_get_auxdata(this->c_ptr(), arg); }
350
358 template<class T>
359 void set_auxdata(int arg, T * data, void (*destroy)(T*)noexcept) noexcept
360 { sqlite3_set_auxdata(this->c_ptr(), arg, data, (void(*)(void*))destroy); }
361
371 template<class T>
372 T * user_data() noexcept
373 { return (T *)sqlite3_user_data(this->c_ptr()); }
374
375#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 22, 0)
381 bool vtab_nochange() const noexcept
382 { return sqlite3_vtab_nochange(c_ptr()); }
383#endif
384 };
385
388}
389
390
391#endif
sqlite3_aggregate_context
#define SQLITE_TRANSIENT
SQL Function Context Object.
Definition context_iface.hpp:37
void result(int value) noexcept
Return an int from the implemented SQL function.
Definition context_iface.hpp:112
void result_subtype(unsigned value) noexcept
Sets the subtype of the result of the implemented SQL function.
Definition context_iface.hpp:336
void result(double value) noexcept
Return a double from implemented SQL function.
Definition context_iface.hpp:128
~context() noexcept=delete
Contexts are never destroyed by user code.
void result_reference(const std::u8string_view &value, void(*unref)(const char8_t *)) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition context_iface.hpp:211
class database & database() const noexcept
Retrieve database connection for the context.
Definition context_iface.hpp:55
void result(int64_t value) noexcept
Return an int64_t from implemented SQL function.
Definition context_iface.hpp:120
void error(int error_code) noexcept
Changes the error code returned by function evaluation.
Definition context_iface.hpp:80
void result(std::unique_ptr< T > ptr) noexcept
Return a custom pointer from the implemented SQL function.
Definition context_iface.hpp:317
void result_reference(const std::string_view &value) noexcept
Return a string by reference from the implemented SQL function.
Definition context_iface.hpp:165
T * get_auxdata(int arg) const noexcept
Get auxiliary data associated with argument values.
Definition context_iface.hpp:348
void result(const blob_view &value) noexcept
Return a blob by value from the implemented SQL function.
Definition context_iface.hpp:233
void error_toobig() noexcept
Causes the implemented SQL function to throw an SQL exception indicating that a string or BLOB is too...
Definition context_iface.hpp:96
void result_reference(const std::string_view &value, void(*unref)(const char *)) noexcept
Return a string by reference from the implemented SQL function.
Definition context_iface.hpp:195
void result(const zero_blob &value) noexcept
Return a blob of zeroes from the implemented SQL function.
Definition context_iface.hpp:291
void result(const value &val) noexcept
Return a copy of the passed value from the implemented SQL function.
Definition context_iface.hpp:325
void result(const std::u8string_view &value) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition context_iface.hpp:148
T * user_data() noexcept
Return the function's user data.
Definition context_iface.hpp:372
bool vtab_nochange() const noexcept
Return if a value being fetched as part of an UPDATE operation during which the column value will not...
Definition context_iface.hpp:381
void result(std::nullptr_t) noexcept
Return NULL from the implemented SQL function.
Definition context_iface.hpp:104
void error_nomem() noexcept
Causes the implemented SQL function to throw an SQL exception indicating that a memory allocation fai...
Definition context_iface.hpp:88
void error(const std::string_view &value) noexcept
Cause the implemented SQL function to throw an SQL exception.
Definition context_iface.hpp:63
void result_reference(const std::u8string_view &value) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition context_iface.hpp:175
void result_reference(const blob_view &value, void(*unref)(const std::byte *) noexcept) noexcept
Return a blob by reference from the implemented SQL function.
Definition context_iface.hpp:270
void * aggregate_context(int size) noexcept
Allocate memory for aggregate function context.
Definition context_iface.hpp:47
void result(T *ptr, const char *type, void(*destroy)(T *)) noexcept
Definition context_iface.hpp:303
void result_reference(const blob_view &value) noexcept
Return a blob by reference from the implemented SQL function.
Definition context_iface.hpp:250
void set_auxdata(int arg, T *data, void(*destroy)(T *) noexcept) noexcept
Associate auxiliary data with argument values.
Definition context_iface.hpp:359
void result(const std::string_view &value) noexcept
Return a string by value from the implemented SQL function.
Definition context_iface.hpp:138
void error(const std::u8string_view &value) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition context_iface.hpp:67
Database Connection.
Definition database_iface.hpp:108
Base functionality for all fake wrapper classes
Definition handle.hpp:27
sqlite3_context * c_ptr() const noexcept
Definition handle.hpp:45
Dynamically Typed Value Object.
Definition value_iface.hpp:35
An efficient blob of zeroes of a given size.
Definition span.hpp:256
sqlite3_context_db_handle
sqlite3_get_auxdata
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
sqlite3_result_error
sqlite3_result_subtype
sqlite3_user_data
sqlite3_vtab_nochange