~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Brian Aker
  • Date: 2011-01-25 07:22:15 UTC
  • mfrom: (2109.1.7 drizzle-build)
  • Revision ID: brian@tangent.org-20110125072215-567z6uzy5vdvn4va
Merge in build/timestamp patches/fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2009 Sun Microsystems
 
1
/* Copyright (C) 2009 Sun Microsystems, Inc.
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
20
20
#include <drizzled/session.h>
21
21
#include <drizzled/module/option_map.h>
22
22
 
 
23
#include <drizzled/plugin/catalog.h>
 
24
 
23
25
#include <iostream>
24
26
 
25
27
#include <boost/program_options.hpp>
40
42
  uint32_t max_column;
41
43
  const std::string &username;
42
44
  const std::string &password;
43
 
  const std::string &db;
 
45
  const std::string &schema;
 
46
  const std::string &_catalog;
44
47
 
45
48
public:
46
49
  ClientConsole(const std::string &username_arg,
47
50
                const std::string &password_arg,
48
 
                const std::string &db_arg) :
 
51
                const std::string &schema_arg,
 
52
                const std::string &catalog_arg) :
49
53
    is_dead(false),
50
54
    column(0),
51
55
    max_column(0),
52
56
    username(username_arg),
53
57
    password(password_arg),
54
 
    db(db_arg)
 
58
    schema(schema_arg),
 
59
    _catalog(catalog_arg)
55
60
  {}
56
61
 
57
62
  virtual void printDebug(const char *message)
60
65
      cout << "CONSOLE: " << message << endl;
61
66
  }
62
67
 
 
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
 
63
79
  virtual int getFileDescriptor(void)
64
80
  {
65
81
    printDebug("getFileDescriptor");
102
118
    identifier::User::shared_ptr user= identifier::User::make_shared();
103
119
    user->setUser(username);
104
120
    session->setUser(user);
105
 
    return session->checkUser(password, db);
 
121
 
 
122
    return session->checkUser(password, schema);
106
123
  }
107
124
 
108
125
  virtual bool readCommand(char **packet, uint32_t *packet_length)
133
150
    }
134
151
    while (cin.eof() == false && cin.fail() == true);
135
152
 
136
 
    if ((*packet_length == 1 && cin.eof() == true) ||
137
 
        !strncasecmp(*packet + 1, "quit", 4) ||
138
 
        !strncasecmp(*packet + 1, "exit", 4))
 
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))
139
157
    {
140
158
      is_dead= true;
141
159
      *packet_length= 1;
142
160
      (*packet)[0]= COM_SHUTDOWN;
 
161
 
143
162
      return true;
144
163
    }
145
164
 
147
166
    cin.ignore(2, '\n');
148
167
 
149
168
    (*packet)[0]= COM_QUERY;
 
169
 
150
170
    return true;
151
171
  }
152
172
 
271
291
    printDebug("wasAborted");
272
292
    return false;
273
293
  }
 
294
 
 
295
  bool isConsole()
 
296
  {
 
297
    return true;
 
298
  }
274
299
};
275
300
 
276
301
class ListenConsole: public plugin::Listen
278
303
  int pipe_fds[2];
279
304
  const std::string username;
280
305
  const std::string password;
281
 
  const std::string db;
 
306
  const std::string schema;
 
307
  const std::string _catalog;
282
308
 
283
309
public:
284
310
  ListenConsole(const std::string &name_arg,
285
311
                const std::string &username_arg,
286
312
                const std::string &password_arg,
287
 
                const std::string &db_arg) :
 
313
                const std::string &schema_arg,
 
314
                const std::string &catalog_arg) :
288
315
    plugin::Listen(name_arg),
289
316
    username(username_arg),
290
317
    password(password_arg),
291
 
    db(db_arg)
 
318
    schema(schema_arg),
 
319
    _catalog(catalog_arg)
292
320
  {
293
321
    pipe_fds[0]= -1;
294
322
  }
325
353
  {
326
354
    char buffer[1];
327
355
    assert(read(fd, buffer, 1) == 1);
328
 
    return new ClientConsole(username, password, db);
 
356
 
 
357
    return new ClientConsole(username, password, schema, _catalog);
329
358
  }
330
359
};
331
360
 
334
363
  const module::option_map &vm= context.getOptions();
335
364
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
336
365
  const string password(vm.count("password") ? vm["password"].as<string>() : "");
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));
 
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
 
344
372
  return 0;
345
373
}
346
374
 
358
386
  context("password",
359
387
          po::value<string>(),
360
388
          N_("Password to use for auth."));
361
 
  context("db",
362
 
          po::value<string>(),
363
 
          N_("Default database to use."));
 
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."));
364
395
}
365
396
 
366
397
DRIZZLE_DECLARE_PLUGIN
367
398
{
368
399
  DRIZZLE_VERSION_ID,
369
400
  "console",
370
 
  "0.1",
 
401
  "0.2",
371
402
  "Eric Day",
372
403
  "Console Client",
373
404
  PLUGIN_LICENSE_BSD,