Drizzled Public API Documentation

reverse_function.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  * Copyright (C) 2010 Stewart Smith
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include <drizzled/plugin/function.h>
24 #include <drizzled/function/str/strfunc.h>
25 #include <drizzled/charset.h>
26 
27 using namespace drizzled;
28 
30 {
31  String tmp_value;
32 public:
34  String *val_str(String *);
35  void fix_length_and_dec();
36  const char *func_name() const { return "reverse"; }
37 
38  bool check_argument_count(int n)
39  {
40  return n == 1;
41  }
42 };
43 
45 {
46  assert(fixed == 1);
47  String *res = args[0]->val_str(str);
48  char *ptr, *end, *tmp;
49 
50  if ((null_value=args[0]->null_value))
51  return 0;
52  /* An empty string is a special case as the string pointer may be null */
53  if (!res->length())
54  return &my_empty_string;
55  if (tmp_value.alloced_length() < res->length())
56  tmp_value.realloc(res->length());
57  tmp_value.length(res->length());
58  tmp_value.set_charset(res->charset());
59  ptr= (char *) res->ptr();
60  end= ptr + res->length();
61  tmp= (char *) tmp_value.ptr() + tmp_value.length();
62  if (use_mb(res->charset()))
63  {
64  register uint32_t l;
65  while (ptr < end)
66  {
67  if ((l= my_ismbchar(res->charset(),ptr,end)))
68  {
69  tmp-= l;
70  memcpy(tmp,ptr,l);
71  ptr+= l;
72  }
73  else
74  *--tmp= *ptr++;
75  }
76  }
77  else
78  {
79  while (ptr < end)
80  *--tmp= *ptr++;
81  }
82  return &tmp_value;
83 }
84 
85 void ReverseFunction::fix_length_and_dec()
86 {
87  collation.set(args[0]->collation);
88  max_length = args[0]->max_length;
89 }
90 
91 plugin::Create_function<ReverseFunction> *reverse_function= NULL;
92 
93 static int initialize(drizzled::module::Context &context)
94 {
95  reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
96  context.add(reverse_function);
97  return 0;
98 }
99 
100 DRIZZLE_DECLARE_PLUGIN
101 {
102  DRIZZLE_VERSION_ID,
103  "reverse_function",
104  "1.0",
105  "Stewart Smith",
106  N_("REVERSE function"),
107  PLUGIN_LICENSE_GPL,
108  initialize,
109  NULL,
110  NULL
111 }
112 DRIZZLE_DECLARE_PLUGIN_END;