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 |
||
20 |
#include <drizzled/server_includes.h> |
|
21 |
#include CSTDINT_H
|
|
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 |
|
27 |
void Item_func_repeat::fix_length_and_dec() |
|
28 |
{
|
|
29 |
collation.set(args[0]->collation); |
|
30 |
if (args[1]->const_item()) |
|
31 |
{
|
|
32 |
/* must be int64_t to avoid truncation */
|
|
33 |
int64_t count= args[1]->val_int(); |
|
34 |
||
35 |
/* Assumes that the maximum length of a String is < INT32_MAX. */
|
|
36 |
/* Set here so that rest of code sees out-of-bound value as such. */
|
|
37 |
if (count > INT32_MAX) |
|
38 |
count= INT32_MAX; |
|
39 |
||
40 |
uint64_t max_result_length= (uint64_t) args[0]->max_length * count; |
|
41 |
if (max_result_length >= MAX_BLOB_WIDTH) |
|
42 |
{
|
|
43 |
max_result_length= MAX_BLOB_WIDTH; |
|
44 |
maybe_null= 1; |
|
45 |
}
|
|
46 |
max_length= (ulong) max_result_length; |
|
47 |
}
|
|
48 |
else
|
|
49 |
{
|
|
50 |
max_length= MAX_BLOB_WIDTH; |
|
51 |
maybe_null= 1; |
|
52 |
}
|
|
53 |
}
|
|
54 |
||
55 |
/**
|
|
56 |
Item_func_repeat::str is carefully written to avoid reallocs
|
|
57 |
as much as possible at the cost of a local buffer
|
|
58 |
*/
|
|
59 |
||
60 |
String *Item_func_repeat::val_str(String *str) |
|
61 |
{
|
|
62 |
assert(fixed == 1); |
|
63 |
uint32_t length,tot_length; |
|
64 |
char *to; |
|
65 |
/* must be int64_t to avoid truncation */
|
|
66 |
int64_t count= args[1]->val_int(); |
|
67 |
String *res= args[0]->val_str(str); |
|
68 |
||
69 |
if (args[0]->null_value || args[1]->null_value) |
|
70 |
goto err; // string and/or delim are null |
|
71 |
null_value= 0; |
|
72 |
||
73 |
if (count <= 0 && (count == 0 || !args[1]->unsigned_flag)) |
|
74 |
return &my_empty_string; |
|
75 |
||
76 |
/* Assumes that the maximum length of a String is < INT32_MAX. */
|
|
77 |
/* Bounds check on count: If this is triggered, we will error. */
|
|
78 |
if ((uint64_t) count > INT32_MAX) |
|
79 |
count= INT32_MAX; |
|
80 |
if (count == 1) // To avoid reallocs |
|
81 |
return res; |
|
82 |
length=res->length(); |
|
83 |
// Safe length check
|
|
84 |
if (length > current_session->variables.max_allowed_packet / (uint) count) |
|
85 |
{
|
|
86 |
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN, |
|
87 |
ER_WARN_ALLOWED_PACKET_OVERFLOWED, |
|
88 |
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), |
|
89 |
func_name(), current_session->variables.max_allowed_packet); |
|
90 |
goto err; |
|
91 |
}
|
|
92 |
tot_length= length*(uint) count; |
|
93 |
if (!(res= alloc_buffer(res,str,&tmp_value,tot_length))) |
|
94 |
goto err; |
|
95 |
||
96 |
to=(char*) res->ptr()+length; |
|
97 |
while (--count) |
|
98 |
{
|
|
99 |
memcpy(to,res->ptr(),length); |
|
100 |
to+=length; |
|
101 |
}
|
|
102 |
return (res); |
|
103 |
||
104 |
err: |
|
105 |
null_value=1; |
|
106 |
return 0; |
|
107 |
}
|
|
108 |