~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Brian Aker
  • Date: 2009-06-09 01:59:10 UTC
  • mfrom: (1054.1.11 merge)
  • Revision ID: brian@gaz-20090609015910-0sfsovp2x8wfbr2c
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
328
328
 
329
329
  if (!drop_temporary)
330
330
  {
331
 
    if (!session->locked_tables &&
332
 
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
331
    if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
333
332
      return(true);
334
333
  }
335
334
 
430
429
    char *db=table->db;
431
430
    StorageEngine *table_type;
432
431
 
433
 
    error= drop_temporary_table(session, table);
 
432
    error= session->drop_temporary_table(table);
434
433
 
435
434
    switch (error) {
436
435
    case  0:
1693
1692
  if (mysql_prepare_create_table(session, create_info, alter_info,
1694
1693
                                 internal_tmp_table,
1695
1694
                                 &db_options, file,
1696
 
                          &key_info_buffer, &key_count,
1697
 
                          select_field_count))
 
1695
                                 &key_info_buffer, &key_count,
 
1696
                                 select_field_count))
1698
1697
    goto err;
1699
1698
 
1700
1699
      /* Check if table exists */
1718
1717
 
1719
1718
  /* Check if table already exists */
1720
1719
  if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1721
 
      find_temporary_table(session, db, table_name))
 
1720
      session->find_temporary_table(db, table_name))
1722
1721
  {
1723
1722
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1724
1723
    {
1879
1878
                        bool internal_tmp_table,
1880
1879
                        uint32_t select_field_count)
1881
1880
{
1882
 
  Table *name_lock= 0;
 
1881
  Table *name_lock= NULL;
1883
1882
  bool result;
1884
1883
 
1885
1884
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1886
1885
  {
1887
 
    if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
 
1886
    if (session->lock_table_name_if_not_cached(db, table_name, &name_lock))
1888
1887
    {
1889
1888
      result= true;
1890
1889
      goto unlock;
1891
1890
    }
1892
 
    if (!name_lock)
 
1891
    if (name_lock == NULL)
1893
1892
    {
1894
1893
      if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1895
1894
      {
1918
1917
  if (name_lock)
1919
1918
  {
1920
1919
    pthread_mutex_lock(&LOCK_open); /* Lock for removing name_lock during table create */
1921
 
    unlink_open_table(session, name_lock, false);
 
1920
    session->unlink_open_table(name_lock);
1922
1921
    pthread_mutex_unlock(&LOCK_open);
1923
1922
  }
1924
1923
 
2080
2079
    Win32 clients must also have a WRITE LOCK on the table !
2081
2080
*/
2082
2081
 
2083
 
void close_cached_table(Session *session, Table *table)
 
2082
void Session::close_cached_table(Table *table)
2084
2083
{
2085
2084
 
2086
 
  wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
 
2085
  wait_while_table_is_used(this, table, HA_EXTRA_FORCE_REOPEN);
2087
2086
  /* Close lock if this is not got with LOCK TABLES */
2088
 
  if (session->lock)
 
2087
  if (lock)
2089
2088
  {
2090
 
    mysql_unlock_tables(session, session->lock);
2091
 
    session->lock= NULL;                        // Start locked threads
 
2089
    mysql_unlock_tables(this, lock);
 
2090
    lock= NULL;                 // Start locked threads
2092
2091
  }
2093
2092
  /* Close all copies of 'table'.  This also frees all LOCK TABLES lock */
2094
 
  unlink_open_table(session, table, true);
 
2093
  unlink_open_table(table);
2095
2094
 
2096
2095
  /* When lock on LOCK_open is freed other threads can continue */
2097
2096
  broadcast_refresh();
2132
2131
    char key[MAX_DBKEY_LENGTH];
2133
2132
    uint32_t key_length;
2134
2133
 
2135
 
    key_length= create_table_def_key(key, table_list);
 
2134
    key_length= table_list->create_table_def_key(key);
2136
2135
    pthread_mutex_lock(&LOCK_open); /* Lock table for repair */
2137
2136
    if (!(share= (get_table_share(session, table_list, key, key_length, 0,
2138
2137
                                  &error))))
2195
2194
  if (table_list->table)
2196
2195
  {
2197
2196
    pthread_mutex_lock(&LOCK_open); /* Close for repair table */
2198
 
    close_cached_table(session, table);
 
2197
    session->close_cached_table(table);
2199
2198
    pthread_mutex_unlock(&LOCK_open);
2200
2199
  }
2201
2200
  if (lock_and_wait_for_table_name(session,table_list))
2236
2235
    to finish the repair in the handler later on.
2237
2236
  */
2238
2237
  pthread_mutex_lock(&LOCK_open); /* Lock for opening partially repaired table */
2239
 
  if (reopen_name_locked_table(session, table_list, true))
 
2238
  if (session->reopen_name_locked_table(table_list, true))
2240
2239
  {
2241
2240
    unlock_table_name(table_list);
2242
2241
    pthread_mutex_unlock(&LOCK_open);
2329
2328
      lex->query_tables_own_last= 0;
2330
2329
      session->no_warnings_for_error= no_warnings_for_error;
2331
2330
 
2332
 
      open_and_lock_tables(session, table);
 
2331
      session->open_and_lock_tables(table);
2333
2332
      session->no_warnings_for_error= 0;
2334
2333
      table->next_global= save_next_global;
2335
2334
      table->next_local= save_next_local;
2544
2543
      session->close_thread_tables();
2545
2544
      if (!result_code) // recreation went ok
2546
2545
      {
2547
 
        if ((table->table= open_ltable(session, table, lock_type)) &&
 
2546
        if ((table->table= session->open_ltable(table, lock_type)) &&
2548
2547
            ((result_code= table->table->file->ha_analyze(session, check_opt)) > 0))
2549
2548
          result_code= 0; // analyze went ok
2550
2549
      }
2822
2821
    we ensure that our statement is properly isolated from all concurrent
2823
2822
    operations which matter.
2824
2823
  */
2825
 
  if (open_tables(session, &src_table, &not_used, 0))
2826
 
    return(true);
 
2824
  if (session->open_tables_from_list(&src_table, &not_used, 0))
 
2825
    return true;
2827
2826
 
2828
2827
  strncpy(src_path, src_table->table->s->path.str, sizeof(src_path));
2829
2828
 
2833
2832
  */
2834
2833
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2835
2834
  {
2836
 
    if (find_temporary_table(session, db, table_name))
 
2835
    if (session->find_temporary_table(db, table_name))
2837
2836
      goto table_exists;
2838
2837
    dst_path_length= build_tmptable_filename(session, dst_path, sizeof(dst_path));
2839
2838
    create_info->table_options|= HA_CREATE_DELAY_KEY_WRITE;
2840
2839
  }
2841
2840
  else
2842
2841
  {
2843
 
    if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
 
2842
    if (session->lock_table_name_if_not_cached(db, table_name, &name_lock))
2844
2843
      goto err;
2845
2844
    if (!name_lock)
2846
2845
      goto table_exists;
2951
2950
        */
2952
2951
        table->table= name_lock;
2953
2952
        pthread_mutex_lock(&LOCK_open); /* Open new table we have just acquired */
2954
 
        if (reopen_name_locked_table(session, table, false))
 
2953
        if (session->reopen_name_locked_table(table, false))
2955
2954
        {
2956
2955
          pthread_mutex_unlock(&LOCK_open);
2957
2956
          goto err;
2988
2987
  if (name_lock)
2989
2988
  {
2990
2989
    pthread_mutex_lock(&LOCK_open); /* unlink open tables for create table like*/
2991
 
    unlink_open_table(session, name_lock, false);
 
2990
    session->unlink_open_table(name_lock);
2992
2991
    pthread_mutex_unlock(&LOCK_open);
2993
2992
  }
2994
2993
  return(res);
3040
3039
   not complain when we lock the table
3041
3040
 */
3042
3041
  session->tablespace_op= true;
3043
 
  if (!(table= open_ltable(session, table_list, TL_WRITE)))
 
3042
  if (!(table= session->open_ltable(table_list, TL_WRITE)))
3044
3043
  {
3045
3044
    session->tablespace_op= false;
3046
3045
    return -1;
3694
3693
  if (table_list && table_list->schema_table)
3695
3694
  {
3696
3695
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
3697
 
    return(true);
 
3696
    return true;
3698
3697
  }
3699
3698
 
3700
3699
  /*
3732
3731
    This code is wrong and will be removed, please do not copy.
3733
3732
  */
3734
3733
 
3735
 
  if (!(table= open_ltable(session, table_list, TL_WRITE_ALLOW_READ)))
 
3734
  if (!(table= session->open_ltable(table_list, TL_WRITE_ALLOW_READ)))
3736
3735
    return true;
3737
3736
  table->use_all_columns();
3738
3737
 
3760
3759
    {
3761
3760
      if (table->s->tmp_table != NO_TMP_TABLE)
3762
3761
      {
3763
 
        if (find_temporary_table(session,new_db,new_name_buff))
 
3762
        if (session->find_temporary_table(new_db, new_name_buff))
3764
3763
        {
3765
3764
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
3766
 
          return(true);
 
3765
          return true;
3767
3766
        }
3768
3767
      }
3769
3768
      else
3770
3769
      {
3771
 
        if (lock_table_name_if_not_cached(session, new_db, new_name, &name_lock))
3772
 
          return(true);
 
3770
        if (session->lock_table_name_if_not_cached(new_db, new_name, &name_lock))
 
3771
          return true;
 
3772
 
3773
3773
        if (!name_lock)
3774
3774
        {
3775
3775
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
3776
 
          return(true);
 
3776
          return true;
3777
3777
        }
3778
3778
 
3779
3779
        build_table_filename(new_name_buff, sizeof(new_name_buff),
3881
3881
        Then do a 'simple' rename of the table. First we need to close all
3882
3882
        instances of 'source' table.
3883
3883
      */
3884
 
      close_cached_table(session, table);
 
3884
      session->close_cached_table(table);
3885
3885
      /*
3886
3886
        Then, we want check once again that target table does not exist.
3887
3887
        Actually the order of these two steps does not matter since
3928
3928
      error= -1;
3929
3929
    }
3930
3930
    if (name_lock)
3931
 
      unlink_open_table(session, name_lock, false);
 
3931
      session->unlink_open_table(name_lock);
3932
3932
    pthread_mutex_unlock(&LOCK_open);
3933
3933
    table_list->table= NULL;                    // For query cache
3934
3934
    return(error);
3974
3974
  if (table->s->tmp_table)
3975
3975
  {
3976
3976
    TableList tbl;
3977
 
    memset(&tbl, 0, sizeof(tbl));
3978
3977
    tbl.db= new_db;
3979
3978
    tbl.alias= tmp_name;
3980
3979
    tbl.table_name= tmp_name;
3981
3980
 
3982
3981
    /* Table is in session->temporary_tables */
3983
 
    new_table= open_table(session, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
 
3982
    new_table= session->open_table(&tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
3984
3983
  }
3985
3984
  else
3986
3985
  {
4026
4025
      session->lock=0;
4027
4026
    }
4028
4027
    /* Remove link to old table and rename the new one */
4029
 
    close_temporary_table(session, table, true, true);
 
4028
    session->close_temporary_table(table, true, true);
4030
4029
    /* Should pass the 'new_name' as we store table name in the cache */
4031
4030
    if (rename_temporary_table(new_table, new_db, new_name))
4032
4031
      goto err1;
4115
4114
 
4116
4115
  quick_rm_table(old_db_type, db, old_name, true);
4117
4116
 
4118
 
  if (session->locked_tables && new_name == table_name && new_db == db)
4119
 
  {
4120
 
    error= session->reopen_tables(true, true);
4121
 
 
4122
 
    if (error)
4123
 
      goto err_with_placeholders;
4124
 
  }
4125
4117
  pthread_mutex_unlock(&LOCK_open);
4126
4118
 
4127
4119
  session->set_proc_info("end");
4152
4144
  }
4153
4145
  table_list->table=0;                          // For query cache
4154
4146
 
4155
 
  if (session->locked_tables && (new_name != table_name || new_db != db))
4156
 
  {
4157
 
    /*
4158
 
      If are we under LOCK TABLES and did ALTER Table with RENAME we need
4159
 
      to remove placeholders for the old table and for the target table
4160
 
      from the list of open tables and table cache. If we are not under
4161
 
      LOCK TABLES we can rely on close_thread_tables() doing this job.
4162
 
    */
4163
 
    pthread_mutex_lock(&LOCK_open); /* LOCK for ALTER TABLE, but this locks for LOCK TABLES */
4164
 
    unlink_open_table(session, table, false);
4165
 
    unlink_open_table(session, name_lock, false);
4166
 
    pthread_mutex_unlock(&LOCK_open);
4167
 
  }
4168
 
 
4169
4147
end_temporary:
4170
4148
  /*
4171
4149
   * Field::store() may have called my_error().  If this is 
4191
4169
  if (new_table)
4192
4170
  {
4193
4171
    /* close_temporary_table() frees the new_table pointer. */
4194
 
    close_temporary_table(session, new_table, true, true);
 
4172
    session->close_temporary_table(new_table, true, true);
4195
4173
  }
4196
4174
  else
4197
4175
    quick_rm_table(new_db_type, new_db, tmp_name, true);
4231
4209
  if (name_lock)
4232
4210
  {
4233
4211
    pthread_mutex_lock(&LOCK_open); /* ALTER TABLe */
4234
 
    unlink_open_table(session, name_lock, false);
 
4212
    session->unlink_open_table(name_lock);
4235
4213
    pthread_mutex_unlock(&LOCK_open);
4236
4214
  }
4237
 
  return(true);
 
4215
  return true;
4238
4216
 
4239
4217
err_with_placeholders:
4240
4218
  /*
4242
4220
    being altered. To be safe under LOCK TABLES we should remove placeholders
4243
4221
    from list of open tables list and table cache.
4244
4222
  */
4245
 
  unlink_open_table(session, table, false);
 
4223
  session->unlink_open_table(table);
4246
4224
  if (name_lock)
4247
 
    unlink_open_table(session, name_lock, false);
 
4225
    session->unlink_open_table(name_lock);
4248
4226
  pthread_mutex_unlock(&LOCK_open);
4249
4227
  return(true);
4250
4228
}
4519
4497
 
4520
4498
    sprintf(table_name,"%s.%s",table->db,table->table_name);
4521
4499
 
4522
 
    t= table->table= open_ltable(session, table, TL_READ);
 
4500
    t= table->table= session->open_ltable(table, TL_READ);
4523
4501
    session->clear_error();                     // these errors shouldn't get client
4524
4502
 
4525
4503
    protocol->prepareForResend();