349
307
int store_create_info(TableList *table_list, String *packet, bool is_if_not_exists)
351
List<Item> field_list;
352
char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
355
String type(tmp, sizeof(tmp), system_charset_info);
356
String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
358
uint32_t primary_key;
360
309
Table *table= table_list->table;
361
Cursor *cursor= table->cursor;
362
TableShare *share= table->s;
363
HA_CREATE_INFO create_info;
364
bool show_table_options= false;
365
my_bitmap_map *old_map;
367
311
table->restoreRecordAsDefault(); // Get empty record
369
if (share->tmp_table)
370
packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
372
packet->append(STRING_WITH_LEN("CREATE TABLE "));
373
if (is_if_not_exists)
374
packet->append(STRING_WITH_LEN("IF NOT EXISTS "));
375
alias= share->table_name.str;
377
packet->append_identifier(alias, strlen(alias));
378
packet->append(STRING_WITH_LEN(" (\n"));
380
We need this to get default values from the table
381
We have to restore the read_set if we are called from insert in case
382
of row based replication.
384
old_map= table->use_all_columns(table->read_set);
386
for (ptr=table->field ; (field= *ptr); ptr++)
388
uint32_t flags = field->flags;
390
if (ptr != table->field)
391
packet->append(STRING_WITH_LEN(",\n"));
393
packet->append(STRING_WITH_LEN(" "));
394
packet->append_identifier(field->field_name, strlen(field->field_name));
396
// check for surprises from the previous call to Field::sql_type()
397
if (type.ptr() != tmp)
398
type.set(tmp, sizeof(tmp), system_charset_info);
400
type.set_charset(system_charset_info);
402
field->sql_type(type);
403
packet->append(type.ptr(), type.length(), system_charset_info);
405
if (field->has_charset())
407
if (field->charset() != share->table_charset)
409
packet->append(STRING_WITH_LEN(" CHARACTER SET "));
410
packet->append(field->charset()->csname);
414
For string types dump collation name only if
415
collation is not primary for the given charset
417
if (!(field->charset()->state & MY_CS_PRIMARY))
419
packet->append(STRING_WITH_LEN(" COLLATE "));
420
packet->append(field->charset()->name);
424
if (flags & NOT_NULL_FLAG)
425
packet->append(STRING_WITH_LEN(" NOT NULL"));
426
else if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
429
TIMESTAMP field require explicit NULL flag, because unlike
430
all other fields they are treated as NOT NULL by default.
432
packet->append(STRING_WITH_LEN(" NULL"));
436
Add field flags about FIELD FORMAT (FIXED or DYNAMIC)
437
and about STORAGE (DISK or MEMORY).
439
enum column_format_type column_format= (enum column_format_type)
440
((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
443
packet->append(STRING_WITH_LEN(" /*!"));
444
packet->append(STRING_WITH_LEN(" COLUMN_FORMAT"));
445
if (column_format == COLUMN_FORMAT_TYPE_FIXED)
446
packet->append(STRING_WITH_LEN(" FIXED */"));
448
packet->append(STRING_WITH_LEN(" DYNAMIC */"));
451
if (get_field_default_value(table->timestamp_field, field, &def_value, 1))
453
packet->append(STRING_WITH_LEN(" DEFAULT "));
454
packet->append(def_value.ptr(), def_value.length(), system_charset_info);
457
if (table->timestamp_field == field && field->unireg_check != Field::TIMESTAMP_DN_FIELD)
458
packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
460
if (field->unireg_check == Field::NEXT_NUMBER)
461
packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
463
if (field->comment.length)
465
packet->append(STRING_WITH_LEN(" COMMENT "));
466
append_unescaped(packet, field->comment.str, field->comment.length);
470
key_info= table->key_info;
471
memset(&create_info, 0, sizeof(create_info));
472
/* Allow update_create_info to update row type */
473
create_info.row_type= share->row_type;
474
cursor->update_create_info(&create_info);
475
primary_key= share->primary_key;
477
for (uint32_t i=0 ; i < share->keys ; i++,key_info++)
479
KEY_PART_INFO *key_part= key_info->key_part;
480
bool found_primary=0;
481
packet->append(STRING_WITH_LEN(",\n "));
483
if (i == primary_key && is_primary_key(key_info))
487
No space at end, because a space will be added after where the
488
identifier would go, but that is not added for primary key.
490
packet->append(STRING_WITH_LEN("PRIMARY KEY"));
492
else if (key_info->flags & HA_NOSAME)
493
packet->append(STRING_WITH_LEN("UNIQUE KEY "));
495
packet->append(STRING_WITH_LEN("KEY "));
498
packet->append_identifier(key_info->name, strlen(key_info->name));
500
packet->append(STRING_WITH_LEN(" ("));
502
for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
508
packet->append_identifier(key_part->field->field_name,
509
strlen(key_part->field->field_name));
510
if (key_part->field &&
512
table->field[key_part->fieldnr-1]->key_length()))
515
buff.append(to_string((int32_t) key_part->length /
516
key_part->field->charset()->mbmaxlen));
518
packet->append(buff.c_str(), buff.length());
522
store_key_options(packet, table, key_info);
526
Get possible foreign key definitions stored in InnoDB and append them
527
to the CREATE TABLE statement
530
if ((for_str= cursor->get_foreign_key_create_info()))
532
packet->append(for_str, strlen(for_str));
533
cursor->free_foreign_key_create_info(for_str);
536
packet->append(STRING_WITH_LEN("\n)"));
538
show_table_options= true;
540
Get possible table space definitions and append them
541
to the CREATE TABLE statement
545
We should always store engine since we will now be
546
making sure engines accept options (aka... no
547
dangling arguments for engines.
549
packet->append(STRING_WITH_LEN(" ENGINE="));
550
packet->append(cursor->engine->getName().c_str());
552
if (share->db_create_options & HA_OPTION_PACK_KEYS)
553
packet->append(STRING_WITH_LEN(" PACK_KEYS=1"));
554
if (share->db_create_options & HA_OPTION_NO_PACK_KEYS)
555
packet->append(STRING_WITH_LEN(" PACK_KEYS=0"));
556
if (create_info.row_type != ROW_TYPE_DEFAULT)
558
packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
559
packet->append(ha_row_type[(uint32_t) create_info.row_type]);
561
if (table->s->hasKeyBlockSize())
563
packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
564
buff= to_string(table->s->getKeyBlockSize());
565
packet->append(buff.c_str(), buff.length());
567
if (share->block_size)
569
packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
570
buff= to_string(share->block_size);
571
packet->append(buff.c_str(), buff.length());
573
table->cursor->append_create_info(packet);
574
if (share->hasComment() && share->getCommentLength())
576
packet->append(STRING_WITH_LEN(" COMMENT="));
577
append_unescaped(packet, share->getComment(),
578
share->getCommentLength());
581
table->restore_column_map(old_map);
315
enum drizzled::message::TransformSqlError transform_err;
317
(void)is_if_not_exists;
319
transform_err= message::transformTableDefinitionToSql(*(table->getShare()->getTableProto()),
324
packet->append(create_sql.c_str(), create_sql.length(), default_charset_info);
585
static void store_key_options(String *packet, Table *table, KEY *key_info)
589
if (key_info->algorithm == HA_KEY_ALG_BTREE)
590
packet->append(STRING_WITH_LEN(" USING BTREE"));
592
if (key_info->algorithm == HA_KEY_ALG_HASH)
593
packet->append(STRING_WITH_LEN(" USING HASH"));
595
if ((key_info->flags & HA_USES_BLOCK_SIZE) &&
596
table->s->getKeyBlockSize() != key_info->block_size)
598
packet->append(STRING_WITH_LEN(" KEY_BLOCK_SIZE="));
599
end= internal::int64_t10_to_str(key_info->block_size, buff, 10);
600
packet->append(buff, (uint32_t) (end - buff));
603
assert(test(key_info->flags & HA_USES_COMMENT) ==
604
(key_info->comment.length > 0));
605
if (key_info->flags & HA_USES_COMMENT)
607
packet->append(STRING_WITH_LEN(" COMMENT "));
608
append_unescaped(packet, key_info->comment.str,
609
key_info->comment.length);
614
329
/****************************************************************************
615
330
Return info about all processes
616
331
returns for each thread: thread id, user, host, db, command, info
647
/*****************************************************************************
649
*****************************************************************************/
651
static vector<drizzle_show_var *> all_status_vars;
652
static vector<drizzle_show_var *> com_status_vars;
653
static bool status_vars_inited= 0;
654
static int show_var_cmp(const void *var1, const void *var2)
656
return strcmp(((drizzle_show_var*)var1)->name, ((drizzle_show_var*)var2)->name);
659
class show_var_cmp_functor
662
show_var_cmp_functor() { }
663
inline bool operator()(const drizzle_show_var *var1, const drizzle_show_var *var2) const
665
int val= strcmp(var1->name, var2->name);
670
class show_var_remove_if
673
show_var_remove_if() { }
674
inline bool operator()(const drizzle_show_var *curr) const
676
return (curr->type == SHOW_UNDEF);
680
drizzle_show_var *getFrontOfStatusVars()
682
return all_status_vars.front();
685
drizzle_show_var *getCommandStatusVars()
687
return com_status_vars.front();
691
Adds an array of drizzle_show_var entries to the output of SHOW STATUS
694
add_status_vars(drizzle_show_var *list)
695
list - an array of drizzle_show_var entries to add to all_status_vars
696
the last entry must be {0,0,SHOW_UNDEF}
699
The handling of all_status_vars[] is completely internal, it's allocated
700
automatically when something is added to it, and deleted completely when
701
the last entry is removed.
703
As a special optimization, if add_status_vars() is called before
704
init_status_vars(), it assumes "startup mode" - neither concurrent access
705
to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
707
int add_status_vars(drizzle_show_var *list)
710
if (status_vars_inited)
711
pthread_mutex_lock(&LOCK_status);
713
all_status_vars.insert(all_status_vars.begin(), list++);
714
if (status_vars_inited)
715
sort(all_status_vars.begin(), all_status_vars.end(),
716
show_var_cmp_functor());
717
if (status_vars_inited)
718
pthread_mutex_unlock(&LOCK_status);
722
int add_com_status_vars(drizzle_show_var *list)
727
com_status_vars.insert(com_status_vars.begin(), list++);
728
if (status_vars_inited)
729
sort(com_status_vars.begin(), com_status_vars.end(),
730
show_var_cmp_functor());
736
Make all_status_vars[] usable for SHOW STATUS
739
See add_status_vars(). Before init_status_vars() call, add_status_vars()
740
works in a special fast "startup" mode. Thus init_status_vars()
741
should be called as late as possible but before enabling multi-threading.
743
void init_status_vars()
745
status_vars_inited= 1;
746
sort(all_status_vars.begin(), all_status_vars.end(),
747
show_var_cmp_functor());
748
sort(com_status_vars.begin(), com_status_vars.end(),
749
show_var_cmp_functor());
752
void reset_status_vars()
754
vector<drizzle_show_var *>::iterator p;
756
p= all_status_vars.begin();
757
while (p != all_status_vars.end())
759
/* Note that SHOW_LONG_NOFLUSH variables are not reset */
760
if ((*p)->type == SHOW_LONG)
765
p= com_status_vars.begin();
766
while (p != com_status_vars.end())
768
/* Note that SHOW_LONG_NOFLUSH variables are not reset */
769
if ((*p)->type == SHOW_LONG)
776
catch-all cleanup function, cleans up everything no matter what
779
This function is not strictly required if all add_to_status/
780
remove_status_vars are properly paired, but it's a safety measure that
781
deletes everything from the all_status_vars vector even if some
782
remove_status_vars were forgotten
784
void free_status_vars()
786
all_status_vars.clear();
787
com_status_vars.clear();
791
Removes an array of drizzle_show_var entries from the output of SHOW STATUS
794
remove_status_vars(drizzle_show_var *list)
795
list - an array of drizzle_show_var entries to remove to all_status_vars
796
the last entry must be {0,0,SHOW_UNDEF}
799
there's lots of room for optimizing this, especially in non-sorted mode,
800
but nobody cares - it may be called only in case of failed plugin
801
initialization in the mysqld startup.
804
void remove_status_vars(drizzle_show_var *list)
806
if (status_vars_inited)
808
pthread_mutex_lock(&LOCK_status);
809
drizzle_show_var *all= all_status_vars.front();
810
int a= 0, b= all_status_vars.size(), c= (a+b)/2;
812
for (; list->name; list++)
815
for (a= 0, b= all_status_vars.size(); b-a > 1; c= (a+b)/2)
817
res= show_var_cmp(list, all+c);
826
all[c].type= SHOW_UNDEF;
828
/* removes all the SHOW_UNDEF elements from the vector */
829
all_status_vars.erase(std::remove_if(all_status_vars.begin(),
830
all_status_vars.end(),show_var_remove_if()),
831
all_status_vars.end());
832
pthread_mutex_unlock(&LOCK_status);
836
drizzle_show_var *all= all_status_vars.front();
838
for (; list->name; list++)
840
for (i= 0; i < all_status_vars.size(); i++)
842
if (show_var_cmp(list, all+i))
844
all[i].type= SHOW_UNDEF;
848
/* removes all the SHOW_UNDEF elements from the vector */
849
all_status_vars.erase(std::remove_if(all_status_vars.begin(),
850
all_status_vars.end(),show_var_remove_if()),
851
all_status_vars.end());
855
/* collect status for all running threads */
857
void calc_sum_of_all_status(system_status_var *to)
859
/* Ensure that thread id not killed during loop */
860
pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
862
/* Get global values as base */
863
*to= global_status_var;
865
/* Add to this status from existing threads */
866
for(SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
868
add_to_status(to, &((*it)->status_var));
871
pthread_mutex_unlock(&LOCK_thread_count);
875
362
} /* namespace drizzled */