~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>
2154.2.4 by Brian Aker
This fixes 716459
25
#include <drizzled/charset_info.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;
55
  if (tmp_value.alloced_length() < res->length() &&
56
      tmp_value.realloc(res->length()))
57
  {
58
    null_value= 1;
59
    return 0;
60
  }
61
  tmp_value.length(res->length());
62
  tmp_value.set_charset(res->charset());
63
  ptr= (char *) res->ptr();
64
  end= ptr + res->length();
65
  tmp= (char *) tmp_value.ptr() + tmp_value.length();
66
  if (use_mb(res->charset()))
67
  {
68
    register uint32_t l;
69
    while (ptr < end)
70
    {
71
      if ((l= my_ismbchar(res->charset(),ptr,end)))
72
      {
73
        tmp-= l;
74
        memcpy(tmp,ptr,l);
75
        ptr+= l;
76
      }
77
      else
78
        *--tmp= *ptr++;
79
    }
80
  }
81
  else
82
  {
83
    while (ptr < end)
84
      *--tmp= *ptr++;
85
  }
86
  return &tmp_value;
87
}
88
89
void ReverseFunction::fix_length_and_dec()
90
{
91
  collation.set(args[0]->collation);
92
  max_length = args[0]->max_length;
93
}
94
95
plugin::Create_function<ReverseFunction> *reverse_function= NULL;
96
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
97
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.
98
{
99
  reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
100
  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.
101
  return 0;
102
}
103
104
DRIZZLE_DECLARE_PLUGIN
105
{
106
  DRIZZLE_VERSION_ID,
107
  "reverse_function",
108
  "1.0",
109
  "Stewart Smith",
110
  "reverses a string",
111
  PLUGIN_LICENSE_GPL,
112
  initialize, /* Plugin Init */
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
113
  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.
114
  NULL    /* config options */
115
}
116
DRIZZLE_DECLARE_PLUGIN_END;