1
/* Copyright (C) 2009 Sun Microsystems, Inc.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
17
#include <drizzled/gettext.h>
18
#include <drizzled/plugin/listen_tcp.h>
19
#include <drizzled/plugin/client.h>
20
#include <drizzled/session.h>
21
#include <drizzled/module/option_map.h>
25
#include <boost/program_options.hpp>
28
using namespace drizzled;
30
namespace po= boost::program_options;
32
static bool enabled= false;
33
static bool debug_enabled= false;
36
class ClientConsole: public plugin::Client
41
const std::string &username;
42
const std::string &password;
43
const std::string &db;
46
ClientConsole(const std::string &username_arg,
47
const std::string &password_arg,
48
const std::string &db_arg) :
52
username(username_arg),
53
password(password_arg),
57
virtual void printDebug(const char *message)
60
cout << "CONSOLE: " << message << endl;
63
virtual int getFileDescriptor(void)
65
printDebug("getFileDescriptor");
69
virtual bool isConnected(void)
71
printDebug("isConnected");
75
virtual bool isReading(void)
77
printDebug("isReading");
81
virtual bool isWriting(void)
83
printDebug("isWriting");
87
virtual bool flush(void)
93
virtual void close(void)
99
virtual bool authenticate(void)
101
printDebug("authenticate");
102
identifier::User::shared_ptr user= identifier::User::make_shared();
103
user->setUser(username);
104
session->setUser(user);
105
return session->checkUser(password, db);
108
virtual bool readCommand(char **packet, uint32_t *packet_length)
115
cout << "drizzled> ";
120
/* Start with 1 byte offset so we can set command. */
125
*packet= (char *)realloc(*packet, length);
130
cin.getline(*packet + *packet_length, length - *packet_length, ';');
131
*packet_length+= cin.gcount();
134
while (cin.eof() == false && cin.fail() == true);
136
if ((*packet_length == 1 && cin.eof() == true) ||
137
!strncasecmp(*packet + 1, "quit", 4) ||
138
!strncasecmp(*packet + 1, "exit", 4))
142
(*packet)[0]= COM_SHUTDOWN;
146
/* Skip \r and \n for next time. */
149
(*packet)[0]= COM_QUERY;
153
virtual void sendOK(void)
155
cout << "OK" << endl;
158
virtual void sendEOF(void)
160
printDebug("sendEOF");
163
virtual void sendError(uint32_t sql_errno, const char *err)
165
cout << "Error: " << sql_errno << " " << err << endl;
168
virtual bool sendFields(List<Item> *list)
170
List_iterator_fast<Item> it(*list);
179
item->make_field(&field);
180
cout << field.col_name << "\t";
189
virtual void checkRowEnd(void)
191
if (++column % max_column == 0)
197
virtual bool store(Field *from)
202
char buff[MAX_FIELD_WIDTH];
203
String str(buff, sizeof(buff), &my_charset_bin);
204
from->val_str_internal(&str);
205
return store(str.ptr(), str.length());
208
virtual bool store(void)
210
cout << "NULL" << "\t";
215
virtual bool store(int32_t from)
217
cout << from << "\t";
222
virtual bool store(uint32_t from)
224
cout << from << "\t";
229
virtual bool store(int64_t from)
231
cout << from << "\t";
236
virtual bool store(uint64_t from)
238
cout << from << "\t";
243
virtual bool store(double from, uint32_t decimals, String *buffer)
245
buffer->set_real(from, decimals, &my_charset_bin);
246
return store(buffer->ptr(), buffer->length());
249
virtual bool store(const char *from, size_t length)
251
cout.write(from, length);
257
virtual bool haveMoreData(void)
259
printDebug("haveMoreData");
263
virtual bool haveError(void)
265
printDebug("haveError");
269
virtual bool wasAborted(void)
271
printDebug("wasAborted");
276
class ListenConsole: public plugin::Listen
279
const std::string username;
280
const std::string password;
281
const std::string db;
284
ListenConsole(const std::string &name_arg,
285
const std::string &username_arg,
286
const std::string &password_arg,
287
const std::string &db_arg) :
288
plugin::Listen(name_arg),
289
username(username_arg),
290
password(password_arg),
296
virtual ~ListenConsole()
298
if (pipe_fds[0] != -1)
305
virtual bool getFileDescriptors(std::vector<int> &fds)
313
if (pipe(pipe_fds) == -1)
315
errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
319
fds.push_back(pipe_fds[0]);
320
assert(write(pipe_fds[1], "\0", 1) == 1);
324
virtual drizzled::plugin::Client *getClient(int fd)
327
assert(read(fd, buffer, 1) == 1);
328
return new ClientConsole(username, password, db);
332
static int init(drizzled::module::Context &context)
334
const module::option_map &vm= context.getOptions();
335
const string username(vm.count("username") ? vm["username"].as<string>() : "");
336
const string password(vm.count("password") ? vm["password"].as<string>() : "");
337
const string db(vm.count("db") ? vm["db"].as<string>() : "");
338
context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
339
context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
340
context.registerVariable(new sys_var_const_string_val("username", username));
341
context.registerVariable(new sys_var_const_string_val("password", password));
342
context.registerVariable(new sys_var_const_string_val("db", db));
343
context.add(new ListenConsole("console", username, password, db));
347
static void init_options(drizzled::module::option_context &context)
350
po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
351
N_("Enable the console."));
353
po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
354
N_("Turn on extra debugging."));
357
N_("User to use for auth."));
360
N_("Password to use for auth."));
363
N_("Default database to use."));
366
DRIZZLE_DECLARE_PLUGIN
374
init, /* Plugin Init */
375
NULL, /* system variables */
376
init_options /* config options */
378
DRIZZLE_DECLARE_PLUGIN_END;