~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Lee Bieber
  • Date: 2011-01-24 17:20:08 UTC
  • mfrom: (2107.2.1 trunk-bug-703913)
  • mto: This revision was merged to the branch mainline in revision 2109.
  • Revision ID: kalebral@gmail.com-20110124172008-y5maihyoyu7gn18p
Merge Andrew - fix bug 703913: some support-files should be dropped

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "config.h"
17
17
#include <drizzled/gettext.h>
18
18
#include <drizzled/plugin/listen_tcp.h>
19
19
#include <drizzled/plugin/client.h>
20
20
#include <drizzled/session.h>
21
21
#include <drizzled/module/option_map.h>
22
22
 
23
 
#include <drizzled/plugin/catalog.h>
24
 
 
25
23
#include <iostream>
26
24
 
27
25
#include <boost/program_options.hpp>
42
40
  uint32_t max_column;
43
41
  const std::string &username;
44
42
  const std::string &password;
45
 
  const std::string &schema;
46
 
  const std::string &_catalog;
 
43
  const std::string &db;
47
44
 
48
45
public:
49
46
  ClientConsole(const std::string &username_arg,
50
47
                const std::string &password_arg,
51
 
                const std::string &schema_arg,
52
 
                const std::string &catalog_arg) :
 
48
                const std::string &db_arg) :
53
49
    is_dead(false),
54
50
    column(0),
55
51
    max_column(0),
56
52
    username(username_arg),
57
53
    password(password_arg),
58
 
    schema(schema_arg),
59
 
    _catalog(catalog_arg)
 
54
    db(db_arg)
60
55
  {}
61
56
 
62
57
  virtual void printDebug(const char *message)
65
60
      cout << "CONSOLE: " << message << endl;
66
61
  }
67
62
 
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
63
  virtual int getFileDescriptor(void)
80
64
  {
81
65
    printDebug("getFileDescriptor");
118
102
    identifier::User::shared_ptr user= identifier::User::make_shared();
119
103
    user->setUser(username);
120
104
    session->setUser(user);
121
 
 
122
 
    return session->checkUser(password, schema);
 
105
    return session->checkUser(password, db);
123
106
  }
124
107
 
125
108
  virtual bool readCommand(char **packet, uint32_t *packet_length)
150
133
    }
151
134
    while (cin.eof() == false && cin.fail() == true);
152
135
 
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))
 
136
    if ((*packet_length == 1 && cin.eof() == true) ||
 
137
        !strncasecmp(*packet + 1, "quit", 4) ||
 
138
        !strncasecmp(*packet + 1, "exit", 4))
157
139
    {
158
140
      is_dead= true;
159
141
      *packet_length= 1;
160
142
      (*packet)[0]= COM_SHUTDOWN;
161
 
 
162
143
      return true;
163
144
    }
164
145
 
166
147
    cin.ignore(2, '\n');
167
148
 
168
149
    (*packet)[0]= COM_QUERY;
169
 
 
170
150
    return true;
171
151
  }
172
152
 
180
160
    printDebug("sendEOF");
181
161
  }
182
162
 
183
 
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
 
163
  virtual void sendError(uint32_t sql_errno, const char *err)
184
164
  {
185
 
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
 
165
    cout << "Error: " << sql_errno << " " << err << endl;
186
166
  }
187
167
 
188
168
  virtual bool sendFields(List<Item> *list)
189
169
  {
190
 
    List<Item>::iterator it(list->begin());
 
170
    List_iterator_fast<Item> it(*list);
191
171
    Item *item;
192
172
 
193
173
    column= 0;
303
283
  int pipe_fds[2];
304
284
  const std::string username;
305
285
  const std::string password;
306
 
  const std::string schema;
307
 
  const std::string _catalog;
 
286
  const std::string db;
308
287
 
309
288
public:
310
289
  ListenConsole(const std::string &name_arg,
311
290
                const std::string &username_arg,
312
291
                const std::string &password_arg,
313
 
                const std::string &schema_arg,
314
 
                const std::string &catalog_arg) :
 
292
                const std::string &db_arg) :
315
293
    plugin::Listen(name_arg),
316
294
    username(username_arg),
317
295
    password(password_arg),
318
 
    schema(schema_arg),
319
 
    _catalog(catalog_arg)
 
296
    db(db_arg)
320
297
  {
321
298
    pipe_fds[0]= -1;
322
299
  }
340
317
 
341
318
    if (pipe(pipe_fds) == -1)
342
319
    {
343
 
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
 
320
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
344
321
      return true;
345
322
    }
346
323
 
353
330
  {
354
331
    char buffer[1];
355
332
    assert(read(fd, buffer, 1) == 1);
356
 
 
357
 
    return new ClientConsole(username, password, schema, _catalog);
 
333
    return new ClientConsole(username, password, db);
358
334
  }
359
335
};
360
336
 
363
339
  const module::option_map &vm= context.getOptions();
364
340
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
365
341
  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
 
 
 
342
  const string db(vm.count("db") ? vm["db"].as<string>() : "");
 
343
  context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
 
344
  context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
 
345
  context.registerVariable(new sys_var_const_string_val("username", username));
 
346
  context.registerVariable(new sys_var_const_string_val("password", password));
 
347
  context.registerVariable(new sys_var_const_string_val("db", db));
 
348
  context.add(new ListenConsole("console", username, password, db));
372
349
  return 0;
373
350
}
374
351
 
386
363
  context("password",
387
364
          po::value<string>(),
388
365
          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."));
 
366
  context("db",
 
367
          po::value<string>(),
 
368
          N_("Default database to use."));
395
369
}
396
370
 
397
371
DRIZZLE_DECLARE_PLUGIN
398
372
{
399
373
  DRIZZLE_VERSION_ID,
400
374
  "console",
401
 
  "0.2",
 
375
  "0.1",
402
376
  "Eric Day",
403
377
  "Console Client",
404
378
  PLUGIN_LICENSE_BSD,
405
379
  init,   /* Plugin Init */
406
 
  NULL,   /* depends */
 
380
  NULL,   /* system variables */
407
381
  init_options    /* config options */
408
382
}
409
383
DRIZZLE_DECLARE_PLUGIN_END;