22 #include <drizzled/function/set_user_var.h>
23 #include <drizzled/field/num.h>
24 #include <drizzled/session.h>
25 #include <drizzled/plugin/client.h>
26 #include <drizzled/user_var_entry.h>
27 #include <drizzled/table.h>
36 bool Item_func_set_user_var::fix_fields(Session *session, Item **ref)
40 if (Item_func::fix_fields(session, ref) ||
41 !(entry= session->getVariable(name,
true)))
48 entry->update_query_id= session->getQueryId();
64 null_item= (args[0]->type() == NULL_ITEM);
65 if (!entry->collation.collation || !null_item)
66 entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
67 collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
68 cached_result_type= args[0]->result_type();
73 Item_func_set_user_var::fix_length_and_dec()
76 max_length=args[0]->max_length;
77 decimals=args[0]->decimals;
78 collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
89 bool Item_func_set_user_var::register_field_in_read_map(
unsigned char *arg)
93 Table *table= (Table *) arg;
94 if (result_field->getTable() == table || !table)
95 result_field->getTable()->setReadSet(result_field->position());
102 Item_func_set_user_var::update_hash(
data_ref data,
103 Item_result res_type,
104 const charset_info_st *
const cs,
Derivation dv,
112 res_type= entry->type;
133 if (use_result_field && !result_field)
134 use_result_field=
false;
136 switch (cached_result_type) {
139 save_result.vreal= use_result_field ? result_field->val_real() :
145 save_result.vint= use_result_field ? result_field->val_int() :
148 unsigned_flag= use_result_field ? ((
Field_num*)result_field)->unsigned_flag:
149 args[0]->unsigned_flag;
155 save_result.vstr= use_result_field ? result_field->val_str_internal(&value) :
161 save_result.vdec= use_result_field ?
162 result_field->val_decimal(&decimal_buff) :
193 switch (cached_result_type) {
196 update_hash(
data_ref(&save_result.vreal,
sizeof(save_result.vreal)), REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
202 update_hash(
data_ref(&save_result.vint,
sizeof(save_result.vint)), INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, unsigned_flag);
208 if (!save_result.vstr)
209 update_hash(
data_ref(), STRING_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
211 update_hash(*save_result.vstr, STRING_RESULT, save_result.vstr->charset(), DERIVATION_IMPLICIT, 0);
217 if (!save_result.vdec)
218 update_hash(
data_ref(), DECIMAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
220 update_hash(
data_ref(save_result.vdec,
sizeof(
type::Decimal)), DECIMAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
264 double Item_func_set_user_var::val_result()
272 int64_t Item_func_set_user_var::val_int_result()
280 String *Item_func_set_user_var::str_result(String *str)
289 type::Decimal *Item_func_set_user_var::val_decimal_result(type::Decimal *val)
299 str->append(STRING_WITH_LEN(
"(@"));
301 str->append(STRING_WITH_LEN(
":="));
312 client->store(result_field);
318 void Item_func_set_user_var::make_field(
SendField *tmp_field)
322 result_field->make_field(tmp_field);
323 assert(tmp_field->table_name != 0);
329 Item::make_field(tmp_field);
371 int Item_func_set_user_var::save_in_field(Field *field,
bool no_conversions,
372 bool can_use_result_field)
374 bool use_result_field= (!can_use_result_field ? 0 :
375 (result_field && result_field != field));
379 check(use_result_field);
382 if (result_type() == STRING_RESULT ||
383 (result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
386 const charset_info_st *
const cs= collation.collation;
387 char buff[MAX_FIELD_WIDTH];
388 str_value.set_quick(buff,
sizeof(buff), cs);
399 field->set_notnull();
400 error=field->store(result->ptr(),result->length(),cs);
403 else if (result_type() == REAL_RESULT)
407 return set_field_to_null(field);
408 field->set_notnull();
409 error=field->store(nr);
411 else if (result_type() == DECIMAL_RESULT)
413 type::Decimal decimal_value;
416 return set_field_to_null(field);
417 field->set_notnull();
418 error=field->store_decimal(val);
425 field->set_notnull();
426 error=field->store(nr, unsigned_flag);