~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Brian Aker
  • Date: 2010-08-03 19:24:14 UTC
  • mto: This revision was merged to the branch mainline in revision 1683.
  • Revision ID: brian@gaz-20100803192414-jk20j0u6cnze3ja6
Do comparison via non-case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* drop and alter of tables */
17
17
 
41
41
#include "drizzled/global_charset_info.h"
42
42
#include "drizzled/charset.h"
43
43
 
44
 
#include "drizzled/definition/cache.h"
45
 
 
46
44
 
47
45
#include "drizzled/statement/alter_table.h"
48
46
#include "drizzled/sql_table.h"
107
105
    cursor
108
106
*/
109
107
 
110
 
void write_bin_log(Session *session, const std::string &query)
 
108
void write_bin_log(Session *session,
 
109
                   char const *query)
111
110
{
112
111
  TransactionServices &transaction_services= TransactionServices::singleton();
113
112
  transaction_services.rawStatement(session, query);
114
113
}
115
114
 
 
115
 
 
116
/* Should should be refactored to go away */
 
117
void write_bin_log_drop_table(Session *session, bool if_exists, const char *db_name, const char *table_name)
 
118
{
 
119
  TransactionServices &transaction_services= TransactionServices::singleton();
 
120
  string built_query;
 
121
 
 
122
  if (if_exists)
 
123
    built_query.append("DROP TABLE IF EXISTS ");
 
124
  else
 
125
    built_query.append("DROP TABLE ");
 
126
 
 
127
  built_query.append("`");
 
128
  if (session->db.empty() || strcmp(db_name, session->db.c_str()) != 0)
 
129
  {
 
130
    built_query.append(db_name);
 
131
    built_query.append("`.`");
 
132
  }
 
133
 
 
134
  built_query.append(table_name);
 
135
  built_query.append("`");
 
136
  transaction_services.rawStatement(session, built_query);
 
137
}
 
138
 
116
139
/*
117
140
  Execute the drop of a normal or temporary table
118
141
 
148
171
  int error= 0;
149
172
  bool foreign_key_error= false;
150
173
 
151
 
  {
152
 
    table::Cache::singleton().mutex().lock(); /* Part 2 of rm a table */
153
 
 
154
 
    if (not drop_temporary && session->lock_table_names_exclusively(tables))
155
 
    {
156
 
      table::Cache::singleton().mutex().unlock();
157
 
      return 1;
158
 
    }
159
 
 
160
 
    /* Don't give warnings for not found errors, as we already generate notes */
161
 
    session->no_warnings_for_error= 1;
162
 
 
163
 
    for (table= tables; table; table= table->next_local)
164
 
    {
165
 
      TableIdentifier tmp_identifier(table->getSchemaName(), table->getTableName());
166
 
 
167
 
      error= session->drop_temporary_table(tmp_identifier);
168
 
 
169
 
      switch (error) {
170
 
      case  0:
171
 
        // removed temporary table
172
 
        continue;
173
 
      case -1:
 
174
  pthread_mutex_lock(&LOCK_open); /* Part 2 of rm a table */
 
175
 
 
176
  /*
 
177
    If we have the table in the definition cache, we don't have to check the
 
178
    .frm cursor to find if the table is a normal table (not view) and what
 
179
    engine to use.
 
180
  */
 
181
 
 
182
  for (table= tables; table; table= table->next_local)
 
183
  {
 
184
    TableIdentifier identifier(table->db, table->table_name);
 
185
    TableShare *share;
 
186
    table->setDbType(NULL);
 
187
 
 
188
    if ((share= TableShare::getShare(identifier)))
 
189
    {
 
190
      table->setDbType(share->db_type());
 
191
    }
 
192
  }
 
193
 
 
194
  if (not drop_temporary && lock_table_names_exclusively(session, tables))
 
195
  {
 
196
    pthread_mutex_unlock(&LOCK_open);
 
197
    return 1;
 
198
  }
 
199
 
 
200
  /* Don't give warnings for not found errors, as we already generate notes */
 
201
  session->no_warnings_for_error= 1;
 
202
 
 
203
  for (table= tables; table; table= table->next_local)
 
204
  {
 
205
    char *db=table->db;
 
206
 
 
207
    error= session->drop_temporary_table(table);
 
208
 
 
209
    switch (error) {
 
210
    case  0:
 
211
      // removed temporary table
 
212
      continue;
 
213
    case -1:
 
214
      error= 1;
 
215
      goto err_with_placeholders;
 
216
    default:
 
217
      // temporary table not found
 
218
      error= 0;
 
219
    }
 
220
 
 
221
    if (drop_temporary == false)
 
222
    {
 
223
      Table *locked_table;
 
224
      TableIdentifier identifier(db, table->table_name);
 
225
      abort_locked_tables(session, identifier);
 
226
      remove_table_from_cache(session, identifier,
 
227
                              RTFC_WAIT_OTHER_THREAD_FLAG |
 
228
                              RTFC_CHECK_KILLED_FLAG);
 
229
      /*
 
230
        If the table was used in lock tables, remember it so that
 
231
        unlock_table_names can free it
 
232
      */
 
233
      if ((locked_table= drop_locked_tables(session, identifier)))
 
234
        table->table= locked_table;
 
235
 
 
236
      if (session->killed)
 
237
      {
 
238
        error= -1;
 
239
        goto err_with_placeholders;
 
240
      }
 
241
    }
 
242
    TableIdentifier identifier(db, table->table_name, table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
 
243
 
 
244
    if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
 
245
    {
 
246
      // Table was not found on disk and table can't be created from engine
 
247
      if (if_exists)
 
248
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
249
                            ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
 
250
                            table->table_name);
 
251
      else
174
252
        error= 1;
175
 
        goto err_with_placeholders;
176
 
      default:
177
 
        // temporary table not found
 
253
    }
 
254
    else
 
255
    {
 
256
      error= plugin::StorageEngine::dropTable(*session, identifier);
 
257
 
 
258
      if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && if_exists)
 
259
      {
178
260
        error= 0;
179
 
      }
180
 
 
181
 
      if (drop_temporary == false)
182
 
      {
183
 
        Table *locked_table;
184
 
        abort_locked_tables(session, tmp_identifier);
185
 
        table::Cache::singleton().removeTable(session, tmp_identifier,
186
 
                                              RTFC_WAIT_OTHER_THREAD_FLAG |
187
 
                                              RTFC_CHECK_KILLED_FLAG);
188
 
        /*
189
 
          If the table was used in lock tables, remember it so that
190
 
          unlock_table_names can free it
191
 
        */
192
 
        if ((locked_table= drop_locked_tables(session, tmp_identifier)))
193
 
          table->table= locked_table;
194
 
 
195
 
        if (session->getKilled())
196
 
        {
197
 
          error= -1;
198
 
          goto err_with_placeholders;
199
 
        }
200
 
      }
201
 
      TableIdentifier identifier(table->getSchemaName(), table->getTableName(), table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
202
 
 
203
 
      if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
204
 
      {
205
 
        // Table was not found on disk and table can't be created from engine
206
 
        if (if_exists)
207
 
          push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
208
 
                              ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
209
 
                              table->getTableName());
210
 
        else
211
 
          error= 1;
212
 
      }
213
 
      else
214
 
      {
215
 
        error= plugin::StorageEngine::dropTable(*session, identifier);
216
 
 
217
 
        if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && if_exists)
218
 
        {
219
 
          error= 0;
220
 
          session->clear_error();
221
 
        }
222
 
 
223
 
        if (error == HA_ERR_ROW_IS_REFERENCED)
224
 
        {
225
 
          /* the table is referenced by a foreign key constraint */
226
 
          foreign_key_error= true;
227
 
        }
228
 
      }
229
 
 
230
 
      if (error == 0 || (if_exists && foreign_key_error == false))
231
 
      {
232
 
        TransactionServices &transaction_services= TransactionServices::singleton();
233
 
        transaction_services.dropTable(session, string(table->getSchemaName()), string(table->getTableName()), if_exists);
234
 
      }
235
 
 
236
 
      if (error)
237
 
      {
238
 
        if (wrong_tables.length())
239
 
          wrong_tables.append(',');
240
 
        wrong_tables.append(String(table->getTableName(), system_charset_info));
241
 
      }
242
 
    }
243
 
    /*
244
 
      It's safe to unlock table::Cache::singleton().mutex(): we have an exclusive lock
245
 
      on the table name.
246
 
    */
247
 
    table::Cache::singleton().mutex().unlock();
 
261
        session->clear_error();
 
262
      }
 
263
 
 
264
      if (error == HA_ERR_ROW_IS_REFERENCED)
 
265
      {
 
266
        /* the table is referenced by a foreign key constraint */
 
267
        foreign_key_error= true;
 
268
      }
 
269
    }
 
270
 
 
271
    if (error == 0 || (if_exists && foreign_key_error == false))
 
272
    {
 
273
      TransactionServices &transaction_services= TransactionServices::singleton();
 
274
      transaction_services.dropTable(session, string(db), string(table->table_name), if_exists);
 
275
    }
 
276
 
 
277
    if (error)
 
278
    {
 
279
      if (wrong_tables.length())
 
280
        wrong_tables.append(',');
 
281
      wrong_tables.append(String(table->table_name,system_charset_info));
 
282
    }
248
283
  }
 
284
  /*
 
285
    It's safe to unlock LOCK_open: we have an exclusive lock
 
286
    on the table name.
 
287
  */
 
288
  pthread_mutex_unlock(&LOCK_open);
249
289
  error= 0;
250
290
 
251
291
  if (wrong_tables.length())
262
302
    error= 1;
263
303
  }
264
304
 
265
 
  table::Cache::singleton().mutex().lock(); /* final bit in rm table lock */
 
305
  pthread_mutex_lock(&LOCK_open); /* final bit in rm table lock */
266
306
 
267
307
err_with_placeholders:
268
 
  tables->unlock_table_names();
269
 
  table::Cache::singleton().mutex().unlock();
 
308
  unlock_table_names(tables, NULL);
 
309
  pthread_mutex_unlock(&LOCK_open);
270
310
  session->no_warnings_for_error= 0;
271
311
 
272
312
  return error;
273
313
}
274
314
 
 
315
 
 
316
/*
 
317
  Quickly remove a table.
 
318
 
 
319
  SYNOPSIS
 
320
    quick_rm_table()
 
321
      base                      The plugin::StorageEngine handle.
 
322
      db                        The database name.
 
323
      table_name                The table name.
 
324
      is_tmp                    If the table is temp.
 
325
 
 
326
  RETURN
 
327
    0           OK
 
328
    != 0        Error
 
329
*/
 
330
bool quick_rm_table(Session& session,
 
331
                    TableIdentifier &identifier)
 
332
{
 
333
  return (plugin::StorageEngine::dropTable(session, identifier));
 
334
}
 
335
 
275
336
/*
276
337
  Sort keys in the following order:
277
338
  - PRIMARY KEY
606
667
 
607
668
    if (sql_field->sql_type == DRIZZLE_TYPE_ENUM)
608
669
    {
609
 
      size_t dummy;
 
670
      uint32_t dummy;
610
671
      const CHARSET_INFO * const cs= sql_field->charset;
611
672
      TYPELIB *interval= sql_field->interval;
612
673
 
639
700
          if (String::needs_conversion(tmp->length(), tmp->charset(),
640
701
                                       cs, &dummy))
641
702
          {
642
 
            size_t cnv_errs;
 
703
            uint32_t cnv_errs;
643
704
            conv.copy(tmp->ptr(), tmp->length(), tmp->charset(), cs, &cnv_errs);
644
705
            interval->type_names[i]= session->mem_root->strmake_root(conv.ptr(), conv.length());
645
706
            interval->type_lengths[i]= conv.length();
681
742
            }
682
743
          }
683
744
        }
684
 
        uint32_t new_dummy;
685
 
        calculate_interval_lengths(cs, interval, &field_length, &new_dummy);
 
745
        calculate_interval_lengths(cs, interval, &field_length, &dummy);
686
746
        sql_field->length= field_length;
687
747
      }
688
748
      set_if_smaller(sql_field->length, (uint32_t)MAX_FIELD_WIDTH-1);
818
878
      fk_key_count++;
819
879
      if (((Foreign_key *)key)->validate(alter_info->create_list))
820
880
        return true;
821
 
 
822
881
      Foreign_key *fk_key= (Foreign_key*) key;
823
 
 
824
 
      add_foreign_key_to_table_message(&create_proto,
825
 
                                       fk_key->name.str,
826
 
                                       fk_key->columns,
827
 
                                       fk_key->ref_table,
828
 
                                       fk_key->ref_columns,
829
 
                                       fk_key->delete_opt,
830
 
                                       fk_key->update_opt,
831
 
                                       fk_key->match_opt);
832
 
 
833
882
      if (fk_key->ref_columns.elements &&
834
883
          fk_key->ref_columns.elements != fk_key->columns.elements)
835
884
      {
1265
1314
}
1266
1315
 
1267
1316
static bool locked_create_event(Session *session,
1268
 
                                const TableIdentifier &identifier,
 
1317
                                TableIdentifier &identifier,
1269
1318
                                HA_CREATE_INFO *create_info,
1270
1319
                                message::Table &table_proto,
1271
1320
                                AlterInfo *alter_info,
1300
1349
        return error;
1301
1350
      }
1302
1351
 
1303
 
      std::string path;
1304
 
      identifier.getSQLPath(path);
1305
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1306
 
 
 
1352
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
1307
1353
      return error;
1308
1354
    }
1309
1355
 
1320
1366
      /*
1321
1367
        @todo improve this error condition.
1322
1368
      */
1323
 
      if (definition::Cache::singleton().find(identifier.getKey()))
 
1369
      if (TableShare::getShare(identifier))
1324
1370
      {
1325
 
        std::string path;
1326
 
        identifier.getSQLPath(path);
1327
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1328
 
 
 
1371
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
1329
1372
        return error;
1330
1373
      }
1331
1374
    }
1401
1444
*/
1402
1445
 
1403
1446
bool mysql_create_table_no_lock(Session *session,
1404
 
                                const TableIdentifier &identifier,
 
1447
                                TableIdentifier &identifier,
1405
1448
                                HA_CREATE_INFO *create_info,
1406
1449
                                message::Table &table_proto,
1407
1450
                                AlterInfo *alter_info,
1427
1470
 
1428
1471
  /* Build a Table object to pass down to the engine, and the do the actual create. */
1429
1472
  if (not mysql_prepare_create_table(session, create_info, table_proto, alter_info,
1430
 
                                     internal_tmp_table,
1431
 
                                     &db_options,
1432
 
                                     &key_info_buffer, &key_count,
1433
 
                                     select_field_count))
 
1473
                                 internal_tmp_table,
 
1474
                                 &db_options,
 
1475
                                 &key_info_buffer, &key_count,
 
1476
                                 select_field_count))
1434
1477
  {
1435
 
    boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* CREATE TABLE (some confussion on naming, double check) */
 
1478
    pthread_mutex_lock(&LOCK_open); /* CREATE TABLE (some confussion on naming, double check) */
1436
1479
    error= locked_create_event(session,
1437
1480
                               identifier,
1438
1481
                               create_info,
1442
1485
                               internal_tmp_table,
1443
1486
                               db_options, key_count,
1444
1487
                               key_info_buffer);
 
1488
    pthread_mutex_unlock(&LOCK_open);
1445
1489
  }
1446
1490
 
1447
1491
  session->set_proc_info("After create");
1453
1497
  @note the following two methods implement create [temporary] table.
1454
1498
*/
1455
1499
static bool drizzle_create_table(Session *session,
1456
 
                                 const TableIdentifier &identifier,
 
1500
                                 TableIdentifier &identifier,
1457
1501
                                 HA_CREATE_INFO *create_info,
1458
1502
                                 message::Table &table_proto,
1459
1503
                                 AlterInfo *alter_info,
1480
1524
    }
1481
1525
    else
1482
1526
    {
1483
 
      std::string path;
1484
 
      identifier.getSQLPath(path);
1485
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
 
1527
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
1486
1528
      result= true;
1487
1529
    }
1488
1530
  }
1500
1542
 
1501
1543
  if (name_lock)
1502
1544
  {
1503
 
    boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* Lock for removing name_lock during table create */
 
1545
    pthread_mutex_lock(&LOCK_open); /* Lock for removing name_lock during table create */
1504
1546
    session->unlink_open_table(name_lock);
 
1547
    pthread_mutex_unlock(&LOCK_open);
1505
1548
  }
1506
1549
 
1507
1550
  return(result);
1512
1555
  Database locking aware wrapper for mysql_create_table_no_lock(),
1513
1556
*/
1514
1557
bool mysql_create_table(Session *session,
1515
 
                        const TableIdentifier &identifier,
 
1558
                        TableIdentifier &identifier,
1516
1559
                        HA_CREATE_INFO *create_info,
1517
1560
                        message::Table &table_proto,
1518
1561
                        AlterInfo *alter_info,
1608
1651
bool
1609
1652
mysql_rename_table(Session &session,
1610
1653
                   plugin::StorageEngine *base,
1611
 
                   const TableIdentifier &from,
1612
 
                   const TableIdentifier &to)
 
1654
                   TableIdentifier &from,
 
1655
                   TableIdentifier &to)
1613
1656
{
1614
1657
  int error= 0;
1615
1658
 
1629
1672
  }
1630
1673
  else if (error)
1631
1674
  {
1632
 
    std::string from_path;
1633
 
    std::string to_path;
1634
 
 
1635
 
    from.getSQLPath(from_path);
1636
 
    to.getSQLPath(to_path);
1637
 
 
1638
 
    const char *from_identifier= from.isTmp() ? "#sql-temporary" : from_path.c_str();
1639
 
    const char *to_identifier= to.isTmp() ? "#sql-temporary" : to_path.c_str();
 
1675
    const char *from_identifier= from.isTmp() ? "#sql-temporary" : from.getSQLPath().c_str();
 
1676
    const char *to_identifier= to.isTmp() ? "#sql-temporary" : to.getSQLPath().c_str();
1640
1677
 
1641
1678
    my_error(ER_ERROR_ON_RENAME, MYF(0), from_identifier, to_identifier, error);
1642
1679
  }
1660
1697
   the table is closed.
1661
1698
 
1662
1699
  PREREQUISITES
1663
 
    Lock on table::Cache::singleton().mutex()
 
1700
    Lock on LOCK_open
1664
1701
    Win32 clients must also have a WRITE LOCK on the table !
1665
1702
*/
1666
1703
 
1668
1705
                              enum ha_extra_function function)
1669
1706
{
1670
1707
 
1671
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
1708
  safe_mutex_assert_owner(&LOCK_open);
1672
1709
 
1673
1710
  table->cursor->extra(function);
1674
1711
  /* Mark all tables that are in use as 'old' */
1675
 
  session->abortLock(table);    /* end threads waiting on lock */
 
1712
  mysql_lock_abort(session, table);     /* end threads waiting on lock */
1676
1713
 
1677
1714
  /* Wait until all there are no other threads that has this table open */
1678
 
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName());
1679
 
  table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG);
 
1715
  TableIdentifier identifier(table->getMutableShare()->getSchemaName(), table->getMutableShare()->getTableName());
 
1716
  remove_table_from_cache(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG);
1680
1717
}
1681
1718
 
1682
1719
/*
1692
1729
    reopen the table.
1693
1730
 
1694
1731
  PREREQUISITES
1695
 
    Lock on table::Cache::singleton().mutex()
 
1732
    Lock on LOCK_open
1696
1733
    Win32 clients must also have a WRITE LOCK on the table !
1697
1734
*/
1698
1735
 
1703
1740
  /* Close lock if this is not got with LOCK TABLES */
1704
1741
  if (lock)
1705
1742
  {
1706
 
    unlockTables(lock);
 
1743
    mysql_unlock_tables(this, lock);
1707
1744
    lock= NULL;                 // Start locked threads
1708
1745
  }
1709
1746
  /* Close all copies of 'table'.  This also frees all LOCK TABLES lock */
1710
1747
  unlink_open_table(table);
1711
1748
 
1712
 
  /* When lock on table::Cache::singleton().mutex() is freed other threads can continue */
1713
 
  locking::broadcast_refresh();
 
1749
  /* When lock on LOCK_open is freed other threads can continue */
 
1750
  broadcast_refresh();
1714
1751
}
1715
1752
 
1716
1753
/*
1754
1791
  for (table= tables; table; table= table->next_local)
1755
1792
  {
1756
1793
    char table_name[NAME_LEN*2+2];
 
1794
    char* db = table->db;
1757
1795
    bool fatal_error=0;
1758
1796
 
1759
 
    snprintf(table_name, sizeof(table_name), "%s.%s", table->getSchemaName(), table->getTableName());
 
1797
    snprintf(table_name, sizeof(table_name), "%s.%s",db,table->table_name);
1760
1798
    table->lock_type= lock_type;
1761
1799
    /* open only one table from local list of command */
1762
1800
    {
1822
1860
    /* Close all instances of the table to allow repair to rename files */
1823
1861
    if (lock_type == TL_WRITE && table->table->getShare()->getVersion())
1824
1862
    {
1825
 
      table::Cache::singleton().mutex().lock(); /* Lock type is TL_WRITE and we lock to repair the table */
1826
 
      const char *old_message=session->enter_cond(COND_refresh, table::Cache::singleton().mutex(),
1827
 
                                                  "Waiting to get writelock");
1828
 
      session->abortLock(table->table);
1829
 
      TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1830
 
      table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
 
1863
      pthread_mutex_lock(&LOCK_open); /* Lock type is TL_WRITE and we lock to repair the table */
 
1864
      const char *old_message=session->enter_cond(&COND_refresh, &LOCK_open,
 
1865
                                              "Waiting to get writelock");
 
1866
      mysql_lock_abort(session,table->table);
 
1867
      TableIdentifier identifier(table->table->getMutableShare()->getSchemaName(), table->table->getMutableShare()->getTableName());
 
1868
      remove_table_from_cache(session, identifier,
 
1869
                              RTFC_WAIT_OTHER_THREAD_FLAG |
 
1870
                              RTFC_CHECK_KILLED_FLAG);
1831
1871
      session->exit_cond(old_message);
1832
 
      if (session->getKilled())
 
1872
      if (session->killed)
1833
1873
        goto err;
1834
1874
      open_for_modify= 0;
1835
1875
    }
1927
1967
        }
1928
1968
        else
1929
1969
        {
1930
 
          boost::unique_lock<boost::mutex> lock(table::Cache::singleton().mutex());
1931
 
          TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1932
 
          table::Cache::singleton().removeTable(session, identifier, RTFC_NO_FLAG);
 
1970
          pthread_mutex_lock(&LOCK_open);
 
1971
          TableIdentifier identifier(table->table->getMutableShare()->getSchemaName(), table->table->getMutableShare()->getTableName());
 
1972
          remove_table_from_cache(session, identifier, RTFC_NO_FLAG);
 
1973
          pthread_mutex_unlock(&LOCK_open);
1933
1974
        }
1934
1975
      }
1935
1976
    }
1959
2000
    Altough exclusive name-lock on target table protects us from concurrent
1960
2001
    DML and DDL operations on it we still want to wrap .FRM creation and call
1961
2002
    to plugin::StorageEngine::createTable() in critical section protected by
1962
 
    table::Cache::singleton().mutex() in order to provide minimal atomicity against operations which
 
2003
    LOCK_open in order to provide minimal atomicity against operations which
1963
2004
    disregard name-locks, like I_S implementation, for example. This is a
1964
2005
    temporary and should not be copied. Instead we should fix our code to
1965
2006
    always honor name-locks.
1966
2007
 
1967
 
    Also some engines (e.g. NDB cluster) require that table::Cache::singleton().mutex() should be held
 
2008
    Also some engines (e.g. NDB cluster) require that LOCK_open should be held
1968
2009
    during the call to plugin::StorageEngine::createTable().
1969
2010
    See bug #28614 for more info.
1970
2011
  */
1971
2012
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1972
 
                                 const TableIdentifier &destination_identifier,
1973
 
                                 const TableIdentifier &src_table,
 
2013
                                 TableIdentifier &destination_identifier,
 
2014
                                 TableIdentifier &src_table,
1974
2015
                                 bool is_engine_set)
1975
2016
{
1976
2017
  int protoerr= EEXIST;
1977
2018
  message::Table new_proto;
1978
 
  message::table::shared_ptr src_proto;
 
2019
  message::Table src_proto;
1979
2020
 
1980
2021
  protoerr= plugin::StorageEngine::getTableDefinition(session,
1981
2022
                                                      src_table,
1982
2023
                                                      src_proto);
1983
 
  new_proto.CopyFrom(*src_proto);
 
2024
  new_proto.CopyFrom(src_proto);
1984
2025
 
1985
2026
  if (destination_identifier.isTmp())
1986
2027
  {
2046
2087
*/
2047
2088
 
2048
2089
bool mysql_create_like_table(Session* session,
2049
 
                             const TableIdentifier &destination_identifier,
 
2090
                             TableIdentifier &destination_identifier,
2050
2091
                             TableList* table, TableList* src_table,
2051
2092
                             message::Table &create_table_proto,
2052
2093
                             bool is_if_not_exists,
2053
2094
                             bool is_engine_set)
2054
2095
{
 
2096
  char *db= table->db;
 
2097
  char *table_name= table->table_name;
2055
2098
  bool res= true;
2056
2099
  uint32_t not_used;
2057
2100
 
2067
2110
  if (session->open_tables_from_list(&src_table, &not_used))
2068
2111
    return true;
2069
2112
 
2070
 
  TableIdentifier src_identifier(src_table->table->getShare()->getSchemaName(),
2071
 
                                 src_table->table->getShare()->getTableName(), src_table->table->getShare()->getType());
 
2113
  TableIdentifier src_identifier(src_table->table->getMutableShare()->getSchemaName(),
 
2114
                                 src_table->table->getMutableShare()->getTableName(), src_table->table->getMutableShare()->getType());
2072
2115
 
2073
2116
 
2074
2117
 
2081
2124
  bool table_exists= false;
2082
2125
  if (destination_identifier.isTmp())
2083
2126
  {
2084
 
    if (session->find_temporary_table(destination_identifier))
 
2127
    if (session->find_temporary_table(db, table_name))
2085
2128
    {
2086
2129
      table_exists= true;
2087
2130
    }
2112
2155
    {
2113
2156
      if (name_lock)
2114
2157
      {
2115
 
        boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* unlink open tables for create table like*/
 
2158
        pthread_mutex_lock(&LOCK_open); /* unlink open tables for create table like*/
2116
2159
        session->unlink_open_table(name_lock);
 
2160
        pthread_mutex_unlock(&LOCK_open);
2117
2161
      }
2118
2162
 
2119
2163
      return res;
2129
2173
    }
2130
2174
    else // Otherwise we create the table
2131
2175
    {
2132
 
      bool was_created;
2133
 
      {
2134
 
        boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* We lock for CREATE TABLE LIKE to copy table definition */
2135
 
        was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
2136
 
                                               src_identifier, is_engine_set);
2137
 
      }
 
2176
      pthread_mutex_lock(&LOCK_open); /* We lock for CREATE TABLE LIKE to copy table definition */
 
2177
      bool was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
 
2178
                                             src_identifier, is_engine_set);
 
2179
      pthread_mutex_unlock(&LOCK_open);
2138
2180
 
2139
2181
      // So we blew the creation of the table, and we scramble to clean up
2140
2182
      // anything that might have been created (read... it is a hack)
2141
2183
      if (not was_created)
2142
2184
      {
2143
 
        plugin::StorageEngine::dropTable(*session, destination_identifier);
 
2185
        quick_rm_table(*session, destination_identifier);
2144
2186
      } 
2145
2187
      else
2146
2188
      {
2150
2192
 
2151
2193
    if (name_lock)
2152
2194
    {
2153
 
      boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* unlink open tables for create table like*/
 
2195
      pthread_mutex_lock(&LOCK_open); /* unlink open tables for create table like*/
2154
2196
      session->unlink_open_table(name_lock);
 
2197
      pthread_mutex_unlock(&LOCK_open);
2155
2198
    }
2156
2199
  }
2157
2200
 
2161
2204
    {
2162
2205
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
2163
2206
      snprintf(warn_buff, sizeof(warn_buff),
2164
 
               ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
 
2207
               ER(ER_TABLE_EXISTS_ERROR), table_name);
2165
2208
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2166
2209
                   ER_TABLE_EXISTS_ERROR,warn_buff);
2167
2210
      res= false;
2168
2211
    }
2169
2212
    else
2170
2213
    {
2171
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());
 
2214
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
2172
2215
    }
2173
2216
  }
2174
2217