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>
24
using namespace drizzled;
26
static bool enabled= false;
27
static bool debug_enabled= false;
29
class ClientConsole: public plugin::Client
42
virtual void printDebug(const char *message)
45
cout << "CONSOLE: " << message << endl;
48
virtual int getFileDescriptor(void)
50
printDebug("getFileDescriptor");
54
virtual bool isConnected(void)
56
printDebug("isConnected");
60
virtual bool isReading(void)
62
printDebug("isReading");
66
virtual bool isWriting(void)
68
printDebug("isWriting");
72
virtual bool flush(void)
78
virtual void close(void)
84
virtual bool authenticate(void)
86
printDebug("authenticate");
90
virtual bool readCommand(char **packet, uint32_t *packet_length)
102
/* Start with 1 byte offset so we can set command. */
107
*packet= (char *)realloc(*packet, length);
112
cin.getline(*packet + *packet_length, length - *packet_length, ';');
113
*packet_length+= cin.gcount();
116
while (cin.eof() == false && cin.fail() == true);
118
if ((*packet_length == 1 && cin.eof() == true) ||
119
!strncasecmp(*packet + 1, "quit", 4) ||
120
!strncasecmp(*packet + 1, "exit", 4))
124
(*packet)[0]= COM_SHUTDOWN;
128
/* Skip \r and \n for next time. */
131
(*packet)[0]= COM_QUERY;
135
virtual void sendOK(void)
137
cout << "OK" << endl;
140
virtual void sendEOF(void)
142
printDebug("sendEOF");
145
virtual void sendError(uint32_t sql_errno, const char *err)
147
cout << "Error: " << sql_errno << " " << err << endl;
150
virtual bool sendFields(List<Item> *list)
152
List_iterator_fast<Item> it(*list);
161
item->make_field(&field);
162
cout << field.col_name << "\t";
171
virtual void checkRowEnd(void)
173
if (++column % max_column == 0)
179
virtual bool store(Field *from)
184
char buff[MAX_FIELD_WIDTH];
185
String str(buff, sizeof(buff), &my_charset_bin);
187
return store(str.ptr(), str.length());
190
virtual bool store(void)
192
cout << "NULL" << "\t";
197
virtual bool store(int32_t from)
199
cout << from << "\t";
204
virtual bool store(uint32_t from)
206
cout << from << "\t";
211
virtual bool store(int64_t from)
213
cout << from << "\t";
218
virtual bool store(uint64_t from)
220
cout << from << "\t";
225
virtual bool store(double from, uint32_t decimals, String *buffer)
227
buffer->set_real(from, decimals, &my_charset_bin);
228
return store(buffer->ptr(), buffer->length());
231
virtual bool store(const char *from, size_t length)
233
cout.write(from, length);
239
virtual bool haveMoreData(void)
241
printDebug("haveMoreData");
245
virtual bool haveError(void)
247
printDebug("haveError");
251
virtual bool wasAborted(void)
253
printDebug("wasAborted");
258
class ListenConsole: public plugin::Listen
263
ListenConsole(std::string name_arg)
264
: plugin::Listen(name_arg)
269
virtual ~ListenConsole()
271
if (pipe_fds[0] != -1)
278
virtual bool getFileDescriptors(std::vector<int> &fds)
283
if (enabled == false)
286
if (pipe(pipe_fds) == -1)
288
errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
292
fds.push_back(pipe_fds[0]);
293
assert(write(pipe_fds[1], "\0", 1) == 1);
297
virtual drizzled::plugin::Client *getClient(int fd)
300
assert(read(fd, buffer, 1) == 1);
301
return new ClientConsole;
305
static ListenConsole *listen_obj= NULL;
307
static int init(drizzled::plugin::Registry ®istry)
309
listen_obj= new ListenConsole("console");
310
registry.add(listen_obj);
314
static int deinit(drizzled::plugin::Registry ®istry)
316
registry.remove(listen_obj);
321
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
322
N_("Enable the console."), NULL, NULL, false);
324
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
325
N_("Turn on extra debugging."), NULL, NULL, false);
327
static drizzle_sys_var* vars[]= {
328
DRIZZLE_SYSVAR(enable),
329
DRIZZLE_SYSVAR(debug),
333
DRIZZLE_DECLARE_PLUGIN
341
init, /* Plugin Init */
342
deinit, /* Plugin Deinit */
343
NULL, /* status variables */
344
vars, /* system variables */
345
NULL /* config options */
347
DRIZZLE_DECLARE_PLUGIN_END;