1
/* Copyright (C) 2009 Sun Microsystems
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>
25
using namespace drizzled;
27
static bool enabled= false;
28
static bool debug_enabled= false;
29
static char* user = (char*)"";
30
static char* password = (char*)"";
31
static char* db = NULL;
34
class ClientConsole: public plugin::Client
47
virtual void printDebug(const char *message)
50
cout << "CONSOLE: " << message << endl;
53
virtual int getFileDescriptor(void)
55
printDebug("getFileDescriptor");
59
virtual bool isConnected(void)
61
printDebug("isConnected");
65
virtual bool isReading(void)
67
printDebug("isReading");
71
virtual bool isWriting(void)
73
printDebug("isWriting");
77
virtual bool flush(void)
83
virtual void close(void)
89
virtual bool authenticate(void)
91
printDebug("authenticate");
92
session->getSecurityContext().setUser(user);
93
return session->checkUser(password, strlen(password), db);
96
virtual bool readCommand(char **packet, uint32_t *packet_length)
103
cout << "drizzled> ";
108
/* Start with 1 byte offset so we can set command. */
113
*packet= (char *)realloc(*packet, length);
118
cin.getline(*packet + *packet_length, length - *packet_length, ';');
119
*packet_length+= cin.gcount();
122
while (cin.eof() == false && cin.fail() == true);
124
if ((*packet_length == 1 && cin.eof() == true) ||
125
!strncasecmp(*packet + 1, "quit", 4) ||
126
!strncasecmp(*packet + 1, "exit", 4))
130
(*packet)[0]= COM_SHUTDOWN;
134
/* Skip \r and \n for next time. */
137
(*packet)[0]= COM_QUERY;
141
virtual void sendOK(void)
143
cout << "OK" << endl;
146
virtual void sendEOF(void)
148
printDebug("sendEOF");
151
virtual void sendError(uint32_t sql_errno, const char *err)
153
cout << "Error: " << sql_errno << " " << err << endl;
156
virtual bool sendFields(List<Item> *list)
158
List_iterator_fast<Item> it(*list);
167
item->make_field(&field);
168
cout << field.col_name << "\t";
177
virtual void checkRowEnd(void)
179
if (++column % max_column == 0)
185
virtual bool store(Field *from)
190
char buff[MAX_FIELD_WIDTH];
191
String str(buff, sizeof(buff), &my_charset_bin);
193
return store(str.ptr(), str.length());
196
virtual bool store(void)
198
cout << "NULL" << "\t";
203
virtual bool store(int32_t from)
205
cout << from << "\t";
210
virtual bool store(uint32_t from)
212
cout << from << "\t";
217
virtual bool store(int64_t from)
219
cout << from << "\t";
224
virtual bool store(uint64_t from)
226
cout << from << "\t";
231
virtual bool store(double from, uint32_t decimals, String *buffer)
233
buffer->set_real(from, decimals, &my_charset_bin);
234
return store(buffer->ptr(), buffer->length());
237
virtual bool store(const char *from, size_t length)
239
cout.write(from, length);
245
virtual bool haveMoreData(void)
247
printDebug("haveMoreData");
251
virtual bool haveError(void)
253
printDebug("haveError");
257
virtual bool wasAborted(void)
259
printDebug("wasAborted");
264
class ListenConsole: public plugin::Listen
269
ListenConsole(std::string name_arg)
270
: plugin::Listen(name_arg)
275
virtual ~ListenConsole()
277
if (pipe_fds[0] != -1)
284
virtual bool getFileDescriptors(std::vector<int> &fds)
289
if (enabled == false)
292
if (pipe(pipe_fds) == -1)
294
errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
298
fds.push_back(pipe_fds[0]);
299
assert(write(pipe_fds[1], "\0", 1) == 1);
303
virtual drizzled::plugin::Client *getClient(int fd)
306
assert(read(fd, buffer, 1) == 1);
307
return new ClientConsole;
311
static ListenConsole *listen_obj= NULL;
313
static int init(drizzled::plugin::Context &context)
315
listen_obj= new ListenConsole("console");
316
context.add(listen_obj);
320
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
321
N_("Enable the console."), NULL, NULL, false);
323
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
324
N_("Turn on extra debugging."), NULL, NULL, false);
325
static DRIZZLE_SYSVAR_STR(user, user, PLUGIN_VAR_READONLY,
326
N_("User to use for auth."), NULL, NULL, NULL);
327
static DRIZZLE_SYSVAR_STR(password, password, PLUGIN_VAR_READONLY,
328
N_("Password to use for auth."), NULL, NULL, NULL);
329
static DRIZZLE_SYSVAR_STR(db, db, PLUGIN_VAR_READONLY,
330
N_("Default database to use."), NULL, NULL, NULL);
332
static drizzle_sys_var* vars[]= {
333
DRIZZLE_SYSVAR(enable),
334
DRIZZLE_SYSVAR(debug),
335
DRIZZLE_SYSVAR(user),
336
DRIZZLE_SYSVAR(password),
341
DRIZZLE_DECLARE_PLUGIN
349
init, /* Plugin Init */
350
vars, /* system variables */
351
NULL /* config options */
353
DRIZZLE_DECLARE_PLUGIN_END;