1 #ifndef SQL_UDF_INCLUDED 2 #define SQL_UDF_INCLUDED 3 4 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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, version 2.0, 8 as published by the Free Software Foundation. 9 10 This program is also distributed with certain software (including 11 but not limited to OpenSSL) that is licensed under separate terms, 12 as designated in a particular file or component or in included license 13 documentation. The authors of MySQL hereby grant you an additional 14 permission to link the program and your derivative works with the 15 separately licensed software that they have included with MySQL. 16 17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License, version 2.0, for more details. 21 22 You should have received a copy of the GNU General Public License 23 along with this program; if not, write to the Free Software Foundation, 24 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ 25 26 27 /* This file defines structures needed by udf functions */ 28 29 enum Item_udftype {UDFTYPE_FUNCTION=1,UDFTYPE_AGGREGATE}; 30 31 typedef void (*Udf_func_clear)(UDF_INIT *, uchar *, uchar *); 32 typedef void (*Udf_func_add)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *); 33 typedef void (*Udf_func_deinit)(UDF_INIT*); 34 typedef my_bool (*Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *); 35 typedef void (*Udf_func_any)(); 36 typedef double (*Udf_func_double)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *); 37 typedef longlong (*Udf_func_longlong)(UDF_INIT *, UDF_ARGS *, uchar *, 38 uchar *); 39 40 typedef struct st_udf_func 41 { 42 LEX_STRING name; 43 Item_result returns; 44 Item_udftype type; 45 char *dl; 46 void *dlhandle; 47 Udf_func_any func; 48 Udf_func_init func_init; 49 Udf_func_deinit func_deinit; 50 Udf_func_clear func_clear; 51 Udf_func_add func_add; 52 ulong usage_count; 53 } udf_func; 54 55 class Item_result_field; 56 57 class udf_handler :public Sql_alloc 58 { 59 protected: 60 udf_func *u_d; 61 String *buffers; 62 UDF_ARGS f_args; 63 UDF_INIT initid; 64 char *num_buffer; 65 uchar error, is_null; 66 bool initialized; 67 Item **args; 68 69 public: 70 table_map used_tables_cache; 71 bool const_item_cache; 72 bool not_original; udf_handler(udf_func * udf_arg)73 udf_handler(udf_func *udf_arg) :u_d(udf_arg), buffers(0), error(0), 74 is_null(0), initialized(0), not_original(0) 75 {} 76 ~udf_handler(); name()77 const char *name() const { return u_d ? u_d->name.str : "?"; } result_type()78 Item_result result_type () const 79 { return u_d ? u_d->returns : STRING_RESULT;} 80 bool get_arguments(); 81 bool fix_fields(THD *thd, Item_result_field *item, 82 uint arg_count, Item **args); 83 void cleanup(); val(my_bool * null_value)84 double val(my_bool *null_value) 85 { 86 is_null= 0; 87 if (get_arguments()) 88 { 89 *null_value=1; 90 return 0.0; 91 } 92 Udf_func_double func= (Udf_func_double) u_d->func; 93 double tmp=func(&initid, &f_args, &is_null, &error); 94 if (is_null || error) 95 { 96 *null_value=1; 97 return 0.0; 98 } 99 *null_value=0; 100 return tmp; 101 } val_int(my_bool * null_value)102 longlong val_int(my_bool *null_value) 103 { 104 is_null= 0; 105 if (get_arguments()) 106 { 107 *null_value=1; 108 return 0LL; 109 } 110 Udf_func_longlong func= (Udf_func_longlong) u_d->func; 111 longlong tmp=func(&initid, &f_args, &is_null, &error); 112 if (is_null || error) 113 { 114 *null_value=1; 115 return 0LL; 116 } 117 *null_value=0; 118 return tmp; 119 } 120 my_decimal *val_decimal(my_bool *null_value, my_decimal *dec_buf); clear()121 void clear() 122 { 123 is_null= 0; 124 Udf_func_clear func= u_d->func_clear; 125 func(&initid, &is_null, &error); 126 } add(my_bool * null_value)127 void add(my_bool *null_value) 128 { 129 if (get_arguments()) 130 { 131 *null_value=1; 132 return; 133 } 134 Udf_func_add func= u_d->func_add; 135 func(&initid, &f_args, &is_null, &error); 136 *null_value= (my_bool) (is_null || error); 137 } 138 String *val_str(String *str,String *save_str); 139 }; 140 141 142 #ifdef HAVE_DLOPEN 143 void udf_init(void),udf_free(void); 144 udf_func *find_udf(const char *name, size_t len=0,bool mark_used=0); 145 void free_udf(udf_func *udf); 146 int mysql_create_function(THD *thd,udf_func *udf); 147 int mysql_drop_function(THD *thd,const LEX_STRING *name); 148 #endif 149 #endif /* SQL_UDF_INCLUDED */ 150