Drizzled Public API Documentation

locate.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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <drizzled/charset.h>
23 #include <drizzled/function/locate.h>
24 #include <drizzled/lex_string.h>
25 
26 namespace drizzled
27 {
28 
29 void Item_func_locate::fix_length_and_dec()
30 {
31  max_length= MY_INT32_NUM_DECIMAL_DIGITS;
32  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
33 }
34 
36 {
37  assert(fixed == 1);
38  String *a=args[0]->val_str(&value1);
39  String *b=args[1]->val_str(&value2);
40  if (!a || !b)
41  {
42  null_value=1;
43  return 0;
44  }
45  null_value=0;
46  /* must be int64_t to avoid truncation */
47  int64_t start= 0;
48  int64_t start0= 0;
49  my_match_t match;
50 
51  if (arg_count == 3)
52  {
53  start0= start= args[2]->val_int() - 1;
54 
55  if ((start < 0) || (start > static_cast<int64_t>(a->length())))
56  return 0;
57 
58  /* start is now sufficiently valid to pass to charpos function */
59  start= a->charpos((int) start);
60 
61  if (start + b->length() > a->length())
62  return 0;
63  }
64  if (!b->length()) // Found empty string at start
65  return start + 1;
66 
67  if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
68  a->ptr()+start,
69  (uint32_t) (a->length()-start),
70  b->ptr(), b->length(),
71  &match, 1))
72  return 0;
73  return (int64_t) match.mb_len + start0 + 1;
74 }
75 
76 
78 {
79  str->append(STRING_WITH_LEN("locate("));
80  args[1]->print(str);
81  str->append(',');
82  args[0]->print(str);
83  if (arg_count == 3)
84  {
85  str->append(',');
86  args[2]->print(str);
87  }
88  str->append(')');
89 }
90 
91 } /* namespace drizzled */
virtual int64_t val_int()=0
bool fixed
Definition: item.h:120
TODO: Rename this file - func.h is stupid.
virtual void print(String *str)
Definition: locate.cc:77
bool null_value
Definition: item.h:122
virtual String * val_str(String *str)=0
virtual void print(String *str)
Definition: item.cc:362