~drizzle-pbxt/drizzle/drizzle-pbxt-2

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Paul McCullagh
  • Date: 2009-11-10 14:18:39 UTC
  • mfrom: (1038.1.7 drizzle-pbxt-pre-merge)
  • Revision ID: paul.mccullagh@primebase.org-20091110141839-2j3k43b17ag6f605
Merged Drizzle trunk and PBXT 1.0.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2009 Sun Microsystems
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
#include <drizzled/server_includes.h>
 
17
#include <drizzled/gettext.h>
 
18
#include <drizzled/plugin/listen_tcp.h>
 
19
#include <drizzled/plugin/client.h>
 
20
 
 
21
#include <iostream>
 
22
 
 
23
using namespace std;
 
24
using namespace drizzled;
 
25
 
 
26
static bool enabled= false;
 
27
static bool debug_enabled= false;
 
28
 
 
29
class ClientConsole: public plugin::Client
 
30
{
 
31
  bool is_dead;
 
32
  uint32_t column;
 
33
  uint32_t max_column;
 
34
 
 
35
public:
 
36
  ClientConsole():
 
37
    is_dead(false),
 
38
    column(0),
 
39
    max_column(0)
 
40
  {}
 
41
 
 
42
  virtual void printDebug(const char *message)
 
43
  {
 
44
    if (debug_enabled)
 
45
      cout << "CONSOLE: " << message << endl;
 
46
  }
 
47
 
 
48
  virtual int getFileDescriptor(void)
 
49
  {
 
50
    printDebug("getFileDescriptor");
 
51
    return 0;
 
52
  }
 
53
 
 
54
  virtual bool isConnected(void)
 
55
  {
 
56
    printDebug("isConnected");
 
57
    return true;
 
58
  }
 
59
 
 
60
  virtual bool isReading(void)
 
61
  {
 
62
    printDebug("isReading");
 
63
    return false;
 
64
  }
 
65
 
 
66
  virtual bool isWriting(void)
 
67
  {
 
68
    printDebug("isWriting");
 
69
    return false;
 
70
  }
 
71
 
 
72
  virtual bool flush(void)
 
73
  {
 
74
    printDebug("flush");
 
75
    return false;
 
76
  }
 
77
 
 
78
  virtual void close(void)
 
79
  {
 
80
    printDebug("close");
 
81
    is_dead= true;
 
82
  }
 
83
 
 
84
  virtual bool authenticate(void)
 
85
  {
 
86
    printDebug("authenticate");
 
87
    return true;
 
88
  }
 
89
 
 
90
  virtual bool readCommand(char **packet, uint32_t *packet_length)
 
91
  {
 
92
    uint32_t length;
 
93
 
 
94
    if (is_dead)
 
95
      return false;
 
96
 
 
97
    cout << "drizzled> ";
 
98
 
 
99
    length= 1024;
 
100
    *packet= NULL;
 
101
 
 
102
    /* Start with 1 byte offset so we can set command. */
 
103
    *packet_length= 1;
 
104
 
 
105
    do
 
106
    {
 
107
      *packet= (char *)realloc(*packet, length);
 
108
      if (*packet == NULL)
 
109
        return false;
 
110
 
 
111
      cin.clear();
 
112
      cin.getline(*packet + *packet_length, length - *packet_length, ';');
 
113
      *packet_length+= cin.gcount();
 
114
      length*= 2;
 
115
    }
 
116
    while (cin.eof() == false && cin.fail() == true);
 
117
 
 
118
    if ((*packet_length == 1 && cin.eof() == true) ||
 
119
        !strncasecmp(*packet + 1, "quit", 4) ||
 
120
        !strncasecmp(*packet + 1, "exit", 4))
 
121
    {
 
122
      is_dead= true;
 
123
      *packet_length= 1;
 
124
      (*packet)[0]= COM_SHUTDOWN;
 
125
      return true;
 
126
    }
 
127
 
 
128
    /* Skip \r and \n for next time. */
 
129
    cin.ignore(2, '\n');
 
130
 
 
131
    (*packet)[0]= COM_QUERY;
 
132
    return true;
 
133
  }
 
134
 
 
135
  virtual void sendOK(void)
 
136
  {
 
137
    cout << "OK" << endl;
 
138
  }
 
139
 
 
140
  virtual void sendEOF(void)
 
141
  {
 
142
    printDebug("sendEOF");
 
143
  }
 
144
 
 
145
  virtual void sendError(uint32_t sql_errno, const char *err)
 
146
  {
 
147
    cout << "Error: " << sql_errno << " " << err << endl;
 
148
  }
 
149
 
 
150
  virtual bool sendFields(List<Item> *list)
 
151
  {
 
152
    List_iterator_fast<Item> it(*list);
 
153
    Item *item;
 
154
 
 
155
    column= 0;
 
156
    max_column= 0;
 
157
 
 
158
    while ((item=it++))
 
159
    {
 
160
      SendField field;
 
161
      item->make_field(&field);
 
162
      cout << field.col_name << "\t";
 
163
      max_column++;
 
164
    }
 
165
 
 
166
    cout << endl;
 
167
 
 
168
    return false;
 
169
  }
 
170
 
 
171
  virtual void checkRowEnd(void)
 
172
  {
 
173
    if (++column % max_column == 0)
 
174
      cout << endl;
 
175
  }
 
176
 
 
177
  using Client::store;
 
178
 
 
179
  virtual bool store(Field *from)
 
180
  {
 
181
    if (from->is_null())
 
182
      return store();
 
183
 
 
184
    char buff[MAX_FIELD_WIDTH];
 
185
    String str(buff, sizeof(buff), &my_charset_bin);
 
186
    from->val_str(&str);
 
187
    return store(str.ptr(), str.length());
 
188
  }
 
189
 
 
190
  virtual bool store(void)
 
191
  {
 
192
    cout << "NULL" << "\t";
 
193
    checkRowEnd();
 
194
    return false;
 
195
  }
 
196
 
 
197
  virtual bool store(int32_t from)
 
198
  {
 
199
    cout << from << "\t";
 
200
    checkRowEnd();
 
201
    return false;
 
202
  }
 
203
 
 
204
  virtual bool store(uint32_t from)
 
205
  {
 
206
    cout << from << "\t";
 
207
    checkRowEnd();
 
208
    return false;
 
209
  }
 
210
 
 
211
  virtual bool store(int64_t from)
 
212
  {
 
213
    cout << from << "\t";
 
214
    checkRowEnd();
 
215
    return false;
 
216
  }
 
217
 
 
218
  virtual bool store(uint64_t from)
 
219
  {
 
220
    cout << from << "\t";
 
221
    checkRowEnd();
 
222
    return false;
 
223
  }
 
224
 
 
225
  virtual bool store(double from, uint32_t decimals, String *buffer)
 
226
  {
 
227
    buffer->set_real(from, decimals, &my_charset_bin);
 
228
    return store(buffer->ptr(), buffer->length());
 
229
  }
 
230
 
 
231
  virtual bool store(const char *from, size_t length)
 
232
  {
 
233
    cout.write(from, length);
 
234
    cout << "\t";
 
235
    checkRowEnd();
 
236
    return false;
 
237
  }
 
238
 
 
239
  virtual bool haveMoreData(void)
 
240
  {
 
241
    printDebug("haveMoreData");
 
242
    return false;
 
243
  }
 
244
 
 
245
  virtual bool haveError(void)
 
246
  {
 
247
    printDebug("haveError");
 
248
    return false;
 
249
  }
 
250
 
 
251
  virtual bool wasAborted(void)
 
252
  {
 
253
    printDebug("wasAborted");
 
254
    return false;
 
255
  }
 
256
};
 
257
 
 
258
class ListenConsole: public plugin::Listen
 
259
{
 
260
  int pipe_fds[2];
 
261
 
 
262
public:
 
263
  ListenConsole(std::string name_arg)
 
264
    : plugin::Listen(name_arg)
 
265
  {
 
266
    pipe_fds[0]= -1;
 
267
  }
 
268
 
 
269
  virtual ~ListenConsole()
 
270
  {
 
271
    if (pipe_fds[0] != -1)
 
272
    {
 
273
      close(pipe_fds[0]);
 
274
      close(pipe_fds[1]);
 
275
    }
 
276
  }
 
277
 
 
278
  virtual bool getFileDescriptors(std::vector<int> &fds)
 
279
  {
 
280
    if (debug_enabled)
 
281
      enabled= true;
 
282
 
 
283
    if (enabled == false)
 
284
      return false;
 
285
 
 
286
    if (pipe(pipe_fds) == -1)
 
287
    {
 
288
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
 
289
      return true;
 
290
    }
 
291
 
 
292
    fds.push_back(pipe_fds[0]);
 
293
    assert(write(pipe_fds[1], "\0", 1) == 1);
 
294
    return false;
 
295
  }
 
296
 
 
297
  virtual drizzled::plugin::Client *getClient(int fd)
 
298
  {
 
299
    char buffer[1];
 
300
    assert(read(fd, buffer, 1) == 1);
 
301
    return new ClientConsole;
 
302
  }
 
303
};
 
304
 
 
305
static ListenConsole *listen_obj= NULL;
 
306
 
 
307
static int init(drizzled::plugin::Registry &registry)
 
308
{
 
309
  listen_obj= new ListenConsole("console");
 
310
  registry.add(listen_obj);
 
311
  return 0;
 
312
}
 
313
 
 
314
static int deinit(drizzled::plugin::Registry &registry)
 
315
{
 
316
  registry.remove(listen_obj);
 
317
  delete listen_obj;
 
318
  return 0;
 
319
}
 
320
 
 
321
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
 
322
                           N_("Enable the console."), NULL, NULL, false);
 
323
 
 
324
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
 
325
                           N_("Turn on extra debugging."), NULL, NULL, false);
 
326
 
 
327
static struct st_mysql_sys_var* vars[]= {
 
328
  DRIZZLE_SYSVAR(enable),
 
329
  DRIZZLE_SYSVAR(debug),
 
330
  NULL
 
331
};
 
332
 
 
333
drizzle_declare_plugin(console)
 
334
{
 
335
  "console",
 
336
  "0.1",
 
337
  "Eric Day",
 
338
  "Console Client",
 
339
  PLUGIN_LICENSE_BSD,
 
340
  init,   /* Plugin Init */
 
341
  deinit, /* Plugin Deinit */
 
342
  NULL,   /* status variables */
 
343
  vars,   /* system variables */
 
344
  NULL    /* config options */
 
345
}
 
346
drizzle_declare_plugin_end;