Drizzled Public API Documentation

charlength.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/math/int.h>
00023 #include <drizzled/plugin.h>
00024 #include <drizzled/plugin/function.h>
00025 
00026 using namespace std;
00027 using namespace drizzled;
00028 
00029 class CharLengthFunction :public Item_int_func
00030 {
00031   String value;
00032 public:
00033   int64_t val_int();
00034   CharLengthFunction() :Item_int_func() {}
00035   
00036   const char *func_name() const 
00037   { 
00038     return "char_length"; 
00039   }
00040 
00041   void fix_length_and_dec() 
00042   { 
00043     max_length= 10;
00044   }
00045 
00046   bool check_argument_count(int n)
00047   {
00048     return (n == 1);
00049   }
00050 };
00051 
00052 
00053 int64_t CharLengthFunction::val_int()
00054 {
00055   assert(fixed == true);
00056   String *res=args[0]->val_str(&value);
00057   
00058   if (res == NULL)
00059   {
00060     null_value= true;
00061     return 0;
00062   }
00063   
00064   null_value= false;
00065   return (int64_t) res->numchars();
00066 }
00067 
00068 plugin::Create_function<CharLengthFunction> *charlengthudf= NULL;
00069 plugin::Create_function<CharLengthFunction> *characterlengthudf= NULL;
00070 
00071 static int initialize(drizzled::module::Context &context)
00072 {
00073   charlengthudf= new plugin::Create_function<CharLengthFunction>("char_length");
00074   characterlengthudf= new plugin::Create_function<CharLengthFunction>("character_length");
00075   context.add(charlengthudf);
00076   context.add(characterlengthudf);
00077   return 0;
00078 }
00079 
00080 DRIZZLE_DECLARE_PLUGIN
00081 {
00082   DRIZZLE_VERSION_ID,
00083   "charlength",
00084   "1.0",
00085   "Devananda van der Veen",
00086   "Return the number of characters in a string",
00087   PLUGIN_LICENSE_GPL,
00088   initialize, /* Plugin Init */
00089   NULL,   /* depends */
00090   NULL    /* config options */
00091 }
00092 DRIZZLE_DECLARE_PLUGIN_END;