~drizzle-trunk/drizzle/development

574.3.3 by Lee
moving functions from item_strfunc to functions/str directory
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/str/replace.h>
23
#include <drizzled/error.h>
24
25
/**
26
  Replace all occurences of string2 in string1 with string3.
27
28
  Don't reallocate val_str() if not needed.
29
30
  @todo
31
    Fix that this works with binary strings when using USE_MB
32
*/
33
34
String *Item_func_replace::val_str(String *str)
35
{
36
  assert(fixed == 1);
37
  String *res,*res2,*res3;
38
  int offset;
39
  uint32_t from_length,to_length;
40
  bool alloced=0;
41
#ifdef USE_MB
42
  const char *ptr,*end,*strend,*search,*search_end;
43
  register uint32_t l;
44
  bool binary_cmp;
45
#endif
46
47
  null_value=0;
48
  res=args[0]->val_str(str);
49
  if (args[0]->null_value)
50
    goto null;
51
  res2=args[1]->val_str(&tmp_value);
52
  if (args[1]->null_value)
53
    goto null;
54
55
  res->set_charset(collation.collation);
56
57
#ifdef USE_MB
58
  binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
59
#endif
60
61
  if (res2->length() == 0)
62
    return res;
63
#ifndef USE_MB
64
  if ((offset=res->strstr(*res2)) < 0)
65
    return res;
66
#else
67
  offset=0;
68
  if (binary_cmp && (offset=res->strstr(*res2)) < 0)
69
    return res;
70
#endif
71
  if (!(res3=args[2]->val_str(&tmp_value2)))
72
    goto null;
73
  from_length= res2->length();
74
  to_length=   res3->length();
75
76
#ifdef USE_MB
77
  if (!binary_cmp)
78
  {
79
    search=res2->ptr();
80
    search_end=search+from_length;
81
redo:
82
    ptr=res->ptr()+offset;
83
    strend=res->ptr()+res->length();
84
    end=strend-from_length+1;
85
    while (ptr < end)
86
    {
87
        if (*ptr == *search)
88
        {
89
          register char *i,*j;
90
          i=(char*) ptr+1; j=(char*) search+1;
91
          while (j != search_end)
92
            if (*i++ != *j++) goto skip;
93
          offset= (int) (ptr-res->ptr());
94
          if (res->length()-from_length + to_length >
95
              current_session->variables.max_allowed_packet)
96
          {
97
            push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
98
                                ER_WARN_ALLOWED_PACKET_OVERFLOWED,
99
                                ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
100
                                func_name(),
101
                                current_session->variables.max_allowed_packet);
102
103
            goto null;
104
          }
105
          if (!alloced)
106
          {
107
            alloced=1;
108
            res=copy_if_not_alloced(str,res,res->length()+to_length);
109
          }
110
          res->replace((uint) offset,from_length,*res3);
111
          offset+=(int) to_length;
112
          goto redo;
113
        }
114
skip:
115
        if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
116
        else ++ptr;
117
    }
118
  }
119
  else
120
#endif /* USE_MB */
121
    do
122
    {
123
      if (res->length()-from_length + to_length >
124
          current_session->variables.max_allowed_packet)
125
      {
126
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
127
                            ER_WARN_ALLOWED_PACKET_OVERFLOWED,
128
                            ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
129
                            current_session->variables.max_allowed_packet);
130
        goto null;
131
      }
132
      if (!alloced)
133
      {
134
        alloced=1;
135
        res=copy_if_not_alloced(str,res,res->length()+to_length);
136
      }
137
      res->replace((uint) offset,from_length,*res3);
138
      offset+=(int) to_length;
139
    }
140
    while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
141
  return res;
142
143
null:
144
  null_value=1;
145
  return 0;
146
}
147
148
149
void Item_func_replace::fix_length_and_dec()
150
{
151
  uint64_t max_result_length= args[0]->max_length;
152
  int diff=(int) (args[2]->max_length - args[1]->max_length);
153
  if (diff > 0 && args[1]->max_length)
154
  {                                             // Calculate of maxreplaces
155
    uint64_t max_substrs= max_result_length/args[1]->max_length;
156
    max_result_length+= max_substrs * (uint) diff;
157
  }
158
  if (max_result_length >= MAX_BLOB_WIDTH)
159
  {
160
    max_result_length= MAX_BLOB_WIDTH;
161
    maybe_null= 1;
162
  }
163
  max_length= (ulong) max_result_length;
164
165
  if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV, 1))
166
    return;
167
}
168
169