Drizzled Public API Documentation

locate.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/charset_info.h>
00023 #include <drizzled/function/locate.h>
00024 #include <drizzled/lex_string.h>
00025 
00026 namespace drizzled
00027 {
00028 
00029 void Item_func_locate::fix_length_and_dec()
00030 {
00031   max_length= MY_INT32_NUM_DECIMAL_DIGITS;
00032   agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
00033 }
00034 
00035 int64_t Item_func_locate::val_int()
00036 {
00037   assert(fixed == 1);
00038   String *a=args[0]->val_str(&value1);
00039   String *b=args[1]->val_str(&value2);
00040   if (!a || !b)
00041   {
00042     null_value=1;
00043     return 0;
00044   }
00045   null_value=0;
00046   /* must be int64_t to avoid truncation */
00047   int64_t start=  0;
00048   int64_t start0= 0;
00049   my_match_t match;
00050 
00051   if (arg_count == 3)
00052   {
00053     start0= start= args[2]->val_int() - 1;
00054 
00055     if ((start < 0) || (start > static_cast<int64_t>(a->length())))
00056       return 0;
00057 
00058     /* start is now sufficiently valid to pass to charpos function */
00059     start= a->charpos((int) start);
00060 
00061     if (start + b->length() > a->length())
00062       return 0;
00063   }
00064   if (!b->length())                             // Found empty string at start
00065     return start + 1;
00066 
00067   if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
00068                                             a->ptr()+start,
00069                                             (uint32_t) (a->length()-start),
00070                                             b->ptr(), b->length(),
00071                                             &match, 1))
00072     return 0;
00073   return (int64_t) match.mb_len + start0 + 1;
00074 }
00075 
00076 
00077 void Item_func_locate::print(String *str)
00078 {
00079   str->append(STRING_WITH_LEN("locate("));
00080   args[1]->print(str);
00081   str->append(',');
00082   args[0]->print(str);
00083   if (arg_count == 3)
00084   {
00085     str->append(',');
00086     args[2]->print(str);
00087   }
00088   str->append(')');
00089 }
00090 
00091 } /* namespace drizzled */