~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

Merged Drizzle's Trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
#include <algorithm>
50
50
#include <sstream>
51
51
 
 
52
#include <boost/unordered_set.hpp>
 
53
 
52
54
using namespace std;
53
55
 
54
56
namespace drizzled
395
397
    1             Error
396
398
*/
397
399
 
 
400
class typelib_set_member
 
401
{
 
402
public:
 
403
  string s;
 
404
  const CHARSET_INFO * const cs;
 
405
 
 
406
  typelib_set_member(const char* value, unsigned int length,
 
407
                     const CHARSET_INFO * const charset)
 
408
    : s(value, length),
 
409
      cs(charset)
 
410
  {}
 
411
};
 
412
 
 
413
static bool operator==(typelib_set_member const& a, typelib_set_member const& b)
 
414
{
 
415
  return (my_strnncoll(a.cs,
 
416
                       (const unsigned char*)a.s.c_str(), a.s.length(),
 
417
                       (const unsigned char*)b.s.c_str(), b.s.length())==0);
 
418
}
 
419
 
 
420
 
 
421
namespace
 
422
{
 
423
class typelib_set_member_hasher
 
424
{
 
425
  boost::hash<string> hasher;
 
426
public:
 
427
  std::size_t operator()(const typelib_set_member& t) const
 
428
  {
 
429
    return hasher(t.s);
 
430
  }
 
431
};
 
432
}
 
433
 
398
434
static bool check_duplicates_in_interval(const char *set_or_name,
399
435
                                         const char *name, TYPELIB *typelib,
400
436
                                         const CHARSET_INFO * const cs,
405
441
  unsigned int *cur_length= typelib->type_lengths;
406
442
  *dup_val_count= 0;
407
443
 
408
 
  for ( ; tmp.count > 1; cur_value++, cur_length++)
 
444
  boost::unordered_set<typelib_set_member, typelib_set_member_hasher> interval_set;
 
445
 
 
446
  for ( ; tmp.count > 0; cur_value++, cur_length++)
409
447
  {
410
448
    tmp.type_names++;
411
449
    tmp.type_lengths++;
412
450
    tmp.count--;
413
 
    if (find_type2(&tmp, (const char*)*cur_value, *cur_length, cs))
 
451
    if (interval_set.find(typelib_set_member(*cur_value, *cur_length, cs)) != interval_set.end())
414
452
    {
415
453
      my_error(ER_DUPLICATED_VALUE_IN_TYPE, MYF(0),
416
454
               name,*cur_value,set_or_name);
417
455
      return 1;
418
456
    }
 
457
    else
 
458
      interval_set.insert(typelib_set_member(*cur_value, *cur_length, cs));
419
459
  }
420
460
  return 0;
421
461
}
488
528
 
489
529
  switch (sql_field->sql_type) {
490
530
  case DRIZZLE_TYPE_BLOB:
491
 
    sql_field->pack_flag= pack_length_to_packflag(sql_field->pack_length - portable_sizeof_char_ptr);
492
531
    sql_field->length= 8; // Unireg field length
493
532
    (*blob_columns)++;
494
533
    break;
495
534
  case DRIZZLE_TYPE_VARCHAR:
496
 
    sql_field->pack_flag=0;
497
535
    break;
498
536
  case DRIZZLE_TYPE_ENUM:
499
 
    sql_field->pack_flag=pack_length_to_packflag(sql_field->pack_length);
500
537
    if (check_duplicates_in_interval("ENUM",
501
538
                                     sql_field->field_name,
502
539
                                     sql_field->interval,
507
544
  case DRIZZLE_TYPE_DATE:  // Rest of string types
508
545
  case DRIZZLE_TYPE_DATETIME:
509
546
  case DRIZZLE_TYPE_NULL:
510
 
    sql_field->pack_flag=f_settype((uint32_t) sql_field->sql_type);
511
547
    break;
512
548
  case DRIZZLE_TYPE_DECIMAL:
513
 
    sql_field->pack_flag= 0;
514
549
    break;
515
550
  case DRIZZLE_TYPE_TIMESTAMP:
516
551
    /* We should replace old TIMESTAMP fields with their newer analogs */
522
557
        (*timestamps_with_niladic)++;
523
558
      }
524
559
      else
 
560
      {
525
561
        sql_field->unireg_check= Field::NONE;
 
562
      }
526
563
    }
527
564
    else if (sql_field->unireg_check != Field::NONE)
528
565
      (*timestamps_with_niladic)++;
530
567
    (*timestamps)++;
531
568
    /* fall-through */
532
569
  default:
533
 
    sql_field->pack_flag=(0 |
534
 
                          f_settype((uint32_t) sql_field->sql_type));
535
570
    break;
536
571
  }
 
572
 
537
573
  return 0;
538
574
}
539
575
 
1060
1096
 
1061
1097
      key_part_info->fieldnr= field;
1062
1098
      key_part_info->offset=  (uint16_t) sql_field->offset;
1063
 
      key_part_info->key_type=sql_field->pack_flag;
 
1099
      key_part_info->key_type= 0;
1064
1100
      length= sql_field->key_length;
1065
1101
 
1066
1102
      if (column->length)
1602
1638
 
1603
1639
  SYNOPSIS
1604
1640
    mysql_rename_table()
 
1641
      session
1605
1642
      base                      The plugin::StorageEngine handle.
1606
1643
      old_db                    The old database name.
1607
1644
      old_name                  The old table name.
1614
1651
*/
1615
1652
 
1616
1653
bool
1617
 
mysql_rename_table(plugin::StorageEngine *base,
 
1654
mysql_rename_table(Session &session,
 
1655
                   plugin::StorageEngine *base,
1618
1656
                   TableIdentifier &from,
1619
1657
                   TableIdentifier &to)
1620
1658
{
1621
 
  Session *session= current_session;
1622
1659
  int error= 0;
1623
1660
 
1624
1661
  assert(base);
1629
1666
    return true;
1630
1667
  }
1631
1668
 
1632
 
  error= base->renameTable(*session, from, to);
 
1669
  error= base->renameTable(session, from, to);
1633
1670
 
1634
1671
  if (error == HA_ERR_WRONG_COMMAND)
1635
1672
  {
1677
1714
  mysql_lock_abort(session, table);     /* end threads waiting on lock */
1678
1715
 
1679
1716
  /* Wait until all there are no other threads that has this table open */
1680
 
  remove_table_from_cache(session, table->s->getSchemaName(),
1681
 
                          table->s->getTableName(),
 
1717
  remove_table_from_cache(session, table->getMutableShare()->getSchemaName(),
 
1718
                          table->getMutableShare()->getTableName(),
1682
1719
                          RTFC_WAIT_OTHER_THREAD_FLAG);
1683
1720
}
1684
1721
 
1825
1862
    }
1826
1863
 
1827
1864
    /* Close all instances of the table to allow repair to rename files */
1828
 
    if (lock_type == TL_WRITE && table->table->s->version)
 
1865
    if (lock_type == TL_WRITE && table->table->getShare()->getVersion())
1829
1866
    {
1830
1867
      pthread_mutex_lock(&LOCK_open); /* Lock type is TL_WRITE and we lock to repair the table */
1831
1868
      const char *old_message=session->enter_cond(&COND_refresh, &LOCK_open,
1832
1869
                                              "Waiting to get writelock");
1833
1870
      mysql_lock_abort(session,table->table);
1834
 
      remove_table_from_cache(session, table->table->s->getSchemaName(),
1835
 
                              table->table->s->getTableName(),
 
1871
      remove_table_from_cache(session, table->table->getMutableShare()->getSchemaName(),
 
1872
                              table->table->getMutableShare()->getTableName(),
1836
1873
                              RTFC_WAIT_OTHER_THREAD_FLAG |
1837
1874
                              RTFC_CHECK_KILLED_FLAG);
1838
1875
      session->exit_cond(old_message);
1923
1960
    if (table->table)
1924
1961
    {
1925
1962
      if (fatal_error)
1926
 
        table->table->s->version=0;               // Force close of table
 
1963
      {
 
1964
        table->table->getMutableShare()->resetVersion();               // Force close of table
 
1965
      }
1927
1966
      else if (open_for_modify)
1928
1967
      {
1929
 
        if (table->table->s->tmp_table)
 
1968
        if (table->table->getShare()->tmp_table)
 
1969
        {
1930
1970
          table->table->cursor->info(HA_STATUS_CONST);
 
1971
        }
1931
1972
        else
1932
1973
        {
1933
1974
          pthread_mutex_lock(&LOCK_open);
1934
 
          remove_table_from_cache(session, table->table->s->getSchemaName(),
1935
 
                                  table->table->s->getTableName(), RTFC_NO_FLAG);
 
1975
          remove_table_from_cache(session, table->table->getMutableShare()->getSchemaName(),
 
1976
                                  table->table->getMutableShare()->getTableName(), RTFC_NO_FLAG);
1936
1977
          pthread_mutex_unlock(&LOCK_open);
1937
1978
        }
1938
1979
      }
2122
2163
  if (session->open_tables_from_list(&src_table, &not_used))
2123
2164
    return true;
2124
2165
 
2125
 
  TableIdentifier src_identifier(src_table->table->s->getSchemaName(),
2126
 
                                 src_table->table->s->getTableName(), src_table->table->s->tmp_table);
 
2166
  TableIdentifier src_identifier(src_table->table->getMutableShare()->getSchemaName(),
 
2167
                                 src_table->table->getMutableShare()->getTableName(), src_table->table->getMutableShare()->tmp_table);
2127
2168
 
2128
2169
 
2129
2170
 
2198
2239
      } 
2199
2240
      else
2200
2241
      {
2201
 
        bool rc= replicateCreateTableLike(session, table, name_lock, (src_table->table->s->tmp_table), is_if_not_exists);
 
2242
        bool rc= replicateCreateTableLike(session, table, name_lock, (src_table->table->getShare()->tmp_table), is_if_not_exists);
2202
2243
        (void)rc;
2203
2244
 
2204
2245
        res= false;