summaryrefslogtreecommitdiffstats
path: root/src/json-extension-functions.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/json-extension-functions.cc72
1 files changed, 36 insertions, 36 deletions
diff --git a/src/json-extension-functions.cc b/src/json-extension-functions.cc
index 36dc290..034b713 100644
--- a/src/json-extension-functions.cc
+++ b/src/json-extension-functions.cc
@@ -43,10 +43,10 @@
#include "yajlpp/json_op.hh"
#include "yajlpp/yajlpp.hh"
-using namespace mapbox;
-
#define JSON_SUBTYPE 74 /* Ascii for "J" */
+namespace {
+
class sql_json_op : public json_op {
public:
explicit sql_json_op(json_ptr& ptr) : json_op(ptr){};
@@ -68,7 +68,8 @@ null_or_default(sqlite3_context* context, int argc, sqlite3_value* argv[])
}
struct contains_userdata {
- util::variant<string_fragment, sqlite3_int64, bool> cu_match_value{false};
+ mapbox::util::variant<string_fragment, sqlite3_int64, bool> cu_match_value{
+ false};
size_t cu_depth{0};
bool cu_result{false};
};
@@ -119,12 +120,10 @@ json_contains(vtab_types::nullable<const char> nullable_json_in,
}
const auto* json_in = nullable_json_in.n_value;
- auto_mem<yajl_handle_t> handle(yajl_free);
- yajl_callbacks cb;
- contains_userdata cu;
- memset(&cb, 0, sizeof(cb));
- handle = yajl_alloc(&cb, nullptr, &cu);
+ yajl_callbacks cb{};
+ contains_userdata cu;
+ auto handle = yajlpp::alloc_handle(&cb, &cu);
cb.yajl_start_array = +[](void* ctx) {
auto& cu = *((contains_userdata*) ctx);
@@ -266,17 +265,16 @@ sql_jget(sqlite3_context* context, int argc, sqlite3_value** argv)
return;
}
- const char* json_in = (const char*) sqlite3_value_text(argv[0]);
+ const auto json_in = from_sqlite<string_fragment>()(argc, argv, 0);
if (sqlite3_value_type(argv[1]) == SQLITE_NULL) {
- sqlite3_result_text(context, json_in, -1, SQLITE_TRANSIENT);
+ sqlite3_result_text(context, json_in.data(), -1, SQLITE_TRANSIENT);
return;
}
const char* ptr_in = (const char*) sqlite3_value_text(argv[1]);
json_ptr jp(ptr_in);
sql_json_op jo(jp);
- auto_mem<yajl_handle_t> handle(yajl_free);
unsigned char* err;
yajlpp_gen gen;
@@ -289,16 +287,15 @@ sql_jget(sqlite3_context* context, int argc, sqlite3_value** argv)
jo.jo_ptr_callbacks.yajl_number = gen_handle_number;
jo.jo_ptr_data = gen.get_handle();
- handle.reset(yajl_alloc(&json_op::ptr_callbacks, nullptr, &jo));
- switch (yajl_parse(
- handle.in(), (const unsigned char*) json_in, strlen(json_in)))
- {
+ auto handle = yajlpp::alloc_handle(&json_op::ptr_callbacks, &jo);
+ switch (yajl_parse(handle.in(), json_in.udata(), json_in.length())) {
case yajl_status_error: {
- err = yajl_get_error(handle.in(),
- 1,
- (const unsigned char*) json_in,
- strlen(json_in));
- sqlite3_result_error(context, (const char*) err, -1);
+ err = yajl_get_error(
+ handle.in(), 1, json_in.udata(), json_in.length());
+ auto um = lnav::console::user_message::error("invalid JSON")
+ .with_reason((const char*) err);
+
+ to_sqlite(context, um);
yajl_free_error(handle.in(), err);
return;
}
@@ -318,11 +315,12 @@ sql_jget(sqlite3_context* context, int argc, sqlite3_value** argv)
switch (yajl_complete_parse(handle.in())) {
case yajl_status_error: {
- err = yajl_get_error(handle.in(),
- 1,
- (const unsigned char*) json_in,
- strlen(json_in));
- sqlite3_result_error(context, (const char*) err, -1);
+ err = yajl_get_error(
+ handle.in(), 1, json_in.udata(), json_in.length());
+ auto um = lnav::console::user_message::error("invalid JSON")
+ .with_reason((const char*) err);
+
+ to_sqlite(context, um);
yajl_free_error(handle.in(), err);
return;
}
@@ -342,10 +340,7 @@ sql_jget(sqlite3_context* context, int argc, sqlite3_value** argv)
switch (jo.sjo_type) {
case SQLITE3_TEXT:
- sqlite3_result_text(context,
- jo.sjo_str.c_str(),
- jo.sjo_str.size(),
- SQLITE_TRANSIENT);
+ to_sqlite(context, jo.sjo_str);
return;
case SQLITE_NULL:
sqlite3_result_null(context);
@@ -358,7 +353,7 @@ sql_jget(sqlite3_context* context, int argc, sqlite3_value** argv)
return;
}
- string_fragment result = gen.to_string_fragment();
+ const auto result = gen.to_string_fragment();
if (result.empty()) {
null_or_default(context, argc, argv);
@@ -468,7 +463,6 @@ concat_gen_end_array(void* ctx)
static void
concat_gen_elements(yajl_gen gen, const unsigned char* text, size_t len)
{
- auto_mem<yajl_handle_t> handle(yajl_free);
yajl_callbacks callbacks = {nullptr};
concat_context cc{gen};
@@ -482,7 +476,7 @@ concat_gen_elements(yajl_gen gen, const unsigned char* text, size_t len)
callbacks.yajl_start_array = concat_gen_start_array;
callbacks.yajl_end_array = concat_gen_end_array;
- handle = yajl_alloc(&callbacks, nullptr, &cc);
+ auto handle = yajlpp::alloc_handle(&callbacks, &cc);
yajl_config(handle, yajl_allow_comments, 1);
if (yajl_parse(handle, (const unsigned char*) text, len) != yajl_status_ok
|| yajl_complete_parse(handle) != yajl_status_ok)
@@ -600,7 +594,7 @@ sql_json_group_object_step(sqlite3_context* context,
return;
}
- json_agg_context* jac = (json_agg_context*) sqlite3_aggregate_context(
+ auto* jac = (json_agg_context*) sqlite3_aggregate_context(
context, sizeof(json_agg_context));
if (jac->jac_yajl_gen == nullptr) {
@@ -662,8 +656,7 @@ sql_json_group_object_step(sqlite3_context* context,
static void
sql_json_group_object_final(sqlite3_context* context)
{
- json_agg_context* jac
- = (json_agg_context*) sqlite3_aggregate_context(context, 0);
+ auto* jac = (json_agg_context*) sqlite3_aggregate_context(context, 0);
if (jac == nullptr) {
sqlite3_result_text(context, "{}", -1, SQLITE_STATIC);
@@ -686,7 +679,7 @@ sql_json_group_array_step(sqlite3_context* context,
int argc,
sqlite3_value** argv)
{
- json_agg_context* jac = (json_agg_context*) sqlite3_aggregate_context(
+ auto* jac = (json_agg_context*) sqlite3_aggregate_context(
context, sizeof(json_agg_context));
if (jac->jac_yajl_gen == nullptr) {
@@ -759,6 +752,8 @@ sql_json_group_array_final(sqlite3_context* context)
}
}
+} // namespace
+
int
json_extension_functions(struct FuncDef** basic_funcs,
struct FuncDefAgg** agg_funcs)
@@ -778,6 +773,7 @@ json_extension_functions(struct FuncDef** basic_funcs,
"array with "
"two elements: the initial value and the given value.")
.sql_function()
+ .with_prql_path({"json", "concat"})
.with_parameter({"json", "The initial JSON value."})
.with_parameter(
help_text("value",
@@ -801,6 +797,7 @@ json_extension_functions(struct FuncDef** basic_funcs,
help_text("json_contains",
"Check if a JSON value contains the given element.")
.sql_function()
+ .with_prql_path({"json", "contains"})
.with_parameter({"json", "The JSON value to query."})
.with_parameter(
{"value", "The value to look for in the first argument"})
@@ -823,6 +820,7 @@ json_extension_functions(struct FuncDef** basic_funcs,
help_text("jget",
"Get the value from a JSON object using a JSON-Pointer.")
.sql_function()
+ .with_prql_path({"json", "get"})
.with_parameter({"json", "The JSON object to query."})
.with_parameter(
{"ptr", "The JSON-Pointer to lookup in the object."})
@@ -864,6 +862,7 @@ json_extension_functions(struct FuncDef** basic_funcs,
sql_json_group_object_final,
help_text("json_group_object")
.sql_function()
+ .with_prql_path({"json", "group_object"})
.with_summary(
"Collect the given values from a query into a JSON object")
.with_parameter(
@@ -888,6 +887,7 @@ json_extension_functions(struct FuncDef** basic_funcs,
sql_json_group_array_final,
help_text("json_group_array")
.sql_function()
+ .with_prql_path({"json", "group_array"})
.with_summary(
"Collect the given values from a query into a JSON array")
.with_parameter(