~drizzle-trunk/drizzle/development

1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
21
2207.8.2 by Olaf van der Spek
x
22
#include <boost/foreach.hpp>
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
23
#include <boost/lexical_cast.hpp>
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
24
#include <boost/exception/get_error_info.hpp>
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
25
#include <string>
26
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
27
#include <drizzled/session.h>
28
#include <drizzled/item/string.h>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
29
#include <drizzled/function/set_user_var.h>
2234.1.4 by Olaf van der Spek
Refactor includes
30
#include <drizzled/sql_lex.h>
2239.1.9 by Olaf van der Spek
Refactor includes
31
#include <drizzled/util/test.h>
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
32
33
using namespace std;
34
35
namespace drizzled
36
{
37
38
/**
39
  Execute update of all variables.
40
41
  First run a check of all variables that all updates will go ok.
42
  If yes, then execute all updates, returning an error if any one failed.
43
44
  This should ensure that in all normal cases none all or variables are
45
  updated.
46
47
  @param Session		Thread id
48
  @param var_list       List of variables to update
49
50
  @retval
51
    0	ok
52
  @retval
53
    1	ERROR, message sent (normally no variables was updated)
54
  @retval
55
    -1  ERROR, message not sent
56
*/
57
2040.6.4 by Monty Taylor
Replaced List<set_var_base> with
58
int sql_set_variables(Session *session, const SetVarVector &var_list)
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
59
{
60
  int error;
2207.8.2 by Olaf van der Spek
x
61
  BOOST_FOREACH(SetVarVector::const_reference it, var_list)
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
62
  {
2207.8.2 by Olaf van der Spek
x
63
    if ((error= it->check(session)))
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
64
      goto err;
65
  }
66
  if (!(error= test(session->is_error())))
67
  {
2207.8.2 by Olaf van der Spek
x
68
    BOOST_FOREACH(SetVarVector::const_reference it, var_list)
2040.6.4 by Monty Taylor
Replaced List<set_var_base> with
69
    {
2207.8.2 by Olaf van der Spek
x
70
      error|= it->update(session);         // Returns 0, -1 or 1
2040.6.4 by Monty Taylor
Replaced List<set_var_base> with
71
    }
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
72
  }
2363.1.4 by Brian Aker
Fixes bug where true/false would not be interpreted correctly/displayed correctly.
73
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
74
err:
2227.4.8 by Olaf van der Spek
Session::lex()
75
  free_underlaid_joins(session, &session->lex().select_lex);
2207.8.2 by Olaf van der Spek
x
76
  return error;
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
77
}
78
79
80
/*****************************************************************************
81
  Functions to handle SET mysql_internal_variable=const_expr
82
*****************************************************************************/
1878.3.2 by Monty Taylor
Split out show_type into its own header and made sys_var work through
83
set_var::set_var(sql_var_t type_arg, sys_var *var_arg,
2371.1.2 by Brian Aker
Remove the typedef on lexkey
84
                 const lex_string_t *base_name_arg, Item *value_arg) :
2070.2.2 by Monty Taylor
Removed some guts from set_var.
85
  uint64_t_value(0),
86
  str_value(""),
87
  var(var_arg),
88
  type(type_arg),
89
  base(*base_name_arg)
1878.3.2 by Monty Taylor
Split out show_type into its own header and made sys_var work through
90
{
91
  /*
92
    If the set value is a field, change it to a string to allow things like
93
    SET table_type=MYISAM;
94
  */
95
  if (value_arg && value_arg->type() == Item::FIELD_ITEM)
96
  {
97
    Item_field *item= (Item_field*) value_arg;
2318.6.78 by Olaf van der Spek
Refactor
98
    value=new Item_string(item->field_name, (uint32_t) strlen(item->field_name), item->collation.collation);
1878.3.2 by Monty Taylor
Split out show_type into its own header and made sys_var work through
99
  }
100
  else
2070.2.2 by Monty Taylor
Removed some guts from set_var.
101
  {
102
    value= value_arg;
103
  }
1878.3.2 by Monty Taylor
Split out show_type into its own header and made sys_var work through
104
}
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
105
106
int set_var::check(Session *session)
107
{
108
  if (var->is_readonly())
109
  {
110
    my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->getName().c_str(), "read only");
111
    return -1;
112
  }
113
  if (var->check_type(type))
114
  {
115
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
2054.1.2 by Brian Aker
Rename of the Loooongggggg error type over to simply drizzled::error_t
116
    my_error(static_cast<drizzled::error_t>(err), MYF(0), var->getName().c_str());
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
117
    return -1;
118
  }
119
  /* value is a NULL pointer if we are using SET ... = DEFAULT */
2318.6.78 by Olaf van der Spek
Refactor
120
  if (not value)
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
121
  {
122
    if (var->check_default(type))
123
    {
124
      my_error(ER_NO_DEFAULT, MYF(0), var->getName().c_str());
125
      return -1;
126
    }
127
    return 0;
128
  }
129
130
  if ((!value->fixed &&
131
       value->fix_fields(session, &value)) || value->check_cols(1))
132
    return -1;
133
  if (var->check_update_type(value->result_type()))
134
  {
135
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->getName().c_str());
136
    return -1;
137
  }
138
  return var->check(session, this) ? -1 : 0;
139
}
140
141
/**
142
  Update variable
143
144
  @param   session    thread handler
145
  @returns 0|1    ok or	ERROR
146
147
  @note ERROR can be only due to abnormal operations involving
148
  the server's execution evironment such as
149
  out of memory, hard disk failure or the computer blows up.
150
  Consider set_var::check() method if there is a need to return
151
  an error due to logics.
152
*/
153
int set_var::update(Session *session)
154
{
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
155
  try
156
  {
2318.6.78 by Olaf van der Spek
Refactor
157
    if (not value)
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
158
      var->set_default(session, type);
159
    else if (var->update(session, this))
160
      return -1;				// should never happen
161
    if (var->getAfterUpdateTrigger())
162
      (*var->getAfterUpdateTrigger())(session, type);
163
  }
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
164
  catch (invalid_option_value &ex)
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
165
  {
1897.4.18 by Monty Taylor
Cleaned up another debugging leftover.
166
    /* TODO: Fix this to be typesafe once we have properly typed set_var */
2070.2.2 by Monty Taylor
Removed some guts from set_var.
167
    string new_val= boost::lexical_cast<string>(uint64_t_value);
1964.1.5 by Monty Taylor
Change how we deal with error_info, because on karmic it's returning a shared_ptr. Sigh.
168
    if (boost::get_error_info<invalid_max_info>(ex) != NULL)
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
169
    { 
1964.1.5 by Monty Taylor
Change how we deal with error_info, because on karmic it's returning a shared_ptr. Sigh.
170
      const uint64_t max_val= *(boost::get_error_info<invalid_max_info>(ex));
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
171
      string explanation("(> ");
1964.1.5 by Monty Taylor
Change how we deal with error_info, because on karmic it's returning a shared_ptr. Sigh.
172
      explanation.append(boost::lexical_cast<std::string>(max_val));
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
173
      explanation.push_back(')');
174
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
175
                          ER_INVALID_OPTION_VALUE,
176
                          ER(ER_INVALID_OPTION_VALUE),
177
                          var->getName().c_str(),
178
                          new_val.c_str(),
179
                          explanation.c_str());
180
    }
1964.1.5 by Monty Taylor
Change how we deal with error_info, because on karmic it's returning a shared_ptr. Sigh.
181
    else if (boost::get_error_info<invalid_min_info>(ex) != NULL)
182
    { 
183
      const int64_t min_val= *(boost::get_error_info<invalid_min_info>(ex));
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
184
      string explanation("(< ");
1964.1.5 by Monty Taylor
Change how we deal with error_info, because on karmic it's returning a shared_ptr. Sigh.
185
      explanation.append(boost::lexical_cast<std::string>(min_val));
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
186
      explanation.push_back(')');
187
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
188
                          ER_INVALID_OPTION_VALUE,
189
                          ER(ER_INVALID_OPTION_VALUE),
190
                          var->getName().c_str(),
191
                          new_val.c_str(),
192
                          explanation.c_str());
193
    }
2070.2.1 by Monty Taylor
First step in getting that anonymous union out of set_var.
194
    else if (boost::get_error_info<invalid_value>(ex) != NULL)
195
    {
196
      const std::string str_val= *(boost::get_error_info<invalid_value>(ex));
197
      string explanation("(");
198
      explanation.append(str_val);
199
      explanation.push_back(')');
200
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
201
                          ER_INVALID_OPTION_VALUE,
202
                          ER(ER_INVALID_OPTION_VALUE),
203
                          var->getName().c_str(),
204
                          new_val.c_str(),
205
                          explanation.c_str());
206
    }
1964.1.4 by Monty Taylor
Use boost::exception data bundling instead of co-opting the exceptions
207
    else
208
    {
209
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
210
                          ER_INVALID_OPTION_VALUE,
211
                          ER(ER_INVALID_OPTION_VALUE),
212
                          var->getName().c_str(),
213
                          new_val.c_str(),
214
                          "");
215
    }
1897.4.14 by Monty Taylor
Update to support default values and properly throw warnings on value
216
  }
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
217
  return 0;
218
}
219
220
/*****************************************************************************
221
  Functions to handle SET @user_variable=const_expr
222
*****************************************************************************/
223
224
int set_var_user::check(Session *session)
225
{
226
  /*
227
    Item_func_set_user_var can't substitute something else on its place =>
228
    0 can be passed as last argument (reference on item)
229
  */
230
  return (user_var_item->fix_fields(session, (Item**) 0) ||
231
	  user_var_item->check(0)) ? -1 : 0;
232
}
233
234
235
int set_var_user::update(Session *)
236
{
2246.3.5 by Olaf van der Spek
Propogate return void
237
  user_var_item->update();
238
	return 0;
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
239
}
240
2070.2.1 by Monty Taylor
First step in getting that anonymous union out of set_var.
241
void set_var::setValue(const std::string &new_value)
242
{
243
  str_value= new_value;
244
}
245
246
void set_var::setValue(uint64_t new_value)
247
{
2070.2.2 by Monty Taylor
Removed some guts from set_var.
248
  uint64_t_value= new_value;
2070.2.1 by Monty Taylor
First step in getting that anonymous union out of set_var.
249
}
250
251
void set_var::updateValue()
252
{
2070.2.2 by Monty Taylor
Removed some guts from set_var.
253
  if (var->show_type() != SHOW_CHAR)
254
  {
255
    uint64_t_value= value->val_int();
256
  }
2070.2.1 by Monty Taylor
First step in getting that anonymous union out of set_var.
257
}
258
259
1878.3.1 by Monty Taylor
Split set_var.* into sys_var.* and set_var.*
260
} /* namespace drizzled */