2021-11-16 15:30:45 +01:00
|
|
|
/* Copyright (C) 2014, 2021, MariaDB Corporation.
|
2014-12-02 00:14:17 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2019-05-11 21:19:05 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
2014-05-27 19:04:45 +02:00
|
|
|
|
2021-12-15 20:11:26 +01:00
|
|
|
#include "my_global.h"
|
2021-12-07 09:57:51 +01:00
|
|
|
#include "my_json_writer.h"
|
2014-05-27 19:04:45 +02:00
|
|
|
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
2021-11-12 02:36:10 +01:00
|
|
|
|
2021-10-24 23:19:37 +02:00
|
|
|
bool Json_writer::named_item_expected() const
|
|
|
|
{
|
|
|
|
return named_items_expectation.size()
|
|
|
|
&& named_items_expectation.back();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
void Json_writer::append_indent()
|
|
|
|
{
|
|
|
|
if (!document_start)
|
|
|
|
output.append('\n');
|
|
|
|
for (int i=0; i< indent_level; i++)
|
|
|
|
output.append(' ');
|
|
|
|
}
|
|
|
|
|
2021-10-24 23:19:37 +02:00
|
|
|
inline void Json_writer::on_start_object()
|
2014-05-27 19:04:45 +02:00
|
|
|
{
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
2021-11-05 12:36:59 +01:00
|
|
|
if(!fmt_helper.is_making_writer_calls())
|
2021-10-24 23:19:37 +02:00
|
|
|
{
|
2021-11-12 02:36:10 +01:00
|
|
|
if (got_name != named_item_expected())
|
|
|
|
{
|
|
|
|
sql_print_error(got_name
|
|
|
|
? "Json_writer got a member name which is not expected.\n"
|
|
|
|
: "Json_writer: a member name was expected.\n");
|
|
|
|
VALIDITY_ASSERT(got_name == named_item_expected());
|
|
|
|
}
|
2021-10-24 23:19:37 +02:00
|
|
|
named_items_expectation.push_back(true);
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-12 13:02:09 +02:00
|
|
|
fmt_helper.on_start_object();
|
2021-10-24 23:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Json_writer::start_object()
|
|
|
|
{
|
|
|
|
on_start_object();
|
2014-08-12 13:02:09 +02:00
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
if (!element_started)
|
|
|
|
start_element();
|
|
|
|
|
Reduce usage of strlen()
Changes:
- To detect automatic strlen() I removed the methods in String that
uses 'const char *' without a length:
- String::append(const char*)
- Binary_string(const char *str)
- String(const char *str, CHARSET_INFO *cs)
- append_for_single_quote(const char *)
All usage of append(const char*) is changed to either use
String::append(char), String::append(const char*, size_t length) or
String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
String::append()
- Added overflow argument to escape_string_for_mysql() and
escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
This was needed as most usage of the above functions never tested the
result for -1 and would have given wrong results or crashes in case
of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
Changed all Item_func::func_name()'s to func_name_cstring()'s.
The old Item_func_or_sum::func_name() is now an inline function that
returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
LEX_CSTRING.
- Changed for some functions the name argument from const char * to
to const LEX_CSTRING &:
- Item::Item_func_fix_attributes()
- Item::check_type_...()
- Type_std_attributes::agg_item_collations()
- Type_std_attributes::agg_item_set_converter()
- Type_std_attributes::agg_arg_charsets...()
- Type_handler_hybrid_field_type::aggregate_for_result()
- Type_handler_geometry::check_type_geom_or_binary()
- Type_handler::Item_func_or_sum_illegal_param()
- Predicant_to_list_comparator::add_value_skip_null()
- Predicant_to_list_comparator::add_value()
- cmp_item_row::prepare_comparators()
- cmp_item_row::aggregate_row_elements_for_comparison()
- Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
could be simplified to not use String_space(), thanks to the fixed
my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
- NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
clarify what the function really does.
- Rename of protocol function:
bool store(const char *from, CHARSET_INFO *cs) to
bool store_string_or_null(const char *from, CHARSET_INFO *cs).
This was done to both clarify the difference between this 'store' function
and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.
Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
2020-08-12 19:29:55 +02:00
|
|
|
output.append('{');
|
2014-05-27 19:04:45 +02:00
|
|
|
indent_level+=INDENT_SIZE;
|
|
|
|
first_child=true;
|
|
|
|
element_started= false;
|
|
|
|
document_start= false;
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
2021-10-24 23:19:37 +02:00
|
|
|
got_name= false;
|
2021-11-12 02:36:10 +01:00
|
|
|
named_items.emplace();
|
2021-10-24 23:19:37 +02:00
|
|
|
#endif
|
2014-05-27 19:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Json_writer::start_array()
|
|
|
|
{
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
2021-11-05 12:36:59 +01:00
|
|
|
if(!fmt_helper.is_making_writer_calls())
|
2021-10-24 23:19:37 +02:00
|
|
|
{
|
2021-11-05 18:01:43 +01:00
|
|
|
VALIDITY_ASSERT(got_name == named_item_expected());
|
2021-10-24 23:19:37 +02:00
|
|
|
named_items_expectation.push_back(false);
|
|
|
|
got_name= false;
|
2021-11-12 02:36:10 +01:00
|
|
|
if (document_start)
|
|
|
|
named_items.emplace();
|
2021-10-24 23:19:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
if (fmt_helper.on_start_array())
|
|
|
|
return;
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
if (!element_started)
|
|
|
|
start_element();
|
|
|
|
|
Reduce usage of strlen()
Changes:
- To detect automatic strlen() I removed the methods in String that
uses 'const char *' without a length:
- String::append(const char*)
- Binary_string(const char *str)
- String(const char *str, CHARSET_INFO *cs)
- append_for_single_quote(const char *)
All usage of append(const char*) is changed to either use
String::append(char), String::append(const char*, size_t length) or
String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
String::append()
- Added overflow argument to escape_string_for_mysql() and
escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
This was needed as most usage of the above functions never tested the
result for -1 and would have given wrong results or crashes in case
of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
Changed all Item_func::func_name()'s to func_name_cstring()'s.
The old Item_func_or_sum::func_name() is now an inline function that
returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
LEX_CSTRING.
- Changed for some functions the name argument from const char * to
to const LEX_CSTRING &:
- Item::Item_func_fix_attributes()
- Item::check_type_...()
- Type_std_attributes::agg_item_collations()
- Type_std_attributes::agg_item_set_converter()
- Type_std_attributes::agg_arg_charsets...()
- Type_handler_hybrid_field_type::aggregate_for_result()
- Type_handler_geometry::check_type_geom_or_binary()
- Type_handler::Item_func_or_sum_illegal_param()
- Predicant_to_list_comparator::add_value_skip_null()
- Predicant_to_list_comparator::add_value()
- cmp_item_row::prepare_comparators()
- cmp_item_row::aggregate_row_elements_for_comparison()
- Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
could be simplified to not use String_space(), thanks to the fixed
my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
- NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
clarify what the function really does.
- Rename of protocol function:
bool store(const char *from, CHARSET_INFO *cs) to
bool store_string_or_null(const char *from, CHARSET_INFO *cs).
This was done to both clarify the difference between this 'store' function
and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.
Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
2020-08-12 19:29:55 +02:00
|
|
|
output.append('[');
|
2014-05-27 19:04:45 +02:00
|
|
|
indent_level+=INDENT_SIZE;
|
|
|
|
first_child=true;
|
|
|
|
element_started= false;
|
|
|
|
document_start= false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Json_writer::end_object()
|
|
|
|
{
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
|
|
|
VALIDITY_ASSERT(named_item_expected());
|
2021-10-24 23:19:37 +02:00
|
|
|
named_items_expectation.pop_back();
|
2021-11-05 18:01:43 +01:00
|
|
|
VALIDITY_ASSERT(!got_name);
|
2021-10-24 23:19:37 +02:00
|
|
|
got_name= false;
|
2021-11-12 02:36:10 +01:00
|
|
|
VALIDITY_ASSERT(named_items.size());
|
|
|
|
named_items.pop();
|
2021-10-24 23:19:37 +02:00
|
|
|
#endif
|
2014-05-27 19:04:45 +02:00
|
|
|
indent_level-=INDENT_SIZE;
|
|
|
|
if (!first_child)
|
|
|
|
append_indent();
|
2019-01-10 01:25:23 +01:00
|
|
|
first_child= false;
|
Reduce usage of strlen()
Changes:
- To detect automatic strlen() I removed the methods in String that
uses 'const char *' without a length:
- String::append(const char*)
- Binary_string(const char *str)
- String(const char *str, CHARSET_INFO *cs)
- append_for_single_quote(const char *)
All usage of append(const char*) is changed to either use
String::append(char), String::append(const char*, size_t length) or
String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
String::append()
- Added overflow argument to escape_string_for_mysql() and
escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
This was needed as most usage of the above functions never tested the
result for -1 and would have given wrong results or crashes in case
of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
Changed all Item_func::func_name()'s to func_name_cstring()'s.
The old Item_func_or_sum::func_name() is now an inline function that
returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
LEX_CSTRING.
- Changed for some functions the name argument from const char * to
to const LEX_CSTRING &:
- Item::Item_func_fix_attributes()
- Item::check_type_...()
- Type_std_attributes::agg_item_collations()
- Type_std_attributes::agg_item_set_converter()
- Type_std_attributes::agg_arg_charsets...()
- Type_handler_hybrid_field_type::aggregate_for_result()
- Type_handler_geometry::check_type_geom_or_binary()
- Type_handler::Item_func_or_sum_illegal_param()
- Predicant_to_list_comparator::add_value_skip_null()
- Predicant_to_list_comparator::add_value()
- cmp_item_row::prepare_comparators()
- cmp_item_row::aggregate_row_elements_for_comparison()
- Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
could be simplified to not use String_space(), thanks to the fixed
my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
- NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
clarify what the function really does.
- Rename of protocol function:
bool store(const char *from, CHARSET_INFO *cs) to
bool store_string_or_null(const char *from, CHARSET_INFO *cs).
This was done to both clarify the difference between this 'store' function
and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.
Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
2020-08-12 19:29:55 +02:00
|
|
|
output.append('}');
|
2014-05-27 19:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Json_writer::end_array()
|
|
|
|
{
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
|
|
|
VALIDITY_ASSERT(!named_item_expected());
|
2021-10-24 23:19:37 +02:00
|
|
|
named_items_expectation.pop_back();
|
|
|
|
got_name= false;
|
|
|
|
#endif
|
2014-08-12 13:02:09 +02:00
|
|
|
if (fmt_helper.on_end_array())
|
|
|
|
return;
|
2014-05-27 19:04:45 +02:00
|
|
|
indent_level-=INDENT_SIZE;
|
|
|
|
if (!first_child)
|
|
|
|
append_indent();
|
Reduce usage of strlen()
Changes:
- To detect automatic strlen() I removed the methods in String that
uses 'const char *' without a length:
- String::append(const char*)
- Binary_string(const char *str)
- String(const char *str, CHARSET_INFO *cs)
- append_for_single_quote(const char *)
All usage of append(const char*) is changed to either use
String::append(char), String::append(const char*, size_t length) or
String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
String::append()
- Added overflow argument to escape_string_for_mysql() and
escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
This was needed as most usage of the above functions never tested the
result for -1 and would have given wrong results or crashes in case
of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
Changed all Item_func::func_name()'s to func_name_cstring()'s.
The old Item_func_or_sum::func_name() is now an inline function that
returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
LEX_CSTRING.
- Changed for some functions the name argument from const char * to
to const LEX_CSTRING &:
- Item::Item_func_fix_attributes()
- Item::check_type_...()
- Type_std_attributes::agg_item_collations()
- Type_std_attributes::agg_item_set_converter()
- Type_std_attributes::agg_arg_charsets...()
- Type_handler_hybrid_field_type::aggregate_for_result()
- Type_handler_geometry::check_type_geom_or_binary()
- Type_handler::Item_func_or_sum_illegal_param()
- Predicant_to_list_comparator::add_value_skip_null()
- Predicant_to_list_comparator::add_value()
- cmp_item_row::prepare_comparators()
- cmp_item_row::aggregate_row_elements_for_comparison()
- Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
could be simplified to not use String_space(), thanks to the fixed
my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
- NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
clarify what the function really does.
- Rename of protocol function:
bool store(const char *from, CHARSET_INFO *cs) to
bool store_string_or_null(const char *from, CHARSET_INFO *cs).
This was done to both clarify the difference between this 'store' function
and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.
Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
2020-08-12 19:29:55 +02:00
|
|
|
output.append(']');
|
2014-05-27 19:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Json_writer& Json_writer::add_member(const char *name)
|
|
|
|
{
|
2020-03-06 09:33:11 +01:00
|
|
|
size_t len= strlen(name);
|
2021-10-24 23:19:37 +02:00
|
|
|
return add_member(name, len);
|
2020-03-06 09:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Json_writer& Json_writer::add_member(const char *name, size_t len)
|
|
|
|
{
|
2021-10-24 23:19:37 +02:00
|
|
|
if (!fmt_helper.on_add_member(name, len))
|
|
|
|
{
|
|
|
|
// assert that we are in an object
|
|
|
|
DBUG_ASSERT(!element_started);
|
|
|
|
start_element();
|
2020-03-06 09:33:11 +01:00
|
|
|
|
2021-10-24 23:19:37 +02:00
|
|
|
output.append('"');
|
|
|
|
output.append(name, len);
|
2021-11-16 15:30:45 +01:00
|
|
|
output.append(STRING_WITH_LEN("\": "));
|
2021-10-24 23:19:37 +02:00
|
|
|
}
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
2021-11-05 12:36:59 +01:00
|
|
|
if (!fmt_helper.is_making_writer_calls())
|
2021-11-12 02:36:10 +01:00
|
|
|
{
|
|
|
|
VALIDITY_ASSERT(!got_name);
|
2021-10-24 23:19:37 +02:00
|
|
|
got_name= true;
|
2021-11-12 02:36:10 +01:00
|
|
|
VALIDITY_ASSERT(named_items.size());
|
|
|
|
auto& named_items_keys= named_items.top();
|
|
|
|
auto emplaced= named_items_keys.emplace(name, len);
|
|
|
|
auto is_uniq_key= emplaced.second;
|
|
|
|
if(!is_uniq_key)
|
|
|
|
{
|
|
|
|
sql_print_error("Duplicated key: %s\n", emplaced.first->c_str());
|
|
|
|
VALIDITY_ASSERT(is_uniq_key);
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 23:19:37 +02:00
|
|
|
#endif
|
2014-05-27 19:04:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Used by formatting helper to print something that is formatted by the helper.
|
|
|
|
We should only separate it from the previous element.
|
|
|
|
*/
|
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
void Json_writer::start_sub_element()
|
|
|
|
{
|
|
|
|
//element_started= true;
|
|
|
|
if (first_child)
|
|
|
|
first_child= false;
|
|
|
|
else
|
|
|
|
output.append(',');
|
|
|
|
|
|
|
|
append_indent();
|
|
|
|
}
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char buf[64];
|
2017-01-16 11:50:12 +01:00
|
|
|
my_snprintf(buf, sizeof(buf), "%lld", val);
|
2014-08-12 13:02:09 +02:00
|
|
|
add_unquoted_str(buf);
|
2014-05-27 19:04:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 22:54:46 +02:00
|
|
|
void Json_writer::add_ull(ulonglong val)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
my_snprintf(buf, sizeof(buf), "%llu", val);
|
|
|
|
add_unquoted_str(buf);
|
|
|
|
}
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
|
2015-04-12 03:48:42 +02:00
|
|
|
/* Add a memory size, printing in Kb, Kb, Gb if necessary */
|
|
|
|
void Json_writer::add_size(longlong val)
|
|
|
|
{
|
|
|
|
char buf[64];
|
2020-03-06 09:33:11 +01:00
|
|
|
size_t len;
|
2015-04-12 03:48:42 +02:00
|
|
|
if (val < 1024)
|
2020-03-06 09:33:11 +01:00
|
|
|
len= my_snprintf(buf, sizeof(buf), "%lld", val);
|
2015-04-12 03:48:42 +02:00
|
|
|
else if (val < 1024*1024*16)
|
|
|
|
{
|
|
|
|
/* Values less than 16MB are specified in KB for precision */
|
2020-03-06 09:33:11 +01:00
|
|
|
len= my_snprintf(buf, sizeof(buf), "%lld", val/1024);
|
2015-04-12 03:48:42 +02:00
|
|
|
strcpy(buf + len, "Kb");
|
2020-03-06 09:33:11 +01:00
|
|
|
len+= 2;
|
2015-04-12 03:48:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-06 09:33:11 +01:00
|
|
|
len= my_snprintf(buf, sizeof(buf), "%lld", val/(1024*1024));
|
2015-04-12 03:48:42 +02:00
|
|
|
strcpy(buf + len, "Mb");
|
2020-03-06 09:33:11 +01:00
|
|
|
len+= 2;
|
2015-04-12 03:48:42 +02:00
|
|
|
}
|
2020-03-06 09:33:11 +01:00
|
|
|
add_str(buf, len);
|
2015-04-12 03:48:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
void Json_writer::add_double(double val)
|
|
|
|
{
|
2014-08-12 13:02:09 +02:00
|
|
|
char buf[64];
|
2020-04-17 16:41:49 +02:00
|
|
|
size_t len= my_snprintf(buf, sizeof(buf), "%-.11lg", val);
|
2020-03-06 09:33:11 +01:00
|
|
|
add_unquoted_str(buf, len);
|
2014-08-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Json_writer::add_bool(bool val)
|
|
|
|
{
|
|
|
|
add_unquoted_str(val? "true" : "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-29 01:07:24 +01:00
|
|
|
void Json_writer::add_null()
|
|
|
|
{
|
2020-03-06 09:33:11 +01:00
|
|
|
add_unquoted_str("null", (size_t) 4);
|
2014-11-29 01:07:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
void Json_writer::add_unquoted_str(const char* str)
|
|
|
|
{
|
2020-03-06 09:33:11 +01:00
|
|
|
size_t len= strlen(str);
|
2021-10-24 23:19:37 +02:00
|
|
|
add_unquoted_str(str, len);
|
2020-03-06 09:33:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Json_writer::add_unquoted_str(const char* str, size_t len)
|
|
|
|
{
|
2021-11-05 18:01:43 +01:00
|
|
|
VALIDITY_ASSERT(fmt_helper.is_making_writer_calls() ||
|
|
|
|
got_name == named_item_expected());
|
2021-10-24 23:19:37 +02:00
|
|
|
if (on_add_str(str, len))
|
2014-08-12 13:02:09 +02:00
|
|
|
return;
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
if (!element_started)
|
|
|
|
start_element();
|
|
|
|
|
2020-03-06 09:33:11 +01:00
|
|
|
output.append(str, len);
|
2014-05-27 19:04:45 +02:00
|
|
|
element_started= false;
|
|
|
|
}
|
|
|
|
|
2021-10-24 23:19:37 +02:00
|
|
|
inline bool Json_writer::on_add_str(const char *str, size_t num_bytes)
|
|
|
|
{
|
2021-11-08 20:11:05 +01:00
|
|
|
#if !defined(NDEBUG) || defined(JSON_WRITER_UNIT_TEST)
|
2021-10-24 23:19:37 +02:00
|
|
|
got_name= false;
|
|
|
|
#endif
|
|
|
|
bool helped= fmt_helper.on_add_str(str, num_bytes);
|
|
|
|
return helped;
|
|
|
|
}
|
|
|
|
|
2014-05-27 19:04:45 +02:00
|
|
|
void Json_writer::add_str(const char *str)
|
|
|
|
{
|
2020-03-06 09:33:11 +01:00
|
|
|
size_t len= strlen(str);
|
2021-10-24 23:19:37 +02:00
|
|
|
add_str(str, len);
|
2014-05-27 19:04:45 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 01:25:23 +01:00
|
|
|
/*
|
|
|
|
This function is used to add only num_bytes of str to the output string
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Json_writer::add_str(const char* str, size_t num_bytes)
|
|
|
|
{
|
2021-11-05 18:01:43 +01:00
|
|
|
VALIDITY_ASSERT(fmt_helper.is_making_writer_calls() ||
|
|
|
|
got_name == named_item_expected());
|
2021-10-24 23:19:37 +02:00
|
|
|
if (on_add_str(str, num_bytes))
|
2019-01-10 01:25:23 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!element_started)
|
|
|
|
start_element();
|
|
|
|
|
|
|
|
output.append('"');
|
|
|
|
output.append(str, num_bytes);
|
|
|
|
output.append('"');
|
|
|
|
element_started= false;
|
|
|
|
}
|
2014-05-27 19:04:45 +02:00
|
|
|
|
|
|
|
void Json_writer::add_str(const String &str)
|
|
|
|
{
|
2019-01-10 01:25:23 +01:00
|
|
|
add_str(str.ptr(), str.length());
|
2014-05-27 19:04:45 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 11:36:12 +02:00
|
|
|
#ifdef ENABLED_JSON_WRITER_CONSISTENCY_CHECKS
|
2021-10-16 01:35:16 +02:00
|
|
|
thread_local std::vector<bool> Json_writer_struct::named_items_expectation;
|
2021-10-17 11:36:12 +02:00
|
|
|
#endif
|
2021-10-16 01:35:16 +02:00
|
|
|
|
2019-02-18 12:41:20 +01:00
|
|
|
Json_writer_temp_disable::Json_writer_temp_disable(THD *thd_arg)
|
|
|
|
{
|
|
|
|
thd= thd_arg;
|
|
|
|
thd->opt_trace.disable_tracing_if_required();
|
|
|
|
}
|
|
|
|
Json_writer_temp_disable::~Json_writer_temp_disable()
|
|
|
|
{
|
|
|
|
thd->opt_trace.enable_tracing_if_required();
|
|
|
|
}
|
|
|
|
|
2020-03-06 09:33:11 +01:00
|
|
|
bool Single_line_formatting_helper::on_add_member(const char *name,
|
|
|
|
size_t len)
|
2014-08-12 13:02:09 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(state== INACTIVE || state == DISABLED);
|
|
|
|
if (state != DISABLED)
|
|
|
|
{
|
|
|
|
// remove everything from the array
|
|
|
|
buf_ptr= buffer;
|
|
|
|
|
|
|
|
//append member name to the array
|
|
|
|
if (len < MAX_LINE_LEN)
|
|
|
|
{
|
|
|
|
memcpy(buf_ptr, name, len);
|
|
|
|
buf_ptr+=len;
|
|
|
|
*(buf_ptr++)= 0;
|
|
|
|
|
2018-02-06 13:55:58 +01:00
|
|
|
line_len= owner->indent_level + (uint)len + 1;
|
2014-08-12 13:02:09 +02:00
|
|
|
state= ADD_MEMBER;
|
|
|
|
return true; // handled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false; // not handled
|
|
|
|
}
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
bool Single_line_formatting_helper::on_start_array()
|
|
|
|
{
|
|
|
|
if (state == ADD_MEMBER)
|
|
|
|
{
|
|
|
|
state= IN_ARRAY;
|
|
|
|
return true; // handled
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-06 18:13:38 +01:00
|
|
|
if (state != DISABLED)
|
|
|
|
state= INACTIVE;
|
2014-08-12 13:02:09 +02:00
|
|
|
// TODO: what if we have accumulated some stuff already? shouldn't we
|
|
|
|
// flush it?
|
|
|
|
return false; // not handled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
bool Single_line_formatting_helper::on_end_array()
|
|
|
|
{
|
|
|
|
if (state == IN_ARRAY)
|
|
|
|
{
|
|
|
|
flush_on_one_line();
|
|
|
|
state= INACTIVE;
|
|
|
|
return true; // handled
|
|
|
|
}
|
|
|
|
return false; // not handled
|
|
|
|
}
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
void Single_line_formatting_helper::on_start_object()
|
|
|
|
{
|
|
|
|
// Nested objects will not be printed on one line
|
|
|
|
disable_and_flush();
|
|
|
|
}
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
|
2019-01-10 01:25:23 +01:00
|
|
|
bool Single_line_formatting_helper::on_add_str(const char *str,
|
2020-03-06 09:33:11 +01:00
|
|
|
size_t len)
|
2014-08-12 13:02:09 +02:00
|
|
|
{
|
|
|
|
if (state == IN_ARRAY)
|
|
|
|
{
|
|
|
|
// New length will be:
|
|
|
|
// "$string",
|
|
|
|
// quote + quote + comma + space = 4
|
|
|
|
if (line_len + len + 4 > MAX_LINE_LEN)
|
|
|
|
{
|
|
|
|
disable_and_flush();
|
|
|
|
return false; // didn't handle the last element
|
|
|
|
}
|
|
|
|
|
|
|
|
//append string to array
|
|
|
|
memcpy(buf_ptr, str, len);
|
|
|
|
buf_ptr+=len;
|
|
|
|
*(buf_ptr++)= 0;
|
2018-02-06 13:55:58 +01:00
|
|
|
line_len += (uint)len + 4;
|
2014-08-12 13:02:09 +02:00
|
|
|
return true; // handled
|
|
|
|
}
|
|
|
|
|
|
|
|
disable_and_flush();
|
|
|
|
return false; // not handled
|
|
|
|
}
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Append everything accumulated to the output on one line
|
|
|
|
*/
|
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
void Single_line_formatting_helper::flush_on_one_line()
|
|
|
|
{
|
|
|
|
owner->start_sub_element();
|
|
|
|
char *ptr= buffer;
|
|
|
|
int nr= 0;
|
|
|
|
while (ptr < buf_ptr)
|
|
|
|
{
|
|
|
|
char *str= ptr;
|
|
|
|
|
|
|
|
if (nr == 0)
|
|
|
|
{
|
|
|
|
owner->output.append('"');
|
|
|
|
owner->output.append(str);
|
Reduce usage of strlen()
Changes:
- To detect automatic strlen() I removed the methods in String that
uses 'const char *' without a length:
- String::append(const char*)
- Binary_string(const char *str)
- String(const char *str, CHARSET_INFO *cs)
- append_for_single_quote(const char *)
All usage of append(const char*) is changed to either use
String::append(char), String::append(const char*, size_t length) or
String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
String::append()
- Added overflow argument to escape_string_for_mysql() and
escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
This was needed as most usage of the above functions never tested the
result for -1 and would have given wrong results or crashes in case
of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
Changed all Item_func::func_name()'s to func_name_cstring()'s.
The old Item_func_or_sum::func_name() is now an inline function that
returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
LEX_CSTRING.
- Changed for some functions the name argument from const char * to
to const LEX_CSTRING &:
- Item::Item_func_fix_attributes()
- Item::check_type_...()
- Type_std_attributes::agg_item_collations()
- Type_std_attributes::agg_item_set_converter()
- Type_std_attributes::agg_arg_charsets...()
- Type_handler_hybrid_field_type::aggregate_for_result()
- Type_handler_geometry::check_type_geom_or_binary()
- Type_handler::Item_func_or_sum_illegal_param()
- Predicant_to_list_comparator::add_value_skip_null()
- Predicant_to_list_comparator::add_value()
- cmp_item_row::prepare_comparators()
- cmp_item_row::aggregate_row_elements_for_comparison()
- Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
could be simplified to not use String_space(), thanks to the fixed
my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
- NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
clarify what the function really does.
- Rename of protocol function:
bool store(const char *from, CHARSET_INFO *cs) to
bool store_string_or_null(const char *from, CHARSET_INFO *cs).
This was done to both clarify the difference between this 'store' function
and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.
Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
2020-08-12 19:29:55 +02:00
|
|
|
owner->output.append(STRING_WITH_LEN("\": "));
|
2014-08-12 13:02:09 +02:00
|
|
|
owner->output.append('[');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nr != 1)
|
Reduce usage of strlen()
Changes:
- To detect automatic strlen() I removed the methods in String that
uses 'const char *' without a length:
- String::append(const char*)
- Binary_string(const char *str)
- String(const char *str, CHARSET_INFO *cs)
- append_for_single_quote(const char *)
All usage of append(const char*) is changed to either use
String::append(char), String::append(const char*, size_t length) or
String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
String::append()
- Added overflow argument to escape_string_for_mysql() and
escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
This was needed as most usage of the above functions never tested the
result for -1 and would have given wrong results or crashes in case
of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
Changed all Item_func::func_name()'s to func_name_cstring()'s.
The old Item_func_or_sum::func_name() is now an inline function that
returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
LEX_CSTRING.
- Changed for some functions the name argument from const char * to
to const LEX_CSTRING &:
- Item::Item_func_fix_attributes()
- Item::check_type_...()
- Type_std_attributes::agg_item_collations()
- Type_std_attributes::agg_item_set_converter()
- Type_std_attributes::agg_arg_charsets...()
- Type_handler_hybrid_field_type::aggregate_for_result()
- Type_handler_geometry::check_type_geom_or_binary()
- Type_handler::Item_func_or_sum_illegal_param()
- Predicant_to_list_comparator::add_value_skip_null()
- Predicant_to_list_comparator::add_value()
- cmp_item_row::prepare_comparators()
- cmp_item_row::aggregate_row_elements_for_comparison()
- Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
could be simplified to not use String_space(), thanks to the fixed
my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
- NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
clarify what the function really does.
- Rename of protocol function:
bool store(const char *from, CHARSET_INFO *cs) to
bool store_string_or_null(const char *from, CHARSET_INFO *cs).
This was done to both clarify the difference between this 'store' function
and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.
Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
2020-08-12 19:29:55 +02:00
|
|
|
owner->output.append(STRING_WITH_LEN(", "));
|
2014-08-12 13:02:09 +02:00
|
|
|
owner->output.append('"');
|
|
|
|
owner->output.append(str);
|
|
|
|
owner->output.append('"');
|
|
|
|
}
|
|
|
|
nr++;
|
|
|
|
|
|
|
|
while (*ptr!=0)
|
|
|
|
ptr++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
owner->output.append(']');
|
2016-08-25 18:47:38 +02:00
|
|
|
/* We've printed out the contents of the buffer, mark it as empty */
|
|
|
|
buf_ptr= buffer;
|
2014-08-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Single_line_formatting_helper::disable_and_flush()
|
|
|
|
{
|
2014-12-06 18:13:38 +01:00
|
|
|
if (state == DISABLED)
|
|
|
|
return;
|
|
|
|
|
2014-11-27 17:32:48 +01:00
|
|
|
bool start_array= (state == IN_ARRAY);
|
2014-08-12 13:02:09 +02:00
|
|
|
state= DISABLED;
|
|
|
|
// deactivate ourselves and flush all accumulated calls.
|
|
|
|
char *ptr= buffer;
|
|
|
|
int nr= 0;
|
|
|
|
while (ptr < buf_ptr)
|
|
|
|
{
|
|
|
|
char *str= ptr;
|
2020-03-06 09:33:11 +01:00
|
|
|
size_t len= strlen(str);
|
|
|
|
|
2014-08-12 13:02:09 +02:00
|
|
|
if (nr == 0)
|
|
|
|
{
|
2020-03-06 09:33:11 +01:00
|
|
|
owner->add_member(str, len);
|
2014-11-27 17:32:48 +01:00
|
|
|
if (start_array)
|
|
|
|
owner->start_array();
|
2014-08-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-27 17:32:48 +01:00
|
|
|
//if (nr == 1)
|
|
|
|
// owner->start_array();
|
2020-03-06 09:33:11 +01:00
|
|
|
owner->add_str(str, len);
|
2014-08-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nr++;
|
2020-03-06 09:33:11 +01:00
|
|
|
ptr+= len+1;
|
2014-08-12 13:02:09 +02:00
|
|
|
}
|
|
|
|
buf_ptr= buffer;
|
|
|
|
state= INACTIVE;
|
|
|
|
}
|
|
|
|
|