Drizzled Public API Documentation

crc32udf.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/plugin.h>
00023 #include <drizzled/plugin/function.h>
00024 #include <drizzled/item/func.h>
00025 #include <drizzled/algorithm/crc32.h>
00026 
00027 #include <string>
00028 
00029 using namespace std;
00030 using namespace drizzled;
00031 
00032 class Crc32Function :public Item_int_func
00033 {
00034 public:
00035   int64_t val_int();
00036   
00037   Crc32Function() :Item_int_func() 
00038   { 
00039     unsigned_flag= true; 
00040   }
00041   
00042   const char *func_name() const 
00043   { 
00044     return "crc32"; 
00045   }
00046   
00047   void fix_length_and_dec() 
00048   { 
00049     max_length= 10; 
00050   }
00051   
00052   bool check_argument_count(int n) 
00053   { 
00054     return (n == 1); 
00055   }
00056 };
00057 
00058 int64_t Crc32Function::val_int()
00059 {
00060   assert(fixed == true);
00061   String value;
00062   String *res=args[0]->val_str(&value);
00063   
00064   if (res == NULL)
00065   {
00066     null_value= true;
00067     return 0;
00068   }
00069 
00070   null_value= false;
00071   return static_cast<int64_t>(drizzled::algorithm::crc32(res->ptr(), res->length()));
00072 }
00073 
00074 plugin::Create_function<Crc32Function> *crc32udf= NULL;
00075 
00076 static int initialize(module::Context &context)
00077 {
00078   crc32udf= new plugin::Create_function<Crc32Function>("crc32");
00079   context.add(crc32udf);
00080   return 0;
00081 }
00082 
00083 DRIZZLE_PLUGIN(initialize, NULL, NULL);