~drizzle-trunk/drizzle/development

574.3.8 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
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/str/repeat.h>
574.3.8 by Lee
moving functions from item_strfunc to functions/str directory
23
#include <drizzled/error.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
24
#include <drizzled/function/str/alloc_buffer.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
25
#include <drizzled/session.h>
574.3.8 by Lee
moving functions from item_strfunc to functions/str directory
26
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
27
namespace drizzled
28
{
29
574.3.8 by Lee
moving functions from item_strfunc to functions/str directory
30
void Item_func_repeat::fix_length_and_dec()
31
{
32
  collation.set(args[0]->collation);
33
  if (args[1]->const_item())
34
  {
35
    /* must be int64_t to avoid truncation */
36
    int64_t count= args[1]->val_int();
37
38
    /* Assumes that the maximum length of a String is < INT32_MAX. */
39
    /* Set here so that rest of code sees out-of-bound value as such. */
40
    if (count > INT32_MAX)
41
      count= INT32_MAX;
42
43
    uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
44
    if (max_result_length >= MAX_BLOB_WIDTH)
45
    {
46
      max_result_length= MAX_BLOB_WIDTH;
47
      maybe_null= 1;
48
    }
49
    max_length= (ulong) max_result_length;
50
  }
51
  else
52
  {
53
    max_length= MAX_BLOB_WIDTH;
54
    maybe_null= 1;
55
  }
56
}
57
58
/**
59
  Item_func_repeat::str is carefully written to avoid reallocs
60
  as much as possible at the cost of a local buffer
61
*/
62
63
String *Item_func_repeat::val_str(String *str)
64
{
65
  assert(fixed == 1);
66
  uint32_t length,tot_length;
67
  char *to;
68
  /* must be int64_t to avoid truncation */
69
  int64_t count= args[1]->val_int();
70
  String *res= args[0]->val_str(str);
71
72
  if (args[0]->null_value || args[1]->null_value)
73
    goto err;                           // string and/or delim are null
74
  null_value= 0;
75
76
  if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
77
    return &my_empty_string;
78
79
  /* Assumes that the maximum length of a String is < INT32_MAX. */
80
  /* Bounds check on count:  If this is triggered, we will error. */
81
  if ((uint64_t) count > INT32_MAX)
82
    count= INT32_MAX;
83
  if (count == 1)                       // To avoid reallocs
84
    return res;
85
  length=res->length();
86
  // Safe length check
1633.4.5 by Brian Aker
More current_session.
87
  if (length > session.variables.max_allowed_packet / (uint) count)
574.3.8 by Lee
moving functions from item_strfunc to functions/str directory
88
  {
1633.4.5 by Brian Aker
More current_session.
89
    push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
574.3.8 by Lee
moving functions from item_strfunc to functions/str directory
90
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
91
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
1633.4.5 by Brian Aker
More current_session.
92
                        func_name(), session.variables.max_allowed_packet);
574.3.8 by Lee
moving functions from item_strfunc to functions/str directory
93
    goto err;
94
  }
95
  tot_length= length*(uint) count;
96
  if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
97
    goto err;
98
99
  to=(char*) res->ptr()+length;
100
  while (--count)
101
  {
102
    memcpy(to,res->ptr(),length);
103
    to+=length;
104
  }
105
  return (res);
106
107
err:
108
  null_value=1;
109
  return 0;
110
}
111
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
112
} /* namespace drizzled */