~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/locate.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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
 
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 <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/functions/locate.h>
 
23
 
 
24
void Item_func_locate::fix_length_and_dec()
 
25
{
 
26
  max_length= MY_INT32_NUM_DECIMAL_DIGITS;
 
27
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
 
28
}
 
29
 
 
30
int64_t Item_func_locate::val_int()
 
31
{
 
32
  assert(fixed == 1);
 
33
  String *a=args[0]->val_str(&value1);
 
34
  String *b=args[1]->val_str(&value2);
 
35
  if (!a || !b)
 
36
  {
 
37
    null_value=1;
 
38
    return 0; /* purecov: inspected */
 
39
  }
 
40
  null_value=0;
 
41
  /* must be int64_t to avoid truncation */
 
42
  int64_t start=  0;
 
43
  int64_t start0= 0;
 
44
  my_match_t match;
 
45
 
 
46
  if (arg_count == 3)
 
47
  {
 
48
    start0= start= args[2]->val_int() - 1;
 
49
 
 
50
    if ((start < 0) || (start > a->length()))
 
51
      return 0;
 
52
 
 
53
    /* start is now sufficiently valid to pass to charpos function */
 
54
    start= a->charpos((int) start);
 
55
 
 
56
    if (start + b->length() > a->length())
 
57
      return 0;
 
58
  }
 
59
  if (!b->length())                             // Found empty string at start
 
60
    return start + 1;
 
61
 
 
62
  if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
 
63
                                            a->ptr()+start,
 
64
                                            (uint) (a->length()-start),
 
65
                                            b->ptr(), b->length(),
 
66
                                            &match, 1))
 
67
    return 0;
 
68
  return (int64_t) match.mb_len + start0 + 1;
 
69
}
 
70
 
 
71
 
 
72
void Item_func_locate::print(String *str, enum_query_type query_type)
 
73
{
 
74
  str->append(STRING_WITH_LEN("locate("));
 
75
  args[1]->print(str, query_type);
 
76
  str->append(',');
 
77
  args[0]->print(str, query_type);
 
78
  if (arg_count == 3)
 
79
  {
 
80
    str->append(',');
 
81
    args[2]->print(str, query_type);
 
82
  }
 
83
  str->append(')');
 
84
}
 
85