~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/replace.cc

  • Committer: Brian Aker
  • Date: 2009-10-01 22:56:26 UTC
  • mto: (1154.1.1 staging)
  • mto: This revision was merged to the branch mainline in revision 1155.
  • Revision ID: brian@gaz-20091001225626-sb1pdykpxlnkheaj
Remove Factory/make scheduler work like everything else.

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