~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/set_user_var.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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
 
 
27
 
namespace drizzled
28
 
{
29
28
 
30
29
/*
31
30
  When a user variable is updated (in a SET command or a query like
37
36
  assert(fixed == 0);
38
37
  /* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
39
38
  if (Item_func::fix_fields(session, ref) ||
40
 
      !(entry= session->getVariable(name, true)))
 
39
      !(entry= get_variable(&session->user_vars, name, 1)))
41
40
    return true;
42
41
  /*
43
42
     Remember the last query which updated it, this way a query can later know
44
43
     if this variable is a constant item in the query (it is if update_query_id
45
44
     is different from query_id).
46
45
  */
47
 
  entry->update_query_id= session->getQueryId();
 
46
  entry->update_query_id= session->query_id;
48
47
  /*
49
48
    As it is wrong and confusing to associate any
50
49
    character set with NULL, @a should be latin2
90
89
  if (result_field)
91
90
  {
92
91
    Table *table= (Table *) arg;
93
 
    if (result_field->getTable() == table || !table)
94
 
      result_field->getTable()->setReadSet(result_field->position());
95
 
  }
96
 
  return 0;
97
 
}
98
 
 
 
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
}
99
116
 
100
117
bool
101
118
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
109
126
  */
110
127
  if ((null_value= args[0]->null_value) && null_item)
111
128
    res_type= entry->type;                      // Don't change type of item
112
 
  if (entry->update_hash((null_value= args[0]->null_value),
113
 
                         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))
114
131
  {
115
132
    null_value= 1;
116
133
    return 1;
140
157
 
141
158
  switch (cached_result_type) {
142
159
  case REAL_RESULT:
143
 
    {
144
 
      save_result.vreal= use_result_field ? result_field->val_real() :
145
 
        args[0]->val_real();
146
 
      break;
147
 
    }
 
160
  {
 
161
    save_result.vreal= use_result_field ? result_field->val_real() :
 
162
                        args[0]->val_real();
 
163
    break;
 
164
  }
148
165
  case INT_RESULT:
149
 
    {
150
 
      save_result.vint= use_result_field ? result_field->val_int() :
151
 
        args[0]->val_int();
152
 
      unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
153
 
        args[0]->unsigned_flag;
154
 
      break;
155
 
    }
 
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
  }
156
173
  case STRING_RESULT:
157
 
    {
158
 
      save_result.vstr= use_result_field ? result_field->val_str_internal(&value) :
159
 
        args[0]->val_str(&value);
160
 
      break;
161
 
    }
 
174
  {
 
175
    save_result.vstr= use_result_field ? result_field->val_str(&value) :
 
176
                       args[0]->val_str(&value);
 
177
    break;
 
178
  }
162
179
  case DECIMAL_RESULT:
163
 
    {
164
 
      save_result.vdec= use_result_field ?
165
 
        result_field->val_decimal(&decimal_buff) :
166
 
        args[0]->val_decimal(&decimal_buff);
167
 
      break;
168
 
    }
 
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
  }
169
186
  case ROW_RESULT:
 
187
  default:
170
188
    // This case should never be chosen
171
189
    assert(0);
172
190
    break;
173
191
  }
174
 
 
175
 
  return false;
 
192
  return(false);
176
193
}
177
194
 
178
195
/**
197
214
 
198
215
  switch (cached_result_type) {
199
216
  case REAL_RESULT:
200
 
    {
201
 
      res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
202
 
                       REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
203
 
      break;
204
 
    }
205
 
 
 
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
  }
206
222
  case INT_RESULT:
207
 
    {
208
 
      res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
209
 
                       INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
210
 
                       unsigned_flag);
211
 
      break;
212
 
    }
213
 
 
 
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
  }
214
229
  case STRING_RESULT:
215
 
    {
216
 
      if (!save_result.vstr)                                      // Null value
217
 
        res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
218
 
                         DERIVATION_IMPLICIT, 0);
219
 
      else
220
 
        res= update_hash((void*) save_result.vstr->ptr(),
221
 
                         save_result.vstr->length(), STRING_RESULT,
222
 
                         save_result.vstr->charset(),
223
 
                         DERIVATION_IMPLICIT, 0);
224
 
      break;
225
 
    }
226
 
 
 
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
  }
227
241
  case DECIMAL_RESULT:
228
 
    {
229
 
      if (!save_result.vdec)                                      // Null value
230
 
        res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
231
 
                         DERIVATION_IMPLICIT, 0);
232
 
      else
233
 
        res= update_hash((void*) save_result.vdec,
234
 
                         sizeof(type::Decimal), DECIMAL_RESULT,
235
 
                         &my_charset_bin, DERIVATION_IMPLICIT, 0);
236
 
      break;
237
 
    }
238
 
 
 
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
  }
239
252
  case ROW_RESULT:
 
253
  default:
240
254
    // This case should never be chosen
241
255
    assert(0);
242
256
    break;
243
257
  }
244
 
 
245
258
  return(res);
246
259
}
247
260
 
270
283
}
271
284
 
272
285
 
273
 
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)
274
287
{
275
288
  assert(fixed == 1);
276
289
  check(0);
303
316
}
304
317
 
305
318
 
306
 
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)
307
320
{
308
321
  assert(fixed == 1);
309
322
  check(true);
320
333
  str->append(')');
321
334
}
322
335
 
323
 
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)
324
348
{
325
349
  if (result_field)
326
350
  {
327
351
    check(1);
328
352
    update();
329
 
    return client->store(result_field);
 
353
    return protocol->store(result_field);
330
354
  }
331
 
  return Item::send(client, str_arg);
 
355
  return Item::send(protocol, str_arg);
332
356
}
333
357
 
334
 
void Item_func_set_user_var::make_field(SendField *tmp_field)
 
358
void Item_func_set_user_var::make_field(Send_field *tmp_field)
335
359
{
336
360
  if (result_field)
337
361
  {
341
365
      tmp_field->col_name=Item::name;               // Use user supplied name
342
366
  }
343
367
  else
344
 
  {
345
368
    Item::make_field(tmp_field);
346
 
  }
347
369
}
348
370
 
349
371
/*
426
448
  }
427
449
  else if (result_type() == DECIMAL_RESULT)
428
450
  {
429
 
    type::Decimal decimal_value;
430
 
    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);
431
453
    if (null_value)
432
454
      return set_field_to_null(field);
433
455
    field->set_notnull();
445
467
}
446
468
 
447
469
 
448
 
} /* namespace drizzled */