22 #include <drizzled/charset.h>
23 #include <drizzled/function/str/strfunc.h>
24 #include <drizzled/internal/m_string.h>
25 #include <drizzled/plugin/function.h>
26 #include <drizzled/util/convert.h>
28 using namespace drizzled;
35 const char *func_name()
const {
return "hex"; }
37 void fix_length_and_dec()
39 collation.set(default_charset());
41 max_length=args[0]->max_length*2*collation.collation->mbmaxlen;
56 const char *func_name()
const {
return "unhex"; }
58 void fix_length_and_dec()
60 collation.set(&my_charset_bin);
62 max_length=(1+args[0]->max_length)/2;
70 static int hexchar_to_int(
char c)
72 if (c <= '9' && c >=
'0')
75 if (c <= 'f' && c >=
'a')
84 if (args[0]->result_type() != STRING_RESULT)
89 if (args[0]->result_type() == REAL_RESULT ||
90 args[0]->result_type() == DECIMAL_RESULT)
92 double val= args[0]->val_real();
93 if ((val <= (
double) INT64_MIN) ||
94 (val >= (
double) (uint64_t) UINT64_MAX))
97 dec= (uint64_t) (val + (val > 0 ? 0.5 : -0.5));
100 dec= (uint64_t) args[0]->val_int();
102 if ((null_value= args[0]->null_value))
104 ptr= internal::int64_t2str(dec,ans,16);
105 str->copy(ans,(uint32_t) (ptr-ans),default_charset());
110 res= args[0]->val_str(str);
117 tmp_value.alloc(res->length()*2+1);
118 tmp_value.length(res->length()*2);
120 (void) drizzled_string_to_hex((
char*) tmp_value.ptr(), res->ptr(),
129 const char *from, *end;
135 res= args[0]->val_str(str);
141 tmp_value.alloc(length= (1+res->length())/2);
145 tmp_value.length(length);
146 to= (
char*) tmp_value.ptr();
147 if (res->length() % 2)
150 *to++= hex_char= hexchar_to_int(*from++);
151 if ((null_value= (hex_char == -1)))
154 for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
157 *to= (hex_char= hexchar_to_int(from[0])) << 4;
158 if ((null_value= (hex_char == -1)))
160 *to|= hex_char= hexchar_to_int(from[1]);
161 if ((null_value= (hex_char == -1)))
167 plugin::Create_function<HexFunction> *hex_function= NULL;
168 plugin::Create_function<UnHexFunction> *unhex_function= NULL;
172 hex_function=
new plugin::Create_function<HexFunction>(
"hex");
173 unhex_function=
new plugin::Create_function<UnHexFunction>(
"unhex");
174 context.add(hex_function);
175 context.add(unhex_function);
179 DRIZZLE_DECLARE_PLUGIN
185 N_(
"HEX and UNHEX functions"),
191 DRIZZLE_DECLARE_PLUGIN_END;
bool check_argument_count(int n)
bool check_argument_count(int n)
String * val_str(String *)
String * val_str(String *)