~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/set_var.cc

  • Committer: Lee Bieber
  • Date: 2010-10-26 14:51:47 UTC
  • mfrom: (1879.1.3 build)
  • Revision ID: kalebral@gmail.com-20101026145147-yqop1w9qw7watara
Merge Monty - clean up of set_var and sys_var
Merge Monty - Generate ChangeLog from bzr as part of distcheck. Also, cleaned up AUTHORS file from bzr log parsed by hand.
Merge Shrews - update transaction log documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
#include "drizzled/session.h"
 
22
#include "drizzled/item/string.h"
 
23
#include "drizzled/sql_list.h"
 
24
 
 
25
using namespace std;
 
26
 
 
27
namespace drizzled
 
28
{
 
29
 
 
30
/**
 
31
  Execute update of all variables.
 
32
 
 
33
  First run a check of all variables that all updates will go ok.
 
34
  If yes, then execute all updates, returning an error if any one failed.
 
35
 
 
36
  This should ensure that in all normal cases none all or variables are
 
37
  updated.
 
38
 
 
39
  @param Session                Thread id
 
40
  @param var_list       List of variables to update
 
41
 
 
42
  @retval
 
43
    0   ok
 
44
  @retval
 
45
    1   ERROR, message sent (normally no variables was updated)
 
46
  @retval
 
47
    -1  ERROR, message not sent
 
48
*/
 
49
 
 
50
int sql_set_variables(Session *session, List<set_var_base> *var_list)
 
51
{
 
52
  int error;
 
53
  List_iterator_fast<set_var_base> it(*var_list);
 
54
 
 
55
  set_var_base *var;
 
56
  while ((var=it++))
 
57
  {
 
58
    if ((error= var->check(session)))
 
59
      goto err;
 
60
  }
 
61
  if (!(error= test(session->is_error())))
 
62
  {
 
63
    it.rewind();
 
64
    while ((var= it++))
 
65
      error|= var->update(session);         // Returns 0, -1 or 1
 
66
  }
 
67
 
 
68
err:
 
69
  free_underlaid_joins(session, &session->lex->select_lex);
 
70
  return(error);
 
71
}
 
72
 
 
73
 
 
74
/*****************************************************************************
 
75
  Functions to handle SET mysql_internal_variable=const_expr
 
76
*****************************************************************************/
 
77
set_var::set_var(sql_var_t type_arg, sys_var *var_arg,
 
78
                 const LEX_STRING *base_name_arg, Item *value_arg) :
 
79
  var(var_arg), type(type_arg), base(*base_name_arg)
 
80
{
 
81
  /*
 
82
    If the set value is a field, change it to a string to allow things like
 
83
    SET table_type=MYISAM;
 
84
  */
 
85
  if (value_arg && value_arg->type() == Item::FIELD_ITEM)
 
86
  {
 
87
    Item_field *item= (Item_field*) value_arg;
 
88
    if (!(value=new Item_string(item->field_name,
 
89
                                (uint32_t) strlen(item->field_name),
 
90
                                item->collation.collation)))
 
91
      value=value_arg;                  /* Give error message later */
 
92
  }
 
93
  else
 
94
    value=value_arg;
 
95
}
 
96
 
 
97
int set_var::check(Session *session)
 
98
{
 
99
  if (var->is_readonly())
 
100
  {
 
101
    my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->getName().c_str(), "read only");
 
102
    return -1;
 
103
  }
 
104
  if (var->check_type(type))
 
105
  {
 
106
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
 
107
    my_error(err, MYF(0), var->getName().c_str());
 
108
    return -1;
 
109
  }
 
110
  /* value is a NULL pointer if we are using SET ... = DEFAULT */
 
111
  if (!value)
 
112
  {
 
113
    if (var->check_default(type))
 
114
    {
 
115
      my_error(ER_NO_DEFAULT, MYF(0), var->getName().c_str());
 
116
      return -1;
 
117
    }
 
118
    return 0;
 
119
  }
 
120
 
 
121
  if ((!value->fixed &&
 
122
       value->fix_fields(session, &value)) || value->check_cols(1))
 
123
    return -1;
 
124
  if (var->check_update_type(value->result_type()))
 
125
  {
 
126
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->getName().c_str());
 
127
    return -1;
 
128
  }
 
129
  return var->check(session, this) ? -1 : 0;
 
130
}
 
131
 
 
132
/**
 
133
  Update variable
 
134
 
 
135
  @param   session    thread handler
 
136
  @returns 0|1    ok or ERROR
 
137
 
 
138
  @note ERROR can be only due to abnormal operations involving
 
139
  the server's execution evironment such as
 
140
  out of memory, hard disk failure or the computer blows up.
 
141
  Consider set_var::check() method if there is a need to return
 
142
  an error due to logics.
 
143
*/
 
144
int set_var::update(Session *session)
 
145
{
 
146
  if (! value)
 
147
    var->set_default(session, type);
 
148
  else if (var->update(session, this))
 
149
    return -1;                          // should never happen
 
150
  if (var->getAfterUpdateTrigger())
 
151
    (*var->getAfterUpdateTrigger())(session, type);
 
152
  return 0;
 
153
}
 
154
 
 
155
/*****************************************************************************
 
156
  Functions to handle SET @user_variable=const_expr
 
157
*****************************************************************************/
 
158
 
 
159
int set_var_user::check(Session *session)
 
160
{
 
161
  /*
 
162
    Item_func_set_user_var can't substitute something else on its place =>
 
163
    0 can be passed as last argument (reference on item)
 
164
  */
 
165
  return (user_var_item->fix_fields(session, (Item**) 0) ||
 
166
          user_var_item->check(0)) ? -1 : 0;
 
167
}
 
168
 
 
169
 
 
170
int set_var_user::update(Session *)
 
171
{
 
172
  if (user_var_item->update())
 
173
  {
 
174
    /* Give an error if it's not given already */
 
175
    my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY), MYF(0));
 
176
    return -1;
 
177
  }
 
178
  return 0;
 
179
}
 
180
 
 
181
} /* namespace drizzled */