ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the 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/master/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{
25
34 SQLITEPP_EXPORTED
35 class value final : public handle<sqlite3_value, value>
36 {
37 public:
38#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 18, 0)
50 static std::unique_ptr<value> dup(const value * src)
51 {
52 auto ret = sqlite3_value_dup(src ? src->c_ptr() : nullptr);
53 if (!ret && src)
54 throw exception(SQLITE_NOMEM);
55 return std::unique_ptr<value>((value *)ret);
56 }
57
60 { return dup(src.get()); }
61
67 ~value() noexcept
69#endif
70
71 private:
72 template<typename T>
73 static constexpr bool supported_column_type =
78 #if __cpp_char8_t >= 201811
80 #endif
82
83 public:
84
99 template<class T>
100 SQLITEPP_ENABLE_IF(supported_column_type<T>,
101 T) get() const noexcept;
102
103 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 20, 0)
104
121 template<class T>
122 SQLITEPP_ENABLE_IF(std::is_pointer_v<T>,
123 T) get(const char * type = nullptr) const noexcept
124 { return static_cast<T>(sqlite3_value_pointer(c_ptr(), type ? type : typeid(T).name())); }
125
126
127 #endif
128
136 int type() const noexcept
137 { return sqlite3_value_type(c_ptr()); }
138
139#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 9, 0)
147 unsigned subtype()const noexcept
148 { return sqlite3_value_subtype(c_ptr()); }
149#endif
150
158 int numeric_type() const noexcept
159 { return sqlite3_value_numeric_type(c_ptr()); }
160
161#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 22, 0)
169 bool nochange() const noexcept
170 { return sqlite3_value_nochange(c_ptr()); }
171#endif
172
173#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 28, 0)
181 bool frombind() const noexcept
182 { return sqlite3_value_frombind(c_ptr()); }
183#endif
184
185#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 38, 0)
191 value * in_first() const
192 {
193 sqlite3_value * ret;
194 int res = sqlite3_vtab_in_first(c_ptr(), &ret);
195 if (res != SQLITE_OK && res != SQLITE_DONE)
196 throw exception(res);
197 return from(ret);
198 }
199
205 value * in_next() const
206 {
207 sqlite3_value * ret;
208 int res = sqlite3_vtab_in_next(c_ptr(), &ret);
209 if (res != SQLITE_OK && res != SQLITE_DONE)
210 throw exception(res);
211 return from(ret);
212 }
213#endif
214 };
215
217
219
220 template<>
221 inline int value::get<int>() const noexcept
222 { return sqlite3_value_int(c_ptr()); }
223
224 template<>
225 inline int64_t value::get<int64_t>() const noexcept
226 { return sqlite3_value_int64(c_ptr()); }
227
228 template<>
229 inline std::string_view value::get<std::string_view>() const noexcept
230 {
231 auto first = (const char *)sqlite3_value_text(c_ptr());
232 auto size = (size_t)sqlite3_value_bytes(c_ptr());
233 return std::string_view(first, size);
234 }
235
236#if __cpp_char8_t >= 201811
237 template<>
238 inline std::u8string_view value::get<std::u8string_view>() const noexcept
239 {
240 auto first = (const char8_t *)sqlite3_value_text(c_ptr());
241 auto size = (size_t)sqlite3_value_bytes(c_ptr());
242 return std::u8string_view(first, size);
243 }
244#endif
245
246 template<>
247 inline double value::get<double>() const noexcept
248 { return sqlite3_value_double(c_ptr()); }
249
250 template<>
251 inline blob_view value::get<blob_view>() const noexcept
252 {
253 auto first = (const std::byte *)sqlite3_value_blob(c_ptr());
255 return blob_view(first, first + size);
256 }
257
259}
260
261
262#endif
263
Exception used to report any SQLite errors.
Definition exception_iface.hpp:180
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:36
bool nochange() const noexcept
Whether the column is unchanged in an UPDATE against a virtual table.
Definition value_iface.hpp:169
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:59
value * in_next() const
Get next element on the right-hand side of an IN constraint.
Definition value_iface.hpp:205
unsigned subtype() const noexcept
Subtype of the value.
Definition value_iface.hpp:147
T get(const char *type=nullptr) const noexcept
Obtain a pointer stored in the value.
Definition value_iface.hpp:123
T get() const noexcept
Obtain value's content.
~value() noexcept
Equivalent to sqlite3_value_free.
Definition value_iface.hpp:67
int numeric_type() const noexcept
Best numeric datatype of the value.
Definition value_iface.hpp:158
int type() const noexcept
Default datatype of the value.
Definition value_iface.hpp:136
value * in_first() const
Get first element on the right-hand side of an IN constraint.
Definition value_iface.hpp:191
bool frombind() const noexcept
Whether if value originated from a bound parameter.
Definition value_iface.hpp:181
static std::unique_ptr< value > dup(const value *src)
Creates a new value by copying an original one.
Definition value_iface.hpp:50
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