~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Brian Aker
  • Date: 2011-02-12 06:56:00 UTC
  • mto: This revision was merged to the branch mainline in revision 2161.
  • Revision ID: brian@tangent.org-20110212065600-m6c68fybw51rflhj
Further strip out includes.

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