~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Stewart Smith
  • Date: 2010-06-04 02:51:04 UTC
  • mto: This revision was merged to the branch mainline in revision 1590.
  • Revision ID: stewart@flamingspork.com-20100604025104-ilf5mrvwbpd5crzv
have a constant for the maximum number of enum elements

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <drizzled/lock.h>
40
40
#include <drizzled/item/return_date_time.h>
41
41
#include <drizzled/item/empty_string.h>
42
 
#include "drizzled/session/cache.h"
 
42
#include "drizzled/session_list.h"
43
43
#include <drizzled/message/schema.pb.h>
44
44
#include <drizzled/plugin/client.h>
45
45
#include <drizzled/cached_directory.h>
48
48
#include "drizzled/pthread_globals.h"
49
49
#include "drizzled/internal/m_string.h"
50
50
#include "drizzled/internal/my_sys.h"
51
 
#include "drizzled/message/statement_transform.h"
52
 
 
53
51
 
54
52
#include <sys/stat.h>
55
53
 
70
68
  return str ? str : "<nil>";
71
69
}
72
70
 
 
71
static void store_key_options(String *packet, Table *table, KeyInfo *key_info);
 
72
 
 
73
 
73
74
int wild_case_compare(const CHARSET_INFO * const cs, const char *str, const char *wildstr)
74
75
{
75
76
  register int flag;
118
119
  return (*str != '\0');
119
120
}
120
121
 
 
122
 
 
123
bool drizzled_show_create(Session *session, TableList *table_list, bool is_if_not_exists)
 
124
{
 
125
  char buff[2048];
 
126
  String buffer(buff, sizeof(buff), system_charset_info);
 
127
 
 
128
  /* Only one table for now, but VIEW can involve several tables */
 
129
  if (session->openTables(table_list))
 
130
  {
 
131
    if (session->is_error())
 
132
      return true;
 
133
 
 
134
    /*
 
135
      Clear all messages with 'error' level status and
 
136
      issue a warning with 'warning' level status in
 
137
      case of invalid view and last error is ER_VIEW_INVALID
 
138
    */
 
139
    drizzle_reset_errors(session, true);
 
140
    session->clear_error();
 
141
  }
 
142
 
 
143
  buffer.length(0);
 
144
 
 
145
  if (store_create_info(table_list, &buffer, is_if_not_exists))
 
146
    return true;
 
147
 
 
148
  List<Item> field_list;
 
149
  {
 
150
    field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
 
151
    // 1024 is for not to confuse old clients
 
152
    field_list.push_back(new Item_empty_string("Create Table",
 
153
                                               max(buffer.length(),(uint32_t)1024)));
 
154
  }
 
155
 
 
156
  if (session->client->sendFields(&field_list))
 
157
    return true;
 
158
  {
 
159
    session->client->store(table_list->table->alias);
 
160
  }
 
161
 
 
162
  session->client->store(buffer.ptr(), buffer.length());
 
163
 
 
164
  if (session->client->flush())
 
165
    return true;
 
166
 
 
167
  session->my_eof();
 
168
  return false;
 
169
}
 
170
 
 
171
/**
 
172
  Get a CREATE statement for a given database.
 
173
 
 
174
  The database is identified by its name, passed as @c dbname parameter.
 
175
  The name should be encoded using the system character set (UTF8 currently).
 
176
 
 
177
  Resulting statement is stored in the string pointed by @c buffer. The string
 
178
  is emptied first and its character set is set to the system character set.
 
179
 
 
180
  If is_if_not_exists is set, then
 
181
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
 
182
  in @c create_options are ignored.
 
183
 
 
184
  @param  session           The current thread instance.
 
185
  @param  dbname        The name of the database.
 
186
  @param  buffer        A String instance where the statement is stored.
 
187
  @param  create_info   If not NULL, the options member influences the resulting
 
188
                        CRATE statement.
 
189
 
 
190
  @returns true if errors are detected, false otherwise.
 
191
*/
 
192
 
 
193
static bool store_db_create_info(SchemaIdentifier &schema_identifier, string &buffer, bool if_not_exists)
 
194
{
 
195
  message::Schema schema;
 
196
 
 
197
  bool found= plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema);
 
198
  if (not found)
 
199
    return false;
 
200
 
 
201
  buffer.append("CREATE DATABASE ");
 
202
 
 
203
  if (if_not_exists)
 
204
    buffer.append("IF NOT EXISTS ");
 
205
 
 
206
  buffer.append("`");
 
207
  buffer.append(schema.name());
 
208
  buffer.append("`");
 
209
 
 
210
  if (schema.has_collation())
 
211
  {
 
212
    buffer.append(" COLLATE = ");
 
213
    buffer.append(schema.collation());
 
214
  }
 
215
 
 
216
  return true;
 
217
}
 
218
 
 
219
bool mysqld_show_create_db(Session &session, SchemaIdentifier &schema_identifier, bool if_not_exists)
 
220
{
 
221
  message::Schema schema_message;
 
222
  string buffer;
 
223
 
 
224
  if (not plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema_message))
 
225
  {
 
226
    /*
 
227
      This assumes that the only reason for which store_db_create_info()
 
228
      can fail is incorrect database name (which is the case now).
 
229
    */
 
230
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
 
231
    return true;
 
232
  }
 
233
 
 
234
  if (not store_db_create_info(schema_identifier, buffer, if_not_exists))
 
235
  {
 
236
    /*
 
237
      This assumes that the only reason for which store_db_create_info()
 
238
      can fail is incorrect database name (which is the case now).
 
239
    */
 
240
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
 
241
    return true;
 
242
  }
 
243
 
 
244
  List<Item> field_list;
 
245
  field_list.push_back(new Item_empty_string("Database",NAME_CHAR_LEN));
 
246
  field_list.push_back(new Item_empty_string("Create Database",1024));
 
247
 
 
248
  if (session.client->sendFields(&field_list))
 
249
    return true;
 
250
 
 
251
  session.client->store(schema_message.name());
 
252
  session.client->store(buffer);
 
253
 
 
254
  if (session.client->flush())
 
255
    return true;
 
256
 
 
257
  session.my_eof();
 
258
 
 
259
  return false;
 
260
}
 
261
 
121
262
/*
122
263
  Get the quote character for displaying an identifier.
123
264
 
143
284
  return '`';
144
285
}
145
286
 
 
287
 
 
288
#define LIST_PROCESS_HOST_LEN 64
 
289
 
 
290
static bool get_field_default_value(Field *timestamp_field,
 
291
                                    Field *field, String *def_value,
 
292
                                    bool quoted)
 
293
{
 
294
  bool has_default;
 
295
  bool has_now_default;
 
296
 
 
297
  /*
 
298
     We are using CURRENT_TIMESTAMP instead of NOW because it is
 
299
     more standard
 
300
  */
 
301
  has_now_default= (timestamp_field == field &&
 
302
                    field->unireg_check != Field::TIMESTAMP_UN_FIELD);
 
303
 
 
304
  has_default= (field->type() != DRIZZLE_TYPE_BLOB &&
 
305
                !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
 
306
                field->unireg_check != Field::NEXT_NUMBER);
 
307
 
 
308
  def_value->length(0);
 
309
  if (has_default)
 
310
  {
 
311
    if (has_now_default)
 
312
      def_value->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
 
313
    else if (!field->is_null())
 
314
    {                                             // Not null by default
 
315
      char tmp[MAX_FIELD_WIDTH];
 
316
      String type(tmp, sizeof(tmp), field->charset());
 
317
      field->val_str(&type);
 
318
      if (type.length())
 
319
      {
 
320
        String def_val;
 
321
        uint32_t dummy_errors;
 
322
        /* convert to system_charset_info == utf8 */
 
323
        def_val.copy(type.ptr(), type.length(), field->charset(),
 
324
                     system_charset_info, &dummy_errors);
 
325
        if (quoted)
 
326
          append_unescaped(def_value, def_val.ptr(), def_val.length());
 
327
        else
 
328
          def_value->append(def_val.ptr(), def_val.length());
 
329
      }
 
330
      else if (quoted)
 
331
        def_value->append(STRING_WITH_LEN("''"));
 
332
    }
 
333
    else if (field->maybe_null() && quoted)
 
334
      def_value->append(STRING_WITH_LEN("NULL"));    // Null as default
 
335
    else
 
336
      return false;
 
337
  }
 
338
  return has_default;
 
339
}
 
340
 
 
341
/*
 
342
  Build a CREATE TABLE statement for a table.
 
343
 
 
344
  SYNOPSIS
 
345
    store_create_info()
 
346
    table_list        A list containing one table to write statement
 
347
                      for.
 
348
    packet            Pointer to a string where statement will be
 
349
                      written.
 
350
 
 
351
  NOTE
 
352
    Currently always return 0, but might return error code in the
 
353
    future.
 
354
 
 
355
  RETURN
 
356
    0       OK
 
357
 */
 
358
 
 
359
int store_create_info(TableList *table_list, String *packet, bool is_if_not_exists)
 
360
{
 
361
  List<Item> field_list;
 
362
  char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
 
363
  const char *alias;
 
364
  string buff;
 
365
  String type(tmp, sizeof(tmp), system_charset_info);
 
366
  String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
 
367
  Field **ptr,*field;
 
368
  uint32_t primary_key;
 
369
  KeyInfo *key_info;
 
370
  Table *table= table_list->table;
 
371
  Cursor *cursor= table->cursor;
 
372
  HA_CREATE_INFO create_info;
 
373
  my_bitmap_map *old_map;
 
374
 
 
375
  table->restoreRecordAsDefault(); // Get empty record
 
376
 
 
377
  if (table->getShare()->tmp_table)
 
378
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
 
379
  else
 
380
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
 
381
  if (is_if_not_exists)
 
382
    packet->append(STRING_WITH_LEN("IF NOT EXISTS "));
 
383
  alias= table->getShare()->getTableName();
 
384
 
 
385
  packet->append_identifier(alias, strlen(alias));
 
386
  packet->append(STRING_WITH_LEN(" (\n"));
 
387
  /*
 
388
    We need this to get default values from the table
 
389
    We have to restore the read_set if we are called from insert in case
 
390
    of row based replication.
 
391
  */
 
392
  old_map= table->use_all_columns(table->read_set);
 
393
 
 
394
  for (ptr=table->field ; (field= *ptr); ptr++)
 
395
  {
 
396
    uint32_t flags = field->flags;
 
397
 
 
398
    if (ptr != table->field)
 
399
      packet->append(STRING_WITH_LEN(",\n"));
 
400
 
 
401
    packet->append(STRING_WITH_LEN("  "));
 
402
    packet->append_identifier(field->field_name, strlen(field->field_name));
 
403
    packet->append(' ');
 
404
    // check for surprises from the previous call to Field::sql_type()
 
405
    if (type.ptr() != tmp)
 
406
      type.set(tmp, sizeof(tmp), system_charset_info);
 
407
    else
 
408
      type.set_charset(system_charset_info);
 
409
 
 
410
    field->sql_type(type);
 
411
    packet->append(type.ptr(), type.length(), system_charset_info);
 
412
 
 
413
    if (field->has_charset())
 
414
    {
 
415
      /*
 
416
        For string types dump collation name only if
 
417
        collation is not primary for the given charset
 
418
      */
 
419
      if (!(field->charset()->state & MY_CS_PRIMARY))
 
420
      {
 
421
        packet->append(STRING_WITH_LEN(" COLLATE "));
 
422
        packet->append(field->charset()->name);
 
423
      }
 
424
    }
 
425
 
 
426
    if (flags & NOT_NULL_FLAG)
 
427
      packet->append(STRING_WITH_LEN(" NOT NULL"));
 
428
    else if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
429
    {
 
430
      /*
 
431
        TIMESTAMP field require explicit NULL flag, because unlike
 
432
        all other fields they are treated as NOT NULL by default.
 
433
      */
 
434
      packet->append(STRING_WITH_LEN(" NULL"));
 
435
    }
 
436
    {
 
437
      /*
 
438
        Add field flags about FIELD FORMAT (FIXED or DYNAMIC)
 
439
        and about STORAGE (DISK or MEMORY).
 
440
      */
 
441
      enum column_format_type column_format= (enum column_format_type)
 
442
        ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
 
443
      if (column_format)
 
444
      {
 
445
        packet->append(STRING_WITH_LEN(" /*!"));
 
446
        packet->append(STRING_WITH_LEN(" COLUMN_FORMAT"));
 
447
        if (column_format == COLUMN_FORMAT_TYPE_FIXED)
 
448
          packet->append(STRING_WITH_LEN(" FIXED */"));
 
449
        else
 
450
          packet->append(STRING_WITH_LEN(" DYNAMIC */"));
 
451
      }
 
452
    }
 
453
    if (get_field_default_value(table->timestamp_field, field, &def_value, 1))
 
454
    {
 
455
      packet->append(STRING_WITH_LEN(" DEFAULT "));
 
456
      packet->append(def_value.ptr(), def_value.length(), system_charset_info);
 
457
    }
 
458
 
 
459
    if (table->timestamp_field == field && field->unireg_check != Field::TIMESTAMP_DN_FIELD)
 
460
      packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
 
461
 
 
462
    if (field->unireg_check == Field::NEXT_NUMBER)
 
463
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
 
464
 
 
465
    if (field->comment.length)
 
466
    {
 
467
      packet->append(STRING_WITH_LEN(" COMMENT "));
 
468
      append_unescaped(packet, field->comment.str, field->comment.length);
 
469
    }
 
470
  }
 
471
 
 
472
  key_info= table->key_info;
 
473
  memset(&create_info, 0, sizeof(create_info));
 
474
  /* Allow update_create_info to update row type */
 
475
  create_info.row_type= table->getShare()->row_type;
 
476
  cursor->update_create_info(&create_info);
 
477
  primary_key= table->getShare()->primary_key;
 
478
 
 
479
  for (uint32_t i=0 ; i < table->getShare()->keys ; i++,key_info++)
 
480
  {
 
481
    KeyPartInfo *key_part= key_info->key_part;
 
482
    bool found_primary=0;
 
483
    packet->append(STRING_WITH_LEN(",\n  "));
 
484
 
 
485
    if (i == primary_key && is_primary_key(key_info))
 
486
    {
 
487
      found_primary=1;
 
488
      /*
 
489
        No space at end, because a space will be added after where the
 
490
        identifier would go, but that is not added for primary key.
 
491
      */
 
492
      packet->append(STRING_WITH_LEN("PRIMARY KEY"));
 
493
    }
 
494
    else if (key_info->flags & HA_NOSAME)
 
495
      packet->append(STRING_WITH_LEN("UNIQUE KEY "));
 
496
    else
 
497
      packet->append(STRING_WITH_LEN("KEY "));
 
498
 
 
499
    if (!found_primary)
 
500
     packet->append_identifier(key_info->name, strlen(key_info->name));
 
501
 
 
502
    packet->append(STRING_WITH_LEN(" ("));
 
503
 
 
504
    for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
 
505
    {
 
506
      if (j)
 
507
        packet->append(',');
 
508
 
 
509
      if (key_part->field)
 
510
        packet->append_identifier(key_part->field->field_name,
 
511
                                  strlen(key_part->field->field_name));
 
512
      if (key_part->field &&
 
513
          (key_part->length !=
 
514
           table->field[key_part->fieldnr-1]->key_length()))
 
515
      {
 
516
        buff.assign("(");
 
517
        buff.append(to_string((int32_t) key_part->length /
 
518
                              key_part->field->charset()->mbmaxlen));
 
519
        buff.append(")");
 
520
        packet->append(buff.c_str(), buff.length());
 
521
      }
 
522
    }
 
523
    packet->append(')');
 
524
    store_key_options(packet, table, key_info);
 
525
  }
 
526
 
 
527
  /*
 
528
    Get possible foreign key definitions stored in InnoDB and append them
 
529
    to the CREATE TABLE statement
 
530
  */
 
531
 
 
532
  if ((for_str= cursor->get_foreign_key_create_info()))
 
533
  {
 
534
    packet->append(for_str, strlen(for_str));
 
535
    cursor->free_foreign_key_create_info(for_str);
 
536
  }
 
537
 
 
538
  packet->append(STRING_WITH_LEN("\n)"));
 
539
  {
 
540
    /*
 
541
      Get possible table space definitions and append them
 
542
      to the CREATE TABLE statement
 
543
    */
 
544
 
 
545
    /* 
 
546
      We should always store engine since we will now be 
 
547
      making sure engines accept options (aka... no
 
548
      dangling arguments for engines.
 
549
    */
 
550
    packet->append(STRING_WITH_LEN(" ENGINE="));
 
551
    packet->append(cursor->getEngine()->getName().c_str());
 
552
 
 
553
    size_t num_engine_options= table->getShare()->getTableProto()->engine().options_size();
 
554
    for (size_t x= 0; x < num_engine_options; ++x)
 
555
    {
 
556
      const message::Engine::Option &option= table->getShare()->getTableProto()->engine().options(x);
 
557
      packet->append(" ");
 
558
      packet->append(option.name().c_str());
 
559
      packet->append("=");
 
560
      append_unescaped(packet, option.state().c_str(), option.state().length());
 
561
    }
 
562
 
 
563
#if 0
 
564
    if (create_info.row_type != ROW_TYPE_DEFAULT)
 
565
    {
 
566
      packet->append(STRING_WITH_LEN(" ROW_FORMAT="));
 
567
      packet->append(ha_row_type[(uint32_t) create_info.row_type]);
 
568
    }
 
569
#endif
 
570
    if (table->getShare()->block_size)
 
571
    {
 
572
      packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
 
573
      buff= to_string(table->getShare()->block_size);
 
574
      packet->append(buff.c_str(), buff.length());
 
575
    }
 
576
    table->cursor->append_create_info(packet);
 
577
    if (table->getMutableShare()->hasComment() && table->getMutableShare()->getCommentLength())
 
578
    {
 
579
      packet->append(STRING_WITH_LEN(" COMMENT="));
 
580
      append_unescaped(packet, table->getMutableShare()->getComment(),
 
581
                       table->getMutableShare()->getCommentLength());
 
582
    }
 
583
  }
 
584
  table->restore_column_map(old_map);
 
585
  return(0);
 
586
}
 
587
 
 
588
static void store_key_options(String *packet, Table *, KeyInfo *key_info)
 
589
{
 
590
  if (key_info->algorithm == HA_KEY_ALG_BTREE)
 
591
    packet->append(STRING_WITH_LEN(" USING BTREE"));
 
592
 
 
593
  if (key_info->algorithm == HA_KEY_ALG_HASH)
 
594
    packet->append(STRING_WITH_LEN(" USING HASH"));
 
595
 
 
596
  assert(test(key_info->flags & HA_USES_COMMENT) ==
 
597
              (key_info->comment.length > 0));
 
598
  if (key_info->flags & HA_USES_COMMENT)
 
599
  {
 
600
    packet->append(STRING_WITH_LEN(" COMMENT "));
 
601
    append_unescaped(packet, key_info->comment.str,
 
602
                     key_info->comment.length);
 
603
  }
 
604
}
 
605
 
 
606
 
 
607
/****************************************************************************
 
608
  Return info about all processes
 
609
  returns for each thread: thread id, user, host, db, command, info
 
610
****************************************************************************/
 
611
 
 
612
class thread_info
 
613
{
 
614
  thread_info();
 
615
public:
 
616
  uint64_t thread_id;
 
617
  time_t start_time;
 
618
  uint32_t   command;
 
619
  string user;
 
620
  string host;
 
621
  string db;
 
622
  string proc_info;
 
623
  string state_info;
 
624
  string query;
 
625
  thread_info(uint64_t thread_id_arg,
 
626
              time_t start_time_arg,
 
627
              uint32_t command_arg,
 
628
              const string &user_arg,
 
629
              const string &host_arg,
 
630
              const string &db_arg,
 
631
              const string &proc_info_arg,
 
632
              const string &state_info_arg,
 
633
              const string &query_arg)
 
634
    : thread_id(thread_id_arg), start_time(start_time_arg), command(command_arg),
 
635
      user(user_arg), host(host_arg), db(db_arg), proc_info(proc_info_arg),
 
636
      state_info(state_info_arg), query(query_arg)
 
637
  {}
 
638
};
 
639
 
 
640
/*****************************************************************************
 
641
  Status functions
 
642
*****************************************************************************/
 
643
 
 
644
static vector<drizzle_show_var *> all_status_vars;
 
645
static bool status_vars_inited= 0;
 
646
static int show_var_cmp(const void *var1, const void *var2)
 
647
{
 
648
  return strcmp(((drizzle_show_var*)var1)->name, ((drizzle_show_var*)var2)->name);
 
649
}
 
650
 
 
651
class show_var_cmp_functor
 
652
{
 
653
  public:
 
654
  show_var_cmp_functor() { }
 
655
  inline bool operator()(const drizzle_show_var *var1, const drizzle_show_var *var2) const
 
656
  {
 
657
    int val= strcmp(var1->name, var2->name);
 
658
    return (val < 0);
 
659
  }
 
660
};
 
661
 
 
662
class show_var_remove_if
 
663
{
 
664
  public:
 
665
  show_var_remove_if() { }
 
666
  inline bool operator()(const drizzle_show_var *curr) const
 
667
  {
 
668
    return (curr->type == SHOW_UNDEF);
 
669
  }
 
670
};
 
671
 
 
672
drizzle_show_var *getFrontOfStatusVars()
 
673
{
 
674
  return all_status_vars.front();
 
675
}
 
676
 
 
677
/*
 
678
  Adds an array of drizzle_show_var entries to the output of SHOW STATUS
 
679
 
 
680
  SYNOPSIS
 
681
    add_status_vars(drizzle_show_var *list)
 
682
    list - an array of drizzle_show_var entries to add to all_status_vars
 
683
           the last entry must be {0,0,SHOW_UNDEF}
 
684
 
 
685
  NOTE
 
686
    The handling of all_status_vars[] is completely internal, it's allocated
 
687
    automatically when something is added to it, and deleted completely when
 
688
    the last entry is removed.
 
689
 
 
690
    As a special optimization, if add_status_vars() is called before
 
691
    init_status_vars(), it assumes "startup mode" - neither concurrent access
 
692
    to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
 
693
*/
 
694
int add_status_vars(drizzle_show_var *list)
 
695
{
 
696
  int res= 0;
 
697
  if (status_vars_inited)
 
698
    pthread_mutex_lock(&LOCK_status);
 
699
  while (list->name)
 
700
    all_status_vars.insert(all_status_vars.begin(), list++);
 
701
  if (status_vars_inited)
 
702
    sort(all_status_vars.begin(), all_status_vars.end(),
 
703
         show_var_cmp_functor());
 
704
  if (status_vars_inited)
 
705
    pthread_mutex_unlock(&LOCK_status);
 
706
  return res;
 
707
}
 
708
 
 
709
/*
 
710
  Make all_status_vars[] usable for SHOW STATUS
 
711
 
 
712
  NOTE
 
713
    See add_status_vars(). Before init_status_vars() call, add_status_vars()
 
714
    works in a special fast "startup" mode. Thus init_status_vars()
 
715
    should be called as late as possible but before enabling multi-threading.
 
716
*/
 
717
void init_status_vars()
 
718
{
 
719
  status_vars_inited= 1;
 
720
  sort(all_status_vars.begin(), all_status_vars.end(),
 
721
       show_var_cmp_functor());
 
722
}
 
723
 
 
724
void reset_status_vars()
 
725
{
 
726
  vector<drizzle_show_var *>::iterator p;
 
727
 
 
728
  p= all_status_vars.begin();
 
729
  while (p != all_status_vars.end())
 
730
  {
 
731
    /* Note that SHOW_LONG_NOFLUSH variables are not reset */
 
732
    if ((*p)->type == SHOW_LONG)
 
733
      (*p)->value= 0;
 
734
    ++p;
 
735
  }
 
736
}
 
737
 
 
738
/*
 
739
  catch-all cleanup function, cleans up everything no matter what
 
740
 
 
741
  DESCRIPTION
 
742
    This function is not strictly required if all add_to_status/
 
743
    remove_status_vars are properly paired, but it's a safety measure that
 
744
    deletes everything from the all_status_vars vector even if some
 
745
    remove_status_vars were forgotten
 
746
*/
 
747
void free_status_vars()
 
748
{
 
749
  all_status_vars.clear();
 
750
}
 
751
 
 
752
/*
 
753
  Removes an array of drizzle_show_var entries from the output of SHOW STATUS
 
754
 
 
755
  SYNOPSIS
 
756
    remove_status_vars(drizzle_show_var *list)
 
757
    list - an array of drizzle_show_var entries to remove to all_status_vars
 
758
           the last entry must be {0,0,SHOW_UNDEF}
 
759
 
 
760
  NOTE
 
761
    there's lots of room for optimizing this, especially in non-sorted mode,
 
762
    but nobody cares - it may be called only in case of failed plugin
 
763
    initialization in the mysqld startup.
 
764
*/
 
765
 
 
766
void remove_status_vars(drizzle_show_var *list)
 
767
{
 
768
  if (status_vars_inited)
 
769
  {
 
770
    pthread_mutex_lock(&LOCK_status);
 
771
    drizzle_show_var *all= all_status_vars.front();
 
772
    int a= 0, b= all_status_vars.size(), c= (a+b)/2;
 
773
 
 
774
    for (; list->name; list++)
 
775
    {
 
776
      int res= 0;
 
777
      for (a= 0, b= all_status_vars.size(); b-a > 1; c= (a+b)/2)
 
778
      {
 
779
        res= show_var_cmp(list, all+c);
 
780
        if (res < 0)
 
781
          b= c;
 
782
        else if (res > 0)
 
783
          a= c;
 
784
        else
 
785
          break;
 
786
      }
 
787
      if (res == 0)
 
788
        all[c].type= SHOW_UNDEF;
 
789
    }
 
790
    /* removes all the SHOW_UNDEF elements from the vector */
 
791
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
 
792
                            all_status_vars.end(),show_var_remove_if()),
 
793
                            all_status_vars.end());
 
794
    pthread_mutex_unlock(&LOCK_status);
 
795
  }
 
796
  else
 
797
  {
 
798
    drizzle_show_var *all= all_status_vars.front();
 
799
    uint32_t i;
 
800
    for (; list->name; list++)
 
801
    {
 
802
      for (i= 0; i < all_status_vars.size(); i++)
 
803
      {
 
804
        if (show_var_cmp(list, all+i))
 
805
          continue;
 
806
        all[i].type= SHOW_UNDEF;
 
807
        break;
 
808
      }
 
809
    }
 
810
    /* removes all the SHOW_UNDEF elements from the vector */
 
811
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
 
812
                            all_status_vars.end(),show_var_remove_if()),
 
813
                            all_status_vars.end());
 
814
  }
 
815
}
 
816
 
 
817
/* collect status for all running threads */
 
818
 
 
819
void calc_sum_of_all_status(system_status_var *to)
 
820
{
 
821
  /* Ensure that thread id not killed during loop */
 
822
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
823
 
 
824
  /* Get global values as base */
 
825
  *to= global_status_var;
 
826
 
 
827
  /* Add to this status from existing threads */
 
828
  for(SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
829
  {
 
830
    add_to_status(to, &((*it)->status_var));
 
831
  }
 
832
 
 
833
  pthread_mutex_unlock(&LOCK_thread_count);
 
834
  return;
 
835
}
 
836
 
146
837
} /* namespace drizzled */