1 #ifndef ITEM_XMLFUNC_INCLUDED
2 #define ITEM_XMLFUNC_INCLUDED
3 
4 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; version 2 of the License.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
18 
19 
20 /* This file defines all XML functions */
21 
22 
23 typedef struct my_xml_node_st MY_XML_NODE;
24 
25 
26 class Item_xml_str_func: public Item_str_func
27 {
28 protected:
29   /*
30     A helper class to store raw and parsed XML.
31   */
32   class XML
33   {
34     bool m_cached;
35     String *m_raw_ptr;   // Pointer to text representation
36     String m_raw_buf;    // Cached text representation
37     String m_parsed_buf; // Array of MY_XML_NODEs, pointing to raw_buffer
38     bool parse();
reset()39     void reset()
40     {
41       m_cached= false;
42       m_raw_ptr= (String *) 0;
43     }
44   public:
XML()45     XML() { reset(); }
set_charset(CHARSET_INFO * cs)46     void set_charset(CHARSET_INFO *cs) { m_parsed_buf.set_charset(cs); }
raw()47     String *raw() { return m_raw_ptr; }
parsed()48     String *parsed() { return &m_parsed_buf; }
49     const MY_XML_NODE *node(uint idx);
cached()50     bool cached() { return m_cached; }
51     bool parse(String *raw, bool cache);
parse(Item * item,bool cache)52     bool parse(Item *item, bool cache)
53     {
54       String *res;
55       if (!(res= item->val_str(&m_raw_buf)))
56       {
57         m_raw_ptr= (String *) 0;
58         m_cached= cache;
59         return true;
60       }
61       return parse(res, cache);
62     }
63   };
64   String m_xpath_query; // XPath query text
65   Item *nodeset_func;
66   XML xml;
67   bool get_xml(XML *xml_arg, bool cache= false)
68   {
69     if (!cache && xml_arg->cached())
70       return xml_arg->raw() == 0;
71     return xml_arg->parse(args[0], cache);
72   }
73 public:
Item_xml_str_func(THD * thd,Item * a,Item * b)74   Item_xml_str_func(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b)
75   {
76     maybe_null= TRUE;
77   }
Item_xml_str_func(THD * thd,Item * a,Item * b,Item * c)78   Item_xml_str_func(THD *thd, Item *a, Item *b, Item *c):
79     Item_str_func(thd, a, b, c)
80   {
81     maybe_null= TRUE;
82   }
83   bool fix_fields(THD *thd, Item **ref);
84   bool fix_length_and_dec();
const_item()85   bool const_item() const
86   {
87     return const_item_cache && (!nodeset_func || nodeset_func->const_item());
88   }
89 };
90 
91 
92 class Item_func_xml_extractvalue: public Item_xml_str_func
93 {
94 public:
Item_func_xml_extractvalue(THD * thd,Item * a,Item * b)95   Item_func_xml_extractvalue(THD *thd, Item *a, Item *b):
96     Item_xml_str_func(thd, a, b) {}
func_name()97   const char *func_name() const { return "extractvalue"; }
98   String *val_str(String *);
get_copy(THD * thd)99   Item *get_copy(THD *thd)
100   { return get_item_copy<Item_func_xml_extractvalue>(thd, this); }
101 };
102 
103 
104 class Item_func_xml_update: public Item_xml_str_func
105 {
106   String tmp_value2, tmp_value3;
107   bool collect_result(String *str,
108                       const MY_XML_NODE *cut,
109                       const String *replace);
110 public:
Item_func_xml_update(THD * thd,Item * a,Item * b,Item * c)111   Item_func_xml_update(THD *thd, Item *a, Item *b, Item *c):
112     Item_xml_str_func(thd, a, b, c) {}
func_name()113   const char *func_name() const { return "updatexml"; }
114   String *val_str(String *);
get_copy(THD * thd)115   Item *get_copy(THD *thd)
116   { return get_item_copy<Item_func_xml_update>(thd, this); }
117 };
118 
119 #endif /* ITEM_XMLFUNC_INCLUDED */
120