~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/user_function/user_function.cc

  • Committer: Stewart Smith
  • Date: 2010-03-18 11:20:28 UTC
  • mto: (1666.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 1366.
  • Revision ID: stewart@flamingspork.com-20100318112028-p0vemjw0c2c03ycd
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.

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
 
 
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
 
 
100
static int initialize(drizzled::plugin::Registry &registry)
 
101
{
 
102
  user_function= new plugin::Create_function<UserFunction>("user");
 
103
  registry.add(user_function);
 
104
  return 0;
 
105
}
 
106
 
 
107
static int finalize(drizzled::plugin::Registry &registry)
 
108
{
 
109
   registry.remove(user_function);
 
110
   delete user_function;
 
111
   return 0;
 
112
}
 
113
 
 
114
DRIZZLE_DECLARE_PLUGIN
 
115
{
 
116
  DRIZZLE_VERSION_ID,
 
117
  "user_function",
 
118
  "1.0",
 
119
  "Stewart Smith",
 
120
  "USER() and CURRENT_USER()",
 
121
  PLUGIN_LICENSE_GPL,
 
122
  initialize, /* Plugin Init */
 
123
  finalize,   /* Plugin Deinit */
 
124
  NULL,   /* system variables */
 
125
  NULL    /* config options */
 
126
}
 
127
DRIZZLE_DECLARE_PLUGIN_END;