Drizzled Public API Documentation

repeat.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <drizzled/function/str/repeat.h>
00023 #include <drizzled/error.h>
00024 #include <drizzled/function/str/alloc_buffer.h>
00025 #include <drizzled/session.h>
00026 
00027 namespace drizzled
00028 {
00029 
00030 void Item_func_repeat::fix_length_and_dec()
00031 {
00032   collation.set(args[0]->collation);
00033   if (args[1]->const_item())
00034   {
00035     /* must be int64_t to avoid truncation */
00036     int64_t count= args[1]->val_int();
00037 
00038     /* Assumes that the maximum length of a String is < INT32_MAX. */
00039     /* Set here so that rest of code sees out-of-bound value as such. */
00040     if (count > INT32_MAX)
00041       count= INT32_MAX;
00042 
00043     uint64_t max_result_length= (uint64_t) args[0]->max_length * count;
00044     if (max_result_length >= MAX_BLOB_WIDTH)
00045     {
00046       max_result_length= MAX_BLOB_WIDTH;
00047       maybe_null= 1;
00048     }
00049     max_length= (ulong) max_result_length;
00050   }
00051   else
00052   {
00053     max_length= MAX_BLOB_WIDTH;
00054     maybe_null= 1;
00055   }
00056 }
00057 
00063 String *Item_func_repeat::val_str(String *str)
00064 {
00065   assert(fixed == 1);
00066   uint32_t length,tot_length;
00067   char *to;
00068   /* must be int64_t to avoid truncation */
00069   int64_t count= args[1]->val_int();
00070   String *res= args[0]->val_str(str);
00071 
00072   if (args[0]->null_value || args[1]->null_value)
00073     goto err;                           // string and/or delim are null
00074   null_value= 0;
00075 
00076   if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
00077     return &my_empty_string;
00078 
00079   /* Assumes that the maximum length of a String is < INT32_MAX. */
00080   /* Bounds check on count:  If this is triggered, we will error. */
00081   if ((uint64_t) count > INT32_MAX)
00082     count= INT32_MAX;
00083   if (count == 1)                       // To avoid reallocs
00084     return res;
00085   length=res->length();
00086   // Safe length check
00087   if (length > session.variables.max_allowed_packet / (uint) count)
00088   {
00089     push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
00090                         ER_WARN_ALLOWED_PACKET_OVERFLOWED,
00091                         ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
00092                         func_name(), session.variables.max_allowed_packet);
00093     goto err;
00094   }
00095   tot_length= length*(uint) count;
00096   if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
00097     goto err;
00098 
00099   to=(char*) res->ptr()+length;
00100   while (--count)
00101   {
00102     memcpy(to,res->ptr(),length);
00103     to+=length;
00104   }
00105   return (res);
00106 
00107 err:
00108   null_value=1;
00109   return 0;
00110 }
00111 
00112 } /* namespace drizzled */