1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_SQLITE_JSON_HPP
#define BOOST_SQLITE_JSON_HPP
#include <boost/sqlite/detail/config.hpp>
#include <boost/sqlite/field.hpp>
#include <boost/sqlite/resultset.hpp>
#include <boost/sqlite/value.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/serializer.hpp>
#include <boost/json/storage_ptr.hpp>
#include <boost/json/value_from.hpp>
BOOST_SQLITE_BEGIN_NAMESPACE
struct resultset;
struct field;
struct value;
/// @brief The subtype value used by the sqlite json extension. See the [sqlite reference](https://www.sqlite.org/json1.html)
constexpr int json_subtype = static_cast<int>('J');
inline void tag_invoke(const struct set_result_tag &, sqlite3_context * ctx, const json::value & value)
{
json::serializer ser;
ser.reset(&value);
sqlite3_int64 len = 4096;
unique_ptr<char> c{static_cast<char*>(sqlite3_malloc64(len))};
len = sqlite3_msize(c.get());
auto v = ser.read(c.get(), len);
while (!ser.done())
{
auto l = v.size();
len *= 2;
c.reset(static_cast<char*>(sqlite3_realloc(c.release(), len)));
v = ser.read(c.get() + l, len);
}
sqlite3_result_text(ctx, c.release(), v.size(), sqlite3_free);
sqlite3_result_subtype(ctx, json_subtype);
}
///@{
/// @brief Check if the value or field is a json. @ingroup reference
inline bool is_json(const value & v) { return v.type() == value_type::text && v.subtype() == json_subtype; }
inline bool is_json(const field & f) { return f.type() == value_type::text && f.get_value().subtype() == json_subtype; }
///@}
///@{
/// @brief Convert the value or field to a json. @ingroup reference
inline json::value as_json(const value & v, json::storage_ptr ptr = {})
{
return json::parse(v.get_text(), ptr);
}
inline json::value as_json(const field & f, json::storage_ptr ptr = {})
{
return json::parse(f.get_text(), ptr);
}
///@}
inline void tag_invoke( const json::value_from_tag &, json::value& val, const value & f)
{
switch (f.type())
{
case value_type::integer:
val.emplace_int64() = f.get_int();
break;
case value_type::floating:
val.emplace_double() = f.get_double();
break;
case value_type::text:
{
auto txt = f.get_text();
if (f.subtype() == json_subtype)
val = json::parse(txt, val.storage());
else
val.emplace_string() = txt;
}
break;
case value_type::blob:
throw_exception(std::invalid_argument("cannot convert blob to json"));
case value_type::null:
default:
val.emplace_null();
}
}
inline void tag_invoke( const json::value_from_tag &, json::value& val, const field & f)
{
switch (f.type())
{
case value_type::integer:
val.emplace_int64() = f.get_int();
break;
case value_type::floating:
val.emplace_double() = f.get_double();
break;
case value_type::text:
{
auto txt = f.get_text();
if (f.get_value().subtype() == json_subtype)
val = json::parse(txt, val.storage());
else
val.emplace_string() = txt;
}
break;
case value_type::blob:
throw_exception(std::invalid_argument("cannot convert blob to json"));
case value_type::null:
default:
val.emplace_null();
}
}
inline void tag_invoke( const json::value_from_tag &, json::value& val, resultset && rs)
{
auto & obj = val.emplace_array();
for (auto r : rs)
{
auto & row = obj.emplace_back(json::object(obj.storage())).get_object();
for (auto c : r)
row[c.column_name()] = json::value_from(c, row.storage());
}
}
BOOST_SQLITE_END_NAMESPACE
#endif //BOOST_SQLITE_JSON_HPP
|