~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to plugin/uuid_function/uuid_function.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-03-15 14:05:26 UTC
  • mfrom: (1237.9.99 staging)
  • Revision ID: osullivan.padraig@gmail.com-20100315140526-opbgwdwn6tfecdkq
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *  Copyright (C) 2010 Stewart Smith
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; version 2 of the License.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include <drizzled/plugin/function.h>
 
23
#include <drizzled/item/func.h>
 
24
#include <drizzled/function/str/strfunc.h>
 
25
#include <uuid/uuid.h>
 
26
 
 
27
#define UUID_LENGTH (8+1+4+1+4+1+4+1+12)
 
28
 
 
29
using namespace drizzled;
 
30
 
 
31
class UuidFunction: public Item_str_func
 
32
{
 
33
public:
 
34
  UuidFunction(): Item_str_func() {}
 
35
  void fix_length_and_dec() {
 
36
    collation.set(system_charset_info);
 
37
    /*
 
38
       NOTE! uuid() should be changed to use 'ascii'
 
39
       charset when hex(), format(), md5(), etc, and implicit
 
40
       number-to-string conversion will use 'ascii'
 
41
    */
 
42
    max_length= UUID_LENGTH * system_charset_info->mbmaxlen;
 
43
  }
 
44
  const char *func_name() const{ return "uuid"; }
 
45
  String *val_str(String *);
 
46
};
 
47
 
 
48
String *UuidFunction::val_str(String *str)
 
49
{
 
50
  uuid_t uu;
 
51
  char *uuid_string;
 
52
 
 
53
  /* 36 characters for uuid string +1 for NULL */
 
54
  str->realloc(UUID_LENGTH+1);
 
55
  str->length(UUID_LENGTH);
 
56
  str->set_charset(system_charset_info);
 
57
  uuid_string= (char *) str->ptr();
 
58
  uuid_generate_time(uu);
 
59
  uuid_unparse(uu, uuid_string);
 
60
 
 
61
  return str;
 
62
}
 
63
 
 
64
plugin::Create_function<UuidFunction> *uuid_function= NULL;
 
65
 
 
66
static int initialize(drizzled::plugin::Registry &registry)
 
67
{
 
68
  uuid_function= new plugin::Create_function<UuidFunction>("uuid");
 
69
  registry.add(uuid_function);
 
70
  return 0;
 
71
}
 
72
 
 
73
static int finalize(drizzled::plugin::Registry &registry)
 
74
{
 
75
   registry.remove(uuid_function);
 
76
   delete uuid_function;
 
77
   return 0;
 
78
}
 
79
 
 
80
DRIZZLE_DECLARE_PLUGIN
 
81
{
 
82
  DRIZZLE_VERSION_ID,
 
83
  "uuid",
 
84
  "1.0",
 
85
  "Stewart Smith",
 
86
  "UUID() function using libuuid",
 
87
  PLUGIN_LICENSE_GPL,
 
88
  initialize, /* Plugin Init */
 
89
  finalize,   /* Plugin Deinit */
 
90
  NULL,   /* system variables */
 
91
  NULL    /* config options */
 
92
}
 
93
DRIZZLE_DECLARE_PLUGIN_END;