~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#include <drizzled/server_includes.h>
19
19
#include <storage/myisam/myisam.h>
20
20
#include <drizzled/sql_show.h>
21
 
#include <drizzled/error.h>
22
 
#include <drizzled/gettext.h>
23
 
#include <drizzled/data_home.h>
24
 
#include <drizzled/sql_parse.h>
 
21
#include <drizzled/drizzled_error_messages.h>
 
22
#include <libdrizzle/gettext.h>
25
23
 
26
24
int creating_table= 0;        // How many mysql_create_table are running
27
25
 
28
 
const char * primary_key_name="PRIMARY";
 
26
const char *primary_key_name="PRIMARY";
29
27
 
30
28
static bool check_if_keyname_exists(const char *name,KEY *start, KEY *end);
31
29
static char *make_unique_key_name(const char *field_name,KEY *start,KEY *end);
32
30
static int copy_data_between_tables(Table *from,Table *to,
33
31
                                    List<Create_field> &create, bool ignore,
34
 
                                    uint32_t order_num, order_st *order,
 
32
                                    uint order_num, order_st *order,
35
33
                                    ha_rows *copied,ha_rows *deleted,
36
34
                                    enum enum_enable_or_disable keys_onoff,
37
35
                                    bool error_if_not_empty);
38
36
 
39
 
static bool prepare_blob_field(Session *session, Create_field *sql_field);
40
 
static bool check_engine(Session *, const char *, HA_CREATE_INFO *);
 
37
static bool prepare_blob_field(THD *thd, Create_field *sql_field);
 
38
static bool check_engine(THD *, const char *, HA_CREATE_INFO *);
41
39
static int
42
 
mysql_prepare_create_table(Session *session, HA_CREATE_INFO *create_info,
 
40
mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
43
41
                           Alter_info *alter_info,
44
42
                           bool tmp_table,
45
 
                               uint32_t *db_options,
 
43
                               uint *db_options,
46
44
                               handler *file, KEY **key_info_buffer,
47
 
                               uint32_t *key_count, int select_field_count);
 
45
                               uint *key_count, int select_field_count);
48
46
static bool
49
 
mysql_prepare_alter_table(Session *session, Table *table,
 
47
mysql_prepare_alter_table(THD *thd, Table *table,
50
48
                          HA_CREATE_INFO *create_info,
51
49
                          Alter_info *alter_info);
52
50
 
62
60
  RETURN
63
61
    Table name length.
64
62
*/
65
 
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length)
 
63
uint filename_to_tablename(const char *from, char *to, uint to_length)
66
64
{
67
 
  uint32_t errors;
68
 
  uint32_t res;
 
65
  uint errors;
 
66
  uint res;
69
67
 
70
68
  if (!memcmp(from, tmp_file_prefix, tmp_file_prefix_length))
71
69
  {
72
70
    /* Temporary table name. */
73
 
    res= (my_stpncpy(to, from, to_length) - to);
 
71
    res= (stpncpy(to, from, to_length) - to);
74
72
  }
75
73
  else
76
74
  {
78
76
                    system_charset_info,  to, to_length, &errors);
79
77
    if (errors) // Old 5.0 name
80
78
    {
81
 
      strcpy(to, MYSQL50_TABLE_NAME_PREFIX);
82
 
      strncat(to, from, to_length-MYSQL50_TABLE_NAME_PREFIX_LENGTH-1);
83
 
      res= strlen(to);
 
79
      res= (strxnmov(to, to_length, MYSQL50_TABLE_NAME_PREFIX,  from, NullS) -
 
80
            to);
84
81
      sql_print_error(_("Invalid (old?) table or database name '%s'"), from);
85
82
    }
86
83
  }
101
98
  RETURN
102
99
    File name length.
103
100
*/
104
 
uint32_t tablename_to_filename(const char *from, char *to, uint32_t to_length)
 
101
 
 
102
uint tablename_to_filename(const char *from, char *to, uint to_length)
105
103
{
106
 
  uint32_t errors, length;
 
104
  uint errors, length;
107
105
 
108
106
  if (from[0] == '#' && !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
109
107
                                 MYSQL50_TABLE_NAME_PREFIX_LENGTH))
155
153
    path length
156
154
*/
157
155
 
158
 
uint32_t build_table_filename(char *buff, size_t bufflen, const char *db,
159
 
                          const char *table_name, const char *ext, uint32_t flags)
 
156
uint build_table_filename(char *buff, size_t bufflen, const char *db,
 
157
                          const char *table_name, const char *ext, uint flags)
160
158
{
161
159
  char dbbuff[FN_REFLEN];
162
160
  char tbbuff[FN_REFLEN];
163
161
 
164
162
  if (flags & FN_IS_TMP) // FN_FROM_IS_TMP | FN_TO_IS_TMP
165
 
    my_stpncpy(tbbuff, table_name, sizeof(tbbuff));
 
163
    stpncpy(tbbuff, table_name, sizeof(tbbuff));
166
164
  else
167
 
    tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
 
165
    VOID(tablename_to_filename(table_name, tbbuff, sizeof(tbbuff)));
168
166
 
169
 
  tablename_to_filename(db, dbbuff, sizeof(dbbuff));
 
167
  VOID(tablename_to_filename(db, dbbuff, sizeof(dbbuff)));
170
168
 
171
169
  char *end = buff + bufflen;
172
170
  /* Don't add FN_ROOTDIR if mysql_data_home already includes it */
173
 
  char *pos = my_stpncpy(buff, mysql_data_home, bufflen);
 
171
  char *pos = stpncpy(buff, mysql_data_home, bufflen);
174
172
  int rootdir_len= strlen(FN_ROOTDIR);
175
173
  if (pos - rootdir_len >= buff &&
176
174
      memcmp(pos - rootdir_len, FN_ROOTDIR, rootdir_len) != 0)
177
 
    pos= my_stpncpy(pos, FN_ROOTDIR, end - pos);
178
 
  pos= my_stpncpy(pos, dbbuff, end-pos);
179
 
  pos= my_stpncpy(pos, FN_ROOTDIR, end-pos);
 
175
    pos= stpncpy(pos, FN_ROOTDIR, end - pos);
 
176
  pos= strxnmov(pos, end - pos, dbbuff, FN_ROOTDIR, NullS);
180
177
#ifdef USE_SYMDIR
181
178
  unpack_dirname(buff, buff);
182
179
  pos= strend(buff);
183
180
#endif
184
 
  pos= my_stpncpy(pos, tbbuff, end - pos);
185
 
  pos= my_stpncpy(pos, ext, end - pos);
 
181
  pos= strxnmov(pos, end - pos, tbbuff, ext, NullS);
186
182
 
187
183
  return(pos - buff);
188
184
}
193
189
 
194
190
  SYNOPSIS
195
191
   build_tmptable_filename()
196
 
     session                        The thread handle.
 
192
     thd                        The thread handle.
197
193
     buff                       Where to write result in my_charset_filename.
198
194
     bufflen                    buff size
199
195
 
206
202
    path length
207
203
*/
208
204
 
209
 
uint32_t build_tmptable_filename(Session* session, char *buff, size_t bufflen)
 
205
uint build_tmptable_filename(THD* thd, char *buff, size_t bufflen)
210
206
{
211
207
 
212
 
  char *p= my_stpncpy(buff, mysql_tmpdir, bufflen);
213
 
  snprintf(p, bufflen - (p - buff), "/%s%lx_%"PRIx64"_%x%s",
 
208
  char *p= stpncpy(buff, mysql_tmpdir, bufflen);
 
209
  snprintf(p, bufflen - (p - buff), "/%s%lx_%lx_%x%s",
214
210
              tmp_file_prefix, current_pid,
215
 
              session->thread_id, session->tmp_table++, reg_ext);
 
211
              thd->thread_id, thd->tmp_table++, reg_ext);
216
212
 
217
213
  if (lower_case_table_names)
218
214
  {
220
216
    my_casedn_str(files_charset_info, p);
221
217
  }
222
218
 
223
 
  uint32_t length= unpack_filename(buff, buff);
 
219
  uint length= unpack_filename(buff, buff);
224
220
  return(length);
225
221
}
226
222
 
227
223
/*
228
224
  SYNOPSIS
229
225
    write_bin_log()
230
 
    session                           Thread object
 
226
    thd                           Thread object
231
227
    clear_error                   is clear_error to be called
232
228
    query                         Query to log
233
229
    query_length                  Length of query
240
236
    file
241
237
*/
242
238
 
243
 
void write_bin_log(Session *session, bool clear_error,
 
239
void write_bin_log(THD *thd, bool clear_error,
244
240
                   char const *query, ulong query_length)
245
241
{
246
242
  if (mysql_bin_log.is_open())
247
243
  {
248
244
    if (clear_error)
249
 
      session->clear_error();
250
 
    session->binlog_query(Session::STMT_QUERY_TYPE,
 
245
      thd->clear_error();
 
246
    thd->binlog_query(THD::STMT_QUERY_TYPE,
251
247
                      query, query_length, false, false);
252
248
  }
253
249
}
258
254
 
259
255
  SYNOPSIS
260
256
   mysql_rm_table()
261
 
   session                      Thread handle
 
257
   thd                  Thread handle
262
258
   tables               List of tables to delete
263
259
   if_exists            If 1, don't give error if one table doesn't exists
264
260
 
277
273
 
278
274
*/
279
275
 
280
 
bool mysql_rm_table(Session *session,TableList *tables, bool if_exists, bool drop_temporary)
 
276
bool mysql_rm_table(THD *thd,TableList *tables, bool if_exists, bool drop_temporary)
281
277
{
282
278
  bool error, need_start_waiting= false;
283
279
 
291
287
 
292
288
  if (!drop_temporary)
293
289
  {
294
 
    if (!session->locked_tables &&
295
 
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
290
    if (!thd->locked_tables &&
 
291
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
296
292
      return(true);
297
293
  }
298
294
 
301
297
    LOCK_open during wait_if_global_read_lock(), other threads could not
302
298
    close their tables. This would make a pretty deadlock.
303
299
  */
304
 
  error= mysql_rm_table_part2(session, tables, if_exists, drop_temporary, 0, 0);
 
300
  error= mysql_rm_table_part2(thd, tables, if_exists, drop_temporary, 0, 0);
305
301
 
306
302
  if (need_start_waiting)
307
 
    start_waiting_global_read_lock(session);
 
303
    start_waiting_global_read_lock(thd);
308
304
 
309
305
  if (error)
310
306
    return(true);
311
 
  my_ok(session);
 
307
  my_ok(thd);
312
308
  return(false);
313
309
}
314
310
 
317
313
 
318
314
  SYNOPSIS
319
315
    mysql_rm_table_part2()
320
 
    session                     Thread handler
 
316
    thd                 Thread handler
321
317
    tables              Tables to drop
322
318
    if_exists           If set, don't give an error if table doesn't exists.
323
319
                        In this case we give an warning of level 'NOTE'
342
338
   -1   Thread was killed
343
339
*/
344
340
 
345
 
int mysql_rm_table_part2(Session *session, TableList *tables, bool if_exists,
 
341
int mysql_rm_table_part2(THD *thd, TableList *tables, bool if_exists,
346
342
                         bool drop_temporary, bool drop_view,
347
343
                         bool dont_log_query)
348
344
{
349
345
  TableList *table;
350
346
  char path[FN_REFLEN], *alias;
351
 
  uint32_t path_length;
 
347
  uint path_length;
352
348
  String wrong_tables;
353
349
  int error= 0;
354
350
  int non_temp_tables_count= 0;
355
351
  bool some_tables_deleted=0, tmp_table_deleted=0, foreign_key_error=0;
356
352
  String built_query;
357
353
 
358
 
  if (session->current_stmt_binlog_row_based && !dont_log_query)
 
354
  if (thd->current_stmt_binlog_row_based && !dont_log_query)
359
355
  {
360
356
    built_query.set_charset(system_charset_info);
361
357
    if (if_exists)
364
360
      built_query.append("DROP Table ");
365
361
  }
366
362
 
367
 
  mysql_ha_rm_tables(session, tables, false);
 
363
  mysql_ha_rm_tables(thd, tables, false);
368
364
 
369
365
  pthread_mutex_lock(&LOCK_open);
370
366
 
382
378
      table->db_type= share->db_type();
383
379
  }
384
380
 
385
 
  if (!drop_temporary && lock_table_names_exclusively(session, tables))
 
381
  if (!drop_temporary && lock_table_names_exclusively(thd, tables))
386
382
  {
387
383
    pthread_mutex_unlock(&LOCK_open);
388
384
    return(1);
389
385
  }
390
386
 
391
387
  /* Don't give warnings for not found errors, as we already generate notes */
392
 
  session->no_warnings_for_error= 1;
 
388
  thd->no_warnings_for_error= 1;
393
389
 
394
390
  for (table= tables; table; table= table->next_local)
395
391
  {
398
394
    enum legacy_db_type frm_db_type;
399
395
 
400
396
 
401
 
    error= drop_temporary_table(session, table);
 
397
    error= drop_temporary_table(thd, table);
402
398
 
403
399
    switch (error) {
404
400
    case  0:
406
402
      tmp_table_deleted= 1;
407
403
      continue;
408
404
    case -1:
 
405
      assert(thd->in_sub_stmt);
409
406
      error= 1;
410
407
      goto err_with_placeholders;
411
408
    default:
419
416
      being built.  The string always end in a comma and the comma
420
417
      will be chopped off before being written to the binary log.
421
418
      */
422
 
    if (session->current_stmt_binlog_row_based && !dont_log_query)
 
419
    if (thd->current_stmt_binlog_row_based && !dont_log_query)
423
420
    {
424
421
      non_temp_tables_count++;
425
422
      /*
426
423
        Don't write the database name if it is the current one (or if
427
 
        session->db is NULL).
 
424
        thd->db is NULL).
428
425
      */
429
426
      built_query.append("`");
430
 
      if (session->db == NULL || strcmp(db,session->db) != 0)
 
427
      if (thd->db == NULL || strcmp(db,thd->db) != 0)
431
428
      {
432
429
        built_query.append(db);
433
430
        built_query.append("`.`");
441
438
    if (!drop_temporary)
442
439
    {
443
440
      Table *locked_table;
444
 
      abort_locked_tables(session, db, table->table_name);
445
 
      remove_table_from_cache(session, db, table->table_name,
 
441
      abort_locked_tables(thd, db, table->table_name);
 
442
      remove_table_from_cache(thd, db, table->table_name,
446
443
                              RTFC_WAIT_OTHER_THREAD_FLAG |
447
444
                              RTFC_CHECK_KILLED_FLAG);
448
445
      /*
449
446
        If the table was used in lock tables, remember it so that
450
447
        unlock_table_names can free it
451
448
      */
452
 
      if ((locked_table= drop_locked_tables(session, db, table->table_name)))
 
449
      if ((locked_table= drop_locked_tables(thd, db, table->table_name)))
453
450
        table->table= locked_table;
454
451
 
455
 
      if (session->killed)
 
452
      if (thd->killed)
456
453
      {
457
454
        error= -1;
458
455
        goto err_with_placeholders;
464
461
                                        FN_IS_TMP : 0);
465
462
    }
466
463
    if (drop_temporary ||
467
 
        ((table_type == NULL && (access(path, F_OK) && ha_create_table_from_engine(session, db, alias))) ||
468
 
         (!drop_view && mysql_frm_type(session, path, &frm_db_type) != true)))
 
464
        ((table_type == NULL && (access(path, F_OK) && ha_create_table_from_engine(thd, db, alias))) ||
 
465
         (!drop_view && mysql_frm_type(thd, path, &frm_db_type) != true)))
469
466
    {
470
467
      // Table was not found on disk and table can't be created from engine
471
468
      if (if_exists)
472
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
469
        push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
473
470
                            ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
474
471
                            table->table_name);
475
472
      else
480
477
      char *end;
481
478
      if (table_type == NULL)
482
479
      {
483
 
        mysql_frm_type(session, path, &frm_db_type);
484
 
        table_type= ha_resolve_by_legacy_type(session, frm_db_type);
 
480
        mysql_frm_type(thd, path, &frm_db_type);
 
481
        table_type= ha_resolve_by_legacy_type(thd, frm_db_type);
485
482
      }
486
483
      // Remove extension for delete
487
484
      *(end= path + path_length - reg_ext_length)= '\0';
488
 
      error= ha_delete_table(session, table_type, path, db, table->table_name,
 
485
      error= ha_delete_table(thd, table_type, path, db, table->table_name,
489
486
                             !dont_log_query);
490
487
      if ((error == ENOENT || error == HA_ERR_NO_SUCH_TABLE) && 
491
488
          (if_exists || table_type == NULL))
492
489
      {
493
490
        error= 0;
494
 
        session->clear_error();
 
491
        thd->clear_error();
495
492
      }
496
493
      if (error == HA_ERR_ROW_IS_REFERENCED)
497
494
      {
502
499
      {
503
500
        int new_error;
504
501
        /* Delete the table definition file */
505
 
        my_stpcpy(end,reg_ext);
 
502
        stpcpy(end,reg_ext);
506
503
        if (!(new_error=my_delete(path,MYF(MY_WME))))
507
504
        {
508
505
          some_tables_deleted=1;
523
520
    on the table name.
524
521
  */
525
522
  pthread_mutex_unlock(&LOCK_open);
526
 
  session->thread_specific_used|= tmp_table_deleted;
 
523
  thd->thread_specific_used|= tmp_table_deleted;
527
524
  error= 0;
528
525
  if (wrong_tables.length())
529
526
  {
541
538
  {
542
539
    if (!dont_log_query)
543
540
    {
544
 
      if (!session->current_stmt_binlog_row_based ||
 
541
      if (!thd->current_stmt_binlog_row_based ||
545
542
          (non_temp_tables_count > 0 && !tmp_table_deleted))
546
543
      {
547
544
        /*
551
548
          tables).  In this case, we can write the original query into
552
549
          the binary log.
553
550
         */
554
 
        write_bin_log(session, !error, session->query, session->query_length);
 
551
        write_bin_log(thd, !error, thd->query, thd->query_length);
555
552
      }
556
 
      else if (session->current_stmt_binlog_row_based &&
 
553
      else if (thd->current_stmt_binlog_row_based &&
557
554
               non_temp_tables_count > 0 &&
558
555
               tmp_table_deleted)
559
556
      {
571
568
        */
572
569
        built_query.chop();                  // Chop of the last comma
573
570
        built_query.append(" /* generated by server */");
574
 
        write_bin_log(session, !error, built_query.ptr(), built_query.length());
 
571
        write_bin_log(thd, !error, built_query.ptr(), built_query.length());
575
572
      }
576
573
      /*
577
574
        The remaining cases are:
585
582
  }
586
583
  pthread_mutex_lock(&LOCK_open);
587
584
err_with_placeholders:
588
 
  unlock_table_names(session, tables, (TableList*) 0);
 
585
  unlock_table_names(thd, tables, (TableList*) 0);
589
586
  pthread_mutex_unlock(&LOCK_open);
590
 
  session->no_warnings_for_error= 0;
 
587
  thd->no_warnings_for_error= 0;
591
588
  return(error);
592
589
}
593
590
 
608
605
*/
609
606
 
610
607
bool quick_rm_table(handlerton *base,const char *db,
611
 
                    const char *table_name, uint32_t flags)
 
608
                    const char *table_name, uint flags)
612
609
{
613
610
  char path[FN_REFLEN];
614
611
  bool error= 0;
615
612
 
616
 
  uint32_t path_length= build_table_filename(path, sizeof(path),
 
613
  uint path_length= build_table_filename(path, sizeof(path),
617
614
                                         db, table_name, reg_ext, flags);
618
615
  if (my_delete(path,MYF(0)))
619
616
    error= 1; /* purecov: inspected */
620
617
  path[path_length - reg_ext_length]= '\0'; // Remove reg_ext
621
 
  return(ha_delete_table(current_session, base, path, db, table_name, 0) ||
 
618
  return(ha_delete_table(current_thd, base, path, db, table_name, 0) ||
622
619
              error);
623
620
}
624
621
 
734
731
                                uint32_t *max_length, uint32_t *tot_length)
735
732
{
736
733
  const char **pos;
737
 
  uint32_t *len;
 
734
  uint *len;
738
735
  *max_length= *tot_length= 0;
739
736
  for (pos= interval->type_names, len= interval->type_lengths;
740
737
       *pos ; pos++, len++)
741
738
  {
742
 
    uint32_t length= cs->cset->numchars(cs, *pos, *pos + *len);
 
739
    uint length= cs->cset->numchars(cs, *pos, *pos + *len);
743
740
    *tot_length+= length;
744
741
    set_if_bigger(*max_length, (uint32_t)length);
745
742
  }
766
763
*/
767
764
 
768
765
int prepare_create_field(Create_field *sql_field, 
769
 
                         uint32_t *blob_columns,
 
766
                         uint *blob_columns,
770
767
                         int *timestamps, int *timestamps_with_niladic,
771
768
                         int64_t table_flags __attribute__((unused)))
772
769
{
843
840
                          (sql_field->decimals << FIELDFLAG_DEC_SHIFT));
844
841
    break;
845
842
  }
846
 
  if (!(sql_field->flags & NOT_NULL_FLAG) ||
847
 
      (sql_field->vcol_info)) /* Make virtual columns always allow NULL values */
 
843
  if (!(sql_field->flags & NOT_NULL_FLAG))
848
844
    sql_field->pack_flag|= FIELDFLAG_MAYBE_NULL;
849
845
  if (sql_field->flags & NO_DEFAULT_VALUE_FLAG)
850
846
    sql_field->pack_flag|= FIELDFLAG_NO_DEFAULT;
856
852
 
857
853
  SYNOPSIS
858
854
    mysql_prepare_create_table()
859
 
      session                       Thread object.
 
855
      thd                       Thread object.
860
856
      create_info               Create information (like MAX_ROWS).
861
857
      alter_info                List of columns and indexes to create
862
858
      tmp_table                 If a temporary table is to be created.
878
874
*/
879
875
 
880
876
static int
881
 
mysql_prepare_create_table(Session *session, HA_CREATE_INFO *create_info,
 
877
mysql_prepare_create_table(THD *thd, HA_CREATE_INFO *create_info,
882
878
                           Alter_info *alter_info,
883
879
                           bool tmp_table,
884
 
                               uint32_t *db_options,
 
880
                               uint *db_options,
885
881
                               handler *file, KEY **key_info_buffer,
886
 
                               uint32_t *key_count, int select_field_count)
 
882
                               uint *key_count, int select_field_count)
887
883
{
888
884
  const char    *key_name;
889
885
  Create_field  *sql_field,*dup_field;
896
892
  int           select_field_pos,auto_increment=0;
897
893
  List_iterator<Create_field> it(alter_info->create_list);
898
894
  List_iterator<Create_field> it2(alter_info->create_list);
899
 
  uint32_t total_uneven_bit_length= 0;
 
895
  uint total_uneven_bit_length= 0;
900
896
 
901
897
  select_field_pos= alter_info->create_list.elements - select_field_count;
902
898
  null_fields=blob_columns=0;
982
978
          occupied memory at the same time when we free this
983
979
          sql_field -- at the end of execution.
984
980
        */
985
 
        interval= sql_field->interval= typelib(session->mem_root,
 
981
        interval= sql_field->interval= typelib(thd->mem_root,
986
982
                                               sql_field->interval_list);
987
983
        List_iterator<String> int_it(sql_field->interval_list);
988
984
        String conv, *tmp;
989
985
        char comma_buf[4];
990
 
        int comma_length= cs->cset->wc_mb(cs, ',', (unsigned char*) comma_buf,
991
 
                                          (unsigned char*) comma_buf + 
 
986
        int comma_length= cs->cset->wc_mb(cs, ',', (uchar*) comma_buf,
 
987
                                          (uchar*) comma_buf + 
992
988
                                          sizeof(comma_buf));
993
989
        assert(comma_length > 0);
994
 
        for (uint32_t i= 0; (tmp= int_it++); i++)
 
990
        for (uint i= 0; (tmp= int_it++); i++)
995
991
        {
996
 
          uint32_t lengthsp;
 
992
          uint lengthsp;
997
993
          if (String::needs_conversion(tmp->length(), tmp->charset(),
998
994
                                       cs, &dummy))
999
995
          {
1000
 
            uint32_t cnv_errs;
 
996
            uint cnv_errs;
1001
997
            conv.copy(tmp->ptr(), tmp->length(), tmp->charset(), cs, &cnv_errs);
1002
 
            interval->type_names[i]= strmake_root(session->mem_root, conv.ptr(),
 
998
            interval->type_names[i]= strmake_root(thd->mem_root, conv.ptr(),
1003
999
                                                  conv.length());
1004
1000
            interval->type_lengths[i]= conv.length();
1005
1001
          }
1008
1004
          lengthsp= cs->cset->lengthsp(cs, interval->type_names[i],
1009
1005
                                       interval->type_lengths[i]);
1010
1006
          interval->type_lengths[i]= lengthsp;
1011
 
          ((unsigned char *)interval->type_names[i])[lengthsp]= '\0';
 
1007
          ((uchar *)interval->type_names[i])[lengthsp]= '\0';
1012
1008
        }
1013
1009
        sql_field->interval_list.empty(); // Don't need interval_list anymore
1014
1010
      }
1047
1043
    }
1048
1044
 
1049
1045
    sql_field->create_length_to_internal_length();
1050
 
    if (prepare_blob_field(session, sql_field))
 
1046
    if (prepare_blob_field(thd, sql_field))
1051
1047
      return(true);
1052
1048
 
1053
1049
    if (!(sql_field->flags & NOT_NULL_FLAG))
1098
1094
            null_fields--;
1099
1095
          sql_field->flags=             dup_field->flags;
1100
1096
          sql_field->interval=          dup_field->interval;
1101
 
          sql_field->vcol_info=         dup_field->vcol_info;
1102
 
          sql_field->is_stored=      dup_field->is_stored;
1103
1097
          it2.remove();                 // Remove first (create) definition
1104
1098
          select_field_pos--;
1105
1099
          break;
1131
1125
    sql_field->offset= record_offset;
1132
1126
    if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
1133
1127
      auto_increment++;
1134
 
    /*
1135
 
          For now skip fields that are not physically stored in the database
1136
 
          (virtual fields) and update their offset later 
1137
 
          (see the next loop).
1138
 
        */
1139
 
    if (sql_field->is_stored)
1140
 
      record_offset+= sql_field->pack_length;
1141
 
  }
1142
 
  /* Update virtual fields' offset */
1143
 
  it.rewind();
1144
 
  while ((sql_field=it++))
1145
 
  {
1146
 
    if (not sql_field->is_stored)
1147
 
    {
1148
 
      sql_field->offset= record_offset;
1149
 
      record_offset+= sql_field->pack_length;
1150
 
    }
 
1128
    record_offset+= sql_field->pack_length;
1151
1129
  }
1152
1130
  if (timestamps_with_niladic > 1)
1153
1131
  {
1179
1157
 
1180
1158
  List_iterator<Key> key_iterator(alter_info->key_list);
1181
1159
  List_iterator<Key> key_iterator2(alter_info->key_list);
1182
 
  uint32_t key_parts=0, fk_key_count=0;
 
1160
  uint key_parts=0, fk_key_count=0;
1183
1161
  bool primary_key=0,unique_key=0;
1184
1162
  Key *key, *key2;
1185
 
  uint32_t tmp, key_number;
 
1163
  uint tmp, key_number;
1186
1164
  /* special marker for keys to be ignored */
1187
1165
  static char ignore_key[1];
1188
1166
 
1194
1172
    if (key->type == Key::FOREIGN_KEY)
1195
1173
    {
1196
1174
      fk_key_count++;
1197
 
      if (((Foreign_key *)key)->validate(alter_info->create_list))
1198
 
        return true;
1199
1175
      Foreign_key *fk_key= (Foreign_key*) key;
1200
1176
      if (fk_key->ref_columns.elements &&
1201
1177
          fk_key->ref_columns.elements != fk_key->columns.elements)
1274
1250
  key_number=0;
1275
1251
  for (; (key=key_iterator++) ; key_number++)
1276
1252
  {
1277
 
    uint32_t key_length=0;
 
1253
    uint key_length=0;
1278
1254
    Key_part_spec *column;
1279
1255
 
1280
1256
    if (key->name.str == ignore_key)
1318
1294
    if (key_info->block_size)
1319
1295
      key_info->flags|= HA_USES_BLOCK_SIZE;
1320
1296
 
1321
 
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
 
1297
    uint tmp_len= system_charset_info->cset->charpos(system_charset_info,
1322
1298
                                           key->key_create_info.comment.str,
1323
1299
                                           key->key_create_info.comment.str +
1324
1300
                                           key->key_create_info.comment.length,
1340
1316
    }
1341
1317
 
1342
1318
    List_iterator<Key_part_spec> cols(key->columns), cols2(key->columns);
1343
 
    for (uint32_t column_nr=0 ; (column=cols++) ; column_nr++)
 
1319
    for (uint column_nr=0 ; (column=cols++) ; column_nr++)
1344
1320
    {
1345
 
      uint32_t length;
 
1321
      uint length;
1346
1322
      Key_part_spec *dup_column;
1347
1323
 
1348
1324
      it.rewind();
1385
1361
            return(true);
1386
1362
          }
1387
1363
        }
1388
 
        if (not sql_field->is_stored)
1389
 
        {
1390
 
          /* Key fields must always be physically stored. */
1391
 
          my_error(ER_KEY_BASED_ON_GENERATED_VIRTUAL_COLUMN, MYF(0));
1392
 
          return(true);
1393
 
        }
1394
 
        if (key->type == Key::PRIMARY && sql_field->vcol_info)
1395
 
        {
1396
 
          my_error(ER_PRIMARY_KEY_BASED_ON_VIRTUAL_COLUMN, MYF(0));
1397
 
          return(true);
1398
 
        }
1399
1364
        if (!(sql_field->flags & NOT_NULL_FLAG))
1400
1365
        {
1401
1366
          if (key->type == Key::PRIMARY)
1434
1399
          if ((length=column->length) > max_key_length ||
1435
1400
              length > file->max_key_part_length())
1436
1401
          {
1437
 
            length=cmin(max_key_length, file->max_key_part_length());
 
1402
            length=min(max_key_length, file->max_key_part_length());
1438
1403
            if (key->type == Key::MULTIPLE)
1439
1404
            {
1440
1405
              /* not a critical problem */
1441
1406
              char warn_buff[DRIZZLE_ERRMSG_SIZE];
1442
1407
              snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1443
1408
                       length);
1444
 
              push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1409
              push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1445
1410
                           ER_TOO_LONG_KEY, warn_buff);
1446
1411
              /* Align key length to multibyte char boundary */
1447
1412
              length-= length % sql_field->charset->mbmaxlen;
1480
1445
          char warn_buff[DRIZZLE_ERRMSG_SIZE];
1481
1446
          snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1482
1447
                   length);
1483
 
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1448
          push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1484
1449
                       ER_TOO_LONG_KEY, warn_buff);
1485
1450
          /* Align key length to multibyte char boundary */
1486
1451
          length-= length % sql_field->charset->mbmaxlen;
1563
1528
    return(true);
1564
1529
  }
1565
1530
  /* Sort keys in optimized order */
1566
 
  my_qsort((unsigned char*) *key_info_buffer, *key_count, sizeof(KEY),
 
1531
  my_qsort((uchar*) *key_info_buffer, *key_count, sizeof(KEY),
1567
1532
           (qsort_cmp) sort_keys);
1568
1533
  create_info->null_bits= null_fields;
1569
1534
 
1573
1538
  {
1574
1539
    Field::utype type= (Field::utype) MTYP_TYPENR(sql_field->unireg_check);
1575
1540
 
1576
 
    if (session->variables.sql_mode & MODE_NO_ZERO_DATE &&
 
1541
    if (thd->variables.sql_mode & MODE_NO_ZERO_DATE &&
1577
1542
        !sql_field->def &&
1578
1543
        sql_field->sql_type == DRIZZLE_TYPE_TIMESTAMP &&
1579
1544
        (sql_field->flags & NOT_NULL_FLAG) &&
1615
1580
    apply it to the table.
1616
1581
*/
1617
1582
 
1618
 
static void set_table_default_charset(Session *session,
 
1583
static void set_table_default_charset(THD *thd,
1619
1584
                                      HA_CREATE_INFO *create_info, char *db)
1620
1585
{
1621
1586
  /*
1627
1592
  {
1628
1593
    HA_CREATE_INFO db_info;
1629
1594
 
1630
 
    load_db_opt_by_name(session, db, &db_info);
 
1595
    load_db_opt_by_name(thd, db, &db_info);
1631
1596
 
1632
1597
    create_info->default_table_charset= db_info.default_table_charset;
1633
1598
  }
1647
1612
        In this case the error is given
1648
1613
*/
1649
1614
 
1650
 
static bool prepare_blob_field(Session *session __attribute__((unused)),
 
1615
static bool prepare_blob_field(THD *thd __attribute__((unused)),
1651
1616
                               Create_field *sql_field)
1652
1617
{
1653
1618
 
1674
1639
 
1675
1640
 
1676
1641
/*
 
1642
  Preparation of Create_field for SP function return values.
 
1643
  Based on code used in the inner loop of mysql_prepare_create_table()
 
1644
  above.
 
1645
 
 
1646
  SYNOPSIS
 
1647
    sp_prepare_create_field()
 
1648
    thd                 Thread object
 
1649
    sql_field           Field to prepare
 
1650
 
 
1651
  DESCRIPTION
 
1652
    Prepares the field structures for field creation.
 
1653
 
 
1654
*/
 
1655
 
 
1656
void sp_prepare_create_field(THD *thd, Create_field *sql_field)
 
1657
{
 
1658
  if (sql_field->sql_type == DRIZZLE_TYPE_ENUM)
 
1659
  {
 
1660
    uint32_t field_length, dummy;
 
1661
    /* DRIZZLE_TYPE_ENUM */
 
1662
    {
 
1663
      calculate_interval_lengths(sql_field->charset,
 
1664
                                 sql_field->interval,
 
1665
                                 &field_length, &dummy);
 
1666
      sql_field->length= field_length;
 
1667
    }
 
1668
    set_if_smaller(sql_field->length, MAX_FIELD_WIDTH-1);
 
1669
  }
 
1670
 
 
1671
  sql_field->create_length_to_internal_length();
 
1672
  assert(sql_field->def == 0);
 
1673
  /* Can't go wrong as sql_field->def is not defined */
 
1674
  (void) prepare_blob_field(thd, sql_field);
 
1675
}
 
1676
 
 
1677
 
 
1678
/*
1677
1679
  Create a table
1678
1680
 
1679
1681
  SYNOPSIS
1680
1682
    mysql_create_table_no_lock()
1681
 
    session                     Thread object
 
1683
    thd                 Thread object
1682
1684
    db                  Database
1683
1685
    table_name          Table name
1684
1686
    create_info         Create information (like MAX_ROWS)
1706
1708
    true  error
1707
1709
*/
1708
1710
 
1709
 
bool mysql_create_table_no_lock(Session *session,
 
1711
bool mysql_create_table_no_lock(THD *thd,
1710
1712
                                const char *db, const char *table_name,
1711
1713
                                HA_CREATE_INFO *create_info,
1712
1714
                                Alter_info *alter_info,
1713
1715
                                bool internal_tmp_table,
1714
 
                                uint32_t select_field_count,
1715
 
                                bool lock_open_lock)
 
1716
                                uint select_field_count)
1716
1717
{
1717
1718
  char          path[FN_REFLEN];
1718
 
  uint32_t          path_length;
 
1719
  uint          path_length;
1719
1720
  const char    *alias;
1720
1721
  uint          db_options, key_count;
1721
1722
  KEY           *key_info_buffer;
1728
1729
               MYF(0));
1729
1730
    return(true);
1730
1731
  }
1731
 
  if (check_engine(session, table_name, create_info))
 
1732
  if (check_engine(thd, table_name, create_info))
1732
1733
    return(true);
1733
1734
  db_options= create_info->table_options;
1734
1735
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
1735
1736
    db_options|=HA_OPTION_PACK_RECORD;
1736
1737
  alias= table_case_name(create_info, table_name);
1737
 
  if (!(file= get_new_handler((TABLE_SHARE*) 0, session->mem_root,
 
1738
  if (!(file= get_new_handler((TABLE_SHARE*) 0, thd->mem_root,
1738
1739
                              create_info->db_type)))
1739
1740
  {
1740
1741
    my_error(ER_OUTOFMEMORY, MYF(0), sizeof(handler));
1741
1742
    return(true);
1742
1743
  }
1743
1744
 
1744
 
  set_table_default_charset(session, create_info, (char*) db);
 
1745
  set_table_default_charset(thd, create_info, (char*) db);
1745
1746
 
1746
 
  if (mysql_prepare_create_table(session, create_info, alter_info,
 
1747
  if (mysql_prepare_create_table(thd, create_info, alter_info,
1747
1748
                                 internal_tmp_table,
1748
1749
                                 &db_options, file,
1749
1750
                          &key_info_buffer, &key_count,
1753
1754
      /* Check if table exists */
1754
1755
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1755
1756
  {
1756
 
    path_length= build_tmptable_filename(session, path, sizeof(path));
 
1757
    path_length= build_tmptable_filename(thd, path, sizeof(path));
1757
1758
    create_info->table_options|=HA_CREATE_DELAY_KEY_WRITE;
1758
1759
  }
1759
1760
  else  
1772
1773
 
1773
1774
  /* Check if table already exists */
1774
1775
  if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
1775
 
      find_temporary_table(session, db, table_name))
 
1776
      find_temporary_table(thd, db, table_name))
1776
1777
  {
1777
1778
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1778
1779
    {
1779
1780
      create_info->table_existed= 1;            // Mark that table existed
1780
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1781
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1781
1782
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1782
1783
                          alias);
1783
1784
      error= 0;
1787
1788
    goto err;
1788
1789
  }
1789
1790
 
1790
 
  if (lock_open_lock)
1791
 
    pthread_mutex_lock(&LOCK_open);
 
1791
  VOID(pthread_mutex_lock(&LOCK_open));
1792
1792
  if (!internal_tmp_table && !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1793
1793
  {
1794
1794
    if (!access(path,F_OK))
1826
1826
  {
1827
1827
    bool create_if_not_exists =
1828
1828
      create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS;
1829
 
    int retcode = ha_table_exists_in_engine(session, db, table_name);
 
1829
    int retcode = ha_table_exists_in_engine(thd, db, table_name);
1830
1830
    switch (retcode)
1831
1831
    {
1832
1832
      case HA_ERR_NO_SUCH_TABLE:
1845
1845
    }
1846
1846
  }
1847
1847
 
1848
 
  session->set_proc_info("creating table");
 
1848
  thd_proc_info(thd, "creating table");
1849
1849
  create_info->table_existed= 0;                // Mark that table is created
1850
1850
 
1851
1851
#ifdef HAVE_READLINK
1864
1864
#endif /* HAVE_READLINK */
1865
1865
  {
1866
1866
    if (create_info->data_file_name)
1867
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1867
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1868
1868
                   "DATA DIRECTORY option ignored");
1869
1869
    if (create_info->index_file_name)
1870
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1870
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1871
1871
                   "INDEX DIRECTORY option ignored");
1872
1872
    create_info->data_file_name= create_info->index_file_name= 0;
1873
1873
  }
1874
1874
  create_info->table_options=db_options;
1875
1875
 
1876
1876
  path[path_length - reg_ext_length]= '\0'; // Remove .frm extension
1877
 
  if (rea_create_table(session, path, db, table_name,
 
1877
  if (rea_create_table(thd, path, db, table_name,
1878
1878
                       create_info, alter_info->create_list,
1879
1879
                       key_count, key_info_buffer, file))
1880
1880
    goto unlock_and_end;
1882
1882
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
1883
1883
  {
1884
1884
    /* Open table and put in temporary table list */
1885
 
    if (!(open_temporary_table(session, path, db, table_name, 1, OTM_OPEN)))
 
1885
    if (!(open_temporary_table(thd, path, db, table_name, 1, OTM_OPEN)))
1886
1886
    {
1887
1887
      (void) rm_temporary_table(create_info->db_type, path, false);
1888
1888
      goto unlock_and_end;
1889
1889
    }
1890
 
    session->thread_specific_used= true;
 
1890
    thd->thread_specific_used= true;
1891
1891
  }
1892
1892
 
1893
1893
  /*
1898
1898
    Otherwise, the statement shall be binlogged.
1899
1899
   */
1900
1900
  if (!internal_tmp_table &&
1901
 
      (!session->current_stmt_binlog_row_based ||
1902
 
       (session->current_stmt_binlog_row_based &&
 
1901
      (!thd->current_stmt_binlog_row_based ||
 
1902
       (thd->current_stmt_binlog_row_based &&
1903
1903
        !(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
1904
 
    write_bin_log(session, true, session->query, session->query_length);
 
1904
    write_bin_log(thd, true, thd->query, thd->query_length);
1905
1905
  error= false;
1906
1906
unlock_and_end:
1907
 
  if (lock_open_lock)
1908
 
    pthread_mutex_unlock(&LOCK_open);
 
1907
  VOID(pthread_mutex_unlock(&LOCK_open));
1909
1908
 
1910
1909
err:
1911
 
  session->set_proc_info("After create");
 
1910
  thd_proc_info(thd, "After create");
1912
1911
  delete file;
1913
1912
  return(error);
1914
1913
 
1915
1914
warn:
1916
1915
  error= false;
1917
 
  push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1916
  push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1918
1917
                      ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1919
1918
                      alias);
1920
1919
  create_info->table_existed= 1;                // Mark that table existed
1926
1925
  Database locking aware wrapper for mysql_create_table_no_lock(),
1927
1926
*/
1928
1927
 
1929
 
bool mysql_create_table(Session *session, const char *db, const char *table_name,
 
1928
bool mysql_create_table(THD *thd, const char *db, const char *table_name,
1930
1929
                        HA_CREATE_INFO *create_info,
1931
1930
                        Alter_info *alter_info,
1932
1931
                        bool internal_tmp_table,
1933
 
                        uint32_t select_field_count)
 
1932
                        uint select_field_count)
1934
1933
{
1935
1934
  Table *name_lock= 0;
1936
1935
  bool result;
1937
1936
 
1938
1937
  /* Wait for any database locks */
1939
1938
  pthread_mutex_lock(&LOCK_lock_db);
1940
 
  while (!session->killed &&
1941
 
         hash_search(&lock_db_cache,(unsigned char*) db, strlen(db)))
 
1939
  while (!thd->killed &&
 
1940
         hash_search(&lock_db_cache,(uchar*) db, strlen(db)))
1942
1941
  {
1943
 
    wait_for_condition(session, &LOCK_lock_db, &COND_refresh);
 
1942
    wait_for_condition(thd, &LOCK_lock_db, &COND_refresh);
1944
1943
    pthread_mutex_lock(&LOCK_lock_db);
1945
1944
  }
1946
1945
 
1947
 
  if (session->killed)
 
1946
  if (thd->killed)
1948
1947
  {
1949
1948
    pthread_mutex_unlock(&LOCK_lock_db);
1950
1949
    return(true);
1954
1953
 
1955
1954
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1956
1955
  {
1957
 
    if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
 
1956
    if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock))
1958
1957
    {
1959
1958
      result= true;
1960
1959
      goto unlock;
1963
1962
    {
1964
1963
      if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
1965
1964
      {
1966
 
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1965
        push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1967
1966
                            ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1968
1967
                            table_name);
1969
1968
        create_info->table_existed= 1;
1978
1977
    }
1979
1978
  }
1980
1979
 
1981
 
  result= mysql_create_table_no_lock(session, db, table_name, create_info,
 
1980
  result= mysql_create_table_no_lock(thd, db, table_name, create_info,
1982
1981
                                     alter_info,
1983
1982
                                     internal_tmp_table,
1984
 
                                     select_field_count, true);
 
1983
                                     select_field_count);
1985
1984
 
1986
1985
unlock:
1987
1986
  if (name_lock)
1988
1987
  {
1989
1988
    pthread_mutex_lock(&LOCK_open);
1990
 
    unlink_open_table(session, name_lock, false);
 
1989
    unlink_open_table(thd, name_lock, false);
1991
1990
    pthread_mutex_unlock(&LOCK_open);
1992
1991
  }
1993
1992
  pthread_mutex_lock(&LOCK_lock_db);
2026
2025
    Only 3 chars + '\0' left, so need to limit to 2 digit
2027
2026
    This is ok as we can't have more than 100 keys anyway
2028
2027
  */
2029
 
  for (uint32_t i=2 ; i< 100; i++)
 
2028
  for (uint i=2 ; i< 100; i++)
2030
2029
  {
2031
2030
    *buff_end= '_';
2032
2031
    int10_to_str(i, buff_end+1, 10);
2066
2065
bool
2067
2066
mysql_rename_table(handlerton *base, const char *old_db,
2068
2067
                   const char *old_name, const char *new_db,
2069
 
                   const char *new_name, uint32_t flags)
 
2068
                   const char *new_name, uint flags)
2070
2069
{
2071
 
  Session *session= current_session;
 
2070
  THD *thd= current_thd;
2072
2071
  char from[FN_REFLEN], to[FN_REFLEN], lc_from[FN_REFLEN], lc_to[FN_REFLEN];
2073
2072
  char *from_base= from, *to_base= to;
2074
2073
  char tmp_name[NAME_LEN+1];
2076
2075
  int error=0;
2077
2076
 
2078
2077
  file= (base == NULL ? 0 :
2079
 
         get_new_handler((TABLE_SHARE*) 0, session->mem_root, base));
 
2078
         get_new_handler((TABLE_SHARE*) 0, thd->mem_root, base));
2080
2079
 
2081
2080
  build_table_filename(from, sizeof(from), old_db, old_name, "",
2082
2081
                       flags & FN_FROM_IS_TMP);
2091
2090
  if (lower_case_table_names == 2 && file &&
2092
2091
      !(file->ha_table_flags() & HA_FILE_BASED))
2093
2092
  {
2094
 
    my_stpcpy(tmp_name, old_name);
 
2093
    stpcpy(tmp_name, old_name);
2095
2094
    my_casedn_str(files_charset_info, tmp_name);
2096
2095
    build_table_filename(lc_from, sizeof(lc_from), old_db, tmp_name, "",
2097
2096
                         flags & FN_FROM_IS_TMP);
2098
2097
    from_base= lc_from;
2099
2098
 
2100
 
    my_stpcpy(tmp_name, new_name);
 
2099
    stpcpy(tmp_name, new_name);
2101
2100
    my_casedn_str(files_charset_info, tmp_name);
2102
2101
    build_table_filename(lc_to, sizeof(lc_to), new_db, tmp_name, "",
2103
2102
                         flags & FN_TO_IS_TMP);
2127
2126
 
2128
2127
  SYNOPSIS
2129
2128
    wait_while_table_is_used()
2130
 
    session                     Thread handler
 
2129
    thd                 Thread handler
2131
2130
    table               Table to remove from cache
2132
2131
    function            HA_EXTRA_PREPARE_FOR_DROP if table is to be deleted
2133
2132
                        HA_EXTRA_FORCE_REOPEN if table is not be used
2141
2140
    Win32 clients must also have a WRITE LOCK on the table !
2142
2141
*/
2143
2142
 
2144
 
void wait_while_table_is_used(Session *session, Table *table,
 
2143
void wait_while_table_is_used(THD *thd, Table *table,
2145
2144
                              enum ha_extra_function function)
2146
2145
{
2147
2146
 
2148
2147
  safe_mutex_assert_owner(&LOCK_open);
2149
2148
 
2150
 
  table->file->extra(function);
 
2149
  VOID(table->file->extra(function));
2151
2150
  /* Mark all tables that are in use as 'old' */
2152
 
  mysql_lock_abort(session, table, true);       /* end threads waiting on lock */
 
2151
  mysql_lock_abort(thd, table, true);   /* end threads waiting on lock */
2153
2152
 
2154
2153
  /* Wait until all there are no other threads that has this table open */
2155
 
  remove_table_from_cache(session, table->s->db.str,
 
2154
  remove_table_from_cache(thd, table->s->db.str,
2156
2155
                          table->s->table_name.str,
2157
2156
                          RTFC_WAIT_OTHER_THREAD_FLAG);
2158
2157
  return;
2163
2162
 
2164
2163
  SYNOPSIS
2165
2164
    close_cached_table()
2166
 
    session                     Thread handler
 
2165
    thd                 Thread handler
2167
2166
    table               Table to remove from cache
2168
2167
 
2169
2168
  NOTES
2175
2174
    Win32 clients must also have a WRITE LOCK on the table !
2176
2175
*/
2177
2176
 
2178
 
void close_cached_table(Session *session, Table *table)
 
2177
void close_cached_table(THD *thd, Table *table)
2179
2178
{
2180
2179
 
2181
 
  wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
 
2180
  wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
2182
2181
  /* Close lock if this is not got with LOCK TABLES */
2183
 
  if (session->lock)
 
2182
  if (thd->lock)
2184
2183
  {
2185
 
    mysql_unlock_tables(session, session->lock);
2186
 
    session->lock=0;                    // Start locked threads
 
2184
    mysql_unlock_tables(thd, thd->lock);
 
2185
    thd->lock=0;                        // Start locked threads
2187
2186
  }
2188
2187
  /* Close all copies of 'table'.  This also frees all LOCK TABLES lock */
2189
 
  unlink_open_table(session, table, true);
 
2188
  unlink_open_table(thd, table, true);
2190
2189
 
2191
2190
  /* When lock on LOCK_open is freed other threads can continue */
2192
2191
  broadcast_refresh();
2193
2192
  return;
2194
2193
}
2195
2194
 
2196
 
static int send_check_errmsg(Session *session, TableList* table,
 
2195
static int send_check_errmsg(THD *thd, TableList* table,
2197
2196
                             const char* operator_name, const char* errmsg)
2198
2197
 
2199
2198
{
2200
 
  Protocol *protocol= session->protocol;
 
2199
  Protocol *protocol= thd->protocol;
2201
2200
  protocol->prepare_for_resend();
2202
2201
  protocol->store(table->alias, system_charset_info);
2203
2202
  protocol->store((char*) operator_name, system_charset_info);
2204
2203
  protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2205
2204
  protocol->store(errmsg, system_charset_info);
2206
 
  session->clear_error();
 
2205
  thd->clear_error();
2207
2206
  if (protocol->write())
2208
2207
    return -1;
2209
2208
  return 1;
2210
2209
}
2211
2210
 
2212
2211
 
2213
 
static int prepare_for_repair(Session *session, TableList *table_list,
 
2212
static int prepare_for_repair(THD *thd, TableList *table_list,
2214
2213
                              HA_CHECK_OPT *check_opt)
2215
2214
{
2216
2215
  int error= 0;
2226
2225
  if (!(table= table_list->table))              /* if open_ltable failed */
2227
2226
  {
2228
2227
    char key[MAX_DBKEY_LENGTH];
2229
 
    uint32_t key_length;
 
2228
    uint key_length;
2230
2229
 
2231
 
    key_length= create_table_def_key(session, key, table_list, 0);
 
2230
    key_length= create_table_def_key(thd, key, table_list, 0);
2232
2231
    pthread_mutex_lock(&LOCK_open);
2233
 
    if (!(share= (get_table_share(session, table_list, key, key_length, 0,
 
2232
    if (!(share= (get_table_share(thd, table_list, key, key_length, 0,
2234
2233
                                  &error))))
2235
2234
    {
2236
2235
      pthread_mutex_unlock(&LOCK_open);
2237
2236
      return(0);                                // Can't open frm file
2238
2237
    }
2239
2238
 
2240
 
    if (open_table_from_share(session, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
 
2239
    if (open_table_from_share(thd, share, "", 0, 0, 0, &tmp_table, OTM_OPEN))
2241
2240
    {
2242
2241
      release_table_share(share, RELEASE_NORMAL);
2243
2242
      pthread_mutex_unlock(&LOCK_open);
2252
2251
  */
2253
2252
  if (table->s->tmp_table)
2254
2253
  {
2255
 
    error= send_check_errmsg(session, table_list, "repair",
 
2254
    error= send_check_errmsg(thd, table_list, "repair",
2256
2255
                             "Cannot repair temporary table from .frm file");
2257
2256
    goto end;
2258
2257
  }
2278
2277
    goto end;                                   // No data file
2279
2278
 
2280
2279
  // Name of data file
2281
 
  strxmov(from, table->s->normalized_path.str, ext[1], NULL);
 
2280
  strxmov(from, table->s->normalized_path.str, ext[1], NullS);
2282
2281
  if (stat(from, &stat_info))
2283
2282
    goto end;                           // Can't use USE_FRM flag
2284
2283
 
2285
 
  snprintf(tmp, sizeof(tmp), "%s-%lx_%"PRIx64,
2286
 
           from, current_pid, session->thread_id);
 
2284
  snprintf(tmp, sizeof(tmp), "%s-%lx_%lx",
 
2285
           from, current_pid, thd->thread_id);
2287
2286
 
2288
2287
  /* If we could open the table, close it */
2289
2288
  if (table_list->table)
2290
2289
  {
2291
2290
    pthread_mutex_lock(&LOCK_open);
2292
 
    close_cached_table(session, table);
 
2291
    close_cached_table(thd, table);
2293
2292
    pthread_mutex_unlock(&LOCK_open);
2294
2293
  }
2295
 
  if (lock_and_wait_for_table_name(session,table_list))
 
2294
  if (lock_and_wait_for_table_name(thd,table_list))
2296
2295
  {
2297
2296
    error= -1;
2298
2297
    goto end;
2300
2299
  if (my_rename(from, tmp, MYF(MY_WME)))
2301
2300
  {
2302
2301
    pthread_mutex_lock(&LOCK_open);
2303
 
    unlock_table_name(session, table_list);
 
2302
    unlock_table_name(thd, table_list);
2304
2303
    pthread_mutex_unlock(&LOCK_open);
2305
 
    error= send_check_errmsg(session, table_list, "repair",
 
2304
    error= send_check_errmsg(thd, table_list, "repair",
2306
2305
                             "Failed renaming data file");
2307
2306
    goto end;
2308
2307
  }
2309
 
  if (mysql_truncate(session, table_list, 1))
 
2308
  if (mysql_truncate(thd, table_list, 1))
2310
2309
  {
2311
2310
    pthread_mutex_lock(&LOCK_open);
2312
 
    unlock_table_name(session, table_list);
 
2311
    unlock_table_name(thd, table_list);
2313
2312
    pthread_mutex_unlock(&LOCK_open);
2314
 
    error= send_check_errmsg(session, table_list, "repair",
 
2313
    error= send_check_errmsg(thd, table_list, "repair",
2315
2314
                             "Failed generating table from .frm file");
2316
2315
    goto end;
2317
2316
  }
2318
2317
  if (my_rename(tmp, from, MYF(MY_WME)))
2319
2318
  {
2320
2319
    pthread_mutex_lock(&LOCK_open);
2321
 
    unlock_table_name(session, table_list);
 
2320
    unlock_table_name(thd, table_list);
2322
2321
    pthread_mutex_unlock(&LOCK_open);
2323
 
    error= send_check_errmsg(session, table_list, "repair",
 
2322
    error= send_check_errmsg(thd, table_list, "repair",
2324
2323
                             "Failed restoring .MYD file");
2325
2324
    goto end;
2326
2325
  }
2330
2329
    to finish the repair in the handler later on.
2331
2330
  */
2332
2331
  pthread_mutex_lock(&LOCK_open);
2333
 
  if (reopen_name_locked_table(session, table_list, true))
 
2332
  if (reopen_name_locked_table(thd, table_list, true))
2334
2333
  {
2335
 
    unlock_table_name(session, table_list);
 
2334
    unlock_table_name(thd, table_list);
2336
2335
    pthread_mutex_unlock(&LOCK_open);
2337
 
    error= send_check_errmsg(session, table_list, "repair",
 
2336
    error= send_check_errmsg(thd, table_list, "repair",
2338
2337
                             "Failed to open partially repaired table");
2339
2338
    goto end;
2340
2339
  }
2358
2357
    true  Message should be sent by caller 
2359
2358
          (admin operation or network communication failed)
2360
2359
*/
2361
 
static bool mysql_admin_table(Session* session, TableList* tables,
 
2360
static bool mysql_admin_table(THD* thd, TableList* tables,
2362
2361
                              HA_CHECK_OPT* check_opt,
2363
2362
                              const char *operator_name,
2364
2363
                              thr_lock_type lock_type,
2365
2364
                              bool open_for_modify,
2366
2365
                              bool no_warnings_for_error,
2367
 
                              uint32_t extra_open_options,
2368
 
                              int (*prepare_func)(Session *, TableList *,
 
2366
                              uint extra_open_options,
 
2367
                              int (*prepare_func)(THD *, TableList *,
2369
2368
                                                  HA_CHECK_OPT *),
2370
 
                              int (handler::*operator_func)(Session *,
 
2369
                              int (handler::*operator_func)(THD *,
2371
2370
                                                            HA_CHECK_OPT *))
2372
2371
{
2373
2372
  TableList *table;
2374
 
  SELECT_LEX *select= &session->lex->select_lex;
 
2373
  SELECT_LEX *select= &thd->lex->select_lex;
2375
2374
  List<Item> field_list;
2376
2375
  Item *item;
2377
 
  Protocol *protocol= session->protocol;
2378
 
  LEX *lex= session->lex;
 
2376
  Protocol *protocol= thd->protocol;
 
2377
  LEX *lex= thd->lex;
2379
2378
  int result_code= 0;
2380
2379
  const CHARSET_INFO * const cs= system_charset_info;
2381
2380
 
2382
 
  if (end_active_trans(session))
 
2381
  if (end_active_trans(thd))
2383
2382
    return(1);
2384
2383
  field_list.push_back(item = new Item_empty_string("Table",
2385
2384
                                                    NAME_CHAR_LEN * 2,
2395
2394
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
2396
2395
    return(true);
2397
2396
 
2398
 
  mysql_ha_rm_tables(session, tables, false);
 
2397
  mysql_ha_rm_tables(thd, tables, false);
2399
2398
 
2400
2399
  for (table= tables; table; table= table->next_local)
2401
2400
  {
2403
2402
    char* db = table->db;
2404
2403
    bool fatal_error=0;
2405
2404
 
2406
 
    strxmov(table_name, db, ".", table->table_name, NULL);
2407
 
    session->open_options|= extra_open_options;
 
2405
    strxmov(table_name, db, ".", table->table_name, NullS);
 
2406
    thd->open_options|= extra_open_options;
2408
2407
    table->lock_type= lock_type;
2409
2408
    /* open only one table from local list of command */
2410
2409
    {
2413
2412
      table->next_global= 0;
2414
2413
      save_next_local= table->next_local;
2415
2414
      table->next_local= 0;
2416
 
      select->table_list.first= (unsigned char*)table;
 
2415
      select->table_list.first= (uchar*)table;
2417
2416
      /*
2418
2417
        Time zone tables and SP tables can be add to lex->query_tables list,
2419
2418
        so it have to be prepared.
2423
2422
      lex->query_tables= table;
2424
2423
      lex->query_tables_last= &table->next_global;
2425
2424
      lex->query_tables_own_last= 0;
2426
 
      session->no_warnings_for_error= no_warnings_for_error;
 
2425
      thd->no_warnings_for_error= no_warnings_for_error;
2427
2426
 
2428
 
      open_and_lock_tables(session, table);
2429
 
      session->no_warnings_for_error= 0;
 
2427
      open_and_lock_tables(thd, table);
 
2428
      thd->no_warnings_for_error= 0;
2430
2429
      table->next_global= save_next_global;
2431
2430
      table->next_local= save_next_local;
2432
 
      session->open_options&= ~extra_open_options;
 
2431
      thd->open_options&= ~extra_open_options;
2433
2432
    }
2434
2433
 
2435
2434
    if (prepare_func)
2436
2435
    {
2437
 
      switch ((*prepare_func)(session, table, check_opt)) {
 
2436
      switch ((*prepare_func)(thd, table, check_opt)) {
2438
2437
      case  1:           // error, message written to net
2439
 
        ha_autocommit_or_rollback(session, 1);
2440
 
        end_trans(session, ROLLBACK);
2441
 
        close_thread_tables(session);
 
2438
        ha_autocommit_or_rollback(thd, 1);
 
2439
        end_trans(thd, ROLLBACK);
 
2440
        close_thread_tables(thd);
2442
2441
        continue;
2443
2442
      case -1:           // error, message could be written to net
2444
2443
        /* purecov: begin inspected */
2459
2458
    */
2460
2459
    if (!table->table)
2461
2460
    {
2462
 
      if (!session->warn_list.elements)
2463
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2461
      if (!thd->warn_list.elements)
 
2462
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2464
2463
                     ER_CHECK_NO_SUCH_TABLE, ER(ER_CHECK_NO_SUCH_TABLE));
2465
2464
      goto send_result;
2466
2465
    }
2469
2468
    {
2470
2469
      /* purecov: begin inspected */
2471
2470
      char buff[FN_REFLEN + DRIZZLE_ERRMSG_SIZE];
2472
 
      uint32_t length;
 
2471
      uint length;
2473
2472
      protocol->prepare_for_resend();
2474
2473
      protocol->store(table_name, system_charset_info);
2475
2474
      protocol->store(operator_name, system_charset_info);
2477
2476
      length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
2478
2477
                       table_name);
2479
2478
      protocol->store(buff, length, system_charset_info);
2480
 
      ha_autocommit_or_rollback(session, 0);
2481
 
      end_trans(session, COMMIT);
2482
 
      close_thread_tables(session);
 
2479
      ha_autocommit_or_rollback(thd, 0);
 
2480
      end_trans(thd, COMMIT);
 
2481
      close_thread_tables(thd);
2483
2482
      lex->reset_query_tables_list(false);
2484
2483
      table->table=0;                           // For query cache
2485
2484
      if (protocol->write())
2492
2491
    if (lock_type == TL_WRITE && table->table->s->version)
2493
2492
    {
2494
2493
      pthread_mutex_lock(&LOCK_open);
2495
 
      const char *old_message=session->enter_cond(&COND_refresh, &LOCK_open,
 
2494
      const char *old_message=thd->enter_cond(&COND_refresh, &LOCK_open,
2496
2495
                                              "Waiting to get writelock");
2497
 
      mysql_lock_abort(session,table->table, true);
2498
 
      remove_table_from_cache(session, table->table->s->db.str,
 
2496
      mysql_lock_abort(thd,table->table, true);
 
2497
      remove_table_from_cache(thd, table->table->s->db.str,
2499
2498
                              table->table->s->table_name.str,
2500
2499
                              RTFC_WAIT_OTHER_THREAD_FLAG |
2501
2500
                              RTFC_CHECK_KILLED_FLAG);
2502
 
      session->exit_cond(old_message);
2503
 
      if (session->killed)
 
2501
      thd->exit_cond(old_message);
 
2502
      if (thd->killed)
2504
2503
        goto err;
2505
2504
      open_for_modify= 0;
2506
2505
    }
2526
2525
          (table->table->file->ha_check_for_upgrade(check_opt) ==
2527
2526
           HA_ADMIN_NEEDS_ALTER))
2528
2527
      {
2529
 
        ha_autocommit_or_rollback(session, 1);
2530
 
        close_thread_tables(session);
2531
 
        tmp_disable_binlog(session); // binlogging is done by caller if wanted
2532
 
        result_code= mysql_recreate_table(session, table);
2533
 
        reenable_binlog(session);
 
2528
        ha_autocommit_or_rollback(thd, 1);
 
2529
        close_thread_tables(thd);
 
2530
        tmp_disable_binlog(thd); // binlogging is done by caller if wanted
 
2531
        result_code= mysql_recreate_table(thd, table);
 
2532
        reenable_binlog(thd);
2534
2533
        /*
2535
2534
          mysql_recreate_table() can push OK or ERROR.
2536
2535
          Clear 'OK' status. If there is an error, keep it:
2537
2536
          we will store the error message in a result set row 
2538
2537
          and then clear.
2539
2538
        */
2540
 
        if (session->main_da.is_ok())
2541
 
          session->main_da.reset_diagnostics_area();
 
2539
        if (thd->main_da.is_ok())
 
2540
          thd->main_da.reset_diagnostics_area();
2542
2541
        goto send_result;
2543
2542
      }
2544
2543
    }
2545
2544
 
2546
 
    result_code = (table->table->file->*operator_func)(session, check_opt);
 
2545
    result_code = (table->table->file->*operator_func)(thd, check_opt);
2547
2546
 
2548
2547
send_result:
2549
2548
 
2550
2549
    lex->cleanup_after_one_table_open();
2551
 
    session->clear_error();  // these errors shouldn't get client
 
2550
    thd->clear_error();  // these errors shouldn't get client
2552
2551
    {
2553
 
      List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
 
2552
      List_iterator_fast<DRIZZLE_ERROR> it(thd->warn_list);
2554
2553
      DRIZZLE_ERROR *err;
2555
2554
      while ((err= it++))
2556
2555
      {
2564
2563
        if (protocol->write())
2565
2564
          goto err;
2566
2565
      }
2567
 
      drizzle_reset_errors(session, true);
 
2566
      drizzle_reset_errors(thd, true);
2568
2567
    }
2569
2568
    protocol->prepare_for_resend();
2570
2569
    protocol->store(table_name, system_charset_info);
2576
2575
    case HA_ADMIN_NOT_IMPLEMENTED:
2577
2576
      {
2578
2577
        char buf[ERRMSGSIZE+20];
2579
 
        uint32_t length=snprintf(buf, ERRMSGSIZE,
 
2578
        uint length=snprintf(buf, ERRMSGSIZE,
2580
2579
                             ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
2581
2580
        protocol->store(STRING_WITH_LEN("note"), system_charset_info);
2582
2581
        protocol->store(buf, length, system_charset_info);
2586
2585
    case HA_ADMIN_NOT_BASE_TABLE:
2587
2586
      {
2588
2587
        char buf[ERRMSGSIZE+20];
2589
 
        uint32_t length= snprintf(buf, ERRMSGSIZE,
 
2588
        uint length= snprintf(buf, ERRMSGSIZE,
2590
2589
                              ER(ER_BAD_TABLE_ERROR), table_name);
2591
2590
        protocol->store(STRING_WITH_LEN("note"), system_charset_info);
2592
2591
        protocol->store(buf, length, system_charset_info);
2636
2635
        "try with alter", so here we close the table, do an ALTER Table,
2637
2636
        reopen the table and do ha_innobase::analyze() on it.
2638
2637
      */
2639
 
      ha_autocommit_or_rollback(session, 0);
2640
 
      close_thread_tables(session);
 
2638
      ha_autocommit_or_rollback(thd, 0);
 
2639
      close_thread_tables(thd);
2641
2640
      TableList *save_next_local= table->next_local,
2642
2641
                 *save_next_global= table->next_global;
2643
2642
      table->next_local= table->next_global= 0;
2644
 
      tmp_disable_binlog(session); // binlogging is done by caller if wanted
2645
 
      result_code= mysql_recreate_table(session, table);
2646
 
      reenable_binlog(session);
 
2643
      tmp_disable_binlog(thd); // binlogging is done by caller if wanted
 
2644
      result_code= mysql_recreate_table(thd, table);
 
2645
      reenable_binlog(thd);
2647
2646
      /*
2648
2647
        mysql_recreate_table() can push OK or ERROR.
2649
2648
        Clear 'OK' status. If there is an error, keep it:
2650
2649
        we will store the error message in a result set row 
2651
2650
        and then clear.
2652
2651
      */
2653
 
      if (session->main_da.is_ok())
2654
 
        session->main_da.reset_diagnostics_area();
2655
 
      ha_autocommit_or_rollback(session, 0);
2656
 
      close_thread_tables(session);
 
2652
      if (thd->main_da.is_ok())
 
2653
        thd->main_da.reset_diagnostics_area();
 
2654
      ha_autocommit_or_rollback(thd, 0);
 
2655
      close_thread_tables(thd);
2657
2656
      if (!result_code) // recreation went ok
2658
2657
      {
2659
 
        if ((table->table= open_ltable(session, table, lock_type, 0)) &&
2660
 
            ((result_code= table->table->file->ha_analyze(session, check_opt)) > 0))
 
2658
        if ((table->table= open_ltable(thd, table, lock_type, 0)) &&
 
2659
            ((result_code= table->table->file->ha_analyze(thd, check_opt)) > 0))
2661
2660
          result_code= 0; // analyze went ok
2662
2661
      }
2663
2662
      if (result_code) // either mysql_recreate_table or analyze failed
2664
2663
      {
2665
 
        assert(session->is_error());
2666
 
        if (session->is_error())
 
2664
        assert(thd->is_error());
 
2665
        if (thd->is_error())
2667
2666
        {
2668
 
          const char *err_msg= session->main_da.message();
2669
 
          if (!session->vio_ok())
 
2667
          const char *err_msg= thd->main_da.message();
 
2668
          if (!thd->vio_ok())
2670
2669
          {
2671
 
            sql_print_error("%s",err_msg);
 
2670
            sql_print_error(err_msg);
2672
2671
          }
2673
2672
          else
2674
2673
          {
2681
2680
            protocol->store(table_name, system_charset_info);
2682
2681
            protocol->store(operator_name, system_charset_info);
2683
2682
          }
2684
 
          session->clear_error();
 
2683
          thd->clear_error();
2685
2684
        }
2686
2685
      }
2687
2686
      result_code= result_code ? HA_ADMIN_FAILED : HA_ADMIN_OK;
2701
2700
    case HA_ADMIN_NEEDS_ALTER:
2702
2701
    {
2703
2702
      char buf[ERRMSGSIZE];
2704
 
      uint32_t length;
 
2703
      uint length;
2705
2704
 
2706
2705
      protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2707
2706
      length=snprintf(buf, ERRMSGSIZE, ER(ER_TABLE_NEEDS_UPGRADE), table->table_name);
2713
2712
    default:                            // Probably HA_ADMIN_INTERNAL_ERROR
2714
2713
      {
2715
2714
        char buf[ERRMSGSIZE+20];
2716
 
        uint32_t length=snprintf(buf, ERRMSGSIZE,
 
2715
        uint length=snprintf(buf, ERRMSGSIZE,
2717
2716
                             _("Unknown - internal error %d during operation"),
2718
2717
                             result_code);
2719
2718
        protocol->store(STRING_WITH_LEN("error"), system_charset_info);
2733
2732
        else
2734
2733
        {
2735
2734
          pthread_mutex_lock(&LOCK_open);
2736
 
          remove_table_from_cache(session, table->table->s->db.str,
 
2735
          remove_table_from_cache(thd, table->table->s->db.str,
2737
2736
                                  table->table->s->table_name.str, RTFC_NO_FLAG);
2738
2737
          pthread_mutex_unlock(&LOCK_open);
2739
2738
        }
2740
2739
      }
2741
2740
    }
2742
 
    ha_autocommit_or_rollback(session, 0);
2743
 
    end_trans(session, COMMIT);
2744
 
    close_thread_tables(session);
 
2741
    ha_autocommit_or_rollback(thd, 0);
 
2742
    end_trans(thd, COMMIT);
 
2743
    close_thread_tables(thd);
2745
2744
    table->table=0;                             // For query cache
2746
2745
    if (protocol->write())
2747
2746
      goto err;
2748
2747
  }
2749
2748
 
2750
 
  my_eof(session);
 
2749
  my_eof(thd);
2751
2750
  return(false);
2752
2751
 
2753
2752
err:
2754
 
  ha_autocommit_or_rollback(session, 1);
2755
 
  end_trans(session, ROLLBACK);
2756
 
  close_thread_tables(session);                 // Shouldn't be needed
 
2753
  ha_autocommit_or_rollback(thd, 1);
 
2754
  end_trans(thd, ROLLBACK);
 
2755
  close_thread_tables(thd);                     // Shouldn't be needed
2757
2756
  if (table)
2758
2757
    table->table=0;
2759
2758
  return(true);
2760
2759
}
2761
2760
 
2762
2761
 
2763
 
bool mysql_repair_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
 
2762
bool mysql_repair_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2764
2763
{
2765
 
  return(mysql_admin_table(session, tables, check_opt,
 
2764
  return(mysql_admin_table(thd, tables, check_opt,
2766
2765
                                "repair", TL_WRITE, 1,
2767
2766
                                test(check_opt->sql_flags & TT_USEFRM),
2768
2767
                                HA_OPEN_FOR_REPAIR,
2771
2770
}
2772
2771
 
2773
2772
 
2774
 
bool mysql_optimize_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
 
2773
bool mysql_optimize_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
2775
2774
{
2776
 
  return(mysql_admin_table(session, tables, check_opt,
 
2775
  return(mysql_admin_table(thd, tables, check_opt,
2777
2776
                                "optimize", TL_WRITE, 1,0,0,0,
2778
2777
                                &handler::ha_optimize));
2779
2778
}
2784
2783
 
2785
2784
  SYNOPSIS
2786
2785
    mysql_assign_to_keycache()
2787
 
    session             Thread object
 
2786
    thd         Thread object
2788
2787
    tables      Table list (one table only)
2789
2788
 
2790
2789
  RETURN VALUES
2792
2791
   true  error
2793
2792
*/
2794
2793
 
2795
 
bool mysql_assign_to_keycache(Session* session, TableList* tables,
 
2794
bool mysql_assign_to_keycache(THD* thd, TableList* tables,
2796
2795
                             LEX_STRING *key_cache_name)
2797
2796
{
2798
2797
  HA_CHECK_OPT check_opt;
2808
2807
  }
2809
2808
  pthread_mutex_unlock(&LOCK_global_system_variables);
2810
2809
  check_opt.key_cache= key_cache;
2811
 
  return(mysql_admin_table(session, tables, &check_opt,
 
2810
  return(mysql_admin_table(thd, tables, &check_opt,
2812
2811
                                "assign_to_keycache", TL_READ_NO_INSERT, 0, 0,
2813
2812
                                0, 0, &handler::assign_to_keycache));
2814
2813
}
2819
2818
 
2820
2819
  SYNOPSIS
2821
2820
    reassign_keycache_tables()
2822
 
    session             Thread object
 
2821
    thd         Thread object
2823
2822
    src_cache   Reference to the key cache to clean up
2824
2823
    dest_cache  New key cache
2825
2824
 
2839
2838
    0     ok
2840
2839
*/
2841
2840
 
2842
 
int reassign_keycache_tables(Session *session __attribute__((unused)),
 
2841
int reassign_keycache_tables(THD *thd __attribute__((unused)),
2843
2842
                             KEY_CACHE *src_cache,
2844
2843
                             KEY_CACHE *dst_cache)
2845
2844
{
2854
2853
/**
2855
2854
  @brief          Create frm file based on I_S table
2856
2855
 
2857
 
  @param[in]      session                      thread handler
 
2856
  @param[in]      thd                      thread handler
2858
2857
  @param[in]      schema_table             I_S table           
2859
2858
  @param[in]      dst_path                 path where frm should be created
2860
2859
  @param[in]      create_info              Create info
2863
2862
    @retval       0                        success
2864
2863
    @retval       1                        error
2865
2864
*/
2866
 
bool mysql_create_like_schema_frm(Session* session, TableList* schema_table,
 
2865
bool mysql_create_like_schema_frm(THD* thd, TableList* schema_table,
2867
2866
                                  char *dst_path, HA_CREATE_INFO *create_info)
2868
2867
{
2869
2868
  HA_CREATE_INFO local_create_info;
2870
2869
  Alter_info alter_info;
2871
2870
  bool tmp_table= (create_info->options & HA_LEX_CREATE_TMP_TABLE);
2872
 
  uint32_t keys= schema_table->table->s->keys;
2873
 
  uint32_t db_options= 0;
 
2871
  uint keys= schema_table->table->s->keys;
 
2872
  uint db_options= 0;
2874
2873
 
2875
2874
  memset(&local_create_info, 0, sizeof(local_create_info));
2876
2875
  local_create_info.db_type= schema_table->table->s->db_type();
2878
2877
  local_create_info.default_table_charset=default_charset_info;
2879
2878
  alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
2880
2879
  schema_table->table->use_all_columns();
2881
 
  if (mysql_prepare_alter_table(session, schema_table->table,
 
2880
  if (mysql_prepare_alter_table(thd, schema_table->table,
2882
2881
                                &local_create_info, &alter_info))
2883
2882
    return(1);
2884
 
  if (mysql_prepare_create_table(session, &local_create_info, &alter_info,
 
2883
  if (mysql_prepare_create_table(thd, &local_create_info, &alter_info,
2885
2884
                                 tmp_table, &db_options,
2886
2885
                                 schema_table->table->file,
2887
2886
                                 &schema_table->table->s->key_info, &keys, 0))
2888
2887
    return(1);
2889
2888
  local_create_info.max_rows= 0;
2890
 
  if (mysql_create_frm(session, dst_path, NULL, NULL,
 
2889
  if (mysql_create_frm(thd, dst_path, NullS, NullS,
2891
2890
                       &local_create_info, alter_info.create_list,
2892
2891
                       keys, schema_table->table->s->key_info,
2893
2892
                       schema_table->table->file))
2901
2900
 
2902
2901
  SYNOPSIS
2903
2902
    mysql_create_like_table()
2904
 
    session             Thread object
 
2903
    thd         Thread object
2905
2904
    table       Table list element for target table
2906
2905
    src_table   Table list element for source table
2907
2906
    create_info Create info
2911
2910
    true  error
2912
2911
*/
2913
2912
 
2914
 
bool mysql_create_like_table(Session* session, TableList* table, TableList* src_table,
 
2913
bool mysql_create_like_table(THD* thd, TableList* table, TableList* src_table,
2915
2914
                             HA_CREATE_INFO *create_info)
2916
2915
{
2917
2916
  Table *name_lock= 0;
2918
2917
  char src_path[FN_REFLEN], dst_path[FN_REFLEN];
2919
 
  uint32_t dst_path_length;
 
2918
  uint dst_path_length;
2920
2919
  char *db= table->db;
2921
2920
  char *table_name= table->table_name;
2922
2921
  int  err;
2923
2922
  bool res= true;
2924
 
  uint32_t not_used;
 
2923
  uint not_used;
2925
2924
 
2926
2925
  /*
2927
2926
    By opening source table we guarantee that it exists and no concurrent
2932
2931
    we ensure that our statement is properly isolated from all concurrent
2933
2932
    operations which matter.
2934
2933
  */
2935
 
  if (open_tables(session, &src_table, &not_used, 0))
 
2934
  if (open_tables(thd, &src_table, &not_used, 0))
2936
2935
    return(true);
2937
2936
 
2938
 
  strxmov(src_path, src_table->table->s->path.str, reg_ext, NULL);
 
2937
  strxmov(src_path, src_table->table->s->path.str, reg_ext, NullS);
2939
2938
 
2940
2939
  /* 
2941
2940
    Check that destination tables does not exist. Note that its name
2943
2942
  */
2944
2943
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2945
2944
  {
2946
 
    if (find_temporary_table(session, db, table_name))
 
2945
    if (find_temporary_table(thd, db, table_name))
2947
2946
      goto table_exists;
2948
 
    dst_path_length= build_tmptable_filename(session, dst_path, sizeof(dst_path));
 
2947
    dst_path_length= build_tmptable_filename(thd, dst_path, sizeof(dst_path));
2949
2948
    create_info->table_options|= HA_CREATE_DELAY_KEY_WRITE;
2950
2949
  }
2951
2950
  else
2952
2951
  {
2953
 
    if (lock_table_name_if_not_cached(session, db, table_name, &name_lock))
 
2952
    if (lock_table_name_if_not_cached(thd, db, table_name, &name_lock))
2954
2953
      goto err;
2955
2954
    if (!name_lock)
2956
2955
      goto table_exists;
2973
2972
    Also some engines (e.g. NDB cluster) require that LOCK_open should be held
2974
2973
    during the call to ha_create_table(). See bug #28614 for more info.
2975
2974
  */
2976
 
  pthread_mutex_lock(&LOCK_open);
 
2975
  VOID(pthread_mutex_lock(&LOCK_open));
2977
2976
  if (src_table->schema_table)
2978
2977
  {
2979
 
    if (mysql_create_like_schema_frm(session, src_table, dst_path, create_info))
 
2978
    if (mysql_create_like_schema_frm(thd, src_table, dst_path, create_info))
2980
2979
    {
2981
 
      pthread_mutex_unlock(&LOCK_open);
 
2980
      VOID(pthread_mutex_unlock(&LOCK_open));
2982
2981
      goto err;
2983
2982
    }
2984
2983
  }
2988
2987
      my_error(ER_BAD_DB_ERROR,MYF(0),db);
2989
2988
    else
2990
2989
      my_error(ER_CANT_CREATE_FILE,MYF(0),dst_path,my_errno);
2991
 
    pthread_mutex_unlock(&LOCK_open);
 
2990
    VOID(pthread_mutex_unlock(&LOCK_open));
2992
2991
    goto err;
2993
2992
  }
2994
2993
 
2998
2997
    and temporary tables).
2999
2998
  */
3000
2999
  dst_path[dst_path_length - reg_ext_length]= '\0';  // Remove .frm
3001
 
  if (session->variables.keep_files_on_create)
 
3000
  if (thd->variables.keep_files_on_create)
3002
3001
    create_info->options|= HA_CREATE_KEEP_FILES;
3003
 
  err= ha_create_table(session, dst_path, db, table_name, create_info, 1);
3004
 
  pthread_mutex_unlock(&LOCK_open);
 
3002
  err= ha_create_table(thd, dst_path, db, table_name, create_info, 1);
 
3003
  VOID(pthread_mutex_unlock(&LOCK_open));
3005
3004
 
3006
3005
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
3007
3006
  {
3008
 
    if (err || !open_temporary_table(session, dst_path, db, table_name, 1,
 
3007
    if (err || !open_temporary_table(thd, dst_path, db, table_name, 1,
3009
3008
                                     OTM_OPEN))
3010
3009
    {
3011
3010
      (void) rm_temporary_table(create_info->db_type,
3023
3022
  /*
3024
3023
    We have to write the query before we unlock the tables.
3025
3024
  */
3026
 
  if (session->current_stmt_binlog_row_based)
 
3025
  if (thd->current_stmt_binlog_row_based)
3027
3026
  {
3028
3027
    /*
3029
3028
       Since temporary tables are not replicated under row-based
3056
3055
          of this function.
3057
3056
        */
3058
3057
        table->table= name_lock;
3059
 
        pthread_mutex_lock(&LOCK_open);
3060
 
        if (reopen_name_locked_table(session, table, false))
 
3058
        VOID(pthread_mutex_lock(&LOCK_open));
 
3059
        if (reopen_name_locked_table(thd, table, false))
3061
3060
        {
3062
 
          pthread_mutex_unlock(&LOCK_open);
 
3061
          VOID(pthread_mutex_unlock(&LOCK_open));
3063
3062
          goto err;
3064
3063
        }
3065
 
        pthread_mutex_unlock(&LOCK_open);
 
3064
        VOID(pthread_mutex_unlock(&LOCK_open));
3066
3065
 
3067
 
        int result= store_create_info(session, table, &query,
 
3066
        int result= store_create_info(thd, table, &query,
3068
3067
                                               create_info);
3069
3068
 
3070
3069
        assert(result == 0); // store_create_info() always return 0
3071
 
        write_bin_log(session, true, query.ptr(), query.length());
 
3070
        write_bin_log(thd, true, query.ptr(), query.length());
3072
3071
      }
3073
3072
      else                                      // Case 1
3074
 
        write_bin_log(session, true, session->query, session->query_length);
 
3073
        write_bin_log(thd, true, thd->query, thd->query_length);
3075
3074
    }
3076
3075
    /*
3077
3076
      Case 3 and 4 does nothing under RBR
3078
3077
    */
3079
3078
  }
3080
3079
  else
3081
 
    write_bin_log(session, true, session->query, session->query_length);
 
3080
    write_bin_log(thd, true, thd->query, thd->query_length);
3082
3081
 
3083
3082
  res= false;
3084
3083
  goto err;
3089
3088
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3090
3089
    snprintf(warn_buff, sizeof(warn_buff),
3091
3090
             ER(ER_TABLE_EXISTS_ERROR), table_name);
3092
 
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
3091
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3093
3092
                 ER_TABLE_EXISTS_ERROR,warn_buff);
3094
3093
    res= false;
3095
3094
  }
3100
3099
  if (name_lock)
3101
3100
  {
3102
3101
    pthread_mutex_lock(&LOCK_open);
3103
 
    unlink_open_table(session, name_lock, false);
 
3102
    unlink_open_table(thd, name_lock, false);
3104
3103
    pthread_mutex_unlock(&LOCK_open);
3105
3104
  }
3106
3105
  return(res);
3107
3106
}
3108
3107
 
3109
3108
 
3110
 
bool mysql_analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
 
3109
bool mysql_analyze_table(THD* thd, TableList* tables, HA_CHECK_OPT* check_opt)
3111
3110
{
3112
3111
  thr_lock_type lock_type = TL_READ_NO_INSERT;
3113
3112
 
3114
 
  return(mysql_admin_table(session, tables, check_opt,
 
3113
  return(mysql_admin_table(thd, tables, check_opt,
3115
3114
                                "analyze", lock_type, 1, 0, 0, 0,
3116
3115
                                &handler::ha_analyze));
3117
3116
}
3118
3117
 
3119
3118
 
3120
 
bool mysql_check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
 
3119
bool mysql_check_table(THD* thd, TableList* tables,HA_CHECK_OPT* check_opt)
3121
3120
{
3122
3121
  thr_lock_type lock_type = TL_READ_NO_INSERT;
3123
3122
 
3124
 
  return(mysql_admin_table(session, tables, check_opt,
 
3123
  return(mysql_admin_table(thd, tables, check_opt,
3125
3124
                                "check", lock_type,
3126
3125
                                0, 0, HA_OPEN_FOR_REPAIR, 0,
3127
3126
                                &handler::ha_check));
3130
3129
 
3131
3130
/* table_list should contain just one table */
3132
3131
static int
3133
 
mysql_discard_or_import_tablespace(Session *session,
 
3132
mysql_discard_or_import_tablespace(THD *thd,
3134
3133
                                   TableList *table_list,
3135
3134
                                   enum tablespace_op_type tablespace_op)
3136
3135
{
3143
3142
    ALTER Table
3144
3143
  */
3145
3144
 
3146
 
  session->set_proc_info("discard_or_import_tablespace");
 
3145
  thd_proc_info(thd, "discard_or_import_tablespace");
3147
3146
 
3148
3147
  discard= test(tablespace_op == DISCARD_TABLESPACE);
3149
3148
 
3151
3150
   We set this flag so that ha_innobase::open and ::external_lock() do
3152
3151
   not complain when we lock the table
3153
3152
 */
3154
 
  session->tablespace_op= true;
3155
 
  if (!(table=open_ltable(session, table_list, TL_WRITE, 0)))
 
3153
  thd->tablespace_op= true;
 
3154
  if (!(table=open_ltable(thd, table_list, TL_WRITE, 0)))
3156
3155
  {
3157
 
    session->tablespace_op=false;
 
3156
    thd->tablespace_op=false;
3158
3157
    return(-1);
3159
3158
  }
3160
3159
 
3161
3160
  error= table->file->ha_discard_or_import_tablespace(discard);
3162
3161
 
3163
 
  session->set_proc_info("end");
 
3162
  thd_proc_info(thd, "end");
3164
3163
 
3165
3164
  if (error)
3166
3165
    goto err;
3167
3166
 
3168
3167
  /* The ALTER Table is always in its own transaction */
3169
 
  error = ha_autocommit_or_rollback(session, 0);
3170
 
  if (end_active_trans(session))
 
3168
  error = ha_autocommit_or_rollback(thd, 0);
 
3169
  if (end_active_trans(thd))
3171
3170
    error=1;
3172
3171
  if (error)
3173
3172
    goto err;
3174
 
  write_bin_log(session, false, session->query, session->query_length);
 
3173
  write_bin_log(thd, false, thd->query, thd->query_length);
3175
3174
 
3176
3175
err:
3177
 
  ha_autocommit_or_rollback(session, error);
3178
 
  session->tablespace_op=false;
 
3176
  ha_autocommit_or_rollback(thd, error);
 
3177
  thd->tablespace_op=false;
3179
3178
  
3180
3179
  if (error == 0)
3181
3180
  {
3182
 
    my_ok(session);
 
3181
    my_ok(thd);
3183
3182
    return(0);
3184
3183
  }
3185
3184
 
3194
3193
 
3195
3194
void setup_ha_alter_flags(Alter_info *alter_info, HA_ALTER_FLAGS *alter_flags)
3196
3195
{
3197
 
  uint32_t flags= alter_info->flags;
 
3196
  uint flags= alter_info->flags;
3198
3197
 
3199
3198
  if (ALTER_ADD_COLUMN & flags)
3200
3199
    *alter_flags|= HA_ADD_COLUMN;
3224
3223
 
3225
3224
 
3226
3225
/**
3227
 
   @param       session                Thread
 
3226
   @param       thd                Thread
3228
3227
   @param       table              The original table.
3229
3228
   @param       alter_info         Alter options, fields and keys for the new
3230
3229
                                   table.
3256
3255
 
3257
3256
static
3258
3257
bool
3259
 
compare_tables(Session *session,
 
3258
compare_tables(THD *thd,
3260
3259
               Table *table,
3261
3260
               Alter_info *alter_info,
3262
3261
                           HA_CREATE_INFO *create_info,
3263
 
               uint32_t order_num,
 
3262
               uint order_num,
3264
3263
               HA_ALTER_FLAGS *alter_flags,
3265
3264
               HA_ALTER_INFO *ha_alter_info,
3266
 
               uint32_t *table_changes)
 
3265
               uint *table_changes)
3267
3266
{
3268
3267
  Field **f_ptr, *field;
3269
 
  uint32_t table_changes_local= 0;
 
3268
  uint table_changes_local= 0;
3270
3269
  List_iterator_fast<Create_field> new_field_it(alter_info->create_list);
3271
3270
  Create_field *new_field;
3272
3271
  KEY_PART_INFO *key_part;
3293
3292
      to evaluate possibility of fast ALTER Table, and then
3294
3293
      destroy the copy.
3295
3294
    */
3296
 
    Alter_info tmp_alter_info(*alter_info, session->mem_root);
3297
 
    Session *session= table->in_use;
3298
 
    uint32_t db_options= 0; /* not used */
 
3295
    Alter_info tmp_alter_info(*alter_info, thd->mem_root);
 
3296
    THD *thd= table->in_use;
 
3297
    uint db_options= 0; /* not used */
3299
3298
    /* Create the prepared information. */
3300
 
    if (mysql_prepare_create_table(session, create_info,
 
3299
    if (mysql_prepare_create_table(thd, create_info,
3301
3300
                                   &tmp_alter_info,
3302
3301
                                   (table->s->tmp_table != NO_TMP_TABLE),
3303
3302
                                   &db_options,
3308
3307
      return(true);
3309
3308
    /* Allocate result buffers. */
3310
3309
    if (! (ha_alter_info->index_drop_buffer=
3311
 
           (uint*) session->alloc(sizeof(uint) * table->s->keys)) ||
 
3310
           (uint*) thd->alloc(sizeof(uint) * table->s->keys)) ||
3312
3311
        ! (ha_alter_info->index_add_buffer=
3313
 
           (uint*) session->alloc(sizeof(uint) *
 
3312
           (uint*) thd->alloc(sizeof(uint) *
3314
3313
                              tmp_alter_info.key_list.elements)))
3315
3314
      return(true);
3316
3315
  }
3408
3407
      if (!(table_changes_local= field->is_equal(new_field)))
3409
3408
        *alter_flags|= HA_ALTER_COLUMN_TYPE;
3410
3409
 
3411
 
      /*
3412
 
        Check if the altered column is a stored virtual field.
3413
 
        TODO: Mark such a column with an alter flag only if
3414
 
        the expression functions are not equal. 
3415
 
      */
3416
 
      if (field->is_stored && field->vcol_info)
3417
 
        *alter_flags|= HA_ALTER_STORED_VCOL;
3418
 
 
3419
3410
      /* Check if field was renamed */
3420
3411
      field->flags&= ~FIELD_IS_RENAMED;
3421
3412
      if (my_strcasecmp(system_charset_info,
3645
3636
 
3646
3637
  if (error == HA_ERR_WRONG_COMMAND)
3647
3638
  {
3648
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
3639
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
3649
3640
                        ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
3650
3641
                        table->s->table_name.str);
3651
3642
    error= 0;
3655
3646
  return(error);
3656
3647
}
3657
3648
 
3658
 
int create_temporary_table(Session *session,
 
3649
int create_temporary_table(THD *thd,
3659
3650
                           Table *table,
3660
3651
                           char *new_db,
3661
3652
                           char *tmp_name,
3697
3688
    if (create_info->index_file_name)
3698
3689
    {
3699
3690
      /* Fix index_file_name to have 'tmp_name' as basename */
3700
 
      my_stpcpy(index_file, tmp_name);
 
3691
      stpcpy(index_file, tmp_name);
3701
3692
      create_info->index_file_name=fn_same(index_file,
3702
3693
                                           create_info->index_file_name,
3703
3694
                                           1);
3705
3696
    if (create_info->data_file_name)
3706
3697
    {
3707
3698
      /* Fix data_file_name to have 'tmp_name' as basename */
3708
 
      my_stpcpy(data_file, tmp_name);
 
3699
      stpcpy(data_file, tmp_name);
3709
3700
      create_info->data_file_name=fn_same(data_file,
3710
3701
                                          create_info->data_file_name,
3711
3702
                                          1);
3719
3710
    With create_info->frm_only == 1 this creates a .frm file only.
3720
3711
    We don't log the statement, it will be logged later.
3721
3712
  */
3722
 
  tmp_disable_binlog(session);
3723
 
  error= mysql_create_table(session, new_db, tmp_name,
 
3713
  tmp_disable_binlog(thd);
 
3714
  error= mysql_create_table(thd, new_db, tmp_name,
3724
3715
                            create_info, alter_info, 1, 0);
3725
 
  reenable_binlog(session);
 
3716
  reenable_binlog(thd);
3726
3717
 
3727
3718
  return(error);
3728
3719
}
3733
3724
 
3734
3725
  SYNOPSIS
3735
3726
    create_altered_table()
3736
 
      session              Thread handle
 
3727
      thd              Thread handle
3737
3728
      table            The original table
3738
3729
      create_info      Information from the parsing phase about new
3739
3730
                       table properties.
3747
3738
    The temporary table is created without storing it in any storage engine
3748
3739
    and is opened only to get the table struct and frm file reference.
3749
3740
*/
3750
 
Table *create_altered_table(Session *session,
 
3741
Table *create_altered_table(THD *thd,
3751
3742
                            Table *table,
3752
3743
                            char *new_db,
3753
3744
                            HA_CREATE_INFO *create_info,
3760
3751
  char tmp_name[80];
3761
3752
  char path[FN_REFLEN];
3762
3753
 
3763
 
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64,
3764
 
           tmp_file_prefix, current_pid, session->thread_id);
 
3754
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx",
 
3755
           tmp_file_prefix, current_pid, thd->thread_id);
3765
3756
  /* Safety fix for InnoDB */
3766
3757
  if (lower_case_table_names)
3767
3758
    my_casedn_str(files_charset_info, tmp_name);
3768
3759
  altered_create_info.options&= ~HA_LEX_CREATE_TMP_TABLE;
3769
3760
  altered_create_info.frm_only= 1;
3770
 
  if ((error= create_temporary_table(session, table, new_db, tmp_name,
 
3761
  if ((error= create_temporary_table(thd, table, new_db, tmp_name,
3771
3762
                                     &altered_create_info,
3772
3763
                                     alter_info, db_change)))
3773
3764
  {
3776
3767
 
3777
3768
  build_table_filename(path, sizeof(path), new_db, tmp_name, "",
3778
3769
                       FN_IS_TMP);
3779
 
  altered_table= open_temporary_table(session, path, new_db, tmp_name, 1,
 
3770
  altered_table= open_temporary_table(thd, path, new_db, tmp_name, 1,
3780
3771
                                      OTM_ALTER);
3781
3772
  return(altered_table);
3782
3773
 
3789
3780
 
3790
3781
  SYNOPSIS
3791
3782
    mysql_fast_or_online_alter_table()
3792
 
      session              Thread handle
 
3783
      thd              Thread handle
3793
3784
      table            The original table
3794
3785
      altered_table    A temporary table showing how we will change table
3795
3786
      create_info      Information from the parsing phase about new
3809
3800
    operation directly, on-line without mysql having to copy
3810
3801
    the table.
3811
3802
*/
3812
 
int mysql_fast_or_online_alter_table(Session *session,
 
3803
int mysql_fast_or_online_alter_table(THD *thd,
3813
3804
                                     Table *table,
3814
3805
                                     Table *altered_table,
3815
3806
                                     HA_CREATE_INFO *create_info,
3826
3817
   /*
3827
3818
      Tell the handler to prepare for the online alter
3828
3819
    */
3829
 
    if ((error= table->file->alter_table_phase1(session,
 
3820
    if ((error= table->file->alter_table_phase1(thd,
3830
3821
                                                altered_table,
3831
3822
                                                create_info,
3832
3823
                                                alter_info,
3841
3832
       if check_if_supported_alter() returned HA_ALTER_SUPPORTED_WAIT_LOCK
3842
3833
       we need to wrap the next call with a DDL lock.
3843
3834
     */
3844
 
    if ((error= table->file->alter_table_phase2(session,
 
3835
    if ((error= table->file->alter_table_phase2(thd,
3845
3836
                                                altered_table,
3846
3837
                                                create_info,
3847
3838
                                                alter_info,
3854
3845
    The final .frm file is already created as a temporary file
3855
3846
    and will be renamed to the original table name.
3856
3847
  */
3857
 
  pthread_mutex_lock(&LOCK_open);
3858
 
  wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
 
3848
  VOID(pthread_mutex_lock(&LOCK_open));
 
3849
  wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
3859
3850
  alter_table_manage_keys(table, table->file->indexes_are_disabled(),
3860
3851
                          keys_onoff);
3861
 
  close_data_files_and_morph_locks(session,
 
3852
  close_data_files_and_morph_locks(thd,
3862
3853
                                   table->pos_in_table_list->db,
3863
3854
                                   table->pos_in_table_list->table_name);
3864
3855
  if (mysql_rename_table(NULL,
3868
3859
                         table->s->table_name.str, FN_FROM_IS_TMP))
3869
3860
  {
3870
3861
    error= 1;
3871
 
    pthread_mutex_unlock(&LOCK_open);
 
3862
    VOID(pthread_mutex_unlock(&LOCK_open));
3872
3863
    goto err;
3873
3864
  }
3874
3865
  broadcast_refresh();
3875
 
  pthread_mutex_unlock(&LOCK_open);
 
3866
  VOID(pthread_mutex_unlock(&LOCK_open));
3876
3867
 
3877
3868
  /*
3878
3869
    The ALTER Table is always in its own transaction.
3880
3871
    wait_if_global_read_lock(), which could create a deadlock if called
3881
3872
    with LOCK_open.
3882
3873
  */
3883
 
  error= ha_autocommit_or_rollback(session, 0);
 
3874
  error= ha_autocommit_or_rollback(thd, 0);
3884
3875
 
3885
 
  if (ha_commit(session))
 
3876
  if (ha_commit(thd))
3886
3877
    error=1;
3887
3878
  if (error)
3888
3879
    goto err;
3889
3880
  if (online)
3890
3881
  {
3891
 
    pthread_mutex_lock(&LOCK_open);
 
3882
    VOID(pthread_mutex_lock(&LOCK_open));
3892
3883
    if (reopen_table(table))
3893
3884
    {
3894
3885
      error= -1;
3895
3886
      goto err;
3896
3887
    }
3897
 
    pthread_mutex_unlock(&LOCK_open);
 
3888
    VOID(pthread_mutex_unlock(&LOCK_open));
3898
3889
    t_table= table;
3899
3890
 
3900
3891
   /*
3901
3892
      Tell the handler that the changed frm is on disk and table
3902
3893
      has been re-opened
3903
3894
   */
3904
 
    if ((error= t_table->file->alter_table_phase3(session, t_table)))
 
3895
    if ((error= t_table->file->alter_table_phase3(thd, t_table)))
3905
3896
    {
3906
3897
      goto err;
3907
3898
    }
3913
3904
    */
3914
3905
    assert(t_table == table);
3915
3906
    table->open_placeholder= 1;
3916
 
    pthread_mutex_lock(&LOCK_open);
 
3907
    VOID(pthread_mutex_lock(&LOCK_open));
3917
3908
    close_handle_and_leave_table_as_lock(table);
3918
 
    pthread_mutex_unlock(&LOCK_open);
 
3909
    VOID(pthread_mutex_unlock(&LOCK_open));
3919
3910
  }
3920
3911
 
3921
3912
 err:
3940
3931
  instructions that require change in table data, not only in
3941
3932
  table definition or indexes.
3942
3933
 
3943
 
  @param[in,out]  session         thread handle. Used as a memory pool
 
3934
  @param[in,out]  thd         thread handle. Used as a memory pool
3944
3935
                              and source of environment information.
3945
3936
  @param[in]      table       the source table, open and locked
3946
3937
                              Used as an interface to the storage engine
3967
3958
*/
3968
3959
 
3969
3960
static bool
3970
 
mysql_prepare_alter_table(Session *session, Table *table,
 
3961
mysql_prepare_alter_table(THD *thd, Table *table,
3971
3962
                          HA_CREATE_INFO *create_info,
3972
3963
                          Alter_info *alter_info)
3973
3964
{
3982
3973
  List_iterator<Create_field> find_it(new_create_list);
3983
3974
  List_iterator<Create_field> field_it(new_create_list);
3984
3975
  List<Key_part_spec> key_parts;
3985
 
  uint32_t db_create_options= (table->s->db_create_options
 
3976
  uint db_create_options= (table->s->db_create_options
3986
3977
                           & ~(HA_OPTION_PACK_RECORD));
3987
 
  uint32_t used_fields= create_info->used_fields;
 
3978
  uint used_fields= create_info->used_fields;
3988
3979
  KEY *key_info=table->key_info;
3989
3980
  bool rc= true;
3990
3981
 
4009
4000
  }
4010
4001
  if (!(used_fields & HA_CREATE_USED_KEY_BLOCK_SIZE))
4011
4002
    create_info->key_block_size= table->s->key_block_size;
 
4003
  if (!(used_fields & HA_CREATE_USED_TRANSACTIONAL))
 
4004
    create_info->transactional= table->s->transactional;
4012
4005
 
4013
4006
  restore_record(table, s->default_values);     // Empty record for DEFAULT
4014
4007
  Create_field *def;
4053
4046
    if (def)
4054
4047
    {                                           // Field is changed
4055
4048
      def->field=field;
4056
 
      if (field->is_stored != def->is_stored)
4057
 
      {
4058
 
        my_error(ER_UNSUPPORTED_ACTION_ON_VIRTUAL_COLUMN,
4059
 
                 MYF(0),
4060
 
                 "Changing the STORED status");
4061
 
        goto err;
4062
 
      }
4063
4049
      if (!def->after)
4064
4050
        {
4065
4051
        new_create_list.push_back(def);
4115
4101
         def->sql_type == DRIZZLE_TYPE_DATETIME) &&
4116
4102
         !alter_info->datetime_field &&
4117
4103
         !(~def->flags & (NO_DEFAULT_VALUE_FLAG | NOT_NULL_FLAG)) &&
4118
 
         session->variables.sql_mode & MODE_NO_ZERO_DATE)
 
4104
         thd->variables.sql_mode & MODE_NO_ZERO_DATE)
4119
4105
    {
4120
4106
        alter_info->datetime_field= def;
4121
4107
        alter_info->error_if_not_empty= true;
4151
4137
      */
4152
4138
      if (alter_info->build_method == HA_BUILD_ONLINE)
4153
4139
      {
4154
 
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
 
4140
        my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4155
4141
        goto err;
4156
4142
      }
4157
4143
      alter_info->build_method= HA_BUILD_OFFLINE;
4175
4161
    for which some fields exists.
4176
4162
    */
4177
4163
 
4178
 
  for (uint32_t i=0 ; i < table->s->keys ; i++,key_info++)
 
4164
  for (uint i=0 ; i < table->s->keys ; i++,key_info++)
4179
4165
    {
4180
4166
    char *key_name= key_info->name;
4181
4167
    Alter_drop *drop;
4194
4180
 
4195
4181
    KEY_PART_INFO *key_part= key_info->key_part;
4196
4182
    key_parts.empty();
4197
 
    for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
 
4183
    for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)
4198
4184
    {
4199
4185
      if (!key_part->field)
4200
4186
        continue;                               // Wrong field (from UNIREG)
4215
4201
      }
4216
4202
      if (!cfield)
4217
4203
        continue;                               // Field is removed
4218
 
      uint32_t key_part_length=key_part->length;
 
4204
      uint key_part_length=key_part->length;
4219
4205
      if (cfield->field)                        // Not new field
4220
4206
      {
4221
4207
        /*
4277
4263
    Key *key;
4278
4264
    while ((key=key_it++))                      // Add new keys
4279
4265
    {
4280
 
      if (key->type == Key::FOREIGN_KEY &&
4281
 
          ((Foreign_key *)key)->validate(new_create_list))
4282
 
        goto err;
4283
4266
      if (key->type != Key::FOREIGN_KEY)
4284
4267
        new_key_list.push_back(key);
4285
4268
      if (key->name.str &&
4340
4323
 
4341
4324
  SYNOPSIS
4342
4325
    mysql_alter_table()
4343
 
      session              Thread handle
 
4326
      thd              Thread handle
4344
4327
      new_db           If there is a RENAME clause
4345
4328
      new_name         If there is a RENAME clause
4346
4329
      create_info      Information from the parsing phase about new
4377
4360
    true   Error
4378
4361
*/
4379
4362
 
4380
 
bool mysql_alter_table(Session *session,char *new_db, char *new_name,
 
4363
bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
4381
4364
                       HA_CREATE_INFO *create_info,
4382
4365
                       TableList *table_list,
4383
4366
                       Alter_info *alter_info,
4384
 
                       uint32_t order_num, order_st *order, bool ignore)
 
4367
                       uint order_num, order_st *order, bool ignore)
4385
4368
{
4386
4369
  Table *table, *new_table=0, *name_lock= 0;;
4387
4370
  int error= 0;
4403
4386
    to simplify further comparisons: we want to see if it's a RENAME
4404
4387
    later just by comparing the pointers, avoiding the need for strcmp.
4405
4388
  */
4406
 
  session->set_proc_info("init");
 
4389
  thd_proc_info(thd, "init");
4407
4390
  table_name=table_list->table_name;
4408
4391
  alias= (lower_case_table_names == 2) ? table_list->alias : table_name;
4409
4392
  db=table_list->db;
4411
4394
    new_db= db;
4412
4395
  build_table_filename(path, sizeof(path), db, table_name, "", 0);
4413
4396
 
4414
 
  mysql_ha_rm_tables(session, table_list, false);
 
4397
  mysql_ha_rm_tables(thd, table_list, false);
4415
4398
 
4416
4399
  /* DISCARD/IMPORT TABLESPACE is always alone in an ALTER Table */
4417
4400
  if (alter_info->tablespace_op != NO_TABLESPACE_OP)
4418
4401
    /* Conditionally writes to binlog. */
4419
 
    return(mysql_discard_or_import_tablespace(session,table_list,
 
4402
    return(mysql_discard_or_import_tablespace(thd,table_list,
4420
4403
                                              alter_info->tablespace_op));
4421
 
  char* pos= new_name_buff;
4422
 
  char* pos_end= pos+strlen(new_name_buff)-1;
4423
 
  pos= my_stpncpy(new_name_buff, mysql_data_home, pos_end-pos);
4424
 
  pos= my_stpncpy(new_name_buff, "/", pos_end-pos);
4425
 
  pos= my_stpncpy(new_name_buff, db, pos_end-pos);
4426
 
  pos= my_stpncpy(new_name_buff, "/", pos_end-pos);
4427
 
  pos= my_stpncpy(new_name_buff, table_name, pos_end-pos);
4428
 
  pos= my_stpncpy(new_name_buff, reg_ext, pos_end-pos);
4429
 
 
 
4404
  strxnmov(new_name_buff, sizeof (new_name_buff) - 1, mysql_data_home, "/", db, 
 
4405
           "/", table_name, reg_ext, NullS);
4430
4406
  (void) unpack_filename(new_name_buff, new_name_buff);
4431
4407
  /*
4432
4408
    If this is just a rename of a view, short cut to the
4441
4417
    into the main table list, like open_tables does).
4442
4418
    This code is wrong and will be removed, please do not copy.
4443
4419
  */
4444
 
  (void)mysql_frm_type(session, new_name_buff, &table_type);
 
4420
  (void)mysql_frm_type(thd, new_name_buff, &table_type);
4445
4421
 
4446
 
  if (!(table= open_n_lock_single_table(session, table_list, TL_WRITE_ALLOW_READ)))
 
4422
  if (!(table= open_n_lock_single_table(thd, table_list, TL_WRITE_ALLOW_READ)))
4447
4423
    return(true);
4448
4424
  table->use_all_columns();
4449
4425
 
4450
4426
  /* Check that we are not trying to rename to an existing table */
4451
4427
  if (new_name)
4452
4428
  {
4453
 
    my_stpcpy(new_name_buff,new_name);
4454
 
    my_stpcpy(new_alias= new_alias_buff, new_name);
 
4429
    stpcpy(new_name_buff,new_name);
 
4430
    stpcpy(new_alias= new_alias_buff, new_name);
4455
4431
    if (lower_case_table_names)
4456
4432
    {
4457
4433
      if (lower_case_table_names != 2)
4474
4450
    {
4475
4451
      if (table->s->tmp_table != NO_TMP_TABLE)
4476
4452
      {
4477
 
        if (find_temporary_table(session,new_db,new_name_buff))
 
4453
        if (find_temporary_table(thd,new_db,new_name_buff))
4478
4454
        {
4479
4455
          my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_name_buff);
4480
4456
          return(true);
4482
4458
      }
4483
4459
      else
4484
4460
      {
4485
 
        if (lock_table_name_if_not_cached(session, new_db, new_name, &name_lock))
 
4461
        if (lock_table_name_if_not_cached(thd, new_db, new_name, &name_lock))
4486
4462
          return(true);
4487
4463
        if (!name_lock)
4488
4464
        {
4513
4489
    create_info->db_type= old_db_type;
4514
4490
  }
4515
4491
 
4516
 
  if (check_engine(session, new_name, create_info))
 
4492
  if (check_engine(thd, new_name, create_info))
4517
4493
    goto err;
4518
4494
  new_db_type= create_info->db_type;
4519
4495
 
4535
4511
    goto err;
4536
4512
  }
4537
4513
 
4538
 
  session->set_proc_info("setup");
 
4514
  thd_proc_info(thd, "setup");
4539
4515
  if (!(alter_info->flags & ~(ALTER_RENAME | ALTER_KEYS_ONOFF)) &&
4540
4516
      !table->s->tmp_table) // no need to touch frm
4541
4517
  {
4552
4528
        while the fact that the table is still open gives us protection
4553
4529
        from concurrent DDL statements.
4554
4530
      */
4555
 
      pthread_mutex_lock(&LOCK_open);
4556
 
      wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4557
 
      pthread_mutex_unlock(&LOCK_open);
 
4531
      VOID(pthread_mutex_lock(&LOCK_open));
 
4532
      wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
 
4533
      VOID(pthread_mutex_unlock(&LOCK_open));
4558
4534
      error= table->file->ha_enable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4559
4535
      /* COND_refresh will be signaled in close_thread_tables() */
4560
4536
      break;
4561
4537
    case DISABLE:
4562
 
      pthread_mutex_lock(&LOCK_open);
4563
 
      wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4564
 
      pthread_mutex_unlock(&LOCK_open);
 
4538
      VOID(pthread_mutex_lock(&LOCK_open));
 
4539
      wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
 
4540
      VOID(pthread_mutex_unlock(&LOCK_open));
4565
4541
      error=table->file->ha_disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
4566
4542
      /* COND_refresh will be signaled in close_thread_tables() */
4567
4543
      break;
4573
4549
    if (error == HA_ERR_WRONG_COMMAND)
4574
4550
    {
4575
4551
      error= 0;
4576
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
4552
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
4577
4553
                          ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
4578
4554
                          table->alias);
4579
4555
    }
4580
4556
 
4581
 
    pthread_mutex_lock(&LOCK_open);
 
4557
    VOID(pthread_mutex_lock(&LOCK_open));
4582
4558
    /*
4583
4559
      Unlike to the above case close_cached_table() below will remove ALL
4584
4560
      instances of Table from table cache (it will also remove table lock
4590
4566
 
4591
4567
    if (!error && (new_name != table_name || new_db != db))
4592
4568
    {
4593
 
      session->set_proc_info("rename");
 
4569
      thd_proc_info(thd, "rename");
4594
4570
      /*
4595
4571
        Then do a 'simple' rename of the table. First we need to close all
4596
4572
        instances of 'source' table.
4597
4573
      */
4598
 
      close_cached_table(session, table);
 
4574
      close_cached_table(thd, table);
4599
4575
      /*
4600
4576
        Then, we want check once again that target table does not exist.
4601
4577
        Actually the order of these two steps does not matter since
4616
4592
          error= -1;
4617
4593
        else if (0)
4618
4594
      {
4619
 
          mysql_rename_table(old_db_type, new_db, new_alias, db,
4620
 
                             table_name, 0);
 
4595
          VOID(mysql_rename_table(old_db_type, new_db, new_alias, db,
 
4596
                                  table_name, 0));
4621
4597
          error= -1;
4622
4598
      }
4623
4599
    }
4626
4602
    if (error == HA_ERR_WRONG_COMMAND)
4627
4603
  {
4628
4604
      error= 0;
4629
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
4605
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
4630
4606
                          ER_ILLEGAL_HA, ER(ER_ILLEGAL_HA),
4631
4607
                          table->alias);
4632
4608
  }
4633
4609
 
4634
4610
    if (!error)
4635
4611
    {
4636
 
      write_bin_log(session, true, session->query, session->query_length);
4637
 
      my_ok(session);
 
4612
      write_bin_log(thd, true, thd->query, thd->query_length);
 
4613
      my_ok(thd);
4638
4614
  }
4639
4615
    else if (error > 0)
4640
4616
  {
4642
4618
      error= -1;
4643
4619
    }
4644
4620
    if (name_lock)
4645
 
      unlink_open_table(session, name_lock, false);
4646
 
    pthread_mutex_unlock(&LOCK_open);
 
4621
      unlink_open_table(thd, name_lock, false);
 
4622
    VOID(pthread_mutex_unlock(&LOCK_open));
4647
4623
    table_list->table= NULL;                    // For query cache
4648
4624
    return(error);
4649
4625
  }
4663
4639
  */
4664
4640
  new_db_type= create_info->db_type;
4665
4641
 
4666
 
  if (mysql_prepare_alter_table(session, table, create_info, alter_info))
 
4642
  if (mysql_prepare_alter_table(thd, table, create_info, alter_info))
4667
4643
      goto err;
4668
4644
 
4669
 
  set_table_default_charset(session, create_info, db);
4670
 
 
4671
 
 
4672
 
  if (session->variables.old_alter_table
 
4645
  set_table_default_charset(thd, create_info, db);
 
4646
 
 
4647
 
 
4648
  if (thd->variables.old_alter_table
4673
4649
      || (table->s->db_type() != create_info->db_type)
4674
4650
     )
4675
4651
  {
4676
4652
    if (alter_info->build_method == HA_BUILD_ONLINE)
4677
4653
    {
4678
 
      my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
 
4654
      my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
4679
4655
      goto err;
4680
4656
    }
4681
4657
    alter_info->build_method= HA_BUILD_OFFLINE;
4686
4662
    Table *altered_table= 0;
4687
4663
    HA_ALTER_INFO ha_alter_info;
4688
4664
    HA_ALTER_FLAGS ha_alter_flags;
4689
 
    uint32_t table_changes= IS_EQUAL_YES;
 
4665
    uint table_changes= IS_EQUAL_YES;
4690
4666
    bool need_copy_table= true;
4691
4667
    /* Check how much the tables differ. */
4692
 
    if (compare_tables(session, table, alter_info,
 
4668
    if (compare_tables(thd, table, alter_info,
4693
4669
                       create_info, order_num,
4694
4670
                       &ha_alter_flags,
4695
4671
                       &ha_alter_info,
4712
4688
    if (new_name == table_name && new_db == db &&
4713
4689
        ha_alter_flags.is_set())
4714
4690
    {
4715
 
      Alter_info tmp_alter_info(*alter_info, session->mem_root);
 
4691
      Alter_info tmp_alter_info(*alter_info, thd->mem_root);
4716
4692
 
4717
4693
      /*
4718
4694
        If no table rename,
4719
4695
        check if table can be altered on-line
4720
4696
      */
4721
 
      if (!(altered_table= create_altered_table(session,
 
4697
      if (!(altered_table= create_altered_table(thd,
4722
4698
                                                table,
4723
4699
                                                new_db,
4724
4700
                                                create_info,
4746
4722
      case HA_ALTER_NOT_SUPPORTED:
4747
4723
        if (alter_info->build_method == HA_BUILD_ONLINE)
4748
4724
        {
4749
 
          my_error(ER_NOT_SUPPORTED_YET, MYF(0), session->query);
4750
 
          close_temporary_table(session, altered_table, 1, 1);
 
4725
          my_error(ER_NOT_SUPPORTED_YET, MYF(0), thd->query);
 
4726
          close_temporary_table(thd, altered_table, 1, 1);
4751
4727
          goto err;
4752
4728
        }
4753
4729
        need_copy_table= true;
4754
4730
        break;
4755
4731
      case HA_ALTER_ERROR:
4756
4732
      default:
4757
 
        close_temporary_table(session, altered_table, 1, 1);
 
4733
        close_temporary_table(thd, altered_table, 1, 1);
4758
4734
        goto err;
4759
4735
      }
4760
4736
 
4765
4741
 
4766
4742
    if (!need_copy_table)
4767
4743
    {
4768
 
      error= mysql_fast_or_online_alter_table(session,
 
4744
      error= mysql_fast_or_online_alter_table(thd,
4769
4745
                                              table,
4770
4746
                                              altered_table,
4771
4747
                                              create_info,
4772
4748
                                              &ha_alter_info,
4773
4749
                                              &ha_alter_flags,
4774
4750
                                              alter_info->keys_onoff);
4775
 
      if (session->lock)
 
4751
      if (thd->lock)
4776
4752
      {
4777
 
        mysql_unlock_tables(session, session->lock);
4778
 
        session->lock=0;
 
4753
        mysql_unlock_tables(thd, thd->lock);
 
4754
        thd->lock=0;
4779
4755
      }
4780
 
      close_temporary_table(session, altered_table, 1, 1);
 
4756
      close_temporary_table(thd, altered_table, 1, 1);
4781
4757
 
4782
4758
      if (error)
4783
4759
      {
4796
4772
    }
4797
4773
 
4798
4774
    if (altered_table)
4799
 
      close_temporary_table(session, altered_table, 1, 1);
 
4775
      close_temporary_table(thd, altered_table, 1, 1);
4800
4776
  }
4801
4777
 
4802
 
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%"PRIx64, tmp_file_prefix,
4803
 
           current_pid, session->thread_id);
 
4778
  snprintf(tmp_name, sizeof(tmp_name), "%s-%lx_%lx", tmp_file_prefix,
 
4779
           current_pid, thd->thread_id);
4804
4780
  /* Safety fix for innodb */
4805
4781
  if (lower_case_table_names)
4806
4782
    my_casedn_str(files_charset_info, tmp_name);
4807
4783
 
4808
4784
 
4809
4785
  /* Create a temporary table with the new format */
4810
 
  if ((error= create_temporary_table(session, table, new_db, tmp_name, 
 
4786
  if ((error= create_temporary_table(thd, table, new_db, tmp_name, 
4811
4787
                                     create_info, alter_info, 
4812
4788
                                     !strcmp(db, new_db))))
4813
4789
  {
4821
4797
    memset(&tbl, 0, sizeof(tbl));
4822
4798
    tbl.db= new_db;
4823
4799
    tbl.table_name= tbl.alias= tmp_name;
4824
 
    /* Table is in session->temporary_tables */
4825
 
    new_table= open_table(session, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
 
4800
    /* Table is in thd->temporary_tables */
 
4801
    new_table= open_table(thd, &tbl, (bool*) 0, DRIZZLE_LOCK_IGNORE_FLUSH);
4826
4802
  }
4827
4803
  else
4828
4804
  {
4831
4807
    build_table_filename(path, sizeof(path), new_db, tmp_name, "",
4832
4808
                         FN_IS_TMP);
4833
4809
    /* Open our intermediate table */
4834
 
    new_table=open_temporary_table(session, path, new_db, tmp_name, 0, OTM_OPEN);
 
4810
    new_table=open_temporary_table(thd, path, new_db, tmp_name, 0, OTM_OPEN);
4835
4811
  }
4836
4812
  if (!new_table)
4837
4813
    goto err1;
4838
4814
 
4839
4815
  /* Copy the data if necessary. */
4840
 
  session->count_cuted_fields= CHECK_FIELD_WARN;        // calc cuted fields
4841
 
  session->cuted_fields=0L;
4842
 
  session->set_proc_info("copy to tmp table");
 
4816
  thd->count_cuted_fields= CHECK_FIELD_WARN;    // calc cuted fields
 
4817
  thd->cuted_fields=0L;
 
4818
  thd_proc_info(thd, "copy to tmp table");
4843
4819
  copied=deleted=0;
4844
4820
  /*
4845
4821
    We do not copy data for MERGE tables. Only the children have data.
4858
4834
  }
4859
4835
  else
4860
4836
  {
4861
 
    pthread_mutex_lock(&LOCK_open);
4862
 
    wait_while_table_is_used(session, table, HA_EXTRA_FORCE_REOPEN);
4863
 
    pthread_mutex_unlock(&LOCK_open);
 
4837
    VOID(pthread_mutex_lock(&LOCK_open));
 
4838
    wait_while_table_is_used(thd, table, HA_EXTRA_FORCE_REOPEN);
 
4839
    VOID(pthread_mutex_unlock(&LOCK_open));
4864
4840
    alter_table_manage_keys(table, table->file->indexes_are_disabled(),
4865
4841
                            alter_info->keys_onoff);
4866
 
    error= ha_autocommit_or_rollback(session, 0);
4867
 
    if (end_active_trans(session))
 
4842
    error= ha_autocommit_or_rollback(thd, 0);
 
4843
    if (end_active_trans(thd))
4868
4844
      error= 1;
4869
4845
  }
4870
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
4846
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
4871
4847
 
4872
4848
  if (table->s->tmp_table != NO_TMP_TABLE)
4873
4849
  {
4875
4851
    if (error)
4876
4852
      goto err1;
4877
4853
    /* Close lock if this is a transactional table */
4878
 
    if (session->lock)
 
4854
    if (thd->lock)
4879
4855
    {
4880
 
      mysql_unlock_tables(session, session->lock);
4881
 
      session->lock=0;
 
4856
      mysql_unlock_tables(thd, thd->lock);
 
4857
      thd->lock=0;
4882
4858
    }
4883
4859
    /* Remove link to old table and rename the new one */
4884
 
    close_temporary_table(session, table, 1, 1);
 
4860
    close_temporary_table(thd, table, 1, 1);
4885
4861
    /* Should pass the 'new_name' as we store table name in the cache */
4886
 
    if (rename_temporary_table(session, new_table, new_db, new_name))
 
4862
    if (rename_temporary_table(thd, new_table, new_db, new_name))
4887
4863
      goto err1;
4888
4864
    /* We don't replicate alter table statement on temporary tables */
4889
 
    if (!session->current_stmt_binlog_row_based)
4890
 
      write_bin_log(session, true, session->query, session->query_length);
 
4865
    if (!thd->current_stmt_binlog_row_based)
 
4866
      write_bin_log(thd, true, thd->query, thd->query_length);
4891
4867
    goto end_temporary;
4892
4868
  }
4893
4869
 
4898
4874
      Note that MERGE tables do not have their children attached here.
4899
4875
    */
4900
4876
    intern_close_table(new_table);
4901
 
    free(new_table);
 
4877
    my_free(new_table,MYF(0));
4902
4878
  }
4903
 
  pthread_mutex_lock(&LOCK_open);
 
4879
  VOID(pthread_mutex_lock(&LOCK_open));
4904
4880
  if (error)
4905
4881
  {
4906
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
4907
 
    pthread_mutex_unlock(&LOCK_open);
 
4882
    VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
 
4883
    VOID(pthread_mutex_unlock(&LOCK_open));
4908
4884
    goto err;
4909
4885
  }
4910
4886
 
4924
4900
       call to remove name-locks from table cache and list of open table.
4925
4901
  */
4926
4902
 
4927
 
  session->set_proc_info("rename result table");
4928
 
  snprintf(old_name, sizeof(old_name), "%s2-%lx-%"PRIx64, tmp_file_prefix,
4929
 
           current_pid, session->thread_id);
 
4903
  thd_proc_info(thd, "rename result table");
 
4904
  snprintf(old_name, sizeof(old_name), "%s2-%lx-%lx", tmp_file_prefix,
 
4905
           current_pid, thd->thread_id);
4930
4906
  if (lower_case_table_names)
4931
4907
    my_casedn_str(files_charset_info, old_name);
4932
4908
 
4933
 
  wait_while_table_is_used(session, table, HA_EXTRA_PREPARE_FOR_RENAME);
4934
 
  close_data_files_and_morph_locks(session, db, table_name);
 
4909
  wait_while_table_is_used(thd, table, HA_EXTRA_PREPARE_FOR_RENAME);
 
4910
  close_data_files_and_morph_locks(thd, db, table_name);
4935
4911
 
4936
4912
  error=0;
4937
4913
  save_old_db_type= old_db_type;
4953
4929
                         FN_TO_IS_TMP))
4954
4930
  {
4955
4931
    error=1;
4956
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
 
4932
    VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
4957
4933
  }
4958
4934
  else if (mysql_rename_table(new_db_type, new_db, tmp_name, new_db,
4959
4935
                              new_alias, FN_FROM_IS_TMP) || ((new_name != table_name || new_db != db) && 0))
4960
4936
  {
4961
4937
    /* Try to get everything back. */
4962
4938
    error=1;
4963
 
    quick_rm_table(new_db_type,new_db,new_alias, 0);
4964
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
4965
 
    mysql_rename_table(old_db_type, db, old_name, db, alias,
4966
 
                       FN_FROM_IS_TMP);
 
4939
    VOID(quick_rm_table(new_db_type,new_db,new_alias, 0));
 
4940
    VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
 
4941
    VOID(mysql_rename_table(old_db_type, db, old_name, db, alias,
 
4942
                            FN_FROM_IS_TMP));
4967
4943
  }
4968
4944
 
4969
4945
  if (error)
4972
4948
    goto err_with_placeholders;
4973
4949
  }
4974
4950
 
4975
 
  quick_rm_table(old_db_type, db, old_name, FN_IS_TMP);
 
4951
  VOID(quick_rm_table(old_db_type, db, old_name, FN_IS_TMP));
4976
4952
 
4977
4953
end_online:
4978
 
  if (session->locked_tables && new_name == table_name && new_db == db)
 
4954
  if (thd->locked_tables && new_name == table_name && new_db == db)
4979
4955
  {
4980
 
    session->in_lock_tables= 1;
4981
 
    error= reopen_tables(session, 1, 1);
4982
 
    session->in_lock_tables= 0;
 
4956
    thd->in_lock_tables= 1;
 
4957
    error= reopen_tables(thd, 1, 1);
 
4958
    thd->in_lock_tables= 0;
4983
4959
    if (error)
4984
4960
      goto err_with_placeholders;
4985
4961
  }
4986
 
  pthread_mutex_unlock(&LOCK_open);
4987
 
 
4988
 
  session->set_proc_info("end");
 
4962
  VOID(pthread_mutex_unlock(&LOCK_open));
 
4963
 
 
4964
  thd_proc_info(thd, "end");
 
4965
 
 
4966
  ha_binlog_log_query(thd, create_info->db_type, LOGCOM_ALTER_TABLE,
 
4967
                      thd->query, thd->query_length,
 
4968
                      db, table_name);
4989
4969
 
4990
4970
  assert(!(mysql_bin_log.is_open() &&
4991
 
                session->current_stmt_binlog_row_based &&
 
4971
                thd->current_stmt_binlog_row_based &&
4992
4972
                (create_info->options & HA_LEX_CREATE_TMP_TABLE)));
4993
 
  write_bin_log(session, true, session->query, session->query_length);
 
4973
  write_bin_log(thd, true, thd->query, thd->query_length);
4994
4974
 
4995
4975
  if (ha_check_storage_engine_flag(old_db_type, HTON_FLUSH_AFTER_RENAME))
4996
4976
  {
5002
4982
    char path[FN_REFLEN];
5003
4983
    Table *t_table;
5004
4984
    build_table_filename(path, sizeof(path), new_db, table_name, "", 0);
5005
 
    t_table= open_temporary_table(session, path, new_db, tmp_name, false, OTM_OPEN);
 
4985
    t_table= open_temporary_table(thd, path, new_db, tmp_name, false, OTM_OPEN);
5006
4986
    if (t_table)
5007
4987
    {
5008
4988
      intern_close_table(t_table);
5009
 
      free(t_table);
 
4989
      my_free(t_table, MYF(0));
5010
4990
    }
5011
4991
    else
5012
4992
      sql_print_warning(_("Could not open table %s.%s after rename\n"),
5015
4995
  }
5016
4996
  table_list->table=0;                          // For query cache
5017
4997
 
5018
 
  if (session->locked_tables && (new_name != table_name || new_db != db))
 
4998
  if (thd->locked_tables && (new_name != table_name || new_db != db))
5019
4999
  {
5020
5000
    /*
5021
5001
      If are we under LOCK TABLES and did ALTER Table with RENAME we need
5024
5004
      LOCK TABLES we can rely on close_thread_tables() doing this job.
5025
5005
    */
5026
5006
    pthread_mutex_lock(&LOCK_open);
5027
 
    unlink_open_table(session, table, false);
5028
 
    unlink_open_table(session, name_lock, false);
 
5007
    unlink_open_table(thd, table, false);
 
5008
    unlink_open_table(thd, name_lock, false);
5029
5009
    pthread_mutex_unlock(&LOCK_open);
5030
5010
  }
5031
5011
 
5032
5012
end_temporary:
5033
5013
  snprintf(tmp_name, sizeof(tmp_name), ER(ER_INSERT_INFO),
5034
5014
           (ulong) (copied + deleted), (ulong) deleted,
5035
 
           (ulong) session->cuted_fields);
5036
 
  my_ok(session, copied + deleted, 0L, tmp_name);
5037
 
  session->some_tables_deleted=0;
 
5015
           (ulong) thd->cuted_fields);
 
5016
  my_ok(thd, copied + deleted, 0L, tmp_name);
 
5017
  thd->some_tables_deleted=0;
5038
5018
  return(false);
5039
5019
 
5040
5020
err1:
5041
5021
  if (new_table)
5042
5022
  {
5043
5023
    /* close_temporary_table() frees the new_table pointer. */
5044
 
    close_temporary_table(session, new_table, 1, 1);
 
5024
    close_temporary_table(thd, new_table, 1, 1);
5045
5025
  }
5046
5026
  else
5047
 
    quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP);
 
5027
    VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
5048
5028
 
5049
5029
err:
5050
5030
  /*
5053
5033
    the table to be altered isn't empty.
5054
5034
    Report error here.
5055
5035
  */
5056
 
  if (alter_info->error_if_not_empty && session->row_count)
 
5036
  if (alter_info->error_if_not_empty && thd->row_count)
5057
5037
  {
5058
5038
    const char *f_val= 0;
5059
5039
    enum enum_drizzle_timestamp_type t_type= DRIZZLE_TIMESTAMP_DATE;
5071
5051
        /* Shouldn't get here. */
5072
5052
        assert(0);
5073
5053
    }
5074
 
    bool save_abort_on_warning= session->abort_on_warning;
5075
 
    session->abort_on_warning= true;
5076
 
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
5054
    bool save_abort_on_warning= thd->abort_on_warning;
 
5055
    thd->abort_on_warning= true;
 
5056
    make_truncated_value_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
5077
5057
                                 f_val, strlength(f_val), t_type,
5078
5058
                                 alter_info->datetime_field->field_name);
5079
 
    session->abort_on_warning= save_abort_on_warning;
 
5059
    thd->abort_on_warning= save_abort_on_warning;
5080
5060
  }
5081
5061
  if (name_lock)
5082
5062
  {
5083
5063
    pthread_mutex_lock(&LOCK_open);
5084
 
    unlink_open_table(session, name_lock, false);
 
5064
    unlink_open_table(thd, name_lock, false);
5085
5065
    pthread_mutex_unlock(&LOCK_open);
5086
5066
  }
5087
5067
  return(true);
5092
5072
    being altered. To be safe under LOCK TABLES we should remove placeholders
5093
5073
    from list of open tables list and table cache.
5094
5074
  */
5095
 
  unlink_open_table(session, table, false);
 
5075
  unlink_open_table(thd, table, false);
5096
5076
  if (name_lock)
5097
 
    unlink_open_table(session, name_lock, false);
5098
 
  pthread_mutex_unlock(&LOCK_open);
 
5077
    unlink_open_table(thd, name_lock, false);
 
5078
  VOID(pthread_mutex_unlock(&LOCK_open));
5099
5079
  return(true);
5100
5080
}
5101
5081
/* mysql_alter_table */
5104
5084
copy_data_between_tables(Table *from,Table *to,
5105
5085
                         List<Create_field> &create,
5106
5086
                         bool ignore,
5107
 
                         uint32_t order_num, order_st *order,
 
5087
                         uint order_num, order_st *order,
5108
5088
                         ha_rows *copied,
5109
5089
                         ha_rows *deleted,
5110
5090
                         enum enum_enable_or_disable keys_onoff,
5113
5093
  int error;
5114
5094
  Copy_field *copy,*copy_end;
5115
5095
  ulong found_count,delete_count;
5116
 
  Session *session= current_session;
5117
 
  uint32_t length= 0;
 
5096
  THD *thd= current_thd;
 
5097
  uint length= 0;
5118
5098
  SORT_FIELD *sortorder;
5119
5099
  READ_RECORD info;
5120
5100
  TableList   tables;
5131
5111
    
5132
5112
    This needs to be done before external_lock
5133
5113
  */
5134
 
  error= ha_enable_transaction(session, false);
 
5114
  error= ha_enable_transaction(thd, false);
5135
5115
  if (error)
5136
5116
    return(-1);
5137
5117
  
5138
5118
  if (!(copy= new Copy_field[to->s->fields]))
5139
5119
    return(-1);                         /* purecov: inspected */
5140
5120
 
5141
 
  if (to->file->ha_external_lock(session, F_WRLCK))
 
5121
  if (to->file->ha_external_lock(thd, F_WRLCK))
5142
5122
    return(-1);
5143
5123
 
5144
5124
  /* We need external lock before we can disable/enable keys */
5145
5125
  alter_table_manage_keys(to, from->file->indexes_are_disabled(), keys_onoff);
5146
5126
 
5147
5127
  /* We can abort alter table for any table type */
5148
 
  session->abort_on_warning= !ignore;
 
5128
  thd->abort_on_warning= !ignore;
5149
5129
 
5150
5130
  from->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5151
5131
  to->file->ha_start_bulk_insert(from->file->stats.records);
5152
5132
 
5153
 
  save_sql_mode= session->variables.sql_mode;
 
5133
  save_sql_mode= thd->variables.sql_mode;
5154
5134
 
5155
5135
  List_iterator<Create_field> it(create);
5156
5136
  Create_field *def;
5179
5159
               _("order_st BY ignored because there is a user-defined clustered "
5180
5160
                 "index in the table '%-.192s'"),
5181
5161
               from->s->table_name.str);
5182
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
5162
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
5183
5163
                   warn_buff);
5184
5164
    }
5185
5165
    else
5192
5172
      tables.db= from->s->db.str;
5193
5173
      error= 1;
5194
5174
 
5195
 
      if (session->lex->select_lex.setup_ref_array(session, order_num) ||
5196
 
          setup_order(session, session->lex->select_lex.ref_pointer_array,
 
5175
      if (thd->lex->select_lex.setup_ref_array(thd, order_num) ||
 
5176
          setup_order(thd, thd->lex->select_lex.ref_pointer_array,
5197
5177
                      &tables, fields, all_fields, order) ||
5198
5178
          !(sortorder= make_unireg_sortorder(order, &length, NULL)) ||
5199
 
          (from->sort.found_records= filesort(session, from, sortorder, length,
 
5179
          (from->sort.found_records= filesort(thd, from, sortorder, length,
5200
5180
                                              (SQL_SELECT *) 0, HA_POS_ERROR,
5201
5181
                                              1, &examined_rows)) ==
5202
5182
          HA_POS_ERROR)
5206
5186
 
5207
5187
  /* Tell handler that we have values for all columns in the to table */
5208
5188
  to->use_all_columns();
5209
 
  init_read_record(&info, session, from, (SQL_SELECT *) 0, 1,1);
 
5189
  init_read_record(&info, thd, from, (SQL_SELECT *) 0, 1,1);
5210
5190
  if (ignore)
5211
5191
    to->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
5212
 
  session->row_count= 0;
 
5192
  thd->row_count= 0;
5213
5193
  restore_record(to, s->default_values);        // Create empty record
5214
5194
  while (!(error=info.read_record(&info)))
5215
5195
  {
5216
 
    if (session->killed)
 
5196
    if (thd->killed)
5217
5197
    {
5218
 
      session->send_kill_message();
 
5198
      thd->send_kill_message();
5219
5199
      error= 1;
5220
5200
      break;
5221
5201
    }
5222
 
    session->row_count++;
 
5202
    thd->row_count++;
5223
5203
    /* Return error if source table isn't empty. */
5224
5204
    if (error_if_not_empty)
5225
5205
    {
5239
5219
      copy_ptr->do_copy(copy_ptr);
5240
5220
    }
5241
5221
    prev_insert_id= to->file->next_insert_id;
5242
 
    update_virtual_fields_marked_for_write(to, false);
5243
5222
    error=to->file->ha_write_row(to->record[0]);
5244
5223
    to->auto_increment_field_not_null= false;
5245
5224
    if (error)
5249
5228
      {
5250
5229
         if (!to->file->is_fatal_error(error, HA_CHECK_DUP))
5251
5230
         {
5252
 
           uint32_t key_nr= to->file->get_dup_key(error);
 
5231
           uint key_nr= to->file->get_dup_key(error);
5253
5232
           if ((int) key_nr >= 0)
5254
5233
           {
5255
5234
             const char *err_msg= ER(ER_DUP_ENTRY_WITH_KEY_NAME);
5282
5261
  }
5283
5262
  to->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
5284
5263
 
5285
 
  if (ha_enable_transaction(session, true))
 
5264
  if (ha_enable_transaction(thd, true))
5286
5265
  {
5287
5266
    error= 1;
5288
5267
    goto err;
5292
5271
    Ensure that the new table is saved properly to disk so that we
5293
5272
    can do a rename
5294
5273
  */
5295
 
  if (ha_autocommit_or_rollback(session, 0))
 
5274
  if (ha_autocommit_or_rollback(thd, 0))
5296
5275
    error=1;
5297
 
  if (end_active_trans(session))
 
5276
  if (end_active_trans(thd))
5298
5277
    error=1;
5299
5278
 
5300
5279
 err:
5301
 
  session->variables.sql_mode= save_sql_mode;
5302
 
  session->abort_on_warning= 0;
 
5280
  thd->variables.sql_mode= save_sql_mode;
 
5281
  thd->abort_on_warning= 0;
5303
5282
  free_io_cache(from);
5304
5283
  *copied= found_count;
5305
5284
  *deleted=delete_count;
5306
5285
  to->file->ha_release_auto_increment();
5307
 
  if (to->file->ha_external_lock(session,F_UNLCK))
 
5286
  if (to->file->ha_external_lock(thd,F_UNLCK))
5308
5287
    error=1;
5309
5288
  return(error > 0 ? -1 : 0);
5310
5289
}
5315
5294
 
5316
5295
  SYNOPSIS
5317
5296
    mysql_recreate_table()
5318
 
    session                     Thread handler
 
5297
    thd                 Thread handler
5319
5298
    tables              Tables to recreate
5320
5299
 
5321
5300
 RETURN
5322
5301
    Like mysql_alter_table().
5323
5302
*/
5324
 
bool mysql_recreate_table(Session *session, TableList *table_list)
 
5303
bool mysql_recreate_table(THD *thd, TableList *table_list)
5325
5304
{
5326
5305
  HA_CREATE_INFO create_info;
5327
5306
  Alter_info alter_info;
5338
5317
  create_info.default_table_charset=default_charset_info;
5339
5318
  /* Force alter table to recreate table */
5340
5319
  alter_info.flags= (ALTER_CHANGE_COLUMN | ALTER_RECREATE);
5341
 
  return(mysql_alter_table(session, NULL, NULL, &create_info,
 
5320
  return(mysql_alter_table(thd, NullS, NullS, &create_info,
5342
5321
                                table_list, &alter_info, 0,
5343
5322
                                (order_st *) 0, 0));
5344
5323
}
5345
5324
 
5346
5325
 
5347
 
bool mysql_checksum_table(Session *session, TableList *tables,
 
5326
bool mysql_checksum_table(THD *thd, TableList *tables,
5348
5327
                          HA_CHECK_OPT *check_opt)
5349
5328
{
5350
5329
  TableList *table;
5351
5330
  List<Item> field_list;
5352
5331
  Item *item;
5353
 
  Protocol *protocol= session->protocol;
 
5332
  Protocol *protocol= thd->protocol;
5354
5333
 
5355
5334
  field_list.push_back(item = new Item_empty_string("Table", NAME_LEN*2));
5356
5335
  item->maybe_null= 1;
5367
5346
    char table_name[NAME_LEN*2+2];
5368
5347
    Table *t;
5369
5348
 
5370
 
    strxmov(table_name, table->db ,".", table->table_name, NULL);
 
5349
    strxmov(table_name, table->db ,".", table->table_name, NullS);
5371
5350
 
5372
 
    t= table->table= open_n_lock_single_table(session, table, TL_READ);
5373
 
    session->clear_error();                     // these errors shouldn't get client
 
5351
    t= table->table= open_n_lock_single_table(thd, table, TL_READ);
 
5352
    thd->clear_error();                 // these errors shouldn't get client
5374
5353
 
5375
5354
    protocol->prepare_for_resend();
5376
5355
    protocol->store(table_name, system_charset_info);
5379
5358
    {
5380
5359
      /* Table didn't exist */
5381
5360
      protocol->store_null();
5382
 
      session->clear_error();
 
5361
      thd->clear_error();
5383
5362
    }
5384
5363
    else
5385
5364
    {
5393
5372
      {
5394
5373
        /* calculating table's checksum */
5395
5374
        ha_checksum crc= 0;
5396
 
        unsigned char null_mask=256 -  (1 << t->s->last_null_bit_pos);
 
5375
        uchar null_mask=256 -  (1 << t->s->last_null_bit_pos);
5397
5376
 
5398
5377
        t->use_all_columns();
5399
5378
 
5421
5400
              row_crc= my_checksum(row_crc, t->record[0], t->s->null_bytes);
5422
5401
            }
5423
5402
 
5424
 
            for (uint32_t i= 0; i < t->s->fields; i++ )
 
5403
            for (uint i= 0; i < t->s->fields; i++ )
5425
5404
            {
5426
5405
              Field *f= t->field[i];
5427
5406
              if ((f->type() == DRIZZLE_TYPE_BLOB) ||
5429
5408
              {
5430
5409
                String tmp;
5431
5410
                f->val_str(&tmp);
5432
 
                row_crc= my_checksum(row_crc, (unsigned char*) tmp.ptr(), tmp.length());
 
5411
                row_crc= my_checksum(row_crc, (uchar*) tmp.ptr(), tmp.length());
5433
5412
              }
5434
5413
              else
5435
5414
                row_crc= my_checksum(row_crc, f->ptr,
5442
5421
          t->file->ha_rnd_end();
5443
5422
        }
5444
5423
      }
5445
 
      session->clear_error();
5446
 
      close_thread_tables(session);
 
5424
      thd->clear_error();
 
5425
      close_thread_tables(thd);
5447
5426
      table->table=0;                           // For query cache
5448
5427
    }
5449
5428
    if (protocol->write())
5450
5429
      goto err;
5451
5430
  }
5452
5431
 
5453
 
  my_eof(session);
 
5432
  my_eof(thd);
5454
5433
  return(false);
5455
5434
 
5456
5435
 err:
5457
 
  close_thread_tables(session);                 // Shouldn't be needed
 
5436
  close_thread_tables(thd);                     // Shouldn't be needed
5458
5437
  if (table)
5459
5438
    table->table=0;
5460
5439
  return(true);
5461
5440
}
5462
5441
 
5463
 
static bool check_engine(Session *session, const char *table_name,
 
5442
static bool check_engine(THD *thd, const char *table_name,
5464
5443
                         HA_CREATE_INFO *create_info)
5465
5444
{
5466
5445
  handlerton **new_engine= &create_info->db_type;
5467
5446
  handlerton *req_engine= *new_engine;
5468
5447
  bool no_substitution= 1;
5469
 
  if (!(*new_engine= ha_checktype(session, ha_legacy_type(req_engine),
 
5448
  if (!(*new_engine= ha_checktype(thd, ha_legacy_type(req_engine),
5470
5449
                                  no_substitution, 1)))
5471
5450
    return true;
5472
5451
 
5473
5452
  if (req_engine && req_engine != *new_engine)
5474
5453
  {
5475
 
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
5454
    push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
5476
5455
                       ER_WARN_USING_OTHER_HANDLER,
5477
5456
                       ER(ER_WARN_USING_OTHER_HANDLER),
5478
5457
                       ha_resolve_storage_engine_name(*new_engine),