~drizzle-trunk/drizzle/development

1300.4.2 by Stewart Smith
make REVERSE() a plugin instead of built in function. Generalise the code for such keyword functions otu into reserved_keyword_function() in sql_yacc.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
1300.4.2 by Stewart Smith
make REVERSE() a plugin instead of built in function. Generalise the code for such keyword functions otu into reserved_keyword_function() in sql_yacc.
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
26
using namespace drizzled;
27
28
class ReverseFunction :public Item_str_func
29
{
30
  String tmp_value;
31
public:
32
  ReverseFunction() :Item_str_func() {}
33
  String *val_str(String *);
34
  void fix_length_and_dec();
35
  const char *func_name() const { return "reverse"; }
36
37
  bool check_argument_count(int n)
38
  {
39
    return n == 1;
40
  }
41
};
42
43
String *ReverseFunction::val_str(String *str)
44
{
45
  assert(fixed == 1);
46
  String *res = args[0]->val_str(str);
47
  char *ptr, *end, *tmp;
48
49
  if ((null_value=args[0]->null_value))
50
    return 0;
51
  /* An empty string is a special case as the string pointer may be null */
52
  if (!res->length())
53
    return &my_empty_string;
54
  if (tmp_value.alloced_length() < res->length() &&
55
      tmp_value.realloc(res->length()))
56
  {
57
    null_value= 1;
58
    return 0;
59
  }
60
  tmp_value.length(res->length());
61
  tmp_value.set_charset(res->charset());
62
  ptr= (char *) res->ptr();
63
  end= ptr + res->length();
64
  tmp= (char *) tmp_value.ptr() + tmp_value.length();
65
  if (use_mb(res->charset()))
66
  {
67
    register uint32_t l;
68
    while (ptr < end)
69
    {
70
      if ((l= my_ismbchar(res->charset(),ptr,end)))
71
      {
72
        tmp-= l;
73
        memcpy(tmp,ptr,l);
74
        ptr+= l;
75
      }
76
      else
77
        *--tmp= *ptr++;
78
    }
79
  }
80
  else
81
  {
82
    while (ptr < end)
83
      *--tmp= *ptr++;
84
  }
85
  return &tmp_value;
86
}
87
88
void ReverseFunction::fix_length_and_dec()
89
{
90
  collation.set(args[0]->collation);
91
  max_length = args[0]->max_length;
92
}
93
94
plugin::Create_function<ReverseFunction> *reverse_function= NULL;
95
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
96
static int initialize(drizzled::module::Context &context)
1300.4.2 by Stewart Smith
make REVERSE() a plugin instead of built in function. Generalise the code for such keyword functions otu into reserved_keyword_function() in sql_yacc.
97
{
98
  reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
99
  context.add(reverse_function);
1300.4.2 by Stewart Smith
make REVERSE() a plugin instead of built in function. Generalise the code for such keyword functions otu into reserved_keyword_function() in sql_yacc.
100
  return 0;
101
}
102
103
DRIZZLE_DECLARE_PLUGIN
104
{
105
  DRIZZLE_VERSION_ID,
106
  "reverse_function",
107
  "1.0",
108
  "Stewart Smith",
109
  "reverses a string",
110
  PLUGIN_LICENSE_GPL,
111
  initialize, /* Plugin Init */
112
  NULL,   /* system variables */
113
  NULL    /* config options */
114
}
115
DRIZZLE_DECLARE_PLUGIN_END;