~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_db.cc

update to latest from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
static TYPELIB deletable_extentions=
37
37
{array_elements(del_exts)-1,"del_exts", del_exts, NULL};
38
38
 
39
 
static long mysql_rm_known_files(THD *thd, MY_DIR *dirp,
 
39
static long mysql_rm_known_files(Session *session, MY_DIR *dirp,
40
40
                                 const char *db, const char *path, uint32_t level, 
41
41
                                 TableList **dropped_tables);
42
42
         
43
43
static bool rm_dir_w_symlink(const char *org_path, bool send_error);
44
 
static void mysql_change_db_impl(THD *thd,
 
44
static void mysql_change_db_impl(Session *session,
45
45
                                 LEX_STRING *new_db_name,
46
46
                                 const CHARSET_INFO * const new_db_charset);
47
47
 
133
133
  Helper function to write a query to binlog used by mysql_rm_db()
134
134
*/
135
135
 
136
 
static inline void write_to_binlog(THD *thd, char *query, uint32_t q_len,
 
136
static inline void write_to_binlog(Session *session, char *query, uint32_t q_len,
137
137
                                   char *db, uint32_t db_len)
138
138
{
139
 
  Query_log_event qinfo(thd, query, q_len, 0, 0);
 
139
  Query_log_event qinfo(session, query, q_len, 0, 0);
140
140
  qinfo.error_code= 0;
141
141
  qinfo.db= db;
142
142
  qinfo.db_len= db_len;
334
334
  1     Could not create file or write to it.  Error sent through my_error()
335
335
*/
336
336
 
337
 
static bool write_db_opt(THD *thd, const char *path, const char *name, HA_CREATE_INFO *create)
 
337
static bool write_db_opt(Session *session, const char *path, const char *name, HA_CREATE_INFO *create)
338
338
{
339
339
  bool error= true;
340
340
  drizzle::Schema db;
342
342
  assert(name);
343
343
 
344
344
  if (!create->default_table_charset)
345
 
    create->default_table_charset= thd->variables.collation_server;
 
345
    create->default_table_charset= session->variables.collation_server;
346
346
 
347
347
  if (put_dbopt(path, create))
348
348
    return 1;
374
374
 
375
375
*/
376
376
 
377
 
bool load_db_opt(THD *thd, const char *path, HA_CREATE_INFO *create)
 
377
bool load_db_opt(Session *session, const char *path, HA_CREATE_INFO *create)
378
378
{
379
379
  bool error=1;
380
380
  drizzle::Schema db;
381
381
  string buffer;
382
382
 
383
383
  memset(create, 0, sizeof(*create));
384
 
  create->default_table_charset= thd->variables.collation_server;
 
384
  create->default_table_charset= session->variables.collation_server;
385
385
 
386
386
  /* Check if options for this database are already in the hash */
387
387
  if (!get_dbopt(path, create))
453
453
    true    Failed to retrieve options
454
454
*/
455
455
 
456
 
bool load_db_opt_by_name(THD *thd, const char *db_name,
 
456
bool load_db_opt_by_name(Session *session, const char *db_name,
457
457
                         HA_CREATE_INFO *db_create_info)
458
458
{
459
459
  char db_opt_path[FN_REFLEN];
465
465
  (void) build_table_filename(db_opt_path, sizeof(db_opt_path),
466
466
                              db_name, "", MY_DB_OPT_FILE, 0);
467
467
 
468
 
  return load_db_opt(thd, db_opt_path, db_create_info);
 
468
  return load_db_opt(session, db_opt_path, db_create_info);
469
469
}
470
470
 
471
471
 
472
472
/**
473
473
  Return default database collation.
474
474
 
475
 
  @param thd     Thread context.
 
475
  @param session     Thread context.
476
476
  @param db_name Database name.
477
477
 
478
478
  @return CHARSET_INFO object. The operation always return valid character
479
479
    set, even if the database does not exist.
480
480
*/
481
481
 
482
 
const CHARSET_INFO *get_default_db_collation(THD *thd, const char *db_name)
 
482
const CHARSET_INFO *get_default_db_collation(Session *session, const char *db_name)
483
483
{
484
484
  HA_CREATE_INFO db_info;
485
485
 
486
 
  if (thd->db != NULL && strcmp(db_name, thd->db) == 0)
487
 
    return thd->db_charset;
 
486
  if (session->db != NULL && strcmp(db_name, session->db) == 0)
 
487
    return session->db_charset;
488
488
 
489
 
  load_db_opt_by_name(thd, db_name, &db_info);
 
489
  load_db_opt_by_name(session, db_name, &db_info);
490
490
 
491
491
  /*
492
492
    NOTE: even if load_db_opt_by_name() fails,
505
505
 
506
506
  SYNOPSIS
507
507
  mysql_create_db()
508
 
  thd           Thread handler
 
508
  session               Thread handler
509
509
  db            Name of database to create
510
510
                Function assumes that this is already validated.
511
511
  create_info   Database create options (like character set)
524
524
 
525
525
*/
526
526
 
527
 
int mysql_create_db(THD *thd, char *db, HA_CREATE_INFO *create_info, bool silent)
 
527
int mysql_create_db(Session *session, char *db, HA_CREATE_INFO *create_info, bool silent)
528
528
{
529
529
  char   path[FN_REFLEN+16];
530
530
  char   tmp_query[FN_REFLEN+16];
553
553
    has the global read lock and refuses the operation with
554
554
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
555
555
  */
556
 
  if (wait_if_global_read_lock(thd, 0, 1))
 
556
  if (wait_if_global_read_lock(session, 0, 1))
557
557
  {
558
558
    error= -1;
559
559
    goto exit2;
573
573
      error= -1;
574
574
      goto exit;
575
575
    }
576
 
    push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
576
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
577
577
                        ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), db);
578
578
    if (!silent)
579
 
      my_ok(thd);
 
579
      my_ok(session);
580
580
    error= 0;
581
581
    goto exit;
582
582
  }
597
597
 
598
598
  path[path_len-1]= FN_LIBCHAR;
599
599
  strmake(path+path_len, MY_DB_OPT_FILE, sizeof(path)-path_len-1);
600
 
  if (write_db_opt(thd, path, db, create_info))
 
600
  if (write_db_opt(session, path, db, create_info))
601
601
  {
602
602
    /*
603
603
      Could not create options file.
621
621
    char *query;
622
622
    uint32_t query_length;
623
623
 
624
 
    if (!thd->query)                            // Only in replication
 
624
    if (!session->query)                                // Only in replication
625
625
    {
626
626
      query=         tmp_query;
627
627
      query_length= (uint) (strxmov(tmp_query,"create database `",
629
629
    }
630
630
    else
631
631
    {
632
 
      query=        thd->query;
633
 
      query_length= thd->query_length;
 
632
      query=        session->query;
 
633
      query_length= session->query_length;
634
634
    }
635
635
 
636
636
    if (mysql_bin_log.is_open())
637
637
    {
638
 
      Query_log_event qinfo(thd, query, query_length, 0, 
 
638
      Query_log_event qinfo(session, query, query_length, 0, 
639
639
                            /* suppress_use */ true);
640
640
 
641
641
      /*
661
661
      /* These DDL methods and logging protected with LOCK_mysql_create_db */
662
662
      mysql_bin_log.write(&qinfo);
663
663
    }
664
 
    my_ok(thd, result);
 
664
    my_ok(session, result);
665
665
  }
666
666
 
667
667
exit:
668
668
  pthread_mutex_unlock(&LOCK_mysql_create_db);
669
 
  start_waiting_global_read_lock(thd);
 
669
  start_waiting_global_read_lock(session);
670
670
exit2:
671
671
  return(error);
672
672
}
674
674
 
675
675
/* db-name is already validated when we come here */
676
676
 
677
 
bool mysql_alter_db(THD *thd, const char *db, HA_CREATE_INFO *create_info)
 
677
bool mysql_alter_db(Session *session, const char *db, HA_CREATE_INFO *create_info)
678
678
{
679
679
  char path[FN_REFLEN+16];
680
680
  long result=1;
692
692
    has the global read lock and refuses the operation with
693
693
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
694
694
  */
695
 
  if ((error=wait_if_global_read_lock(thd,0,1)))
 
695
  if ((error=wait_if_global_read_lock(session,0,1)))
696
696
    goto exit2;
697
697
 
698
698
  pthread_mutex_lock(&LOCK_mysql_create_db);
703
703
     "table name to file name" encoding.
704
704
  */
705
705
  build_table_filename(path, sizeof(path), db, "", MY_DB_OPT_FILE, 0);
706
 
  if ((error=write_db_opt(thd, path, db, create_info)))
 
706
  if ((error=write_db_opt(session, path, db, create_info)))
707
707
    goto exit;
708
708
 
709
709
  /* Change options if current database is being altered. */
710
710
 
711
 
  if (thd->db && !strcmp(thd->db,db))
 
711
  if (session->db && !strcmp(session->db,db))
712
712
  {
713
 
    thd->db_charset= create_info->default_table_charset ?
 
713
    session->db_charset= create_info->default_table_charset ?
714
714
                     create_info->default_table_charset :
715
 
                     thd->variables.collation_server;
716
 
    thd->variables.collation_database= thd->db_charset;
 
715
                     session->variables.collation_server;
 
716
    session->variables.collation_database= session->db_charset;
717
717
  }
718
718
 
719
719
  if (mysql_bin_log.is_open())
720
720
  {
721
 
    Query_log_event qinfo(thd, thd->query, thd->query_length, 0,
 
721
    Query_log_event qinfo(session, session->query, session->query_length, 0,
722
722
                          /* suppress_use */ true);
723
723
 
724
724
    /*
729
729
    qinfo.db     = db;
730
730
    qinfo.db_len = strlen(db);
731
731
 
732
 
    thd->clear_error();
 
732
    session->clear_error();
733
733
    /* These DDL methods and logging protected with LOCK_mysql_create_db */
734
734
    mysql_bin_log.write(&qinfo);
735
735
  }
736
 
  my_ok(thd, result);
 
736
  my_ok(session, result);
737
737
 
738
738
exit:
739
739
  pthread_mutex_unlock(&LOCK_mysql_create_db);
740
 
  start_waiting_global_read_lock(thd);
 
740
  start_waiting_global_read_lock(session);
741
741
exit2:
742
742
  return(error);
743
743
}
748
748
 
749
749
  SYNOPSIS
750
750
    mysql_rm_db()
751
 
    thd                 Thread handle
 
751
    session                     Thread handle
752
752
    db                  Database name in the case given by user
753
753
                        It's already validated and set to lower case
754
754
                        (if needed) when we come here
760
760
    ERROR Error
761
761
*/
762
762
 
763
 
bool mysql_rm_db(THD *thd,char *db,bool if_exists, bool silent)
 
763
bool mysql_rm_db(Session *session,char *db,bool if_exists, bool silent)
764
764
{
765
765
  long deleted=0;
766
766
  int error= false;
787
787
    has the global read lock and refuses the operation with
788
788
    ER_CANT_UPDATE_WITH_READLOCK if applicable.
789
789
  */
790
 
  if (wait_if_global_read_lock(thd, 0, 1))
 
790
  if (wait_if_global_read_lock(session, 0, 1))
791
791
  {
792
792
    error= -1;
793
793
    goto exit2;
800
800
    row-based replication. The flag will be reset at the end of the
801
801
    statement.
802
802
  */
803
 
  thd->clear_current_stmt_binlog_row_based();
 
803
  session->clear_current_stmt_binlog_row_based();
804
804
 
805
805
  length= build_table_filename(path, sizeof(path), db, "", "", 0);
806
806
  my_stpcpy(path+length, MY_DB_OPT_FILE);               // Append db option file name
817
817
      goto exit;
818
818
    }
819
819
    else
820
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
820
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
821
821
                          ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS), db);
822
822
  }
823
823
  else
828
828
 
829
829
    
830
830
    error= -1;
831
 
    if ((deleted= mysql_rm_known_files(thd, dirp, db, path, 0,
 
831
    if ((deleted= mysql_rm_known_files(session, dirp, db, path, 0,
832
832
                                       &dropped_tables)) >= 0)
833
833
    {
834
834
      ha_drop_database(path);
839
839
  {
840
840
    const char *query;
841
841
    uint32_t query_length;
842
 
    if (!thd->query)
 
842
    if (!session->query)
843
843
    {
844
844
      /* The client used the old obsolete mysql_drop_db() call */
845
845
      query= path;
848
848
    }
849
849
    else
850
850
    {
851
 
      query =thd->query;
852
 
      query_length= thd->query_length;
 
851
      query =session->query;
 
852
      query_length= session->query_length;
853
853
    }
854
854
    if (mysql_bin_log.is_open())
855
855
    {
856
 
      Query_log_event qinfo(thd, query, query_length, 0, 
 
856
      Query_log_event qinfo(session, query, query_length, 0, 
857
857
                            /* suppress_use */ true);
858
858
      /*
859
859
        Write should use the database being created as the "current
863
863
      qinfo.db     = db;
864
864
      qinfo.db_len = strlen(db);
865
865
 
866
 
      thd->clear_error();
 
866
      session->clear_error();
867
867
      /* These DDL methods and logging protected with LOCK_mysql_create_db */
868
868
      mysql_bin_log.write(&qinfo);
869
869
    }
870
 
    thd->clear_error();
871
 
    thd->server_status|= SERVER_STATUS_DB_DROPPED;
872
 
    my_ok(thd, (uint32_t) deleted);
873
 
    thd->server_status&= ~SERVER_STATUS_DB_DROPPED;
 
870
    session->clear_error();
 
871
    session->server_status|= SERVER_STATUS_DB_DROPPED;
 
872
    my_ok(session, (uint32_t) deleted);
 
873
    session->server_status&= ~SERVER_STATUS_DB_DROPPED;
874
874
  }
875
875
  else if (mysql_bin_log.is_open())
876
876
  {
878
878
    TableList *tbl;
879
879
    uint32_t db_len;
880
880
 
881
 
    if (!(query= (char*) thd->alloc(MAX_DROP_TABLE_Q_LEN)))
 
881
    if (!(query= (char*) session->alloc(MAX_DROP_TABLE_Q_LEN)))
882
882
      goto exit; /* not much else we can do */
883
883
    query_pos= query_data_start= my_stpcpy(query,"drop table ");
884
884
    query_end= query + MAX_DROP_TABLE_Q_LEN;
893
893
      if (query_pos + tbl_name_len + 1 >= query_end)
894
894
      {
895
895
        /* These DDL methods and logging protected with LOCK_mysql_create_db */
896
 
        write_to_binlog(thd, query, query_pos -1 - query, db, db_len);
 
896
        write_to_binlog(session, query, query_pos -1 - query, db, db_len);
897
897
        query_pos= query_data_start;
898
898
      }
899
899
 
906
906
    if (query_pos != query_data_start)
907
907
    {
908
908
      /* These DDL methods and logging protected with LOCK_mysql_create_db */
909
 
      write_to_binlog(thd, query, query_pos -1 - query, db, db_len);
 
909
      write_to_binlog(session, query, query_pos -1 - query, db, db_len);
910
910
    }
911
911
  }
912
912
 
914
914
  /*
915
915
    If this database was the client's selected database, we silently
916
916
    change the client's selected database to nothing (to have an empty
917
 
    SELECT DATABASE() in the future). For this we free() thd->db and set
 
917
    SELECT DATABASE() in the future). For this we free() session->db and set
918
918
    it to 0.
919
919
  */
920
 
  if (thd->db && !strcmp(thd->db, db))
921
 
    mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
 
920
  if (session->db && !strcmp(session->db, db))
 
921
    mysql_change_db_impl(session, NULL, session->variables.collation_server);
922
922
  pthread_mutex_unlock(&LOCK_mysql_create_db);
923
 
  start_waiting_global_read_lock(thd);
 
923
  start_waiting_global_read_lock(session);
924
924
exit2:
925
925
  return(error);
926
926
}
927
927
 
928
928
/*
929
929
  Removes files with known extensions plus.
930
 
  thd MUST be set when calling this function!
 
930
  session MUST be set when calling this function!
931
931
*/
932
932
 
933
 
static long mysql_rm_known_files(THD *thd, MY_DIR *dirp, const char *db,
 
933
static long mysql_rm_known_files(Session *session, MY_DIR *dirp, const char *db,
934
934
                                 const char *org_path, uint32_t level,
935
935
                                 TableList **dropped_tables)
936
936
{
942
942
  tot_list_next= &tot_list;
943
943
 
944
944
  for (uint32_t idx=0 ;
945
 
       idx < (uint) dirp->number_off_files && !thd->killed ;
 
945
       idx < (uint) dirp->number_off_files && !session->killed ;
946
946
       idx++)
947
947
  {
948
948
    FILEINFO *file=dirp->dir_entry+idx;
968
968
      /* Drop the table nicely */
969
969
      *extension= 0;                    // Remove extension
970
970
      TableList *table_list=(TableList*)
971
 
                              thd->calloc(sizeof(*table_list) + 
 
971
                              session->calloc(sizeof(*table_list) + 
972
972
                                          strlen(db) + 1 +
973
973
                                          MYSQL50_TABLE_NAME_PREFIX_LENGTH + 
974
974
                                          strlen(file->name) + 1);
996
996
      }
997
997
    }
998
998
  }
999
 
  if (thd->killed ||
1000
 
      (tot_list && mysql_rm_table_part2(thd, tot_list, 1, 0, 1, 1)))
 
999
  if (session->killed ||
 
1000
      (tot_list && mysql_rm_table_part2(session, tot_list, 1, 0, 1, 1)))
1001
1001
    goto err;
1002
1002
 
1003
1003
  my_dirend(dirp);  
1084
1084
/**
1085
1085
  @brief Internal implementation: switch current database to a valid one.
1086
1086
 
1087
 
  @param thd            Thread context.
 
1087
  @param session            Thread context.
1088
1088
  @param new_db_name    Name of the database to switch to. The function will
1089
1089
                        take ownership of the name (the caller must not free
1090
1090
                        the allocated memory). If the name is NULL, we're
1092
1092
  @param new_db_charset Character set of the new database.
1093
1093
*/
1094
1094
 
1095
 
static void mysql_change_db_impl(THD *thd,
 
1095
static void mysql_change_db_impl(Session *session,
1096
1096
                                 LEX_STRING *new_db_name,
1097
1097
                                 const CHARSET_INFO * const new_db_charset)
1098
1098
{
1099
 
  /* 1. Change current database in THD. */
 
1099
  /* 1. Change current database in Session. */
1100
1100
 
1101
1101
  if (new_db_name == NULL)
1102
1102
  {
1103
1103
    /*
1104
 
      THD::set_db() does all the job -- it frees previous database name and
 
1104
      Session::set_db() does all the job -- it frees previous database name and
1105
1105
      sets the new one.
1106
1106
    */
1107
1107
 
1108
 
    thd->set_db(NULL, 0);
 
1108
    session->set_db(NULL, 0);
1109
1109
  }
1110
1110
  else if (new_db_name == &INFORMATION_SCHEMA_NAME)
1111
1111
  {
1112
1112
    /*
1113
 
      Here we must use THD::set_db(), because we want to copy
 
1113
      Here we must use Session::set_db(), because we want to copy
1114
1114
      INFORMATION_SCHEMA_NAME constant.
1115
1115
    */
1116
1116
 
1117
 
    thd->set_db(INFORMATION_SCHEMA_NAME.str, INFORMATION_SCHEMA_NAME.length);
 
1117
    session->set_db(INFORMATION_SCHEMA_NAME.str, INFORMATION_SCHEMA_NAME.length);
1118
1118
  }
1119
1119
  else
1120
1120
  {
1121
1121
    /*
1122
 
      Here we already have a copy of database name to be used in THD. So,
1123
 
      we just call THD::reset_db(). Since THD::reset_db() does not releases
 
1122
      Here we already have a copy of database name to be used in Session. So,
 
1123
      we just call Session::reset_db(). Since Session::reset_db() does not releases
1124
1124
      the previous database name, we should do it explicitly.
1125
1125
    */
1126
1126
 
1127
 
    if (thd->db)
1128
 
      free(thd->db);
 
1127
    if (session->db)
 
1128
      free(session->db);
1129
1129
 
1130
 
    thd->reset_db(new_db_name->str, new_db_name->length);
 
1130
    session->reset_db(new_db_name->str, new_db_name->length);
1131
1131
  }
1132
1132
 
1133
1133
  /* 3. Update db-charset environment variables. */
1134
1134
 
1135
 
  thd->db_charset= new_db_charset;
1136
 
  thd->variables.collation_database= new_db_charset;
 
1135
  session->db_charset= new_db_charset;
 
1136
  session->variables.collation_database= new_db_charset;
1137
1137
}
1138
1138
 
1139
1139
 
1141
1141
/**
1142
1142
  Backup the current database name before switch.
1143
1143
 
1144
 
  @param[in]      thd             thread handle
 
1144
  @param[in]      session             thread handle
1145
1145
  @param[in, out] saved_db_name   IN: "str" points to a buffer where to store
1146
1146
                                  the old database name, "length" contains the
1147
1147
                                  buffer size
1153
1153
                                  "length" is set to 0.
1154
1154
*/
1155
1155
 
1156
 
static void backup_current_db_name(THD *thd,
 
1156
static void backup_current_db_name(Session *session,
1157
1157
                                   LEX_STRING *saved_db_name)
1158
1158
{
1159
 
  if (!thd->db)
 
1159
  if (!session->db)
1160
1160
  {
1161
1161
    /* No current (default) database selected. */
1162
1162
 
1165
1165
  }
1166
1166
  else
1167
1167
  {
1168
 
    strmake(saved_db_name->str, thd->db, saved_db_name->length - 1);
1169
 
    saved_db_name->length= thd->db_length;
 
1168
    strmake(saved_db_name->str, session->db, saved_db_name->length - 1);
 
1169
    saved_db_name->length= session->db_length;
1170
1170
  }
1171
1171
}
1172
1172
 
1198
1198
/**
1199
1199
  @brief Change the current database and its attributes unconditionally.
1200
1200
 
1201
 
  @param thd          thread handle
 
1201
  @param session          thread handle
1202
1202
  @param new_db_name  database name
1203
1203
  @param force_switch if force_switch is false, then the operation will fail if
1204
1204
 
1224
1224
                          @@collation_server, but the operation will fail;
1225
1225
 
1226
1226
                        - user privileges will not be checked
1227
 
                          (THD::db_access however is updated);
 
1227
                          (Session::db_access however is updated);
1228
1228
 
1229
1229
                          TODO: is this really the intention?
1230
1230
                                (see sp-security.test).
1237
1237
  @details The function checks that the database name corresponds to a
1238
1238
  valid and existent database, checks access rights and changes the current
1239
1239
  database with database attributes (@@collation_database session variable,
1240
 
  THD::db_access).
 
1240
  Session::db_access).
1241
1241
 
1242
1242
  This function is not the only way to switch the database that is
1243
1243
  currently employed. When the replication slave thread switches the
1244
 
  database before executing a query, it calls thd->set_db directly.
 
1244
  database before executing a query, it calls session->set_db directly.
1245
1245
  However, if the query, in turn, uses a stored routine, the stored routine
1246
1246
  will use this function, even if it's run on the slave.
1247
1247
 
1257
1257
    @retval true  Error
1258
1258
*/
1259
1259
 
1260
 
bool mysql_change_db(THD *thd, const LEX_STRING *new_db_name, bool force_switch)
 
1260
bool mysql_change_db(Session *session, const LEX_STRING *new_db_name, bool force_switch)
1261
1261
{
1262
1262
  LEX_STRING new_db_file_name;
1263
1263
  const CHARSET_INFO *db_default_cl;
1277
1277
        new_db_name->length == 0.
1278
1278
      */
1279
1279
 
1280
 
      mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
 
1280
      mysql_change_db_impl(session, NULL, session->variables.collation_server);
1281
1281
 
1282
1282
      return(false);
1283
1283
    }
1294
1294
  {
1295
1295
    /* Switch the current database to INFORMATION_SCHEMA. */
1296
1296
 
1297
 
    mysql_change_db_impl(thd, &INFORMATION_SCHEMA_NAME, system_charset_info);
 
1297
    mysql_change_db_impl(session, &INFORMATION_SCHEMA_NAME, system_charset_info);
1298
1298
 
1299
1299
    return(false);
1300
1300
  }
1328
1328
    free(new_db_file_name.str);
1329
1329
 
1330
1330
    if (force_switch)
1331
 
      mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
 
1331
      mysql_change_db_impl(session, NULL, session->variables.collation_server);
1332
1332
 
1333
1333
    return(true);
1334
1334
  }
1339
1339
    {
1340
1340
      /* Throw a warning and free new_db_file_name. */
1341
1341
 
1342
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1342
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1343
1343
                          ER_BAD_DB_ERROR, ER(ER_BAD_DB_ERROR),
1344
1344
                          new_db_file_name.str);
1345
1345
 
1347
1347
 
1348
1348
      /* Change db to NULL. */
1349
1349
 
1350
 
      mysql_change_db_impl(thd, NULL, thd->variables.collation_server);
 
1350
      mysql_change_db_impl(session, NULL, session->variables.collation_server);
1351
1351
 
1352
1352
      /* The operation succeed. */
1353
1353
 
1367
1367
  }
1368
1368
 
1369
1369
  /*
1370
 
    NOTE: in mysql_change_db_impl() new_db_file_name is assigned to THD
1371
 
    attributes and will be freed in THD::~THD().
 
1370
    NOTE: in mysql_change_db_impl() new_db_file_name is assigned to Session
 
1371
    attributes and will be freed in Session::~Session().
1372
1372
  */
1373
1373
 
1374
 
  db_default_cl= get_default_db_collation(thd, new_db_file_name.str);
 
1374
  db_default_cl= get_default_db_collation(session, new_db_file_name.str);
1375
1375
 
1376
 
  mysql_change_db_impl(thd, &new_db_file_name, db_default_cl);
 
1376
  mysql_change_db_impl(session, &new_db_file_name, db_default_cl);
1377
1377
 
1378
1378
  return(false);
1379
1379
}
1382
1382
/**
1383
1383
  Change the current database and its attributes if needed.
1384
1384
 
1385
 
  @param          thd             thread handle
 
1385
  @param          session             thread handle
1386
1386
  @param          new_db_name     database name
1387
1387
  @param[in, out] saved_db_name   IN: "str" points to a buffer where to store
1388
1388
                                  the old database name, "length" contains the
1399
1399
                                  the function suceeded)
1400
1400
*/
1401
1401
 
1402
 
bool mysql_opt_change_db(THD *thd,
 
1402
bool mysql_opt_change_db(Session *session,
1403
1403
                         const LEX_STRING *new_db_name,
1404
1404
                         LEX_STRING *saved_db_name,
1405
1405
                         bool force_switch,
1406
1406
                         bool *cur_db_changed)
1407
1407
{
1408
 
  *cur_db_changed= !cmp_db_names(thd->db, new_db_name->str);
 
1408
  *cur_db_changed= !cmp_db_names(session->db, new_db_name->str);
1409
1409
 
1410
1410
  if (!*cur_db_changed)
1411
1411
    return false;
1412
1412
 
1413
 
  backup_current_db_name(thd, saved_db_name);
 
1413
  backup_current_db_name(session, saved_db_name);
1414
1414
 
1415
 
  return mysql_change_db(thd, new_db_name, force_switch);
 
1415
  return mysql_change_db(session, new_db_name, force_switch);
1416
1416
}
1417
1417
 
1418
1418