~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_udf.h

  • Committer: Stewart Smith
  • Date: 2008-07-13 06:56:15 UTC
  • mto: (210.1.1 drizzle)
  • mto: This revision was merged to the branch mainline in revision 211.
  • Revision ID: stewart@flamingspork.com-20080713065615-vzok75kgnnviokl9
Move MD5() into a UDF

Show diffs side-by-side

added added

removed removed

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