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 */
16
#include <drizzled/server_includes.h>
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 enable= false;
27
static bool debug= 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)
99
if (fgets(buffer, 8192, stdin) == NULL ||!strcasecmp(buffer, "quit\n") ||
100
!strcasecmp(buffer, "exit\n"))
104
*packet= (char *)malloc(*packet_length);
105
(*packet)[0]= COM_SHUTDOWN;
109
*packet_length= strlen(buffer);
110
*packet= (char *)malloc(*packet_length);
111
(*packet)[0]= COM_QUERY;
112
memcpy(*packet + 1, buffer, *packet_length - 1);
117
virtual void sendOK(void)
119
cout << "OK" << endl;
122
virtual void sendEOF(void)
124
printDebug("sendEOF");
127
virtual void sendError(uint32_t sql_errno, const char *err)
129
cout << "Error: " << sql_errno << " " << err << endl;
132
virtual bool sendFields(List<Item> *list)
134
List_iterator_fast<Item> it(*list);
143
item->make_field(&field);
144
cout << field.col_name << "\t";
153
virtual void checkRowEnd(void)
155
if (++column % max_column == 0)
161
virtual bool store(Field *from)
166
char buff[MAX_FIELD_WIDTH];
167
String str(buff, sizeof(buff), &my_charset_bin);
169
return store(str.ptr(), str.length());
172
virtual bool store(void)
174
cout << "NULL" << "\t";
179
virtual bool store(int32_t from)
181
cout << from << "\t";
186
virtual bool store(uint32_t from)
188
cout << from << "\t";
193
virtual bool store(int64_t from)
195
cout << from << "\t";
200
virtual bool store(uint64_t from)
202
cout << from << "\t";
207
virtual bool store(double from, uint32_t decimals, String *buffer)
209
buffer->set_real(from, decimals, &my_charset_bin);
210
return store(buffer->ptr(), buffer->length());
213
virtual bool store(const DRIZZLE_TIME *tm)
219
switch (tm->time_type)
221
case DRIZZLE_TIMESTAMP_DATETIME:
222
length= sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d",
230
length+= sprintf(buff+length, ".%06d", (int)tm->second_part);
233
case DRIZZLE_TIMESTAMP_DATE:
234
length= sprintf(buff, "%04d-%02d-%02d",
240
case DRIZZLE_TIMESTAMP_TIME:
241
day= (tm->year || tm->month) ? 0 : tm->day;
242
length= sprintf(buff, "%s%02ld:%02d:%02d", tm->neg ? "-" : "",
243
(long) day*24L+(long) tm->hour, (int) tm->minute,
246
length+= sprintf(buff+length, ".%06d", (int)tm->second_part);
249
case DRIZZLE_TIMESTAMP_NONE:
250
case DRIZZLE_TIMESTAMP_ERROR:
259
virtual bool store(const char *from, size_t length)
261
printf("%.*s\t", (uint32_t)length, from);
266
virtual bool haveMoreData(void)
268
printDebug("haveMoreData");
272
virtual bool haveError(void)
274
printDebug("haveError");
278
virtual bool wasAborted(void)
280
printDebug("wasAborted");
285
class ListenConsole: public plugin::Listen
295
virtual ~ListenConsole()
297
if (pipe_fds[0] != -1)
304
virtual bool getFileDescriptors(std::vector<int> &fds)
312
if (pipe(pipe_fds) == -1)
314
errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
318
fds.push_back(pipe_fds[0]);
319
assert(write(pipe_fds[1], "\0", 1) == 1);
323
virtual drizzled::plugin::Client *getClient(int fd)
326
assert(read(fd, buffer, 1) == 1);
327
return new ClientConsole;
331
static ListenConsole listen_obj;
333
static int init(drizzled::plugin::Registry ®istry)
335
registry.listen.add(listen_obj);
339
static int deinit(drizzled::plugin::Registry ®istry)
341
registry.listen.remove(listen_obj);
345
static DRIZZLE_SYSVAR_BOOL(enable, enable, PLUGIN_VAR_NOCMDARG,
346
N_("Enable the console."), NULL, NULL, false);
348
static DRIZZLE_SYSVAR_BOOL(debug, debug, PLUGIN_VAR_NOCMDARG,
349
N_("Turn on extra debugging."), NULL, NULL, false);
351
static struct st_mysql_sys_var* vars[]= {
352
DRIZZLE_SYSVAR(enable),
353
DRIZZLE_SYSVAR(debug),
357
drizzle_declare_plugin(console)
364
init, /* Plugin Init */
365
deinit, /* Plugin Deinit */
366
NULL, /* status variables */
367
vars, /* system variables */
368
NULL /* config options */
370
drizzle_declare_plugin_end;