Commit cf8e78a4 authored by Sergei Krivonos's avatar Sergei Krivonos

Implemented Json_writer_array & Json_writer_object subitems name presence control

parent df383043
...@@ -10,8 +10,11 @@ ...@@ -10,8 +10,11 @@
.ninja_* .ninja_*
*.mri *.mri
*.mri.tpl *.mri.tpl
/.cproject
/.project
.gdb_history .gdb_history
.vs/ .vs/
/.settings/
errmsg.sys errmsg.sys
typescript typescript
_CPack_Packages _CPack_Packages
......
...@@ -260,6 +260,8 @@ void Json_writer::add_str(const String &str) ...@@ -260,6 +260,8 @@ void Json_writer::add_str(const String &str)
add_str(str.ptr(), str.length()); add_str(str.ptr(), str.length());
} }
thread_local std::vector<bool> Json_writer_struct::named_items_expectation;
Json_writer_temp_disable::Json_writer_temp_disable(THD *thd_arg) Json_writer_temp_disable::Json_writer_temp_disable(THD *thd_arg)
{ {
thd= thd_arg; thd= thd_arg;
......
...@@ -15,8 +15,12 @@ ...@@ -15,8 +15,12 @@
#ifndef JSON_WRITER_INCLUDED #ifndef JSON_WRITER_INCLUDED
#define JSON_WRITER_INCLUDED #define JSON_WRITER_INCLUDED
#include "my_base.h" #include "my_base.h"
#include "sql_select.h" #include "sql_select.h"
#include <vector>
class Opt_trace_stmt; class Opt_trace_stmt;
class Opt_trace_context; class Opt_trace_context;
class Json_writer; class Json_writer;
...@@ -308,6 +312,7 @@ class Json_value_helper ...@@ -308,6 +312,7 @@ class Json_value_helper
/* A common base for Json_writer_object and Json_writer_array */ /* A common base for Json_writer_object and Json_writer_array */
class Json_writer_struct class Json_writer_struct
{ {
static thread_local std::vector<bool> named_items_expectation;
protected: protected:
Json_writer* my_writer; Json_writer* my_writer;
Json_value_helper context; Json_value_helper context;
...@@ -317,16 +322,29 @@ class Json_writer_struct ...@@ -317,16 +322,29 @@ class Json_writer_struct
bool closed; bool closed;
public: public:
explicit Json_writer_struct(THD *thd) explicit Json_writer_struct(THD *thd, bool expect_named_children)
{ {
my_writer= thd->opt_trace.get_current_json(); my_writer= thd->opt_trace.get_current_json();
context.init(my_writer); context.init(my_writer);
closed= false; closed= false;
named_items_expectation.push_back(expect_named_children);
}
virtual ~Json_writer_struct()
{
named_items_expectation.pop_back();
} }
bool trace_started()
bool trace_started() const
{ {
return my_writer != 0; return my_writer != 0;
} }
bool named_item_expected() const
{
return named_items_expectation.size() > 1
&& *(named_items_expectation.rbegin() + 1);
}
}; };
...@@ -347,15 +365,17 @@ class Json_writer_object : public Json_writer_struct ...@@ -347,15 +365,17 @@ class Json_writer_object : public Json_writer_struct
} }
public: public:
explicit Json_writer_object(THD *thd) explicit Json_writer_object(THD *thd)
: Json_writer_struct(thd) : Json_writer_struct(thd, true)
{ {
DBUG_ASSERT(!named_item_expected());
if (unlikely(my_writer)) if (unlikely(my_writer))
my_writer->start_object(); my_writer->start_object();
} }
explicit Json_writer_object(THD* thd, const char *str) explicit Json_writer_object(THD* thd, const char *str)
: Json_writer_struct(thd) : Json_writer_struct(thd, true)
{ {
DBUG_ASSERT(named_item_expected());
if (unlikely(my_writer)) if (unlikely(my_writer))
my_writer->add_member(str).start_object(); my_writer->add_member(str).start_object();
} }
...@@ -519,14 +539,18 @@ class Json_writer_object : public Json_writer_struct ...@@ -519,14 +539,18 @@ class Json_writer_object : public Json_writer_struct
class Json_writer_array : public Json_writer_struct class Json_writer_array : public Json_writer_struct
{ {
public: public:
Json_writer_array(THD *thd): Json_writer_struct(thd) Json_writer_array(THD *thd)
: Json_writer_struct(thd, false)
{ {
DBUG_ASSERT(!named_item_expected());
if (unlikely(my_writer)) if (unlikely(my_writer))
my_writer->start_array(); my_writer->start_array();
} }
Json_writer_array(THD *thd, const char *str) : Json_writer_struct(thd) Json_writer_array(THD *thd, const char *str)
: Json_writer_struct(thd, false)
{ {
DBUG_ASSERT(named_item_expected());
if (unlikely(my_writer)) if (unlikely(my_writer))
my_writer->add_member(str).start_array(); my_writer->add_member(str).start_array();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment