ThinSQLite++
A thin, safe and convenient modern C++ wrapper for SQLite API.
Loading...
Searching...
No Matches
value_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_VALUE_IFACE_INCLUDED
10#define HEADER_SQLITEPP_VALUE_IFACE_INCLUDED
11
12#include "handle.hpp"
13#include "span.hpp"
14#include "exception_iface.hpp"
15
16#include <memory>
17#include <string_view>
18
19namespace thinsqlitepp
20{
34 class value final : public handle<sqlite3_value, value>
35 {
36 public:
37#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 18, 11)
49 static std::unique_ptr<value> dup(const value * src)
50 {
51 auto ret = sqlite3_value_dup(src->c_ptr());
52 if (!ret && src)
53 throw exception(SQLITE_NOMEM);
54 return std::unique_ptr<value>((value *)ret);
55 }
56
59 { return dup(src.get()); }
60
66 ~value() noexcept
68#endif
69
70 private:
71 template<typename T>
72 static constexpr bool supported_column_type =
77 #if __cpp_char8_t >= 201811
79 #endif
81
82 public:
83
98 template<class T>
99 SQLITEPP_ENABLE_IF(supported_column_type<T>,
100 T) get() const noexcept;
101
102 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 20, 0)
103
120 template<class T>
121 SQLITEPP_ENABLE_IF(std::is_pointer_v<T>,
122 T) get(const char * type = nullptr) const noexcept
123 { return sqlite3_value_pointer(c_ptr(), type ? type : typeid(T).name()); }
124
125
126 #endif
127
135 int type() const noexcept
136 { return sqlite3_value_type(c_ptr()); }
137
138#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 9, 0)
146 unsigned subtype()const noexcept
147 { return sqlite3_value_subtype(c_ptr()); }
148#endif
149
157 int numeric_type() const noexcept
158 { return sqlite3_value_numeric_type(c_ptr()); }
159
160#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 22, 0)
168 bool nochange() const noexcept
169 { return sqlite3_value_nochange(c_ptr()); }
170#endif
171
172#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 28, 0)
180 bool frombind() const noexcept
181 { return sqlite3_value_frombind(c_ptr()); }
182#endif
183
184#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 38, 0)
190 value * in_first() const
191 {
192 sqlite3_value * ret;
193 int res = sqlite3_vtab_in_first(c_ptr(), &ret);
194 if (res != SQLITE_OK && res != SQLITE_DONE)
195 throw exception(res);
196 return from(ret);
197 }
198
204 value * in_next() const
205 {
206 sqlite3_value * ret;
207 int res = sqlite3_vtab_in_next(c_ptr(), &ret);
208 if (res != SQLITE_OK && res != SQLITE_DONE)
209 throw exception(res);
210 return from(ret);
211 }
212#endif
213 };
214
218
219 template<>
220 inline int value::get<int>() const noexcept
221 { return sqlite3_value_int(c_ptr()); }
222
223 template<>
224 inline int64_t value::get<int64_t>() const noexcept
225 { return sqlite3_value_int64(c_ptr()); }
226
227 template<>
228 inline std::string_view value::get<std::string_view>() const noexcept
229 {
230 auto first = (const char *)sqlite3_value_text(c_ptr());
231 auto size = (size_t)sqlite3_value_bytes(c_ptr());
232 return std::string_view(first, size);
233 }
234
235#if __cpp_char8_t >= 201811
236 template<>
238 {
239 auto first = (const char8_t *)sqlite3_value_text(c_ptr());
240 auto size = (size_t)sqlite3_value_bytes(c_ptr());
241 return std::u8string_view(first, size);
242 }
243#endif
244
245 template<>
246 inline double value::get<double>() const noexcept
247 { return sqlite3_value_double(c_ptr()); }
248
249 template<>
250 inline blob_view value::get<blob_view>() const noexcept
251 {
252 auto first = (const std::byte *)sqlite3_value_blob(c_ptr());
254 return blob_view(first, first + size);
255 }
256
258}
259
260
261#endif
262
Exception used to report any SQLite errors.
Definition exception_iface.hpp:166
Base functionality for all fake wrapper classes
Definition handle.hpp:27
T * c_ptr() const noexcept
Access the real underlying SQLite type.
Definition handle.hpp:45
static value * from(sqlite3_value *obj) noexcept
Definition handle.hpp:41
Dynamically Typed Value Object.
Definition value_iface.hpp:35
bool nochange() const noexcept
Whether the column is unchanged in an UPDATE against a virtual table.
Definition value_iface.hpp:168
static std::unique_ptr< value > dup(const std::unique_ptr< value > &src)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition value_iface.hpp:58
value * in_next() const
Get next element on the right-hand side of an IN constraint.
Definition value_iface.hpp:204
unsigned subtype() const noexcept
Subtype of the value.
Definition value_iface.hpp:146
T get(const char *type=nullptr) const noexcept
Obtain a pointer stored in the value.
Definition value_iface.hpp:122
T get() const noexcept
Obtain value's content.
~value() noexcept
Equivalent to sqlite3_value_free.
Definition value_iface.hpp:66
int numeric_type() const noexcept
Best numeric datatype of the value.
Definition value_iface.hpp:157
int type() const noexcept
Default datatype of the value.
Definition value_iface.hpp:135
value * in_first() const
Get first element on the right-hand side of an IN constraint.
Definition value_iface.hpp:190
bool frombind() const noexcept
Whether if value originated from a bound parameter
Definition value_iface.hpp:180
static std::unique_ptr< value > dup(const value *src)
Creates a new value by copying an original one.
Definition value_iface.hpp:49
T get(T... args)
span< const std::byte > blob_view
A blob_view is a span of bytes.
Definition span.hpp:239
T is_pointer_v
T is_same_v
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_DONE
#define SQLITE_OK
T size(T... args)
sqlite3_value_pointer
sqlite3_value_dup
sqlite3_value_subtype
sqlite3_vtab_in_first