~drizzle-trunk/drizzle/development

1351.2.1 by Stewart Smith
move USER() function into a plugin (loaded by default). Also, remove the special case (and completely untested) CURRENT_USER() function and just have it also point to USER(). If somebody wants CURRENT_USER() style behaviour they can add it back in with a test.
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
23
#include <drizzled/plugin/function.h>
24
#include <drizzled/session.h>
25
26
using namespace std;
27
using namespace drizzled;
28
29
#include <drizzled/function/str/strfunc.h>
30
31
class UserFunction :public Item_str_func
32
{
33
protected:
34
  bool init (const char *user, const char *host);
35
36
public:
37
  UserFunction()
38
  {
39
    str_value.set("", 0, system_charset_info);
40
  }
41
  String *val_str(String *)
42
  {
43
    assert(fixed == 1);
44
    return (null_value ? 0 : &str_value);
45
  }
46
  bool fix_fields(Session *session, Item **ref);
47
  void fix_length_and_dec()
48
  {
49
    max_length= (USERNAME_CHAR_LENGTH + HOSTNAME_LENGTH + 1) *
50
                system_charset_info->mbmaxlen;
51
  }
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,
55
                    bool )
56
  {
57
    return save_str_value_in_field(field, &str_value);
58
  }
59
};
60
61
/**
62
  @todo
63
  make USER() replicate properly (currently it is replicated to "")
64
*/
65
bool UserFunction::init(const char *user, const char *host)
66
{
67
  assert(fixed == 1);
68
69
  // For system threads (e.g. replication SQL thread) user may be empty
70
  if (user)
71
  {
72
    const CHARSET_INFO * const cs= str_value.charset();
73
    uint32_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
74
75
    if (str_value.alloc(res_length))
76
    {
77
      null_value=1;
78
      return true;
79
    }
80
81
    res_length=cs->cset->snprintf(cs, (char*)str_value.ptr(), res_length,
82
                                  "%s@%s", user, host);
83
    str_value.length(res_length);
84
    str_value.mark_as_const();
85
  }
86
  return false;
87
}
88
89
90
bool UserFunction::fix_fields(Session *session, Item **ref)
91
{
92
  return (Item_str_func::fix_fields(session, ref) ||
93
          init(session->getSecurityContext().getUser().c_str(),
94
               session->getSecurityContext().getIp().c_str()));
95
}
96
97
98
plugin::Create_function<UserFunction> *user_function= NULL;
99
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
100
static int initialize(drizzled::module::Context &context)
1351.2.1 by Stewart Smith
move USER() function into a plugin (loaded by default). Also, remove the special case (and completely untested) CURRENT_USER() function and just have it also point to USER(). If somebody wants CURRENT_USER() style behaviour they can add it back in with a test.
101
{
102
  user_function= new plugin::Create_function<UserFunction>("user");
1324.2.20 by Monty Taylor
Updated Stewart's new plugins.
103
  context.add(user_function);
1351.2.1 by Stewart Smith
move USER() function into a plugin (loaded by default). Also, remove the special case (and completely untested) CURRENT_USER() function and just have it also point to USER(). If somebody wants CURRENT_USER() style behaviour they can add it back in with a test.
104
  return 0;
105
}
106
107
DRIZZLE_DECLARE_PLUGIN
108
{
109
  DRIZZLE_VERSION_ID,
110
  "user_function",
111
  "1.0",
112
  "Stewart Smith",
113
  "USER() and CURRENT_USER()",
114
  PLUGIN_LICENSE_GPL,
115
  initialize, /* Plugin Init */
116
  NULL,   /* system variables */
117
  NULL    /* config options */
118
}
119
DRIZZLE_DECLARE_PLUGIN_END;