~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/find_in_set.cc

  • Committer: Brian Aker
  • Date: 2008-09-04 19:31:00 UTC
  • Revision ID: brian@tangent.org-20080904193100-l849hgghfy4urj43
Changing default character set from this point on.

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/find_in_set.h>
23
 
 
24
 
/* Search after a string in a string of strings separated by ',' */
25
 
/* Returns number of found type >= 1 or 0 if not found */
26
 
/* This optimizes searching in enums to bit testing! */
27
 
 
28
 
void Item_func_find_in_set::fix_length_and_dec()
29
 
{
30
 
  decimals=0;
31
 
  max_length=3;                                 // 1-999
32
 
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
33
 
}
34
 
 
35
 
static const char separator=',';
36
 
 
37
 
int64_t Item_func_find_in_set::val_int()
38
 
{
39
 
  assert(fixed == 1);
40
 
  if (enum_value)
41
 
  {
42
 
    uint64_t tmp=(uint64_t) args[1]->val_int();
43
 
    if (!(null_value=args[1]->null_value || args[0]->null_value))
44
 
    {
45
 
      if (tmp & enum_bit)
46
 
        return enum_value;
47
 
    }
48
 
    return 0L;
49
 
  }
50
 
 
51
 
  String *find=args[0]->val_str(&value);
52
 
  String *buffer=args[1]->val_str(&value2);
53
 
  if (!find || !buffer)
54
 
  {
55
 
    null_value=1;
56
 
    return 0; /* purecov: inspected */
57
 
  }
58
 
  null_value=0;
59
 
 
60
 
  int diff;
61
 
  if ((diff=buffer->length() - find->length()) >= 0)
62
 
  {
63
 
    my_wc_t wc;
64
 
    const CHARSET_INFO * const cs= cmp_collation.collation;
65
 
    const char *str_begin= buffer->ptr();
66
 
    const char *str_end= buffer->ptr();
67
 
    const char *real_end= str_end+buffer->length();
68
 
    const unsigned char *find_str= (const unsigned char *) find->ptr();
69
 
    uint32_t find_str_len= find->length();
70
 
    int position= 0;
71
 
    while (1)
72
 
    {
73
 
      int symbol_len;
74
 
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end, 
75
 
                                       (unsigned char*) real_end)) > 0)
76
 
      {
77
 
        const char *substr_end= str_end + symbol_len;
78
 
        bool is_last_item= (substr_end == real_end);
79
 
        bool is_separator= (wc == (my_wc_t) separator);
80
 
        if (is_separator || is_last_item)
81
 
        {
82
 
          position++;
83
 
          if (is_last_item && !is_separator)
84
 
            str_end= substr_end;
85
 
          if (!my_strnncoll(cs, (const unsigned char *) str_begin,
86
 
                            str_end - str_begin,
87
 
                            find_str, find_str_len))
88
 
            return (int64_t) position;
89
 
          else
90
 
            str_begin= substr_end;
91
 
        }
92
 
        str_end= substr_end;
93
 
      }
94
 
      else if (str_end - str_begin == 0 &&
95
 
               find_str_len == 0 &&
96
 
               wc == (my_wc_t) separator)
97
 
        return (int64_t) ++position;
98
 
      else
99
 
        return 0L;
100
 
    }
101
 
  }
102
 
  return 0;
103
 
}
104