~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Olaf van der Spek
  • Date: 2011-03-24 00:16:14 UTC
  • mto: This revision was merged to the branch mainline in revision 2251.
  • Revision ID: olafvdspek@gmail.com-20110324001614-wvmgc6eg52oq2321
Remove const_reference and reference from Session

Show diffs side-by-side

added added

removed removed

Lines of Context:
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");
102
119
    identifier::User::shared_ptr user= identifier::User::make_shared();
103
120
    user->setUser(username);
104
121
    session->setUser(user);
105
 
    return session->checkUser(password, db);
 
122
 
 
123
    return session->checkUser(password, schema);
106
124
  }
107
125
 
108
126
  virtual bool readCommand(char **packet, uint32_t *packet_length)
133
151
    }
134
152
    while (cin.eof() == false && cin.fail() == true);
135
153
 
136
 
    if ((*packet_length == 1 && cin.eof() == true) ||
137
 
        !strncasecmp(*packet + 1, "quit", 4) ||
138
 
        !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))
139
158
    {
140
159
      is_dead= true;
141
160
      *packet_length= 1;
142
161
      (*packet)[0]= COM_SHUTDOWN;
 
162
 
143
163
      return true;
144
164
    }
145
165
 
147
167
    cin.ignore(2, '\n');
148
168
 
149
169
    (*packet)[0]= COM_QUERY;
 
170
 
150
171
    return true;
151
172
  }
152
173
 
160
181
    printDebug("sendEOF");
161
182
  }
162
183
 
163
 
  virtual void sendError(uint32_t sql_errno, const char *err)
 
184
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
164
185
  {
165
 
    cout << "Error: " << sql_errno << " " << err << endl;
 
186
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
166
187
  }
167
188
 
168
189
  virtual bool sendFields(List<Item> *list)
169
190
  {
170
 
    List_iterator_fast<Item> it(*list);
 
191
    List<Item>::iterator it(list->begin());
171
192
    Item *item;
172
193
 
173
194
    column= 0;
271
292
    printDebug("wasAborted");
272
293
    return false;
273
294
  }
 
295
 
 
296
  bool isConsole() const
 
297
  {
 
298
    return true;
 
299
  }
 
300
 
 
301
  bool isInteractive() const
 
302
  {
 
303
    return true;
 
304
  }
274
305
};
275
306
 
276
307
class ListenConsole: public plugin::Listen
278
309
  int pipe_fds[2];
279
310
  const std::string username;
280
311
  const std::string password;
281
 
  const std::string db;
 
312
  const std::string schema;
 
313
  const std::string _catalog;
282
314
 
283
315
public:
284
316
  ListenConsole(const std::string &name_arg,
285
317
                const std::string &username_arg,
286
318
                const std::string &password_arg,
287
 
                const std::string &db_arg) :
 
319
                const std::string &schema_arg,
 
320
                const std::string &catalog_arg) :
288
321
    plugin::Listen(name_arg),
289
322
    username(username_arg),
290
323
    password(password_arg),
291
 
    db(db_arg)
 
324
    schema(schema_arg),
 
325
    _catalog(catalog_arg)
292
326
  {
293
327
    pipe_fds[0]= -1;
294
328
  }
312
346
 
313
347
    if (pipe(pipe_fds) == -1)
314
348
    {
315
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("pipe() failed with errno %d"), errno);
 
349
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
316
350
      return true;
317
351
    }
318
352
 
325
359
  {
326
360
    char buffer[1];
327
361
    assert(read(fd, buffer, 1) == 1);
328
 
    return new ClientConsole(username, password, db);
 
362
 
 
363
    return new ClientConsole(username, password, schema, _catalog);
329
364
  }
330
365
};
331
366
 
334
369
  const module::option_map &vm= context.getOptions();
335
370
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
336
371
  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));
 
372
  const string schema(vm.count("schema") ? vm["schema"].as<string>() : "");
 
373
 
 
374
  const std::string catalog(vm.count("catalog") ? vm["catalog"].as<string>() : "LOCAL");
 
375
 
 
376
  context.add(new ListenConsole("console", username, password, schema, catalog));
 
377
 
344
378
  return 0;
345
379
}
346
380
 
358
392
  context("password",
359
393
          po::value<string>(),
360
394
          N_("Password to use for auth."));
361
 
  context("db",
362
 
          po::value<string>(),
363
 
          N_("Default database to use."));
 
395
  context("catalog",
 
396
          po::value<string>(),
 
397
          N_("Default catalog to use."));
 
398
  context("schema",
 
399
          po::value<string>(),
 
400
          N_("Default schema to use."));
364
401
}
365
402
 
366
403
DRIZZLE_DECLARE_PLUGIN
367
404
{
368
405
  DRIZZLE_VERSION_ID,
369
406
  "console",
370
 
  "0.1",
 
407
  "0.2",
371
408
  "Eric Day",
372
409
  "Console Client",
373
410
  PLUGIN_LICENSE_BSD,
374
411
  init,   /* Plugin Init */
375
 
  NULL,   /* system variables */
 
412
  NULL,   /* depends */
376
413
  init_options    /* config options */
377
414
}
378
415
DRIZZLE_DECLARE_PLUGIN_END;