ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the 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/master/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 SQLITEPP_EXPORTED class database;
21
26
36 SQLITEPP_EXPORTED
37 class context final : public handle<sqlite3_context, context>
38 {
39 public:
41 ~context() noexcept = delete;
42
48 void * aggregate_context(int size) noexcept
49 { return sqlite3_aggregate_context(c_ptr(), size); }
50
56 class database & database() const noexcept
57 { return *(class database *)sqlite3_context_db_handle(c_ptr()); }
58
67 void error(const std::string_view & value) noexcept
68 { sqlite3_result_error(c_ptr(), value.size() ? &value[0] : "", int_size(value.size())); }
69 #if __cpp_char8_t >= 201811
71 void error(const std::u8string_view & value) noexcept
72 { sqlite3_result_error(c_ptr(), value.size() ? (const char *)&value[0] : "", int_size(value.size())); }
73 #endif
74
84 void error(int error_code) noexcept
85 { sqlite3_result_error_code(c_ptr(), error_code); }
86
92 void error_nomem() noexcept
94
100 void error_toobig() noexcept
102
108 void result(std::nullptr_t) noexcept
110
116 void result(int value) noexcept
118
124 void result(int64_t value) noexcept
126
132 void result(double value) noexcept
134
145 void result(const std::string_view & value) noexcept
146 {
147 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
148 if (auto data = value.data())
149 sqlite3_result_text(c_ptr(), data, int_size(value.size()), SQLITE_TRANSIENT);
150 else
152 }
153 #if __cpp_char8_t >= 201811
155 void result(const std::u8string_view & value) noexcept
156 {
157 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
158 if (auto data = value.data())
159 sqlite3_result_text(c_ptr(), (const char *)data, int_size(value.size()), SQLITE_TRANSIENT);
160 else
162 }
163 #endif
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(), data, int_size(value.size()), SQLITE_STATIC);
180 else
182 }
183 #if __cpp_char8_t >= 201811
186 {
187 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
188 if (auto data = value.data())
189 sqlite3_result_text(c_ptr(), (const char *)data, int_size(value.size()), SQLITE_STATIC);
190 else
192 }
193 #endif
208 void result_reference(const std::string_view & value, void (*unref)(const char *) noexcept) noexcept
209 {
210 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
211 if (auto data = value.data())
212 {
213 sqlite3_result_text(c_ptr(), data, int_size(value.size()), (void (*)(void*))unref);
214 }
215 else
216 {
217 unref(nullptr);
219 }
220 }
221
222 #if __cpp_char8_t >= 201811
224 void result_reference(const std::u8string_view & value, void (*unref)(const char8_t *) noexcept) noexcept
225 {
226 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
227 if (auto data = value.data())
228 {
229 sqlite3_result_text(c_ptr(), (const char *)data, int_size(value.size()), (void (*)(void *))unref);
230 }
231 else
232 {
233 unref(nullptr);
235 }
236 }
237 #endif
238
249 void result(const blob_view & value) noexcept
250 {
251 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
252 if (auto data = value.data())
253 sqlite3_result_blob(c_ptr(), data, int_size(value.size()), SQLITE_TRANSIENT);
254 else
256 }
257
269 void result_reference(const blob_view & value) noexcept
270 {
271 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
272 if (auto data = value.data())
273 sqlite3_result_blob(c_ptr(), data, int_size(value.size()), SQLITE_STATIC);
274 else
276 }
277
292 void result_reference(const blob_view & value, void (*unref)(const std::byte *) noexcept) noexcept
293 {
294 //passing a null pointer to sqlite3_result_ returns NULL not zero length text
295 if (auto data = value.data())
296 {
297 sqlite3_result_blob(c_ptr(), data, int_size(value.size()), (void (*)(void *))unref);
298 }
299 else
300 {
301 unref(nullptr);
303 }
304 }
305
306
316 void result(const zero_blob & value) noexcept
317 { sqlite3_result_zeroblob(c_ptr(), int_size(value.size())); }
318
320
330 template<class T, class D>
331 SQLITEPP_ENABLE_IF((std::is_convertible_v<D, void(*)(T *) noexcept>),
332 void) result(T * ptr, const char * type, D destroy) noexcept
333 { sqlite3_result_pointer(this->c_ptr(), ptr, type, (void(*)(void*))(void(*)(T *))destroy); }
334
345 template<class T>
346 void result(std::unique_ptr<T> ptr) noexcept
347 { this->result(ptr.release(), typeid(T).name(), [](T * p) noexcept { delete p;}); }
348
354 void result(const value & val) noexcept
355 { sqlite3_result_value(c_ptr(), val.c_ptr()); }
356
357#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 9, 0)
365 void result_subtype(unsigned value) noexcept
367#endif
368
376 template<class T>
377 T * get_auxdata(int arg) const noexcept
378 { return (T *)sqlite3_get_auxdata(this->c_ptr(), arg); }
379
387 template<class T>
388 void set_auxdata(int arg, T * data, void (*destroy)(T*)noexcept) noexcept
389 { sqlite3_set_auxdata(this->c_ptr(), arg, data, (void(*)(void*))destroy); }
390
400 template<class T>
401 T * user_data() noexcept
402 { return (T *)sqlite3_user_data(this->c_ptr()); }
403
404#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 22, 0)
410 bool vtab_nochange() const noexcept
411 { return sqlite3_vtab_nochange(c_ptr()); }
412#endif
413 };
414
416
417}
418
419
420#endif
sqlite3_aggregate_context
#define SQLITE_TRANSIENT
SQL Function Context Object.
Definition context_iface.hpp:38
void result(int value) noexcept
Return an int from the implemented SQL function.
Definition context_iface.hpp:116
void result_subtype(unsigned value) noexcept
Sets the subtype of the result of the implemented SQL function.
Definition context_iface.hpp:365
void result(double value) noexcept
Return a double from implemented SQL function.
Definition context_iface.hpp:132
~context() noexcept=delete
Contexts are never destroyed by user code.
class database & database() const noexcept
Retrieve database connection for the context.
Definition context_iface.hpp:56
void result(int64_t value) noexcept
Return an int64_t from implemented SQL function.
Definition context_iface.hpp:124
void error(int error_code) noexcept
Changes the error code returned by function evaluation.
Definition context_iface.hpp:84
void result(std::unique_ptr< T > ptr) noexcept
Return a custom pointer from the implemented SQL function.
Definition context_iface.hpp:346
void result_reference(const std::string_view &value) noexcept
Return a string by reference from the implemented SQL function.
Definition context_iface.hpp:175
T * get_auxdata(int arg) const noexcept
Get auxiliary data associated with argument values.
Definition context_iface.hpp:377
void result(const blob_view &value) noexcept
Return a blob by value from the implemented SQL function.
Definition context_iface.hpp:249
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:100
void result(const zero_blob &value) noexcept
Return a blob of zeroes from the implemented SQL function.
Definition context_iface.hpp:316
void result(const value &val) noexcept
Return a copy of the passed value from the implemented SQL function.
Definition context_iface.hpp:354
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:155
T * user_data() noexcept
Return the function's user data.
Definition context_iface.hpp:401
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:410
void result(std::nullptr_t) noexcept
Return NULL from the implemented SQL function.
Definition context_iface.hpp:108
void error_nomem() noexcept
Causes the implemented SQL function to throw an SQL exception indicating that a memory allocation fai...
Definition context_iface.hpp:92
void result(T *ptr, const char *type, D destroy) noexcept
Definition context_iface.hpp:332
void error(const std::string_view &value) noexcept
Cause the implemented SQL function to throw an SQL exception.
Definition context_iface.hpp:67
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:185
void result_reference(const std::u8string_view &value, void(*unref)(const char8_t *) noexcept) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition context_iface.hpp:224
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:292
void * aggregate_context(int size) noexcept
Allocate memory for aggregate function context.
Definition context_iface.hpp:48
void result_reference(const blob_view &value) noexcept
Return a blob by reference from the implemented SQL function.
Definition context_iface.hpp:269
void set_auxdata(int arg, T *data, void(*destroy)(T *) noexcept) noexcept
Associate auxiliary data with argument values.
Definition context_iface.hpp:388
void result(const std::string_view &value) noexcept
Return a string by value from the implemented SQL function.
Definition context_iface.hpp:145
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:71
void result_reference(const std::string_view &value, void(*unref)(const char *) noexcept) noexcept
Return a string by reference from the implemented SQL function.
Definition context_iface.hpp:208
Database Connection.
Definition database_iface.hpp:109
sqlite3_context * c_ptr() const noexcept
Definition handle.hpp:45
Dynamically Typed Value Object.
Definition value_iface.hpp:36
An efficient blob of zeroes of a given size.
Definition span.hpp:257
sqlite3_context_db_handle
sqlite3_get_auxdata
span< const std::byte > blob_view
A blob_view is a span of bytes.
Definition span.hpp:239
T is_convertible_v
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
sqlite3_result_error
sqlite3_result_subtype
sqlite3_user_data
sqlite3_vtab_nochange