drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
set_var.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
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
20#include <config.h>
21
22#include <boost/lexical_cast.hpp>
23#include <boost/exception/get_error_info.hpp>
24#include <string>
25
26#include <drizzled/session.h>
27#include <drizzled/item/string.h>
28#include <drizzled/function/set_user_var.h>
29#include <drizzled/sql_lex.h>
30#include <drizzled/util/test.h>
31
32using namespace std;
33
34namespace drizzled
35{
36
56
57int sql_set_variables(Session *session, const SetVarVector &var_list)
58{
59 int error;
60 for (SetVarVector::const_reference it : var_list)
61 {
62 if ((error= it->check(session)))
63 goto err;
64 }
65 if (!(error= test(session->is_error())))
66 {
67 for (SetVarVector::const_reference it : var_list)
68 {
69 error|= it->update(session); // Returns 0, -1 or 1
70 }
71 }
72
73err:
74 free_underlaid_joins(session, &session->lex().select_lex);
75 return error;
76}
77
78
79/*****************************************************************************
80 Functions to handle SET mysql_internal_variable=const_expr
81*****************************************************************************/
82set_var::set_var(sql_var_t type_arg, sys_var *var_arg, str_ref base_name_arg, Item *value_arg) :
83 uint64_t_value(0),
84 var(var_arg),
85 type(type_arg),
86 base(base_name_arg)
87{
88 /*
89 If the set value is a field, change it to a string to allow things like
90 SET table_type=MYISAM;
91 */
92 if (value_arg && value_arg->type() == Item::FIELD_ITEM)
93 {
94 Item_field *item= (Item_field*) value_arg;
95 value= new Item_string(str_ref(item->field_name), item->collation.collation);
96 }
97 else
98 {
99 value= value_arg;
100 }
101}
102
103int set_var::check(Session *session)
104{
105 if (var->is_readonly())
106 {
107 my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->getName().c_str(), "read only");
108 return -1;
109 }
110 if (var->check_type(type))
111 {
112 int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
113 my_error(static_cast<drizzled::error_t>(err), MYF(0), var->getName().c_str());
114 return -1;
115 }
116 /* value is a NULL pointer if we are using SET ... = DEFAULT */
117 if (not value)
118 {
119 if (var->check_default(type))
120 {
121 my_error(ER_NO_DEFAULT, MYF(0), var->getName().c_str());
122 return -1;
123 }
124 return 0;
125 }
126
127 if ((!value->fixed &&
128 value->fix_fields(session, &value)) || value->check_cols(1))
129 return -1;
130 if (var->check_update_type(value->result_type()))
131 {
132 my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->getName().c_str());
133 return -1;
134 }
135 return var->check(session, this) ? -1 : 0;
136}
137
151{
152 try
153 {
154 if (not value)
155 var->set_default(session, type);
156 else if (var->update(session, this))
157 return -1; // should never happen
158 if (var->getAfterUpdateTrigger())
159 (*var->getAfterUpdateTrigger())(session, type);
160 }
161 catch (invalid_option_value &ex)
162 {
163 /* TODO: Fix this to be typesafe once we have properly typed set_var */
164 string new_val= boost::lexical_cast<string>(uint64_t_value);
165 if (boost::get_error_info<invalid_max_info>(ex) != NULL)
166 {
167 const uint64_t max_val= *(boost::get_error_info<invalid_max_info>(ex));
168 string explanation("(> ");
169 explanation.append(boost::lexical_cast<std::string>(max_val));
170 explanation.push_back(')');
171 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
172 ER_INVALID_OPTION_VALUE,
173 ER(ER_INVALID_OPTION_VALUE),
174 var->getName().c_str(),
175 new_val.c_str(),
176 explanation.c_str());
177 }
178 else if (boost::get_error_info<invalid_min_info>(ex) != NULL)
179 {
180 const int64_t min_val= *(boost::get_error_info<invalid_min_info>(ex));
181 string explanation("(< ");
182 explanation.append(boost::lexical_cast<std::string>(min_val));
183 explanation.push_back(')');
184 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
185 ER_INVALID_OPTION_VALUE,
186 ER(ER_INVALID_OPTION_VALUE),
187 var->getName().c_str(),
188 new_val.c_str(),
189 explanation.c_str());
190 }
191 else if (boost::get_error_info<invalid_value>(ex) != NULL)
192 {
193 const std::string str_val= *(boost::get_error_info<invalid_value>(ex));
194 string explanation("(");
195 explanation.append(str_val);
196 explanation.push_back(')');
197 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
198 ER_INVALID_OPTION_VALUE,
199 ER(ER_INVALID_OPTION_VALUE),
200 var->getName().c_str(),
201 new_val.c_str(),
202 explanation.c_str());
203 }
204 else
205 {
206 push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
207 ER_INVALID_OPTION_VALUE,
208 ER(ER_INVALID_OPTION_VALUE),
209 var->getName().c_str(),
210 new_val.c_str(),
211 "");
212 }
213 }
214 return 0;
215}
216
217/*****************************************************************************
218 Functions to handle SET @user_variable=const_expr
219*****************************************************************************/
220
221int set_var_user::check(Session *session)
222{
223 /*
224 Item_func_set_user_var can't substitute something else on its place =>
225 0 can be passed as last argument (reference on item)
226 */
227 return (user_var_item->fix_fields(session, (Item**) 0) ||
228 user_var_item->check(0)) ? -1 : 0;
229}
230
231
232int set_var_user::update(Session *)
233{
234 user_var_item->update();
235 return 0;
236}
237
238void set_var::setValue(const std::string &new_value)
239{
240 str_value= new_value;
241}
242
243void set_var::setValue(uint64_t new_value)
244{
245 uint64_t_value= new_value;
246}
247
248void set_var::updateValue()
249{
250 if (var->show_type() != SHOW_CHAR)
251 {
252 uint64_t_value= value->val_int();
253 }
254}
255
256
257} /* namespace drizzled */
int update(Session *session)
Definition set_var.cc:150
void free_underlaid_joins(Session *session, Select_Lex *select)
TODO: Rename this file - func.h is stupid.
int sql_set_variables(Session *session, const SetVarVector &var_list)
Definition set_var.cc:57
STL namespace.