~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Brian Aker
  • Date: 2010-10-20 20:26:18 UTC
  • mfrom: (1859.2.13 refactor)
  • Revision ID: brian@tangent.org-20101020202618-9222n39lm329urv5
Merge for Brian 

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
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");
115
99
  virtual bool authenticate(void)
116
100
  {
117
101
    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);
 
102
    session->getSecurityContext().setUser(username);
 
103
    return session->checkUser(password, db);
123
104
  }
124
105
 
125
106
  virtual bool readCommand(char **packet, uint32_t *packet_length)
150
131
    }
151
132
    while (cin.eof() == false && cin.fail() == true);
152
133
 
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))
 
134
    if ((*packet_length == 1 && cin.eof() == true) ||
 
135
        !strncasecmp(*packet + 1, "quit", 4) ||
 
136
        !strncasecmp(*packet + 1, "exit", 4))
157
137
    {
158
138
      is_dead= true;
159
139
      *packet_length= 1;
160
140
      (*packet)[0]= COM_SHUTDOWN;
161
 
 
162
141
      return true;
163
142
    }
164
143
 
166
145
    cin.ignore(2, '\n');
167
146
 
168
147
    (*packet)[0]= COM_QUERY;
169
 
 
170
148
    return true;
171
149
  }
172
150
 
180
158
    printDebug("sendEOF");
181
159
  }
182
160
 
183
 
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
 
161
  virtual void sendError(uint32_t sql_errno, const char *err)
184
162
  {
185
 
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
 
163
    cout << "Error: " << sql_errno << " " << err << endl;
186
164
  }
187
165
 
188
166
  virtual bool sendFields(List<Item> *list)
221
199
 
222
200
    char buff[MAX_FIELD_WIDTH];
223
201
    String str(buff, sizeof(buff), &my_charset_bin);
224
 
    from->val_str_internal(&str);
 
202
    from->val_str(&str);
225
203
    return store(str.ptr(), str.length());
226
204
  }
227
205
 
291
269
    printDebug("wasAborted");
292
270
    return false;
293
271
  }
294
 
 
295
 
  bool isConsole()
296
 
  {
297
 
    return true;
298
 
  }
299
272
};
300
273
 
301
274
class ListenConsole: public plugin::Listen
303
276
  int pipe_fds[2];
304
277
  const std::string username;
305
278
  const std::string password;
306
 
  const std::string schema;
307
 
  const std::string _catalog;
 
279
  const std::string db;
308
280
 
309
281
public:
310
282
  ListenConsole(const std::string &name_arg,
311
283
                const std::string &username_arg,
312
284
                const std::string &password_arg,
313
 
                const std::string &schema_arg,
314
 
                const std::string &catalog_arg) :
 
285
                const std::string &db_arg) :
315
286
    plugin::Listen(name_arg),
316
287
    username(username_arg),
317
288
    password(password_arg),
318
 
    schema(schema_arg),
319
 
    _catalog(catalog_arg)
 
289
    db(db_arg)
320
290
  {
321
291
    pipe_fds[0]= -1;
322
292
  }
340
310
 
341
311
    if (pipe(pipe_fds) == -1)
342
312
    {
343
 
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
 
313
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
344
314
      return true;
345
315
    }
346
316
 
353
323
  {
354
324
    char buffer[1];
355
325
    assert(read(fd, buffer, 1) == 1);
356
 
 
357
 
    return new ClientConsole(username, password, schema, _catalog);
 
326
    return new ClientConsole(username, password, db);
358
327
  }
359
328
};
360
329
 
363
332
  const module::option_map &vm= context.getOptions();
364
333
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
365
334
  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
 
 
 
335
  const string db(vm.count("db") ? vm["db"].as<string>() : "");
 
336
  context.registerVariable(new sys_var_bool_ptr("enable", &enabled));
 
337
  context.registerVariable(new sys_var_bool_ptr("debug_enable", &debug_enabled));
 
338
  context.registerVariable(new sys_var_const_string_val("username", username));
 
339
  context.registerVariable(new sys_var_const_string_val("password", password));
 
340
  context.registerVariable(new sys_var_const_string_val("db", db));
 
341
  context.add(new ListenConsole("console", username, password, db));
372
342
  return 0;
373
343
}
374
344
 
386
356
  context("password",
387
357
          po::value<string>(),
388
358
          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."));
 
359
  context("db",
 
360
          po::value<string>(),
 
361
          N_("Default database to use."));
395
362
}
396
363
 
397
364
DRIZZLE_DECLARE_PLUGIN
398
365
{
399
366
  DRIZZLE_VERSION_ID,
400
367
  "console",
401
 
  "0.2",
 
368
  "0.1",
402
369
  "Eric Day",
403
370
  "Console Client",
404
371
  PLUGIN_LICENSE_BSD,
405
372
  init,   /* Plugin Init */
406
 
  NULL,   /* depends */
 
373
  NULL,   /* system variables */
407
374
  init_options    /* config options */
408
375
}
409
376
DRIZZLE_DECLARE_PLUGIN_END;