~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Brian Aker
  • Date: 2009-11-12 16:13:04 UTC
  • mfrom: (1211.1.7 staging)
  • Revision ID: brian@gaz-20091112161304-opamiauv36fg0n6u
Rollup of Brian, Padraig, and Stewart patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
}
76
76
 
77
77
/*
78
 
  Translate a file name to a table name (WL #1324).
 
78
  Translate a cursor name to a table name (WL #1324).
79
79
 
80
80
  SYNOPSIS
81
81
    filename_to_tablename()
82
 
      from                      The file name
 
82
      from                      The cursor name
83
83
      to                OUT     The table name
84
84
      to_length                 The size of the table name buffer.
85
85
 
125
125
 
126
126
 
127
127
/*
128
 
  Translate a table name to a file name (WL #1324).
 
128
  Translate a table name to a cursor name (WL #1324).
129
129
 
130
130
  SYNOPSIS
131
131
    tablename_to_filename()
132
132
      from                      The table name
133
 
      to                OUT     The file name
134
 
      to_length                 The size of the file name buffer.
 
133
      to                OUT     The cursor name
 
134
      to_length                 The size of the cursor name buffer.
135
135
 
136
136
  RETURN
137
137
    true if errors happen. false on success.
180
180
 
181
181
 
182
182
/*
183
 
  Creates path to a file: drizzle_data_dir/db/table.ext
 
183
  Creates path to a cursor: drizzle_data_dir/db/table.ext
184
184
 
185
185
  SYNOPSIS
186
186
   build_table_filename()
196
196
  NOTES
197
197
 
198
198
    Uses database and table name, and extension to create
199
 
    a file name in drizzle_data_dir. Database and table
 
199
    a cursor name in drizzle_data_dir. Database and table
200
200
    names are converted from system_charset_info into "fscs".
201
201
    Unless flags indicate a temporary table name.
202
202
    'db' is always converted.
269
269
 
270
270
 
271
271
/*
272
 
  Creates path to a file: drizzle_tmpdir/#sql1234_12_1.ext
 
272
  Creates path to a cursor: drizzle_tmpdir/#sql1234_12_1.ext
273
273
 
274
274
  SYNOPSIS
275
275
   build_tmptable_filename()
280
280
  NOTES
281
281
 
282
282
    Uses current_pid, thread_id, and tmp_table counter to create
283
 
    a file name in drizzle_tmpdir.
 
283
    a cursor name in drizzle_tmpdir.
284
284
 
285
285
  RETURN
286
286
    path length on success, 0 on failure
322
322
 
323
323
  DESCRIPTION
324
324
    Write the binlog if open, routine used in multiple places in this
325
 
    file
 
325
    cursor
326
326
*/
327
327
 
328
328
void write_bin_log(Session *session,
458
458
 
459
459
  /*
460
460
    If we have the table in the definition cache, we don't have to check the
461
 
    .frm file to find if the table is a normal table (not view) and what
 
461
    .frm cursor to find if the table is a normal table (not view) and what
462
462
    engine to use.
463
463
  */
464
464
 
518
518
        error= -1;
519
519
        goto err_with_placeholders;
520
520
      }
521
 
      /* remove .frm file and engine files */
 
521
      /* remove .frm cursor and engine files */
522
522
      path_length= build_table_filename(path, sizeof(path), db, table->table_name, table->internal_tmp_table);
523
523
    }
524
524
 
802
802
  case DRIZZLE_TYPE_NULL:
803
803
    sql_field->pack_flag=f_settype((uint32_t) sql_field->sql_type);
804
804
    break;
805
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
805
  case DRIZZLE_TYPE_DECIMAL:
806
806
    sql_field->pack_flag= 0;
807
807
    break;
808
808
  case DRIZZLE_TYPE_TIMESTAMP:
836
836
                               AlterInfo *alter_info,
837
837
                               bool tmp_table,
838
838
                               uint32_t *db_options,
839
 
                               Cursor *file,
 
839
                               Cursor *cursor,
840
840
                               KEY **key_info_buffer,
841
841
                               uint32_t *key_count,
842
842
                               int select_field_count)
856
856
 
857
857
  select_field_pos= alter_info->create_list.elements - select_field_count;
858
858
  null_fields=blob_columns=0;
859
 
  max_key_length= file->max_key_length();
 
859
  max_key_length= cursor->max_key_length();
860
860
 
861
861
  for (field_no=0; (sql_field=it++) ; field_no++)
862
862
  {
1080
1080
 
1081
1081
    if (prepare_create_field(sql_field, &blob_columns,
1082
1082
                             &timestamps, &timestamps_with_niladic,
1083
 
                             file->ha_table_flags()))
 
1083
                             cursor->ha_table_flags()))
1084
1084
      return(true);
1085
1085
    sql_field->offset= record_offset;
1086
1086
    if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
1098
1098
    return(true);
1099
1099
  }
1100
1100
  if (auto_increment &&
1101
 
      (file->ha_table_flags() & HA_NO_AUTO_INCREMENT))
 
1101
      (cursor->ha_table_flags() & HA_NO_AUTO_INCREMENT))
1102
1102
  {
1103
1103
    my_message(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT,
1104
1104
               ER(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT), MYF(0));
1105
1105
    return(true);
1106
1106
  }
1107
1107
 
1108
 
  if (blob_columns && (file->ha_table_flags() & HA_NO_BLOBS))
 
1108
  if (blob_columns && (cursor->ha_table_flags() & HA_NO_BLOBS))
1109
1109
  {
1110
1110
    my_message(ER_TABLE_CANT_HANDLE_BLOB, ER(ER_TABLE_CANT_HANDLE_BLOB),
1111
1111
               MYF(0));
1146
1146
      continue;
1147
1147
    }
1148
1148
    (*key_count)++;
1149
 
    tmp=file->max_key_parts();
 
1149
    tmp=cursor->max_key_parts();
1150
1150
    if (key->columns.elements > tmp)
1151
1151
    {
1152
1152
      my_error(ER_TOO_MANY_KEY_PARTS,MYF(0),tmp);
1195
1195
      return(true);
1196
1196
    }
1197
1197
  }
1198
 
  tmp=file->max_keys();
 
1198
  tmp=cursor->max_keys();
1199
1199
  if (*key_count > tmp)
1200
1200
  {
1201
1201
    my_error(ER_TOO_MANY_KEYS,MYF(0),tmp);
1311
1311
 
1312
1312
        if (sql_field->sql_type == DRIZZLE_TYPE_BLOB)
1313
1313
        {
1314
 
          if (! (file->ha_table_flags() & HA_CAN_INDEX_BLOBS))
 
1314
          if (! (cursor->ha_table_flags() & HA_CAN_INDEX_BLOBS))
1315
1315
          {
1316
1316
            my_error(ER_BLOB_USED_AS_KEY, MYF(0), column->field_name.str);
1317
1317
            return true;
1333
1333
          else
1334
1334
          {
1335
1335
            key_info->flags|= HA_NULL_PART_KEY;
1336
 
            if (! (file->ha_table_flags() & HA_NULL_IN_KEY))
 
1336
            if (! (cursor->ha_table_flags() & HA_NULL_IN_KEY))
1337
1337
            {
1338
1338
              my_error(ER_NULL_COLUMN_IN_INDEX, MYF(0), column->field_name.str);
1339
1339
              return true;
1342
1342
        }
1343
1343
        if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
1344
1344
        {
1345
 
          if (column_nr == 0 || (file->ha_table_flags() & HA_AUTO_PART_KEY))
 
1345
          if (column_nr == 0 || (cursor->ha_table_flags() & HA_AUTO_PART_KEY))
1346
1346
            auto_increment--;                   // Field is used
1347
1347
        }
1348
1348
      }
1357
1357
        if (sql_field->sql_type == DRIZZLE_TYPE_BLOB)
1358
1358
        {
1359
1359
          if ((length=column->length) > max_key_length ||
1360
 
              length > file->max_key_part_length())
 
1360
              length > cursor->max_key_part_length())
1361
1361
          {
1362
 
            length= min(max_key_length, file->max_key_part_length());
 
1362
            length= min(max_key_length, cursor->max_key_part_length());
1363
1363
            if (key->type == Key::MULTIPLE)
1364
1364
            {
1365
1365
              /* not a critical problem */
1384
1384
          my_message(ER_WRONG_SUB_KEY, ER(ER_WRONG_SUB_KEY), MYF(0));
1385
1385
          return(true);
1386
1386
        }
1387
 
        else if (!(file->ha_table_flags() & HA_NO_PREFIX_CHAR_KEYS))
 
1387
        else if (!(cursor->ha_table_flags() & HA_NO_PREFIX_CHAR_KEYS))
1388
1388
          length=column->length;
1389
1389
      }
1390
1390
      else if (length == 0)
1392
1392
        my_error(ER_WRONG_KEY_COLUMN, MYF(0), column->field_name.str);
1393
1393
          return(true);
1394
1394
      }
1395
 
      if (length > file->max_key_part_length())
 
1395
      if (length > cursor->max_key_part_length())
1396
1396
      {
1397
 
        length= file->max_key_part_length();
 
1397
        length= cursor->max_key_part_length();
1398
1398
        if (key->type == Key::MULTIPLE)
1399
1399
        {
1400
1400
          /* not a critical problem */
1474
1474
    key_info++;
1475
1475
  }
1476
1476
  if (!unique_key && !primary_key &&
1477
 
      (file->ha_table_flags() & HA_REQUIRE_PRIMARY_KEY))
 
1477
      (cursor->ha_table_flags() & HA_REQUIRE_PRIMARY_KEY))
1478
1478
  {
1479
1479
    my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
1480
1480
    return(true);
1602
1602
  uint32_t          path_length;
1603
1603
  uint          db_options, key_count;
1604
1604
  KEY           *key_info_buffer;
1605
 
  Cursor        *file;
 
1605
  Cursor        *cursor;
1606
1606
  bool          error= true;
 
1607
  TableShare share;
 
1608
 
1607
1609
  /* Check for duplicate fields and check type of table to create */
1608
1610
  if (!alter_info->create_list.elements)
1609
1611
  {
1617
1619
  db_options= create_info->table_options;
1618
1620
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
1619
1621
    db_options|=HA_OPTION_PACK_RECORD;
1620
 
  if (!(file= create_info->db_type->getCursor((TableShare*) 0, session->mem_root)))
 
1622
  if (!(cursor= create_info->db_type->getCursor(share, session->mem_root)))
1621
1623
  {
1622
1624
    my_error(ER_OUTOFMEMORY, MYF(0), sizeof(Cursor));
1623
1625
    return true;
1627
1629
 
1628
1630
  if (mysql_prepare_create_table(session, create_info, table_proto, alter_info,
1629
1631
                                 internal_tmp_table,
1630
 
                                 &db_options, file,
 
1632
                                 &db_options, cursor,
1631
1633
                                 &key_info_buffer, &key_count,
1632
1634
                                 select_field_count))
1633
1635
    goto err;
1689
1691
      We don't assert here, but check the result, because the table could be
1690
1692
      in the table definition cache and in the same time the .frm could be
1691
1693
      missing from the disk, in case of manual intervention which deletes
1692
 
      the .frm file. The user has to use FLUSH TABLES; to clear the cache.
 
1694
      the .frm cursor. The user has to use FLUSH TABLES; to clear the cache.
1693
1695
      Then she could create the table. This case is pretty obscure and
1694
1696
      therefore we don't introduce a new error message only for it.
1695
1697
    */
1707
1709
    unless user specified CREATE TABLE IF EXISTS
1708
1710
    The LOCK_open mutex has been locked to make sure no
1709
1711
    one else is attempting to discover the table. Since
1710
 
    it's not on disk as a frm file, no one could be using it!
 
1712
    it's not on disk as a frm cursor, no one could be using it!
1711
1713
  */
1712
1714
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1713
1715
  {
1781
1783
 
1782
1784
err:
1783
1785
  session->set_proc_info("After create");
1784
 
  delete file;
 
1786
  delete cursor;
1785
1787
  return(error);
1786
1788
}
1787
1789
 
1902
1904
      flags                     flags for build_table_filename().
1903
1905
                                FN_FROM_IS_TMP old_name is temporary.
1904
1906
                                FN_TO_IS_TMP   new_name is temporary.
1905
 
                                NO_FRM_RENAME  Don't rename the FRM file
 
1907
                                NO_FRM_RENAME  Don't rename the FRM cursor
1906
1908
                                but only the table in the storage engine.
1907
1909
 
1908
1910
  RETURN
1971
1973
 
1972
1974
  safe_mutex_assert_owner(&LOCK_open);
1973
1975
 
1974
 
  table->file->extra(function);
 
1976
  table->cursor->extra(function);
1975
1977
  /* Mark all tables that are in use as 'old' */
1976
1978
  mysql_lock_abort(session, table);     /* end threads waiting on lock */
1977
1979
 
2170
2172
        goto err;
2171
2173
    }
2172
2174
 
2173
 
    result_code = (table->table->file->*operator_func)(session, check_opt);
 
2175
    result_code = (table->table->cursor->*operator_func)(session, check_opt);
2174
2176
 
2175
2177
send_result:
2176
2178
 
2265
2267
      if (!result_code) // recreation went ok
2266
2268
      {
2267
2269
        if ((table->table= session->openTableLock(table, lock_type)) &&
2268
 
            ((result_code= table->table->file->ha_analyze(session, check_opt)) > 0))
 
2270
            ((result_code= table->table->cursor->ha_analyze(session, check_opt)) > 0))
2269
2271
          result_code= 0; // analyze went ok
2270
2272
      }
2271
2273
      if (result_code) // either mysql_recreate_table or analyze failed
2321
2323
      else if (open_for_modify)
2322
2324
      {
2323
2325
        if (table->table->s->tmp_table)
2324
 
          table->table->file->info(HA_STATUS_CONST);
 
2326
          table->table->cursor->info(HA_STATUS_CONST);
2325
2327
        else
2326
2328
        {
2327
2329
          pthread_mutex_lock(&LOCK_open);
2390
2392
  /*
2391
2393
    By opening source table we guarantee that it exists and no concurrent
2392
2394
    DDL operation will mess with it. Later we also take an exclusive
2393
 
    name-lock on target table name, which makes copying of .frm file,
 
2395
    name-lock on target table name, which makes copying of .frm cursor,
2394
2396
    call to plugin::StorageEngine::createTable() and binlogging atomic
2395
2397
    against concurrent DML and DDL operations on target table.
2396
2398
    Thus by holding both these "locks" we ensure that our statement is
2694
2696
    }
2695
2697
    else
2696
2698
    {
2697
 
      if (t->file->ha_table_flags() & HA_HAS_CHECKSUM &&
 
2699
      if (t->cursor->ha_table_flags() & HA_HAS_CHECKSUM &&
2698
2700
          !(check_opt->flags & T_EXTEND))
2699
 
        session->client->store((uint64_t)t->file->checksum());
2700
 
      else if (!(t->file->ha_table_flags() & HA_HAS_CHECKSUM) &&
 
2701
        session->client->store((uint64_t)t->cursor->checksum());
 
2702
      else if (!(t->cursor->ha_table_flags() & HA_HAS_CHECKSUM) &&
2701
2703
               (check_opt->flags & T_QUICK))
2702
2704
        session->client->store();
2703
2705
      else
2708
2710
 
2709
2711
        t->use_all_columns();
2710
2712
 
2711
 
        if (t->file->ha_rnd_init(1))
 
2713
        if (t->cursor->ha_rnd_init(1))
2712
2714
          session->client->store();
2713
2715
        else
2714
2716
        {
2715
2717
          for (;;)
2716
2718
          {
2717
2719
            ha_checksum row_crc= 0;
2718
 
            int error= t->file->rnd_next(t->record[0]);
 
2720
            int error= t->cursor->rnd_next(t->record[0]);
2719
2721
            if (unlikely(error))
2720
2722
            {
2721
2723
              if (error == HA_ERR_RECORD_DELETED)
2750
2752
            crc+= row_crc;
2751
2753
          }
2752
2754
          session->client->store((uint64_t)crc);
2753
 
          t->file->ha_rnd_end();
 
2755
          t->cursor->ha_rnd_end();
2754
2756
        }
2755
2757
      }
2756
2758
      session->clear_error();