~drizzle-trunk/drizzle/development

1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
1
/* Copyright (C) 2009 Sun Microsystems, Inc.
971.6.9 by Eric Day
Added console plugin.
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
16
#include <config.h>
2234.1.1 by Olaf van der Spek
Refactor includes
17
#include <drizzled/field.h>
971.6.9 by Eric Day
Added console plugin.
18
#include <drizzled/gettext.h>
19
#include <drizzled/plugin/listen_tcp.h>
20
#include <drizzled/plugin/client.h>
1305.1.2 by Eric Day
Added command line options to console to pass username, password, and db name during authentication step.
21
#include <drizzled/session.h>
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
22
#include <drizzled/module/option_map.h>
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
23
#include <drizzled/plugin/catalog.h>
2239.1.6 by Olaf van der Spek
Refactor includes
24
#include <drizzled/plugin.h>
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
25
971.6.9 by Eric Day
Added console plugin.
26
#include <iostream>
27
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
28
#include <boost/program_options.hpp>
29
2281.7.1 by Andrew Hutchings
Add better shell user detect functionality and add it to console
30
#include <client/user_detect.h>
31
971.6.9 by Eric Day
Added console plugin.
32
using namespace std;
33
using namespace drizzled;
34
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
35
namespace po= boost::program_options;
36
1857.3.3 by Monty Taylor
It works - has a valgrind issue somewhere.
37
static bool enabled= false;
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
38
static bool debug_enabled= false;
1305.1.2 by Eric Day
Added command line options to console to pass username, password, and db name during authentication step.
39
971.6.9 by Eric Day
Added console plugin.
40
41
class ClientConsole: public plugin::Client
42
{
43
  bool is_dead;
44
  uint32_t column;
45
  uint32_t max_column;
1857.4.1 by Monty Taylor
Added string sys_var type.
46
  const std::string &username;
47
  const std::string &password;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
48
  const std::string &schema;
49
  const std::string &_catalog;
971.6.9 by Eric Day
Added console plugin.
50
51
public:
1857.4.1 by Monty Taylor
Added string sys_var type.
52
  ClientConsole(const std::string &username_arg,
53
                const std::string &password_arg,
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
54
                const std::string &schema_arg,
55
                const std::string &catalog_arg) :
971.6.9 by Eric Day
Added console plugin.
56
    is_dead(false),
57
    column(0),
1857.4.1 by Monty Taylor
Added string sys_var type.
58
    max_column(0),
59
    username(username_arg),
60
    password(password_arg),
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
61
    schema(schema_arg),
62
    _catalog(catalog_arg)
971.6.9 by Eric Day
Added console plugin.
63
  {}
64
65
  virtual void printDebug(const char *message)
66
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
67
    if (debug_enabled)
971.6.9 by Eric Day
Added console plugin.
68
      cout << "CONSOLE: " << message << endl;
69
  }
70
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
71
  catalog::Instance::shared_ptr catalog()
72
  {
73
    identifier::Catalog identifier(_catalog);
74
    catalog::Instance::shared_ptr tmp= plugin::Catalog::getInstance(identifier);
75
    if (not tmp)
76
    {
77
      std::cerr << "Invalid catalog '" << identifier << "', resorting to 'local' catalog" << std::endl;
78
    }
79
    return tmp;
80
  }
81
971.6.9 by Eric Day
Added console plugin.
82
  virtual int getFileDescriptor(void)
83
  {
84
    printDebug("getFileDescriptor");
85
    return 0;
86
  }
87
88
  virtual bool isConnected(void)
89
  {
90
    printDebug("isConnected");
91
    return true;
92
  }
93
94
  virtual bool flush(void)
95
  {
96
    printDebug("flush");
97
    return false;
98
  }
99
100
  virtual void close(void)
101
  {
102
    printDebug("close");
103
    is_dead= true;
104
  }
105
106
  virtual bool authenticate(void)
107
  {
108
    printDebug("authenticate");
2252.1.8 by Olaf van der Spek
Common fwd
109
    identifier::user::mptr user= identifier::User::make_shared();
2008.1.1 by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency
110
    user->setUser(username);
111
    session->setUser(user);
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
112
113
    return session->checkUser(password, schema);
971.6.9 by Eric Day
Added console plugin.
114
  }
115
2312.1.4 by Brian Aker
Fix packet length, no pointer to using a pointer there.
116
  virtual bool readCommand(char **packet, uint32_t& packet_length)
971.6.9 by Eric Day
Added console plugin.
117
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
118
    uint32_t length;
971.6.9 by Eric Day
Added console plugin.
119
120
    if (is_dead)
121
      return false;
122
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
123
    cout << "drizzled> ";
124
125
    length= 1024;
126
    *packet= NULL;
127
128
    /* Start with 1 byte offset so we can set command. */
2312.1.4 by Brian Aker
Fix packet length, no pointer to using a pointer there.
129
    packet_length= 1;
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
130
131
    do
132
    {
133
      *packet= (char *)realloc(*packet, length);
134
      if (*packet == NULL)
135
        return false;
136
137
      cin.clear();
2312.1.4 by Brian Aker
Fix packet length, no pointer to using a pointer there.
138
      cin.getline(*packet + packet_length, length - packet_length, ';');
139
      packet_length+= cin.gcount();
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
140
      length*= 2;
141
    }
142
    while (cin.eof() == false && cin.fail() == true);
143
2312.1.4 by Brian Aker
Fix packet length, no pointer to using a pointer there.
144
    if ((packet_length == 1 && cin.eof() == true) or
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
145
        not strncasecmp(*packet + 1, "quit", 4) or
146
        not strncasecmp(*packet + 1, "exit", 4) or
147
        not strncasecmp(*packet + 1, "shutdown", sizeof("shutdown") -1))
971.6.9 by Eric Day
Added console plugin.
148
    {
149
      is_dead= true;
2312.1.4 by Brian Aker
Fix packet length, no pointer to using a pointer there.
150
      packet_length= 1;
971.6.9 by Eric Day
Added console plugin.
151
      (*packet)[0]= COM_SHUTDOWN;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
152
971.6.9 by Eric Day
Added console plugin.
153
      return true;
154
    }
155
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
156
    /* Skip \r and \n for next time. */
157
    cin.ignore(2, '\n');
158
971.6.9 by Eric Day
Added console plugin.
159
    (*packet)[0]= COM_QUERY;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
160
971.6.9 by Eric Day
Added console plugin.
161
    return true;
162
  }
163
164
  virtual void sendOK(void)
165
  {
166
    cout << "OK" << endl;
167
  }
168
169
  virtual void sendEOF(void)
170
  {
171
    printDebug("sendEOF");
172
  }
173
2126.3.4 by Brian Aker
Additional error cleanup (passing error correctly to the client code).
174
  virtual void sendError(const drizzled::error_t sql_errno, const char *err)
971.6.9 by Eric Day
Added console plugin.
175
  {
2126.3.4 by Brian Aker
Additional error cleanup (passing error correctly to the client code).
176
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
971.6.9 by Eric Day
Added console plugin.
177
  }
178
2318.3.8 by Olaf van der Spek
Refactor
179
  virtual void sendFields(List<Item>& list)
971.6.9 by Eric Day
Added console plugin.
180
  {
2318.3.8 by Olaf van der Spek
Refactor
181
    List<Item>::iterator it(list.begin());
971.6.9 by Eric Day
Added console plugin.
182
183
    column= 0;
184
    max_column= 0;
185
2318.3.8 by Olaf van der Spek
Refactor
186
    while (Item* item=it++)
971.6.9 by Eric Day
Added console plugin.
187
    {
188
      SendField field;
189
      item->make_field(&field);
190
      cout << field.col_name << "\t";
191
      max_column++;
192
    }
193
    cout << endl;
194
  }
195
196
  virtual void checkRowEnd(void)
197
  {
198
    if (++column % max_column == 0)
199
      cout << endl;
200
  }
201
202
  using Client::store;
203
2313.3.6 by Olaf van der Spek
Return void
204
  virtual void store(Field *from)
971.6.9 by Eric Day
Added console plugin.
205
  {
206
    if (from->is_null())
207
      return store();
208
209
    char buff[MAX_FIELD_WIDTH];
210
    String str(buff, sizeof(buff), &my_charset_bin);
1996.2.1 by Brian Aker
uuid type code.
211
    from->val_str_internal(&str);
971.6.9 by Eric Day
Added console plugin.
212
    return store(str.ptr(), str.length());
213
  }
214
2313.3.6 by Olaf van der Spek
Return void
215
  virtual void store(void)
971.6.9 by Eric Day
Added console plugin.
216
  {
217
    cout << "NULL" << "\t";
218
    checkRowEnd();
2313.3.6 by Olaf van der Spek
Return void
219
  }
220
221
  virtual void store(int32_t from)
222
  {
223
    cout << from << "\t";
224
    checkRowEnd();
225
  }
226
227
  virtual void store(uint32_t from)
228
  {
229
    cout << from << "\t";
230
    checkRowEnd();
231
  }
232
233
  virtual void store(int64_t from)
234
  {
235
    cout << from << "\t";
236
    checkRowEnd();
237
  }
238
239
  virtual void store(uint64_t from)
240
  {
241
    cout << from << "\t";
242
    checkRowEnd();
243
  }
244
245
  virtual void store(double from, uint32_t decimals, String *buffer)
971.6.9 by Eric Day
Added console plugin.
246
  {
247
    buffer->set_real(from, decimals, &my_charset_bin);
2313.3.6 by Olaf van der Spek
Return void
248
    store(buffer->ptr(), buffer->length());
971.6.9 by Eric Day
Added console plugin.
249
  }
250
2313.3.6 by Olaf van der Spek
Return void
251
  virtual void store(const char *from, size_t length)
971.6.9 by Eric Day
Added console plugin.
252
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
253
    cout.write(from, length);
254
    cout << "\t";
971.6.9 by Eric Day
Added console plugin.
255
    checkRowEnd();
256
  }
257
2313.3.6 by Olaf van der Spek
Return void
258
  virtual bool haveError()
971.6.9 by Eric Day
Added console plugin.
259
  {
260
    printDebug("haveError");
261
    return false;
262
  }
263
2313.3.6 by Olaf van der Spek
Return void
264
  virtual bool wasAborted()
971.6.9 by Eric Day
Added console plugin.
265
  {
266
    printDebug("wasAborted");
267
    return false;
268
  }
2098.4.1 by Brian Aker
Make session encapsulated.
269
2191.1.4 by Brian Aker
Add in interactive mode back to protocol.
270
  bool isConsole() const
2098.4.1 by Brian Aker
Make session encapsulated.
271
  {
272
    return true;
273
  }
2191.1.2 by Brian Aker
Adding SESSION table and adding back in the mysql interactive flag.
274
2191.1.4 by Brian Aker
Add in interactive mode back to protocol.
275
  bool isInteractive() const
2191.1.2 by Brian Aker
Adding SESSION table and adding back in the mysql interactive flag.
276
  {
277
    return true;
278
  }
971.6.9 by Eric Day
Added console plugin.
279
};
280
281
class ListenConsole: public plugin::Listen
282
{
283
  int pipe_fds[2];
1857.4.1 by Monty Taylor
Added string sys_var type.
284
  const std::string username;
285
  const std::string password;
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
286
  const std::string schema;
287
  const std::string _catalog;
971.6.9 by Eric Day
Added console plugin.
288
289
public:
1857.4.1 by Monty Taylor
Added string sys_var type.
290
  ListenConsole(const std::string &name_arg,
291
                const std::string &username_arg,
292
                const std::string &password_arg,
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
293
                const std::string &schema_arg,
294
                const std::string &catalog_arg) :
1857.4.1 by Monty Taylor
Added string sys_var type.
295
    plugin::Listen(name_arg),
296
    username(username_arg),
297
    password(password_arg),
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
298
    schema(schema_arg),
299
    _catalog(catalog_arg)
971.6.9 by Eric Day
Added console plugin.
300
  {
301
    pipe_fds[0]= -1;
302
  }
303
304
  virtual ~ListenConsole()
305
  {
306
    if (pipe_fds[0] != -1)
307
    {
308
      close(pipe_fds[0]);
309
      close(pipe_fds[1]);
310
    }
311
  }
312
313
  virtual bool getFileDescriptors(std::vector<int> &fds)
314
  {
971.7.1 by Eric Day
Client/Listen cleanup, moved globals, console plugin cleanup.
315
    if (debug_enabled)
316
      enabled= true;
971.6.9 by Eric Day
Added console plugin.
317
1857.3.3 by Monty Taylor
It works - has a valgrind issue somewhere.
318
    if (not enabled)
319
      return false;
320
971.6.9 by Eric Day
Added console plugin.
321
    if (pipe(pipe_fds) == -1)
322
    {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
323
      errmsg_printf(error::ERROR, _("pipe() failed with errno %d"), errno);
971.6.9 by Eric Day
Added console plugin.
324
      return true;
325
    }
326
327
    fds.push_back(pipe_fds[0]);
328
    assert(write(pipe_fds[1], "\0", 1) == 1);
329
    return false;
330
  }
331
332
  virtual drizzled::plugin::Client *getClient(int fd)
333
  {
334
    char buffer[1];
335
    assert(read(fd, buffer, 1) == 1);
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
336
337
    return new ClientConsole(username, password, schema, _catalog);
971.6.9 by Eric Day
Added console plugin.
338
  }
339
};
340
1530.2.6 by Monty Taylor
Moved plugin::Context to module::Context.
341
static int init(drizzled::module::Context &context)
971.6.9 by Eric Day
Added console plugin.
342
{
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
343
  const module::option_map &vm= context.getOptions();
2252.1.28 by Olaf van der Spek
Refactor Program Options usage
344
  context.add(new ListenConsole("console", vm["username"].as<string>(), 
345
    vm["password"].as<string>(), vm["schema"].as<string>(), vm["catalog"].as<string>()));
971.6.9 by Eric Day
Added console plugin.
346
  return 0;
347
}
348
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
349
static void init_options(drizzled::module::option_context &context)
350
{
351
  context("enable",
352
          po::value<bool>(&enabled)->default_value(false)->zero_tokens(),
353
          N_("Enable the console."));
354
  context("debug",
355
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
356
          N_("Turn on extra debugging."));
2281.7.9 by Andrew Hutchings
Also fixup console plugin
357
  UserDetect detected_user;
358
  const char* shell_user= detected_user.getUser();
1626.2.2 by Monty Taylor
Moved ownership of the variable_map to the module (avoids copying, also
359
  context("username",
2281.7.1 by Andrew Hutchings
Add better shell user detect functionality and add it to console
360
          po::value<string>()->default_value(shell_user ? shell_user : ""),
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
361
          N_("User to use for auth."));
362
  context("password",
2252.1.28 by Olaf van der Spek
Refactor Program Options usage
363
          po::value<string>()->default_value(""),
1626.2.1 by Monty Taylor
Added wrapper around variables_map to allow us to pull values back out of
364
          N_("Password to use for auth."));
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
365
  context("catalog",
2252.1.28 by Olaf van der Spek
Refactor Program Options usage
366
          po::value<string>()->default_value("LOCAL"),
2104.1.2 by Brian Aker
Update console to switch to different catalogs.
367
          N_("Default catalog to use."));
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
368
  context("schema",
2252.1.28 by Olaf van der Spek
Refactor Program Options usage
369
          po::value<string>()->default_value(""),
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
370
          N_("Default schema to use."));
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
371
}
372
1228.1.5 by Monty Taylor
Merged in some naming things.
373
DRIZZLE_DECLARE_PLUGIN
971.6.9 by Eric Day
Added console plugin.
374
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
375
  DRIZZLE_VERSION_ID,
971.6.9 by Eric Day
Added console plugin.
376
  "console",
2104.1.1 by Brian Aker
Merge in update to use console for catalog DDL.
377
  "0.2",
971.6.9 by Eric Day
Added console plugin.
378
  "Eric Day",
379
  "Console Client",
380
  PLUGIN_LICENSE_BSD,
381
  init,   /* Plugin Init */
2095.3.1 by Monty Taylor
Re-purpose the old plugin sysvar slot in the struct to be a depends list.
382
  NULL,   /* depends */
1625.1.8 by Monty Taylor
Converted really simple options in console plugin.
383
  init_options    /* config options */
971.6.9 by Eric Day
Added console plugin.
384
}
1228.1.5 by Monty Taylor
Merged in some naming things.
385
DRIZZLE_DECLARE_PLUGIN_END;