2009-09-23 23:32:31 +02:00
|
|
|
#ifndef ITEM_XMLFUNC_INCLUDED
|
|
|
|
#define ITEM_XMLFUNC_INCLUDED
|
|
|
|
|
2019-03-07 14:08:19 +01:00
|
|
|
/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
|
2019-09-26 09:06:54 +02:00
|
|
|
Copyright (c) 2009, 2019, MariaDB
|
2005-12-21 14:13:52 +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
|
2006-12-27 02:23:51 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2005-12-21 14:13:52 +01:00
|
|
|
|
|
|
|
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 20:29:06 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
2005-12-21 14:13:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* This file defines all XML functions */
|
|
|
|
|
|
|
|
|
2014-03-23 12:15:07 +01:00
|
|
|
typedef struct my_xml_node_st MY_XML_NODE;
|
|
|
|
|
|
|
|
|
2019-09-26 09:06:54 +02:00
|
|
|
/* Structure to store nodeset elements */
|
|
|
|
class MY_XPATH_FLT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
uint num; // Absolute position in MY_XML_NODE array
|
|
|
|
uint pos; // Relative position in context
|
|
|
|
uint size; // Context size
|
|
|
|
public:
|
|
|
|
MY_XPATH_FLT(uint32 num_arg, uint32 pos_arg)
|
|
|
|
:num(num_arg), pos(pos_arg), size(0)
|
|
|
|
{ }
|
|
|
|
MY_XPATH_FLT(uint32 num_arg, uint32 pos_arg, uint32 size_arg)
|
|
|
|
:num(num_arg), pos(pos_arg), size(size_arg)
|
|
|
|
{ }
|
|
|
|
bool append_to(Native *to) const
|
|
|
|
{
|
|
|
|
return to->append((const char*) this, (uint32) sizeof(*this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class NativeNodesetBuffer: public NativeBuffer<16*sizeof(MY_XPATH_FLT)>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const MY_XPATH_FLT &element(uint i) const
|
|
|
|
{
|
|
|
|
const MY_XPATH_FLT *p= (MY_XPATH_FLT*) (ptr() + i * sizeof(MY_XPATH_FLT));
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
uint32 elements() const
|
|
|
|
{
|
|
|
|
return length() / sizeof(MY_XPATH_FLT);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-12-21 14:13:52 +01:00
|
|
|
class Item_xml_str_func: public Item_str_func
|
|
|
|
{
|
|
|
|
protected:
|
2014-03-23 12:15:07 +01:00
|
|
|
/*
|
|
|
|
A helper class to store raw and parsed XML.
|
|
|
|
*/
|
|
|
|
class XML
|
|
|
|
{
|
|
|
|
bool m_cached;
|
|
|
|
String *m_raw_ptr; // Pointer to text representation
|
|
|
|
String m_raw_buf; // Cached text representation
|
|
|
|
String m_parsed_buf; // Array of MY_XML_NODEs, pointing to raw_buffer
|
|
|
|
bool parse();
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_cached= false;
|
|
|
|
m_raw_ptr= (String *) 0;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
XML() { reset(); }
|
|
|
|
void set_charset(CHARSET_INFO *cs) { m_parsed_buf.set_charset(cs); }
|
|
|
|
String *raw() { return m_raw_ptr; }
|
|
|
|
String *parsed() { return &m_parsed_buf; }
|
|
|
|
const MY_XML_NODE *node(uint idx);
|
|
|
|
bool cached() { return m_cached; }
|
|
|
|
bool parse(String *raw, bool cache);
|
|
|
|
bool parse(Item *item, bool cache)
|
|
|
|
{
|
|
|
|
String *res;
|
|
|
|
if (!(res= item->val_str(&m_raw_buf)))
|
|
|
|
{
|
|
|
|
m_raw_ptr= (String *) 0;
|
|
|
|
m_cached= cache;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return parse(res, cache);
|
|
|
|
}
|
|
|
|
};
|
2018-01-30 08:07:35 +01:00
|
|
|
String m_xpath_query; // XPath query text
|
2005-12-21 14:13:52 +01:00
|
|
|
Item *nodeset_func;
|
2014-03-23 12:15:07 +01:00
|
|
|
XML xml;
|
2015-07-06 19:24:14 +02:00
|
|
|
bool get_xml(XML *xml_arg, bool cache= false)
|
2014-03-23 12:15:07 +01:00
|
|
|
{
|
2015-07-06 19:24:14 +02:00
|
|
|
if (!cache && xml_arg->cached())
|
|
|
|
return xml_arg->raw() == 0;
|
|
|
|
return xml_arg->parse(args[0], cache);
|
2014-03-23 12:15:07 +01:00
|
|
|
}
|
2005-12-21 14:13:52 +01:00
|
|
|
public:
|
2015-08-11 09:18:38 +02:00
|
|
|
Item_xml_str_func(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b)
|
2013-09-25 14:30:13 +02:00
|
|
|
{
|
2020-09-02 02:13:32 +02:00
|
|
|
set_maybe_null();
|
2013-09-25 14:30:13 +02:00
|
|
|
}
|
2015-08-11 09:18:38 +02:00
|
|
|
Item_xml_str_func(THD *thd, Item *a, Item *b, Item *c):
|
|
|
|
Item_str_func(thd, a, b, c)
|
2013-09-25 14:30:13 +02:00
|
|
|
{
|
2020-09-02 02:13:32 +02:00
|
|
|
set_maybe_null();
|
2013-09-25 14:30:13 +02:00
|
|
|
}
|
2020-08-19 01:53:22 +02:00
|
|
|
bool fix_fields(THD *thd, Item **ref) override;
|
|
|
|
bool fix_length_and_dec() override;
|
|
|
|
bool const_item() const override
|
2014-03-23 12:15:07 +01:00
|
|
|
{
|
|
|
|
return const_item_cache && (!nodeset_func || nodeset_func->const_item());
|
|
|
|
}
|
2005-12-21 14:13:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_func_xml_extractvalue: public Item_xml_str_func
|
|
|
|
{
|
|
|
|
public:
|
2015-08-11 09:18:38 +02:00
|
|
|
Item_func_xml_extractvalue(THD *thd, Item *a, Item *b):
|
|
|
|
Item_xml_str_func(thd, a, b) {}
|
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
|
|
|
LEX_CSTRING func_name_cstring() const override
|
|
|
|
{
|
|
|
|
static LEX_CSTRING name= {STRING_WITH_LEN("extractvalue") };
|
|
|
|
return name;
|
|
|
|
}
|
2020-08-19 01:53:22 +02:00
|
|
|
String *val_str(String *) override;
|
|
|
|
Item *get_copy(THD *thd) override
|
2017-11-22 07:01:07 +01:00
|
|
|
{ return get_item_copy<Item_func_xml_extractvalue>(thd, this); }
|
2005-12-21 14:13:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_func_xml_update: public Item_xml_str_func
|
|
|
|
{
|
2019-09-26 09:06:54 +02:00
|
|
|
NativeNodesetBuffer tmp_native_value2;
|
|
|
|
String tmp_value3;
|
2014-03-23 12:15:07 +01:00
|
|
|
bool collect_result(String *str,
|
|
|
|
const MY_XML_NODE *cut,
|
|
|
|
const String *replace);
|
2005-12-21 14:13:52 +01:00
|
|
|
public:
|
2015-08-11 09:18:38 +02:00
|
|
|
Item_func_xml_update(THD *thd, Item *a, Item *b, Item *c):
|
|
|
|
Item_xml_str_func(thd, a, b, c) {}
|
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
|
|
|
LEX_CSTRING func_name_cstring() const override
|
|
|
|
{
|
|
|
|
static LEX_CSTRING name= {STRING_WITH_LEN("updatexml") };
|
|
|
|
return name;
|
|
|
|
}
|
2020-08-19 01:53:22 +02:00
|
|
|
String *val_str(String *) override;
|
|
|
|
Item *get_copy(THD *thd) override
|
2017-11-22 07:01:07 +01:00
|
|
|
{ return get_item_copy<Item_func_xml_update>(thd, this); }
|
2005-12-21 14:13:52 +01:00
|
|
|
};
|
|
|
|
|
2009-09-23 23:32:31 +02:00
|
|
|
#endif /* ITEM_XMLFUNC_INCLUDED */
|