1
/* Copyright (C) 2009 Sun Microsystems
1
/* Copyright (C) 2009 Sun Microsystems, Inc.
3
3
This program is free software; you can redistribute it and/or modify
4
4
it under the terms of the GNU General Public License as published by
13
13
along with this program; if not, write to the Free Software
14
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
17
#include <drizzled/field.h>
17
18
#include <drizzled/gettext.h>
18
19
#include <drizzled/plugin/listen_tcp.h>
19
20
#include <drizzled/plugin/client.h>
20
21
#include <drizzled/session.h>
21
22
#include <drizzled/module/option_map.h>
23
#include <drizzled/plugin/catalog.h>
24
#include <drizzled/plugin.h>
23
26
#include <iostream>
40
43
uint32_t max_column;
41
44
const std::string &username;
42
45
const std::string &password;
43
const std::string &db;
46
const std::string &schema;
47
const std::string &_catalog;
46
50
ClientConsole(const std::string &username_arg,
47
51
const std::string &password_arg,
48
const std::string &db_arg) :
52
const std::string &schema_arg,
53
const std::string &catalog_arg) :
52
57
username(username_arg),
53
58
password(password_arg),
57
63
virtual void printDebug(const char *message)
60
66
cout << "CONSOLE: " << message << endl;
69
catalog::Instance::shared_ptr catalog()
71
identifier::Catalog identifier(_catalog);
72
catalog::Instance::shared_ptr tmp= plugin::Catalog::getInstance(identifier);
75
std::cerr << "Invalid catalog '" << identifier << "', resorting to 'local' catalog" << std::endl;
63
80
virtual int getFileDescriptor(void)
65
82
printDebug("getFileDescriptor");
99
116
virtual bool authenticate(void)
101
118
printDebug("authenticate");
102
session->getSecurityContext().setUser(username);
103
return session->checkUser(password, db);
119
identifier::user::mptr user= identifier::User::make_shared();
120
user->setUser(username);
121
session->setUser(user);
123
return session->checkUser(password, schema);
106
virtual bool readCommand(char **packet, uint32_t *packet_length)
126
virtual bool readCommand(char **packet, uint32_t& packet_length)
128
cin.getline(*packet + *packet_length, length - *packet_length, ';');
129
*packet_length+= cin.gcount();
148
cin.getline(*packet + packet_length, length - packet_length, ';');
149
packet_length+= cin.gcount();
132
152
while (cin.eof() == false && cin.fail() == true);
134
if ((*packet_length == 1 && cin.eof() == true) ||
135
!strncasecmp(*packet + 1, "quit", 4) ||
136
!strncasecmp(*packet + 1, "exit", 4))
154
if ((packet_length == 1 && cin.eof() == true) or
155
not strncasecmp(*packet + 1, "quit", 4) or
156
not strncasecmp(*packet + 1, "exit", 4) or
157
not strncasecmp(*packet + 1, "shutdown", sizeof("shutdown") -1))
140
161
(*packet)[0]= COM_SHUTDOWN;
158
181
printDebug("sendEOF");
161
virtual void sendError(uint32_t sql_errno, const char *err)
184
virtual void sendError(const drizzled::error_t sql_errno, const char *err)
163
cout << "Error: " << sql_errno << " " << err << endl;
186
cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
166
189
virtual bool sendFields(List<Item> *list)
168
List_iterator_fast<Item> it(*list);
191
List<Item>::iterator it(list->begin());
203
226
return store(str.ptr(), str.length());
206
virtual bool store(void)
229
virtual void store(void)
208
231
cout << "NULL" << "\t";
213
virtual bool store(int32_t from)
215
cout << from << "\t";
220
virtual bool store(uint32_t from)
222
cout << from << "\t";
227
virtual bool store(int64_t from)
229
cout << from << "\t";
234
virtual bool store(uint64_t from)
236
cout << from << "\t";
241
virtual bool store(double from, uint32_t decimals, String *buffer)
235
virtual void store(int32_t from)
237
cout << from << "\t";
241
virtual void store(uint32_t from)
243
cout << from << "\t";
247
virtual void store(int64_t from)
249
cout << from << "\t";
253
virtual void store(uint64_t from)
255
cout << from << "\t";
259
virtual void store(double from, uint32_t decimals, String *buffer)
243
261
buffer->set_real(from, decimals, &my_charset_bin);
244
return store(buffer->ptr(), buffer->length());
262
store(buffer->ptr(), buffer->length());
247
virtual bool store(const char *from, size_t length)
265
virtual void store(const char *from, size_t length)
249
267
cout.write(from, length);
255
virtual bool haveMoreData(void)
272
virtual bool haveMoreData()
257
274
printDebug("haveMoreData");
261
virtual bool haveError(void)
278
virtual bool haveError()
263
280
printDebug("haveError");
267
virtual bool wasAborted(void)
284
virtual bool wasAborted()
269
286
printDebug("wasAborted");
290
bool isConsole() const
295
bool isInteractive() const
274
301
class ListenConsole: public plugin::Listen
277
304
const std::string username;
278
305
const std::string password;
279
const std::string db;
306
const std::string schema;
307
const std::string _catalog;
282
310
ListenConsole(const std::string &name_arg,
283
311
const std::string &username_arg,
284
312
const std::string &password_arg,
285
const std::string &db_arg) :
313
const std::string &schema_arg,
314
const std::string &catalog_arg) :
286
315
plugin::Listen(name_arg),
287
316
username(username_arg),
288
317
password(password_arg),
319
_catalog(catalog_arg)
311
341
if (pipe(pipe_fds) == -1)
313
errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
343
errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
325
355
assert(read(fd, buffer, 1) == 1);
326
return new ClientConsole(username, password, db);
357
return new ClientConsole(username, password, schema, _catalog);
330
361
static int init(drizzled::module::Context &context)
332
363
const module::option_map &vm= context.getOptions();
333
const string username(vm.count("username") ? vm["username"].as<string>() : "");
334
const string password(vm.count("password") ? vm["password"].as<string>() : "");
335
const string db(vm.count("db") ? vm["db"].as<string>() : "");
336
context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
337
context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
338
context.registerVariable(new sys_var_const_string_val("username", username));
339
context.registerVariable(new sys_var_const_string_val("password", password));
340
context.registerVariable(new sys_var_const_string_val("db", db));
341
context.add(new ListenConsole("console", username, password, db));
364
context.add(new ListenConsole("console", vm["username"].as<string>(),
365
vm["password"].as<string>(), vm["schema"].as<string>(), vm["catalog"].as<string>()));
351
375
po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
352
376
N_("Turn on extra debugging."));
353
377
context("username",
378
po::value<string>()->default_value(""),
355
379
N_("User to use for auth."));
356
380
context("password",
381
po::value<string>()->default_value(""),
358
382
N_("Password to use for auth."));
361
N_("Default database to use."));
384
po::value<string>()->default_value("LOCAL"),
385
N_("Default catalog to use."));
387
po::value<string>()->default_value(""),
388
N_("Default schema to use."));
364
391
DRIZZLE_DECLARE_PLUGIN
366
393
DRIZZLE_VERSION_ID,
370
397
"Console Client",
371
398
PLUGIN_LICENSE_BSD,
372
399
init, /* Plugin Init */
373
NULL, /* system variables */
374
401
init_options /* config options */
376
403
DRIZZLE_DECLARE_PLUGIN_END;