~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/console/console.cc

  • Committer: Stewart Smith
  • Date: 2009-10-12 05:13:54 UTC
  • mfrom: (1178 staging)
  • mto: This revision was merged to the branch mainline in revision 1179.
  • Revision ID: stewart@flamingspork.com-20091012051354-2n7zpid9f67ddsa0
mergeĀ lp:drizzle/build

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
using namespace std;
24
24
using namespace drizzled;
25
25
 
26
 
static bool enable= false;
27
 
static bool debug= false;
 
26
static bool enabled= false;
 
27
static bool debug_enabled= false;
28
28
 
29
29
class ClientConsole: public plugin::Client
30
30
{
41
41
 
42
42
  virtual void printDebug(const char *message)
43
43
  {
44
 
    if (debug)
 
44
    if (debug_enabled)
45
45
      cout << "CONSOLE: " << message << endl;
46
46
  }
47
47
 
89
89
 
90
90
  virtual bool readCommand(char **packet, uint32_t *packet_length)
91
91
  {
92
 
    char buffer[8192];
93
 
    printf("drizzled> ");
94
 
    fflush(stdout);
 
92
    uint32_t length;
95
93
 
96
94
    if (is_dead)
97
95
      return false;
98
96
 
99
 
    if (fgets(buffer, 8192, stdin) == NULL ||!strcasecmp(buffer, "quit\n") ||
100
 
        !strcasecmp(buffer, "exit\n"))
 
97
    cout << "drizzled> ";
 
98
 
 
99
    length= 1024;
 
100
    *packet= NULL;
 
101
 
 
102
    /* Start with 1 byte offset so we can set command. */
 
103
    *packet_length= 1;
 
104
 
 
105
    do
 
106
    {
 
107
      *packet= (char *)realloc(*packet, length);
 
108
      if (*packet == NULL)
 
109
        return false;
 
110
 
 
111
      cin.clear();
 
112
      cin.getline(*packet + *packet_length, length - *packet_length, ';');
 
113
      *packet_length+= cin.gcount();
 
114
      length*= 2;
 
115
    }
 
116
    while (cin.eof() == false && cin.fail() == true);
 
117
 
 
118
    if ((*packet_length == 1 && cin.eof() == true) ||
 
119
        !strncasecmp(*packet + 1, "quit", 4) ||
 
120
        !strncasecmp(*packet + 1, "exit", 4))
101
121
    {
102
122
      is_dead= true;
103
123
      *packet_length= 1;
104
 
      *packet= (char *)malloc(*packet_length);
105
124
      (*packet)[0]= COM_SHUTDOWN;
106
125
      return true;
107
126
    }
108
127
 
109
 
    *packet_length= strlen(buffer);
110
 
    *packet= (char *)malloc(*packet_length);
 
128
    /* Skip \r and \n for next time. */
 
129
    cin.ignore(2, '\n');
 
130
 
111
131
    (*packet)[0]= COM_QUERY;
112
 
    memcpy(*packet + 1, buffer, *packet_length - 1);
113
 
 
114
132
    return true;
115
133
  }
116
134
 
210
228
    return store(buffer->ptr(), buffer->length());
211
229
  }
212
230
 
213
 
  virtual bool store(const DRIZZLE_TIME *tm)
214
 
  {
215
 
    char buff[40];
216
 
    uint32_t length;
217
 
    uint32_t day;
218
 
 
219
 
    switch (tm->time_type)
220
 
    {
221
 
    case DRIZZLE_TIMESTAMP_DATETIME:
222
 
      length= sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d",
223
 
                      (int) tm->year,
224
 
                      (int) tm->month,
225
 
                      (int) tm->day,
226
 
                      (int) tm->hour,
227
 
                      (int) tm->minute,
228
 
                      (int) tm->second);
229
 
      if (tm->second_part)
230
 
        length+= sprintf(buff+length, ".%06d", (int)tm->second_part);
231
 
      break;
232
 
 
233
 
    case DRIZZLE_TIMESTAMP_DATE:
234
 
      length= sprintf(buff, "%04d-%02d-%02d",
235
 
                      (int) tm->year,
236
 
                      (int) tm->month,
237
 
                      (int) tm->day);
238
 
      break;
239
 
 
240
 
    case DRIZZLE_TIMESTAMP_TIME:
241
 
      day= (tm->year || tm->month) ? 0 : tm->day;
242
 
      length= sprintf(buff, "%s%02ld:%02d:%02d", tm->neg ? "-" : "",
243
 
                      (long) day*24L+(long) tm->hour, (int) tm->minute,
244
 
                      (int) tm->second);
245
 
      if (tm->second_part)
246
 
        length+= sprintf(buff+length, ".%06d", (int)tm->second_part);
247
 
      break;
248
 
 
249
 
    case DRIZZLE_TIMESTAMP_NONE:
250
 
    case DRIZZLE_TIMESTAMP_ERROR:
251
 
    default:
252
 
      assert(0);
253
 
      return false;
254
 
    }
255
 
 
256
 
    return store(buff);
257
 
  }
258
 
 
259
231
  virtual bool store(const char *from, size_t length)
260
232
  {
261
 
    printf("%.*s\t", (uint32_t)length, from);
 
233
    cout.write(from, length);
 
234
    cout << "\t";
262
235
    checkRowEnd();
263
236
    return false;
264
237
  }
303
276
 
304
277
  virtual bool getFileDescriptors(std::vector<int> &fds)
305
278
  {
306
 
    if (debug)
307
 
      enable= true;
 
279
    if (debug_enabled)
 
280
      enabled= true;
308
281
 
309
 
    if (!enable)
 
282
    if (enabled == false)
310
283
      return false;
311
284
 
312
285
    if (pipe(pipe_fds) == -1)
344
317
  return 0;
345
318
}
346
319
 
347
 
static DRIZZLE_SYSVAR_BOOL(enable, enable, PLUGIN_VAR_NOCMDARG,
 
320
static DRIZZLE_SYSVAR_BOOL(enable, enabled, PLUGIN_VAR_NOCMDARG,
348
321
                           N_("Enable the console."), NULL, NULL, false);
349
322
 
350
 
static DRIZZLE_SYSVAR_BOOL(debug, debug, PLUGIN_VAR_NOCMDARG,
 
323
static DRIZZLE_SYSVAR_BOOL(debug, debug_enabled, PLUGIN_VAR_NOCMDARG,
351
324
                           N_("Turn on extra debugging."), NULL, NULL, false);
352
325
 
353
326
static struct st_mysql_sys_var* vars[]= {