00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <drizzled/charset.h>
00023 #include <drizzled/field.h>
00024 #include <drizzled/item/decimal.h>
00025
00026 namespace drizzled {
00027
00028 Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
00029 const charset_info_st * const charset)
00030 {
00031 decimal_value.store(E_DEC_FATAL_ERROR, str_arg, length, charset);
00032 name= (char*) str_arg;
00033 decimals= (uint8_t) decimal_value.frac;
00034 fixed= 1;
00035 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00036 decimals, unsigned_flag);
00037 }
00038
00039 Item_decimal::Item_decimal(int64_t val, bool unsig)
00040 {
00041 int2_class_decimal(E_DEC_FATAL_ERROR, val, unsig, &decimal_value);
00042 decimals= (uint8_t) decimal_value.frac;
00043 fixed= 1;
00044 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00045 decimals, unsigned_flag);
00046 }
00047
00048
00049 Item_decimal::Item_decimal(double val, int, int)
00050 {
00051 double2_class_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
00052 decimals= (uint8_t) decimal_value.frac;
00053 fixed= 1;
00054 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00055 decimals, unsigned_flag);
00056 }
00057
00058 Item_decimal::Item_decimal(const char *str, const type::Decimal *val_arg,
00059 uint32_t decimal_par, uint32_t length)
00060 {
00061 class_decimal2decimal(val_arg, &decimal_value);
00062 name= (char*) str;
00063 decimals= (uint8_t) decimal_par;
00064 max_length= length;
00065 fixed= 1;
00066 }
00067
00068
00069 Item_decimal::Item_decimal(type::Decimal *value_par)
00070 {
00071 class_decimal2decimal(value_par, &decimal_value);
00072 decimals= (uint8_t) decimal_value.frac;
00073 fixed= 1;
00074 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00075 decimals, unsigned_flag);
00076 }
00077
00078
00079 Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
00080 {
00081 binary2_class_decimal(E_DEC_FATAL_ERROR, bin,
00082 &decimal_value, precision, scale);
00083 decimals= (uint8_t) decimal_value.frac;
00084 fixed= 1;
00085 max_length= class_decimal_precision_to_length(precision, decimals,
00086 unsigned_flag);
00087 }
00088
00089 int64_t Item_decimal::val_int()
00090 {
00091 int64_t result;
00092 decimal_value.val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
00093 return result;
00094 }
00095
00096 double Item_decimal::val_real()
00097 {
00098 double result;
00099 class_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &result);
00100 return result;
00101 }
00102
00103 String *Item_decimal::val_str(String *result)
00104 {
00105 result->set_charset(&my_charset_bin);
00106 class_decimal2string(&decimal_value, 0, result);
00107 return result;
00108 }
00109
00110 void Item_decimal::print(String *str)
00111 {
00112 class_decimal2string(&decimal_value, 0, &str_value);
00113 str->append(str_value);
00114 }
00115
00116 bool Item_decimal::eq(const Item *item, bool) const
00117 {
00118 if (type() == item->type() && item->basic_const_item())
00119 {
00120
00121
00122
00123
00124
00125
00126 Item *arg= (Item*) item;
00127 type::Decimal *value= arg->val_decimal(0);
00128 return !class_decimal_cmp(&decimal_value, value);
00129 }
00130 return 0;
00131 }
00132
00133
00134 void Item_decimal::set_decimal_value(type::Decimal *value_par)
00135 {
00136 class_decimal2decimal(value_par, &decimal_value);
00137 decimals= (uint8_t) decimal_value.frac;
00138 unsigned_flag= !decimal_value.sign();
00139 max_length= class_decimal_precision_to_length(decimal_value.intg + decimals,
00140 decimals, unsigned_flag);
00141 }
00142
00143 int Item_decimal::save_in_field(Field *field, bool)
00144 {
00145 field->set_notnull();
00146 return field->store_decimal(&decimal_value);
00147 }
00148
00149
00150 }