~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 17:09:31 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212170931-k1fg0lzsmm5i3ciq
casts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
 
4
 *  Copyright (C) 2010 Brian Aker
4
5
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
6
 *
6
7
 *  This program is free software; you can redistribute it and/or modify
50
51
#include "drizzled/internal/my_sys.h"
51
52
#include "drizzled/message/statement_transform.h"
52
53
 
 
54
#include "drizzled/statement/show.h"
 
55
#include "drizzled/statement/show_errors.h"
 
56
#include "drizzled/statement/show_warnings.h"
 
57
 
53
58
 
54
59
#include <sys/stat.h>
55
60
 
143
148
  return '`';
144
149
}
145
150
 
 
151
namespace show {
 
152
 
 
153
bool buildScemas(Session *session)
 
154
{
 
155
  session->getLex()->sql_command= SQLCOM_SELECT;
 
156
  session->getLex()->statement= new statement::Show(session);
 
157
 
 
158
  std::string column_name= "Database";
 
159
  if (session->getLex()->wild)
 
160
  {
 
161
    column_name.append(" (");
 
162
    column_name.append(session->getLex()->wild->ptr());
 
163
    column_name.append(")");
 
164
  }
 
165
 
 
166
  if (session->getLex()->current_select->where)
 
167
  {
 
168
    if (prepare_new_schema_table(session, session->getLex(), "SCHEMAS"))
 
169
      return false;
 
170
  }
 
171
  else
 
172
  {
 
173
    if (prepare_new_schema_table(session, session->getLex(), "SHOW_SCHEMAS"))
 
174
      return false;
 
175
  }
 
176
 
 
177
  Item_field *my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "SCHEMA_NAME");
 
178
  my_field->is_autogenerated_name= false;
 
179
  my_field->set_name(column_name.c_str(), column_name.length(), system_charset_info);
 
180
 
 
181
  if (session->add_item_to_list(my_field))
 
182
    return false;
 
183
 
 
184
  if (session->add_order_to_list(my_field, true))
 
185
    return false;
 
186
 
 
187
  return true;
 
188
}
 
189
 
 
190
bool buildTables(Session *session, const char *ident)
 
191
{
 
192
  session->getLex()->sql_command= SQLCOM_SELECT;
 
193
 
 
194
  drizzled::statement::Show *select= new statement::Show(session);
 
195
  session->getLex()->statement= select;
 
196
 
 
197
  std::string column_name= "Tables_in_";
 
198
 
 
199
  util::string::const_shared_ptr schema(session->schema());
 
200
  if (ident)
 
201
  {
 
202
    identifier::Schema identifier(ident);
 
203
    column_name.append(ident);
 
204
    session->getLex()->select_lex.db= const_cast<char *>(ident);
 
205
    if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
206
    {
 
207
      my_error(ER_BAD_DB_ERROR, MYF(0), ident);
 
208
    }
 
209
    select->setShowPredicate(ident, "");
 
210
  }
 
211
  else if (schema and not schema->empty())
 
212
  {
 
213
    column_name.append(*schema);
 
214
    select->setShowPredicate(*schema, "");
 
215
  }
 
216
  else
 
217
  {
 
218
    my_error(ER_NO_DB_ERROR, MYF(0));
 
219
    return false;
 
220
  }
 
221
 
 
222
 
 
223
  if (session->getLex()->wild)
 
224
  {
 
225
    column_name.append(" (");
 
226
    column_name.append(session->getLex()->wild->ptr());
 
227
    column_name.append(")");
 
228
  }
 
229
 
 
230
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_TABLES"))
 
231
    return false;
 
232
 
 
233
  Item_field *my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "TABLE_NAME");
 
234
  my_field->is_autogenerated_name= false;
 
235
  my_field->set_name(column_name.c_str(), column_name.length(), system_charset_info);
 
236
 
 
237
  if (session->add_item_to_list(my_field))
 
238
    return false;
 
239
 
 
240
  if (session->add_order_to_list(my_field, true))
 
241
    return false;
 
242
 
 
243
  return true;
 
244
}
 
245
 
 
246
bool buildTemporaryTables(Session *session)
 
247
{
 
248
  session->getLex()->sql_command= SQLCOM_SELECT;
 
249
 
 
250
  session->getLex()->statement= new statement::Show(session);
 
251
 
 
252
 
 
253
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_TEMPORARY_TABLES"))
 
254
    return false;
 
255
 
 
256
  if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
 
257
    return false;
 
258
 
 
259
  (session->lex->current_select->with_wild)++;
 
260
 
 
261
  return true;
 
262
}
 
263
 
 
264
bool buildTableStatus(Session *session, const char *ident)
 
265
{
 
266
  session->getLex()->sql_command= SQLCOM_SELECT;
 
267
  drizzled::statement::Show *select= new statement::Show(session);
 
268
  session->getLex()->statement= select;
 
269
 
 
270
  std::string column_name= "Tables_in_";
 
271
 
 
272
  util::string::const_shared_ptr schema(session->schema());
 
273
  if (ident)
 
274
  {
 
275
    session->getLex()->select_lex.db= const_cast<char *>(ident);
 
276
 
 
277
    identifier::Schema identifier(ident);
 
278
    if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
279
    {
 
280
      my_error(ER_BAD_DB_ERROR, MYF(0), ident);
 
281
    }
 
282
 
 
283
    select->setShowPredicate(ident, "");
 
284
  }
 
285
  else if (schema)
 
286
  {
 
287
    select->setShowPredicate(*schema, "");
 
288
  }
 
289
  else
 
290
  {
 
291
    my_error(ER_NO_DB_ERROR, MYF(0));
 
292
    return false;
 
293
  }
 
294
 
 
295
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_TABLE_STATUS"))
 
296
    return false;
 
297
 
 
298
  if (session->add_item_to_list( new Item_field(&session->lex->current_select->
 
299
                                                  context,
 
300
                                                  NULL, NULL, "*")))
 
301
    return false;
 
302
 
 
303
  (session->lex->current_select->with_wild)++;
 
304
 
 
305
  return true;
 
306
}
 
307
 
 
308
bool buildEngineStatus(Session *session, LEX_STRING)
 
309
{
 
310
  session->getLex()->sql_command= SQLCOM_SELECT;
 
311
  drizzled::statement::Show *select= new statement::Show(session);
 
312
  session->getLex()->statement= select;
 
313
 
 
314
  my_error(ER_USE_DATA_DICTIONARY);
 
315
  return false;
 
316
}
 
317
 
 
318
bool buildColumns(Session *session, const char *schema_ident, Table_ident *table_ident)
 
319
{
 
320
  session->getLex()->sql_command= SQLCOM_SELECT;
 
321
 
 
322
  drizzled::statement::Show *select= new statement::Show(session);
 
323
  session->getLex()->statement= select;
 
324
 
 
325
  util::string::const_shared_ptr schema(session->schema());
 
326
  if (schema_ident)
 
327
  {
 
328
    select->setShowPredicate(schema_ident, table_ident->table.str);
 
329
  }
 
330
  else if (table_ident->db.str)
 
331
  {
 
332
    select->setShowPredicate(table_ident->db.str, table_ident->table.str);
 
333
  }
 
334
  else if (schema)
 
335
  {
 
336
    select->setShowPredicate(*schema, table_ident->table.str);
 
337
  }
 
338
  else
 
339
  {
 
340
    my_error(ER_NO_DB_ERROR, MYF(0));
 
341
    return false;
 
342
  }
 
343
 
 
344
  {
 
345
    drizzled::identifier::Table identifier(select->getShowSchema().c_str(), table_ident->table.str);
 
346
    if (not plugin::StorageEngine::doesTableExist(*session, identifier))
 
347
    {
 
348
      my_error(ER_TABLE_UNKNOWN, identifier);
 
349
    }
 
350
  }
 
351
 
 
352
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_COLUMNS"))
 
353
    return false;
 
354
 
 
355
  if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
 
356
    return false;
 
357
 
 
358
  (session->lex->current_select->with_wild)++;
 
359
 
 
360
  return true;
 
361
}
 
362
 
 
363
void buildSelectWarning(Session *session)
 
364
{
 
365
  (void) create_select_for_variable(session, "warning_count");
 
366
  session->getLex()->statement= new statement::Show(session);
 
367
}
 
368
 
 
369
void buildSelectError(Session *session)
 
370
{
 
371
  (void) create_select_for_variable(session, "error_count");
 
372
  session->getLex()->statement= new statement::Show(session);
 
373
}
 
374
 
 
375
void buildWarnings(Session *session)
 
376
{
 
377
  session->getLex()->statement= new statement::ShowWarnings(session);
 
378
}
 
379
 
 
380
void buildErrors(Session *session)
 
381
{
 
382
  session->getLex()->statement= new statement::ShowErrors(session);
 
383
}
 
384
 
 
385
bool buildIndex(Session *session, const char *schema_ident, Table_ident *table_ident)
 
386
{
 
387
  session->getLex()->sql_command= SQLCOM_SELECT;
 
388
  drizzled::statement::Show *select= new statement::Show(session);
 
389
  session->getLex()->statement= select;
 
390
 
 
391
  util::string::const_shared_ptr schema(session->schema());
 
392
  if (schema_ident)
 
393
  {
 
394
    select->setShowPredicate(schema_ident, table_ident->table.str);
 
395
  }
 
396
  else if (table_ident->db.str)
 
397
  {
 
398
    select->setShowPredicate(table_ident->db.str, table_ident->table.str);
 
399
  }
 
400
  else if (schema)
 
401
  {
 
402
    select->setShowPredicate(*schema, table_ident->table.str);
 
403
  }
 
404
  else
 
405
  {
 
406
    my_error(ER_NO_DB_ERROR, MYF(0));
 
407
    return false;
 
408
  }
 
409
 
 
410
  {
 
411
    drizzled::identifier::Table identifier(select->getShowSchema().c_str(), table_ident->table.str);
 
412
    if (not plugin::StorageEngine::doesTableExist(*session, identifier))
 
413
    {
 
414
      my_error(ER_TABLE_UNKNOWN, identifier);
 
415
    }
 
416
  }
 
417
 
 
418
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_INDEXES"))
 
419
    return false;
 
420
 
 
421
  if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
 
422
    return false;
 
423
 
 
424
  (session->lex->current_select->with_wild)++;
 
425
 
 
426
  return true;
 
427
}
 
428
 
 
429
bool buildStatus(Session *session, const drizzled::sql_var_t is_global)
 
430
{
 
431
  session->getLex()->sql_command= SQLCOM_SELECT;
 
432
  session->getLex()->statement= new statement::Show(session);
 
433
 
 
434
  if (is_global == OPT_GLOBAL)
 
435
  {
 
436
    if (prepare_new_schema_table(session, session->getLex(), "GLOBAL_STATUS"))
 
437
      return false;
 
438
  }
 
439
  else
 
440
  {
 
441
    if (prepare_new_schema_table(session, session->getLex(), "SESSION_STATUS"))
 
442
      return false;
 
443
  }
 
444
 
 
445
  std::string key("Variable_name");
 
446
  std::string value("Value");
 
447
 
 
448
  Item_field *my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "VARIABLE_NAME");
 
449
  my_field->is_autogenerated_name= false;
 
450
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
 
451
 
 
452
  if (session->add_item_to_list(my_field))
 
453
    return false;
 
454
 
 
455
  my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "VARIABLE_VALUE");
 
456
  my_field->is_autogenerated_name= false;
 
457
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
 
458
 
 
459
  if (session->add_item_to_list(my_field))
 
460
    return false;
 
461
 
 
462
  return true;
 
463
}
 
464
 
 
465
bool buildCreateTable(Session *session, Table_ident *ident)
 
466
{
 
467
  session->getLex()->sql_command= SQLCOM_SELECT;
 
468
  statement::Show *select= new statement::Show(session);
 
469
  session->getLex()->statement= select;
 
470
 
 
471
  if (session->getLex()->statement == NULL)
 
472
    return false;
 
473
 
 
474
  if (prepare_new_schema_table(session, session->getLex(), "TABLE_SQL_DEFINITION"))
 
475
    return false;
 
476
 
 
477
  util::string::const_shared_ptr schema(session->schema());
 
478
  if (ident->db.str)
 
479
  {
 
480
    select->setShowPredicate(ident->db.str, ident->table.str);
 
481
  }
 
482
  else if (schema)
 
483
  {
 
484
    select->setShowPredicate(*schema, ident->table.str);
 
485
  }
 
486
  else
 
487
  {
 
488
    my_error(ER_NO_DB_ERROR, MYF(0));
 
489
    return false;
 
490
  }
 
491
 
 
492
  std::string key("Table");
 
493
  std::string value("Create Table");
 
494
 
 
495
  Item_field *my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "TABLE_NAME");
 
496
  my_field->is_autogenerated_name= false;
 
497
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
 
498
 
 
499
  if (session->add_item_to_list(my_field))
 
500
    return false;
 
501
 
 
502
  my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "TABLE_SQL_DEFINITION");
 
503
  my_field->is_autogenerated_name= false;
 
504
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
 
505
 
 
506
  if (session->add_item_to_list(my_field))
 
507
    return false;
 
508
 
 
509
  return true;
 
510
}
 
511
 
 
512
bool buildProcesslist(Session *session)
 
513
{
 
514
  session->getLex()->sql_command= SQLCOM_SELECT;
 
515
  session->getLex()->statement= new statement::Show(session);
 
516
 
 
517
  if (prepare_new_schema_table(session, session->getLex(), "PROCESSLIST"))
 
518
    return false;
 
519
 
 
520
  if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
 
521
    return false;
 
522
 
 
523
  (session->lex->current_select->with_wild)++;
 
524
 
 
525
  return true;
 
526
}
 
527
 
 
528
bool buildVariables(Session *session, const drizzled::sql_var_t is_global)
 
529
{
 
530
  session->getLex()->sql_command= SQLCOM_SELECT;
 
531
  session->getLex()->statement= new statement::Show(session);
 
532
 
 
533
  if (is_global == OPT_GLOBAL)
 
534
  {
 
535
    if (prepare_new_schema_table(session, session->getLex(), "GLOBAL_VARIABLES"))
 
536
      return false;
 
537
  }
 
538
  else
 
539
  {
 
540
    if (prepare_new_schema_table(session, session->getLex(), "SESSION_VARIABLES"))
 
541
      return false;
 
542
  }
 
543
 
 
544
  std::string key("Variable_name");
 
545
  std::string value("Value");
 
546
 
 
547
  Item_field *my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "VARIABLE_NAME");
 
548
  my_field->is_autogenerated_name= false;
 
549
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
 
550
 
 
551
  if (session->add_item_to_list(my_field))
 
552
    return false;
 
553
 
 
554
  my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "VARIABLE_VALUE");
 
555
  my_field->is_autogenerated_name= false;
 
556
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
 
557
 
 
558
  if (session->add_item_to_list(my_field))
 
559
    return false;
 
560
 
 
561
  return true;
 
562
}
 
563
 
 
564
bool buildCreateSchema(Session *session, LEX_STRING &ident)
 
565
{
 
566
  session->getLex()->sql_command= SQLCOM_SELECT;
 
567
  drizzled::statement::Show *select= new statement::Show(session);
 
568
  session->getLex()->statement= select;
 
569
 
 
570
  if (prepare_new_schema_table(session, session->getLex(), "SCHEMA_SQL_DEFINITION"))
 
571
    return false;
 
572
 
 
573
  util::string::const_shared_ptr schema(session->schema());
 
574
  if (ident.str)
 
575
  {
 
576
    select->setShowPredicate(ident.str);
 
577
  }
 
578
  else if (schema)
 
579
  {
 
580
    select->setShowPredicate(*schema);
 
581
  }
 
582
  else
 
583
  {
 
584
    my_error(ER_NO_DB_ERROR, MYF(0));
 
585
    return false;
 
586
  }
 
587
 
 
588
  std::string key("Database");
 
589
  std::string value("Create Database");
 
590
 
 
591
  Item_field *my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "SCHEMA_NAME");
 
592
  my_field->is_autogenerated_name= false;
 
593
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
 
594
 
 
595
  if (session->add_item_to_list(my_field))
 
596
    return false;
 
597
 
 
598
  my_field= new Item_field(&session->lex->current_select->context, NULL, NULL, "SCHEMA_SQL_DEFINITION");
 
599
  my_field->is_autogenerated_name= false;
 
600
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
 
601
 
 
602
  if (session->add_item_to_list(my_field))
 
603
    return false;
 
604
 
 
605
  return true;
 
606
}
 
607
 
 
608
bool buildDescribe(Session *session, Table_ident *ident)
 
609
{
 
610
  session->getLex()->lock_option= TL_READ;
 
611
  init_select(session->getLex());
 
612
  session->getLex()->current_select->parsing_place= SELECT_LIST;
 
613
  session->getLex()->sql_command= SQLCOM_SELECT;
 
614
  drizzled::statement::Show *select= new statement::Show(session);
 
615
  session->getLex()->statement= select;
 
616
  session->getLex()->select_lex.db= 0;
 
617
 
 
618
  util::string::const_shared_ptr schema(session->schema());
 
619
  if (ident->db.str)
 
620
  {
 
621
    select->setShowPredicate(ident->db.str, ident->table.str);
 
622
  }
 
623
  else if (schema)
 
624
  {
 
625
    select->setShowPredicate(*schema, ident->table.str);
 
626
  }
 
627
  else
 
628
  {
 
629
    my_error(ER_NO_DB_ERROR, MYF(0));
 
630
    return false;
 
631
  }
 
632
 
 
633
  {
 
634
    drizzled::identifier::Table identifier(select->getShowSchema().c_str(), ident->table.str);
 
635
    if (not plugin::StorageEngine::doesTableExist(*session, identifier))
 
636
    {
 
637
      my_error(ER_TABLE_UNKNOWN, identifier);
 
638
    }
 
639
  }
 
640
 
 
641
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_COLUMNS"))
 
642
  {
 
643
    return false;
 
644
  }
 
645
 
 
646
  if (session->add_item_to_list( new Item_field(&session->lex->current_select->
 
647
                                                  context,
 
648
                                                  NULL, NULL, "*")))
 
649
  {
 
650
    return false;
 
651
  }
 
652
 
 
653
  (session->lex->current_select->with_wild)++;
 
654
 
 
655
  return true;
 
656
}
 
657
 
 
658
} /* namespace drizzled */
 
659
 
146
660
} /* namespace drizzled */