~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/str/repeat.cc

  • Committer: Brian Aker
  • Date: 2008-11-13 02:56:15 UTC
  • mfrom: (575.4.10 devel)
  • Revision ID: brian@tangent.org-20081113025615-snhsi52yb2ivmx6f
Merging Monty's code.

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