~drizzle-trunk/drizzle/development

492.3.14 by Lee
code clean move Item_func_ascii, Item_func_bit_count, Item_func_find_in_set, Item_func_ord to 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.14 by Lee
code clean move Item_func_ascii, Item_func_bit_count, Item_func_find_in_set, Item_func_ord to 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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
2154.2.4 by Brian Aker
This fixes 716459
22
#include <drizzled/charset_info.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
23
#include <drizzled/function/find_in_set.h>
492.3.14 by Lee
code clean move Item_func_ascii, Item_func_bit_count, Item_func_find_in_set, Item_func_ord to functions directory
24
25
/* Search after a string in a string of strings separated by ',' */
26
/* Returns number of found type >= 1 or 0 if not found */
27
/* This optimizes searching in enums to bit testing! */
28
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
29
namespace drizzled
30
{
31
492.3.14 by Lee
code clean move Item_func_ascii, Item_func_bit_count, Item_func_find_in_set, Item_func_ord to functions directory
32
void Item_func_find_in_set::fix_length_and_dec()
33
{
34
  decimals=0;
35
  max_length=3;                                 // 1-999
36
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
37
}
38
39
static const char separator=',';
40
41
int64_t Item_func_find_in_set::val_int()
42
{
43
  assert(fixed == 1);
44
  if (enum_value)
45
  {
46
    uint64_t tmp=(uint64_t) args[1]->val_int();
47
    if (!(null_value=args[1]->null_value || args[0]->null_value))
48
    {
49
      if (tmp & enum_bit)
50
	return enum_value;
51
    }
52
    return 0L;
53
  }
54
55
  String *find=args[0]->val_str(&value);
56
  String *buffer=args[1]->val_str(&value2);
57
  if (!find || !buffer)
58
  {
59
    null_value=1;
971.6.11 by Eric Day
Removed purecov messages.
60
    return 0;
492.3.14 by Lee
code clean move Item_func_ascii, Item_func_bit_count, Item_func_find_in_set, Item_func_ord to functions directory
61
  }
62
  null_value=0;
63
64
  int diff;
65
  if ((diff=buffer->length() - find->length()) >= 0)
66
  {
67
    my_wc_t wc;
68
    const CHARSET_INFO * const cs= cmp_collation.collation;
69
    const char *str_begin= buffer->ptr();
70
    const char *str_end= buffer->ptr();
71
    const char *real_end= str_end+buffer->length();
72
    const unsigned char *find_str= (const unsigned char *) find->ptr();
73
    uint32_t find_str_len= find->length();
74
    int position= 0;
75
    while (1)
76
    {
77
      int symbol_len;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
78
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end,
492.3.14 by Lee
code clean move Item_func_ascii, Item_func_bit_count, Item_func_find_in_set, Item_func_ord to functions directory
79
                                       (unsigned char*) real_end)) > 0)
80
      {
81
        const char *substr_end= str_end + symbol_len;
82
        bool is_last_item= (substr_end == real_end);
83
        bool is_separator= (wc == (my_wc_t) separator);
84
        if (is_separator || is_last_item)
85
        {
86
          position++;
87
          if (is_last_item && !is_separator)
88
            str_end= substr_end;
89
          if (!my_strnncoll(cs, (const unsigned char *) str_begin,
90
                            str_end - str_begin,
91
                            find_str, find_str_len))
92
            return (int64_t) position;
93
          else
94
            str_begin= substr_end;
95
        }
96
        str_end= substr_end;
97
      }
98
      else if (str_end - str_begin == 0 &&
99
               find_str_len == 0 &&
100
               wc == (my_wc_t) separator)
101
        return (int64_t) ++position;
102
      else
103
        return 0L;
104
    }
105
  }
106
  return 0;
107
}
108
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
109
} /* namespace drizzled */