Drizzled Public API Documentation

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