~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

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