Drizzled Public API Documentation

uuid_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) 2008 Sun Microsystems, Inc.
00005  *  Copyright (C) 2010 Stewart Smith
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 #include <config.h>
00022 
00023 #include <drizzled/charset_info.h>
00024 #include <drizzled/function/str/strfunc.h>
00025 #include <drizzled/item/func.h>
00026 #include <drizzled/plugin/function.h>
00027 
00028 #include <uuid/uuid.h>
00029 
00030 #define UUID_LENGTH (8+1+4+1+4+1+4+1+12)
00031 
00032 namespace plugin {
00033 namespace uuid {
00034 
00035 class Generate: public drizzled::Item_str_func
00036 {
00037 public:
00038   Generate(): drizzled::Item_str_func() {}
00039   void fix_length_and_dec()
00040   {
00041     collation.set(drizzled::system_charset_info);
00042     /*
00043        NOTE! uuid() should be changed to use 'ascii'
00044        charset when hex(), format(), md5(), etc, and implicit
00045        number-to-string conversion will use 'ascii'
00046     */
00047     max_length= UUID_LENGTH * drizzled::system_charset_info->mbmaxlen;
00048   }
00049   const char *func_name() const{ return "uuid"; }
00050   drizzled::String *val_str(drizzled::String *);
00051 };
00052 
00053 drizzled::String *Generate::val_str(drizzled::String *str)
00054 {
00055   uuid_t uu;
00056   char *uuid_string;
00057 
00058   /* 36 characters for uuid string +1 for NULL */
00059   str->realloc(UUID_LENGTH+1);
00060   str->length(UUID_LENGTH);
00061   str->set_charset(drizzled::system_charset_info);
00062   uuid_string= (char *) str->ptr();
00063   uuid_generate(uu);
00064   uuid_unparse(uu, uuid_string);
00065 
00066   return str;
00067 }
00068 
00069 } // uuid
00070 } // plugin
00071 
00072 static int initialize(drizzled::module::Context &context)
00073 {
00074   context.add(new drizzled::plugin::Create_function<plugin::uuid::Generate>("uuid"));
00075 
00076   return 0;
00077 }
00078 
00079 DRIZZLE_DECLARE_PLUGIN
00080 {
00081   DRIZZLE_VERSION_ID,
00082   "uuid",
00083   "1.1",
00084   "Stewart Smith, Brian Aker",
00085   "UUID() function using libuuid",
00086   drizzled::PLUGIN_LICENSE_GPL,
00087   initialize, /* Plugin Init */
00088   NULL,   /* depends */
00089   NULL    /* config options */
00090 }
00091 DRIZZLE_DECLARE_PLUGIN_END;