~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/substr_functions/substr_functions.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <algorithm>
 
24
 
 
25
#include <drizzled/charset.h>
 
26
#include <drizzled/function/str/strfunc.h>
 
27
#include <drizzled/plugin/function.h>
 
28
 
 
29
using namespace std;
 
30
using namespace drizzled;
 
31
 
 
32
class SubstrFunction :public Item_str_func
 
33
{
 
34
  String tmp_value;
 
35
public:
 
36
  SubstrFunction() :Item_str_func() {}
 
37
 
 
38
  String *val_str(String *);
 
39
  void fix_length_and_dec();
 
40
  const char *func_name() const { return "substr"; }
 
41
 
 
42
  bool check_argument_count(int n) { return n == 2 || n == 3; }
 
43
};
 
44
 
 
45
 
 
46
class SubstrIndexFunction :public Item_str_func
 
47
{
 
48
  String tmp_value;
 
49
public:
 
50
  SubstrIndexFunction() :Item_str_func() {}
 
51
 
 
52
  String *val_str(String *);
 
53
  void fix_length_and_dec();
 
54
  const char *func_name() const { return "substring_index"; }
 
55
 
 
56
  bool check_argument_count(int n) { return n == 3; }
 
57
};
 
58
 
 
59
String *SubstrFunction::val_str(String *str)
 
60
{
 
61
  assert(fixed == 1);
 
62
  String *res  = args[0]->val_str(str);
 
63
  /* must be int64_t to avoid truncation */
 
64
  int64_t start= args[1]->val_int();
 
65
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
66
  /* Limit so that code sees out-of-bound value properly. */
 
67
  int64_t length= arg_count == 3 ? args[2]->val_int() : INT32_MAX;
 
68
  int64_t tmp_length;
 
69
 
 
70
  if ((null_value=(args[0]->null_value || args[1]->null_value ||
 
71
                   (arg_count == 3 && args[2]->null_value))))
 
72
    return 0;
 
73
 
 
74
  /* Negative or zero length, will return empty string. */
 
75
  if ((arg_count == 3) && (length <= 0) &&
 
76
      (length == 0 || !args[2]->unsigned_flag))
 
77
    return &my_empty_string;
 
78
 
 
79
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
80
  /* Set here so that rest of code sees out-of-bound value as such. */
 
81
  if ((length <= 0) || (length > INT32_MAX))
 
82
    length= INT32_MAX;
 
83
 
 
84
  /* if "unsigned_flag" is set, we have a *huge* positive number. */
 
85
  /* Assumes that the maximum length of a String is < INT32_MAX. */
 
86
  if ((!args[1]->unsigned_flag && (start < INT32_MIN || start > INT32_MAX)) ||
 
87
      (args[1]->unsigned_flag && ((uint64_t) start > INT32_MAX)))
 
88
    return &my_empty_string;
 
89
 
 
90
  start= ((start < 0) ?
 
91
          static_cast<int64_t>(res->numchars() + start)
 
92
          : start - 1);
 
93
  start= res->charpos((int) start);
 
94
  if ((start < 0) || ((uint) start + 1 > res->length()))
 
95
    return &my_empty_string;
 
96
 
 
97
  length= res->charpos((int) length, (uint32_t) start);
 
98
  tmp_length= res->length() - start;
 
99
  length= min(length, tmp_length);
 
100
 
 
101
  if (!start && (int64_t) res->length() == length)
 
102
    return res;
 
103
  tmp_value.set(*res, (uint32_t) start, (uint32_t) length);
 
104
  return &tmp_value;
 
105
}
 
106
 
 
107
void SubstrFunction::fix_length_and_dec()
 
108
{
 
109
  max_length=args[0]->max_length;
 
110
 
 
111
  collation.set(args[0]->collation);
 
112
  if (args[1]->const_item())
 
113
  {
 
114
    int32_t start= (int32_t) args[1]->val_int();
 
115
    if (start < 0)
 
116
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
 
117
    else
 
118
      max_length-= min((uint)(start - 1), max_length);
 
119
  }
 
120
  if (arg_count == 3 && args[2]->const_item())
 
121
  {
 
122
    int32_t length= (int32_t) args[2]->val_int();
 
123
    if (length <= 0)
 
124
      max_length=0;
 
125
    else
 
126
      set_if_smaller(max_length,(uint) length);
 
127
  }
 
128
  max_length*= collation.collation->mbmaxlen;
 
129
}
 
130
 
 
131
 
 
132
void SubstrIndexFunction::fix_length_and_dec()
 
133
{
 
134
  max_length= args[0]->max_length;
 
135
 
 
136
  if (agg_arg_charsets(collation, args, 2, MY_COLL_CMP_CONV, 1))
 
137
    return;
 
138
}
 
139
 
 
140
 
 
141
String *SubstrIndexFunction::val_str(String *str)
 
142
{
 
143
  assert(fixed == 1);
 
144
  String *res= args[0]->val_str(str);
 
145
  String *delimiter= args[1]->val_str(&tmp_value);
 
146
  int32_t count= (int32_t) args[2]->val_int();
 
147
  uint32_t offset;
 
148
 
 
149
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
 
150
  {                                     // string and/or delim are null
 
151
    null_value=1;
 
152
    return 0;
 
153
  }
 
154
  null_value=0;
 
155
  uint32_t delimiter_length= delimiter->length();
 
156
  if (!res->length() || !delimiter_length || !count)
 
157
    return &my_empty_string;            // Wrong parameters
 
158
 
 
159
  res->set_charset(collation.collation);
 
160
 
 
161
  if (use_mb(res->charset()))
 
162
  {
 
163
    const char *ptr= res->ptr();
 
164
    const char *strend= ptr+res->length();
 
165
    const char *end= strend-delimiter_length+1;
 
166
    const char *search= delimiter->ptr();
 
167
    const char *search_end= search+delimiter_length;
 
168
    int32_t n=0,c=count,pass;
 
169
    register uint32_t l;
 
170
    for (pass=(count>0);pass<2;++pass)
 
171
    {
 
172
      while (ptr < end)
 
173
      {
 
174
        if (*ptr == *search)
 
175
        {
 
176
          register char *i,*j;
 
177
          i=(char*) ptr+1; j=(char*) search+1;
 
178
          while (j != search_end)
 
179
            if (*i++ != *j++) goto skip;
 
180
          if (pass==0) ++n;
 
181
          else if (!--c) break;
 
182
          ptr+= delimiter_length;
 
183
          continue;
 
184
        }
 
185
    skip:
 
186
        if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
 
187
        else ++ptr;
 
188
      } /* either not found or got total number when count<0 */
 
189
      if (pass == 0) /* count<0 */
 
190
      {
 
191
        c+=n+1;
 
192
        if (c<=0) return res; /* not found, return original string */
 
193
        ptr=res->ptr();
 
194
      }
 
195
      else
 
196
      {
 
197
        if (c) return res; /* Not found, return original string */
 
198
        if (count>0) /* return left part */
 
199
        {
 
200
          tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
 
201
        }
 
202
        else /* return right part */
 
203
        {
 
204
          ptr+= delimiter_length;
 
205
          tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
 
206
        }
 
207
      }
 
208
    }
 
209
  }
 
210
  else
 
211
  {
 
212
    if (count > 0)
 
213
    {                                   // start counting from the beginning
 
214
      for (offset=0; ; offset+= delimiter_length)
 
215
      {
 
216
        if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
 
217
          return res;                   // Didn't find, return org string
 
218
        if (!--count)
 
219
        {
 
220
          tmp_value.set(*res,0,offset);
 
221
          break;
 
222
        }
 
223
      }
 
224
    }
 
225
    else
 
226
    {
 
227
      /*
 
228
        Negative index, start counting at the end
 
229
      */
 
230
      for (offset=res->length(); offset ;)
 
231
      {
 
232
        /*
 
233
          this call will result in finding the position pointing to one
 
234
          address space less than where the found substring is located
 
235
          in res
 
236
        */
 
237
        if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
 
238
          return res;                   // Didn't find, return org string
 
239
        /*
 
240
          At this point, we've searched for the substring
 
241
          the number of times as supplied by the index value
 
242
        */
 
243
        if (!++count)
 
244
        {
 
245
          offset+= delimiter_length;
 
246
          tmp_value.set(*res,offset,res->length()- offset);
 
247
          break;
 
248
        }
 
249
      }
 
250
    }
 
251
  }
 
252
  /*
 
253
    We always mark tmp_value as const so that if val_str() is called again
 
254
    on this object, we don't disrupt the contents of tmp_value when it was
 
255
    derived from another String.
 
256
  */
 
257
  tmp_value.mark_as_const();
 
258
  return (&tmp_value);
 
259
}
 
260
 
 
261
plugin::Create_function<SubstrFunction> *substr_function= NULL;
 
262
plugin::Create_function<SubstrIndexFunction> *substr_index_function= NULL;
 
263
 
 
264
static int initialize(drizzled::module::Context &context)
 
265
{
 
266
  substr_function= new plugin::Create_function<SubstrFunction>("substr");
 
267
  substr_index_function= new plugin::Create_function<SubstrIndexFunction>("substring_index");
 
268
  context.add(substr_function);
 
269
  context.add(substr_index_function);
 
270
  return 0;
 
271
}
 
272
 
 
273
DRIZZLE_DECLARE_PLUGIN
 
274
{
 
275
  DRIZZLE_VERSION_ID,
 
276
  "substr_functions",
 
277
  "1.0",
 
278
  "Stewart Smith",
 
279
  "SUBSTR and SUBSTR",
 
280
  PLUGIN_LICENSE_GPL,
 
281
  initialize, /* Plugin Init */
 
282
  NULL,   /* depends */
 
283
  NULL    /* config options */
 
284
}
 
285
DRIZZLE_DECLARE_PLUGIN_END;