~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: pawel
  • Date: 2010-03-29 20:16:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1428.
  • Revision ID: pawel@paw-20100329201608-ndqnc736k47uvy3s
changed function-like defines into functions in some files

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
    let's fetch the database default character set and
85
85
    apply it to the table.
86
86
  */
87
 
  SchemaIdentifier identifier(db);
88
87
  if (create_info->default_table_charset == NULL)
89
 
    create_info->default_table_charset= plugin::StorageEngine::getSchemaCollation(identifier);
 
88
    create_info->default_table_charset= plugin::StorageEngine::getSchemaCollation(db);
90
89
}
91
90
 
92
91
/*
237
236
        goto err_with_placeholders;
238
237
      }
239
238
    }
240
 
    TableIdentifier identifier(db, table->table_name, table->internal_tmp_table ? message::Table::INTERNAL : message::Table::STANDARD);
 
239
    TableIdentifier identifier(db, table->table_name, table->internal_tmp_table ? INTERNAL_TMP_TABLE : STANDARD_TABLE);
241
240
 
242
241
    if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
243
242
    {
285
284
  */
286
285
  pthread_mutex_unlock(&LOCK_open);
287
286
  error= 0;
288
 
 
289
287
  if (wrong_tables.length())
290
288
  {
291
 
    if (not foreign_key_error)
292
 
    {
 
289
    if (!foreign_key_error)
293
290
      my_printf_error(ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR), MYF(0),
294
291
                      wrong_tables.c_ptr());
295
 
    }
296
292
    else
297
293
    {
298
294
      my_message(ER_ROW_IS_REFERENCED, ER(ER_ROW_IS_REFERENCED), MYF(0));
301
297
  }
302
298
 
303
299
  pthread_mutex_lock(&LOCK_open); /* final bit in rm table lock */
304
 
 
305
300
err_with_placeholders:
306
301
  unlock_table_names(tables, NULL);
307
302
  pthread_mutex_unlock(&LOCK_open);
308
303
  session->no_warnings_for_error= 0;
309
304
 
310
 
  return error;
 
305
  return(error);
311
306
}
312
307
 
313
308
 
1292
1287
                                message::Table &table_proto,
1293
1288
                                AlterInfo *alter_info,
1294
1289
                                bool is_if_not_exists,
 
1290
                                bool lex_identified_temp_table,
1295
1291
                                bool internal_tmp_table,
1296
1292
                                uint db_options,
1297
1293
                                uint key_count,
1307
1303
      to create a table under a temporary table.
1308
1304
    */
1309
1305
    bool exists= 
1310
 
      plugin::StorageEngine::doesTableExist(*session, identifier, 
1311
 
                                            identifier.getType() != message::Table::STANDARD );
 
1306
      plugin::StorageEngine::doesTableExist(*session, identifier, lex_identified_temp_table);
1312
1307
 
1313
1308
    if (exists)
1314
1309
    {
1322
1317
        return error;
1323
1318
      }
1324
1319
 
1325
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
 
1320
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getTableName().c_str());
1326
1321
      return error;
1327
1322
    }
1328
1323
 
1329
 
    if (identifier.getType() == message::Table::STANDARD) // We have a real table
 
1324
    if (not lex_identified_temp_table) // We have a real table
1330
1325
    {
1331
1326
      /*
1332
1327
        We don't assert here, but check the result, because the table could be
1341
1336
      */
1342
1337
      if (TableShare::getShare(identifier))
1343
1338
      {
1344
 
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
 
1339
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getTableName().c_str());
1345
1340
        return error;
1346
1341
      }
1347
1342
    }
1360
1355
    return error;
1361
1356
  }
1362
1357
 
1363
 
  if (identifier.getType() == message::Table::TEMPORARY)
 
1358
  if (lex_identified_temp_table)
1364
1359
  {
1365
1360
    /* Open table and put in temporary table list */
1366
1361
    if (not (session->open_temporary_table(identifier)))
1429
1424
  KEY           *key_info_buffer;
1430
1425
  bool          error= true;
1431
1426
  TableShare share;
 
1427
  bool lex_identified_temp_table=
 
1428
    (table_proto.type() == message::Table::TEMPORARY);
1432
1429
 
1433
1430
  /* Check for duplicate fields and check type of table to create */
1434
1431
  if (not alter_info->create_list.elements)
1443
1440
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
1444
1441
    db_options|=HA_OPTION_PACK_RECORD;
1445
1442
 
1446
 
  set_table_default_charset(create_info, identifier.getSchemaName().c_str());
 
1443
  set_table_default_charset(create_info, identifier.getDBName().c_str());
1447
1444
 
1448
1445
  /* Build a Table object to pass down to the engine, and the do the actual create. */
1449
1446
  if (not mysql_prepare_create_table(session, create_info, table_proto, alter_info,
1459
1456
                               table_proto,
1460
1457
                               alter_info,
1461
1458
                               is_if_not_exists,
 
1459
                               lex_identified_temp_table,
1462
1460
                               internal_tmp_table,
1463
1461
                               db_options, key_count,
1464
1462
                               key_info_buffer);
1501
1499
    }
1502
1500
    else
1503
1501
    {
1504
 
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getSQLPath().c_str());
 
1502
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), identifier.getTableName().c_str());
1505
1503
      result= true;
1506
1504
    }
1507
1505
  }
1540
1538
                        uint32_t select_field_count,
1541
1539
                        bool is_if_not_exists)
1542
1540
{
1543
 
  if (identifier.isTmp())
 
1541
  bool lex_identified_temp_table=
 
1542
    (table_proto.type() == message::Table::TEMPORARY);
 
1543
 
 
1544
  if (lex_identified_temp_table)
1544
1545
  {
1545
1546
    return mysql_create_table_no_lock(session,
1546
1547
                                      identifier,
1551
1552
                                      select_field_count,
1552
1553
                                      is_if_not_exists);
1553
1554
  }
1554
 
 
1555
 
  return drizzle_create_table(session,
1556
 
                              identifier,
1557
 
                              create_info,
1558
 
                              table_proto,
1559
 
                              alter_info,
1560
 
                              internal_tmp_table,
1561
 
                              select_field_count,
1562
 
                              is_if_not_exists);
 
1555
  else
 
1556
  {
 
1557
    return drizzle_create_table(session,
 
1558
                                identifier,
 
1559
                                create_info,
 
1560
                                table_proto,
 
1561
                                alter_info,
 
1562
                                internal_tmp_table,
 
1563
                                select_field_count,
 
1564
                                is_if_not_exists);
 
1565
  }
1563
1566
}
1564
1567
 
1565
1568
 
1628
1631
*/
1629
1632
 
1630
1633
bool
1631
 
mysql_rename_table(plugin::StorageEngine *base,
1632
 
                   TableIdentifier &from,
1633
 
                   TableIdentifier &to,
1634
 
                   uint32_t )
 
1634
mysql_rename_table(plugin::StorageEngine *base, const char *old_db,
 
1635
                   const char *old_name, const char *new_db,
 
1636
                   const char *new_name, uint32_t flags)
1635
1637
{
1636
1638
  Session *session= current_session;
 
1639
  string from;
 
1640
  string to;
1637
1641
  int error= 0;
1638
1642
 
1639
1643
  assert(base);
1640
1644
 
1641
 
  if (not plugin::StorageEngine::doesSchemaExist(to))
 
1645
  build_table_filename(from, old_db, old_name,
 
1646
                       flags & FN_FROM_IS_TMP);
 
1647
  build_table_filename(to, new_db, new_name,
 
1648
                       flags & FN_TO_IS_TMP);
 
1649
 
 
1650
  if (!(error= base->renameTable(session, from.c_str(), to.c_str())))
1642
1651
  {
1643
 
    my_error(ER_NO_DB_ERROR, MYF(0), to.getSchemaName().c_str());
1644
 
    return true;
 
1652
    if (base->check_flag(HTON_BIT_HAS_DATA_DICTIONARY) == 0
 
1653
       && rename_table_proto_file(from.c_str(), to.c_str()))
 
1654
    {
 
1655
      error= errno;
 
1656
      base->renameTable(session, to.c_str(), from.c_str());
 
1657
    }
1645
1658
  }
1646
1659
 
1647
 
  error= base->renameTable(*session, from, to);
1648
 
 
1649
1660
  if (error == HA_ERR_WRONG_COMMAND)
1650
 
  {
1651
1661
    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "ALTER Table");
1652
 
  }
1653
1662
  else if (error)
1654
 
  {
1655
 
    const char *from_identifier= from.isTmp() ? "#sql-temporary" : from.getSQLPath().c_str();
1656
 
    const char *to_identifier= to.isTmp() ? "#sql-temporary" : to.getSQLPath().c_str();
1657
 
 
1658
 
    my_error(ER_ERROR_ON_RENAME, MYF(0), from_identifier, to_identifier, error);
1659
 
  }
1660
 
 
1661
 
  return error ? true : false; 
 
1663
    my_error(ER_ERROR_ON_RENAME, MYF(0), from.c_str(), to.c_str(), error);
 
1664
 
 
1665
  return(error != 0);
1662
1666
}
1663
1667
 
1664
1668
 
1775
1779
    char* db = table->db;
1776
1780
    bool fatal_error=0;
1777
1781
 
1778
 
    snprintf(table_name, sizeof(table_name), "%s.%s",db,table->table_name);
 
1782
    sprintf(table_name,"%s.%s",db,table->table_name);
1779
1783
    table->lock_type= lock_type;
1780
1784
    /* open only one table from local list of command */
1781
1785
    {
2042
2046
    during the call to plugin::StorageEngine::createTable().
2043
2047
    See bug #28614 for more info.
2044
2048
  */
2045
 
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
 
2049
static bool create_table_wrapper(Session &session, message::Table& create_table_proto,
2046
2050
                                 TableIdentifier &destination_identifier,
2047
2051
                                 TableIdentifier &src_table,
2048
 
                                 bool is_engine_set)
 
2052
                                 bool lex_identified_temp_table, bool is_engine_set)
2049
2053
{
2050
2054
  int protoerr= EEXIST;
2051
2055
  message::Table new_proto;
2056
2060
                                                      src_proto);
2057
2061
  new_proto.CopyFrom(src_proto);
2058
2062
 
2059
 
  if (destination_identifier.isTmp())
 
2063
  if (lex_identified_temp_table)
2060
2064
  {
2061
2065
    new_proto.set_type(message::Table::TEMPORARY);
2062
2066
  }
2073
2077
    protoengine->set_name(create_table_proto.engine().name());
2074
2078
  }
2075
2079
 
2076
 
  { // We now do a selective copy of elements on to the new table.
2077
 
    new_proto.set_name(create_table_proto.name());
2078
 
    new_proto.set_schema(create_table_proto.schema());
2079
 
    new_proto.set_catalog(create_table_proto.catalog());
2080
 
  }
2081
 
 
2082
2080
  if (protoerr && protoerr != EEXIST)
2083
2081
  {
2084
2082
    if (errno == ENOENT)
2119
2117
bool mysql_create_like_table(Session* session,
2120
2118
                             TableIdentifier &destination_identifier,
2121
2119
                             TableList* table, TableList* src_table,
2122
 
                             message::Table &create_table_proto,
 
2120
                             message::Table& create_table_proto,
2123
2121
                             bool is_if_not_exists,
2124
2122
                             bool is_engine_set)
2125
2123
{
2128
2126
  char *table_name= table->table_name;
2129
2127
  bool res= true;
2130
2128
  uint32_t not_used;
 
2129
  bool lex_identified_temp_table=
 
2130
    (create_table_proto.type() == message::Table::TEMPORARY);
2131
2131
  bool was_created;
2132
2132
 
2133
2133
  /*
2152
2152
    was already checked when it was added to the table list.
2153
2153
  */
2154
2154
  bool table_exists= false;
2155
 
  if (destination_identifier.isTmp())
 
2155
  if (lex_identified_temp_table)
2156
2156
  {
2157
2157
    if (session->find_temporary_table(db, table_name))
2158
2158
    {
2203
2203
  {
2204
2204
    pthread_mutex_lock(&LOCK_open); /* We lock for CREATE TABLE LIKE to copy table definition */
2205
2205
    was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
2206
 
                                      src_identifier, is_engine_set);
 
2206
                                      src_identifier, lex_identified_temp_table, is_engine_set);
2207
2207
    pthread_mutex_unlock(&LOCK_open);
2208
2208
 
2209
2209
    // So we blew the creation of the table, and we scramble to clean up
2210
2210
    // anything that might have been created (read... it is a hack)
2211
2211
    if (not was_created)
2212
2212
    {
2213
 
      if (destination_identifier.isTmp())
 
2213
      if (lex_identified_temp_table)
2214
2214
      {
2215
2215
        (void) session->rm_temporary_table(destination_identifier);
2216
2216
      }
2219
2219
        quick_rm_table(*session, destination_identifier);
2220
2220
      }
2221
2221
    } 
2222
 
    else if (destination_identifier.isTmp() && not session->open_temporary_table(destination_identifier))
 
2222
    else if (lex_identified_temp_table && not session->open_temporary_table(destination_identifier))
2223
2223
    {
2224
2224
      // We created, but we can't open... also, a hack.
2225
2225
      (void) session->rm_temporary_table(destination_identifier);
2226
2226
    }
2227
2227
    else
2228
2228
    {
2229
 
      if (not destination_identifier.isTmp())
 
2229
      if (not lex_identified_temp_table)
2230
2230
      {
2231
2231
        bool rc= replicateCreateTableLike(session, table, name_lock, (src_table->table->s->tmp_table), is_if_not_exists);
2232
2232
        (void)rc;
2289
2289
    char table_name[NAME_LEN*2+2];
2290
2290
    Table *t;
2291
2291
 
2292
 
    snprintf(table_name, sizeof(table_name), "%s.%s",table->db,table->table_name);
 
2292
    sprintf(table_name,"%s.%s",table->db,table->table_name);
2293
2293
 
2294
2294
    t= table->table= session->openTableLock(table, TL_READ);
2295
2295
    session->clear_error();                     // these errors shouldn't get client