Drizzled Public API Documentation

function.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2000 MySQL AB
00005  *  Copyright (C) 2008, 2009 Sun Microsystems, Inc.
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /* This implements 'user defined functions' */
00022 #include <config.h>
00023 
00024 #include <boost/unordered_map.hpp>
00025 
00026 #include <drizzled/gettext.h>
00027 #include <drizzled/plugin/function.h>
00028 #include <drizzled/function_container.h>
00029 #include <drizzled/util/find_ptr.h>
00030 #include <drizzled/util/string.h>
00031 
00032 namespace drizzled
00033 {
00034 
00035 static plugin::Function::UdfMap udf_registry;
00036 
00037 const plugin::Function::UdfMap &plugin::Function::getMap()
00038 {
00039   return udf_registry;
00040 }
00041 
00042 bool plugin::Function::addPlugin(const plugin::Function *udf)
00043 {
00044   if (FunctionContainer::getMap().count(udf->getName()))
00045   {
00046     errmsg_printf(error::ERROR,
00047                   _("A function named %s already exists!\n"),
00048                   udf->getName().c_str());
00049     return true;
00050   }
00051 
00052   if (udf_registry.count(udf->getName()))
00053   {
00054     errmsg_printf(error::ERROR,
00055                   _("A function named %s already exists!\n"),
00056                   udf->getName().c_str());
00057     return true;
00058   }
00059 
00060   std::pair<UdfMap::iterator, bool> ret=
00061     udf_registry.insert(make_pair(udf->getName(), udf));
00062 
00063   if (ret.second == false)
00064   {
00065     errmsg_printf(error::ERROR,
00066                   _("Could not add Function!\n"));
00067     return true;
00068   }
00069 
00070   return false;
00071 }
00072 
00073 
00074 void plugin::Function::removePlugin(const plugin::Function *udf)
00075 {
00076   udf_registry.erase(udf->getName());
00077 }
00078 
00079 const plugin::Function *plugin::Function::get(const std::string &name)
00080 {
00081   UdfMap::mapped_type* ptr= find_ptr(udf_registry, name);
00082   return ptr ? *ptr : NULL;
00083 }
00084 
00085 } /* namespace drizzled */