summaryrefslogtreecommitdiff
path: root/sql/item_jsonfunc.h
diff options
context:
space:
mode:
authorAlexey Botchkov <holyfoot@askmonty.org>2016-10-19 14:10:03 +0400
committerAlexey Botchkov <holyfoot@askmonty.org>2016-10-19 14:10:03 +0400
commit27025221fe2ea17aa737ad2ad31011407c00dcc9 (patch)
tree0f5352944d26aceb427320756c90bde3b00f13a3 /sql/item_jsonfunc.h
parent8303aded294ce905bbc513e7ee42623d5f1fdb50 (diff)
downloadmariadb-git-27025221fe2ea17aa737ad2ad31011407c00dcc9.tar.gz
MDEV-9143 JSON_xxx functions.
strings/json_lib.c added as a JSON library. SQL frunction added with sql/item_jsonfunc.h/cc
Diffstat (limited to 'sql/item_jsonfunc.h')
-rw-r--r--sql/item_jsonfunc.h304
1 files changed, 304 insertions, 0 deletions
diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h
new file mode 100644
index 00000000000..54da67b5ab9
--- /dev/null
+++ b/sql/item_jsonfunc.h
@@ -0,0 +1,304 @@
+#ifndef ITEM_JSONFUNC_INCLUDED
+#define ITEM_JSONFUNC_INCLUDED
+
+/* Copyright (c) 2016, MariaDB
+
+ 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
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+
+
+/* This file defines all JSON functions */
+
+
+#include <json_lib.h>
+#include "item_cmpfunc.h" // Item_bool_func
+#include "item_strfunc.h" // Item_str_func
+
+
+class json_path_with_flags
+{
+public:
+ json_path_t p;
+ bool constant;
+ bool parsed;
+ json_path_step_t *cur_step;
+ void set_constant_flag(bool s_constant)
+ {
+ constant= s_constant;
+ parsed= FALSE;
+ }
+};
+
+
+class Item_func_json_valid: public Item_int_func
+{
+protected:
+ String tmp_value;
+
+public:
+ Item_func_json_valid(THD *thd, Item *json) : Item_int_func(thd, json) {}
+ longlong val_int();
+ const char *func_name() const { return "json_valid"; }
+ void fix_length_and_dec()
+ {
+ Item_int_func::fix_length_and_dec();
+ maybe_null= 1;
+ }
+ bool is_bool_type() { return true; }
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_valid>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_exists: public Item_int_func
+{
+protected:
+ json_path_with_flags path;
+ String tmp_js, tmp_path;
+
+public:
+ Item_func_json_exists(THD *thd, Item *js, Item *path):
+ Item_int_func(thd, js, path) {}
+ const char *func_name() const { return "json_exists"; }
+ bool is_bool_type() { return true; }
+ void fix_length_and_dec();
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_exists>(thd, mem_root, this); }
+ longlong val_int();
+};
+
+
+class Item_func_json_value: public Item_str_func
+{
+protected:
+ json_path_with_flags path;
+ String tmp_js, tmp_path;
+
+public:
+ Item_func_json_value(THD *thd, Item *js, Item *path):
+ Item_str_func(thd, js, path) {}
+ const char *func_name() const { return "json_value"; }
+ void fix_length_and_dec();
+ String *val_str(String *);
+ virtual bool check_and_get_value(json_engine_t *je, String *res, int *error);
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_value>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_query: public Item_func_json_value
+{
+public:
+ Item_func_json_query(THD *thd, Item *js, Item *path):
+ Item_func_json_value(thd, js, path) {}
+ const char *func_name() const { return "json_query"; }
+ bool check_and_get_value(json_engine_t *je, String *res, int *error);
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_query>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_quote: public Item_str_func
+{
+protected:
+ String tmp_s;
+
+public:
+ Item_func_json_quote(THD *thd, Item *s): Item_str_func(thd, s) {}
+ const char *func_name() const { return "json_quote"; }
+ void fix_length_and_dec();
+ String *val_str(String *);
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_quote>(thd, mem_root, this); }
+};
+
+
+class Item_json_str_multipath: public Item_str_func
+{
+protected:
+ json_path_with_flags *paths;
+ String *tmp_paths;
+public:
+ Item_json_str_multipath(THD *thd, List<Item> &list):
+ Item_str_func(thd, list), tmp_paths(0) {}
+ bool fix_fields(THD *thd, Item **ref);
+ void cleanup();
+ virtual uint get_n_paths() const = 0;
+};
+
+
+class Item_func_json_extract: public Item_json_str_multipath
+{
+protected:
+ String tmp_js;
+public:
+ Item_func_json_extract(THD *thd, List<Item> &list):
+ Item_json_str_multipath(thd, list) {}
+ const char *func_name() const { return "json_extract"; }
+ void fix_length_and_dec();
+ String *val_str(String *);
+ longlong val_int();
+ uint get_n_paths() const { return arg_count - 1; }
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_extract>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_contains: public Item_int_func
+{
+protected:
+ String tmp_js;
+ json_path_with_flags *paths;
+ String *tmp_paths;
+ bool a2_constant, a2_parsed;
+ String tmp_val, *val;
+public:
+ Item_func_json_contains(THD *thd, List<Item> &list):
+ Item_int_func(thd, list), tmp_paths(0) {}
+ const char *func_name() const { return "json_contains"; }
+ bool fix_fields(THD *thd, Item **ref);
+ void fix_length_and_dec();
+ void cleanup();
+ longlong val_int();
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_contains>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_contains_path: public Item_int_func
+{
+protected:
+ String tmp_js;
+ json_path_with_flags *paths;
+ String *tmp_paths;
+ bool mode_one;
+ bool ooa_constant, ooa_parsed;
+
+public:
+ Item_func_json_contains_path(THD *thd, List<Item> &list):
+ Item_int_func(thd, list), tmp_paths(0) {}
+ const char *func_name() const { return "json_contains_path"; }
+ bool fix_fields(THD *thd, Item **ref);
+ void fix_length_and_dec();
+ void cleanup();
+ longlong val_int();
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_contains_path>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_array: public Item_str_func
+{
+protected:
+ String tmp_val;
+public:
+ Item_func_json_array(THD *thd):
+ Item_str_func(thd) {}
+ Item_func_json_array(THD *thd, List<Item> &list):
+ Item_str_func(thd, list) {}
+ String *val_str(String *);
+ void fix_length_and_dec();
+ const char *func_name() const { return "json_array"; }
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_array>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_array_append: public Item_json_str_multipath
+{
+protected:
+ String tmp_js;
+ String tmp_val;
+public:
+ Item_func_json_array_append(THD *thd, List<Item> &list):
+ Item_json_str_multipath(thd, list) {}
+ void fix_length_and_dec();
+ String *val_str(String *);
+ uint get_n_paths() const { return arg_count/2; }
+ const char *func_name() const { return "json_array_append"; }
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_array_append>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_object: public Item_func_json_array
+{
+public:
+ Item_func_json_object(THD *thd):
+ Item_func_json_array(thd) {}
+ Item_func_json_object(THD *thd, List<Item> &list):
+ Item_func_json_array(thd, list) {}
+ String *val_str(String *);
+ const char *func_name() const { return "json_object"; }
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_object>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_merge: public Item_func_json_array
+{
+protected:
+ String tmp_val;
+public:
+ Item_func_json_merge(THD *thd, List<Item> &list):
+ Item_func_json_array(thd, list) {}
+ String *val_str(String *);
+ const char *func_name() const { return "json_merge"; }
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_merge>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_length: public Item_int_func
+{
+protected:
+ String tmp_js;
+ String tmp_path;
+public:
+ Item_func_json_length(THD *thd, List<Item> &list):
+ Item_int_func(thd, list) {}
+ const char *func_name() const { return "json_length"; }
+ longlong val_int();
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_length>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_depth: public Item_int_func
+{
+protected:
+ String tmp_js;
+public:
+ Item_func_json_depth(THD *thd, Item *js): Item_int_func(thd, js) {}
+ const char *func_name() const { return "json_depth"; }
+ longlong val_int();
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_depth>(thd, mem_root, this); }
+};
+
+
+class Item_func_json_type: public Item_str_func
+{
+protected:
+ String tmp_js;
+public:
+ Item_func_json_type(THD *thd, Item *js): Item_str_func(thd, js) {}
+ const char *func_name() const { return "json_type"; }
+ void fix_length_and_dec();
+ String *val_str(String *);
+ Item *get_copy(THD *thd, MEM_ROOT *mem_root)
+ { return get_item_copy<Item_func_json_type>(thd, mem_root, this); }
+};
+
+
+#endif /* ITEM_JSONFUNC_INCLUDED */