~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
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
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
23
#include <drizzled/plugin/catalog.h>
24
971.6.9 by Eric Day
Added console plugin.
25
#include <iostream>
26
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
27
#include <boost/program_options.hpp>
28
971.6.9 by Eric Day
Added console plugin.
29
using namespace std;
30
using namespace drizzled;
31
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
32
namespace po= boost::program_options;
33
1857.3.3 by Monty Taylor
It works - has a valgrind issue somewhere.
34
static bool enabled= false;
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
35
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.
36
971.6.9 by Eric Day
Added console plugin.
37
38
class ClientConsole: public plugin::Client
39
{
40
  bool is_dead;
41
  uint32_t column;
42
  uint32_t max_column;
1857.4.1 by Monty Taylor
Added string sys_var type.
43
  const std::string &username;
44
  const std::string &password;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
45
  const std::string &schema;
46
  const std::string &_catalog;
971.6.9 by Eric Day
Added console plugin.
47
48
public:
1857.4.1 by Monty Taylor
Added string sys_var type.
49
  ClientConsole(const std::string &username_arg,
50
                const std::string &password_arg,
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
51
                const std::string &schema_arg,
52
                const std::string &catalog_arg) :
971.6.9 by Eric Day
Added console plugin.
53
    is_dead(false),
54
    column(0),
1857.4.1 by Monty Taylor
Added string sys_var type.
55
    max_column(0),
56
    username(username_arg),
57
    password(password_arg),
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
58
    schema(schema_arg),
59
    _catalog(catalog_arg)
971.6.9 by Eric Day
Added console plugin.
60
  {}
61
62
  virtual void printDebug(const char *message)
63
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
64
    if (debug_enabled)
971.6.9 by Eric Day
Added console plugin.
65
      cout << "CONSOLE: " << message << endl;
66
  }
67
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
68
  catalog::Instance::shared_ptr catalog()
69
  {
70
    identifier::Catalog identifier(_catalog);
71
    catalog::Instance::shared_ptr tmp= plugin::Catalog::getInstance(identifier);
72
    if (not tmp)
73
    {
74
      std::cerr << "Invalid catalog '" << identifier << "', resorting to 'local' catalog" << std::endl;
75
    }
76
    return tmp;
77
  }
78
971.6.9 by Eric Day
Added console plugin.
79
  virtual int getFileDescriptor(void)
80
  {
81
    printDebug("getFileDescriptor");
82
    return 0;
83
  }
84
85
  virtual bool isConnected(void)
86
  {
87
    printDebug("isConnected");
88
    return true;
89
  }
90
91
  virtual bool isReading(void)
92
  {
93
    printDebug("isReading");
94
    return false;
95
  }
96
97
  virtual bool isWriting(void)
98
  {
99
    printDebug("isWriting");
100
    return false;
101
  }
102
103
  virtual bool flush(void)
104
  {
105
    printDebug("flush");
106
    return false;
107
  }
108
109
  virtual void close(void)
110
  {
111
    printDebug("close");
112
    is_dead= true;
113
  }
114
115
  virtual bool authenticate(void)
116
  {
117
    printDebug("authenticate");
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
118
    identifier::User::shared_ptr user= identifier::User::make_shared();
119
    user->setUser(username);
120
    session->setUser(user);
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
121
122
    return session->checkUser(password, schema);
971.6.9 by Eric Day
Added console plugin.
123
  }
124
125
  virtual bool readCommand(char **packet, uint32_t *packet_length)
126
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
127
    uint32_t length;
971.6.9 by Eric Day
Added console plugin.
128
129
    if (is_dead)
130
      return false;
131
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
132
    cout << "drizzled> ";
133
134
    length= 1024;
135
    *packet= NULL;
136
137
    /* Start with 1 byte offset so we can set command. */
138
    *packet_length= 1;
139
140
    do
141
    {
142
      *packet= (char *)realloc(*packet, length);
143
      if (*packet == NULL)
144
        return false;
145
146
      cin.clear();
147
      cin.getline(*packet + *packet_length, length - *packet_length, ';');
148
      *packet_length+= cin.gcount();
149
      length*= 2;
150
    }
151
    while (cin.eof() == false && cin.fail() == true);
152
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
153
    if ((*packet_length == 1 && cin.eof() == true) or
154
        not strncasecmp(*packet + 1, "quit", 4) or
155
        not strncasecmp(*packet + 1, "exit", 4) or
156
        not strncasecmp(*packet + 1, "shutdown", sizeof("shutdown") -1))
971.6.9 by Eric Day
Added console plugin.
157
    {
158
      is_dead= true;
159
      *packet_length= 1;
160
      (*packet)[0]= COM_SHUTDOWN;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
161
971.6.9 by Eric Day
Added console plugin.
162
      return true;
163
    }
164
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
165
    /* Skip \r and \n for next time. */
166
    cin.ignore(2, '\n');
167
971.6.9 by Eric Day
Added console plugin.
168
    (*packet)[0]= COM_QUERY;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
169
971.6.9 by Eric Day
Added console plugin.
170
    return true;
171
  }
172
173
  virtual void sendOK(void)
174
  {
175
    cout << "OK" << endl;
176
  }
177
178
  virtual void sendEOF(void)
179
  {
180
    printDebug("sendEOF");
181
  }
182
2126.3.4 by Brian Aker
Additional error cleanup (passing error correctly to the client code).
183
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
971.6.9 by Eric Day
Added console plugin.
184
  {
2126.3.4 by Brian Aker
Additional error cleanup (passing error correctly to the client code).
185
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
971.6.9 by Eric Day
Added console plugin.
186
  }
187
188
  virtual bool sendFields(List<Item> *list)
189
  {
2183.2.9 by Olaf van der Spek
x
190
    List<Item>::iterator it(list->begin());
971.6.9 by Eric Day
Added console plugin.
191
    Item *item;
192
193
    column= 0;
194
    max_column= 0;
195
196
    while ((item=it++))
197
    {
198
      SendField field;
199
      item->make_field(&field);
200
      cout << field.col_name << "\t";
201
      max_column++;
202
    }
203
204
    cout << endl;
205
206
    return false;
207
  }
208
209
  virtual void checkRowEnd(void)
210
  {
211
    if (++column % max_column == 0)
212
      cout << endl;
213
  }
214
215
  using Client::store;
216
217
  virtual bool store(Field *from)
218
  {
219
    if (from->is_null())
220
      return store();
221
222
    char buff[MAX_FIELD_WIDTH];
223
    String str(buff, sizeof(buff), &my_charset_bin);
1996.2.1 by Brian Aker
uuid type code.
224
    from->val_str_internal(&str);
971.6.9 by Eric Day
Added console plugin.
225
    return store(str.ptr(), str.length());
226
  }
227
228
  virtual bool store(void)
229
  {
230
    cout << "NULL" << "\t";
231
    checkRowEnd();
232
    return false;
233
  }
234
235
  virtual bool store(int32_t from)
236
  {
237
    cout << from << "\t";
238
    checkRowEnd();
239
    return false;
240
  }
241
242
  virtual bool store(uint32_t from)
243
  {
244
    cout << from << "\t";
245
    checkRowEnd();
246
    return false;
247
  }
248
249
  virtual bool store(int64_t from)
250
  {
251
    cout << from << "\t";
252
    checkRowEnd();
253
    return false;
254
  }
255
256
  virtual bool store(uint64_t from)
257
  {
258
    cout << from << "\t";
259
    checkRowEnd();
260
    return false;
261
  }
262
263
  virtual bool store(double from, uint32_t decimals, String *buffer)
264
  {
265
    buffer->set_real(from, decimals, &my_charset_bin);
266
    return store(buffer->ptr(), buffer->length());
267
  }
268
269
  virtual bool store(const char *from, size_t length)
270
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
271
    cout.write(from, length);
272
    cout << "\t";
971.6.9 by Eric Day
Added console plugin.
273
    checkRowEnd();
274
    return false;
275
  }
276
277
  virtual bool haveMoreData(void)
278
  {
279
    printDebug("haveMoreData");
280
    return false;
281
  }
282
283
  virtual bool haveError(void)
284
  {
285
    printDebug("haveError");
286
    return false;
287
  }
288
289
  virtual bool wasAborted(void)
290
  {
291
    printDebug("wasAborted");
292
    return false;
293
  }
2098.4.1 by Brian Aker
Make session encapsulated.
294
2191.1.4 by Brian Aker
Add in interactive mode back to protocol.
295
  bool isConsole() const
2098.4.1 by Brian Aker
Make session encapsulated.
296
  {
297
    return true;
298
  }
2191.1.2 by Brian Aker
Adding SESSION table and adding back in the mysql interactive flag.
299
2191.1.4 by Brian Aker
Add in interactive mode back to protocol.
300
  bool isInteractive() const
2191.1.2 by Brian Aker
Adding SESSION table and adding back in the mysql interactive flag.
301
  {
302
    return true;
303
  }
971.6.9 by Eric Day
Added console plugin.
304
};
305
306
class ListenConsole: public plugin::Listen
307
{
308
  int pipe_fds[2];
1857.4.1 by Monty Taylor
Added string sys_var type.
309
  const std::string username;
310
  const std::string password;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
311
  const std::string schema;
312
  const std::string _catalog;
971.6.9 by Eric Day
Added console plugin.
313
314
public:
1857.4.1 by Monty Taylor
Added string sys_var type.
315
  ListenConsole(const std::string &name_arg,
316
                const std::string &username_arg,
317
                const std::string &password_arg,
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
318
                const std::string &schema_arg,
319
                const std::string &catalog_arg) :
1857.4.1 by Monty Taylor
Added string sys_var type.
320
    plugin::Listen(name_arg),
321
    username(username_arg),
322
    password(password_arg),
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
323
    schema(schema_arg),
324
    _catalog(catalog_arg)
971.6.9 by Eric Day
Added console plugin.
325
  {
326
    pipe_fds[0]= -1;
327
  }
328
329
  virtual ~ListenConsole()
330
  {
331
    if (pipe_fds[0] != -1)
332
    {
333
      close(pipe_fds[0]);
334
      close(pipe_fds[1]);
335
    }
336
  }
337
338
  virtual bool getFileDescriptors(std::vector<int> &fds)
339
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
340
    if (debug_enabled)
341
      enabled= true;
971.6.9 by Eric Day
Added console plugin.
342
1857.3.3 by Monty Taylor
It works - has a valgrind issue somewhere.
343
    if (not enabled)
344
      return false;
345
971.6.9 by Eric Day
Added console plugin.
346
    if (pipe(pipe_fds) == -1)
347
    {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
348
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
971.6.9 by Eric Day
Added console plugin.
349
      return true;
350
    }
351
352
    fds.push_back(pipe_fds[0]);
353
    assert(write(pipe_fds[1], "\0", 1) == 1);
354
    return false;
355
  }
356
357
  virtual drizzled::plugin::Client *getClient(int fd)
358
  {
359
    char buffer[1];
360
    assert(read(fd, buffer, 1) == 1);
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
361
362
    return new ClientConsole(username, password, schema, _catalog);
971.6.9 by Eric Day
Added console plugin.
363
  }
364
};
365
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
366
static int init(drizzled::module::Context &context)
971.6.9 by Eric Day
Added console plugin.
367
{
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
368
  const module::option_map &vm= context.getOptions();
1857.4.1 by Monty Taylor
Added string sys_var type.
369
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
370
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
371
  const string schema(vm.count("schema") ? vm["schema"].as<string>() : "");
372
373
  const std::string catalog(vm.count("catalog") ? vm["catalog"].as<string>() : "LOCAL");
374
375
  context.add(new ListenConsole("console", username, password, schema, catalog));
376
971.6.9 by Eric Day
Added console plugin.
377
  return 0;
378
}
379
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
380
static void init_options(drizzled::module::option_context &context)
381
{
382
  context("enable",
383
          po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
384
          N_("Enable the console."));
385
  context("debug",
386
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
387
          N_("Turn on extra debugging."));
1626.2.2 by Monty Taylor
Moved ownership of the variable_map to the module (avoids copying, also
388
  context("username",
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
389
          po::value<string>(),
390
          N_("User to use for auth."));
391
  context("password",
392
          po::value<string>(),
393
          N_("Password to use for auth."));
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
394
  context("catalog",
395
          po::value<string>(),
396
          N_("Default catalog to use."));
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
397
  context("schema",
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
398
          po::value<string>(),
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
399
          N_("Default schema to use."));
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
400
}
401
1228.1.5 by Monty Taylor
Merged in some naming things.
402
DRIZZLE_DECLARE_PLUGIN
971.6.9 by Eric Day
Added console plugin.
403
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
404
  DRIZZLE_VERSION_ID,
971.6.9 by Eric Day
Added console plugin.
405
  "console",
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
406
  "0.2",
971.6.9 by Eric Day
Added console plugin.
407
  "Eric Day",
408
  "Console Client",
409
  PLUGIN_LICENSE_BSD,
410
  init,   /* Plugin Init */
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
411
  NULL,   /* depends */
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
412
  init_options    /* config options */
971.6.9 by Eric Day
Added console plugin.
413
}
1228.1.5 by Monty Taylor
Merged in some naming things.
414
DRIZZLE_DECLARE_PLUGIN_END;