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