~drizzle-trunk/drizzle/development

1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
1
/* Copyright (C) 2009 Sun Microsystems, Inc.
971.6.9 by Eric Day
Added console plugin.
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
16
#include "config.h"
971.6.9 by Eric Day
Added console plugin.
17
#include <drizzled/gettext.h>
18
#include <drizzled/plugin/listen_tcp.h>
19
#include <drizzled/plugin/client.h>
1305.1.2 by Eric Day
Added command line options to console to pass username, password, and db name during authentication step.
20
#include <drizzled/session.h>
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
21
#include <drizzled/module/option_map.h>
971.6.9 by Eric Day
Added console plugin.
22
23
#include <iostream>
24
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
25
#include <boost/program_options.hpp>
26
971.6.9 by Eric Day
Added console plugin.
27
using namespace std;
28
using namespace drizzled;
29
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
30
namespace po= boost::program_options;
31
1857.3.3 by Monty Taylor
It works - has a valgrind issue somewhere.
32
static bool enabled= false;
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
33
static bool debug_enabled= false;
1305.1.2 by Eric Day
Added command line options to console to pass username, password, and db name during authentication step.
34
971.6.9 by Eric Day
Added console plugin.
35
36
class ClientConsole: public plugin::Client
37
{
38
  bool is_dead;
39
  uint32_t column;
40
  uint32_t max_column;
1857.4.1 by Monty Taylor
Added string sys_var type.
41
  const std::string &username;
42
  const std::string &password;
43
  const std::string &db;
971.6.9 by Eric Day
Added console plugin.
44
45
public:
1857.4.1 by Monty Taylor
Added string sys_var type.
46
  ClientConsole(const std::string &username_arg,
47
                const std::string &password_arg,
48
                const std::string &db_arg) :
971.6.9 by Eric Day
Added console plugin.
49
    is_dead(false),
50
    column(0),
1857.4.1 by Monty Taylor
Added string sys_var type.
51
    max_column(0),
52
    username(username_arg),
53
    password(password_arg),
54
    db(db_arg)
971.6.9 by Eric Day
Added console plugin.
55
  {}
56
57
  virtual void printDebug(const char *message)
58
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
59
    if (debug_enabled)
971.6.9 by Eric Day
Added console plugin.
60
      cout << "CONSOLE: " << message << endl;
61
  }
62
63
  virtual int getFileDescriptor(void)
64
  {
65
    printDebug("getFileDescriptor");
66
    return 0;
67
  }
68
69
  virtual bool isConnected(void)
70
  {
71
    printDebug("isConnected");
72
    return true;
73
  }
74
75
  virtual bool isReading(void)
76
  {
77
    printDebug("isReading");
78
    return false;
79
  }
80
81
  virtual bool isWriting(void)
82
  {
83
    printDebug("isWriting");
84
    return false;
85
  }
86
87
  virtual bool flush(void)
88
  {
89
    printDebug("flush");
90
    return false;
91
  }
92
93
  virtual void close(void)
94
  {
95
    printDebug("close");
96
    is_dead= true;
97
  }
98
99
  virtual bool authenticate(void)
100
  {
101
    printDebug("authenticate");
1626.2.2 by Monty Taylor
Moved ownership of the variable_map to the module (avoids copying, also
102
    session->getSecurityContext().setUser(username);
1857.4.1 by Monty Taylor
Added string sys_var type.
103
    return session->checkUser(password, db);
971.6.9 by Eric Day
Added console plugin.
104
  }
105
106
  virtual bool readCommand(char **packet, uint32_t *packet_length)
107
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
108
    uint32_t length;
971.6.9 by Eric Day
Added console plugin.
109
110
    if (is_dead)
111
      return false;
112
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
113
    cout << "drizzled> ";
114
115
    length= 1024;
116
    *packet= NULL;
117
118
    /* Start with 1 byte offset so we can set command. */
119
    *packet_length= 1;
120
121
    do
122
    {
123
      *packet= (char *)realloc(*packet, length);
124
      if (*packet == NULL)
125
        return false;
126
127
      cin.clear();
128
      cin.getline(*packet + *packet_length, length - *packet_length, ';');
129
      *packet_length+= cin.gcount();
130
      length*= 2;
131
    }
132
    while (cin.eof() == false && cin.fail() == true);
133
134
    if ((*packet_length == 1 && cin.eof() == true) ||
135
        !strncasecmp(*packet + 1, "quit", 4) ||
136
        !strncasecmp(*packet + 1, "exit", 4))
971.6.9 by Eric Day
Added console plugin.
137
    {
138
      is_dead= true;
139
      *packet_length= 1;
140
      (*packet)[0]= COM_SHUTDOWN;
141
      return true;
142
    }
143
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
144
    /* Skip \r and \n for next time. */
145
    cin.ignore(2, '\n');
146
971.6.9 by Eric Day
Added console plugin.
147
    (*packet)[0]= COM_QUERY;
148
    return true;
149
  }
150
151
  virtual void sendOK(void)
152
  {
153
    cout << "OK" << endl;
154
  }
155
156
  virtual void sendEOF(void)
157
  {
158
    printDebug("sendEOF");
159
  }
160
161
  virtual void sendError(uint32_t sql_errno, const char *err)
162
  {
163
    cout << "Error: " << sql_errno << " " << err << endl;
164
  }
165
166
  virtual bool sendFields(List<Item> *list)
167
  {
168
    List_iterator_fast<Item> it(*list);
169
    Item *item;
170
171
    column= 0;
172
    max_column= 0;
173
174
    while ((item=it++))
175
    {
176
      SendField field;
177
      item->make_field(&field);
178
      cout << field.col_name << "\t";
179
      max_column++;
180
    }
181
182
    cout << endl;
183
184
    return false;
185
  }
186
187
  virtual void checkRowEnd(void)
188
  {
189
    if (++column % max_column == 0)
190
      cout << endl;
191
  }
192
193
  using Client::store;
194
195
  virtual bool store(Field *from)
196
  {
197
    if (from->is_null())
198
      return store();
199
200
    char buff[MAX_FIELD_WIDTH];
201
    String str(buff, sizeof(buff), &my_charset_bin);
1996.2.1 by Brian Aker
uuid type code.
202
    from->val_str_internal(&str);
971.6.9 by Eric Day
Added console plugin.
203
    return store(str.ptr(), str.length());
204
  }
205
206
  virtual bool store(void)
207
  {
208
    cout << "NULL" << "\t";
209
    checkRowEnd();
210
    return false;
211
  }
212
213
  virtual bool store(int32_t from)
214
  {
215
    cout << from << "\t";
216
    checkRowEnd();
217
    return false;
218
  }
219
220
  virtual bool store(uint32_t from)
221
  {
222
    cout << from << "\t";
223
    checkRowEnd();
224
    return false;
225
  }
226
227
  virtual bool store(int64_t from)
228
  {
229
    cout << from << "\t";
230
    checkRowEnd();
231
    return false;
232
  }
233
234
  virtual bool store(uint64_t from)
235
  {
236
    cout << from << "\t";
237
    checkRowEnd();
238
    return false;
239
  }
240
241
  virtual bool store(double from, uint32_t decimals, String *buffer)
242
  {
243
    buffer->set_real(from, decimals, &my_charset_bin);
244
    return store(buffer->ptr(), buffer->length());
245
  }
246
247
  virtual bool store(const char *from, size_t length)
248
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
249
    cout.write(from, length);
250
    cout << "\t";
971.6.9 by Eric Day
Added console plugin.
251
    checkRowEnd();
252
    return false;
253
  }
254
255
  virtual bool haveMoreData(void)
256
  {
257
    printDebug("haveMoreData");
258
    return false;
259
  }
260
261
  virtual bool haveError(void)
262
  {
263
    printDebug("haveError");
264
    return false;
265
  }
266
267
  virtual bool wasAborted(void)
268
  {
269
    printDebug("wasAborted");
270
    return false;
271
  }
272
};
273
274
class ListenConsole: public plugin::Listen
275
{
276
  int pipe_fds[2];
1857.4.1 by Monty Taylor
Added string sys_var type.
277
  const std::string username;
278
  const std::string password;
279
  const std::string db;
971.6.9 by Eric Day
Added console plugin.
280
281
public:
1857.4.1 by Monty Taylor
Added string sys_var type.
282
  ListenConsole(const std::string &name_arg,
283
                const std::string &username_arg,
284
                const std::string &password_arg,
285
                const std::string &db_arg) :
286
    plugin::Listen(name_arg),
287
    username(username_arg),
288
    password(password_arg),
289
    db(db_arg)
971.6.9 by Eric Day
Added console plugin.
290
  {
291
    pipe_fds[0]= -1;
292
  }
293
294
  virtual ~ListenConsole()
295
  {
296
    if (pipe_fds[0] != -1)
297
    {
298
      close(pipe_fds[0]);
299
      close(pipe_fds[1]);
300
    }
301
  }
302
303
  virtual bool getFileDescriptors(std::vector<int> &fds)
304
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
305
    if (debug_enabled)
306
      enabled= true;
971.6.9 by Eric Day
Added console plugin.
307
1857.3.3 by Monty Taylor
It works - has a valgrind issue somewhere.
308
    if (not enabled)
309
      return false;
310
971.6.9 by Eric Day
Added console plugin.
311
    if (pipe(pipe_fds) == -1)
312
    {
313
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
314
      return true;
315
    }
316
317
    fds.push_back(pipe_fds[0]);
318
    assert(write(pipe_fds[1], "\0", 1) == 1);
319
    return false;
320
  }
321
322
  virtual drizzled::plugin::Client *getClient(int fd)
323
  {
324
    char buffer[1];
325
    assert(read(fd, buffer, 1) == 1);
1857.4.1 by Monty Taylor
Added string sys_var type.
326
    return new ClientConsole(username, password, db);
971.6.9 by Eric Day
Added console plugin.
327
  }
328
};
329
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
330
static int init(drizzled::module::Context &context)
971.6.9 by Eric Day
Added console plugin.
331
{
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
332
  const module::option_map &vm= context.getOptions();
1857.4.1 by Monty Taylor
Added string sys_var type.
333
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
334
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
335
  const string db(vm.count("db") ? vm["db"].as<string>() : "");
1857.3.2 by Monty Taylor
Use new sys_var register code in console plugin.
336
  context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
337
  context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
1857.4.1 by Monty Taylor
Added string sys_var type.
338
  context.registerVariable(new sys_var_const_string_val("username", username));
339
  context.registerVariable(new sys_var_const_string_val("password", password));
340
  context.registerVariable(new sys_var_const_string_val("db", db));
341
  context.add(new ListenConsole("console", username, password, db));
971.6.9 by Eric Day
Added console plugin.
342
  return 0;
343
}
344
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
345
static void init_options(drizzled::module::option_context &context)
346
{
347
  context("enable",
348
          po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
349
          N_("Enable the console."));
350
  context("debug",
351
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
352
          N_("Turn on extra debugging."));
1626.2.2 by Monty Taylor
Moved ownership of the variable_map to the module (avoids copying, also
353
  context("username",
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
354
          po::value<string>(),
355
          N_("User to use for auth."));
356
  context("password",
357
          po::value<string>(),
358
          N_("Password to use for auth."));
359
  context("db",
360
          po::value<string>(),
361
          N_("Default database to use."));
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
362
}
363
1228.1.5 by Monty Taylor
Merged in some naming things.
364
DRIZZLE_DECLARE_PLUGIN
971.6.9 by Eric Day
Added console plugin.
365
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
366
  DRIZZLE_VERSION_ID,
971.6.9 by Eric Day
Added console plugin.
367
  "console",
368
  "0.1",
369
  "Eric Day",
370
  "Console Client",
371
  PLUGIN_LICENSE_BSD,
372
  init,   /* Plugin Init */
1857.3.2 by Monty Taylor
Use new sys_var register code in console plugin.
373
  NULL,   /* system variables */
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
374
  init_options    /* config options */
971.6.9 by Eric Day
Added console plugin.
375
}
1228.1.5 by Monty Taylor
Merged in some naming things.
376
DRIZZLE_DECLARE_PLUGIN_END;