You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.8 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000-2001, 2003-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* This file defines structures needed by udf functions */
  13. #ifdef USE_PRAGMA_INTERFACE
  14. #pragma interface
  15. #endif
  16. enum Item_udftype {UDFTYPE_FUNCTION=1,UDFTYPE_AGGREGATE};
  17. typedef void (*Udf_func_clear)(UDF_INIT *, uchar *, uchar *);
  18. typedef void (*Udf_func_add)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
  19. typedef void (*Udf_func_deinit)(UDF_INIT*);
  20. typedef my_bool (*Udf_func_init)(UDF_INIT *, UDF_ARGS *, char *);
  21. typedef void (*Udf_func_any)();
  22. typedef double (*Udf_func_double)(UDF_INIT *, UDF_ARGS *, uchar *, uchar *);
  23. typedef longlong (*Udf_func_longlong)(UDF_INIT *, UDF_ARGS *, uchar *,
  24. uchar *);
  25. typedef struct st_udf_func
  26. {
  27. LEX_STRING name;
  28. Item_result returns;
  29. Item_udftype type;
  30. char *dl;
  31. void *dlhandle;
  32. Udf_func_any func;
  33. Udf_func_init func_init;
  34. Udf_func_deinit func_deinit;
  35. Udf_func_clear func_clear;
  36. Udf_func_add func_add;
  37. ulong usage_count;
  38. } udf_func;
  39. class Item_result_field;
  40. struct st_table_list;
  41. class udf_handler :public Sql_alloc
  42. {
  43. protected:
  44. udf_func *u_d;
  45. String *buffers;
  46. UDF_ARGS f_args;
  47. UDF_INIT initid;
  48. char *num_buffer;
  49. uchar error, is_null;
  50. bool initialized;
  51. Item **args;
  52. public:
  53. table_map used_tables_cache;
  54. bool const_item_cache;
  55. bool not_original;
  56. udf_handler(udf_func *udf_arg) :u_d(udf_arg), buffers(0), error(0),
  57. is_null(0), initialized(0), not_original(0)
  58. {}
  59. ~udf_handler();
  60. const char *name() const { return u_d ? u_d->name.str : "?"; }
  61. Item_result result_type () const
  62. { return u_d ? u_d->returns : STRING_RESULT;}
  63. bool get_arguments();
  64. bool fix_fields(THD *thd, Item_result_field *item,
  65. uint arg_count, Item **args);
  66. void cleanup();
  67. double val(my_bool *null_value)
  68. {
  69. is_null= 0;
  70. if (get_arguments())
  71. {
  72. *null_value=1;
  73. return 0.0;
  74. }
  75. Udf_func_double func= (Udf_func_double) u_d->func;
  76. double tmp=func(&initid, &f_args, &is_null, &error);
  77. if (is_null || error)
  78. {
  79. *null_value=1;
  80. return 0.0;
  81. }
  82. *null_value=0;
  83. return tmp;
  84. }
  85. longlong val_int(my_bool *null_value)
  86. {
  87. is_null= 0;
  88. if (get_arguments())
  89. {
  90. *null_value=1;
  91. return LL(0);
  92. }
  93. Udf_func_longlong func= (Udf_func_longlong) u_d->func;
  94. longlong tmp=func(&initid, &f_args, &is_null, &error);
  95. if (is_null || error)
  96. {
  97. *null_value=1;
  98. return LL(0);
  99. }
  100. *null_value=0;
  101. return tmp;
  102. }
  103. my_decimal *val_decimal(my_bool *null_value, my_decimal *dec_buf);
  104. void clear()
  105. {
  106. is_null= 0;
  107. Udf_func_clear func= u_d->func_clear;
  108. func(&initid, &is_null, &error);
  109. }
  110. void add(my_bool *null_value)
  111. {
  112. if (get_arguments())
  113. {
  114. *null_value=1;
  115. return;
  116. }
  117. Udf_func_add func= u_d->func_add;
  118. func(&initid, &f_args, &is_null, &error);
  119. *null_value= (my_bool) (is_null || error);
  120. }
  121. String *val_str(String *str,String *save_str);
  122. };
  123. #ifdef HAVE_DLOPEN
  124. void udf_init(void),udf_free(void);
  125. udf_func *find_udf(const char *name, uint len=0,bool mark_used=0);
  126. void free_udf(udf_func *udf);
  127. int mysql_create_function(THD *thd,udf_func *udf);
  128. int mysql_drop_function(THD *thd,const LEX_STRING *name);
  129. #endif