1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
5
* Copyright (C) 2010 Stewart Smith
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.
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.
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
23
#include <drizzled/plugin/function.h>
24
#include <drizzled/session.h>
27
using namespace drizzled;
29
#include <drizzled/function/str/strfunc.h>
31
class UserFunction :public Item_str_func
34
bool init (const char *user, const char *host);
39
str_value.set("", 0, system_charset_info);
41
String *val_str(String *)
44
return (null_value ? 0 : &str_value);
46
bool fix_fields(Session *session, Item **ref);
47
void fix_length_and_dec()
49
max_length= (USERNAME_CHAR_LENGTH + HOSTNAME_LENGTH + 1) *
50
system_charset_info->mbmaxlen;
52
const char *func_name() const { return "user"; }
53
const char *fully_qualified_func_name() const { return "user()"; }
54
int save_in_field(Field *field,
57
return save_str_value_in_field(field, &str_value);
63
make USER() replicate properly (currently it is replicated to "")
65
bool UserFunction::init(const char *user, const char *host)
69
// For system threads (e.g. replication SQL thread) user may be empty
72
const CHARSET_INFO * const cs= str_value.charset();
73
uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
75
if (str_value.alloc(res_length))
81
res_length=cs->cset->snprintf(cs, (char*)str_value.ptr(), res_length,
83
str_value.length(res_length);
84
str_value.mark_as_const();
90
bool UserFunction::fix_fields(Session *session, Item **ref)
92
return (Item_str_func::fix_fields(session, ref) ||
93
init(session->getSecurityContext().getUser().c_str(),
94
session->getSecurityContext().getIp().c_str()));
98
plugin::Create_function<UserFunction> *user_function= NULL;
100
static int initialize(drizzled::plugin::Registry ®istry)
102
user_function= new plugin::Create_function<UserFunction>("user");
103
registry.add(user_function);
107
static int finalize(drizzled::plugin::Registry ®istry)
109
registry.remove(user_function);
110
delete user_function;
114
DRIZZLE_DECLARE_PLUGIN
120
"USER() and CURRENT_USER()",
122
initialize, /* Plugin Init */
123
finalize, /* Plugin Deinit */
124
NULL, /* system variables */
125
NULL /* config options */
127
DRIZZLE_DECLARE_PLUGIN_END;