~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

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