~drizzle-trunk/drizzle/development

492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
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.
492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/locate.h>
492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
23
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
24
namespace drizzled
25
{
26
492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
27
void Item_func_locate::fix_length_and_dec()
28
{
29
  max_length= MY_INT32_NUM_DECIMAL_DIGITS;
30
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
31
}
32
33
int64_t Item_func_locate::val_int()
34
{
35
  assert(fixed == 1);
36
  String *a=args[0]->val_str(&value1);
37
  String *b=args[1]->val_str(&value2);
38
  if (!a || !b)
39
  {
40
    null_value=1;
971.6.11 by Eric Day
Removed purecov messages.
41
    return 0;
492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
42
  }
43
  null_value=0;
44
  /* must be int64_t to avoid truncation */
45
  int64_t start=  0;
46
  int64_t start0= 0;
47
  my_match_t match;
48
49
  if (arg_count == 3)
50
  {
51
    start0= start= args[2]->val_int() - 1;
52
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
53
    if ((start < 0) || (start > static_cast<int64_t>(a->length())))
492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
54
      return 0;
55
56
    /* start is now sufficiently valid to pass to charpos function */
57
    start= a->charpos((int) start);
58
59
    if (start + b->length() > a->length())
60
      return 0;
61
  }
62
  if (!b->length())                             // Found empty string at start
63
    return start + 1;
64
65
  if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
66
                                            a->ptr()+start,
895 by Brian Aker
Completion (?) of uint conversion.
67
                                            (uint32_t) (a->length()-start),
492.3.16 by Lee
break out item_func_bit, item_func_char_length, item_func_coercibility, item_func_locate, item_func_units into functions directory
68
                                            b->ptr(), b->length(),
69
                                            &match, 1))
70
    return 0;
71
  return (int64_t) match.mb_len + start0 + 1;
72
}
73
74
75
void Item_func_locate::print(String *str, enum_query_type query_type)
76
{
77
  str->append(STRING_WITH_LEN("locate("));
78
  args[1]->print(str, query_type);
79
  str->append(',');
80
  args[0]->print(str, query_type);
81
  if (arg_count == 3)
82
  {
83
    str->append(',');
84
    args[2]->print(str, query_type);
85
  }
86
  str->append(')');
87
}
88
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
89
} /* namespace drizzled */