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