~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: LinuxJedi
  • Date: 2010-08-28 09:23:52 UTC
  • mto: (1738.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1739.
  • Revision ID: linuxjedi@linuxjedi-laptop-20100828092352-oe3zbtdy05kq9dtq
Make exit happen in main thread rather than signal handler thread thus avoiding a segfault due to a double kill of the signal handler thread

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
  LOCK_open.lock(); /* 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
    LOCK_open.unlock();
 
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
  LOCK_open.unlock();
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
  LOCK_open.lock(); /* 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
  LOCK_open.unlock();
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);
1265
1325
}
1266
1326
 
1267
1327
static bool locked_create_event(Session *session,
1268
 
                                const TableIdentifier &identifier,
 
1328
                                TableIdentifier &identifier,
1269
1329
                                HA_CREATE_INFO *create_info,
1270
1330
                                message::Table &table_proto,
1271
1331
                                AlterInfo *alter_info,
1300
1360
        return error;
1301
1361
      }
1302
1362
 
1303
 
      std::string path;
1304
 
      identifier.getSQLPath(path);
1305
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1306
 
 
 
1363
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
1307
1364
      return error;
1308
1365
    }
1309
1366
 
1320
1377
      /*
1321
1378
        @todo improve this error condition.
1322
1379
      */
1323
 
      if (definition::Cache::singleton().find(identifier.getKey()))
 
1380
      if (TableShare::getShare(identifier))
1324
1381
      {
1325
 
        std::string path;
1326
 
        identifier.getSQLPath(path);
1327
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1328
 
 
 
1382
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
1329
1383
        return error;
1330
1384
      }
1331
1385
    }
1401
1455
*/
1402
1456
 
1403
1457
bool mysql_create_table_no_lock(Session *session,
1404
 
                                const TableIdentifier &identifier,
 
1458
                                TableIdentifier &identifier,
1405
1459
                                HA_CREATE_INFO *create_info,
1406
1460
                                message::Table &table_proto,
1407
1461
                                AlterInfo *alter_info,
1427
1481
 
1428
1482
  /* Build a Table object to pass down to the engine, and the do the actual create. */
1429
1483
  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))
 
1484
                                 internal_tmp_table,
 
1485
                                 &db_options,
 
1486
                                 &key_info_buffer, &key_count,
 
1487
                                 select_field_count))
1434
1488
  {
1435
 
    boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* CREATE TABLE (some confussion on naming, double check) */
 
1489
    boost::mutex::scoped_lock lock(LOCK_open); /* CREATE TABLE (some confussion on naming, double check) */
1436
1490
    error= locked_create_event(session,
1437
1491
                               identifier,
1438
1492
                               create_info,
1453
1507
  @note the following two methods implement create [temporary] table.
1454
1508
*/
1455
1509
static bool drizzle_create_table(Session *session,
1456
 
                                 const TableIdentifier &identifier,
 
1510
                                 TableIdentifier &identifier,
1457
1511
                                 HA_CREATE_INFO *create_info,
1458
1512
                                 message::Table &table_proto,
1459
1513
                                 AlterInfo *alter_info,
1480
1534
    }
1481
1535
    else
1482
1536
    {
1483
 
      std::string path;
1484
 
      identifier.getSQLPath(path);
1485
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
 
1537
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
1486
1538
      result= true;
1487
1539
    }
1488
1540
  }
1500
1552
 
1501
1553
  if (name_lock)
1502
1554
  {
1503
 
    boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* Lock for removing name_lock during table create */
 
1555
    boost::mutex::scoped_lock lock(LOCK_open); /* Lock for removing name_lock during table create */
1504
1556
    session->unlink_open_table(name_lock);
1505
1557
  }
1506
1558
 
1512
1564
  Database locking aware wrapper for mysql_create_table_no_lock(),
1513
1565
*/
1514
1566
bool mysql_create_table(Session *session,
1515
 
                        const TableIdentifier &identifier,
 
1567
                        TableIdentifier &identifier,
1516
1568
                        HA_CREATE_INFO *create_info,
1517
1569
                        message::Table &table_proto,
1518
1570
                        AlterInfo *alter_info,
1608
1660
bool
1609
1661
mysql_rename_table(Session &session,
1610
1662
                   plugin::StorageEngine *base,
1611
 
                   const TableIdentifier &from,
1612
 
                   const TableIdentifier &to)
 
1663
                   TableIdentifier &from,
 
1664
                   TableIdentifier &to)
1613
1665
{
1614
1666
  int error= 0;
1615
1667
 
1629
1681
  }
1630
1682
  else if (error)
1631
1683
  {
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();
 
1684
    const char *from_identifier= from.isTmp() ? "#sql-temporary" : from.getSQLPath().c_str();
 
1685
    const char *to_identifier= to.isTmp() ? "#sql-temporary" : to.getSQLPath().c_str();
1640
1686
 
1641
1687
    my_error(ER_ERROR_ON_RENAME, MYF(0), from_identifier, to_identifier, error);
1642
1688
  }
1660
1706
   the table is closed.
1661
1707
 
1662
1708
  PREREQUISITES
1663
 
    Lock on table::Cache::singleton().mutex()
 
1709
    Lock on LOCK_open
1664
1710
    Win32 clients must also have a WRITE LOCK on the table !
1665
1711
*/
1666
1712
 
1668
1714
                              enum ha_extra_function function)
1669
1715
{
1670
1716
 
1671
 
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
 
1717
  safe_mutex_assert_owner(LOCK_open.native_handle());
1672
1718
 
1673
1719
  table->cursor->extra(function);
1674
1720
  /* Mark all tables that are in use as 'old' */
1675
 
  session->abortLock(table);    /* end threads waiting on lock */
 
1721
  mysql_lock_abort(session, table);     /* end threads waiting on lock */
1676
1722
 
1677
1723
  /* 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);
 
1724
  TableIdentifier identifier(table->getMutableShare()->getSchemaName(), table->getMutableShare()->getTableName());
 
1725
  remove_table_from_cache(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG);
1680
1726
}
1681
1727
 
1682
1728
/*
1692
1738
    reopen the table.
1693
1739
 
1694
1740
  PREREQUISITES
1695
 
    Lock on table::Cache::singleton().mutex()
 
1741
    Lock on LOCK_open
1696
1742
    Win32 clients must also have a WRITE LOCK on the table !
1697
1743
*/
1698
1744
 
1703
1749
  /* Close lock if this is not got with LOCK TABLES */
1704
1750
  if (lock)
1705
1751
  {
1706
 
    unlockTables(lock);
 
1752
    mysql_unlock_tables(this, lock);
1707
1753
    lock= NULL;                 // Start locked threads
1708
1754
  }
1709
1755
  /* Close all copies of 'table'.  This also frees all LOCK TABLES lock */
1710
1756
  unlink_open_table(table);
1711
1757
 
1712
 
  /* When lock on table::Cache::singleton().mutex() is freed other threads can continue */
1713
 
  locking::broadcast_refresh();
 
1758
  /* When lock on LOCK_open is freed other threads can continue */
 
1759
  broadcast_refresh();
1714
1760
}
1715
1761
 
1716
1762
/*
1754
1800
  for (table= tables; table; table= table->next_local)
1755
1801
  {
1756
1802
    char table_name[NAME_LEN*2+2];
 
1803
    char* db = table->db;
1757
1804
    bool fatal_error=0;
1758
1805
 
1759
 
    snprintf(table_name, sizeof(table_name), "%s.%s", table->getSchemaName(), table->getTableName());
 
1806
    snprintf(table_name, sizeof(table_name), "%s.%s",db,table->table_name);
1760
1807
    table->lock_type= lock_type;
1761
1808
    /* open only one table from local list of command */
1762
1809
    {
1822
1869
    /* Close all instances of the table to allow repair to rename files */
1823
1870
    if (lock_type == TL_WRITE && table->table->getShare()->getVersion())
1824
1871
    {
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(),
 
1872
      LOCK_open.lock(); /* Lock type is TL_WRITE and we lock to repair the table */
 
1873
      const char *old_message=session->enter_cond(COND_refresh, LOCK_open,
1827
1874
                                                  "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);
 
1875
      mysql_lock_abort(session,table->table);
 
1876
      TableIdentifier identifier(table->table->getMutableShare()->getSchemaName(), table->table->getMutableShare()->getTableName());
 
1877
      remove_table_from_cache(session, identifier,
 
1878
                              RTFC_WAIT_OTHER_THREAD_FLAG |
 
1879
                              RTFC_CHECK_KILLED_FLAG);
1831
1880
      session->exit_cond(old_message);
1832
 
      if (session->getKilled())
 
1881
      if (session->killed)
1833
1882
        goto err;
1834
1883
      open_for_modify= 0;
1835
1884
    }
1927
1976
        }
1928
1977
        else
1929
1978
        {
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);
 
1979
          boost::mutex::scoped_lock lock(LOCK_open);
 
1980
          TableIdentifier identifier(table->table->getMutableShare()->getSchemaName(), table->table->getMutableShare()->getTableName());
 
1981
          remove_table_from_cache(session, identifier, RTFC_NO_FLAG);
1933
1982
        }
1934
1983
      }
1935
1984
    }
1959
2008
    Altough exclusive name-lock on target table protects us from concurrent
1960
2009
    DML and DDL operations on it we still want to wrap .FRM creation and call
1961
2010
    to plugin::StorageEngine::createTable() in critical section protected by
1962
 
    table::Cache::singleton().mutex() in order to provide minimal atomicity against operations which
 
2011
    LOCK_open in order to provide minimal atomicity against operations which
1963
2012
    disregard name-locks, like I_S implementation, for example. This is a
1964
2013
    temporary and should not be copied. Instead we should fix our code to
1965
2014
    always honor name-locks.
1966
2015
 
1967
 
    Also some engines (e.g. NDB cluster) require that table::Cache::singleton().mutex() should be held
 
2016
    Also some engines (e.g. NDB cluster) require that LOCK_open should be held
1968
2017
    during the call to plugin::StorageEngine::createTable().
1969
2018
    See bug #28614 for more info.
1970
2019
  */
1971
2020
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1972
 
                                 const TableIdentifier &destination_identifier,
1973
 
                                 const TableIdentifier &src_table,
 
2021
                                 TableIdentifier &destination_identifier,
 
2022
                                 TableIdentifier &src_table,
1974
2023
                                 bool is_engine_set)
1975
2024
{
1976
2025
  int protoerr= EEXIST;
1977
2026
  message::Table new_proto;
1978
 
  message::table::shared_ptr src_proto;
 
2027
  message::Table src_proto;
1979
2028
 
1980
2029
  protoerr= plugin::StorageEngine::getTableDefinition(session,
1981
2030
                                                      src_table,
1982
2031
                                                      src_proto);
1983
 
  new_proto.CopyFrom(*src_proto);
 
2032
  new_proto.CopyFrom(src_proto);
1984
2033
 
1985
2034
  if (destination_identifier.isTmp())
1986
2035
  {
2046
2095
*/
2047
2096
 
2048
2097
bool mysql_create_like_table(Session* session,
2049
 
                             const TableIdentifier &destination_identifier,
 
2098
                             TableIdentifier &destination_identifier,
2050
2099
                             TableList* table, TableList* src_table,
2051
2100
                             message::Table &create_table_proto,
2052
2101
                             bool is_if_not_exists,
2053
2102
                             bool is_engine_set)
2054
2103
{
 
2104
  char *db= table->db;
 
2105
  char *table_name= table->table_name;
2055
2106
  bool res= true;
2056
2107
  uint32_t not_used;
2057
2108
 
2067
2118
  if (session->open_tables_from_list(&src_table, &not_used))
2068
2119
    return true;
2069
2120
 
2070
 
  TableIdentifier src_identifier(src_table->table->getShare()->getSchemaName(),
2071
 
                                 src_table->table->getShare()->getTableName(), src_table->table->getShare()->getType());
 
2121
  TableIdentifier src_identifier(src_table->table->getMutableShare()->getSchemaName(),
 
2122
                                 src_table->table->getMutableShare()->getTableName(), src_table->table->getMutableShare()->getType());
2072
2123
 
2073
2124
 
2074
2125
 
2081
2132
  bool table_exists= false;
2082
2133
  if (destination_identifier.isTmp())
2083
2134
  {
2084
 
    if (session->find_temporary_table(destination_identifier))
 
2135
    if (session->find_temporary_table(db, table_name))
2085
2136
    {
2086
2137
      table_exists= true;
2087
2138
    }
2112
2163
    {
2113
2164
      if (name_lock)
2114
2165
      {
2115
 
        boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* unlink open tables for create table like*/
 
2166
        boost::mutex::scoped_lock lock(LOCK_open); /* unlink open tables for create table like*/
2116
2167
        session->unlink_open_table(name_lock);
2117
2168
      }
2118
2169
 
2131
2182
    {
2132
2183
      bool was_created;
2133
2184
      {
2134
 
        boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* We lock for CREATE TABLE LIKE to copy table definition */
 
2185
        boost::mutex::scoped_lock lock(LOCK_open); /* We lock for CREATE TABLE LIKE to copy table definition */
2135
2186
        was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
2136
2187
                                               src_identifier, is_engine_set);
2137
2188
      }
2140
2191
      // anything that might have been created (read... it is a hack)
2141
2192
      if (not was_created)
2142
2193
      {
2143
 
        plugin::StorageEngine::dropTable(*session, destination_identifier);
 
2194
        quick_rm_table(*session, destination_identifier);
2144
2195
      } 
2145
2196
      else
2146
2197
      {
2150
2201
 
2151
2202
    if (name_lock)
2152
2203
    {
2153
 
      boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* unlink open tables for create table like*/
 
2204
      boost::mutex::scoped_lock lock(LOCK_open); /* unlink open tables for create table like*/
2154
2205
      session->unlink_open_table(name_lock);
2155
2206
    }
2156
2207
  }
2161
2212
    {
2162
2213
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
2163
2214
      snprintf(warn_buff, sizeof(warn_buff),
2164
 
               ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
 
2215
               ER(ER_TABLE_EXISTS_ERROR), table_name);
2165
2216
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2166
2217
                   ER_TABLE_EXISTS_ERROR,warn_buff);
2167
2218
      res= false;
2168
2219
    }
2169
2220
    else
2170
2221
    {
2171
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());
 
2222
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table_name);
2172
2223
    }
2173
2224
  }
2174
2225