~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Brian Aker
  • Date: 2009-01-17 02:46:52 UTC
  • Revision ID: brian@gir-3.local-20090117024652-4ducefje08ajbs1q
Refactor append_identifier and remove dead OPTION_QUOTE_SHOW_CREATE option
(we always quote).

Show diffs side-by-side

added added

removed removed

Lines of Context:
456
456
 
457
457
 
458
458
/*
459
 
  Go through all character combinations and ensure that sql_lex.cc can
460
 
  parse it as an identifier.
461
 
 
462
 
  SYNOPSIS
463
 
  require_quotes()
464
 
  name                  attribute name
465
 
  name_length           length of name
466
 
 
467
 
  RETURN
468
 
    #   Pointer to conflicting character
469
 
    0   No conflicting character
470
 
*/
471
 
 
472
 
static const char *require_quotes(const char *name, uint32_t name_length)
473
 
{
474
 
  uint32_t length;
475
 
  bool pure_digit= true;
476
 
  const char *end= name + name_length;
477
 
 
478
 
  for (; name < end ; name++)
479
 
  {
480
 
    unsigned char chr= (unsigned char) *name;
481
 
    length= my_mbcharlen(system_charset_info, chr);
482
 
    if (length == 1 && !system_charset_info->ident_map[chr])
483
 
      return name;
484
 
    if (length == 1 && (chr < '0' || chr > '9'))
485
 
      pure_digit= false;
486
 
  }
487
 
  if (pure_digit)
488
 
    return name;
489
 
  return 0;
490
 
}
491
 
 
492
 
 
493
 
/*
494
 
  Quote the given identifier if needed and append it to the target string.
495
 
  If the given identifier is empty, it will be quoted.
496
 
 
497
 
  SYNOPSIS
498
 
  append_identifier()
499
 
  session                   thread handler
500
 
  packet                target string
501
 
  name                  the identifier to be appended
502
 
  name_length           length of the appending identifier
503
 
*/
504
 
 
505
 
void
506
 
append_identifier(Session *session, String *packet, const char *name, uint32_t length)
507
 
{
508
 
  const char *name_end;
509
 
  char quote_char;
510
 
  int q= get_quote_char_for_identifier(session, name, length);
511
 
 
512
 
  if (q == EOF)
513
 
  {
514
 
    packet->append(name, length, packet->charset());
515
 
    return;
516
 
  }
517
 
 
518
 
  /*
519
 
    The identifier must be quoted as it includes a quote character or
520
 
   it's a keyword
521
 
  */
522
 
 
523
 
  packet->reserve(length*2 + 2);
524
 
  quote_char= (char) q;
525
 
  packet->append(&quote_char, 1, system_charset_info);
526
 
 
527
 
  for (name_end= name+length ; name < name_end ; name+= length)
528
 
  {
529
 
    unsigned char chr= (unsigned char) *name;
530
 
    length= my_mbcharlen(system_charset_info, chr);
531
 
    /*
532
 
      my_mbcharlen can return 0 on a wrong multibyte
533
 
      sequence. It is possible when upgrading from 4.0,
534
 
      and identifier contains some accented characters.
535
 
      The manual says it does not work. So we'll just
536
 
      change length to 1 not to hang in the endless loop.
537
 
    */
538
 
    if (!length)
539
 
      length= 1;
540
 
    if (length == 1 && chr == (unsigned char) quote_char)
541
 
      packet->append(&quote_char, 1, system_charset_info);
542
 
    packet->append(name, length, system_charset_info);
543
 
  }
544
 
  packet->append(&quote_char, 1, system_charset_info);
545
 
}
546
 
 
547
 
 
548
 
/*
549
459
  Get the quote character for displaying an identifier.
550
460
 
551
461
  SYNOPSIS
568
478
    #     Quote character
569
479
*/
570
480
 
571
 
int get_quote_char_for_identifier(Session *session, const char *name, uint32_t length)
 
481
int get_quote_char_for_identifier(Session *, const char *, uint32_t)
572
482
{
573
 
  if (length &&
574
 
      !is_keyword(name,length) &&
575
 
      !require_quotes(name, length) &&
576
 
      !(session->options & OPTION_QUOTE_SHOW_CREATE))
577
 
    return EOF;
578
483
  return '`';
579
484
}
580
485
 
715
620
      alias= share->table_name.str;
716
621
    }
717
622
  }
718
 
  append_identifier(session, packet, alias, strlen(alias));
 
623
  packet->append_identifier(alias, strlen(alias));
719
624
  packet->append(STRING_WITH_LEN(" (\n"));
720
625
  /*
721
626
    We need this to get default values from the table
732
637
      packet->append(STRING_WITH_LEN(",\n"));
733
638
 
734
639
    packet->append(STRING_WITH_LEN("  "));
735
 
    append_identifier(session,packet,field->field_name, strlen(field->field_name));
 
640
    packet->append_identifier(field->field_name, strlen(field->field_name));
736
641
    packet->append(' ');
737
642
    // check for surprises from the previous call to Field::sql_type()
738
643
    if (type.ptr() != tmp)
854
759
      packet->append(STRING_WITH_LEN("KEY "));
855
760
 
856
761
    if (!found_primary)
857
 
     append_identifier(session, packet, key_info->name, strlen(key_info->name));
 
762
     packet->append_identifier(key_info->name, strlen(key_info->name));
858
763
 
859
764
    packet->append(STRING_WITH_LEN(" ("));
860
765
 
864
769
        packet->append(',');
865
770
 
866
771
      if (key_part->field)
867
 
        append_identifier(session,packet,key_part->field->field_name,
 
772
        packet->append_identifier(key_part->field->field_name,
868
773
                          strlen(key_part->field->field_name));
869
774
      if (key_part->field &&
870
775
          (key_part->length !=
1054
959
  if (create_options & HA_LEX_CREATE_IF_NOT_EXISTS)
1055
960
    buffer->append(STRING_WITH_LEN("IF NOT EXISTS "));
1056
961
 
1057
 
  append_identifier(session, buffer, dbname, strlen(dbname));
 
962
  buffer->append_identifier(dbname, strlen(dbname));
1058
963
 
1059
964
  return(false);
1060
965
}