The world's most popular open source database
00001 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 00018 /* This file defines structures needed by udf functions */ 00019 00020 #ifdef USE_PRAGMA_INTERFACE 00021 #pragma interface 00022 #endif 00023 00024 enum Item_udftype {UDFTYPE_FUNCTION=1,UDFTYPE_AGGREGATE}; 00025 00026 typedef void (*Udf_func_clear)(UDF_INIT *, uchar *, uchar *); 00027 typedef void (*Udf_func_add)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *); 00028 typedef void (*Udf_func_deinit)(UDF_INIT*); 00029 typedef my_bool (*Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *); 00030 typedef void (*Udf_func_any)(); 00031 typedef double (*Udf_func_double)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *); 00032 typedef longlong (*Udf_func_longlong)(UDF_INIT *, UDF_ARGS *, uchar *, 00033 uchar *); 00034 00035 typedef struct st_udf_func 00036 { 00037 LEX_STRING name; 00038 Item_result returns; 00039 Item_udftype type; 00040 char *dl; 00041 void *dlhandle; 00042 Udf_func_any func; 00043 Udf_func_init func_init; 00044 Udf_func_deinit func_deinit; 00045 Udf_func_clear func_clear; 00046 Udf_func_add func_add; 00047 ulong usage_count; 00048 } udf_func; 00049 00050 class Item_result_field; 00051 struct st_table_list; 00052 00053 class udf_handler :public Sql_alloc 00054 { 00055 protected: 00056 udf_func *u_d; 00057 String *buffers; 00058 UDF_ARGS f_args; 00059 UDF_INIT initid; 00060 char *num_buffer; 00061 uchar error, is_null; 00062 bool initialized; 00063 Item **args; 00064 00065 public: 00066 table_map used_tables_cache; 00067 bool const_item_cache; 00068 bool not_original; 00069 udf_handler(udf_func *udf_arg) :u_d(udf_arg), buffers(0), error(0), 00070 is_null(0), initialized(0), not_original(0) 00071 {} 00072 ~udf_handler(); 00073 const char *name() const { return u_d ? u_d->name.str : "?"; } 00074 Item_result result_type () const 00075 { return u_d ? u_d->returns : STRING_RESULT;} 00076 bool get_arguments(); 00077 bool fix_fields(THD *thd, Item_result_field *item, 00078 uint arg_count, Item **args); 00079 void cleanup(); 00080 double val(my_bool *null_value) 00081 { 00082 is_null= 0; 00083 if (get_arguments()) 00084 { 00085 *null_value=1; 00086 return 0.0; 00087 } 00088 Udf_func_double func= (Udf_func_double) u_d->func; 00089 double tmp=func(&initid, &f_args, &is_null, &error); 00090 if (is_null || error) 00091 { 00092 *null_value=1; 00093 return 0.0; 00094 } 00095 *null_value=0; 00096 return tmp; 00097 } 00098 longlong val_int(my_bool *null_value) 00099 { 00100 is_null= 0; 00101 if (get_arguments()) 00102 { 00103 *null_value=1; 00104 return LL(0); 00105 } 00106 Udf_func_longlong func= (Udf_func_longlong) u_d->func; 00107 longlong tmp=func(&initid, &f_args, &is_null, &error); 00108 if (is_null || error) 00109 { 00110 *null_value=1; 00111 return LL(0); 00112 } 00113 *null_value=0; 00114 return tmp; 00115 } 00116 my_decimal *val_decimal(my_bool *null_value, my_decimal *dec_buf); 00117 void clear() 00118 { 00119 is_null= 0; 00120 Udf_func_clear func= u_d->func_clear; 00121 func(&initid, &is_null, &error); 00122 } 00123 void add(my_bool *null_value) 00124 { 00125 if (get_arguments()) 00126 { 00127 *null_value=1; 00128 return; 00129 } 00130 Udf_func_add func= u_d->func_add; 00131 func(&initid, &f_args, &is_null, &error); 00132 *null_value= (my_bool) (is_null || error); 00133 } 00134 String *val_str(String *str,String *save_str); 00135 }; 00136 00137 00138 #ifdef HAVE_DLOPEN 00139 void udf_init(void),udf_free(void); 00140 udf_func *find_udf(const char *name, uint len=0,bool mark_used=0); 00141 void free_udf(udf_func *udf); 00142 int mysql_create_function(THD *thd,udf_func *udf); 00143 int mysql_drop_function(THD *thd,const LEX_STRING *name); 00144 #endif
1.4.7

