~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: patrick crews
  • Date: 2011-06-08 03:02:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2329.
  • Revision ID: gleebix@gmail.com-20110608030227-updkyv2652zvfajc
Initial voodoo worked to give us a crashme mode.  Need docs still

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
#include <drizzled/field.h>
17
18
#include <drizzled/gettext.h>
18
19
#include <drizzled/plugin/listen_tcp.h>
19
20
#include <drizzled/plugin/client.h>
20
21
#include <drizzled/session.h>
21
22
#include <drizzled/module/option_map.h>
 
23
#include <drizzled/plugin/catalog.h>
 
24
#include <drizzled/plugin.h>
22
25
 
23
26
#include <iostream>
24
27
 
40
43
  uint32_t max_column;
41
44
  const std::string &username;
42
45
  const std::string &password;
43
 
  const std::string &db;
 
46
  const std::string &schema;
 
47
  const std::string &_catalog;
44
48
 
45
49
public:
46
50
  ClientConsole(const std::string &username_arg,
47
51
                const std::string &password_arg,
48
 
                const std::string &db_arg) :
 
52
                const std::string &schema_arg,
 
53
                const std::string &catalog_arg) :
49
54
    is_dead(false),
50
55
    column(0),
51
56
    max_column(0),
52
57
    username(username_arg),
53
58
    password(password_arg),
54
 
    db(db_arg)
 
59
    schema(schema_arg),
 
60
    _catalog(catalog_arg)
55
61
  {}
56
62
 
57
63
  virtual void printDebug(const char *message)
60
66
      cout << "CONSOLE: " << message << endl;
61
67
  }
62
68
 
 
69
  catalog::Instance::shared_ptr catalog()
 
70
  {
 
71
    identifier::Catalog identifier(_catalog);
 
72
    catalog::Instance::shared_ptr tmp= plugin::Catalog::getInstance(identifier);
 
73
    if (not tmp)
 
74
    {
 
75
      std::cerr << "Invalid catalog '" << identifier << "', resorting to 'local' catalog" << std::endl;
 
76
    }
 
77
    return tmp;
 
78
  }
 
79
 
63
80
  virtual int getFileDescriptor(void)
64
81
  {
65
82
    printDebug("getFileDescriptor");
99
116
  virtual bool authenticate(void)
100
117
  {
101
118
    printDebug("authenticate");
102
 
    session->getSecurityContext().setUser(username);
103
 
    return session->checkUser(password, db);
 
119
    identifier::user::mptr user= identifier::User::make_shared();
 
120
    user->setUser(username);
 
121
    session->setUser(user);
 
122
 
 
123
    return session->checkUser(password, schema);
104
124
  }
105
125
 
106
 
  virtual bool readCommand(char **packet, uint32_t *packet_length)
 
126
  virtual bool readCommand(char **packet, uint32_t& packet_length)
107
127
  {
108
128
    uint32_t length;
109
129
 
116
136
    *packet= NULL;
117
137
 
118
138
    /* Start with 1 byte offset so we can set command. */
119
 
    *packet_length= 1;
 
139
    packet_length= 1;
120
140
 
121
141
    do
122
142
    {
125
145
        return false;
126
146
 
127
147
      cin.clear();
128
 
      cin.getline(*packet + *packet_length, length - *packet_length, ';');
129
 
      *packet_length+= cin.gcount();
 
148
      cin.getline(*packet + packet_length, length - packet_length, ';');
 
149
      packet_length+= cin.gcount();
130
150
      length*= 2;
131
151
    }
132
152
    while (cin.eof() == false && cin.fail() == true);
133
153
 
134
 
    if ((*packet_length == 1 && cin.eof() == true) ||
135
 
        !strncasecmp(*packet + 1, "quit", 4) ||
136
 
        !strncasecmp(*packet + 1, "exit", 4))
 
154
    if ((packet_length == 1 && cin.eof() == true) or
 
155
        not strncasecmp(*packet + 1, "quit", 4) or
 
156
        not strncasecmp(*packet + 1, "exit", 4) or
 
157
        not strncasecmp(*packet + 1, "shutdown", sizeof("shutdown") -1))
137
158
    {
138
159
      is_dead= true;
139
 
      *packet_length= 1;
 
160
      packet_length= 1;
140
161
      (*packet)[0]= COM_SHUTDOWN;
 
162
 
141
163
      return true;
142
164
    }
143
165
 
145
167
    cin.ignore(2, '\n');
146
168
 
147
169
    (*packet)[0]= COM_QUERY;
 
170
 
148
171
    return true;
149
172
  }
150
173
 
158
181
    printDebug("sendEOF");
159
182
  }
160
183
 
161
 
  virtual void sendError(uint32_t sql_errno, const char *err)
 
184
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
162
185
  {
163
 
    cout << "Error: " << sql_errno << " " << err << endl;
 
186
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
164
187
  }
165
188
 
166
189
  virtual bool sendFields(List<Item> *list)
167
190
  {
168
 
    List_iterator_fast<Item> it(*list);
 
191
    List<Item>::iterator it(list->begin());
169
192
    Item *item;
170
193
 
171
194
    column= 0;
192
215
 
193
216
  using Client::store;
194
217
 
195
 
  virtual bool store(Field *from)
 
218
  virtual void store(Field *from)
196
219
  {
197
220
    if (from->is_null())
198
221
      return store();
203
226
    return store(str.ptr(), str.length());
204
227
  }
205
228
 
206
 
  virtual bool store(void)
 
229
  virtual void store(void)
207
230
  {
208
231
    cout << "NULL" << "\t";
209
232
    checkRowEnd();
210
 
    return false;
211
 
  }
212
 
 
213
 
  virtual bool store(int32_t from)
214
 
  {
215
 
    cout << from << "\t";
216
 
    checkRowEnd();
217
 
    return false;
218
 
  }
219
 
 
220
 
  virtual bool store(uint32_t from)
221
 
  {
222
 
    cout << from << "\t";
223
 
    checkRowEnd();
224
 
    return false;
225
 
  }
226
 
 
227
 
  virtual bool store(int64_t from)
228
 
  {
229
 
    cout << from << "\t";
230
 
    checkRowEnd();
231
 
    return false;
232
 
  }
233
 
 
234
 
  virtual bool store(uint64_t from)
235
 
  {
236
 
    cout << from << "\t";
237
 
    checkRowEnd();
238
 
    return false;
239
 
  }
240
 
 
241
 
  virtual bool store(double from, uint32_t decimals, String *buffer)
 
233
  }
 
234
 
 
235
  virtual void store(int32_t from)
 
236
  {
 
237
    cout << from << "\t";
 
238
    checkRowEnd();
 
239
  }
 
240
 
 
241
  virtual void store(uint32_t from)
 
242
  {
 
243
    cout << from << "\t";
 
244
    checkRowEnd();
 
245
  }
 
246
 
 
247
  virtual void store(int64_t from)
 
248
  {
 
249
    cout << from << "\t";
 
250
    checkRowEnd();
 
251
  }
 
252
 
 
253
  virtual void store(uint64_t from)
 
254
  {
 
255
    cout << from << "\t";
 
256
    checkRowEnd();
 
257
  }
 
258
 
 
259
  virtual void store(double from, uint32_t decimals, String *buffer)
242
260
  {
243
261
    buffer->set_real(from, decimals, &my_charset_bin);
244
 
    return store(buffer->ptr(), buffer->length());
 
262
    store(buffer->ptr(), buffer->length());
245
263
  }
246
264
 
247
 
  virtual bool store(const char *from, size_t length)
 
265
  virtual void store(const char *from, size_t length)
248
266
  {
249
267
    cout.write(from, length);
250
268
    cout << "\t";
251
269
    checkRowEnd();
252
 
    return false;
253
270
  }
254
271
 
255
 
  virtual bool haveMoreData(void)
 
272
  virtual bool haveMoreData()
256
273
  {
257
274
    printDebug("haveMoreData");
258
275
    return false;
259
276
  }
260
277
 
261
 
  virtual bool haveError(void)
 
278
  virtual bool haveError()
262
279
  {
263
280
    printDebug("haveError");
264
281
    return false;
265
282
  }
266
283
 
267
 
  virtual bool wasAborted(void)
 
284
  virtual bool wasAborted()
268
285
  {
269
286
    printDebug("wasAborted");
270
287
    return false;
271
288
  }
 
289
 
 
290
  bool isConsole() const
 
291
  {
 
292
    return true;
 
293
  }
 
294
 
 
295
  bool isInteractive() const
 
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
 
330
361
static int init(drizzled::module::Context &context)
331
362
{
332
363
  const module::option_map &vm= context.getOptions();
333
 
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
334
 
  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));
 
364
  context.add(new ListenConsole("console", vm["username"].as<string>(), 
 
365
    vm["password"].as<string>(), vm["schema"].as<string>(), vm["catalog"].as<string>()));
342
366
  return 0;
343
367
}
344
368
 
351
375
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
352
376
          N_("Turn on extra debugging."));
353
377
  context("username",
354
 
          po::value<string>(),
 
378
          po::value<string>()->default_value(""),
355
379
          N_("User to use for auth."));
356
380
  context("password",
357
 
          po::value<string>(),
 
381
          po::value<string>()->default_value(""),
358
382
          N_("Password to use for auth."));
359
 
  context("db",
360
 
          po::value<string>(),
361
 
          N_("Default database to use."));
 
383
  context("catalog",
 
384
          po::value<string>()->default_value("LOCAL"),
 
385
          N_("Default catalog to use."));
 
386
  context("schema",
 
387
          po::value<string>()->default_value(""),
 
388
          N_("Default schema to use."));
362
389
}
363
390
 
364
391
DRIZZLE_DECLARE_PLUGIN
365
392
{
366
393
  DRIZZLE_VERSION_ID,
367
394
  "console",
368
 
  "0.1",
 
395
  "0.2",
369
396
  "Eric Day",
370
397
  "Console Client",
371
398
  PLUGIN_LICENSE_BSD,
372
399
  init,   /* Plugin Init */
373
 
  NULL,   /* system variables */
 
400
  NULL,   /* depends */
374
401
  init_options    /* config options */
375
402
}
376
403
DRIZZLE_DECLARE_PLUGIN_END;