summaryrefslogtreecommitdiff
path: root/sql/my_json_writer.cc
diff options
context:
space:
mode:
authorSergei Petrunia <psergey@askmonty.org>2014-05-27 21:04:45 +0400
committerSergei Petrunia <psergey@askmonty.org>2014-05-27 21:04:45 +0400
commit5cfd3270ec79238b27765af3062ae7d97f6f06d0 (patch)
tree2bfa2bb941567c1eb7f81d7b86962973e6fbf1c6 /sql/my_json_writer.cc
parent0925ab9d88f4328810d25392333a4cac11c6e694 (diff)
downloadmariadb-git-5cfd3270ec79238b27765af3062ae7d97f6f06d0.tar.gz
MDEV-6109: EXPLAIN JSON10.1-explain-json
- First code, "EXPLAIN FORMAT=JSON stmt" and "ANALYZE FORMAT=JSON stmt" work for basic queries. Complex constructs (e.g subqueries, etc) not yet supported. - No test infrastructure yet
Diffstat (limited to 'sql/my_json_writer.cc')
-rw-r--r--sql/my_json_writer.cc129
1 files changed, 129 insertions, 0 deletions
diff --git a/sql/my_json_writer.cc b/sql/my_json_writer.cc
new file mode 100644
index 00000000000..206597da59e
--- /dev/null
+++ b/sql/my_json_writer.cc
@@ -0,0 +1,129 @@
+/* Todo: SkySQL copyrights */
+
+#include <my_global.h>
+#include "sql_priv.h"
+#include "sql_string.h"
+
+#include "my_json_writer.h"
+
+void Json_writer::append_indent()
+{
+ if (!document_start)
+ output.append('\n');
+ for (int i=0; i< indent_level; i++)
+ output.append(' ');
+}
+
+void Json_writer::start_object()
+{
+ if (!element_started)
+ start_element();
+
+ output.append("{");
+ indent_level+=INDENT_SIZE;
+ first_child=true;
+ element_started= false;
+ document_start= false;
+}
+
+void Json_writer::start_array()
+{
+ if (!element_started)
+ start_element();
+
+ output.append("[");
+ indent_level+=INDENT_SIZE;
+ first_child=true;
+ element_started= false;
+ document_start= false;
+}
+
+
+void Json_writer::end_object()
+{
+ indent_level-=INDENT_SIZE;
+ if (!first_child)
+ append_indent();
+ output.append("}");
+}
+
+
+void Json_writer::end_array()
+{
+ indent_level-=INDENT_SIZE;
+ if (!first_child)
+ append_indent();
+ output.append("]");
+}
+
+
+Json_writer& Json_writer::add_member(const char *name)
+{
+ // assert that we are in an object
+ DBUG_ASSERT(!element_started);
+ start_element();
+
+ output.append('"');
+ output.append(name);
+ output.append("\": ");
+ return *this;
+}
+
+
+void Json_writer::start_element()
+{
+ element_started= true;
+
+ if (first_child)
+ first_child= false;
+ else
+ output.append(',');
+
+ append_indent();
+}
+
+void Json_writer::add_ll(longlong val)
+{
+ if (!element_started)
+ start_element();
+
+ char buf[64];
+ my_snprintf(buf, sizeof(buf), "%ld", val);
+ output.append(buf);
+ element_started= false;
+}
+
+
+void Json_writer::add_double(double val)
+{
+ if (!element_started)
+ start_element();
+
+ char buf[64];
+ my_snprintf(buf, sizeof(buf), "%lf", val);
+ output.append(buf);
+ element_started= false;
+}
+
+
+void Json_writer::add_str(const char *str)
+{
+ if (!element_started)
+ start_element();
+
+ output.append('"');
+ output.append(str);
+ output.append('"');
+ element_started= false;
+}
+
+void Json_writer::add_bool(bool val)
+{
+ add_str(val? "true" : "false");
+}
+
+void Json_writer::add_str(const String &str)
+{
+ add_str(str.ptr());
+}
+