~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
merge lp:~olafvdspek/drizzle/refactor4

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>
22
 
 
23
23
#include <drizzled/plugin/catalog.h>
 
24
#include <drizzled/plugin.h>
24
25
 
25
26
#include <iostream>
26
27
 
27
28
#include <boost/program_options.hpp>
28
29
 
 
30
#include <client/user_detect.h>
 
31
 
29
32
using namespace std;
30
33
using namespace drizzled;
31
34
 
88
91
    return true;
89
92
  }
90
93
 
91
 
  virtual bool isReading(void)
92
 
  {
93
 
    printDebug("isReading");
94
 
    return false;
95
 
  }
96
 
 
97
 
  virtual bool isWriting(void)
98
 
  {
99
 
    printDebug("isWriting");
100
 
    return false;
101
 
  }
102
 
 
103
94
  virtual bool flush(void)
104
95
  {
105
96
    printDebug("flush");
115
106
  virtual bool authenticate(void)
116
107
  {
117
108
    printDebug("authenticate");
118
 
    identifier::User::shared_ptr user= identifier::User::make_shared();
 
109
    identifier::user::mptr user= identifier::User::make_shared();
119
110
    user->setUser(username);
120
111
    session->setUser(user);
121
112
 
122
113
    return session->checkUser(password, schema);
123
114
  }
124
115
 
125
 
  virtual bool readCommand(char **packet, uint32_t *packet_length)
 
116
  virtual bool readCommand(char **packet, uint32_t& packet_length)
126
117
  {
127
118
    uint32_t length;
128
119
 
135
126
    *packet= NULL;
136
127
 
137
128
    /* Start with 1 byte offset so we can set command. */
138
 
    *packet_length= 1;
 
129
    packet_length= 1;
139
130
 
140
131
    do
141
132
    {
144
135
        return false;
145
136
 
146
137
      cin.clear();
147
 
      cin.getline(*packet + *packet_length, length - *packet_length, ';');
148
 
      *packet_length+= cin.gcount();
 
138
      cin.getline(*packet + packet_length, length - packet_length, ';');
 
139
      packet_length+= cin.gcount();
149
140
      length*= 2;
150
141
    }
151
142
    while (cin.eof() == false && cin.fail() == true);
152
143
 
153
 
    if ((*packet_length == 1 && cin.eof() == true) or
 
144
    if ((packet_length == 1 && cin.eof() == true) or
154
145
        not strncasecmp(*packet + 1, "quit", 4) or
155
146
        not strncasecmp(*packet + 1, "exit", 4) or
156
147
        not strncasecmp(*packet + 1, "shutdown", sizeof("shutdown") -1))
157
148
    {
158
149
      is_dead= true;
159
 
      *packet_length= 1;
 
150
      packet_length= 1;
160
151
      (*packet)[0]= COM_SHUTDOWN;
161
152
 
162
153
      return true;
185
176
    cout << "Error: " << static_cast<long>(sql_errno) << " " << err << endl;
186
177
  }
187
178
 
188
 
  virtual bool sendFields(List<Item> *list)
 
179
  virtual void sendFields(List<Item>& list)
189
180
  {
190
 
    List_iterator_fast<Item> it(*list);
191
 
    Item *item;
 
181
    List<Item>::iterator it(list.begin());
192
182
 
193
183
    column= 0;
194
184
    max_column= 0;
195
185
 
196
 
    while ((item=it++))
 
186
    while (Item* item=it++)
197
187
    {
198
188
      SendField field;
199
189
      item->make_field(&field);
200
190
      cout << field.col_name << "\t";
201
191
      max_column++;
202
192
    }
203
 
 
204
193
    cout << endl;
205
 
 
206
 
    return false;
207
194
  }
208
195
 
209
196
  virtual void checkRowEnd(void)
214
201
 
215
202
  using Client::store;
216
203
 
217
 
  virtual bool store(Field *from)
 
204
  virtual void store(Field *from)
218
205
  {
219
206
    if (from->is_null())
220
207
      return store();
225
212
    return store(str.ptr(), str.length());
226
213
  }
227
214
 
228
 
  virtual bool store(void)
 
215
  virtual void store(void)
229
216
  {
230
217
    cout << "NULL" << "\t";
231
218
    checkRowEnd();
232
 
    return false;
233
 
  }
234
 
 
235
 
  virtual bool store(int32_t from)
236
 
  {
237
 
    cout << from << "\t";
238
 
    checkRowEnd();
239
 
    return false;
240
 
  }
241
 
 
242
 
  virtual bool store(uint32_t from)
243
 
  {
244
 
    cout << from << "\t";
245
 
    checkRowEnd();
246
 
    return false;
247
 
  }
248
 
 
249
 
  virtual bool store(int64_t from)
250
 
  {
251
 
    cout << from << "\t";
252
 
    checkRowEnd();
253
 
    return false;
254
 
  }
255
 
 
256
 
  virtual bool store(uint64_t from)
257
 
  {
258
 
    cout << from << "\t";
259
 
    checkRowEnd();
260
 
    return false;
261
 
  }
262
 
 
263
 
  virtual bool store(double from, uint32_t decimals, String *buffer)
 
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)
264
246
  {
265
247
    buffer->set_real(from, decimals, &my_charset_bin);
266
 
    return store(buffer->ptr(), buffer->length());
 
248
    store(buffer->ptr(), buffer->length());
267
249
  }
268
250
 
269
 
  virtual bool store(const char *from, size_t length)
 
251
  virtual void store(const char *from, size_t length)
270
252
  {
271
253
    cout.write(from, length);
272
254
    cout << "\t";
273
255
    checkRowEnd();
274
 
    return false;
275
 
  }
276
 
 
277
 
  virtual bool haveMoreData(void)
278
 
  {
279
 
    printDebug("haveMoreData");
280
 
    return false;
281
 
  }
282
 
 
283
 
  virtual bool haveError(void)
 
256
  }
 
257
 
 
258
  virtual bool haveError()
284
259
  {
285
260
    printDebug("haveError");
286
261
    return false;
287
262
  }
288
263
 
289
 
  virtual bool wasAborted(void)
 
264
  virtual bool wasAborted()
290
265
  {
291
266
    printDebug("wasAborted");
292
267
    return false;
293
268
  }
294
269
 
295
 
  bool isConsole()
 
270
  bool isConsole() const
 
271
  {
 
272
    return true;
 
273
  }
 
274
 
 
275
  bool isInteractive() const
296
276
  {
297
277
    return true;
298
278
  }
361
341
static int init(drizzled::module::Context &context)
362
342
{
363
343
  const module::option_map &vm= context.getOptions();
364
 
  const string username(vm.count("username") ? vm["username"].as<string>() : "");
365
 
  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
 
 
 
344
  context.add(new ListenConsole("console", vm["username"].as<string>(), 
 
345
    vm["password"].as<string>(), vm["schema"].as<string>(), vm["catalog"].as<string>()));
372
346
  return 0;
373
347
}
374
348
 
380
354
  context("debug",
381
355
          po::value<bool>(&debug_enabled)->default_value(false)->zero_tokens(),
382
356
          N_("Turn on extra debugging."));
 
357
  UserDetect detected_user;
 
358
  const char* shell_user= detected_user.getUser();
383
359
  context("username",
384
 
          po::value<string>(),
 
360
          po::value<string>()->default_value(shell_user ? shell_user : ""),
385
361
          N_("User to use for auth."));
386
362
  context("password",
387
 
          po::value<string>(),
 
363
          po::value<string>()->default_value(""),
388
364
          N_("Password to use for auth."));
389
365
  context("catalog",
390
 
          po::value<string>(),
 
366
          po::value<string>()->default_value("LOCAL"),
391
367
          N_("Default catalog to use."));
392
368
  context("schema",
393
 
          po::value<string>(),
 
369
          po::value<string>()->default_value(""),
394
370
          N_("Default schema to use."));
395
371
}
396
372