1#ifndef PROCEDURE_INCLUDED
2#define PROCEDURE_INCLUDED
3
4/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
8 the Free Software Foundation; version 2 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
19/* When using sql procedures */
20
21#ifdef USE_PRAGMA_INTERFACE
22#pragma interface /* gcc class implementation */
23#endif
24
25/*
26 It is necessary to include set_var.h instead of item.h because there
27 are dependencies on include order for set_var.h and item.h. This
28 will be resolved later.
29*/
30#include "sql_class.h" /* select_result, set_var.h: THD */
31#include "set_var.h" /* Item */
32
33#define PROC_NO_SORT 1 /**< Bits in flags */
34#define PROC_GROUP 2 /**< proc must have group */
35
36/* Procedure items used by procedures to store values for send_result_set_metadata */
37
38class Item_proc :public Item
39{
40public:
41 Item_proc(THD *thd, const char *name_par): Item(thd)
42 {
43 this->name.str= name_par;
44 this->name.length= strlen(name_par);
45 }
46 enum Type type() const { return Item::PROC_ITEM; }
47 virtual void set(double nr)=0;
48 virtual void set(const char *str,uint length,CHARSET_INFO *cs)=0;
49 virtual void set(longlong nr)=0;
50 const Type_handler *type_handler() const=0;
51 void set(const char *str) { set(str,(uint) strlen(str), default_charset()); }
52 void make_send_field(THD *thd, Send_field *tmp_field)
53 {
54 init_make_send_field(tmp_field,field_type());
55 }
56 unsigned int size_of() { return sizeof(*this);}
57 bool check_vcol_func_processor(void *arg)
58 {
59 DBUG_ASSERT(0); // impossible
60 return mark_unsupported_function("proc", arg, VCOL_IMPOSSIBLE);
61 }
62 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
63 {
64 return type_handler()->Item_get_date(this, ltime, fuzzydate);
65 }
66 Item* get_copy(THD *thd) { return 0; }
67};
68
69class Item_proc_real :public Item_proc
70{
71 double value;
72public:
73 Item_proc_real(THD *thd, const char *name_par, uint dec):
74 Item_proc(thd, name_par)
75 {
76 decimals=dec; max_length=float_length(dec);
77 }
78 const Type_handler *type_handler() const { return &type_handler_double; }
79 void set(double nr) { value=nr; }
80 void set(longlong nr) { value=(double) nr; }
81 void set(const char *str,uint length,CHARSET_INFO *cs)
82 {
83 int err_not_used;
84 char *end_not_used;
85 value= my_strntod(cs,(char*) str,length, &end_not_used, &err_not_used);
86 }
87 double val_real() { return value; }
88 longlong val_int() { return (longlong) value; }
89 String *val_str(String *s)
90 {
91 s->set_real(value,decimals,default_charset());
92 return s;
93 }
94 my_decimal *val_decimal(my_decimal *);
95 unsigned int size_of() { return sizeof(*this);}
96};
97
98class Item_proc_int :public Item_proc
99{
100 longlong value;
101public:
102 Item_proc_int(THD *thd, const char *name_par): Item_proc(thd, name_par)
103 { max_length=11; }
104 const Type_handler *type_handler() const { return &type_handler_longlong; }
105 void set(double nr) { value=(longlong) nr; }
106 void set(longlong nr) { value=nr; }
107 void set(const char *str,uint length, CHARSET_INFO *cs)
108 { int err; value=my_strntoll(cs,str,length,10,NULL,&err); }
109 double val_real() { return (double) value; }
110 longlong val_int() { return value; }
111 String *val_str(String *s) { s->set(value, default_charset()); return s; }
112 my_decimal *val_decimal(my_decimal *);
113 unsigned int size_of() { return sizeof(*this);}
114};
115
116
117class Item_proc_string :public Item_proc
118{
119public:
120 Item_proc_string(THD *thd, const char *name_par, uint length):
121 Item_proc(thd, name_par) { this->max_length=length; }
122 const Type_handler *type_handler() const { return &type_handler_varchar; }
123 void set(double nr) { str_value.set_real(nr, 2, default_charset()); }
124 void set(longlong nr) { str_value.set(nr, default_charset()); }
125 void set(const char *str, uint length, CHARSET_INFO *cs)
126 { str_value.copy(str,length,cs); }
127 double val_real()
128 {
129 int err_not_used;
130 char *end_not_used;
131 CHARSET_INFO *cs= str_value.charset();
132 return my_strntod(cs, (char*) str_value.ptr(), str_value.length(),
133 &end_not_used, &err_not_used);
134 }
135 longlong val_int()
136 {
137 int err;
138 CHARSET_INFO *cs=str_value.charset();
139 return my_strntoll(cs,str_value.ptr(),str_value.length(),10,NULL,&err);
140 }
141 String *val_str(String*)
142 {
143 return null_value ? (String*) 0 : (String*) &str_value;
144 }
145 my_decimal *val_decimal(my_decimal *);
146 unsigned int size_of() { return sizeof(*this);}
147};
148
149/* The procedure class definitions */
150
151class Procedure {
152protected:
153 List<Item> *fields;
154 select_result *result;
155public:
156 const uint flags;
157 ORDER *group,*param_fields;
158 Procedure(select_result *res,uint flags_par) :result(res),flags(flags_par),
159 group(0),param_fields(0) {}
160 virtual ~Procedure() {group=param_fields=0; fields=0; }
161 virtual void add(void)=0;
162 virtual void end_group(void)=0;
163 virtual int send_row(List<Item> &fields)=0;
164 virtual bool change_columns(THD *thd, List<Item> &fields)= 0;
165 virtual void update_refs(void) {}
166 virtual int end_of_records() { return 0; }
167};
168
169Procedure *setup_procedure(THD *thd,ORDER *proc_param,select_result *result,
170 List<Item> &field_list,int *error);
171
172#endif /* PROCEDURE_INCLUDED */
173