~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/set_user_var.cc

Merged from Toru - removal of my_time_t.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
 
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
22
22
#include <drizzled/function/set_user_var.h>
 
23
#include <drizzled/function/get_variable.h>
 
24
#include <drizzled/function/update_hash.h>
23
25
#include <drizzled/field/num.h>
 
26
#include <drizzled/virtual_column_info.h>
24
27
#include <drizzled/session.h>
25
 
#include <drizzled/plugin/client.h>
26
 
#include <drizzled/user_var_entry.h>
27
 
 
28
 
namespace drizzled
29
 
{
30
28
 
31
29
/*
32
30
  When a user variable is updated (in a SET command or a query like
38
36
  assert(fixed == 0);
39
37
  /* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
40
38
  if (Item_func::fix_fields(session, ref) ||
41
 
      !(entry= session->getVariable(name, true)))
 
39
      !(entry= get_variable(&session->user_vars, name, 1)))
42
40
    return true;
43
41
  /*
44
42
     Remember the last query which updated it, this way a query can later know
45
43
     if this variable is a constant item in the query (it is if update_query_id
46
44
     is different from query_id).
47
45
  */
48
 
  entry->update_query_id= session->getQueryId();
 
46
  entry->update_query_id= session->query_id;
49
47
  /*
50
48
    As it is wrong and confusing to associate any
51
49
    character set with NULL, @a should be latin2
91
89
  if (result_field)
92
90
  {
93
91
    Table *table= (Table *) arg;
94
 
    if (result_field->getTable() == table || !table)
95
 
      result_field->getTable()->setReadSet(result_field->position());
96
 
  }
97
 
  return 0;
98
 
}
99
 
 
 
92
    if (result_field->table == table || !table)
 
93
      bitmap_set_bit(result_field->table->read_set, result_field->field_index);
 
94
    if (result_field->vcol_info && result_field->vcol_info->expr_item)
 
95
      return result_field->vcol_info->
 
96
               expr_item->walk(&Item::register_field_in_read_map, 1, arg);
 
97
  }
 
98
  return 0;
 
99
}
 
100
 
 
101
/*
 
102
  Mark field in bitmap supplied as *arg
 
103
 
 
104
*/
 
105
 
 
106
bool Item_func_set_user_var::register_field_in_bitmap(unsigned char *arg)
 
107
{
 
108
  MY_BITMAP *bitmap = (MY_BITMAP *) arg;
 
109
  assert(bitmap);
 
110
  if (result_field)
 
111
  {
 
112
    bitmap_set_bit(bitmap, result_field->field_index);
 
113
  }
 
114
  return 0;
 
115
}
100
116
 
101
117
bool
102
118
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
110
126
  */
111
127
  if ((null_value= args[0]->null_value) && null_item)
112
128
    res_type= entry->type;                      // Don't change type of item
113
 
  if (entry->update_hash((null_value= args[0]->null_value),
114
 
                         ptr, length, res_type, cs, dv, unsigned_arg))
 
129
  if (::update_hash(entry, (null_value= args[0]->null_value),
 
130
                    ptr, length, res_type, cs, dv, unsigned_arg))
115
131
  {
116
132
    null_value= 1;
117
133
    return 1;
141
157
 
142
158
  switch (cached_result_type) {
143
159
  case REAL_RESULT:
144
 
    {
145
 
      save_result.vreal= use_result_field ? result_field->val_real() :
146
 
        args[0]->val_real();
147
 
      break;
148
 
    }
 
160
  {
 
161
    save_result.vreal= use_result_field ? result_field->val_real() :
 
162
                        args[0]->val_real();
 
163
    break;
 
164
  }
149
165
  case INT_RESULT:
150
 
    {
151
 
      save_result.vint= use_result_field ? result_field->val_int() :
152
 
        args[0]->val_int();
153
 
      unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
154
 
        args[0]->unsigned_flag;
155
 
      break;
156
 
    }
 
166
  {
 
167
    save_result.vint= use_result_field ? result_field->val_int() :
 
168
                       args[0]->val_int();
 
169
    unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
 
170
                    args[0]->unsigned_flag;
 
171
    break;
 
172
  }
157
173
  case STRING_RESULT:
158
 
    {
159
 
      save_result.vstr= use_result_field ? result_field->val_str_internal(&value) :
160
 
        args[0]->val_str(&value);
161
 
      break;
162
 
    }
 
174
  {
 
175
    save_result.vstr= use_result_field ? result_field->val_str(&value) :
 
176
                       args[0]->val_str(&value);
 
177
    break;
 
178
  }
163
179
  case DECIMAL_RESULT:
164
 
    {
165
 
      save_result.vdec= use_result_field ?
166
 
        result_field->val_decimal(&decimal_buff) :
167
 
        args[0]->val_decimal(&decimal_buff);
168
 
      break;
169
 
    }
 
180
  {
 
181
    save_result.vdec= use_result_field ?
 
182
                       result_field->val_decimal(&decimal_buff) :
 
183
                       args[0]->val_decimal(&decimal_buff);
 
184
    break;
 
185
  }
170
186
  case ROW_RESULT:
 
187
  default:
171
188
    // This case should never be chosen
172
189
    assert(0);
173
190
    break;
174
191
  }
175
 
 
176
 
  return false;
 
192
  return(false);
177
193
}
178
194
 
179
195
/**
198
214
 
199
215
  switch (cached_result_type) {
200
216
  case REAL_RESULT:
201
 
    {
202
 
      res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
203
 
                       REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
204
 
      break;
205
 
    }
206
 
 
 
217
  {
 
218
    res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
 
219
                     REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
 
220
    break;
 
221
  }
207
222
  case INT_RESULT:
208
 
    {
209
 
      res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
210
 
                       INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
211
 
                       unsigned_flag);
212
 
      break;
213
 
    }
214
 
 
 
223
  {
 
224
    res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
 
225
                     INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
 
226
                     unsigned_flag);
 
227
    break;
 
228
  }
215
229
  case STRING_RESULT:
216
 
    {
217
 
      if (!save_result.vstr)                                      // Null value
218
 
        res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
219
 
                         DERIVATION_IMPLICIT, 0);
220
 
      else
221
 
        res= update_hash((void*) save_result.vstr->ptr(),
222
 
                         save_result.vstr->length(), STRING_RESULT,
223
 
                         save_result.vstr->charset(),
224
 
                         DERIVATION_IMPLICIT, 0);
225
 
      break;
226
 
    }
227
 
 
 
230
  {
 
231
    if (!save_result.vstr)                                      // Null value
 
232
      res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
 
233
                       DERIVATION_IMPLICIT, 0);
 
234
    else
 
235
      res= update_hash((void*) save_result.vstr->ptr(),
 
236
                       save_result.vstr->length(), STRING_RESULT,
 
237
                       save_result.vstr->charset(),
 
238
                       DERIVATION_IMPLICIT, 0);
 
239
    break;
 
240
  }
228
241
  case DECIMAL_RESULT:
229
 
    {
230
 
      if (!save_result.vdec)                                      // Null value
231
 
        res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
232
 
                         DERIVATION_IMPLICIT, 0);
233
 
      else
234
 
        res= update_hash((void*) save_result.vdec,
235
 
                         sizeof(type::Decimal), DECIMAL_RESULT,
236
 
                         &my_charset_bin, DERIVATION_IMPLICIT, 0);
237
 
      break;
238
 
    }
239
 
 
 
242
  {
 
243
    if (!save_result.vdec)                                      // Null value
 
244
      res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
 
245
                       DERIVATION_IMPLICIT, 0);
 
246
    else
 
247
      res= update_hash((void*) save_result.vdec,
 
248
                       sizeof(my_decimal), DECIMAL_RESULT,
 
249
                       &my_charset_bin, DERIVATION_IMPLICIT, 0);
 
250
    break;
 
251
  }
240
252
  case ROW_RESULT:
 
253
  default:
241
254
    // This case should never be chosen
242
255
    assert(0);
243
256
    break;
244
257
  }
245
 
 
246
258
  return(res);
247
259
}
248
260
 
271
283
}
272
284
 
273
285
 
274
 
type::Decimal *Item_func_set_user_var::val_decimal(type::Decimal *val)
 
286
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
275
287
{
276
288
  assert(fixed == 1);
277
289
  check(0);
304
316
}
305
317
 
306
318
 
307
 
type::Decimal *Item_func_set_user_var::val_decimal_result(type::Decimal *val)
 
319
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
308
320
{
309
321
  assert(fixed == 1);
310
322
  check(true);
321
333
  str->append(')');
322
334
}
323
335
 
324
 
bool Item_func_set_user_var::send(plugin::Client *client, String *str_arg)
 
336
 
 
337
void Item_func_set_user_var::print_as_stmt(String *str,
 
338
                                           enum_query_type query_type)
 
339
{
 
340
  str->append(STRING_WITH_LEN("set @"));
 
341
  str->append(name.str, name.length);
 
342
  str->append(STRING_WITH_LEN(":="));
 
343
  args[0]->print(str, query_type);
 
344
  str->append(')');
 
345
}
 
346
 
 
347
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
325
348
{
326
349
  if (result_field)
327
350
  {
328
351
    check(1);
329
352
    update();
330
 
    return client->store(result_field);
 
353
    return protocol->store(result_field);
331
354
  }
332
 
  return Item::send(client, str_arg);
 
355
  return Item::send(protocol, str_arg);
333
356
}
334
357
 
335
 
void Item_func_set_user_var::make_field(SendField *tmp_field)
 
358
void Item_func_set_user_var::make_field(Send_field *tmp_field)
336
359
{
337
360
  if (result_field)
338
361
  {
342
365
      tmp_field->col_name=Item::name;               // Use user supplied name
343
366
  }
344
367
  else
345
 
  {
346
368
    Item::make_field(tmp_field);
347
 
  }
348
369
}
349
370
 
350
371
/*
427
448
  }
428
449
  else if (result_type() == DECIMAL_RESULT)
429
450
  {
430
 
    type::Decimal decimal_value;
431
 
    type::Decimal *val= entry->val_decimal(&null_value, &decimal_value);
 
451
    my_decimal decimal_value;
 
452
    my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
432
453
    if (null_value)
433
454
      return set_field_to_null(field);
434
455
    field->set_notnull();
446
467
}
447
468
 
448
469
 
449
 
} /* namespace drizzled */