00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <drizzled/plugin/function.h>
00024 #include <drizzled/function/str/strfunc.h>
00025 #include <drizzled/charset.h>
00026
00027 using namespace drizzled;
00028
00029 class ReverseFunction :public Item_str_func
00030 {
00031 String tmp_value;
00032 public:
00033 ReverseFunction() :Item_str_func() {}
00034 String *val_str(String *);
00035 void fix_length_and_dec();
00036 const char *func_name() const { return "reverse"; }
00037
00038 bool check_argument_count(int n)
00039 {
00040 return n == 1;
00041 }
00042 };
00043
00044 String *ReverseFunction::val_str(String *str)
00045 {
00046 assert(fixed == 1);
00047 String *res = args[0]->val_str(str);
00048 char *ptr, *end, *tmp;
00049
00050 if ((null_value=args[0]->null_value))
00051 return 0;
00052
00053 if (!res->length())
00054 return &my_empty_string;
00055 if (tmp_value.alloced_length() < res->length())
00056 tmp_value.realloc(res->length());
00057 tmp_value.length(res->length());
00058 tmp_value.set_charset(res->charset());
00059 ptr= (char *) res->ptr();
00060 end= ptr + res->length();
00061 tmp= (char *) tmp_value.ptr() + tmp_value.length();
00062 if (use_mb(res->charset()))
00063 {
00064 register uint32_t l;
00065 while (ptr < end)
00066 {
00067 if ((l= my_ismbchar(res->charset(),ptr,end)))
00068 {
00069 tmp-= l;
00070 memcpy(tmp,ptr,l);
00071 ptr+= l;
00072 }
00073 else
00074 *--tmp= *ptr++;
00075 }
00076 }
00077 else
00078 {
00079 while (ptr < end)
00080 *--tmp= *ptr++;
00081 }
00082 return &tmp_value;
00083 }
00084
00085 void ReverseFunction::fix_length_and_dec()
00086 {
00087 collation.set(args[0]->collation);
00088 max_length = args[0]->max_length;
00089 }
00090
00091 plugin::Create_function<ReverseFunction> *reverse_function= NULL;
00092
00093 static int initialize(drizzled::module::Context &context)
00094 {
00095 reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
00096 context.add(reverse_function);
00097 return 0;
00098 }
00099
00100 DRIZZLE_DECLARE_PLUGIN
00101 {
00102 DRIZZLE_VERSION_ID,
00103 "reverse_function",
00104 "1.0",
00105 "Stewart Smith",
00106 "reverses a string",
00107 PLUGIN_LICENSE_GPL,
00108 initialize,
00109 NULL,
00110 NULL
00111 }
00112 DRIZZLE_DECLARE_PLUGIN_END;