132
bool buildScemas(Session *session)
134
session->getLex()->sql_command= SQLCOM_SELECT;
135
session->getLex()->statement= new statement::Show(session);
137
std::string column_name= "Database";
138
if (session->getLex()->wild)
140
column_name.append(" (");
141
column_name.append(session->getLex()->wild->ptr());
142
column_name.append(")");
145
if (session->getLex()->current_select->where)
147
if (prepare_new_schema_table(session, session->getLex(), "SCHEMAS"))
152
if (prepare_new_schema_table(session, session->getLex(), "SHOW_SCHEMAS"))
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);
160
if (session->add_item_to_list(my_field))
163
if (session->add_order_to_list(my_field, true))
169
bool buildTables(Session *session, const char *ident)
171
session->getLex()->sql_command= SQLCOM_SELECT;
173
drizzled::statement::Show *select= new statement::Show(session);
174
session->getLex()->statement= select;
176
std::string column_name= "Tables_in_";
178
util::string::const_shared_ptr schema(session->schema());
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))
186
my_error(ER_BAD_DB_ERROR, MYF(0), ident);
188
select->setShowPredicate(ident, "");
190
else if (schema and not schema->empty())
192
column_name.append(*schema);
193
select->setShowPredicate(*schema, "");
197
my_error(ER_NO_DB_ERROR, MYF(0));
202
if (session->getLex()->wild)
204
column_name.append(" (");
205
column_name.append(session->getLex()->wild->ptr());
206
column_name.append(")");
209
if (prepare_new_schema_table(session, session->getLex(), "SHOW_TABLES"))
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);
216
if (session->add_item_to_list(my_field))
219
if (session->add_order_to_list(my_field, true))
225
bool buildTemporaryTables(Session *session)
227
session->getLex()->sql_command= SQLCOM_SELECT;
229
session->getLex()->statement= new statement::Show(session);
232
if (prepare_new_schema_table(session, session->getLex(), "SHOW_TEMPORARY_TABLES"))
235
if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
238
(session->lex->current_select->with_wild)++;
243
bool buildTableStatus(Session *session, const char *ident)
245
session->getLex()->sql_command= SQLCOM_SELECT;
246
drizzled::statement::Show *select= new statement::Show(session);
247
session->getLex()->statement= select;
249
std::string column_name= "Tables_in_";
251
util::string::const_shared_ptr schema(session->schema());
254
session->getLex()->select_lex.db= const_cast<char *>(ident);
256
identifier::Schema identifier(ident);
257
if (not plugin::StorageEngine::doesSchemaExist(identifier))
259
my_error(ER_BAD_DB_ERROR, MYF(0), ident);
262
select->setShowPredicate(ident, "");
266
select->setShowPredicate(*schema, "");
270
my_error(ER_NO_DB_ERROR, MYF(0));
274
if (prepare_new_schema_table(session, session->getLex(), "SHOW_TABLE_STATUS"))
277
if (session->add_item_to_list( new Item_field(&session->lex->current_select->
282
(session->lex->current_select->with_wild)++;
287
bool buildEngineStatus(Session *session, LEX_STRING)
289
session->getLex()->sql_command= SQLCOM_SELECT;
290
drizzled::statement::Show *select= new statement::Show(session);
291
session->getLex()->statement= select;
293
my_error(ER_USE_DATA_DICTIONARY);
297
bool buildColumns(Session *session, const char *schema_ident, Table_ident *table_ident)
299
session->getLex()->sql_command= SQLCOM_SELECT;
301
drizzled::statement::Show *select= new statement::Show(session);
302
session->getLex()->statement= select;
304
util::string::const_shared_ptr schema(session->schema());
307
select->setShowPredicate(schema_ident, table_ident->table.str);
309
else if (table_ident->db.str)
311
select->setShowPredicate(table_ident->db.str, table_ident->table.str);
315
select->setShowPredicate(*schema, table_ident->table.str);
319
my_error(ER_NO_DB_ERROR, MYF(0));
324
drizzled::identifier::Table identifier(select->getShowSchema().c_str(), table_ident->table.str);
325
if (not plugin::StorageEngine::doesTableExist(*session, identifier))
327
my_error(ER_TABLE_UNKNOWN, identifier);
331
if (prepare_new_schema_table(session, session->getLex(), "SHOW_COLUMNS"))
334
if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
337
(session->lex->current_select->with_wild)++;
342
void buildSelectWarning(Session *session)
344
(void) create_select_for_variable(session, "warning_count");
345
session->getLex()->statement= new statement::Show(session);
348
void buildSelectError(Session *session)
350
(void) create_select_for_variable(session, "error_count");
351
session->getLex()->statement= new statement::Show(session);
354
void buildWarnings(Session *session)
356
session->getLex()->statement= new statement::ShowWarnings(session);
359
void buildErrors(Session *session)
361
session->getLex()->statement= new statement::ShowErrors(session);
364
bool buildIndex(Session *session, const char *schema_ident, Table_ident *table_ident)
366
session->getLex()->sql_command= SQLCOM_SELECT;
367
drizzled::statement::Show *select= new statement::Show(session);
368
session->getLex()->statement= select;
370
util::string::const_shared_ptr schema(session->schema());
373
select->setShowPredicate(schema_ident, table_ident->table.str);
375
else if (table_ident->db.str)
377
select->setShowPredicate(table_ident->db.str, table_ident->table.str);
381
select->setShowPredicate(*schema, table_ident->table.str);
385
my_error(ER_NO_DB_ERROR, MYF(0));
390
drizzled::identifier::Table identifier(select->getShowSchema().c_str(), table_ident->table.str);
391
if (not plugin::StorageEngine::doesTableExist(*session, identifier))
393
my_error(ER_TABLE_UNKNOWN, identifier);
397
if (prepare_new_schema_table(session, session->getLex(), "SHOW_INDEXES"))
400
if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
403
(session->lex->current_select->with_wild)++;
408
bool buildStatus(Session *session, const drizzled::sql_var_t is_global)
410
session->getLex()->sql_command= SQLCOM_SELECT;
411
session->getLex()->statement= new statement::Show(session);
413
if (is_global == OPT_GLOBAL)
415
if (prepare_new_schema_table(session, session->getLex(), "GLOBAL_STATUS"))
420
if (prepare_new_schema_table(session, session->getLex(), "SESSION_STATUS"))
424
std::string key("Variable_name");
425
std::string value("Value");
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);
431
if (session->add_item_to_list(my_field))
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);
438
if (session->add_item_to_list(my_field))
444
bool buildCreateTable(Session *session, Table_ident *ident)
446
session->getLex()->sql_command= SQLCOM_SELECT;
447
statement::Show *select= new statement::Show(session);
448
session->getLex()->statement= select;
450
if (session->getLex()->statement == NULL)
453
if (prepare_new_schema_table(session, session->getLex(), "TABLE_SQL_DEFINITION"))
456
util::string::const_shared_ptr schema(session->schema());
459
select->setShowPredicate(ident->db.str, ident->table.str);
463
select->setShowPredicate(*schema, ident->table.str);
467
my_error(ER_NO_DB_ERROR, MYF(0));
471
std::string key("Table");
472
std::string value("Create Table");
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);
478
if (session->add_item_to_list(my_field))
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);
485
if (session->add_item_to_list(my_field))
491
bool buildProcesslist(Session *session)
493
session->getLex()->sql_command= SQLCOM_SELECT;
494
session->getLex()->statement= new statement::Show(session);
496
if (prepare_new_schema_table(session, session->getLex(), "PROCESSLIST"))
499
if (session->add_item_to_list( new Item_field(&session->lex->current_select->context, NULL, NULL, "*")))
502
(session->lex->current_select->with_wild)++;
507
bool buildVariables(Session *session, const drizzled::sql_var_t is_global)
509
session->getLex()->sql_command= SQLCOM_SELECT;
510
session->getLex()->statement= new statement::Show(session);
512
if (is_global == OPT_GLOBAL)
514
if (prepare_new_schema_table(session, session->getLex(), "GLOBAL_VARIABLES"))
519
if (prepare_new_schema_table(session, session->getLex(), "SESSION_VARIABLES"))
523
std::string key("Variable_name");
524
std::string value("Value");
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);
530
if (session->add_item_to_list(my_field))
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);
537
if (session->add_item_to_list(my_field))
543
bool buildCreateSchema(Session *session, LEX_STRING &ident)
545
session->getLex()->sql_command= SQLCOM_SELECT;
546
drizzled::statement::Show *select= new statement::Show(session);
547
session->getLex()->statement= select;
549
if (prepare_new_schema_table(session, session->getLex(), "SCHEMA_SQL_DEFINITION"))
552
util::string::const_shared_ptr schema(session->schema());
555
select->setShowPredicate(ident.str);
559
select->setShowPredicate(*schema);
563
my_error(ER_NO_DB_ERROR, MYF(0));
567
std::string key("Database");
568
std::string value("Create Database");
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);
574
if (session->add_item_to_list(my_field))
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);
581
if (session->add_item_to_list(my_field))
587
bool buildDescribe(Session *session, Table_ident *ident)
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;
597
util::string::const_shared_ptr schema(session->schema());
600
select->setShowPredicate(ident->db.str, ident->table.str);
604
select->setShowPredicate(*schema, ident->table.str);
608
my_error(ER_NO_DB_ERROR, MYF(0));
613
drizzled::identifier::Table identifier(select->getShowSchema().c_str(), ident->table.str);
614
if (not plugin::StorageEngine::doesTableExist(*session, identifier))
616
my_error(ER_TABLE_UNKNOWN, identifier);
620
if (prepare_new_schema_table(session, session->getLex(), "SHOW_COLUMNS"))
625
if (session->add_item_to_list( new Item_field(&session->lex->current_select->
632
(session->lex->current_select->with_wild)++;
637
} /* namespace drizzled */
639
} /* namespace drizzled */
439
/* Append directory name (if exists) to CREATE INFO */
441
static void append_directory(String *packet, const char *dir_type,
442
const char *filename)
446
uint32_t length= dirname_length(filename);
448
packet->append(dir_type);
449
packet->append(STRING_WITH_LEN(" DIRECTORY='"));
450
packet->append(filename, length);
451
packet->append('\'');
456
#define LIST_PROCESS_HOST_LEN 64
458
static bool get_field_default_value(Field *timestamp_field,
459
Field *field, String *def_value,
463
bool has_now_default;
466
We are using CURRENT_TIMESTAMP instead of NOW because it is
469
has_now_default= (timestamp_field == field &&
470
field->unireg_check != Field::TIMESTAMP_UN_FIELD);
472
has_default= (field->type() != DRIZZLE_TYPE_BLOB &&
473
!(field->flags & NO_DEFAULT_VALUE_FLAG) &&
474
field->unireg_check != Field::NEXT_NUMBER);
476
def_value->length(0);
480
def_value->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
481
else if (!field->is_null())
482
{ // Not null by default
483
char tmp[MAX_FIELD_WIDTH];
484
String type(tmp, sizeof(tmp), field->charset());
485
field->val_str(&type);
489
uint32_t dummy_errors;
490
/* convert to system_charset_info == utf8 */
491
def_val.copy(type.ptr(), type.length(), field->charset(),
492
system_charset_info, &dummy_errors);
494
append_unescaped(def_value, def_val.ptr(), def_val.length());
496
def_value->append(def_val.ptr(), def_val.length());
499
def_value->append(STRING_WITH_LEN("''"));
501
else if (field->maybe_null() && quoted)
502
def_value->append(STRING_WITH_LEN("NULL")); // Null as default
510
Build a CREATE TABLE statement for a table.
514
table_list A list containing one table to write statement
516
packet Pointer to a string where statement will be
518
create_info_arg Pointer to create information that can be used
519
to tailor the format of the statement. Can be
520
NULL, in which case only SQL_MODE is considered
521
when building the statement.
524
Currently always return 0, but might return error code in the
531
int store_create_info(TableList *table_list, String *packet, HA_CREATE_INFO *create_info_arg)
533
List<Item> field_list;
534
char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
537
String type(tmp, sizeof(tmp), system_charset_info);
538
String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
540
uint32_t primary_key;
542
Table *table= table_list->table;
543
handler *file= table->file;
544
TableShare *share= table->s;
545
HA_CREATE_INFO create_info;
546
bool show_table_options= false;
547
my_bitmap_map *old_map;
549
table->restoreRecordAsDefault(); // Get empty record
551
if (share->tmp_table)
552
packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
554
packet->append(STRING_WITH_LEN("CREATE TABLE "));
555
if (create_info_arg &&
556
(create_info_arg->options & HA_LEX_CREATE_IF_NOT_EXISTS))
557
packet->append(STRING_WITH_LEN("IF NOT EXISTS "));
558
if (table_list->schema_table)
559
alias= table_list->schema_table->getTableName().c_str();
561
alias= share->table_name.str;
563
packet->append_identifier(alias, strlen(alias));
564
packet->append(STRING_WITH_LEN(" (\n"));
566
We need this to get default values from the table
567
We have to restore the read_set if we are called from insert in case
568
of row based replication.
570
old_map= table->use_all_columns(table->read_set);
572
for (ptr=table->field ; (field= *ptr); ptr++)
574
uint32_t flags = field->flags;
576
if (ptr != table->field)
577
packet->append(STRING_WITH_LEN(",\n"));
579
packet->append(STRING_WITH_LEN(" "));
580
packet->append_identifier(field->field_name, strlen(field->field_name));
582
// check for surprises from the previous call to Field::sql_type()
583
if (type.ptr() != tmp)
584
type.set(tmp, sizeof(tmp), system_charset_info);
586
type.set_charset(system_charset_info);
588
field->sql_type(type);
589
packet->append(type.ptr(), type.length(), system_charset_info);
591
if (field->has_charset())
593
if (field->charset() != share->table_charset)
595
packet->append(STRING_WITH_LEN(" CHARACTER SET "));
596
packet->append(field->charset()->csname);
600
For string types dump collation name only if
601
collation is not primary for the given charset
603
if (!(field->charset()->state & MY_CS_PRIMARY))
605
packet->append(STRING_WITH_LEN(" COLLATE "));
606
packet->append(field->charset()->name);
610
if (flags & NOT_NULL_FLAG)
611
packet->append(STRING_WITH_LEN(" NOT NULL"));
612
else if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
615
TIMESTAMP field require explicit NULL flag, because unlike
616
all other fields they are treated as NOT NULL by default.
618
packet->append(STRING_WITH_LEN(" NULL"));
622
Add field flags about FIELD FORMAT (FIXED or DYNAMIC)
623
and about STORAGE (DISK or MEMORY).
625
enum column_format_type column_format= (enum column_format_type)
626
((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
629
packet->append(STRING_WITH_LEN(" /*!"));
630
packet->append(STRING_WITH_LEN(" COLUMN_FORMAT"));
631
if (column_format == COLUMN_FORMAT_TYPE_FIXED)
632
packet->append(STRING_WITH_LEN(" FIXED */"));
634
packet->append(STRING_WITH_LEN(" DYNAMIC */"));
637
if (get_field_default_value(table->timestamp_field, field, &def_value, 1))
639
packet->append(STRING_WITH_LEN(" DEFAULT "));
640
packet->append(def_value.ptr(), def_value.length(), system_charset_info);
643
if (table->timestamp_field == field && field->unireg_check != Field::TIMESTAMP_DN_FIELD)
644
packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
646
if (field->unireg_check == Field::NEXT_NUMBER)
647
packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
649
if (field->comment.length)
651
packet->append(STRING_WITH_LEN(" COMMENT "));
652
append_unescaped(packet, field->comment.str, field->comment.length);
656
key_info= table->key_info;
657
memset(&create_info, 0, sizeof(create_info));
658
/* Allow update_create_info to update row type */
659
create_info.row_type= share->row_type;
660
file->update_create_info(&create_info);
661
primary_key= share->primary_key;
663
for (uint32_t i=0 ; i < share->keys ; i++,key_info++)
665
KEY_PART_INFO *key_part= key_info->key_part;
666
bool found_primary=0;
667
packet->append(STRING_WITH_LEN(",\n "));
669
if (i == primary_key && is_primary_key(key_info))
673
No space at end, because a space will be added after where the
674
identifier would go, but that is not added for primary key.
676
packet->append(STRING_WITH_LEN("PRIMARY KEY"));
678
else if (key_info->flags & HA_NOSAME)
679
packet->append(STRING_WITH_LEN("UNIQUE KEY "));
681
packet->append(STRING_WITH_LEN("KEY "));
684
packet->append_identifier(key_info->name, strlen(key_info->name));
686
packet->append(STRING_WITH_LEN(" ("));
688
for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
694
packet->append_identifier(key_part->field->field_name,
695
strlen(key_part->field->field_name));
696
if (key_part->field &&
698
table->field[key_part->fieldnr-1]->key_length()))
701
buff.append(to_string((int32_t) key_part->length /
702
key_part->field->charset()->mbmaxlen));
704
packet->append(buff.c_str(), buff.length());
708
store_key_options(packet, table, key_info);
712
Get possible foreign key definitions stored in InnoDB and append them
713
to the CREATE TABLE statement
716
if ((for_str= file->get_foreign_key_create_info()))
718
packet->append(for_str, strlen(for_str));
719
file->free_foreign_key_create_info(for_str);
722
packet->append(STRING_WITH_LEN("\n)"));
724
show_table_options= true;
726
Get possible table space definitions and append them
727
to the CREATE TABLE statement
732
THEN add ENGINE only if it was used when creating the table
734
if (!create_info_arg ||
735
(create_info_arg->used_fields & HA_CREATE_USED_ENGINE))
737
packet->append(STRING_WITH_LEN(" ENGINE="));
738
packet->append(file->engine->getName().c_str());
742
Add AUTO_INCREMENT=... if there is an AUTO_INCREMENT column,
743
and NEXT_ID > 1 (the default). We must not print the clause
744
for engines that do not support this as it would break the
745
import of dumps, but as of this writing, the test for whether
746
AUTO_INCREMENT columns are allowed and wether AUTO_INCREMENT=...
747
is supported is identical, !(file->table_flags() & HA_NO_AUTO_INCREMENT))
748
Because of that, we do not explicitly test for the feature,
749
but may extrapolate its existence from that of an AUTO_INCREMENT column.
752
if (create_info.auto_increment_value > 1)
754
packet->append(STRING_WITH_LEN(" AUTO_INCREMENT="));
755
buff= to_string(create_info.auto_increment_value);
756
packet->append(buff.c_str(), buff.length());
761
packet->append(STRING_WITH_LEN(" MIN_ROWS="));
762
buff= to_string(share->min_rows);
763
packet->append(buff.c_str(), buff.length());
766
if (share->max_rows && !table_list->schema_table)
768
packet->append(STRING_WITH_LEN(" MAX_ROWS="));
769
buff= to_string(share->max_rows);
770
packet->append(buff.c_str(), buff.length());
773
if (share->avg_row_length)
775
packet->append(STRING_WITH_LEN(" AVG_ROW_LENGTH="));
776
buff= to_string(share->avg_row_length);
777
packet->append(buff.c_str(), buff.length());
780
if (share->db_create_options & HA_OPTION_PACK_KEYS)
781
packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
782
if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
783
packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
784
/* We use CHECKSUM, instead of TABLE_CHECKSUM, for backward compability */
785
if (share->db_create_options & HA_OPTION_CHECKSUM)
786
packet->append(STRING_WITH_LEN(" CHECKSUM=1"));
787
if (share->page_checksum != HA_CHOICE_UNDEF)
789
packet->append(STRING_WITH_LEN(" PAGE_CHECKSUM="));
790
packet->append(ha_choice_values[(uint32_t) share->page_checksum], 1);
792
if (share->db_create_options & HA_OPTION_DELAY_KEY_WRITE)
793
packet->append(STRING_WITH_LEN(" DELAY_KEY_WRITE=1"));
794
if (create_info.row_type != ROW_TYPE_DEFAULT)
796
packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
797
packet->append(ha_row_type[(uint32_t) create_info.row_type]);
799
if (table->s->key_block_size)
801
packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
802
buff= to_string(table->s->key_block_size);
803
packet->append(buff.c_str(), buff.length());
805
if (share->block_size)
807
packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
808
buff= to_string(share->block_size);
809
packet->append(buff.c_str(), buff.length());
811
table->file->append_create_info(packet);
812
if (share->comment.length)
814
packet->append(STRING_WITH_LEN(" COMMENT="));
815
append_unescaped(packet, share->comment.str, share->comment.length);
817
if (share->connect_string.length)
819
packet->append(STRING_WITH_LEN(" CONNECTION="));
820
append_unescaped(packet, share->connect_string.str, share->connect_string.length);
822
append_directory(packet, "DATA", create_info.data_file_name);
823
append_directory(packet, "INDEX", create_info.index_file_name);
825
table->restore_column_map(old_map);
829
static void store_key_options(String *packet, Table *table, KEY *key_info)
833
if (key_info->algorithm == HA_KEY_ALG_BTREE)
834
packet->append(STRING_WITH_LEN(" USING BTREE"));
836
if (key_info->algorithm == HA_KEY_ALG_HASH)
837
packet->append(STRING_WITH_LEN(" USING HASH"));
839
if ((key_info->flags & HA_USES_BLOCK_SIZE) &&
840
table->s->key_block_size != key_info->block_size)
842
packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
843
end= int64_t10_to_str(key_info->block_size, buff, 10);
844
packet->append(buff, (uint32_t) (end - buff));
847
assert(test(key_info->flags & HA_USES_COMMENT) ==
848
(key_info->comment.length > 0));
849
if (key_info->flags & HA_USES_COMMENT)
851
packet->append(STRING_WITH_LEN(" COMMENT "));
852
append_unescaped(packet, key_info->comment.str,
853
key_info->comment.length);
858
/****************************************************************************
859
Return info about all processes
860
returns for each thread: thread id, user, host, db, command, info
861
****************************************************************************/
863
class thread_info :public ilink {
865
static void *operator new(size_t size)
867
return (void*) sql_alloc((uint32_t) size);
869
static void operator delete(void *, size_t)
870
{ TRASH(ptr, size); }
872
my_thread_id thread_id;
875
const char *user,*host,*db,*proc_info,*state_info;
879
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
880
template class I_List<thread_info>;
883
void mysqld_list_processes(Session *session,const char *user, bool)
886
List<Item> field_list;
887
I_List<thread_info> thread_infos;
888
Protocol *protocol= session->protocol;
890
field_list.push_back(new Item_int("Id", 0, MY_INT32_NUM_DECIMAL_DIGITS));
891
field_list.push_back(new Item_empty_string("User",16));
892
field_list.push_back(new Item_empty_string("Host",LIST_PROCESS_HOST_LEN));
893
field_list.push_back(field=new Item_empty_string("db",NAME_CHAR_LEN));
894
field->maybe_null= true;
895
field_list.push_back(new Item_empty_string("Command",16));
896
field_list.push_back(new Item_return_int("Time",7, DRIZZLE_TYPE_LONG));
897
field_list.push_back(field=new Item_empty_string("State",30));
898
field->maybe_null= true;
899
field_list.push_back(field=new Item_empty_string("Info", PROCESS_LIST_WIDTH));
900
field->maybe_null= true;
901
if (protocol->sendFields(&field_list,
902
Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
905
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
906
if (!session->killed)
909
for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
912
Security_context *tmp_sctx= &tmp->security_ctx;
913
struct st_my_thread_var *mysys_var;
914
if (tmp->protocol->isConnected() && (!user || (tmp_sctx->user.c_str() && !strcmp(tmp_sctx->user.c_str(), user))))
916
thread_info *session_info= new thread_info;
918
session_info->thread_id=tmp->thread_id;
919
session_info->user= session->strdup(tmp_sctx->user.c_str() ? tmp_sctx->user.c_str() : "unauthenticated user");
920
session_info->host= session->strdup(tmp_sctx->ip.c_str());
921
if ((session_info->db=tmp->db)) // Safe test
922
session_info->db=session->strdup(session_info->db);
923
session_info->command=(int) tmp->command;
924
if ((mysys_var= tmp->mysys_var))
925
pthread_mutex_lock(&mysys_var->mutex);
927
if (tmp->killed == Session::KILL_CONNECTION)
928
session_info->proc_info= (char*) "Killed";
930
session_info->proc_info= command_name[session_info->command].str;
932
session_info->state_info= (char*) (tmp->protocol->isWriting() ?
934
tmp->protocol->isReading() ?
935
(session_info->command == COM_SLEEP ?
936
NULL : "Reading from net") :
937
tmp->get_proc_info() ? tmp->get_proc_info() :
939
tmp->mysys_var->current_cond ?
940
"Waiting on cond" : NULL);
942
pthread_mutex_unlock(&mysys_var->mutex);
944
session_info->start_time= tmp->start_time;
945
session_info->query= NULL;
946
if (tmp->process_list_info[0])
947
session_info->query= session->strdup(tmp->process_list_info);
948
thread_infos.append(session_info);
952
pthread_mutex_unlock(&LOCK_thread_count);
954
thread_info *session_info;
955
time_t now= time(NULL);
956
while ((session_info=thread_infos.get()))
958
protocol->prepareForResend();
959
protocol->store((uint64_t) session_info->thread_id);
960
protocol->store(session_info->user);
961
protocol->store(session_info->host);
962
protocol->store(session_info->db);
963
protocol->store(session_info->proc_info);
965
if (session_info->start_time)
966
protocol->store((uint32_t) (now - session_info->start_time));
970
protocol->store(session_info->state_info);
971
protocol->store(session_info->query);
973
if (protocol->write())
974
break; /* purecov: inspected */
980
/*****************************************************************************
982
*****************************************************************************/
984
static vector<SHOW_VAR *> all_status_vars;
985
static bool status_vars_inited= 0;
986
int show_var_cmp(const void *var1, const void *var2)
988
return strcmp(((SHOW_VAR*)var1)->name, ((SHOW_VAR*)var2)->name);
991
class show_var_cmp_functor
994
show_var_cmp_functor() { }
995
inline bool operator()(const SHOW_VAR *var1, const SHOW_VAR *var2) const
997
int val= strcmp(var1->name, var2->name);
1002
class show_var_remove_if
1005
show_var_remove_if() { }
1006
inline bool operator()(const SHOW_VAR *curr) const
1008
return (curr->type == SHOW_UNDEF);
1012
SHOW_VAR *getFrontOfStatusVars()
1014
return all_status_vars.front();
1018
Adds an array of SHOW_VAR entries to the output of SHOW STATUS
1021
add_status_vars(SHOW_VAR *list)
1022
list - an array of SHOW_VAR entries to add to all_status_vars
1023
the last entry must be {0,0,SHOW_UNDEF}
1026
The handling of all_status_vars[] is completely internal, it's allocated
1027
automatically when something is added to it, and deleted completely when
1028
the last entry is removed.
1030
As a special optimization, if add_status_vars() is called before
1031
init_status_vars(), it assumes "startup mode" - neither concurrent access
1032
to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
1034
int add_status_vars(SHOW_VAR *list)
1037
if (status_vars_inited)
1038
pthread_mutex_lock(&LOCK_status);
1040
all_status_vars.insert(all_status_vars.begin(), list++);
1041
if (status_vars_inited)
1042
sort(all_status_vars.begin(), all_status_vars.end(),
1043
show_var_cmp_functor());
1044
if (status_vars_inited)
1045
pthread_mutex_unlock(&LOCK_status);
1050
Make all_status_vars[] usable for SHOW STATUS
1053
See add_status_vars(). Before init_status_vars() call, add_status_vars()
1054
works in a special fast "startup" mode. Thus init_status_vars()
1055
should be called as late as possible but before enabling multi-threading.
1057
void init_status_vars()
1059
status_vars_inited= 1;
1060
sort(all_status_vars.begin(), all_status_vars.end(),
1061
show_var_cmp_functor());
1064
void reset_status_vars()
1066
vector<SHOW_VAR *>::iterator p= all_status_vars.begin();
1067
while (p != all_status_vars.end())
1069
/* Note that SHOW_LONG_NOFLUSH variables are not reset */
1070
if ((*p)->type == SHOW_LONG)
1077
catch-all cleanup function, cleans up everything no matter what
1080
This function is not strictly required if all add_to_status/
1081
remove_status_vars are properly paired, but it's a safety measure that
1082
deletes everything from the all_status_vars vector even if some
1083
remove_status_vars were forgotten
1085
void free_status_vars()
1087
all_status_vars.clear();
1091
Removes an array of SHOW_VAR entries from the output of SHOW STATUS
1094
remove_status_vars(SHOW_VAR *list)
1095
list - an array of SHOW_VAR entries to remove to all_status_vars
1096
the last entry must be {0,0,SHOW_UNDEF}
1099
there's lots of room for optimizing this, especially in non-sorted mode,
1100
but nobody cares - it may be called only in case of failed plugin
1101
initialization in the mysqld startup.
1104
void remove_status_vars(SHOW_VAR *list)
1106
if (status_vars_inited)
1108
pthread_mutex_lock(&LOCK_status);
1109
SHOW_VAR *all= all_status_vars.front();
1110
int a= 0, b= all_status_vars.size(), c= (a+b)/2;
1112
for (; list->name; list++)
1115
for (a= 0, b= all_status_vars.size(); b-a > 1; c= (a+b)/2)
1117
res= show_var_cmp(list, all+c);
1126
all[c].type= SHOW_UNDEF;
1128
/* removes all the SHOW_UNDEF elements from the vector */
1129
all_status_vars.erase(std::remove_if(all_status_vars.begin(),
1130
all_status_vars.end(),show_var_remove_if()),
1131
all_status_vars.end());
1132
pthread_mutex_unlock(&LOCK_status);
1136
SHOW_VAR *all= all_status_vars.front();
1138
for (; list->name; list++)
1140
for (i= 0; i < all_status_vars.size(); i++)
1142
if (show_var_cmp(list, all+i))
1144
all[i].type= SHOW_UNDEF;
1148
/* removes all the SHOW_UNDEF elements from the vector */
1149
all_status_vars.erase(std::remove_if(all_status_vars.begin(),
1150
all_status_vars.end(),show_var_remove_if()),
1151
all_status_vars.end());
1155
/* collect status for all running threads */
1157
void calc_sum_of_all_status(STATUS_VAR *to)
1159
/* Ensure that thread id not killed during loop */
1160
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1162
/* Get global values as base */
1163
*to= global_status_var;
1165
/* Add to this status from existing threads */
1166
for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
1168
add_to_status(to, &((*it)->status_var));
1171
pthread_mutex_unlock(&LOCK_thread_count);
1176
Store record to I_S table, convert HEAP table
1177
to MyISAM if necessary
1180
schema_table_store_record()
1181
session thread handler
1182
table Information schema table to be updated
1189
bool schema_table_store_record(Session *session, Table *table)
1192
if ((error= table->file->ha_write_row(table->record[0])))
1194
Tmp_Table_Param *param= table->pos_in_table_list->schema_table_param;
1196
if (create_myisam_from_heap(session, table, param->start_recinfo,
1197
¶m->recinfo, error, 0))
1204
static int make_table_list(Session *session, Select_Lex *sel,
1205
LEX_STRING *db_name, LEX_STRING *table_name)
1207
Table_ident *table_ident;
1208
table_ident= new Table_ident(session, *db_name, *table_name, 1);
1210
if (!sel->add_table_to_list(session, table_ident, 0, 0, TL_READ))
1217
@brief Get lookup value from the part of 'WHERE' condition
1219
@details This function gets lookup value from
1220
the part of 'WHERE' condition if it's possible and
1221
fill appropriate lookup_field_vals struct field
1224
@param[in] session thread handler
1225
@param[in] item_func part of WHERE condition
1226
@param[in] table I_S table
1227
@param[in, out] lookup_field_vals Struct which holds lookup values
1231
1 error, there can be no matching records for the condition
1234
static bool get_lookup_value(Session *session, Item_func *item_func,
1236
LOOKUP_FIELD_VALUES *lookup_field_vals)
1238
InfoSchemaTable *schema_table= table->schema_table;
1239
const char *field_name1= schema_table->getFirstColumnIndex() >= 0 ?
1240
schema_table->getColumnName(schema_table->getFirstColumnIndex()).c_str() : "";
1241
const char *field_name2= schema_table->getSecondColumnIndex() >= 0 ?
1242
schema_table->getColumnName(schema_table->getSecondColumnIndex()).c_str() : "";
1244
if (item_func->functype() == Item_func::EQ_FUNC ||
1245
item_func->functype() == Item_func::EQUAL_FUNC)
1247
int idx_field, idx_val;
1248
char tmp[MAX_FIELD_WIDTH];
1249
String *tmp_str, str_buff(tmp, sizeof(tmp), system_charset_info);
1250
Item_field *item_field;
1251
const CHARSET_INFO * const cs= system_charset_info;
1253
if (item_func->arguments()[0]->type() == Item::FIELD_ITEM &&
1254
item_func->arguments()[1]->const_item())
1259
else if (item_func->arguments()[1]->type() == Item::FIELD_ITEM &&
1260
item_func->arguments()[0]->const_item())
1268
item_field= (Item_field*) item_func->arguments()[idx_field];
1269
if (table->table != item_field->field->table)
1271
tmp_str= item_func->arguments()[idx_val]->val_str(&str_buff);
1273
/* impossible value */
1277
/* Lookup value is database name */
1278
if (!cs->coll->strnncollsp(cs, (unsigned char *) field_name1, strlen(field_name1),
1279
(unsigned char *) item_field->field_name,
1280
strlen(item_field->field_name), 0))
1282
session->make_lex_string(&lookup_field_vals->db_value, tmp_str->ptr(),
1283
tmp_str->length(), false);
1285
/* Lookup value is table name */
1286
else if (!cs->coll->strnncollsp(cs, (unsigned char *) field_name2,
1287
strlen(field_name2),
1288
(unsigned char *) item_field->field_name,
1289
strlen(item_field->field_name), 0))
1291
session->make_lex_string(&lookup_field_vals->table_value, tmp_str->ptr(),
1292
tmp_str->length(), false);
1300
@brief Calculates lookup values from 'WHERE' condition
1302
@details This function calculates lookup value(database name, table name)
1303
from 'WHERE' condition if it's possible and
1304
fill lookup_field_vals struct fields with these values.
1306
@param[in] session thread handler
1307
@param[in] cond WHERE condition
1308
@param[in] table I_S table
1309
@param[in, out] lookup_field_vals Struct which holds lookup values
1313
1 error, there can be no matching records for the condition
1316
bool calc_lookup_values_from_cond(Session *session, COND *cond, TableList *table,
1317
LOOKUP_FIELD_VALUES *lookup_field_vals)
1322
if (cond->type() == Item::COND_ITEM)
1324
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1326
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1328
while ((item= li++))
1330
if (item->type() == Item::FUNC_ITEM)
1332
if (get_lookup_value(session, (Item_func*)item, table, lookup_field_vals))
1337
if (calc_lookup_values_from_cond(session, item, table, lookup_field_vals))
1344
else if (cond->type() == Item::FUNC_ITEM &&
1345
get_lookup_value(session, (Item_func*) cond, table, lookup_field_vals))
1351
static bool uses_only_table_name_fields(Item *item, TableList *table)
1353
if (item->type() == Item::FUNC_ITEM)
1355
Item_func *item_func= (Item_func*)item;
1356
for (uint32_t i=0; i<item_func->argument_count(); i++)
1358
if (!uses_only_table_name_fields(item_func->arguments()[i], table))
1362
else if (item->type() == Item::FIELD_ITEM)
1364
Item_field *item_field= (Item_field*)item;
1365
const CHARSET_INFO * const cs= system_charset_info;
1366
InfoSchemaTable *schema_table= table->schema_table;
1367
const char *field_name1= schema_table->getFirstColumnIndex() >= 0 ?
1368
schema_table->getColumnName(schema_table->getFirstColumnIndex()).c_str() : "";
1369
const char *field_name2= schema_table->getSecondColumnIndex() >= 0 ?
1370
schema_table->getColumnName(schema_table->getSecondColumnIndex()).c_str() : "";
1371
if (table->table != item_field->field->table ||
1372
(cs->coll->strnncollsp(cs, (unsigned char *) field_name1, strlen(field_name1),
1373
(unsigned char *) item_field->field_name,
1374
strlen(item_field->field_name), 0) &&
1375
cs->coll->strnncollsp(cs, (unsigned char *) field_name2, strlen(field_name2),
1376
(unsigned char *) item_field->field_name,
1377
strlen(item_field->field_name), 0)))
1380
else if (item->type() == Item::REF_ITEM)
1381
return uses_only_table_name_fields(item->real_item(), table);
1383
if (item->type() == Item::SUBSELECT_ITEM && !item->const_item())
1390
static COND * make_cond_for_info_schema(COND *cond, TableList *table)
1394
if (cond->type() == Item::COND_ITEM)
1396
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1398
/* Create new top level AND item */
1399
Item_cond_and *new_cond=new Item_cond_and;
1402
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1406
Item *fix= make_cond_for_info_schema(item, table);
1408
new_cond->argument_list()->push_back(fix);
1410
switch (new_cond->argument_list()->elements) {
1414
return new_cond->argument_list()->head();
1416
new_cond->quick_fix_field();
1422
Item_cond_or *new_cond=new Item_cond_or;
1425
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1429
Item *fix=make_cond_for_info_schema(item, table);
1432
new_cond->argument_list()->push_back(fix);
1434
new_cond->quick_fix_field();
1435
new_cond->top_level_item();
1440
if (!uses_only_table_name_fields(cond, table))
1447
@brief Calculate lookup values(database name, table name)
1449
@details This function calculates lookup values(database name, table name)
1450
from 'WHERE' condition or wild values (for 'SHOW' commands only)
1451
from LEX struct and fill lookup_field_vals struct field
1454
@param[in] session thread handler
1455
@param[in] cond WHERE condition
1456
@param[in] tables I_S table
1457
@param[in, out] lookup_field_values Struct which holds lookup values
1461
1 error, there can be no matching records for the condition
1464
bool get_lookup_field_values(Session *session, COND *cond, TableList *tables,
1465
LOOKUP_FIELD_VALUES *lookup_field_values)
1467
LEX *lex= session->lex;
1468
const char *wild= lex->wild ? lex->wild->ptr() : NULL;
1469
memset(lookup_field_values, 0, sizeof(LOOKUP_FIELD_VALUES));
1470
switch (lex->sql_command) {
1471
case SQLCOM_SHOW_DATABASES:
1474
lookup_field_values->db_value.str= (char*) wild;
1475
lookup_field_values->db_value.length= strlen(wild);
1476
lookup_field_values->wild_db_value= 1;
1479
case SQLCOM_SHOW_TABLES:
1480
case SQLCOM_SHOW_TABLE_STATUS:
1481
lookup_field_values->db_value.str= lex->select_lex.db;
1482
lookup_field_values->db_value.length=strlen(lex->select_lex.db);
1485
lookup_field_values->table_value.str= (char*)wild;
1486
lookup_field_values->table_value.length= strlen(wild);
1487
lookup_field_values->wild_table_value= 1;
1492
The "default" is for queries over I_S.
1493
All previous cases handle SHOW commands.
1495
return calc_lookup_values_from_cond(session, cond, tables, lookup_field_values);
1502
* Create db names list. Information schema name always is first in list
1504
* @param[in] session Thread handler
1505
* @param[out] files List of db names
1506
* @param[in] wild Wild string
1507
* @param[in] idx_field_vals idx_field_vals->db_name contains db name or
1509
* @param[out] with_i_schema Returns 1 if we added 'IS' name to list
1510
* otherwise returns 0
1515
int make_db_list(Session *session, vector<LEX_STRING*> &files,
1516
LOOKUP_FIELD_VALUES *lookup_field_vals,
1517
bool *with_i_schema)
1519
LEX_STRING *i_s_name_copy= 0;
1520
i_s_name_copy= session->make_lex_string(i_s_name_copy,
1521
INFORMATION_SCHEMA_NAME.c_str(),
1522
INFORMATION_SCHEMA_NAME.length(), true);
1524
if (lookup_field_vals->wild_db_value)
1527
This part of code is only for SHOW DATABASES command.
1528
idx_field_vals->db_value can be 0 when we don't use
1529
LIKE clause (see also get_index_field_values() function)
1531
if (!lookup_field_vals->db_value.str ||
1532
!wild_case_compare(system_charset_info,
1533
INFORMATION_SCHEMA_NAME.c_str(),
1534
lookup_field_vals->db_value.str))
1537
files.push_back(i_s_name_copy);
1539
return (find_files(session, files, NULL, drizzle_data_home,
1540
lookup_field_vals->db_value.str, 1) != FIND_FILES_OK);
1545
If we have db lookup vaule we just add it to list and
1546
exit from the function
1548
if (lookup_field_vals->db_value.str)
1550
if (!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.c_str(),
1551
lookup_field_vals->db_value.str))
1554
files.push_back(i_s_name_copy);
1558
files.push_back(&lookup_field_vals->db_value);
1563
Create list of existing databases. It is used in case
1564
of select from information schema table
1566
files.push_back(i_s_name_copy);
1569
return (find_files(session, files, NULL,
1570
drizzle_data_home, NULL, 1) != FIND_FILES_OK);
1574
class AddSchemaTable : public unary_function<InfoSchemaTable *, bool>
1578
vector<LEX_STRING*> &files;
1581
AddSchemaTable(Session *session_arg, vector<LEX_STRING*> &files_arg, const char *wild_arg)
1582
: session(session_arg), wild(wild_arg), files(files_arg)
1585
result_type operator() (argument_type schema_table)
1587
if (schema_table->isHidden())
1592
const string &schema_table_name= schema_table->getTableName();
1594
if (wild && wild_case_compare(files_charset_info, schema_table_name.c_str(), wild))
1599
LEX_STRING *file_name= 0;
1600
file_name= session->make_lex_string(file_name, schema_table_name.c_str(),
1601
schema_table_name.length(), true);
1602
if (file_name == NULL)
1607
files.push_back(file_name);
1613
static int schema_tables_add(Session *session, vector<LEX_STRING*> &files, const char *wild)
1615
vector<InfoSchemaTable *>::iterator iter= find_if(all_schema_tables.begin(),
1616
all_schema_tables.end(),
1617
AddSchemaTable(session, files, wild));
1619
if (iter != all_schema_tables.end())
1629
@brief Create table names list
1631
@details The function creates the list of table names in
1634
@param[in] session thread handler
1635
@param[in] table_names List of table names in database
1636
@param[in] lex pointer to LEX struct
1637
@param[in] lookup_field_vals pointer to LOOKUP_FIELD_VALUE struct
1638
@param[in] with_i_schema true means that we add I_S tables to list
1639
@param[in] db_name database name
1641
@return Operation status
1643
@retval 1 fatal error
1644
@retval 2 Not fatal error; Safe to ignore this file list
1648
make_table_name_list(Session *session, vector<LEX_STRING*> &table_names, LEX *lex,
1649
LOOKUP_FIELD_VALUES *lookup_field_vals,
1650
bool with_i_schema, LEX_STRING *db_name)
1652
char path[FN_REFLEN];
1653
build_table_filename(path, sizeof(path), db_name->str, "", false);
1654
if (!lookup_field_vals->wild_table_value &&
1655
lookup_field_vals->table_value.str)
1659
if (find_schema_table(lookup_field_vals->table_value.str))
1661
table_names.push_back(&lookup_field_vals->table_value);
1666
table_names.push_back(&lookup_field_vals->table_value);
1672
This call will add all matching the wildcards (if specified) IS tables
1676
return (schema_tables_add(session, table_names,
1677
lookup_field_vals->table_value.str));
1679
string db(db_name->str);
1681
TableNameIterator tniter(db);
1686
err= tniter.next(&table_name);
1690
LEX_STRING *file_name= NULL;
1691
file_name= session->make_lex_string(file_name, table_name.c_str(),
1692
table_name.length(), true);
1693
const char* wild= lookup_field_vals->table_value.str;
1694
if (wild && wild_compare(table_name.c_str(), wild, 0))
1696
table_names.push_back(file_name);
1703
/* who knows what this error condition really does...
1704
anyway, we're keeping behaviour from days of yore */
1705
if (lex->sql_command != SQLCOM_SELECT)
1707
session->clear_error();
1716
@brief Fill I_S table for SHOW COLUMNS|INDEX commands
1718
@param[in] session thread handler
1719
@param[in] tables TableList for I_S table
1720
@param[in] schema_table pointer to I_S structure
1721
@param[in] open_tables_state_backup pointer to Open_tables_state object
1722
which is used to save|restore original
1723
status of variables related to
1726
@return Operation status
1732
fill_schema_show_cols_or_idxs(Session *session, TableList *tables,
1733
InfoSchemaTable *schema_table,
1734
Open_tables_state *open_tables_state_backup)
1736
LEX *lex= session->lex;
1738
LEX_STRING tmp_lex_string, tmp_lex_string1, *db_name, *table_name;
1739
enum_sql_command save_sql_command= lex->sql_command;
1740
TableList *show_table_list= (TableList*) tables->schema_select_lex->
1742
Table *table= tables->table;
1745
lex->all_selects_list= tables->schema_select_lex;
1747
Restore session->temporary_tables to be able to process
1748
temporary tables(only for 'show index' & 'show columns').
1749
This should be changed when processing of temporary tables for
1750
I_S tables will be done.
1752
session->temporary_tables= open_tables_state_backup->temporary_tables;
1754
Let us set fake sql_command so views won't try to merge
1755
themselves into main statement. If we don't do this,
1756
SELECT * from information_schema.xxxx will cause problems.
1757
SQLCOM_SHOW_FIELDS is used because it satisfies 'only_view_structure()'
1759
lex->sql_command= SQLCOM_SHOW_FIELDS;
1760
res= session->open_normal_and_derived_tables(show_table_list, DRIZZLE_LOCK_IGNORE_FLUSH);
1761
lex->sql_command= save_sql_command;
1763
get_all_tables() returns 1 on failure and 0 on success thus
1764
return only these and not the result code of ::process_table()
1766
We should use show_table_list->alias instead of
1767
show_table_list->table_name because table_name
1768
could be changed during opening of I_S tables. It's safe
1769
to use alias because alias contains original table name
1770
in this case(this part of code is used only for
1771
'show columns' & 'show statistics' commands).
1773
table_name= session->make_lex_string(&tmp_lex_string1, show_table_list->alias,
1774
strlen(show_table_list->alias), false);
1775
db_name= session->make_lex_string(&tmp_lex_string, show_table_list->db,
1776
show_table_list->db_length, false);
1779
table->setWriteSet();
1780
error= test(schema_table->processTable(session, show_table_list,
1781
table, res, db_name,
1783
session->temporary_tables= 0;
1784
session->close_tables_for_reopen(&show_table_list);
1791
@brief Fill I_S table for SHOW Table NAMES commands
1793
@param[in] session thread handler
1794
@param[in] table Table struct for I_S table
1795
@param[in] db_name database name
1796
@param[in] table_name table name
1797
@param[in] with_i_schema I_S table if true
1799
@return Operation status
1804
static int fill_schema_table_names(Session *session, Table *table,
1805
LEX_STRING *db_name, LEX_STRING *table_name,
1810
table->field[3]->store(STRING_WITH_LEN("SYSTEM VIEW"),
1811
system_charset_info);
1815
char path[FN_REFLEN];
1816
(void) build_table_filename(path, sizeof(path), db_name->str,
1817
table_name->str, false);
1819
table->field[3]->store(STRING_WITH_LEN("BASE Table"),
1820
system_charset_info);
1822
if (session->is_error() && session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
1824
session->clear_error();
1828
if (schema_table_store_record(session, table))
1835
@brief Get open table method
1837
@details The function calculates the method which will be used
1839
SKIP_OPEN_TABLE - do not open table
1840
OPEN_FRM_ONLY - open FRM file only
1841
OPEN_FULL_TABLE - open FRM, data, index files
1842
@param[in] tables I_S table table_list
1843
@param[in] schema_table I_S table struct
1845
@return return a set of flags
1846
@retval SKIP_OPEN_TABLE | OPEN_FRM_ONLY | OPEN_FULL_TABLE
1849
static uint32_t get_table_open_method(TableList *tables,
1850
InfoSchemaTable *schema_table)
1853
determine which method will be used for table opening
1855
if (schema_table->getRequestedObject() & OPTIMIZE_I_S_TABLE)
1857
Field **ptr, *field;
1858
int table_open_method= 0, field_indx= 0;
1859
for (ptr= tables->table->field; (field= *ptr) ; ptr++)
1861
if (field->isReadSet())
1862
table_open_method|= schema_table->getColumnOpenMethod(field_indx);
1865
return table_open_method;
1867
/* I_S tables which use get_all_tables but can not be optimized */
1868
return (uint32_t) OPEN_FULL_TABLE;
1873
@brief Fill I_S table with data from FRM file only
1875
@param[in] session thread handler
1876
@param[in] table Table struct for I_S table
1877
@param[in] schema_table I_S table struct
1878
@param[in] db_name database name
1879
@param[in] table_name table name
1881
@return Operation status
1882
@retval 0 Table is processed and we can continue
1884
@retval 1 It's view and we have to use
1885
open_tables function for this table
1888
static int fill_schema_table_from_frm(Session *session,TableList *tables,
1889
InfoSchemaTable *schema_table,
1890
LEX_STRING *db_name,
1891
LEX_STRING *table_name)
1893
Table *table= tables->table;
1896
TableList table_list;
1899
char key[MAX_DBKEY_LENGTH];
1900
uint32_t key_length;
1902
memset(&tbl, 0, sizeof(Table));
1904
table_list.table_name= table_name->str;
1905
table_list.db= db_name->str;
1907
key_length= table_list.create_table_def_key(key);
1908
pthread_mutex_lock(&LOCK_open); /* Locking to get table share when filling schema table from FRM */
1909
share= TableShare::getShare(session, &table_list, key, key_length, 0, &error);
1918
table_list.table= &tbl;
1919
res= schema_table->processTable(session, &table_list, table,
1920
res, db_name, table_name);
1922
/* For the moment we just set everything to read */
1923
table->setReadSet();
1925
TableShare::release(share);
1928
pthread_mutex_unlock(&LOCK_open);
1929
session->clear_error();
1936
@brief Fill I_S tables whose data are retrieved
1937
from frm files and storage engine
1939
@details The information schema tables are internally represented as
1940
temporary tables that are filled at query execution time.
1941
Those I_S tables whose data are retrieved
1942
from frm files and storage engine are filled by the function
1943
InfoSchemaMethods::fillTable().
1945
@param[in] session thread handler
1946
@param[in] tables I_S table
1947
@param[in] cond 'WHERE' condition
1949
@return Operation status
1953
int InfoSchemaMethods::fillTable(Session *session, TableList *tables, COND *cond)
1955
LEX *lex= session->lex;
1956
Table *table= tables->table;
1957
Select_Lex *old_all_select_lex= lex->all_selects_list;
1958
enum_sql_command save_sql_command= lex->sql_command;
1959
Select_Lex *lsel= tables->schema_select_lex;
1960
InfoSchemaTable *schema_table= tables->schema_table;
1962
LOOKUP_FIELD_VALUES lookup_field_vals;
1964
vector<LEX_STRING*> db_names, table_names;
1965
COND *partial_cond= 0;
1966
uint32_t derived_tables= lex->derived_tables;
1968
Open_tables_state open_tables_state_backup;
1969
Query_tables_list query_tables_list_backup;
1970
uint32_t table_open_method;
1971
bool old_value= session->no_warnings_for_error;
1974
We should not introduce deadlocks even if we already have some
1975
tables open and locked, since we won't lock tables which we will
1976
open and will ignore possible name-locks for these tables.
1978
session->reset_n_backup_open_tables_state(&open_tables_state_backup);
1980
tables->table_open_method= table_open_method=
1981
get_table_open_method(tables, schema_table);
1983
this branch processes SHOW FIELDS, SHOW INDEXES commands.
1984
see sql_parse.cc, prepare_schema_table() function where
1985
this values are initialized
1987
if (lsel && lsel->table_list.first)
1989
error= fill_schema_show_cols_or_idxs(session, tables, schema_table,
1990
&open_tables_state_backup);
1994
if (get_lookup_field_values(session, cond, tables, &lookup_field_vals))
2000
if (!lookup_field_vals.wild_db_value && !lookup_field_vals.wild_table_value)
2003
if lookup value is empty string then
2004
it's impossible table name or db name
2006
if ((lookup_field_vals.db_value.str && !lookup_field_vals.db_value.str[0]) ||
2007
(lookup_field_vals.table_value.str && !lookup_field_vals.table_value.str[0]))
2014
if (lookup_field_vals.db_value.length &&
2015
!lookup_field_vals.wild_db_value)
2016
tables->has_db_lookup_value= true;
2018
if (lookup_field_vals.table_value.length &&
2019
!lookup_field_vals.wild_table_value)
2020
tables->has_table_lookup_value= true;
2022
if (tables->has_db_lookup_value && tables->has_table_lookup_value)
2025
partial_cond= make_cond_for_info_schema(cond, tables);
2029
/* EXPLAIN SELECT */
2034
table->setWriteSet();
2035
if (make_db_list(session, db_names, &lookup_field_vals, &with_i_schema))
2038
for (vector<LEX_STRING*>::iterator db_name= db_names.begin(); db_name != db_names.end(); ++db_name )
2040
session->no_warnings_for_error= 1;
2041
table_names.clear();
2042
int res= make_table_name_list(session, table_names, lex,
2044
with_i_schema, *db_name);
2046
if (res == 2) /* Not fatal error, continue */
2053
for (vector<LEX_STRING*>::iterator table_name= table_names.begin(); table_name != table_names.end(); ++table_name)
2055
table->restoreRecordAsDefault();
2056
table->field[schema_table->getFirstColumnIndex()]->
2057
store((*db_name)->str, (*db_name)->length, system_charset_info);
2058
table->field[schema_table->getSecondColumnIndex()]->
2059
store((*table_name)->str, (*table_name)->length, system_charset_info);
2061
if (!partial_cond || partial_cond->val_int())
2064
If table is I_S.tables and open_table_method is 0 (eg SKIP_OPEN)
2065
we can skip table opening and we don't have lookup value for
2066
table name or lookup value is wild string(table name list is
2067
already created by make_table_name_list() function).
2069
if (! table_open_method &&
2070
schema_table->getTableName().compare("TABLES") == 0 &&
2071
(! lookup_field_vals.table_value.length ||
2072
lookup_field_vals.wild_table_value))
2074
if (schema_table_store_record(session, table))
2075
goto err; /* Out of space in temporary table */
2079
/* SHOW Table NAMES command */
2080
if (schema_table->getTableName().compare("TABLE_NAMES") == 0)
2082
if (fill_schema_table_names(session, tables->table, *db_name,
2083
*table_name, with_i_schema))
2088
if (!(table_open_method & ~OPEN_FRM_ONLY) &&
2091
if (!fill_schema_table_from_frm(session, tables, schema_table, *db_name,
2096
LEX_STRING tmp_lex_string, orig_db_name;
2098
Set the parent lex of 'sel' because it is needed by
2099
sel.init_query() which is called inside make_table_list.
2101
session->no_warnings_for_error= 1;
2102
sel.parent_lex= lex;
2103
/* db_name can be changed in make_table_list() func */
2104
if (!session->make_lex_string(&orig_db_name, (*db_name)->str,
2105
(*db_name)->length, false))
2108
if (make_table_list(session, &sel, *db_name, *table_name))
2111
TableList *show_table_list= (TableList*) sel.table_list.first;
2112
lex->all_selects_list= &sel;
2113
lex->derived_tables= 0;
2114
lex->sql_command= SQLCOM_SHOW_FIELDS;
2115
show_table_list->i_s_requested_object=
2116
schema_table->getRequestedObject();
2117
res= session->open_normal_and_derived_tables(show_table_list, DRIZZLE_LOCK_IGNORE_FLUSH);
2118
lex->sql_command= save_sql_command;
2120
XXX-> show_table_list has a flag i_is_requested,
2121
and when it's set, open_normal_and_derived_tables()
2122
can return an error without setting an error message
2123
in Session, which is a hack. This is why we have to
2124
check for res, then for session->is_error() only then
2125
for session->main_da.sql_errno().
2127
if (res && session->is_error() &&
2128
session->main_da.sql_errno() == ER_NO_SUCH_TABLE)
2131
Hide error for not existing table.
2132
This error can occur for example when we use
2133
where condition with db name and table name and this
2134
table does not exist.
2137
session->clear_error();
2142
We should use show_table_list->alias instead of
2143
show_table_list->table_name because table_name
2144
could be changed during opening of I_S tables. It's safe
2145
to use alias because alias contains original table name
2148
session->make_lex_string(&tmp_lex_string, show_table_list->alias,
2149
strlen(show_table_list->alias), false);
2150
res= schema_table->processTable(session, show_table_list, table,
2153
session->close_tables_for_reopen(&show_table_list);
2155
assert(!lex->query_tables_own_last);
2162
If we have information schema its always the first table and only
2163
the first table. Reset for other tables.
2171
session->restore_backup_open_tables_state(&open_tables_state_backup);
2172
lex->derived_tables= derived_tables;
2173
lex->all_selects_list= old_all_select_lex;
2174
lex->sql_command= save_sql_command;
2175
session->no_warnings_for_error= old_value;
2181
@brief Store field characteristics into appropriate I_S table columns
2183
@param[in] table I_S table
2184
@param[in] field processed field
2185
@param[in] cs I_S table charset
2186
@param[in] offset offset from beginning of table
2187
to DATE_TYPE column in I_S table
2192
static void store_column_type(Table *table, Field *field,
2193
const CHARSET_INFO * const cs,
2197
int decimals, field_length;
2198
const char *tmp_buff;
2199
char column_type_buff[MAX_FIELD_WIDTH];
2200
String column_type(column_type_buff, sizeof(column_type_buff), cs);
2202
field->sql_type(column_type);
2203
/* DTD_IDENTIFIER column */
2204
table->field[offset + 7]->store(column_type.ptr(), column_type.length(), cs);
2205
table->field[offset + 7]->set_notnull();
2206
tmp_buff= strchr(column_type.ptr(), '(');
2207
/* DATA_TYPE column */
2208
table->field[offset]->store(column_type.ptr(),
2209
(tmp_buff ? tmp_buff - column_type.ptr() :
2210
column_type.length()), cs);
2211
is_blob= (field->type() == DRIZZLE_TYPE_BLOB);
2212
if (field->has_charset() || is_blob ||
2213
field->real_type() == DRIZZLE_TYPE_VARCHAR) // For varbinary type
2215
uint32_t octet_max_length= field->max_display_length();
2216
if (is_blob && octet_max_length != (uint32_t) 4294967295U)
2217
octet_max_length /= field->charset()->mbmaxlen;
2218
int64_t char_max_len= is_blob ?
2219
(int64_t) octet_max_length / field->charset()->mbminlen :
2220
(int64_t) octet_max_length / field->charset()->mbmaxlen;
2221
/* CHARACTER_MAXIMUM_LENGTH column*/
2222
table->field[offset + 1]->store(char_max_len, true);
2223
table->field[offset + 1]->set_notnull();
2224
/* CHARACTER_OCTET_LENGTH column */
2225
table->field[offset + 2]->store((int64_t) octet_max_length, true);
2226
table->field[offset + 2]->set_notnull();
2230
Calculate field_length and decimals.
2231
They are set to -1 if they should not be set (we should return NULL)
2234
decimals= field->decimals();
2235
switch (field->type()) {
2236
case DRIZZLE_TYPE_NEWDECIMAL:
2237
field_length= ((Field_new_decimal*) field)->precision;
2239
case DRIZZLE_TYPE_LONG:
2240
case DRIZZLE_TYPE_LONGLONG:
2241
field_length= field->max_display_length() - 1;
2243
case DRIZZLE_TYPE_DOUBLE:
2244
field_length= field->field_length;
2245
if (decimals == NOT_FIXED_DEC)
2246
decimals= -1; // return NULL
2249
field_length= decimals= -1;
2253
/* NUMERIC_PRECISION column */
2254
if (field_length >= 0)
2256
table->field[offset + 3]->store((int64_t) field_length, true);
2257
table->field[offset + 3]->set_notnull();
2259
/* NUMERIC_SCALE column */
2262
table->field[offset + 4]->store((int64_t) decimals, true);
2263
table->field[offset + 4]->set_notnull();
2265
if (field->has_charset())
2267
/* CHARACTER_SET_NAME column*/
2268
tmp_buff= field->charset()->csname;
2269
table->field[offset + 5]->store(tmp_buff, strlen(tmp_buff), cs);
2270
table->field[offset + 5]->set_notnull();
2271
/* COLLATION_NAME column */
2272
tmp_buff= field->charset()->name;
2273
table->field[offset + 6]->store(tmp_buff, strlen(tmp_buff), cs);
2274
table->field[offset + 6]->set_notnull();
2279
int InfoSchemaMethods::processTable(Session *session, TableList *tables,
2280
Table *table, bool res,
2281
LEX_STRING *db_name,
2282
LEX_STRING *table_name) const
2284
LEX *lex= session->lex;
2285
const char *wild= lex->wild ? lex->wild->ptr() : NULL;
2286
const CHARSET_INFO * const cs= system_charset_info;
2288
TableShare *show_table_share;
2289
Field **ptr, *field, *timestamp_field;
2294
if (lex->sql_command != SQLCOM_SHOW_FIELDS)
2297
I.e. we are in SELECT FROM INFORMATION_SCHEMA.COLUMS
2298
rather than in SHOW COLUMNS
2300
if (session->is_error())
2301
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2302
session->main_da.sql_errno(), session->main_da.message());
2303
session->clear_error();
2309
show_table= tables->table;
2310
show_table_share= show_table->s;
2313
if (tables->schema_table)
2315
ptr= show_table->field;
2316
timestamp_field= show_table->timestamp_field;
2320
ptr= show_table_share->field;
2321
timestamp_field= show_table_share->timestamp_field;
2324
/* For the moment we just set everything to read */
2325
if (!show_table->read_set)
2327
bitmap_set_all(&show_table->def_read_set);
2328
show_table->read_set= &show_table->def_read_set;
2330
show_table->use_all_columns(); // Required for default
2332
for (; (field= *ptr) ; ptr++)
2335
char tmp[MAX_FIELD_WIDTH];
2336
String type(tmp,sizeof(tmp), system_charset_info);
2339
/* to satisfy 'field->val_str' ASSERTs */
2340
field->table= show_table;
2341
show_table->in_use= session;
2343
if (wild && wild[0] &&
2344
wild_case_compare(system_charset_info, field->field_name,wild))
2348
/* Get default row, with all NULL fields set to NULL */
2349
table->restoreRecordAsDefault();
2351
table->field[1]->store(db_name->str, db_name->length, cs);
2352
table->field[2]->store(table_name->str, table_name->length, cs);
2353
table->field[3]->store(field->field_name, strlen(field->field_name),
2355
table->field[4]->store((int64_t) count, true);
2357
if (get_field_default_value(timestamp_field, field, &type, 0))
2359
table->field[5]->store(type.ptr(), type.length(), cs);
2360
table->field[5]->set_notnull();
2362
pos=(unsigned char*) ((field->flags & NOT_NULL_FLAG) ? "NO" : "YES");
2363
table->field[6]->store((const char*) pos,
2364
strlen((const char*) pos), cs);
2365
store_column_type(table, field, cs, 7);
2367
pos=(unsigned char*) ((field->flags & PRI_KEY_FLAG) ? "PRI" :
2368
(field->flags & UNIQUE_KEY_FLAG) ? "UNI" :
2369
(field->flags & MULTIPLE_KEY_FLAG) ? "MUL":"");
2370
table->field[15]->store((const char*) pos,
2371
strlen((const char*) pos), cs);
2374
if (field->unireg_check == Field::NEXT_NUMBER)
2375
table->field[16]->store(STRING_WITH_LEN("auto_increment"), cs);
2376
if (timestamp_field == field &&
2377
field->unireg_check != Field::TIMESTAMP_DN_FIELD)
2378
table->field[16]->store(STRING_WITH_LEN("on update CURRENT_TIMESTAMP"),
2380
table->field[18]->store(field->comment.str, field->comment.length, cs);
2382
enum column_format_type column_format= (enum column_format_type)
2383
((field->flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
2384
pos=(unsigned char*)"Default";
2385
table->field[19]->store((const char*) pos,
2386
strlen((const char*) pos), cs);
2387
pos=(unsigned char*)(column_format == COLUMN_FORMAT_TYPE_DEFAULT ? "Default" :
2388
column_format == COLUMN_FORMAT_TYPE_FIXED ? "Fixed" :
2390
table->field[20]->store((const char*) pos,
2391
strlen((const char*) pos), cs);
2393
if (schema_table_store_record(session, table))
2400
class FindSchemaTableByName : public unary_function<InfoSchemaTable *, bool>
2402
const char *table_name;
2404
FindSchemaTableByName(const char *table_name_arg)
2405
: table_name(table_name_arg) {}
2406
result_type operator() (argument_type schema_table)
2408
return ! my_strcasecmp(system_charset_info,
2409
schema_table->getTableName().c_str(),
2416
Find schema_tables elment by name
2420
table_name table name
2424
# pointer to 'schema_tables' element
2427
InfoSchemaTable *find_schema_table(const char* table_name)
2429
vector<InfoSchemaTable *>::iterator iter=
2430
find_if(all_schema_tables.begin(), all_schema_tables.end(),
2431
FindSchemaTableByName(table_name));
2432
if (iter != all_schema_tables.end())
2441
Table *InfoSchemaMethods::createSchemaTable(Session *session, TableList *table_list)
2447
List<Item> field_list;
2448
const CHARSET_INFO * const cs= system_charset_info;
2449
const InfoSchemaTable::Columns &columns= table_list->schema_table->getColumns();
2450
InfoSchemaTable::Columns::const_iterator iter= columns.begin();
2452
while (iter != columns.end())
2454
const ColumnInfo *column= *iter;
2455
switch (column->getType()) {
2456
case DRIZZLE_TYPE_LONG:
2457
case DRIZZLE_TYPE_LONGLONG:
2458
if (!(item= new Item_return_int(column->getName().c_str(),
2459
column->getLength(),
2461
column->getValue())))
2465
item->unsigned_flag= (column->getFlags() & MY_I_S_UNSIGNED);
2467
case DRIZZLE_TYPE_DATE:
2468
case DRIZZLE_TYPE_TIMESTAMP:
2469
case DRIZZLE_TYPE_DATETIME:
2470
if (!(item=new Item_return_date_time(column->getName().c_str(),
2471
column->getType())))
2476
case DRIZZLE_TYPE_DOUBLE:
2477
if ((item= new Item_float(column->getName().c_str(), 0.0, NOT_FIXED_DEC,
2478
column->getLength())) == NULL)
2481
case DRIZZLE_TYPE_NEWDECIMAL:
2482
if (!(item= new Item_decimal((int64_t) column->getValue(), false)))
2486
item->unsigned_flag= (column->getFlags() & MY_I_S_UNSIGNED);
2487
item->decimals= column->getLength() % 10;
2488
item->max_length= (column->getLength()/100)%100;
2489
if (item->unsigned_flag == 0)
2490
item->max_length+= 1;
2491
if (item->decimals > 0)
2492
item->max_length+= 1;
2493
item->set_name(column->getName().c_str(),
2494
column->getName().length(), cs);
2496
case DRIZZLE_TYPE_BLOB:
2497
if (!(item= new Item_blob(column->getName().c_str(),
2498
column->getLength())))
2504
if (!(item= new Item_empty_string("", column->getLength(), cs)))
2508
item->set_name(column->getName().c_str(),
2509
column->getName().length(), cs);
2512
field_list.push_back(item);
2513
item->maybe_null= (column->getFlags() & MY_I_S_MAYBE_NULL);
2517
Tmp_Table_Param *tmp_table_param =
2518
(Tmp_Table_Param*) (session->alloc(sizeof(Tmp_Table_Param)));
2519
tmp_table_param->init();
2520
tmp_table_param->table_charset= cs;
2521
tmp_table_param->field_count= field_count;
2522
tmp_table_param->schema_table= 1;
2523
Select_Lex *select_lex= session->lex->current_select;
2524
if (!(table= create_tmp_table(session, tmp_table_param,
2525
field_list, (order_st*) 0, 0, 0,
2526
(select_lex->options | session->options |
2527
TMP_TABLE_ALL_COLUMNS),
2528
HA_POS_ERROR, table_list->alias)))
2530
my_bitmap_map* bitmaps=
2531
(my_bitmap_map*) session->alloc(bitmap_buffer_size(field_count));
2532
bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count);
2533
table->read_set= &table->def_read_set;
2534
bitmap_clear_all(table->read_set);
2535
table_list->schema_table_param= tmp_table_param;
2541
For old SHOW compatibility. It is used when
2542
old SHOW doesn't have generated column names
2543
Make list of fields for SHOW
2546
InfoSchemaMethods::oldFormat()
2547
session thread handler
2548
schema_table pointer to 'schema_tables' element
2555
int InfoSchemaMethods::oldFormat(Session *session, InfoSchemaTable *schema_table)
2558
Name_resolution_context *context= &session->lex->select_lex.context;
2559
const InfoSchemaTable::Columns columns= schema_table->getColumns();
2560
InfoSchemaTable::Columns::const_iterator iter= columns.begin();
2562
while (iter != columns.end())
2564
const ColumnInfo *column= *iter;
2565
if (column->getOldName().length() != 0)
2567
Item_field *field= new Item_field(context,
2569
column->getName().c_str());
2572
field->set_name(column->getOldName().c_str(),
2573
column->getOldName().length(),
2574
system_charset_info);
2575
if (session->add_item_to_list(field))
2586
Create information_schema table
2589
mysql_schema_table()
2590
session thread handler
2592
table_list pointer to table_list
2598
bool mysql_schema_table(Session *session, LEX *, TableList *table_list)
2601
if (!(table= table_list->schema_table->createSchemaTable(session, table_list)))
2603
table->s->tmp_table= SYSTEM_TMP_TABLE;
2605
This test is necessary to make
2606
case insensitive file systems +
2607
upper case table names(information schema tables) +
2611
if (table_list->schema_table_name)
2612
table->alias_name_used= my_strcasecmp(table_alias_charset,
2613
table_list->schema_table_name,
2615
table_list->table_name= table->s->table_name.str;
2616
table_list->table_name_length= table->s->table_name.length;
2617
table_list->table= table;
2618
table->next= session->derived_tables;
2619
session->derived_tables= table;
2620
table_list->select_lex->options |= OPTION_SCHEMA_TABLE;
2627
Generate select from information_schema table
2630
make_schema_select()
2631
session thread handler
2632
sel pointer to Select_Lex
2633
schema_table_name name of 'schema_tables' element
2639
bool make_schema_select(Session *session, Select_Lex *sel,
2640
const string& schema_table_name)
2642
InfoSchemaTable *schema_table= find_schema_table(schema_table_name.c_str());
2643
LEX_STRING db, table;
2645
We have to make non const db_name & table_name
2646
because of lower_case_table_names
2648
session->make_lex_string(&db, INFORMATION_SCHEMA_NAME.c_str(),
2649
INFORMATION_SCHEMA_NAME.length(), 0);
2650
session->make_lex_string(&table, schema_table->getTableName().c_str(),
2651
schema_table->getTableName().length(), 0);
2652
if (schema_table->oldFormat(session, schema_table) || /* Handle old syntax */
2653
!sel->add_table_to_list(session, new Table_ident(session, db, table, 0),
2663
Fill temporary schema tables before SELECT
2666
get_schema_tables_result()
2667
join join which use schema tables
2668
executed_place place where I_S table processed
2675
bool get_schema_tables_result(JOIN *join,
2676
enum enum_schema_table_state executed_place)
2678
JoinTable *tmp_join_tab= join->join_tab+join->tables;
2679
Session *session= join->session;
2680
LEX *lex= session->lex;
2683
session->no_warnings_for_error= 1;
2684
for (JoinTable *tab= join->join_tab; tab < tmp_join_tab; tab++)
2686
if (!tab->table || !tab->table->pos_in_table_list)
2689
TableList *table_list= tab->table->pos_in_table_list;
2690
if (table_list->schema_table)
2692
bool is_subselect= (&lex->unit != lex->current_select->master_unit() &&
2693
lex->current_select->master_unit()->item);
2696
/* skip I_S optimizations specific to get_all_tables */
2697
if (session->lex->describe &&
2698
(table_list->schema_table->isOptimizationPossible() != true))
2704
If schema table is already processed and
2705
the statement is not a subselect then
2706
we don't need to fill this table again.
2707
If schema table is already processed and
2708
schema_table_state != executed_place then
2709
table is already processed and
2710
we should skip second data processing.
2712
if (table_list->schema_table_state &&
2713
(!is_subselect || table_list->schema_table_state != executed_place))
2717
if table is used in a subselect and
2718
table has been processed earlier with the same
2719
'executed_place' value then we should refresh the table.
2721
if (table_list->schema_table_state && is_subselect)
2723
table_list->table->file->extra(HA_EXTRA_NO_CACHE);
2724
table_list->table->file->extra(HA_EXTRA_RESET_STATE);
2725
table_list->table->file->ha_delete_all_rows();
2726
free_io_cache(table_list->table);
2727
filesort_free_buffers(table_list->table,1);
2728
table_list->table->null_row= 0;
2731
table_list->table->file->stats.records= 0;
2733
if (table_list->schema_table->fillTable(session, table_list,
2738
tab->read_record.file= table_list->table->file;
2739
table_list->schema_table_state= executed_place;
2742
tab->read_record.file= table_list->table->file;
2743
table_list->schema_table_state= executed_place;
2746
session->no_warnings_for_error= 0;