~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
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.
22
23
#include <drizzled/plugin/function.h>
24
#include <drizzled/function/str/strfunc.h>
2281.5.1 by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file.
25
#include <drizzled/charset.h>
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.
26
27
using namespace drizzled;
28
29
class ReverseFunction :public Item_str_func
30
{
31
  String tmp_value;
32
public:
33
  ReverseFunction() :Item_str_func() {}
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
44
String *ReverseFunction::val_str(String *str)
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;
2281.6.3 by Olaf van der Spek
Return void
55
  if (tmp_value.alloced_length() < res->length())
56
      tmp_value.realloc(res->length());
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.
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
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
93
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.
94
{
95
  reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
96
  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.
97
  return 0;
98
}
99
100
DRIZZLE_DECLARE_PLUGIN
101
{
102
  DRIZZLE_VERSION_ID,
103
  "reverse_function",
104
  "1.0",
105
  "Stewart Smith",
106
  "reverses a string",
107
  PLUGIN_LICENSE_GPL,
108
  initialize, /* Plugin Init */
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
109
  NULL,   /* depends */
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.
110
  NULL    /* config options */
111
}
112
DRIZZLE_DECLARE_PLUGIN_END;