-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathsql_show.cc
9483 lines (8344 loc) · 317 KB
/
sql_show.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* Function with list databases, tables or fields */
#include "sql_show.h"
#include "mutex_lock.h" // Mutex_lock
#include "my_dir.h" // MY_DIR
#include "prealloced_array.h" // Prealloced_array
#include "template_utils.h" // delete_container_pointers
#include "auth_common.h" // check_grant_db
#include "datadict.h" // dd_frm_type
#include "debug_sync.h" // DEBUG_SYNC
#include "field.h" // Field
#include "filesort.h" // filesort_free_buffers
#include "item.h" // Item_empty_string
#include "item_cmpfunc.h" // Item_cond
#include "log.h" // sql_print_warning
#include "mysqld_thd_manager.h" // Global_THD_manager
#include "opt_trace.h" // fill_optimizer_trace_info
#include "protocol.h" // Protocol
#include "sp.h" // MYSQL_PROC_FIELD_DB
#include "sp_head.h" // sp_head
#include "sql_audit.h" // audit_global_variable_get
#include "sql_base.h" // close_thread_tables
#include "sql_class.h" // THD
#include "sql_db.h" // check_db_dir_existence
#include "sql_optimizer.h" // JOIN
#include "sql_parse.h" // command_name
#include "sql_plugin.h" // PLUGIN_IS_DELTED
#include "sql_table.h" // filename_to_tablename
#include "sql_time.h" // interval_type_to_name
#include "sql_tmp_table.h" // create_tmp_table
#include "sql_view.h" // open_and_read_view
#include "table_trigger_dispatcher.h" // Table_trigger_dispatcher
#include "trigger.h" // Trigger
#include "trigger_chain.h" // Trigger_chain
#include "trigger_loader.h" // Trigger_loader
#include "tztime.h" // Time_zone
#ifndef EMBEDDED_LIBRARY
#include "events.h" // Events
#include "event_data_objects.h" // Event_timed
#include "event_parse_data.h" // Event_parse_data
#endif
#include "partition_info.h" // partition_info
#include "partitioning/partition_handler.h" // Partition_handler
#include "pfs_file_provider.h"
#include "mysql/psi/mysql_file.h"
#ifndef EMBEDDED_LIBRARY
#include "srv_session.h"
#endif
#include <algorithm>
#include <functional>
using std::max;
using std::min;
#define STR_OR_NIL(S) ((S) ? (S) : "<nil>")
enum enum_i_s_events_fields
{
ISE_EVENT_CATALOG= 0,
ISE_EVENT_SCHEMA,
ISE_EVENT_NAME,
ISE_DEFINER,
ISE_TIME_ZONE,
ISE_EVENT_BODY,
ISE_EVENT_DEFINITION,
ISE_EVENT_TYPE,
ISE_EXECUTE_AT,
ISE_INTERVAL_VALUE,
ISE_INTERVAL_FIELD,
ISE_SQL_MODE,
ISE_STARTS,
ISE_ENDS,
ISE_STATUS,
ISE_ON_COMPLETION,
ISE_CREATED,
ISE_LAST_ALTERED,
ISE_LAST_EXECUTED,
ISE_EVENT_COMMENT,
ISE_ORIGINATOR,
ISE_CLIENT_CS,
ISE_CONNECTION_CL,
ISE_DB_CL
};
static const LEX_STRING trg_action_time_type_names[]=
{
{ C_STRING_WITH_LEN("BEFORE") },
{ C_STRING_WITH_LEN("AFTER") }
};
static const LEX_STRING trg_event_type_names[]=
{
{ C_STRING_WITH_LEN("INSERT") },
{ C_STRING_WITH_LEN("UPDATE") },
{ C_STRING_WITH_LEN("DELETE") }
};
#ifndef NO_EMBEDDED_ACCESS_CHECKS
static const char *grant_names[]={
"select","insert","update","delete","create","drop","reload","shutdown",
"process","file","grant","references","index","alter"};
static TYPELIB grant_types = { sizeof(grant_names)/sizeof(char **),
"grant_types",
grant_names, NULL};
#endif
static void store_key_options(THD *thd, String *packet, TABLE *table,
KEY *key_info);
static void get_cs_converted_string_value(THD *thd,
String *input_str,
String *output_str,
const CHARSET_INFO *cs,
bool use_hex);
static void
append_algorithm(TABLE_LIST *table, String *buff);
static Item * make_cond_for_info_schema(Item *cond, TABLE_LIST *table);
/***************************************************************************
** List all table types supported
***************************************************************************/
static size_t make_version_string(char *buf, size_t buf_length, uint version)
{
return my_snprintf(buf, buf_length, "%d.%d", version>>8,version&0xff);
}
static my_bool show_plugins(THD *thd, plugin_ref plugin,
void *arg)
{
TABLE *table= (TABLE*) arg;
struct st_mysql_plugin *plug= plugin_decl(plugin);
struct st_plugin_dl *plugin_dl= plugin_dlib(plugin);
CHARSET_INFO *cs= system_charset_info;
char version_buf[20];
restore_record(table, s->default_values);
table->field[0]->store(plugin_name(plugin)->str,
plugin_name(plugin)->length, cs);
table->field[1]->store(version_buf,
make_version_string(version_buf, sizeof(version_buf), plug->version),
cs);
switch (plugin_state(plugin)) {
/* case PLUGIN_IS_FREED: does not happen */
case PLUGIN_IS_DELETED:
table->field[2]->store(STRING_WITH_LEN("DELETED"), cs);
break;
case PLUGIN_IS_UNINITIALIZED:
table->field[2]->store(STRING_WITH_LEN("INACTIVE"), cs);
break;
case PLUGIN_IS_READY:
table->field[2]->store(STRING_WITH_LEN("ACTIVE"), cs);
break;
case PLUGIN_IS_DISABLED:
table->field[2]->store(STRING_WITH_LEN("DISABLED"), cs);
break;
default:
DBUG_ASSERT(0);
}
table->field[3]->store(plugin_type_names[plug->type].str,
plugin_type_names[plug->type].length,
cs);
table->field[4]->store(version_buf,
make_version_string(version_buf, sizeof(version_buf),
*(uint *)plug->info), cs);
if (plugin_dl)
{
table->field[5]->store(plugin_dl->dl.str, plugin_dl->dl.length, cs);
table->field[5]->set_notnull();
table->field[6]->store(version_buf,
make_version_string(version_buf, sizeof(version_buf),
plugin_dl->version),
cs);
table->field[6]->set_notnull();
}
else
{
table->field[5]->set_null();
table->field[6]->set_null();
}
if (plug->author)
{
table->field[7]->store(plug->author, strlen(plug->author), cs);
table->field[7]->set_notnull();
}
else
table->field[7]->set_null();
if (plug->descr)
{
table->field[8]->store(plug->descr, strlen(plug->descr), cs);
table->field[8]->set_notnull();
}
else
table->field[8]->set_null();
switch (plug->license) {
case PLUGIN_LICENSE_GPL:
table->field[9]->store(PLUGIN_LICENSE_GPL_STRING,
strlen(PLUGIN_LICENSE_GPL_STRING), cs);
break;
case PLUGIN_LICENSE_BSD:
table->field[9]->store(PLUGIN_LICENSE_BSD_STRING,
strlen(PLUGIN_LICENSE_BSD_STRING), cs);
break;
default:
table->field[9]->store(PLUGIN_LICENSE_PROPRIETARY_STRING,
strlen(PLUGIN_LICENSE_PROPRIETARY_STRING), cs);
break;
}
table->field[9]->set_notnull();
table->field[10]->store(
global_plugin_typelib_names[plugin_load_option(plugin)],
strlen(global_plugin_typelib_names[plugin_load_option(plugin)]),
cs);
return schema_table_store_record(thd, table);
}
int fill_plugins(THD *thd, TABLE_LIST *tables, Item *cond)
{
DBUG_ENTER("fill_plugins");
if (plugin_foreach_with_mask(thd, show_plugins, MYSQL_ANY_PLUGIN,
~PLUGIN_IS_FREED, tables->table))
DBUG_RETURN(1);
DBUG_RETURN(0);
}
/***************************************************************************
List all privileges supported
***************************************************************************/
struct show_privileges_st {
const char *privilege;
const char *context;
const char *comment;
};
static struct show_privileges_st sys_privileges[]=
{
{"Alter", "Tables", "To alter the table"},
{"Alter routine", "Functions,Procedures", "To alter or drop stored functions/procedures"},
{"Create", "Databases,Tables,Indexes", "To create new databases and tables"},
{"Create routine","Databases","To use CREATE FUNCTION/PROCEDURE"},
{"Create temporary tables","Databases","To use CREATE TEMPORARY TABLE"},
{"Create view", "Tables", "To create new views"},
{"Create user", "Server Admin", "To create new users"},
{"Delete", "Tables", "To delete existing rows"},
{"Drop", "Databases,Tables", "To drop databases, tables, and views"},
#ifndef EMBEDDED_LIBRARY
{"Event","Server Admin","To create, alter, drop and execute events"},
#endif
{"Execute", "Functions,Procedures", "To execute stored routines"},
{"File", "File access on server", "To read and write files on the server"},
{"Grant option", "Databases,Tables,Functions,Procedures", "To give to other users those privileges you possess"},
{"Index", "Tables", "To create or drop indexes"},
{"Insert", "Tables", "To insert data into tables"},
{"Lock tables","Databases","To use LOCK TABLES (together with SELECT privilege)"},
{"Process", "Server Admin", "To view the plain text of currently executing queries"},
{"Proxy", "Server Admin", "To make proxy user possible"},
{"References", "Databases,Tables", "To have references on tables"},
{"Reload", "Server Admin", "To reload or refresh tables, logs and privileges"},
{"Replication client","Server Admin","To ask where the slave or master servers are"},
{"Replication slave","Server Admin","To read binary log events from the master"},
{"Select", "Tables", "To retrieve rows from table"},
{"Show databases","Server Admin","To see all databases with SHOW DATABASES"},
{"Show view","Tables","To see views with SHOW CREATE VIEW"},
{"Shutdown","Server Admin", "To shut down the server"},
{"Super","Server Admin","To use KILL thread, SET GLOBAL, CHANGE MASTER, etc."},
{"Trigger","Tables", "To use triggers"},
{"Create tablespace", "Server Admin", "To create/alter/drop tablespaces"},
{"Update", "Tables", "To update existing rows"},
{"Usage","Server Admin","No privileges - allow connect only"},
{NullS, NullS, NullS}
};
bool mysqld_show_privileges(THD *thd)
{
List<Item> field_list;
Protocol *protocol= thd->get_protocol();
DBUG_ENTER("mysqld_show_privileges");
field_list.push_back(new Item_empty_string("Privilege",10));
field_list.push_back(new Item_empty_string("Context",15));
field_list.push_back(new Item_empty_string("Comment",NAME_CHAR_LEN));
if (thd->send_result_metadata(&field_list,
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
DBUG_RETURN(TRUE);
show_privileges_st *privilege= sys_privileges;
for (privilege= sys_privileges; privilege->privilege ; privilege++)
{
protocol->start_row();
protocol->store(privilege->privilege, system_charset_info);
protocol->store(privilege->context, system_charset_info);
protocol->store(privilege->comment, system_charset_info);
if (protocol->end_row())
DBUG_RETURN(TRUE);
}
my_eof(thd);
DBUG_RETURN(FALSE);
}
/** Hash of LEX_STRINGs used to search for ignored db directories. */
static HASH ignore_db_dirs_hash;
/**
An array of LEX_STRING pointers to collect the options at
option parsing time.
*/
typedef Prealloced_array<LEX_STRING *, 16> Ignore_db_dirs_array;
static Ignore_db_dirs_array *ignore_db_dirs_array;
/**
A value for the read only system variable to show a list of
ignored directories.
*/
char *opt_ignore_db_dirs= NULL;
/**
Sets up the data structures for collection of directories at option
processing time.
We need to collect the directories in an array first, because
we need the character sets initialized before setting up the hash.
*/
void ignore_db_dirs_init()
{
ignore_db_dirs_array= new Ignore_db_dirs_array(key_memory_ignored_db);
}
/**
Retrieves the key (the string itself) from the LEX_STRING hash members.
Needed by hash_init().
@param data the data element from the hash
@param out len_ret Placeholder to return the length of the key
@param unused
@return a pointer to the key
*/
static uchar *
db_dirs_hash_get_key(const uchar *data, size_t *len_ret,
my_bool MY_ATTRIBUTE((unused)))
{
LEX_STRING *e= (LEX_STRING *) data;
*len_ret= e->length;
return (uchar *) e->str;
}
/**
Wrap a directory name into a LEX_STRING and push it to the array.
Called at option processing time for each --ignore-db-dir option.
@param path the name of the directory to push
@return state
@retval TRUE failed
@retval FALSE success
*/
bool
push_ignored_db_dir(char *path)
{
LEX_STRING *new_elt;
char *new_elt_buffer;
size_t path_len= strlen(path);
if (!path_len || path_len >= FN_REFLEN)
return true;
// No need to normalize, it's only a directory name, not a path.
if (!my_multi_malloc(key_memory_ignored_db,
0,
&new_elt, sizeof(LEX_STRING),
&new_elt_buffer, path_len + 1,
NullS))
return true;
new_elt->str= new_elt_buffer;
memcpy(new_elt_buffer, path, path_len);
new_elt_buffer[path_len]= 0;
new_elt->length= path_len;
return ignore_db_dirs_array->push_back(new_elt);
}
/**
Clean up the directory ignore options accumulated so far.
Called at option processing time for each --ignore-db-dir option
with an empty argument.
*/
void
ignore_db_dirs_reset()
{
my_free_container_pointers(*ignore_db_dirs_array);
}
/**
Free the directory ignore option variables.
Called at server shutdown.
*/
void
ignore_db_dirs_free()
{
if (opt_ignore_db_dirs)
{
my_free(opt_ignore_db_dirs);
opt_ignore_db_dirs= NULL;
}
ignore_db_dirs_reset();
delete ignore_db_dirs_array;
my_hash_free(&ignore_db_dirs_hash);
}
/**
Initialize the ignore db directories hash and status variable from
the options collected in the array.
Called when option processing is over and the server's in-memory
structures are fully initialized.
@return state
@retval TRUE failed
@retval FALSE success
*/
bool
ignore_db_dirs_process_additions()
{
size_t len;
char *ptr;
DBUG_ASSERT(opt_ignore_db_dirs == NULL);
if (my_hash_init(&ignore_db_dirs_hash,
lower_case_table_names ?
character_set_filesystem : &my_charset_bin,
0, 0, 0, db_dirs_hash_get_key,
my_free,
HASH_UNIQUE,
key_memory_ignored_db))
return true;
/* len starts from 1 because of the terminating zero. */
len= 1;
LEX_STRING **iter;
for (iter= ignore_db_dirs_array->begin();
iter != ignore_db_dirs_array->end(); ++iter)
{
len+= (*iter)->length + 1; // +1 for the comma
}
/* No delimiter for the last directory. */
if (len > 1)
len--;
/* +1 the terminating zero */
ptr= opt_ignore_db_dirs= (char *) my_malloc(key_memory_ignored_db,
len + 1, MYF(0));
if (!ptr)
return true;
/* Make sure we have an empty string to start with. */
*ptr= 0;
for (iter= ignore_db_dirs_array->begin();
iter != ignore_db_dirs_array->end(); ++iter)
{
LEX_STRING *dir= *iter;
if (my_hash_insert(&ignore_db_dirs_hash, (uchar *)dir))
{
/* ignore duplicates from the config file */
if (my_hash_search(&ignore_db_dirs_hash, (uchar *)dir->str, dir->length))
{
sql_print_warning("Duplicate ignore-db-dir directory name '%.*s' "
"found in the config file(s). Ignoring the duplicate.",
(int) dir->length, dir->str);
/*
Free the excess element since the array will just be reset at
the end of the function, not destructed.
*/
my_free(dir);
(*iter)= NULL;
continue;
}
return true;
}
ptr= my_stpnmov(ptr, dir->str, dir->length);
/* It's safe to always do, since the last one will be repalced with a 0 */
*ptr++ = ',';
/*
Set the transferred array element to NULL to avoid double free
in case of error.
*/
(*iter)= NULL;
}
/* get back to the last comma, if there is one */
if (ptr > opt_ignore_db_dirs)
{
ptr--;
DBUG_ASSERT(*ptr == ',');
}
/* make sure the string is terminated */
DBUG_ASSERT(ptr - opt_ignore_db_dirs <= (ptrdiff_t) len);
*ptr= 0;
/*
It's OK to empty the array here as the allocated elements are
referenced through the hash now.
*/
ignore_db_dirs_array->clear();
return false;
}
/**
Check if a directory name is in the hash of ignored directories.
@return search result
@retval TRUE found
@retval FALSE not found
*/
bool
is_in_ignore_db_dirs_list(const char *directory)
{
return ignore_db_dirs_hash.records &&
NULL != my_hash_search(&ignore_db_dirs_hash, (const uchar *) directory,
strlen(directory));
}
/*
find_files() - find files in a given directory.
SYNOPSIS
find_files()
thd thread handler
files put found files in this list
db database name to set in TABLE_LIST structure
path path to database
wild filter for found files
dir read databases in path if TRUE, read .frm files in
database otherwise
RETURN
FIND_FILES_OK success
FIND_FILES_OOM out of memory error
FIND_FILES_DIR no such directory, or directory can't be read
*/
find_files_result
find_files(THD *thd, List<LEX_STRING> *files, const char *db,
const char *path, const char *wild, bool dir, MEM_ROOT *tmp_mem_root)
{
uint i;
MY_DIR *dirp;
MEM_ROOT **root_ptr= NULL, *old_root= NULL;
#ifndef NO_EMBEDDED_ACCESS_CHECKS
uint col_access=thd->col_access;
#endif
size_t wild_length= 0;
TABLE_LIST table_list;
DBUG_ENTER("find_files");
if (wild)
{
if (!wild[0])
wild= 0;
else
wild_length= strlen(wild);
}
if (!(dirp = my_dir(path,MYF(dir ? MY_WANT_STAT : 0))))
{
if (my_errno() == ENOENT)
my_error(ER_BAD_DB_ERROR, MYF(0), db);
else
{
char errbuf[MYSYS_STRERROR_SIZE];
my_error(ER_CANT_READ_DIR, MYF(0), path,
my_errno(), my_strerror(errbuf, sizeof(errbuf), my_errno()));
}
DBUG_RETURN(FIND_FILES_DIR);
}
if (tmp_mem_root)
{
root_ptr= my_thread_get_THR_MALLOC();
old_root= *root_ptr;
*root_ptr= tmp_mem_root;
}
for (i=0 ; i < dirp->number_off_files ; i++)
{
char uname[NAME_LEN + 1]; /* Unencoded name */
FILEINFO *file;
LEX_STRING *file_name= 0;
size_t file_name_len;
char *ext;
file=dirp->dir_entry+i;
if (dir)
{ /* Return databases */
/*
Ignore all the directories having names that start with a dot (.).
This covers '.' and '..' and other cases like e.g. '.mysqlgui'.
Note that since 5.1 database directory names can't start with a
dot (.) thanks to table name encoding.
*/
if (file->name[0] == '.')
continue;
if (!MY_S_ISDIR(file->mystat->st_mode))
continue;
if (is_in_ignore_db_dirs_list(file->name))
continue;
}
else
{
// Return only .frm files which aren't temp files.
if (my_strcasecmp(system_charset_info, ext=fn_rext(file->name),reg_ext) ||
is_prefix(file->name, tmp_file_prefix))
continue;
*ext=0;
}
file_name_len= filename_to_tablename(file->name, uname, sizeof(uname));
if (wild)
{
if (lower_case_table_names)
{
if (my_wildcmp(files_charset_info,
uname, uname + file_name_len,
wild, wild + wild_length,
wild_prefix, wild_one,wild_many))
continue;
}
else if (wild_compare(uname, wild, 0))
continue;
}
#ifndef NO_EMBEDDED_ACCESS_CHECKS
/* Don't show tables where we don't have any privileges */
if (db && !(col_access & TABLE_ACLS))
{
table_list.db= (char*) db;
table_list.db_length= strlen(db);
table_list.table_name= uname;
table_list.table_name_length= file_name_len;
table_list.grant.privilege=col_access;
if (check_grant(thd, TABLE_ACLS, &table_list, TRUE, 1, TRUE))
continue;
}
#endif
if (!(file_name= tmp_mem_root ?
make_lex_string_root(tmp_mem_root, file_name, uname,
file_name_len, TRUE) :
thd->make_lex_string(file_name, uname,
file_name_len, TRUE)) ||
files->push_back(file_name))
{
my_dirend(dirp);
DBUG_RETURN(FIND_FILES_OOM);
}
}
DBUG_PRINT("info",("found: %d files", files->elements));
my_dirend(dirp);
(void) ha_find_files(thd, db, path, wild, dir, files);
if (tmp_mem_root)
*root_ptr= old_root;
DBUG_RETURN(FIND_FILES_OK);
}
/**
An Internal_error_handler that suppresses errors regarding views'
underlying tables that occur during privilege checking within SHOW CREATE
VIEW commands. This happens in the cases when
- A view's underlying table (e.g. referenced in its SELECT list) does not
exist or columns of underlying table are altered. There should not be an
error as no attempt was made to access it per se.
- Access is denied for some table, column, function or stored procedure
such as mentioned above. This error gets raised automatically, since we
can't untangle its access checking from that of the view itself.
*/
class Show_create_error_handler : public Internal_error_handler
{
TABLE_LIST *m_top_view;
bool m_handling;
Security_context *m_sctx;
char m_view_access_denied_message[MYSQL_ERRMSG_SIZE];
const char *m_view_access_denied_message_ptr;
public:
/**
Creates a new Show_create_error_handler for the particular security
context and view.
@thd Thread context, used for security context information if needed.
@top_view The view. We do not verify at this point that top_view is in
fact a view since, alas, these things do not stay constant.
*/
explicit Show_create_error_handler(THD *thd, TABLE_LIST *top_view) :
m_top_view(top_view), m_handling(false),
m_view_access_denied_message_ptr(NULL)
{
m_sctx = MY_TEST(m_top_view->security_ctx) ?
m_top_view->security_ctx : thd->security_context();
}
private:
/**
Lazy instantiation of 'view access denied' message. The purpose of the
Show_create_error_handler is to hide details of underlying tables for
which we have no privileges behind ER_VIEW_INVALID messages. But this
obviously does not apply if we lack privileges on the view itself.
Unfortunately the information about for which table privilege checking
failed is not available at this point. The only way for us to check is by
reconstructing the actual error message and see if it's the same.
*/
const char* get_view_access_denied_message()
{
if (!m_view_access_denied_message_ptr)
{
m_view_access_denied_message_ptr= m_view_access_denied_message;
my_snprintf(m_view_access_denied_message, MYSQL_ERRMSG_SIZE,
ER(ER_TABLEACCESS_DENIED_ERROR), "SHOW VIEW",
m_sctx->priv_user().str,
m_sctx->host_or_ip().str, m_top_view->get_table_name());
}
return m_view_access_denied_message_ptr;
}
public:
virtual bool handle_condition(THD *thd,
uint sql_errno,
const char *sqlstate,
Sql_condition::enum_severity_level *level,
const char *msg)
{
/*
The handler does not handle the errors raised by itself.
At this point we know if top_view is really a view.
*/
if (m_handling || !m_top_view->is_view())
return false;
m_handling= true;
bool is_handled;
switch (sql_errno)
{
case ER_TABLEACCESS_DENIED_ERROR:
if (!strcmp(get_view_access_denied_message(), msg))
{
/* Access to top view is not granted, don't interfere. */
is_handled= false;
break;
}
// Fall through
case ER_COLUMNACCESS_DENIED_ERROR:
// ER_VIEW_NO_EXPLAIN cannot happen here.
case ER_PROCACCESS_DENIED_ERROR:
is_handled= true;
break;
case ER_BAD_FIELD_ERROR:
/*
Established behavior: warn if column of underlying table is altered.
*/
case ER_NO_SUCH_TABLE:
/* Established behavior: warn if underlying tables are missing. */
case ER_SP_DOES_NOT_EXIST:
/* Established behavior: warn if underlying functions are missing. */
push_warning_printf(thd, Sql_condition::SL_WARNING,
ER_VIEW_INVALID,
ER(ER_VIEW_INVALID),
m_top_view->get_db_name(),
m_top_view->get_table_name());
is_handled= true;
break;
default:
is_handled= false;
}
m_handling= false;
return is_handled;
}
};
class Silence_deprecation_warnings : public Internal_error_handler
{
public:
virtual bool handle_condition(THD *thd,
uint sql_errno,
const char* sqlstate,
Sql_condition::enum_severity_level *level,
const char* msg)
{
if (sql_errno == ER_WARN_DEPRECATED_SYNTAX)
return true;
return false;
}
};
bool
mysqld_show_create(THD *thd, TABLE_LIST *table_list)
{
Protocol *protocol= thd->get_protocol();
char buff[2048];
String buffer(buff, sizeof(buff), system_charset_info);
List<Item> field_list;
bool error= TRUE;
DBUG_ENTER("mysqld_show_create");
DBUG_PRINT("enter",("db: %s table: %s",table_list->db,
table_list->table_name));
/*
Metadata locks taken during SHOW CREATE should be released when
the statmement completes as it is an information statement.
*/
MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
/* We want to preserve the tree for views. */
thd->lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_VIEW;
{
/*
If there is an error during processing of an underlying view, an
error message is wanted, but it has to be converted to a warning,
so that execution can continue.
This is handled by the Show_create_error_handler class.
Use open_tables() instead of open_tables_for_query(). If an error occurs,
this will ensure that tables are not closed on error, but remain open
for the rest of the processing of the SHOW statement.
*/
Show_create_error_handler view_error_suppressor(thd, table_list);
thd->push_internal_handler(&view_error_suppressor);
/*
Filter out deprecation warnings caused by deprecation of
the partition engine. The presence of these depend on TDC
cache behavior. Instead, push a warning later to get
deterministic and repeatable behavior.
*/
Silence_deprecation_warnings deprecation_silencer;
thd->push_internal_handler(&deprecation_silencer);
uint counter;
bool open_error= open_tables(thd, &table_list, &counter,
MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL);
if (!open_error && table_list->is_view_or_derived())
{
/*
Prepare result table for view so that we can read the column list.
Notice that Show_create_error_handler remains active, so that any
errors due to missing underlying objects are converted to warnings.
*/
open_error= table_list->resolve_derived(thd, true);
}
thd->pop_internal_handler();
thd->pop_internal_handler();
if (open_error && (thd->killed || thd->is_error()))
goto exit;
}
/* TODO: add environment variables show when it become possible */
if (thd->lex->only_view && !table_list->is_view())
{
my_error(ER_WRONG_OBJECT, MYF(0),
table_list->db, table_list->table_name, "VIEW");
goto exit;
}
buffer.length(0);
if (table_list->is_view())
buffer.set_charset(table_list->view_creation_ctx->get_client_cs());
/*
Push deprecation warnings for non-natively partitioned tables. Done here
instead of in open_binary_frm (silenced by error handler) to get
predictable and repeatable results without having to flush tables.
*/
if (!table_list->is_view() && table_list->table->s->db_type() &&
is_ha_partition_handlerton(table_list->table->s->db_type()))
push_warning_printf(thd, Sql_condition::SL_WARNING,
ER_WARN_DEPRECATED_SYNTAX,
ER_THD(thd,
ER_PARTITION_ENGINE_DEPRECATED_FOR_TABLE),
table_list->db, table_list->table_name);
if ((table_list->is_view() ?
view_store_create_info(thd, table_list, &buffer) :
store_create_info(thd, table_list, &buffer, NULL,
FALSE /* show_database */)))
goto exit;
if (table_list->is_view())
{
field_list.push_back(new Item_empty_string("View",NAME_CHAR_LEN));
field_list.push_back(new Item_empty_string("Create View",
max<uint>(buffer.length(), 1024U)));
field_list.push_back(new Item_empty_string("character_set_client",
MY_CS_NAME_SIZE));
field_list.push_back(new Item_empty_string("collation_connection",
MY_CS_NAME_SIZE));
}
else
{
field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
// 1024 is for not to confuse old clients
field_list.push_back(new Item_empty_string("Create Table",
max<size_t>(buffer.length(), 1024U)));
}
if (thd->send_result_metadata(&field_list,
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
goto exit;
protocol->start_row();
if (table_list->is_view())
protocol->store(table_list->view_name.str, system_charset_info);
else
{
if (table_list->schema_table)
protocol->store(table_list->schema_table->table_name,
system_charset_info);
else