~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Monty Taylor
  • Date: 2010-12-26 01:32:11 UTC
  • mto: This revision was merged to the branch mainline in revision 2038.
  • Revision ID: mordred@inaugust.com-20101226013211-c1tx52h7evovmijg
fixed dict and eval.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
291
271
    printDebug("wasAborted");
292
272
    return false;
293
273
  }
294
 
 
295
 
  bool isConsole()
296
 
  {
297
 
    return true;
298
 
  }
299
274
};
300
275
 
301
276
class ListenConsole: public plugin::Listen
303
278
  int pipe_fds[2];
304
279
  const std::string username;
305
280
  const std::string password;
306
 
  const std::string schema;
307
 
  const std::string _catalog;
 
281
  const std::string db;
308
282
 
309
283
public:
310
284
  ListenConsole(const std::string &name_arg,
311
285
                const std::string &username_arg,
312
286
                const std::string &password_arg,
313
 
                const std::string &schema_arg,
314
 
                const std::string &catalog_arg) :
 
287
                const std::string &db_arg) :
315
288
    plugin::Listen(name_arg),
316
289
    username(username_arg),
317
290
    password(password_arg),
318
 
    schema(schema_arg),
319
 
    _catalog(catalog_arg)
 
291
    db(db_arg)
320
292
  {
321
293
    pipe_fds[0]= -1;
322
294
  }
340
312
 
341
313
    if (pipe(pipe_fds) == -1)
342
314
    {
343
 
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
 
315
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
344
316
      return true;
345
317
    }
346
318
 
353
325
  {
354
326
    char buffer[1];
355
327
    assert(read(fd, buffer, 1) == 1);
356
 
 
357
 
    return new ClientConsole(username, password, schema, _catalog);
 
328
    return new ClientConsole(username, password, db);
358
329
  }
359
330
};
360
331
 
363
334
  const module::option_map &vm= context.getOptions();
364
335
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
365
336
  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
 
 
 
337
  const string db(vm.count("db") ? vm["db"].as<string>() : "");
 
338
  context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
 
339
  context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
 
340
  context.registerVariable(new sys_var_const_string_val("username", username));
 
341
  context.registerVariable(new sys_var_const_string_val("password", password));
 
342
  context.registerVariable(new sys_var_const_string_val("db", db));
 
343
  context.add(new ListenConsole("console", username, password, db));
372
344
  return 0;
373
345
}
374
346
 
386
358
  context("password",
387
359
          po::value<string>(),
388
360
          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."));
 
361
  context("db",
 
362
          po::value<string>(),
 
363
          N_("Default database to use."));
395
364
}
396
365
 
397
366
DRIZZLE_DECLARE_PLUGIN
398
367
{
399
368
  DRIZZLE_VERSION_ID,
400
369
  "console",
401
 
  "0.2",
 
370
  "0.1",
402
371
  "Eric Day",
403
372
  "Console Client",
404
373
  PLUGIN_LICENSE_BSD,
405
374
  init,   /* Plugin Init */
406
 
  NULL,   /* depends */
 
375
  NULL,   /* system variables */
407
376
  init_options    /* config options */
408
377
}
409
378
DRIZZLE_DECLARE_PLUGIN_END;