~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Brian Aker
  • Date: 2011-01-12 06:45:23 UTC
  • mto: (2073.1.4 catalogs)
  • mto: This revision was merged to the branch mainline in revision 2080.
  • Revision ID: brian@tangent.org-20110112064523-rqhptaqbph22qmj1
RemoveĀ customĀ error.

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