diff options
author | Sergei Petrunia <psergey@askmonty.org> | 2014-11-27 19:32:48 +0300 |
---|---|---|
committer | Sergei Petrunia <psergey@askmonty.org> | 2014-11-27 19:32:48 +0300 |
commit | 37c444e1a079b25d0a34efbbc2fadfae17999966 (patch) | |
tree | 9f6a6cb52ad06d5fd7caf23bd401414421432caa /sql/my_json_writer.h | |
parent | 3d5f97fd708e12201636179baee2c8bc0093c109 (diff) | |
download | mariadb-git-37c444e1a079b25d0a34efbbc2fadfae17999966.tar.gz |
EXPLAIN FORMAT=JSON: further development
Writing JSON:
- Fix a bug in Single_line_formatting_helper
- Add Json_writer_nesting_guard - safety class
EXPLAIN JSON support
- Add basic subquery support
- Add tests for UNION/UNION ALL.
Diffstat (limited to 'sql/my_json_writer.h')
-rw-r--r-- | sql/my_json_writer.h | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/sql/my_json_writer.h b/sql/my_json_writer.h index d92f98f20d8..35bd5cba544 100644 --- a/sql/my_json_writer.h +++ b/sql/my_json_writer.h @@ -34,9 +34,30 @@ class Single_line_formatting_helper DISABLED }; + /* + This works like a finite automaton. + + state=DISABLED means the helper is disabled - all on_XXX functions will + return false (which means "not handled") and do nothing. + + +->-+ + | v + INACTIVE ---> ADD_MEMBER ---> IN_ARRAY--->-+ + ^ | + +------------------<--------------------+ + + For other states: + INACTIVE - initial state, we have nothing. + ADD_MEMBER - add_member() was called, the buffer has "member_name\0". + IN_ARRAY - start_array() was called. + + + */ enum enum_state state; enum { MAX_LINE_LEN= 80 }; char buffer[80]; + + /* The data in the buffer is located between buffer[0] and buf_ptr */ char *buf_ptr; uint line_len; @@ -99,8 +120,9 @@ private: // TODO: a stack of (name, bool is_object_or_array) elements. int indent_level; enum { INDENT_SIZE = 2 }; - + friend class Single_line_formatting_helper; + friend class Json_writer_nesting_guard; bool document_start; bool element_started; bool first_child; @@ -116,3 +138,40 @@ public: String output; }; + +/* + RAII-based helper class to detect incorrect use of Json_writer. + + The idea is that a function typically must leave Json_writer at the same + identation level as it was when it was invoked. Leaving it at a different + level typically means we forgot to close an object or an array + + So, here is a way to guard + void foo(Json_writer *writer) + { + Json_writer_nesting_guard(writer); + .. do something with writer + + // at the end of the function, ~Json_writer_nesting_guard() is called + // and it makes sure that the nesting is the same as when the function was + // entered. + } +*/ + +class Json_writer_nesting_guard +{ + Json_writer* writer; + int indent_level; +public: + Json_writer_nesting_guard(Json_writer *writer_arg) : + writer(writer_arg), + indent_level(writer->indent_level) + {} + + ~Json_writer_nesting_guard() + { + DBUG_ASSERT(indent_level == writer->indent_level); + } +}; + + |