~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Brian Aker
  • Date: 2009-11-18 22:58:22 UTC
  • mto: (1223.1.1 push) (1226.1.2 push)
  • mto: This revision was merged to the branch mainline in revision 1224.
  • Revision ID: brian@gaz-20091118225822-4ryr9rviir23o0kr
Second pass through bugs related to CREATE TABLE LIKE

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2009 Sun Microsystems, Inc.
 
1
/* Copyright (C) 2009 Sun Microsystems
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
#include "config.h"
 
16
#include <drizzled/server_includes.h>
17
17
#include <drizzled/gettext.h>
18
18
#include <drizzled/plugin/listen_tcp.h>
19
19
#include <drizzled/plugin/client.h>
20
 
#include <drizzled/session.h>
21
 
#include <drizzled/module/option_map.h>
22
 
 
23
 
#include <drizzled/plugin/catalog.h>
24
20
 
25
21
#include <iostream>
26
22
 
27
 
#include <boost/program_options.hpp>
28
 
 
29
23
using namespace std;
30
24
using namespace drizzled;
31
25
 
32
 
namespace po= boost::program_options;
33
 
 
34
26
static bool enabled= false;
35
27
static bool debug_enabled= false;
36
28
 
37
 
 
38
29
class ClientConsole: public plugin::Client
39
30
{
40
31
  bool is_dead;
41
32
  uint32_t column;
42
33
  uint32_t max_column;
43
 
  const std::string &username;
44
 
  const std::string &password;
45
 
  const std::string &schema;
46
 
  const std::string &_catalog;
47
34
 
48
35
public:
49
 
  ClientConsole(const std::string &username_arg,
50
 
                const std::string &password_arg,
51
 
                const std::string &schema_arg,
52
 
                const std::string &catalog_arg) :
 
36
  ClientConsole():
53
37
    is_dead(false),
54
38
    column(0),
55
 
    max_column(0),
56
 
    username(username_arg),
57
 
    password(password_arg),
58
 
    schema(schema_arg),
59
 
    _catalog(catalog_arg)
 
39
    max_column(0)
60
40
  {}
61
41
 
62
42
  virtual void printDebug(const char *message)
65
45
      cout << "CONSOLE: " << message << endl;
66
46
  }
67
47
 
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
 
 
79
48
  virtual int getFileDescriptor(void)
80
49
  {
81
50
    printDebug("getFileDescriptor");
115
84
  virtual bool authenticate(void)
116
85
  {
117
86
    printDebug("authenticate");
118
 
    identifier::User::shared_ptr user= identifier::User::make_shared();
119
 
    user->setUser(username);
120
 
    session->setUser(user);
121
 
 
122
 
    return session->checkUser(password, schema);
 
87
    return true;
123
88
  }
124
89
 
125
90
  virtual bool readCommand(char **packet, uint32_t *packet_length)
150
115
    }
151
116
    while (cin.eof() == false && cin.fail() == true);
152
117
 
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))
 
118
    if ((*packet_length == 1 && cin.eof() == true) ||
 
119
        !strncasecmp(*packet + 1, "quit", 4) ||
 
120
        !strncasecmp(*packet + 1, "exit", 4))
157
121
    {
158
122
      is_dead= true;
159
123
      *packet_length= 1;
160
124
      (*packet)[0]= COM_SHUTDOWN;
161
 
 
162
125
      return true;
163
126
    }
164
127
 
166
129
    cin.ignore(2, '\n');
167
130
 
168
131
    (*packet)[0]= COM_QUERY;
169
 
 
170
132
    return true;
171
133
  }
172
134
 
180
142
    printDebug("sendEOF");
181
143
  }
182
144
 
183
 
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
 
145
  virtual void sendError(uint32_t sql_errno, const char *err)
184
146
  {
185
 
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
 
147
    cout << "Error: " << sql_errno << " " << err << endl;
186
148
  }
187
149
 
188
150
  virtual bool sendFields(List<Item> *list)
221
183
 
222
184
    char buff[MAX_FIELD_WIDTH];
223
185
    String str(buff, sizeof(buff), &my_charset_bin);
224
 
    from->val_str_internal(&str);
 
186
    from->val_str(&str);
225
187
    return store(str.ptr(), str.length());
226
188
  }
227
189
 
291
253
    printDebug("wasAborted");
292
254
    return false;
293
255
  }
294
 
 
295
 
  bool isConsole()
296
 
  {
297
 
    return true;
298
 
  }
299
256
};
300
257
 
301
258
class ListenConsole: public plugin::Listen
302
259
{
303
260
  int pipe_fds[2];
304
 
  const std::string username;
305
 
  const std::string password;
306
 
  const std::string schema;
307
 
  const std::string _catalog;
308
261
 
309
262
public:
310
 
  ListenConsole(const std::string &name_arg,
311
 
                const std::string &username_arg,
312
 
                const std::string &password_arg,
313
 
                const std::string &schema_arg,
314
 
                const std::string &catalog_arg) :
315
 
    plugin::Listen(name_arg),
316
 
    username(username_arg),
317
 
    password(password_arg),
318
 
    schema(schema_arg),
319
 
    _catalog(catalog_arg)
 
263
  ListenConsole(std::string name_arg)
 
264
    : plugin::Listen(name_arg)
320
265
  {
321
266
    pipe_fds[0]= -1;
322
267
  }
335
280
    if (debug_enabled)
336
281
      enabled= true;
337
282
 
338
 
    if (not enabled)
 
283
    if (enabled == false)
339
284
      return false;
340
285
 
341
286
    if (pipe(pipe_fds) == -1)
342
287
    {
343
 
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
 
288
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
344
289
      return true;
345
290
    }
346
291
 
353
298
  {
354
299
    char buffer[1];
355
300
    assert(read(fd, buffer, 1) == 1);
356
 
 
357
 
    return new ClientConsole(username, password, schema, _catalog);
 
301
    return new ClientConsole;
358
302
  }
359
303
};
360
304
 
361
 
static int init(drizzled::module::Context &context)
362
 
{
363
 
  const module::option_map &vm= context.getOptions();
364
 
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
365
 
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
366
 
  const string schema(vm.count("schema") ? vm["schema"].as<string>() : "");
367
 
 
368
 
  const std::string catalog(vm.count("catalog") ? vm["catalog"].as<string>() : "LOCAL");
369
 
 
370
 
  context.add(new ListenConsole("console", username, password, schema, catalog));
371
 
 
372
 
  return 0;
373
 
}
374
 
 
375
 
static void init_options(drizzled::module::option_context &context)
376
 
{
377
 
  context("enable",
378
 
          po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
379
 
          N_("Enable the console."));
380
 
  context("debug",
381
 
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
382
 
          N_("Turn on extra debugging."));
383
 
  context("username",
384
 
          po::value<string>(),
385
 
          N_("User to use for auth."));
386
 
  context("password",
387
 
          po::value<string>(),
388
 
          N_("Password to use for auth."));
389
 
  context("catalog",
390
 
          po::value<string>(),
391
 
          N_("Default catalog to use."));
392
 
  context("schema",
393
 
          po::value<string>(),
394
 
          N_("Default schema to use."));
395
 
}
396
 
 
397
 
DRIZZLE_DECLARE_PLUGIN
398
 
{
399
 
  DRIZZLE_VERSION_ID,
 
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
 
334
{
400
335
  "console",
401
 
  "0.2",
 
336
  "0.1",
402
337
  "Eric Day",
403
338
  "Console Client",
404
339
  PLUGIN_LICENSE_BSD,
405
340
  init,   /* Plugin Init */
406
 
  NULL,   /* depends */
407
 
  init_options    /* config options */
 
341
  deinit, /* Plugin Deinit */
 
342
  NULL,   /* status variables */
 
343
  vars,   /* system variables */
 
344
  NULL    /* config options */
408
345
}
409
 
DRIZZLE_DECLARE_PLUGIN_END;
 
346
drizzle_declare_plugin_end;