~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2008-10-20 04:28:21 UTC
  • mto: (492.3.21 drizzle-clean-code)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: brian@tangent.org-20081020042821-rqqdrccuu8195k3y
Second pass of thd cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
static pthread_mutex_t LOCK_table_share;
54
54
static bool table_def_inited= 0;
55
55
 
56
 
static int open_unireg_entry(Session *thd, Table *entry, TableList *table_list,
 
56
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
57
57
                             const char *alias,
58
58
                             char *cache_key, uint32_t cache_key_length);
59
59
static void free_cache_entry(Table *entry);
60
 
static void close_old_data_files(Session *thd, Table *table, bool morph_locks,
 
60
static void close_old_data_files(Session *session, Table *table, bool morph_locks,
61
61
                                 bool send_refresh);
62
62
 
63
63
 
98
98
 
99
99
  SYNOPSIS
100
100
    create_table_def_key()
101
 
    thd                 Thread handler
 
101
    session                     Thread handler
102
102
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
103
103
    table_list          Table definition
104
104
    tmp_table           Set if table is a tmp table
118
118
    Length of key
119
119
*/
120
120
 
121
 
uint32_t create_table_def_key(Session *thd, char *key, TableList *table_list,
 
121
uint32_t create_table_def_key(Session *session, char *key, TableList *table_list,
122
122
                          bool tmp_table)
123
123
{
124
124
  uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
125
125
                                  table_list->table_name)-key)+1;
126
126
  if (tmp_table)
127
127
  {
128
 
    int4store(key + key_length, thd->server_id);
129
 
    int4store(key + key_length + 4, thd->variables.pseudo_thread_id);
 
128
    int4store(key + key_length, session->server_id);
 
129
    int4store(key + key_length + 4, session->variables.pseudo_thread_id);
130
130
    key_length+= TMP_TABLE_KEY_EXTRA;
131
131
  }
132
132
  return key_length;
197
197
  Get TABLE_SHARE for a table.
198
198
 
199
199
  get_table_share()
200
 
  thd                   Thread handle
 
200
  session                       Thread handle
201
201
  table_list            Table that should be opened
202
202
  key                   Table cache key
203
203
  key_length            Length of key
218
218
   #  Share for table
219
219
*/
220
220
 
221
 
TABLE_SHARE *get_table_share(Session *thd, TableList *table_list, char *key,
 
221
TABLE_SHARE *get_table_share(Session *session, TableList *table_list, char *key,
222
222
                             uint32_t key_length, uint32_t db_flags, int *error)
223
223
{
224
224
  TABLE_SHARE *share;
261
261
    free_table_share(share);
262
262
    return(0);                          // return error
263
263
  }
264
 
  if (open_table_def(thd, share, db_flags))
 
264
  if (open_table_def(session, share, db_flags))
265
265
  {
266
266
    *error= share->error;
267
267
    (void) hash_delete(&table_def_cache, (unsigned char*) share);
321
321
*/
322
322
 
323
323
static TABLE_SHARE
324
 
*get_table_share_with_create(Session *thd, TableList *table_list,
 
324
*get_table_share_with_create(Session *session, TableList *table_list,
325
325
                             char *key, uint32_t key_length,
326
326
                             uint32_t db_flags, int *error)
327
327
{
328
328
  TABLE_SHARE *share;
329
329
  int tmp;
330
330
 
331
 
  share= get_table_share(thd, table_list, key, key_length, db_flags, error);
 
331
  share= get_table_share(session, table_list, key, key_length, db_flags, error);
332
332
  /*
333
333
    If share is not NULL, we found an existing share.
334
334
 
348
348
 
349
349
    @todo Rework alternative ways to deal with ER_NO_SUCH Table.
350
350
  */
351
 
  if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
 
351
  if (share || (session->is_error() && (session->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
352
352
 
353
353
    return(share);
354
354
 
355
355
  /* Table didn't exist. Check if some engine can provide it */
356
 
  if ((tmp= ha_create_table_from_engine(thd, table_list->db,
 
356
  if ((tmp= ha_create_table_from_engine(session, table_list->db,
357
357
                                        table_list->table_name)) < 0)
358
358
    return(0);
359
359
 
360
360
  if (tmp)
361
361
  {
362
362
    /* Give right error message */
363
 
    thd->clear_error();
 
363
    session->clear_error();
364
364
    my_printf_error(ER_UNKNOWN_ERROR,
365
365
                    "Failed to open '%-.64s', error while "
366
366
                    "unpacking from engine",
368
368
    return(0);
369
369
  }
370
370
  /* Table existed in engine. Let's open it */
371
 
  drizzle_reset_errors(thd, 1);                   // Clear warnings
372
 
  thd->clear_error();                           // Clear error message
373
 
  return(get_table_share(thd, table_list, key, key_length,
 
371
  drizzle_reset_errors(session, 1);                   // Clear warnings
 
372
  session->clear_error();                           // Clear error message
 
373
  return(get_table_share(session, table_list, key, key_length,
374
374
                              db_flags, error));
375
375
}
376
376
 
471
471
    By leaving the table in the table cache, it disallows any other thread
472
472
    to open the table
473
473
 
474
 
    thd->killed will be set if we run out of memory
 
474
    session->killed will be set if we run out of memory
475
475
 
476
476
    If closing a MERGE child, the calling function has to take care for
477
477
    closing the parent too, if necessary.
518
518
 
519
519
  SYNOPSIS
520
520
    list_open_tables()
521
 
    thd                 Thread Session
 
521
    session                     Thread Session
522
522
    wild                SQL like expression
523
523
 
524
524
  NOTES
531
531
    #           Pointer to list of names of open tables.
532
532
*/
533
533
 
534
 
OPEN_TableList *list_open_tables(Session *thd __attribute__((unused)),
 
534
OPEN_TableList *list_open_tables(Session *session __attribute__((unused)),
535
535
                                  const char *db, const char *wild)
536
536
{
537
537
  int result = 0;
651
651
/*
652
652
  Close all tables which aren't in use by any thread
653
653
 
654
 
  @param thd Thread context
 
654
  @param session Thread context
655
655
  @param tables List of tables to remove from the cache
656
656
  @param have_lock If LOCK_open is locked
657
657
  @param wait_for_refresh Wait for a impending flush
663
663
          and tables must be NULL.
664
664
*/
665
665
 
666
 
bool close_cached_tables(Session *thd, TableList *tables, bool have_lock,
 
666
bool close_cached_tables(Session *session, TableList *tables, bool have_lock,
667
667
                         bool wait_for_refresh, bool wait_for_placeholders)
668
668
{
669
669
  bool result=0;
670
 
  assert(thd || (!wait_for_refresh && !tables));
 
670
  assert(session || (!wait_for_refresh && !tables));
671
671
 
672
672
  if (!have_lock)
673
673
    pthread_mutex_lock(&LOCK_open);
741
741
    bool found=0;
742
742
    for (TableList *table= tables; table; table= table->next_local)
743
743
    {
744
 
      if (remove_table_from_cache(thd, table->db, table->table_name,
 
744
      if (remove_table_from_cache(session, table->db, table->table_name,
745
745
                                  RTFC_OWNED_BY_Session_FLAG))
746
746
        found=1;
747
747
    }
755
755
      If there is any table that has a lower refresh_version, wait until
756
756
      this is closed (or this thread is killed) before returning
757
757
    */
758
 
    thd->mysys_var->current_mutex= &LOCK_open;
759
 
    thd->mysys_var->current_cond= &COND_refresh;
760
 
    thd->set_proc_info("Flushing tables");
 
758
    session->mysys_var->current_mutex= &LOCK_open;
 
759
    session->mysys_var->current_cond= &COND_refresh;
 
760
    session->set_proc_info("Flushing tables");
761
761
 
762
 
    close_old_data_files(thd,thd->open_tables,1,1);
763
 
    mysql_ha_flush(thd);
 
762
    close_old_data_files(session,session->open_tables,1,1);
 
763
    mysql_ha_flush(session);
764
764
 
765
765
    bool found=1;
766
766
    /* Wait until all threads has closed all the tables we had locked */
767
 
    while (found && ! thd->killed)
 
767
    while (found && ! session->killed)
768
768
    {
769
769
      found=0;
770
770
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
771
771
      {
772
772
        Table *table=(Table*) hash_element(&open_cache,idx);
773
773
        /* Avoid a self-deadlock. */
774
 
        if (table->in_use == thd)
 
774
        if (table->in_use == session)
775
775
          continue;
776
776
        /*
777
777
          Note that we wait here only for tables which are actually open, and
801
801
      old locks. This should always succeed (unless some external process
802
802
      has removed the tables)
803
803
    */
804
 
    thd->in_lock_tables=1;
805
 
    result=reopen_tables(thd,1,1);
806
 
    thd->in_lock_tables=0;
 
804
    session->in_lock_tables=1;
 
805
    result=reopen_tables(session,1,1);
 
806
    session->in_lock_tables=0;
807
807
    /* Set version for table */
808
 
    for (Table *table=thd->open_tables; table ; table= table->next)
 
808
    for (Table *table=session->open_tables; table ; table= table->next)
809
809
    {
810
810
      /*
811
811
        Preserve the version (0) of write locked tables so that a impending
819
819
    pthread_mutex_unlock(&LOCK_open);
820
820
  if (wait_for_refresh)
821
821
  {
822
 
    pthread_mutex_lock(&thd->mysys_var->mutex);
823
 
    thd->mysys_var->current_mutex= 0;
824
 
    thd->mysys_var->current_cond= 0;
825
 
    thd->set_proc_info(0);
826
 
    pthread_mutex_unlock(&thd->mysys_var->mutex);
 
822
    pthread_mutex_lock(&session->mysys_var->mutex);
 
823
    session->mysys_var->current_mutex= 0;
 
824
    session->mysys_var->current_cond= 0;
 
825
    session->set_proc_info(0);
 
826
    pthread_mutex_unlock(&session->mysys_var->mutex);
827
827
  }
828
828
  return(result);
829
829
}
834
834
  if specified string is NULL, then any table with a connection string.
835
835
*/
836
836
 
837
 
bool close_cached_connection_tables(Session *thd, bool if_wait_for_refresh,
 
837
bool close_cached_connection_tables(Session *session, bool if_wait_for_refresh,
838
838
                                    LEX_STRING *connection, bool have_lock)
839
839
{
840
840
  uint32_t idx;
841
841
  TableList tmp, *tables= NULL;
842
842
  bool result= false;
843
 
  assert(thd);
 
843
  assert(session);
844
844
 
845
845
  memset(&tmp, 0, sizeof(TableList));
846
846
 
870
870
    tmp.table_name= share->table_name.str;
871
871
    tmp.next_local= tables;
872
872
 
873
 
    tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp, 
 
873
    tables= (TableList *) memdup_root(session->mem_root, (char*)&tmp, 
874
874
                                       sizeof(TableList));
875
875
  }
876
876
 
877
877
  if (tables)
878
 
    result= close_cached_tables(thd, tables, true, false, false);
 
878
    result= close_cached_tables(session, tables, true, false, false);
879
879
 
880
880
  if (!have_lock)
881
881
    pthread_mutex_unlock(&LOCK_open);
882
882
 
883
883
  if (if_wait_for_refresh)
884
884
  {
885
 
    pthread_mutex_lock(&thd->mysys_var->mutex);
886
 
    thd->mysys_var->current_mutex= 0;
887
 
    thd->mysys_var->current_cond= 0;
888
 
    thd->set_proc_info(0);
889
 
    pthread_mutex_unlock(&thd->mysys_var->mutex);
 
885
    pthread_mutex_lock(&session->mysys_var->mutex);
 
886
    session->mysys_var->current_mutex= 0;
 
887
    session->mysys_var->current_cond= 0;
 
888
    session->set_proc_info(0);
 
889
    pthread_mutex_unlock(&session->mysys_var->mutex);
890
890
  }
891
891
 
892
892
  return(result);
897
897
  Mark all temporary tables which were used by the current statement or
898
898
  substatement as free for reuse, but only if the query_id can be cleared.
899
899
 
900
 
  @param thd thread context
 
900
  @param session thread context
901
901
 
902
902
  @remark For temp tables associated with a open SQL HANDLER the query_id
903
903
          is not reset until the HANDLER is closed.
904
904
*/
905
905
 
906
 
static void mark_temp_tables_as_free_for_reuse(Session *thd)
 
906
static void mark_temp_tables_as_free_for_reuse(Session *session)
907
907
{
908
 
  for (Table *table= thd->temporary_tables ; table ; table= table->next)
 
908
  for (Table *table= session->temporary_tables ; table ; table= table->next)
909
909
  {
910
 
    if ((table->query_id == thd->query_id) && ! table->open_by_handler)
 
910
    if ((table->query_id == session->query_id) && ! table->open_by_handler)
911
911
    {
912
912
      table->query_id= 0;
913
913
      table->file->ha_reset();
922
922
 
923
923
  SYNOPSIS
924
924
    mark_used_tables_as_free_for_reuse()
925
 
      thd   - thread context
 
925
      session   - thread context
926
926
      table - head of the list of tables
927
927
 
928
928
  DESCRIPTION
931
931
 
932
932
  NOTE
933
933
    The reason we reset query_id is that it's not enough to just test
934
 
    if table->query_id != thd->query_id to know if a table is in use.
 
934
    if table->query_id != session->query_id to know if a table is in use.
935
935
 
936
936
    For example
937
937
    SELECT f1_that_uses_t1() FROM t1;
939
939
    set to query_id of original query.
940
940
*/
941
941
 
942
 
static void mark_used_tables_as_free_for_reuse(Session *thd, Table *table)
 
942
static void mark_used_tables_as_free_for_reuse(Session *session, Table *table)
943
943
{
944
944
  for (; table ; table= table->next)
945
945
  {
946
 
    if (table->query_id == thd->query_id)
 
946
    if (table->query_id == session->query_id)
947
947
    {
948
948
      table->query_id= 0;
949
949
      table->file->ha_reset();
955
955
/**
956
956
  Auxiliary function to close all tables in the open_tables list.
957
957
 
958
 
  @param thd Thread context.
 
958
  @param session Thread context.
959
959
 
960
960
  @remark It should not ordinarily be called directly.
961
961
*/
962
962
 
963
 
static void close_open_tables(Session *thd)
 
963
static void close_open_tables(Session *session)
964
964
{
965
965
  bool found_old_table= 0;
966
966
 
968
968
 
969
969
  pthread_mutex_lock(&LOCK_open);
970
970
 
971
 
  while (thd->open_tables)
972
 
    found_old_table|= close_thread_table(thd, &thd->open_tables);
973
 
  thd->some_tables_deleted= 0;
 
971
  while (session->open_tables)
 
972
    found_old_table|= close_thread_table(session, &session->open_tables);
 
973
  session->some_tables_deleted= 0;
974
974
 
975
975
  /* Free tables to hold down open files */
976
976
  while (open_cache.records > table_cache_size && unused_tables)
991
991
 
992
992
  SYNOPSIS
993
993
    close_thread_tables()
994
 
    thd                 Thread handler
 
994
    session                     Thread handler
995
995
 
996
996
  IMPLEMENTATION
997
997
    Unlocks tables and frees derived tables.
1003
1003
    leave prelocked mode if needed.
1004
1004
*/
1005
1005
 
1006
 
void close_thread_tables(Session *thd)
 
1006
void close_thread_tables(Session *session)
1007
1007
{
1008
1008
  Table *table;
1009
1009
 
1010
1010
  /*
1011
 
    We are assuming here that thd->derived_tables contains ONLY derived
 
1011
    We are assuming here that session->derived_tables contains ONLY derived
1012
1012
    tables for this substatement. i.e. instead of approach which uses
1013
1013
    query_id matching for determining which of the derived tables belong
1014
1014
    to this substatement we rely on the ability of substatements to
1015
 
    save/restore thd->derived_tables during their execution.
 
1015
    save/restore session->derived_tables during their execution.
1016
1016
 
1017
1017
    TODO: Probably even better approach is to simply associate list of
1018
1018
          derived tables with (sub-)statement instead of thread and destroy
1019
1019
          them at the end of its execution.
1020
1020
  */
1021
 
  if (thd->derived_tables)
 
1021
  if (session->derived_tables)
1022
1022
  {
1023
1023
    Table *next;
1024
1024
    /*
1025
1025
      Close all derived tables generated in queries like
1026
1026
      SELECT * FROM (SELECT * FROM t1)
1027
1027
    */
1028
 
    for (table= thd->derived_tables ; table ; table= next)
 
1028
    for (table= session->derived_tables ; table ; table= next)
1029
1029
    {
1030
1030
      next= table->next;
1031
 
      table->free_tmp_table(thd);
 
1031
      table->free_tmp_table(session);
1032
1032
    }
1033
 
    thd->derived_tables= 0;
 
1033
    session->derived_tables= 0;
1034
1034
  }
1035
1035
 
1036
1036
  /*
1037
1037
    Mark all temporary tables used by this statement as free for reuse.
1038
1038
  */
1039
 
  mark_temp_tables_as_free_for_reuse(thd);
 
1039
  mark_temp_tables_as_free_for_reuse(session);
1040
1040
  /*
1041
1041
    Let us commit transaction for statement. Since in 5.0 we only have
1042
1042
    one statement transaction and don't allow several nested statement
1045
1045
    does not belong to statement for which we do close_thread_tables()).
1046
1046
    TODO: This should be fixed in later releases.
1047
1047
   */
1048
 
  if (!(thd->state_flags & Open_tables_state::BACKUPS_AVAIL))
 
1048
  if (!(session->state_flags & Open_tables_state::BACKUPS_AVAIL))
1049
1049
  {
1050
 
    thd->main_da.can_overwrite_status= true;
1051
 
    ha_autocommit_or_rollback(thd, thd->is_error());
1052
 
    thd->main_da.can_overwrite_status= false;
1053
 
    thd->transaction.stmt.reset();
 
1050
    session->main_da.can_overwrite_status= true;
 
1051
    ha_autocommit_or_rollback(session, session->is_error());
 
1052
    session->main_da.can_overwrite_status= false;
 
1053
    session->transaction.stmt.reset();
1054
1054
  }
1055
1055
 
1056
 
  if (thd->locked_tables)
 
1056
  if (session->locked_tables)
1057
1057
  {
1058
1058
 
1059
1059
    /* Ensure we are calling ha_reset() for all used tables */
1060
 
    mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
 
1060
    mark_used_tables_as_free_for_reuse(session, session->open_tables);
1061
1061
 
1062
1062
    /*
1063
1063
      We are under simple LOCK TABLES so should not do anything else.
1065
1065
    return;
1066
1066
  }
1067
1067
 
1068
 
  if (thd->lock)
 
1068
  if (session->lock)
1069
1069
  {
1070
1070
    /*
1071
1071
      For RBR we flush the pending event just before we unlock all the
1076
1076
      handled either before writing a query log event (inside
1077
1077
      binlog_query()) or when preparing a pending event.
1078
1078
     */
1079
 
    thd->binlog_flush_pending_rows_event(true);
1080
 
    mysql_unlock_tables(thd, thd->lock);
1081
 
    thd->lock=0;
 
1079
    session->binlog_flush_pending_rows_event(true);
 
1080
    mysql_unlock_tables(session, session->lock);
 
1081
    session->lock=0;
1082
1082
  }
1083
1083
  /*
1084
1084
    Note that we need to hold LOCK_open while changing the
1087
1087
    Closing a MERGE child before the parent would be fatal if the
1088
1088
    other thread tries to abort the MERGE lock in between.
1089
1089
  */
1090
 
  if (thd->open_tables)
1091
 
    close_open_tables(thd);
 
1090
  if (session->open_tables)
 
1091
    close_open_tables(session);
1092
1092
 
1093
1093
  return;
1094
1094
}
1096
1096
 
1097
1097
/* move one table to free list */
1098
1098
 
1099
 
bool close_thread_table(Session *thd, Table **table_ptr)
 
1099
bool close_thread_table(Session *session, Table **table_ptr)
1100
1100
{
1101
1101
  bool found_old_table= 0;
1102
1102
  Table *table= *table_ptr;
1107
1107
  *table_ptr=table->next;
1108
1108
 
1109
1109
  if (table->needs_reopen_or_name_lock() ||
1110
 
      thd->version != refresh_version || !table->db_stat)
 
1110
      session->version != refresh_version || !table->db_stat)
1111
1111
  {
1112
1112
    hash_delete(&open_cache,(unsigned char*) table);
1113
1113
    found_old_table=1;
1138
1138
 
1139
1139
 
1140
1140
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1141
 
static inline uint32_t  tmpkeyval(Session *thd __attribute__((unused)),
 
1141
static inline uint32_t  tmpkeyval(Session *session __attribute__((unused)),
1142
1142
                              Table *table)
1143
1143
{
1144
1144
  return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1150
1150
  creates one DROP TEMPORARY Table binlog event for each pseudo-thread 
1151
1151
*/
1152
1152
 
1153
 
void close_temporary_tables(Session *thd)
 
1153
void close_temporary_tables(Session *session)
1154
1154
{
1155
1155
  Table *table;
1156
1156
  Table *next= NULL;
1157
1157
  Table *prev_table;
1158
 
  /* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
 
1158
  /* Assume session->options has OPTION_QUOTE_SHOW_CREATE */
1159
1159
  bool was_quote_show= true;
1160
1160
 
1161
 
  if (!thd->temporary_tables)
 
1161
  if (!session->temporary_tables)
1162
1162
    return;
1163
1163
 
1164
 
  if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
 
1164
  if (!mysql_bin_log.is_open() || session->current_stmt_binlog_row_based)
1165
1165
  {
1166
1166
    Table *tmp_next;
1167
 
    for (table= thd->temporary_tables; table; table= tmp_next)
 
1167
    for (table= session->temporary_tables; table; table= tmp_next)
1168
1168
    {
1169
1169
      tmp_next= table->next;
1170
1170
      close_temporary(table, 1, 1);
1171
1171
    }
1172
 
    thd->temporary_tables= 0;
 
1172
    session->temporary_tables= 0;
1173
1173
    return;
1174
1174
  }
1175
1175
 
1187
1187
    of sublists of equal pseudo_thread_id
1188
1188
  */
1189
1189
 
1190
 
  for (prev_table= thd->temporary_tables, table= prev_table->next;
 
1190
  for (prev_table= session->temporary_tables, table= prev_table->next;
1191
1191
       table;
1192
1192
       prev_table= table, table= table->next)
1193
1193
  {
1196
1196
    {
1197
1197
      if (!found_user_tables)
1198
1198
        found_user_tables= true;
1199
 
      for (prev_sorted= NULL, sorted= thd->temporary_tables; sorted != table;
 
1199
      for (prev_sorted= NULL, sorted= session->temporary_tables; sorted != table;
1200
1200
           prev_sorted= sorted, sorted= sorted->next)
1201
1201
      {
1202
1202
        if (!is_user_table(sorted) ||
1203
 
            tmpkeyval(thd, sorted) > tmpkeyval(thd, table))
 
1203
            tmpkeyval(session, sorted) > tmpkeyval(session, table))
1204
1204
        {
1205
1205
          /* move into the sorted part of the list from the unsorted */
1206
1206
          prev_table->next= table->next;
1211
1211
          }
1212
1212
          else
1213
1213
          {
1214
 
            thd->temporary_tables= table;
 
1214
            session->temporary_tables= table;
1215
1215
          }
1216
1216
          table= prev_table;
1217
1217
          break;
1222
1222
 
1223
1223
  /* We always quote db,table names though it is slight overkill */
1224
1224
  if (found_user_tables &&
1225
 
      !(was_quote_show= test(thd->options & OPTION_QUOTE_SHOW_CREATE)))
 
1225
      !(was_quote_show= test(session->options & OPTION_QUOTE_SHOW_CREATE)))
1226
1226
  {
1227
 
    thd->options |= OPTION_QUOTE_SHOW_CREATE;
 
1227
    session->options |= OPTION_QUOTE_SHOW_CREATE;
1228
1228
  }
1229
1229
 
1230
1230
  /* scan sorted tmps to generate sequence of DROP */
1231
 
  for (table= thd->temporary_tables; table; table= next)
 
1231
  for (table= session->temporary_tables; table; table= next)
1232
1232
  {
1233
1233
    if (is_user_table(table))
1234
1234
    {
1235
 
      my_thread_id save_pseudo_thread_id= thd->variables.pseudo_thread_id;
 
1235
      my_thread_id save_pseudo_thread_id= session->variables.pseudo_thread_id;
1236
1236
      /* Set pseudo_thread_id to be that of the processed table */
1237
 
      thd->variables.pseudo_thread_id= tmpkeyval(thd, table);
 
1237
      session->variables.pseudo_thread_id= tmpkeyval(session, table);
1238
1238
      /*
1239
1239
        Loop forward through all tables within the sublist of
1240
1240
        common pseudo_thread_id to create single DROP query.
1241
1241
      */
1242
1242
      for (s_query.length(stub_len);
1243
1243
           table && is_user_table(table) &&
1244
 
             tmpkeyval(thd, table) == thd->variables.pseudo_thread_id;
 
1244
             tmpkeyval(session, table) == session->variables.pseudo_thread_id;
1245
1245
           table= next)
1246
1246
      {
1247
1247
        /*
1248
1248
          We are going to add 4 ` around the db/table names and possible more
1249
1249
          due to special characters in the names
1250
1250
        */
1251
 
        append_identifier(thd, &s_query, table->s->db.str, strlen(table->s->db.str));
 
1251
        append_identifier(session, &s_query, table->s->db.str, strlen(table->s->db.str));
1252
1252
        s_query.append('.');
1253
 
        append_identifier(thd, &s_query, table->s->table_name.str,
 
1253
        append_identifier(session, &s_query, table->s->table_name.str,
1254
1254
                          strlen(table->s->table_name.str));
1255
1255
        s_query.append(',');
1256
1256
        next= table->next;
1257
1257
        close_temporary(table, 1, 1);
1258
1258
      }
1259
 
      thd->clear_error();
1260
 
      const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1261
 
      thd->variables.character_set_client= system_charset_info;
1262
 
      Query_log_event qinfo(thd, s_query.ptr(),
 
1259
      session->clear_error();
 
1260
      const CHARSET_INFO * const cs_save= session->variables.character_set_client;
 
1261
      session->variables.character_set_client= system_charset_info;
 
1262
      Query_log_event qinfo(session, s_query.ptr(),
1263
1263
                            s_query.length() - 1 /* to remove trailing ',' */,
1264
1264
                            0, false);
1265
 
      thd->variables.character_set_client= cs_save;
 
1265
      session->variables.character_set_client= cs_save;
1266
1266
      /*
1267
1267
        Imagine the thread had created a temp table, then was doing a
1268
1268
        SELECT, and the SELECT was killed. Then it's not clever to
1275
1275
      */
1276
1276
      qinfo.error_code= 0;
1277
1277
      mysql_bin_log.write(&qinfo);
1278
 
      thd->variables.pseudo_thread_id= save_pseudo_thread_id;
 
1278
      session->variables.pseudo_thread_id= save_pseudo_thread_id;
1279
1279
    }
1280
1280
    else
1281
1281
    {
1284
1284
    }
1285
1285
  }
1286
1286
  if (!was_quote_show)
1287
 
    thd->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
1288
 
  thd->temporary_tables=0;
 
1287
    session->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
 
1288
  session->temporary_tables=0;
1289
1289
}
1290
1290
 
1291
1291
/*
1328
1328
 
1329
1329
  SYNOPSIS
1330
1330
    unique_table()
1331
 
    thd                   thread handle
 
1331
    session                   thread handle
1332
1332
    table                 table which should be checked
1333
1333
    table_list            list of tables
1334
1334
    check_alias           whether to check tables' aliases
1359
1359
    0 if table is unique
1360
1360
*/
1361
1361
 
1362
 
TableList* unique_table(Session *thd, TableList *table, TableList *table_list,
 
1362
TableList* unique_table(Session *session, TableList *table, TableList *table_list,
1363
1363
                         bool check_alias)
1364
1364
{
1365
1365
  TableList *res;
1394
1394
  for (;;)
1395
1395
  {
1396
1396
    if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) &&
1397
 
         (! (res= mysql_lock_have_duplicate(thd, table, table_list)))) ||
 
1397
         (! (res= mysql_lock_have_duplicate(session, table, table_list)))) ||
1398
1398
        ((!res->table || res->table != table->table) &&
1399
1399
         (!check_alias || !(lower_case_table_names ?
1400
1400
          my_strcasecmp(files_charset_info, t_alias, res->alias) :
1434
1434
}
1435
1435
 
1436
1436
 
1437
 
Table *find_temporary_table(Session *thd, const char *db, const char *table_name)
 
1437
Table *find_temporary_table(Session *session, const char *db, const char *table_name)
1438
1438
{
1439
1439
  TableList table_list;
1440
1440
 
1441
1441
  table_list.db= (char*) db;
1442
1442
  table_list.table_name= (char*) table_name;
1443
 
  return find_temporary_table(thd, &table_list);
 
1443
  return find_temporary_table(session, &table_list);
1444
1444
}
1445
1445
 
1446
1446
 
1447
 
Table *find_temporary_table(Session *thd, TableList *table_list)
 
1447
Table *find_temporary_table(Session *session, TableList *table_list)
1448
1448
{
1449
1449
  char  key[MAX_DBKEY_LENGTH];
1450
1450
  uint  key_length;
1451
1451
  Table *table;
1452
1452
 
1453
 
  key_length= create_table_def_key(thd, key, table_list, 1);
1454
 
  for (table=thd->temporary_tables ; table ; table= table->next)
 
1453
  key_length= create_table_def_key(session, key, table_list, 1);
 
1454
  for (table=session->temporary_tables ; table ; table= table->next)
1455
1455
  {
1456
1456
    if (table->s->table_cache_key.length == key_length &&
1457
1457
        !memcmp(table->s->table_cache_key.str, key, key_length))
1464
1464
/**
1465
1465
  Drop a temporary table.
1466
1466
 
1467
 
  Try to locate the table in the list of thd->temporary_tables.
 
1467
  Try to locate the table in the list of session->temporary_tables.
1468
1468
  If the table is found:
1469
1469
   - if the table is being used by some outer statement, fail.
1470
 
   - if the table is in thd->locked_tables, unlock it and
 
1470
   - if the table is in session->locked_tables, unlock it and
1471
1471
     remove it from the list of locked tables. Currently only transactional
1472
1472
     temporary tables are present in the locked_tables list.
1473
1473
   - Close the temporary table, remove its .FRM
1478
1478
  or ALTER Table. Even though part of the work done by this function
1479
1479
  is redundant when the table is internal, as long as we
1480
1480
  link both internal and user temporary tables into the same
1481
 
  thd->temporary_tables list, it's impossible to tell here whether
 
1481
  session->temporary_tables list, it's impossible to tell here whether
1482
1482
  we're dealing with an internal or a user temporary table.
1483
1483
 
1484
1484
  @retval  0  the table was found and dropped successfully.
1487
1487
  @retval -1  the table is in use by a outer query
1488
1488
*/
1489
1489
 
1490
 
int drop_temporary_table(Session *thd, TableList *table_list)
 
1490
int drop_temporary_table(Session *session, TableList *table_list)
1491
1491
{
1492
1492
  Table *table;
1493
1493
 
1494
 
  if (!(table= find_temporary_table(thd, table_list)))
 
1494
  if (!(table= find_temporary_table(session, table_list)))
1495
1495
    return(1);
1496
1496
 
1497
1497
  /* Table might be in use by some outer statement. */
1498
 
  if (table->query_id && table->query_id != thd->query_id)
 
1498
  if (table->query_id && table->query_id != session->query_id)
1499
1499
  {
1500
1500
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1501
1501
    return(-1);
1505
1505
    If LOCK TABLES list is not empty and contains this table,
1506
1506
    unlock the table and remove the table from this list.
1507
1507
  */
1508
 
  mysql_lock_remove(thd, thd->locked_tables, table, false);
1509
 
  close_temporary_table(thd, table, 1, 1);
 
1508
  mysql_lock_remove(session, session->locked_tables, table, false);
 
1509
  close_temporary_table(session, table, 1, 1);
1510
1510
  return(0);
1511
1511
}
1512
1512
 
1513
1513
/*
1514
 
  unlink from thd->temporary tables and close temporary table
 
1514
  unlink from session->temporary tables and close temporary table
1515
1515
*/
1516
1516
 
1517
 
void close_temporary_table(Session *thd, Table *table,
 
1517
void close_temporary_table(Session *session, Table *table,
1518
1518
                           bool free_share, bool delete_table)
1519
1519
{
1520
1520
  if (table->prev)
1526
1526
  else
1527
1527
  {
1528
1528
    /* removing the item from the list */
1529
 
    assert(table == thd->temporary_tables);
 
1529
    assert(table == session->temporary_tables);
1530
1530
    /*
1531
1531
      slave must reset its temporary list pointer to zero to exclude
1532
1532
      passing non-zero value to end_slave via rli->save_temporary_tables
1533
1533
      when no temp tables opened, see an invariant below.
1534
1534
    */
1535
 
    thd->temporary_tables= table->next;
1536
 
    if (thd->temporary_tables)
 
1535
    session->temporary_tables= table->next;
 
1536
    if (session->temporary_tables)
1537
1537
      table->next->prev= 0;
1538
1538
  }
1539
 
  if (thd->slave_thread)
 
1539
  if (session->slave_thread)
1540
1540
  {
1541
1541
    /* natural invariant of temporary_tables */
1542
 
    assert(slave_open_temp_tables || !thd->temporary_tables);
 
1542
    assert(slave_open_temp_tables || !session->temporary_tables);
1543
1543
    slave_open_temp_tables--;
1544
1544
  }
1545
1545
  close_temporary(table, free_share, delete_table);
1551
1551
  Close and delete a temporary table
1552
1552
 
1553
1553
  NOTE
1554
 
    This dosn't unlink table from thd->temporary
 
1554
    This dosn't unlink table from session->temporary
1555
1555
    If this is needed, use close_temporary_table()
1556
1556
*/
1557
1557
 
1582
1582
  only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1583
1583
  name).
1584
1584
  Prepares a table cache key, which is the concatenation of db, table_name and
1585
 
  thd->slave_proxy_id, separated by '\0'.
 
1585
  session->slave_proxy_id, separated by '\0'.
1586
1586
*/
1587
1587
 
1588
 
bool rename_temporary_table(Session* thd, Table *table, const char *db,
 
1588
bool rename_temporary_table(Session* session, Table *table, const char *db,
1589
1589
                            const char *table_name)
1590
1590
{
1591
1591
  char *key;
1598
1598
 
1599
1599
  table_list.db= (char*) db;
1600
1600
  table_list.table_name= (char*) table_name;
1601
 
  key_length= create_table_def_key(thd, key, &table_list, 1);
 
1601
  key_length= create_table_def_key(session, key, &table_list, 1);
1602
1602
  share->set_table_cache_key(key, key_length);
1603
1603
  return(0);
1604
1604
}
1625
1625
    Remove all instances of table from thread's open list and
1626
1626
    table cache.
1627
1627
 
1628
 
    @param  thd     Thread context
 
1628
    @param  session     Thread context
1629
1629
    @param  find    Table to remove
1630
1630
    @param  unlock  true  - free all locks on tables removed that are
1631
1631
                            done with LOCK TABLES
1636
1636
          not locked (for example already unlocked).
1637
1637
*/
1638
1638
 
1639
 
void unlink_open_table(Session *thd, Table *find, bool unlock)
 
1639
void unlink_open_table(Session *session, Table *find, bool unlock)
1640
1640
{
1641
1641
  char key[MAX_DBKEY_LENGTH];
1642
1642
  uint32_t key_length= find->s->table_cache_key.length;
1652
1652
    Closing a MERGE child before the parent would be fatal if the
1653
1653
    other thread tries to abort the MERGE lock in between.
1654
1654
  */
1655
 
  for (prev= &thd->open_tables; *prev; )
 
1655
  for (prev= &session->open_tables; *prev; )
1656
1656
  {
1657
1657
    list= *prev;
1658
1658
 
1659
1659
    if (list->s->table_cache_key.length == key_length &&
1660
1660
        !memcmp(list->s->table_cache_key.str, key, key_length))
1661
1661
    {
1662
 
      if (unlock && thd->locked_tables)
1663
 
        mysql_lock_remove(thd, thd->locked_tables, list, true);
 
1662
      if (unlock && session->locked_tables)
 
1663
        mysql_lock_remove(session, session->locked_tables, list, true);
1664
1664
 
1665
1665
      /* Remove table from open_tables list. */
1666
1666
      *prev= list->next;
1683
1683
/**
1684
1684
    Auxiliary routine which closes and drops open table.
1685
1685
 
1686
 
    @param  thd         Thread handle
 
1686
    @param  session         Thread handle
1687
1687
    @param  table       Table object for table to be dropped
1688
1688
    @param  db_name     Name of database for this table
1689
1689
    @param  table_name  Name of this table
1699
1699
          table that was locked with LOCK TABLES.
1700
1700
*/
1701
1701
 
1702
 
void drop_open_table(Session *thd, Table *table, const char *db_name,
 
1702
void drop_open_table(Session *session, Table *table, const char *db_name,
1703
1703
                     const char *table_name)
1704
1704
{
1705
1705
  if (table->s->tmp_table)
1706
 
    close_temporary_table(thd, table, 1, 1);
 
1706
    close_temporary_table(session, table, 1, 1);
1707
1707
  else
1708
1708
  {
1709
1709
    handlerton *table_type= table->s->db_type();
1712
1712
      unlink_open_table() also tells threads waiting for refresh or close
1713
1713
      that something has happened.
1714
1714
    */
1715
 
    unlink_open_table(thd, table, false);
 
1715
    unlink_open_table(session, table, false);
1716
1716
    quick_rm_table(table_type, db_name, table_name, 0);
1717
1717
    pthread_mutex_unlock(&LOCK_open);
1718
1718
  }
1724
1724
 
1725
1725
   SYNOPSIS
1726
1726
     wait_for_condition()
1727
 
     thd        Thread handler
 
1727
     session    Thread handler
1728
1728
     mutex      mutex that is currently hold that is associated with condition
1729
1729
                Will be unlocked on return     
1730
1730
     cond       Condition to wait for
1731
1731
*/
1732
1732
 
1733
 
void wait_for_condition(Session *thd, pthread_mutex_t *mutex, pthread_cond_t *cond)
 
1733
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
1734
1734
{
1735
1735
  /* Wait until the current table is up to date */
1736
1736
  const char *proc_info;
1737
 
  thd->mysys_var->current_mutex= mutex;
1738
 
  thd->mysys_var->current_cond= cond;
1739
 
  proc_info=thd->get_proc_info();
1740
 
  thd->set_proc_info("Waiting for table");
1741
 
  if (!thd->killed)
 
1737
  session->mysys_var->current_mutex= mutex;
 
1738
  session->mysys_var->current_cond= cond;
 
1739
  proc_info=session->get_proc_info();
 
1740
  session->set_proc_info("Waiting for table");
 
1741
  if (!session->killed)
1742
1742
    (void) pthread_cond_wait(cond, mutex);
1743
1743
 
1744
1744
  /*
1753
1753
  */
1754
1754
    
1755
1755
  pthread_mutex_unlock(mutex);
1756
 
  pthread_mutex_lock(&thd->mysys_var->mutex);
1757
 
  thd->mysys_var->current_mutex= 0;
1758
 
  thd->mysys_var->current_cond= 0;
1759
 
  thd->set_proc_info(proc_info);
1760
 
  pthread_mutex_unlock(&thd->mysys_var->mutex);
 
1756
  pthread_mutex_lock(&session->mysys_var->mutex);
 
1757
  session->mysys_var->current_mutex= 0;
 
1758
  session->mysys_var->current_cond= 0;
 
1759
  session->set_proc_info(proc_info);
 
1760
  pthread_mutex_unlock(&session->mysys_var->mutex);
1761
1761
  return;
1762
1762
}
1763
1763
 
1766
1766
  Exclusively name-lock a table that is already write-locked by the
1767
1767
  current thread.
1768
1768
 
1769
 
  @param thd current thread context
 
1769
  @param session current thread context
1770
1770
  @param tables table list containing one table to open.
1771
1771
 
1772
1772
  @return false on success, true otherwise.
1773
1773
*/
1774
1774
 
1775
 
bool name_lock_locked_table(Session *thd, TableList *tables)
 
1775
bool name_lock_locked_table(Session *session, TableList *tables)
1776
1776
{
1777
1777
  /* Under LOCK TABLES we must only accept write locked tables. */
1778
 
  tables->table= find_locked_table(thd, tables->db, tables->table_name);
 
1778
  tables->table= find_locked_table(session, tables->db, tables->table_name);
1779
1779
 
1780
1780
  if (!tables->table)
1781
1781
    my_error(ER_TABLE_NOT_LOCKED, MYF(0), tables->alias);
1787
1787
      Ensures that table is opened only by this thread and that no
1788
1788
      other statement will open this table.
1789
1789
    */
1790
 
    wait_while_table_is_used(thd, tables->table, HA_EXTRA_FORCE_REOPEN);
 
1790
    wait_while_table_is_used(session, tables->table, HA_EXTRA_FORCE_REOPEN);
1791
1791
    return(false);
1792
1792
  }
1793
1793
 
1800
1800
 
1801
1801
  SYNOPSIS
1802
1802
    reopen_name_locked_table()
1803
 
      thd         Thread handle
 
1803
      session         Thread handle
1804
1804
      table_list  TableList object for table to be open, TableList::table
1805
1805
                  member should point to Table object which was used for
1806
1806
                  name-locking.
1818
1818
    true  - Error
1819
1819
*/
1820
1820
 
1821
 
bool reopen_name_locked_table(Session* thd, TableList* table_list, bool link_in)
 
1821
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in)
1822
1822
{
1823
1823
  Table *table= table_list->table;
1824
1824
  TABLE_SHARE *share;
1827
1827
 
1828
1828
  safe_mutex_assert_owner(&LOCK_open);
1829
1829
 
1830
 
  if (thd->killed || !table)
 
1830
  if (session->killed || !table)
1831
1831
    return(true);
1832
1832
 
1833
1833
  orig_table= *table;
1834
1834
 
1835
 
  if (open_unireg_entry(thd, table, table_list, table_name,
 
1835
  if (open_unireg_entry(session, table, table_list, table_name,
1836
1836
                        table->s->table_cache_key.str,
1837
1837
                        table->s->table_cache_key.length))
1838
1838
  {
1857
1857
    before we will get table-level lock on this table.
1858
1858
  */
1859
1859
  share->version=0;
1860
 
  table->in_use = thd;
 
1860
  table->in_use = session;
1861
1861
 
1862
1862
  if (link_in)
1863
1863
  {
1864
 
    table->next= thd->open_tables;
1865
 
    thd->open_tables= table;
 
1864
    table->next= session->open_tables;
 
1865
    session->open_tables= table;
1866
1866
  }
1867
1867
  else
1868
1868
  {
1873
1873
    table->next= orig_table.next;
1874
1874
  }
1875
1875
 
1876
 
  table->tablenr=thd->current_tablenr++;
 
1876
  table->tablenr=session->current_tablenr++;
1877
1877
  table->used_fields=0;
1878
1878
  table->const_table=0;
1879
1879
  table->null_row= false;
1889
1889
    which will prevent its opening (or creation) (a.k.a lock
1890
1890
    table name).
1891
1891
 
1892
 
    @param thd         Thread context
 
1892
    @param session         Thread context
1893
1893
    @param key         Table cache key for name to be locked
1894
1894
    @param key_length  Table cache key length
1895
1895
 
1897
1897
            case of failure.
1898
1898
*/
1899
1899
 
1900
 
Table *table_cache_insert_placeholder(Session *thd, const char *key,
 
1900
Table *table_cache_insert_placeholder(Session *session, const char *key,
1901
1901
                                      uint32_t key_length)
1902
1902
{
1903
1903
  Table *table;
1921
1921
  table->s= share;
1922
1922
  share->set_table_cache_key(key_buff, key, key_length);
1923
1923
  share->tmp_table= INTERNAL_TMP_TABLE;  // for intern_close_table
1924
 
  table->in_use= thd;
 
1924
  table->in_use= session;
1925
1925
  table->locked_by_name=1;
1926
1926
 
1927
1927
  if (my_hash_insert(&open_cache, (unsigned char*)table))
1938
1938
    Obtain an exclusive name lock on the table if it is not cached
1939
1939
    in the table cache.
1940
1940
 
1941
 
    @param      thd         Thread context
 
1941
    @param      session         Thread context
1942
1942
    @param      db          Name of database
1943
1943
    @param      table_name  Name of table
1944
1944
    @param[out] table       Out parameter which is either:
1956
1956
    @retval  false  Success. 'table' parameter set according to above rules.
1957
1957
*/
1958
1958
 
1959
 
bool lock_table_name_if_not_cached(Session *thd, const char *db,
 
1959
bool lock_table_name_if_not_cached(Session *session, const char *db,
1960
1960
                                   const char *table_name, Table **table)
1961
1961
{
1962
1962
  char key[MAX_DBKEY_LENGTH];
1971
1971
    *table= 0;
1972
1972
    return(false);
1973
1973
  }
1974
 
  if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
 
1974
  if (!(*table= table_cache_insert_placeholder(session, key, key_length)))
1975
1975
  {
1976
1976
    pthread_mutex_unlock(&LOCK_open);
1977
1977
    return(true);
1978
1978
  }
1979
1979
  (*table)->open_placeholder= 1;
1980
 
  (*table)->next= thd->open_tables;
1981
 
  thd->open_tables= *table;
 
1980
  (*table)->next= session->open_tables;
 
1981
  session->open_tables= *table;
1982
1982
  pthread_mutex_unlock(&LOCK_open);
1983
1983
  return(false);
1984
1984
}
1988
1988
    Check that table exists in table definition cache, on disk
1989
1989
    or in some storage engine.
1990
1990
 
1991
 
    @param       thd     Thread context
 
1991
    @param       session     Thread context
1992
1992
    @param       table   Table list element
1993
1993
    @param[out]  exists  Out parameter which is set to true if table
1994
1994
                         exists and to false otherwise.
2005
2005
    @retval  false  No error. 'exists' out parameter set accordingly.
2006
2006
*/
2007
2007
 
2008
 
bool check_if_table_exists(Session *thd, TableList *table, bool *exists)
 
2008
bool check_if_table_exists(Session *session, TableList *table, bool *exists)
2009
2009
{
2010
2010
  char path[FN_REFLEN];
2011
2011
  int rc;
2025
2025
 
2026
2026
  /* .FRM file doesn't exist. Check if some engine can provide it. */
2027
2027
 
2028
 
  rc= ha_create_table_from_engine(thd, table->db, table->table_name);
 
2028
  rc= ha_create_table_from_engine(session, table->db, table->table_name);
2029
2029
 
2030
2030
  if (rc < 0)
2031
2031
  {
2052
2052
 
2053
2053
  SYNOPSIS
2054
2054
    open_table()
2055
 
    thd                 Thread context.
 
2055
    session                 Thread context.
2056
2056
    table_list          Open first table in list.
2057
2057
    refresh      INOUT  Pointer to memory that will be set to 1 if
2058
2058
                        we need to close all tables and reopen them.
2080
2080
*/
2081
2081
 
2082
2082
 
2083
 
Table *open_table(Session *thd, TableList *table_list, bool *refresh, uint32_t flags)
 
2083
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags)
2084
2084
{
2085
2085
  register Table *table;
2086
2086
  char key[MAX_DBKEY_LENGTH];
2088
2088
  char *alias= table_list->alias;
2089
2089
  HASH_SEARCH_STATE state;
2090
2090
 
2091
 
  /* Parsing of partitioning information from .frm needs thd->lex set up. */
2092
 
  assert(thd->lex->is_lex_started);
 
2091
  /* Parsing of partitioning information from .frm needs session->lex set up. */
 
2092
  assert(session->lex->is_lex_started);
2093
2093
 
2094
2094
  /* find a unused table in the open table cache */
2095
2095
  if (refresh)
2096
2096
    *refresh=0;
2097
2097
 
2098
2098
  /* an open table operation needs a lot of the stack space */
2099
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
2100
 
    return(0);
2101
 
 
2102
 
  if (thd->killed)
2103
 
    return(0);
2104
 
 
2105
 
  key_length= (create_table_def_key(thd, key, table_list, 1) -
 
2099
  if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
 
2100
    return(0);
 
2101
 
 
2102
  if (session->killed)
 
2103
    return(0);
 
2104
 
 
2105
  key_length= (create_table_def_key(session, key, table_list, 1) -
2106
2106
               TMP_TABLE_KEY_EXTRA);
2107
2107
 
2108
2108
  /*
2113
2113
    TODO: move this block into a separate function.
2114
2114
  */
2115
2115
  {
2116
 
    for (table= thd->temporary_tables; table ; table=table->next)
 
2116
    for (table= session->temporary_tables; table ; table=table->next)
2117
2117
    {
2118
2118
      if (table->s->table_cache_key.length == key_length +
2119
2119
          TMP_TABLE_KEY_EXTRA &&
2131
2131
          my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2132
2132
          return(0);
2133
2133
        }
2134
 
        table->query_id= thd->query_id;
2135
 
        thd->thread_specific_used= true;
 
2134
        table->query_id= session->query_id;
 
2135
        session->thread_specific_used= true;
2136
2136
        goto reset;
2137
2137
      }
2138
2138
    }
2151
2151
    open not pre-opened tables in pre-locked/LOCK TABLES mode.
2152
2152
    TODO: move this block into a separate function.
2153
2153
  */
2154
 
  if (thd->locked_tables)
 
2154
  if (session->locked_tables)
2155
2155
  {                                             // Using table locks
2156
2156
    Table *best_table= 0;
2157
2157
    int best_distance= INT_MIN;
2158
2158
    bool check_if_used= false;
2159
 
    for (table=thd->open_tables; table ; table=table->next)
 
2159
    for (table=session->open_tables; table ; table=table->next)
2160
2160
    {
2161
2161
      if (table->s->table_cache_key.length == key_length &&
2162
2162
          !memcmp(table->s->table_cache_key.str, key, key_length))
2163
2163
      {
2164
2164
        if (check_if_used && table->query_id &&
2165
 
            table->query_id != thd->query_id)
 
2165
            table->query_id != session->query_id)
2166
2166
        {
2167
2167
          /*
2168
2168
            If we are in stored function or trigger we should ensure that
2179
2179
          belong to their parent and cannot be used explicitly.
2180
2180
        */
2181
2181
        if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2182
 
            table->query_id != thd->query_id)  /* skip tables already used */
 
2182
            table->query_id != session->query_id)  /* skip tables already used */
2183
2183
        {
2184
2184
          int distance= ((int) table->reginfo.lock_type -
2185
2185
                         (int) table_list->lock_type);
2216
2216
    if (best_table)
2217
2217
    {
2218
2218
      table= best_table;
2219
 
      table->query_id= thd->query_id;
 
2219
      table->query_id= session->query_id;
2220
2220
      goto reset;
2221
2221
    }
2222
2222
    /*
2256
2256
    and try to reopen them.
2257
2257
    Note: refresh_version is currently changed only during FLUSH TABLES.
2258
2258
  */
2259
 
  if (!thd->open_tables)
2260
 
    thd->version=refresh_version;
2261
 
  else if ((thd->version != refresh_version) &&
 
2259
  if (!session->open_tables)
 
2260
    session->version=refresh_version;
 
2261
  else if ((session->version != refresh_version) &&
2262
2262
           ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
2263
2263
  {
2264
2264
    /* Someone did a refresh while thread was opening tables */
2275
2275
    is pending for any table of the handler_tables list, otherwise a
2276
2276
    deadlock may occur.
2277
2277
  */
2278
 
  if (thd->handler_tables)
2279
 
    mysql_ha_flush(thd);
 
2278
  if (session->handler_tables)
 
2279
    mysql_ha_flush(session);
2280
2280
 
2281
2281
  /*
2282
2282
    Actually try to find the table in the open_cache.
2317
2317
      if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
2318
2318
      {
2319
2319
        /* Force close at once after usage */
2320
 
        thd->version= table->s->version;
 
2320
        session->version= table->s->version;
2321
2321
        continue;
2322
2322
      }
2323
2323
 
2324
2324
      /* Avoid self-deadlocks by detecting self-dependencies. */
2325
 
      if (table->open_placeholder && table->in_use == thd)
 
2325
      if (table->open_placeholder && table->in_use == session)
2326
2326
      {
2327
2327
        pthread_mutex_unlock(&LOCK_open);
2328
2328
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2342
2342
        the earlier call to mysql_ha_flush() in this same critical
2343
2343
        section.
2344
2344
      */
2345
 
      close_old_data_files(thd,thd->open_tables,0,0);
 
2345
      close_old_data_files(session,session->open_tables,0,0);
2346
2346
      /*
2347
2347
        Back-off part 2: try to avoid "busy waiting" on the table:
2348
2348
        if the table is in use by some other thread, we suspend
2358
2358
        after we open first instance but before we open second
2359
2359
        instance.
2360
2360
      */
2361
 
      if (table->in_use != thd)
 
2361
      if (table->in_use != session)
2362
2362
      {
2363
2363
        /* wait_for_conditionwill unlock LOCK_open for us */
2364
 
        wait_for_condition(thd, &LOCK_open, &COND_refresh);
 
2364
        wait_for_condition(session, &LOCK_open, &COND_refresh);
2365
2365
      }
2366
2366
      else
2367
2367
      {
2387
2387
    }
2388
2388
    table->prev->next=table->next;              /* Remove from unused list */
2389
2389
    table->next->prev=table->prev;
2390
 
    table->in_use= thd;
 
2390
    table->in_use= session;
2391
2391
  }
2392
2392
  else
2393
2393
  {
2401
2401
    {
2402
2402
      bool exists;
2403
2403
 
2404
 
      if (check_if_table_exists(thd, table_list, &exists))
 
2404
      if (check_if_table_exists(session, table_list, &exists))
2405
2405
      {
2406
2406
        pthread_mutex_unlock(&LOCK_open);
2407
2407
        return(NULL);
2412
2412
        /*
2413
2413
          Table to be created, so we need to create placeholder in table-cache.
2414
2414
        */
2415
 
        if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
 
2415
        if (!(table= table_cache_insert_placeholder(session, key, key_length)))
2416
2416
        {
2417
2417
          pthread_mutex_unlock(&LOCK_open);
2418
2418
          return(NULL);
2423
2423
          by other trying to take name-lock.
2424
2424
        */
2425
2425
        table->open_placeholder= 1;
2426
 
        table->next= thd->open_tables;
2427
 
        thd->open_tables= table;
 
2426
        table->next= session->open_tables;
 
2427
        session->open_tables= table;
2428
2428
        pthread_mutex_unlock(&LOCK_open);
2429
2429
        return(table);
2430
2430
      }
2438
2438
      return(NULL);
2439
2439
    }
2440
2440
 
2441
 
    error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
 
2441
    error= open_unireg_entry(session, table, table_list, alias, key, key_length);
2442
2442
    /* Combine the follow two */
2443
2443
    if (error > 0)
2444
2444
    {
2458
2458
  pthread_mutex_unlock(&LOCK_open);
2459
2459
  if (refresh)
2460
2460
  {
2461
 
    table->next=thd->open_tables;               /* Link into simple list */
2462
 
    thd->open_tables=table;
 
2461
    table->next=session->open_tables;           /* Link into simple list */
 
2462
    session->open_tables=table;
2463
2463
  }
2464
2464
  table->reginfo.lock_type=TL_READ;             /* Assume read */
2465
2465
 
2466
2466
 reset:
2467
2467
  assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2468
2468
 
2469
 
  if (thd->lex->need_correct_ident())
 
2469
  if (session->lex->need_correct_ident())
2470
2470
    table->alias_name_used= my_strcasecmp(table_alias_charset,
2471
2471
                                          table->s->table_name.str, alias);
2472
2472
  /* Fix alias if table name changes */
2478
2478
    memcpy((void*) table->alias, alias, length);
2479
2479
  }
2480
2480
  /* These variables are also set in reopen_table() */
2481
 
  table->tablenr=thd->current_tablenr++;
 
2481
  table->tablenr=session->current_tablenr++;
2482
2482
  table->used_fields=0;
2483
2483
  table->const_table=0;
2484
2484
  table->null_row= false;
2498
2498
}
2499
2499
 
2500
2500
 
2501
 
Table *find_locked_table(Session *thd, const char *db,const char *table_name)
 
2501
Table *find_locked_table(Session *session, const char *db,const char *table_name)
2502
2502
{
2503
2503
  char  key[MAX_DBKEY_LENGTH];
2504
2504
  uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
2505
2505
 
2506
 
  for (Table *table=thd->open_tables; table ; table=table->next)
 
2506
  for (Table *table=session->open_tables; table ; table=table->next)
2507
2507
  {
2508
2508
    if (table->s->table_cache_key.length == key_length &&
2509
2509
        !memcmp(table->s->table_cache_key.str, key, key_length))
2536
2536
  Field **field;
2537
2537
  uint32_t key,part;
2538
2538
  TableList table_list;
2539
 
  Session *thd= table->in_use;
 
2539
  Session *session= table->in_use;
2540
2540
 
2541
2541
  assert(table->s->ref_count == 0);
2542
2542
  assert(!table->sort.io_cache);
2551
2551
  table_list.table_name= table->s->table_name.str;
2552
2552
  table_list.table=      table;
2553
2553
 
2554
 
  if (wait_for_locked_table_names(thd, &table_list))
 
2554
  if (wait_for_locked_table_names(session, &table_list))
2555
2555
    return(1);                             // Thread was killed
2556
2556
 
2557
 
  if (open_unireg_entry(thd, &tmp, &table_list,
 
2557
  if (open_unireg_entry(session, &tmp, &table_list,
2558
2558
                        table->alias,
2559
2559
                        table->s->table_cache_key.str,
2560
2560
                        table->s->table_cache_key.length))
2571
2571
  tmp.s->table_map_id=  table->s->table_map_id;
2572
2572
 
2573
2573
  /* Get state */
2574
 
  tmp.in_use=           thd;
 
2574
  tmp.in_use=           session;
2575
2575
  tmp.reginfo.lock_type=table->reginfo.lock_type;
2576
2576
 
2577
2577
  /* Replace table in open list */
2614
2614
    Close all instances of a table open by this thread and replace
2615
2615
    them with exclusive name-locks.
2616
2616
 
2617
 
    @param thd        Thread context
 
2617
    @param session        Thread context
2618
2618
    @param db         Database name for the table to be closed
2619
2619
    @param table_name Name of the table to be closed
2620
2620
 
2627
2627
          the strings are used in a loop even after the share may be freed.
2628
2628
*/
2629
2629
 
2630
 
void close_data_files_and_morph_locks(Session *thd, const char *db,
 
2630
void close_data_files_and_morph_locks(Session *session, const char *db,
2631
2631
                                      const char *table_name)
2632
2632
{
2633
2633
  Table *table;
2634
2634
 
2635
2635
  safe_mutex_assert_owner(&LOCK_open);
2636
2636
 
2637
 
  if (thd->lock)
 
2637
  if (session->lock)
2638
2638
  {
2639
2639
    /*
2640
2640
      If we are not under LOCK TABLES we should have only one table
2641
2641
      open and locked so it makes sense to remove the lock at once.
2642
2642
    */
2643
 
    mysql_unlock_tables(thd, thd->lock);
2644
 
    thd->lock= 0;
 
2643
    mysql_unlock_tables(session, session->lock);
 
2644
    session->lock= 0;
2645
2645
  }
2646
2646
 
2647
2647
  /*
2649
2649
    for target table name if we process ALTER Table ... RENAME.
2650
2650
    So loop below makes sense even if we are not under LOCK TABLES.
2651
2651
  */
2652
 
  for (table=thd->open_tables; table ; table=table->next)
 
2652
  for (table=session->open_tables; table ; table=table->next)
2653
2653
  {
2654
2654
    if (!strcmp(table->s->table_name.str, table_name) &&
2655
2655
        !strcmp(table->s->db.str, db))
2656
2656
    {
2657
 
      if (thd->locked_tables)
 
2657
      if (session->locked_tables)
2658
2658
      {
2659
 
        mysql_lock_remove(thd, thd->locked_tables, table, true);
 
2659
        mysql_lock_remove(session, session->locked_tables, table, true);
2660
2660
      }
2661
2661
      table->open_placeholder= 1;
2662
2662
      close_handle_and_leave_table_as_lock(table);
2669
2669
/**
2670
2670
    Reopen all tables with closed data files.
2671
2671
 
2672
 
    @param thd         Thread context
 
2672
    @param session         Thread context
2673
2673
    @param get_locks   Should we get locks after reopening tables ?
2674
2674
    @param mark_share_as_old  Mark share as old to protect from a impending
2675
2675
                              global read lock.
2686
2686
    @return false in case of success, true - otherwise.
2687
2687
*/
2688
2688
 
2689
 
bool reopen_tables(Session *thd, bool get_locks, bool mark_share_as_old)
 
2689
bool reopen_tables(Session *session, bool get_locks, bool mark_share_as_old)
2690
2690
{
2691
2691
  Table *table,*next,**prev;
2692
2692
  Table **tables,**tables_ptr;                  // For locks
2695
2695
                    DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2696
2696
                    DRIZZLE_LOCK_IGNORE_FLUSH;
2697
2697
 
2698
 
  if (!thd->open_tables)
 
2698
  if (!session->open_tables)
2699
2699
    return(0);
2700
2700
 
2701
2701
  safe_mutex_assert_owner(&LOCK_open);
2706
2706
      Do not handle locks of MERGE children.
2707
2707
    */
2708
2708
    uint32_t opens=0;
2709
 
    for (table= thd->open_tables; table ; table=table->next)
 
2709
    for (table= session->open_tables; table ; table=table->next)
2710
2710
      opens++;
2711
2711
    tables= (Table**) my_alloca(sizeof(Table*)*opens);
2712
2712
  }
2713
2713
  else
2714
 
    tables= &thd->open_tables;
 
2714
    tables= &session->open_tables;
2715
2715
  tables_ptr =tables;
2716
2716
 
2717
 
  prev= &thd->open_tables;
2718
 
  for (table=thd->open_tables; table ; table=next)
 
2717
  prev= &session->open_tables;
 
2718
  for (table=session->open_tables; table ; table=next)
2719
2719
  {
2720
2720
    uint32_t db_stat=table->db_stat;
2721
2721
    next=table->next;
2748
2748
      wait_for_tables() as it tries to acquire LOCK_open, which is
2749
2749
      already locked.
2750
2750
    */
2751
 
    thd->some_tables_deleted=0;
2752
 
    if ((lock= mysql_lock_tables(thd, tables, (uint) (tables_ptr - tables),
 
2751
    session->some_tables_deleted=0;
 
2752
    if ((lock= mysql_lock_tables(session, tables, (uint) (tables_ptr - tables),
2753
2753
                                 flags, &not_used)))
2754
2754
    {
2755
 
      thd->locked_tables=mysql_lock_merge(thd->locked_tables,lock);
 
2755
      session->locked_tables=mysql_lock_merge(session->locked_tables,lock);
2756
2756
    }
2757
2757
    else
2758
2758
    {
2778
2778
    Close handlers for tables in list, but leave the Table structure
2779
2779
    intact so that we can re-open these quickly.
2780
2780
 
2781
 
    @param thd           Thread context
 
2781
    @param session           Thread context
2782
2782
    @param table         Head of the list of Table objects
2783
2783
    @param morph_locks   true  - remove locks which we have on tables being closed
2784
2784
                                 but ensure that no DML or DDL will sneak in before
2788
2788
    @param send_refresh  Should we awake waiters even if we didn't close any tables?
2789
2789
*/
2790
2790
 
2791
 
static void close_old_data_files(Session *thd, Table *table, bool morph_locks,
 
2791
static void close_old_data_files(Session *session, Table *table, bool morph_locks,
2792
2792
                                 bool send_refresh)
2793
2793
{
2794
2794
  bool found= send_refresh;
2814
2814
              lock on it. This will also give them a chance to close their
2815
2815
              instances of this table.
2816
2816
            */
2817
 
            mysql_lock_abort(thd, ulcktbl, true);
2818
 
            mysql_lock_remove(thd, thd->locked_tables, ulcktbl, true);
 
2817
            mysql_lock_abort(session, ulcktbl, true);
 
2818
            mysql_lock_remove(session, session->locked_tables, ulcktbl, true);
2819
2819
            ulcktbl->lock_count= 0;
2820
2820
          }
2821
2821
          if ((ulcktbl != table) && ulcktbl->db_stat)
2900
2900
 
2901
2901
/* Wait until all used tables are refreshed */
2902
2902
 
2903
 
bool wait_for_tables(Session *thd)
 
2903
bool wait_for_tables(Session *session)
2904
2904
{
2905
2905
  bool result;
2906
2906
 
2907
 
  thd->set_proc_info("Waiting for tables");
 
2907
  session->set_proc_info("Waiting for tables");
2908
2908
  pthread_mutex_lock(&LOCK_open);
2909
 
  while (!thd->killed)
 
2909
  while (!session->killed)
2910
2910
  {
2911
 
    thd->some_tables_deleted=0;
2912
 
    close_old_data_files(thd,thd->open_tables,0,dropping_tables != 0);
2913
 
    mysql_ha_flush(thd);
2914
 
    if (!table_is_used(thd->open_tables,1))
 
2911
    session->some_tables_deleted=0;
 
2912
    close_old_data_files(session,session->open_tables,0,dropping_tables != 0);
 
2913
    mysql_ha_flush(session);
 
2914
    if (!table_is_used(session->open_tables,1))
2915
2915
      break;
2916
2916
    (void) pthread_cond_wait(&COND_refresh,&LOCK_open);
2917
2917
  }
2918
 
  if (thd->killed)
 
2918
  if (session->killed)
2919
2919
    result= 1;                                  // aborted
2920
2920
  else
2921
2921
  {
2922
2922
    /* Now we can open all tables without any interference */
2923
 
    thd->set_proc_info("Reopen tables");
2924
 
    thd->version= refresh_version;
2925
 
    result=reopen_tables(thd,0,0);
 
2923
    session->set_proc_info("Reopen tables");
 
2924
    session->version= refresh_version;
 
2925
    result=reopen_tables(session,0,0);
2926
2926
  }
2927
2927
  pthread_mutex_unlock(&LOCK_open);
2928
 
  thd->set_proc_info(0);
 
2928
  session->set_proc_info(0);
2929
2929
  return(result);
2930
2930
}
2931
2931
 
2935
2935
 
2936
2936
  SYNOPSIS
2937
2937
    drop_locked_tables()
2938
 
    thd                 Thread thandler
 
2938
    session                     Thread thandler
2939
2939
    db                  Database
2940
2940
    table_name          Table name
2941
2941
 
2954
2954
*/
2955
2955
 
2956
2956
 
2957
 
Table *drop_locked_tables(Session *thd,const char *db, const char *table_name)
 
2957
Table *drop_locked_tables(Session *session,const char *db, const char *table_name)
2958
2958
{
2959
2959
  Table *table,*next,**prev, *found= 0;
2960
 
  prev= &thd->open_tables;
 
2960
  prev= &session->open_tables;
2961
2961
 
2962
2962
  /*
2963
2963
    Note that we need to hold LOCK_open while changing the
2966
2966
    Closing a MERGE child before the parent would be fatal if the
2967
2967
    other thread tries to abort the MERGE lock in between.
2968
2968
  */
2969
 
  for (table= thd->open_tables; table ; table=next)
 
2969
  for (table= session->open_tables; table ; table=next)
2970
2970
  {
2971
2971
    next=table->next;
2972
2972
    if (!strcmp(table->s->table_name.str, table_name) &&
2973
2973
        !strcmp(table->s->db.str, db))
2974
2974
    {
2975
 
      mysql_lock_remove(thd, thd->locked_tables, table, true);
 
2975
      mysql_lock_remove(session, session->locked_tables, table, true);
2976
2976
 
2977
2977
      if (!found)
2978
2978
      {
2999
2999
  *prev=0;
3000
3000
  if (found)
3001
3001
    broadcast_refresh();
3002
 
  if (thd->locked_tables && thd->locked_tables->table_count == 0)
 
3002
  if (session->locked_tables && session->locked_tables->table_count == 0)
3003
3003
  {
3004
 
    free((unsigned char*) thd->locked_tables);
3005
 
    thd->locked_tables=0;
 
3004
    free((unsigned char*) session->locked_tables);
 
3005
    session->locked_tables=0;
3006
3006
  }
3007
3007
  return(found);
3008
3008
}
3014
3014
  other threads trying to get the lock.
3015
3015
*/
3016
3016
 
3017
 
void abort_locked_tables(Session *thd,const char *db, const char *table_name)
 
3017
void abort_locked_tables(Session *session,const char *db, const char *table_name)
3018
3018
{
3019
3019
  Table *table;
3020
 
  for (table= thd->open_tables; table ; table= table->next)
 
3020
  for (table= session->open_tables; table ; table= table->next)
3021
3021
  {
3022
3022
    if (!strcmp(table->s->table_name.str, table_name) &&
3023
3023
        !strcmp(table->s->db.str, db))
3024
3024
    {
3025
3025
      /* If MERGE child, forward lock handling to parent. */
3026
 
      mysql_lock_abort(thd, table, true);
 
3026
      mysql_lock_abort(session, table, true);
3027
3027
      break;
3028
3028
    }
3029
3029
  }
3087
3087
 
3088
3088
  SYNOPSIS
3089
3089
    open_unireg_entry()
3090
 
    thd                 Thread handle
 
3090
    session                     Thread handle
3091
3091
    entry               Store open table definition here
3092
3092
    table_list          TableList with db, table_name
3093
3093
    alias               Alias name
3095
3095
    cache_key_length    length of cache_key
3096
3096
 
3097
3097
  NOTES
3098
 
   Extra argument for open is taken from thd->open_options
 
3098
   Extra argument for open is taken from session->open_options
3099
3099
   One must have a lock on LOCK_open when calling this function
3100
3100
 
3101
3101
  RETURN
3103
3103
    #   Error
3104
3104
*/
3105
3105
 
3106
 
static int open_unireg_entry(Session *thd, Table *entry, TableList *table_list,
 
3106
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
3107
3107
                             const char *alias,
3108
3108
                             char *cache_key, uint32_t cache_key_length)
3109
3109
{
3113
3113
 
3114
3114
  safe_mutex_assert_owner(&LOCK_open);
3115
3115
retry:
3116
 
  if (!(share= get_table_share_with_create(thd, table_list, cache_key,
 
3116
  if (!(share= get_table_share_with_create(session, table_list, cache_key,
3117
3117
                                           cache_key_length, 
3118
3118
                                           table_list->i_s_requested_object,
3119
3119
                                           &error)))
3120
3120
    return(1);
3121
3121
 
3122
 
  while ((error= open_table_from_share(thd, share, alias,
 
3122
  while ((error= open_table_from_share(session, share, alias,
3123
3123
                                       (uint) (HA_OPEN_KEYFILE |
3124
3124
                                               HA_OPEN_RNDFILE |
3125
3125
                                               HA_GET_INDEX |
3126
3126
                                               HA_TRY_READ_ONLY),
3127
3127
                                       (EXTRA_RECORD),
3128
 
                                       thd->open_options, entry, OTM_OPEN)))
 
3128
                                       session->open_options, entry, OTM_OPEN)))
3129
3129
  {
3130
3130
    if (error == 7)                             // Table def changed
3131
3131
    {
3146
3146
        - Start waiting that the share is released
3147
3147
        - Retry by opening all tables again
3148
3148
      */
3149
 
      if (ha_create_table_from_engine(thd, table_list->db,
 
3149
      if (ha_create_table_from_engine(session, table_list->db,
3150
3150
                                      table_list->table_name))
3151
3151
        goto err;
3152
3152
      /*
3158
3158
        goto err;
3159
3159
      /* Free share and wait until it's released by all threads */
3160
3160
      release_table_share(share, RELEASE_WAIT_FOR_DROP);
3161
 
      if (!thd->killed)
 
3161
      if (!session->killed)
3162
3162
      {
3163
 
        drizzle_reset_errors(thd, 1);         // Clear warnings
3164
 
        thd->clear_error();                 // Clear error message
 
3163
        drizzle_reset_errors(session, 1);         // Clear warnings
 
3164
        session->clear_error();                 // Clear error message
3165
3165
        goto retry;
3166
3166
      }
3167
3167
      return(1);
3169
3169
    if (!entry->s || !entry->s->crashed)
3170
3170
      goto err;
3171
3171
     // Code below is for repairing a crashed file
3172
 
     if ((error= lock_table_name(thd, table_list, true)))
 
3172
     if ((error= lock_table_name(session, table_list, true)))
3173
3173
     {
3174
3174
       if (error < 0)
3175
3175
        goto err;
3176
 
       if (wait_for_locked_table_names(thd, table_list))
 
3176
       if (wait_for_locked_table_names(session, table_list))
3177
3177
       {
3178
 
        unlock_table_name(thd, table_list);
 
3178
        unlock_table_name(session, table_list);
3179
3179
        goto err;
3180
3180
       }
3181
3181
     }
3182
3182
     pthread_mutex_unlock(&LOCK_open);
3183
 
     thd->clear_error();                                // Clear error message
 
3183
     session->clear_error();                            // Clear error message
3184
3184
     error= 0;
3185
 
     if (open_table_from_share(thd, share, alias,
 
3185
     if (open_table_from_share(session, share, alias,
3186
3186
                               (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3187
3187
                                       HA_GET_INDEX |
3188
3188
                                       HA_TRY_READ_ONLY),
3189
3189
                               EXTRA_RECORD,
3190
3190
                               ha_open_options | HA_OPEN_FOR_REPAIR,
3191
3191
                               entry, OTM_OPEN) || ! entry->file ||
3192
 
        (entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
 
3192
        (entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
3193
3193
     {
3194
3194
       /* Give right error message */
3195
 
       thd->clear_error();
 
3195
       session->clear_error();
3196
3196
       my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3197
3197
       sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
3198
3198
                       share->table_name.str);
3201
3201
       error=1;
3202
3202
     }
3203
3203
     else
3204
 
       thd->clear_error();                      // Clear error message
 
3204
       session->clear_error();                  // Clear error message
3205
3205
     pthread_mutex_lock(&LOCK_open);
3206
 
     unlock_table_name(thd, table_list);
 
3206
     unlock_table_name(session, table_list);
3207
3207
 
3208
3208
     if (error)
3209
3209
       goto err;
3226
3226
        /* this DELETE FROM is needed even with row-based binlogging */
3227
3227
        end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3228
3228
                      share->db.str,"`.`",share->table_name.str,"`", NULL);
3229
 
        thd->binlog_query(Session::STMT_QUERY_TYPE,
 
3229
        session->binlog_query(Session::STMT_QUERY_TYPE,
3230
3230
                          query, (ulong)(end-query), false, false);
3231
3231
        free(query);
3232
3232
      }
3258
3258
 
3259
3259
  SYNOPSIS
3260
3260
    open_tables()
3261
 
    thd - thread handler
 
3261
    session - thread handler
3262
3262
    start - list of tables in/out
3263
3263
    counter - number of opened tables will be return using this parameter
3264
3264
    flags   - bitmap of flags to modify how the tables will be open:
3281
3281
    -1 - error
3282
3282
*/
3283
3283
 
3284
 
int open_tables(Session *thd, TableList **start, uint32_t *counter, uint32_t flags)
 
3284
int open_tables(Session *session, TableList **start, uint32_t *counter, uint32_t flags)
3285
3285
{
3286
3286
  TableList *tables= NULL;
3287
3287
  bool refresh;
3296
3296
  */
3297
3297
  init_sql_alloc(&new_frm_mem, 8024, 8024);
3298
3298
 
3299
 
  thd->current_tablenr= 0;
 
3299
  session->current_tablenr= 0;
3300
3300
 restart:
3301
3301
  *counter= 0;
3302
 
  thd->set_proc_info("Opening tables");
 
3302
  session->set_proc_info("Opening tables");
3303
3303
 
3304
3304
  /*
3305
3305
    For every table in the list of tables to open, try to find or open
3327
3327
    */
3328
3328
    if (tables->schema_table)
3329
3329
    {
3330
 
      if (!mysql_schema_table(thd, thd->lex, tables))
 
3330
      if (!mysql_schema_table(session, session->lex, tables))
3331
3331
        continue;
3332
3332
      return(-1);
3333
3333
    }
3338
3338
      not opened yet. Try to open the table.
3339
3339
    */
3340
3340
    if (!tables->table)
3341
 
      tables->table= open_table(thd, tables, &refresh, flags);
 
3341
      tables->table= open_table(session, tables, &refresh, flags);
3342
3342
 
3343
3343
    if (!tables->table)
3344
3344
    {
3360
3360
          we pretend that we have finished calculation which we were doing
3361
3361
          currently.
3362
3362
        */
3363
 
        close_tables_for_reopen(thd, start);
 
3363
        close_tables_for_reopen(session, start);
3364
3364
        goto restart;
3365
3365
      }
3366
3366
 
3370
3370
      result= -1;                               // Fatal error
3371
3371
      break;
3372
3372
    }
3373
 
    if (tables->lock_type != TL_UNLOCK && ! thd->locked_tables)
 
3373
    if (tables->lock_type != TL_UNLOCK && ! session->locked_tables)
3374
3374
    {
3375
3375
      if (tables->lock_type == TL_WRITE_DEFAULT)
3376
 
        tables->table->reginfo.lock_type= thd->update_lock_default;
 
3376
        tables->table->reginfo.lock_type= session->update_lock_default;
3377
3377
      else if (tables->table->s->tmp_table == NO_TMP_TABLE)
3378
3378
        tables->table->reginfo.lock_type= tables->lock_type;
3379
3379
    }
3380
3380
  }
3381
3381
 
3382
 
  thd->set_proc_info(0);
 
3382
  session->set_proc_info(0);
3383
3383
  free_root(&new_frm_mem, MYF(0));              // Free pre-alloced block
3384
3384
 
3385
3385
  if (result && tables)
3386
3386
  {
3387
3387
    /*
3388
3388
      Some functions determine success as (tables->table != NULL).
3389
 
      tables->table is in thd->open_tables.
 
3389
      tables->table is in session->open_tables.
3390
3390
    */
3391
3391
    tables->table= NULL;
3392
3392
  }
3399
3399
 
3400
3400
  SYNOPSIS
3401
3401
    check_lock_and_start_stmt()
3402
 
    thd                 Thread handle
 
3402
    session                     Thread handle
3403
3403
    table_list          Table to check
3404
3404
    lock_type           Lock used for table
3405
3405
 
3408
3408
  1     error
3409
3409
*/
3410
3410
 
3411
 
static bool check_lock_and_start_stmt(Session *thd, Table *table,
 
3411
static bool check_lock_and_start_stmt(Session *session, Table *table,
3412
3412
                                      thr_lock_type lock_type)
3413
3413
{
3414
3414
  int error;
3419
3419
    my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3420
3420
    return(1);
3421
3421
  }
3422
 
  if ((error=table->file->start_stmt(thd, lock_type)))
 
3422
  if ((error=table->file->start_stmt(session, lock_type)))
3423
3423
  {
3424
3424
    table->file->print_error(error,MYF(0));
3425
3425
    return(1);
3431
3431
/**
3432
3432
  @brief Open and lock one table
3433
3433
 
3434
 
  @param[in]    thd             thread handle
 
3434
  @param[in]    session             thread handle
3435
3435
  @param[in]    table_l         table to open is first table in this list
3436
3436
  @param[in]    lock_type       lock to use for table
3437
3437
 
3458
3458
    and locking issues because it does not call lock_tables().
3459
3459
*/
3460
3460
 
3461
 
Table *open_n_lock_single_table(Session *thd, TableList *table_l,
 
3461
Table *open_n_lock_single_table(Session *session, TableList *table_l,
3462
3462
                                thr_lock_type lock_type)
3463
3463
{
3464
3464
  TableList *save_next_global;
3472
3472
  table_l->lock_type= lock_type;
3473
3473
 
3474
3474
  /* Open the table. */
3475
 
  if (simple_open_n_lock_tables(thd, table_l))
 
3475
  if (simple_open_n_lock_tables(session, table_l))
3476
3476
    table_l->table= NULL; /* Just to be sure. */
3477
3477
 
3478
3478
  /* Restore list. */
3487
3487
 
3488
3488
  SYNOPSIS
3489
3489
    open_ltable()
3490
 
    thd                 Thread handler
 
3490
    session                     Thread handler
3491
3491
    table_list          Table to open is first table in this list
3492
3492
    lock_type           Lock to use for open
3493
3493
    lock_flags          Flags passed to mysql_lock_table
3506
3506
      table_list->table         table
3507
3507
*/
3508
3508
 
3509
 
Table *open_ltable(Session *thd, TableList *table_list, thr_lock_type lock_type,
 
3509
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type lock_type,
3510
3510
                   uint32_t lock_flags)
3511
3511
{
3512
3512
  Table *table;
3513
3513
  bool refresh;
3514
3514
 
3515
 
  thd->set_proc_info("Opening table");
3516
 
  thd->current_tablenr= 0;
3517
 
  while (!(table= open_table(thd, table_list, &refresh, 0)) &&
 
3515
  session->set_proc_info("Opening table");
 
3516
  session->current_tablenr= 0;
 
3517
  while (!(table= open_table(session, table_list, &refresh, 0)) &&
3518
3518
         refresh)
3519
3519
    ;
3520
3520
 
3522
3522
  {
3523
3523
    table_list->lock_type= lock_type;
3524
3524
    table_list->table=     table;
3525
 
    if (thd->locked_tables)
 
3525
    if (session->locked_tables)
3526
3526
    {
3527
 
      if (check_lock_and_start_stmt(thd, table, lock_type))
 
3527
      if (check_lock_and_start_stmt(session, table, lock_type))
3528
3528
        table= 0;
3529
3529
    }
3530
3530
    else
3531
3531
    {
3532
 
      assert(thd->lock == 0);   // You must lock everything at once
 
3532
      assert(session->lock == 0);       // You must lock everything at once
3533
3533
      if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3534
 
        if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
 
3534
        if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
3535
3535
                                            lock_flags, &refresh)))
3536
3536
          table= 0;
3537
3537
    }
3538
3538
  }
3539
3539
 
3540
 
  thd->set_proc_info(0);
 
3540
  session->set_proc_info(0);
3541
3541
  return(table);
3542
3542
}
3543
3543
 
3547
3547
 
3548
3548
  SYNOPSIS
3549
3549
    open_and_lock_tables_derived()
3550
 
    thd         - thread handler
 
3550
    session             - thread handler
3551
3551
    tables      - list of tables for open&locking
3552
3552
    derived     - if to handle derived tables
3553
3553
 
3560
3560
 
3561
3561
  NOTE
3562
3562
    There are two convenience functions:
3563
 
    - simple_open_n_lock_tables(thd, tables)  without derived handling
3564
 
    - open_and_lock_tables(thd, tables)       with derived handling
 
3563
    - simple_open_n_lock_tables(session, tables)  without derived handling
 
3564
    - open_and_lock_tables(session, tables)       with derived handling
3565
3565
    Both inline functions call open_and_lock_tables_derived() with
3566
3566
    the third argument set appropriately.
3567
3567
*/
3568
3568
 
3569
 
int open_and_lock_tables_derived(Session *thd, TableList *tables, bool derived)
 
3569
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3570
3570
{
3571
3571
  uint32_t counter;
3572
3572
  bool need_reopen;
3573
3573
 
3574
3574
  for ( ; ; ) 
3575
3575
  {
3576
 
    if (open_tables(thd, &tables, &counter, 0))
 
3576
    if (open_tables(session, &tables, &counter, 0))
3577
3577
      return(-1);
3578
3578
 
3579
 
    if (!lock_tables(thd, tables, counter, &need_reopen))
 
3579
    if (!lock_tables(session, tables, counter, &need_reopen))
3580
3580
      break;
3581
3581
    if (!need_reopen)
3582
3582
      return(-1);
3583
 
    close_tables_for_reopen(thd, &tables);
 
3583
    close_tables_for_reopen(session, &tables);
3584
3584
  }
3585
3585
  if (derived &&
3586
 
      (mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3587
 
       (thd->fill_derived_tables() &&
3588
 
        mysql_handle_derived(thd->lex, &mysql_derived_filling))))
 
3586
      (mysql_handle_derived(session->lex, &mysql_derived_prepare) ||
 
3587
       (session->fill_derived_tables() &&
 
3588
        mysql_handle_derived(session->lex, &mysql_derived_filling))))
3589
3589
    return(true); /* purecov: inspected */
3590
3590
  return(0);
3591
3591
}
3596
3596
 
3597
3597
  SYNOPSIS
3598
3598
    open_normal_and_derived_tables
3599
 
    thd         - thread handler
 
3599
    session             - thread handler
3600
3600
    tables      - list of tables for open
3601
3601
    flags       - bitmap of flags to modify how the tables will be open:
3602
3602
                  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3611
3611
    data from the tables.
3612
3612
*/
3613
3613
 
3614
 
bool open_normal_and_derived_tables(Session *thd, TableList *tables, uint32_t flags)
 
3614
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
3615
3615
{
3616
3616
  uint32_t counter;
3617
 
  assert(!thd->fill_derived_tables());
3618
 
  if (open_tables(thd, &tables, &counter, flags) ||
3619
 
      mysql_handle_derived(thd->lex, &mysql_derived_prepare))
 
3617
  assert(!session->fill_derived_tables());
 
3618
  if (open_tables(session, &tables, &counter, flags) ||
 
3619
      mysql_handle_derived(session->lex, &mysql_derived_prepare))
3620
3620
    return(true); /* purecov: inspected */
3621
3621
  return(0);
3622
3622
}
3655
3655
 
3656
3656
   3. Otherwise, statement-based logging is used.
3657
3657
 
3658
 
   @param thd    Client thread
 
3658
   @param session    Client thread
3659
3659
   @param tables Tables involved in the query
3660
3660
 */
3661
3661
 
3662
 
int decide_logging_format(Session *thd, TableList *tables)
 
3662
int decide_logging_format(Session *session, TableList *tables)
3663
3663
{
3664
 
  if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
 
3664
  if (mysql_bin_log.is_open() && (session->options & OPTION_BIN_LOG))
3665
3665
  {
3666
3666
    handler::Table_flags flags_some_set= handler::Table_flags();
3667
3667
    handler::Table_flags flags_all_set= ~handler::Table_flags();
3687
3687
               "Statement cannot be logged to the binary log in"
3688
3688
               " row-based nor statement-based format");
3689
3689
    }
3690
 
    else if (thd->variables.binlog_format == BINLOG_FORMAT_STMT &&
 
3690
    else if (session->variables.binlog_format == BINLOG_FORMAT_STMT &&
3691
3691
             (flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3692
3692
    {
3693
3693
      my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3694
3694
                "Statement-based format required for this statement,"
3695
3695
                " but not allowed by this combination of engines");
3696
3696
    }
3697
 
    else if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW ||
3698
 
              thd->lex->is_stmt_unsafe()) &&
 
3697
    else if ((session->variables.binlog_format == BINLOG_FORMAT_ROW ||
 
3698
              session->lex->is_stmt_unsafe()) &&
3699
3699
             (flags_all_set & HA_BINLOG_ROW_CAPABLE) == 0)
3700
3700
    {
3701
3701
      my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3718
3718
      this code in reset_current_stmt_binlog_row_based(), it has to be
3719
3719
      here.
3720
3720
    */
3721
 
    if (thd->lex->is_stmt_unsafe() ||
 
3721
    if (session->lex->is_stmt_unsafe() ||
3722
3722
        (flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3723
3723
    {
3724
 
      thd->set_current_stmt_binlog_row_based_if_mixed();
 
3724
      session->set_current_stmt_binlog_row_based_if_mixed();
3725
3725
    }
3726
3726
  }
3727
3727
 
3733
3733
 
3734
3734
  SYNOPSIS
3735
3735
    lock_tables()
3736
 
    thd                 Thread handler
 
3736
    session                     Thread handler
3737
3737
    tables              Tables to lock
3738
3738
    count               Number of opened tables
3739
3739
    need_reopen         Out parameter which if true indicates that some
3749
3749
 
3750
3750
    If query for which we are calling this function marked as requring
3751
3751
    prelocking, this function will do implicit LOCK TABLES and change
3752
 
    thd::prelocked_mode accordingly.
 
3752
    session::prelocked_mode accordingly.
3753
3753
 
3754
3754
  RETURN VALUES
3755
3755
   0    ok
3756
3756
   -1   Error
3757
3757
*/
3758
3758
 
3759
 
int lock_tables(Session *thd, TableList *tables, uint32_t count, bool *need_reopen)
 
3759
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
3760
3760
{
3761
3761
  TableList *table;
3762
3762
 
3767
3767
  *need_reopen= false;
3768
3768
 
3769
3769
  if (!tables)
3770
 
    return(decide_logging_format(thd, tables));
 
3770
    return(decide_logging_format(session, tables));
3771
3771
 
3772
 
  if (!thd->locked_tables)
 
3772
  if (!session->locked_tables)
3773
3773
  {
3774
 
    assert(thd->lock == 0);     // You must lock everything at once
 
3774
    assert(session->lock == 0); // You must lock everything at once
3775
3775
    Table **start,**ptr;
3776
3776
    uint32_t lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
3777
3777
 
3778
 
    if (!(ptr=start=(Table**) thd->alloc(sizeof(Table*)*count)))
 
3778
    if (!(ptr=start=(Table**) session->alloc(sizeof(Table*)*count)))
3779
3779
      return(-1);
3780
3780
    for (table= tables; table; table= table->next_global)
3781
3781
    {
3783
3783
        *(ptr++)= table->table;
3784
3784
    }
3785
3785
 
3786
 
    if (!(thd->lock= mysql_lock_tables(thd, start, (uint) (ptr - start),
 
3786
    if (!(session->lock= mysql_lock_tables(session, start, (uint) (ptr - start),
3787
3787
                                       lock_flag, need_reopen)))
3788
3788
    {
3789
3789
      return(-1);
3791
3791
  }
3792
3792
  else
3793
3793
  {
3794
 
    TableList *first_not_own= thd->lex->first_not_own_table();
 
3794
    TableList *first_not_own= session->lex->first_not_own_table();
3795
3795
    /*
3796
3796
      When open_and_lock_tables() is called for a single table out of
3797
3797
      a table list, the 'next_global' chain is temporarily broken. We
3805
3805
         table= table->next_global)
3806
3806
    {
3807
3807
      if (!table->placeholder() &&
3808
 
          check_lock_and_start_stmt(thd, table->table, table->lock_type))
 
3808
          check_lock_and_start_stmt(session, table->table, table->lock_type))
3809
3809
      {
3810
3810
        return(-1);
3811
3811
      }
3812
3812
    }
3813
3813
  }
3814
3814
 
3815
 
  return(decide_logging_format(thd, tables));
 
3815
  return(decide_logging_format(session, tables));
3816
3816
}
3817
3817
 
3818
3818
 
3822
3822
 
3823
3823
  SYNOPSIS
3824
3824
    close_tables_for_reopen()
3825
 
      thd    in     Thread context
 
3825
      session    in     Thread context
3826
3826
      tables in/out List of tables which we were trying to open and lock
3827
3827
 
3828
3828
*/
3829
3829
 
3830
 
void close_tables_for_reopen(Session *thd, TableList **tables)
 
3830
void close_tables_for_reopen(Session *session, TableList **tables)
3831
3831
{
3832
3832
  /*
3833
3833
    If table list consists only from tables from prelocking set, table list
3834
3834
    for new attempt should be empty, so we have to update list's root pointer.
3835
3835
  */
3836
 
  if (thd->lex->first_not_own_table() == *tables)
 
3836
  if (session->lex->first_not_own_table() == *tables)
3837
3837
    *tables= 0;
3838
 
  thd->lex->chop_off_not_own_tables();
 
3838
  session->lex->chop_off_not_own_tables();
3839
3839
  for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
3840
3840
    tmp->table= 0;
3841
 
  close_thread_tables(thd);
 
3841
  close_thread_tables(session);
3842
3842
}
3843
3843
 
3844
3844
 
3847
3847
 
3848
3848
  SYNPOSIS
3849
3849
    open_temporary_table()
3850
 
    thd           Thread object
 
3850
    session               Thread object
3851
3851
    path          Path (without .frm)
3852
3852
    db            database
3853
3853
    table_name    Table name
3854
 
    link_in_list  1 if table should be linked into thd->temporary_tables
 
3854
    link_in_list  1 if table should be linked into session->temporary_tables
3855
3855
 
3856
3856
 NOTES:
3857
3857
    Used by alter_table to open a temporary table and when creating
3862
3862
   #  Table object
3863
3863
*/
3864
3864
 
3865
 
Table *open_temporary_table(Session *thd, const char *path, const char *db,
 
3865
Table *open_temporary_table(Session *session, const char *path, const char *db,
3866
3866
                            const char *table_name, bool link_in_list,
3867
3867
                            open_table_mode open_mode)
3868
3868
{
3875
3875
  table_list.db=         (char*) db;
3876
3876
  table_list.table_name= (char*) table_name;
3877
3877
  /* Create the cache_key for temporary tables */
3878
 
  key_length= create_table_def_key(thd, cache_key, &table_list, 1);
 
3878
  key_length= create_table_def_key(session, cache_key, &table_list, 1);
3879
3879
 
3880
3880
  if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3881
3881
                                      strlen(path)+1 + key_length,
3887
3887
  saved_cache_key= my_stpcpy(tmp_path, path)+1;
3888
3888
  memcpy(saved_cache_key, cache_key, key_length);
3889
3889
 
3890
 
  init_tmp_table_share(thd, share, saved_cache_key, key_length,
 
3890
  init_tmp_table_share(session, share, saved_cache_key, key_length,
3891
3891
                       strchr(saved_cache_key, '\0')+1, tmp_path);
3892
3892
 
3893
 
  if (open_table_def(thd, share, 0) ||
3894
 
      open_table_from_share(thd, share, table_name,
 
3893
  if (open_table_def(session, share, 0) ||
 
3894
      open_table_from_share(session, share, table_name,
3895
3895
                            (open_mode == OTM_ALTER) ? 0 :
3896
3896
                            (uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3897
3897
                                    HA_GET_INDEX),
3923
3923
  if (link_in_list)
3924
3924
  {
3925
3925
    /* growing temp list at the head */
3926
 
    tmp_table->next= thd->temporary_tables;
 
3926
    tmp_table->next= session->temporary_tables;
3927
3927
    if (tmp_table->next)
3928
3928
      tmp_table->next->prev= tmp_table;
3929
 
    thd->temporary_tables= tmp_table;
3930
 
    thd->temporary_tables->prev= 0;
3931
 
    if (thd->slave_thread)
 
3929
    session->temporary_tables= tmp_table;
 
3930
    session->temporary_tables->prev= 0;
 
3931
    if (session->slave_thread)
3932
3932
      slave_open_temp_tables++;
3933
3933
  }
3934
3934
  tmp_table->pos_in_table_list= 0;
3946
3946
  if (my_delete(path,MYF(0)))
3947
3947
    error=1; /* purecov: inspected */
3948
3948
  *ext= 0;                              // remove extension
3949
 
  file= get_new_handler((TABLE_SHARE*) 0, current_thd->mem_root, base);
 
3949
  file= get_new_handler((TABLE_SHARE*) 0, current_session->mem_root, base);
3950
3950
  if (!frm_only && file && file->ha_delete_table(path))
3951
3951
  {
3952
3952
    error=1;
3973
3973
 
3974
3974
#define WRONG_GRANT (Field*) -1
3975
3975
 
3976
 
static void update_field_dependencies(Session *thd, Field *field, Table *table)
 
3976
static void update_field_dependencies(Session *session, Field *field, Table *table)
3977
3977
{
3978
 
  if (thd->mark_used_columns != MARK_COLUMNS_NONE)
 
3978
  if (session->mark_used_columns != MARK_COLUMNS_NONE)
3979
3979
  {
3980
3980
    MY_BITMAP *current_bitmap, *other_bitmap;
3981
3981
 
3987
3987
    table->covering_keys.intersect(field->part_of_key);
3988
3988
    table->merge_keys.merge(field->part_of_key);
3989
3989
 
3990
 
    if (thd->mark_used_columns == MARK_COLUMNS_READ)
 
3990
    if (session->mark_used_columns == MARK_COLUMNS_READ)
3991
3991
    {
3992
3992
      current_bitmap= table->read_set;
3993
3993
      other_bitmap=   table->write_set;
4000
4000
 
4001
4001
    if (bitmap_fast_test_and_set(current_bitmap, field->field_index))
4002
4002
    {
4003
 
      if (thd->mark_used_columns == MARK_COLUMNS_WRITE)
4004
 
        thd->dup_field= field;
 
4003
      if (session->mark_used_columns == MARK_COLUMNS_WRITE)
 
4004
        session->dup_field= field;
4005
4005
      return;
4006
4006
    }
4007
4007
    if (table->get_fields_in_item_tree)
4019
4019
 
4020
4020
  SYNOPSIS
4021
4021
    find_field_in_natural_join()
4022
 
    thd                  [in]  thread handler
 
4022
    session                      [in]  thread handler
4023
4023
    table_ref            [in]  table reference to search
4024
4024
    name                 [in]  name of field
4025
4025
    length               [in]  length of name
4044
4044
*/
4045
4045
 
4046
4046
static Field *
4047
 
find_field_in_natural_join(Session *thd, TableList *table_ref, const char *name,
 
4047
find_field_in_natural_join(Session *session, TableList *table_ref, const char *name,
4048
4048
                           uint32_t length __attribute__((unused)),
4049
4049
                           Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4050
4050
                           TableList **actual_table)
4064
4064
    {
4065
4065
      if (nj_col)
4066
4066
      {
4067
 
        my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
 
4067
        my_error(ER_NON_UNIQ_ERROR, MYF(0), name, session->where);
4068
4068
        return(NULL);
4069
4069
      }
4070
4070
      nj_col= curr_nj_col;
4076
4076
    /* This is a base table. */
4077
4077
    assert(nj_col->table_ref->table == nj_col->table_field->table);
4078
4078
    found_field= nj_col->table_field;
4079
 
    update_field_dependencies(thd, found_field, nj_col->table_ref->table);
 
4079
    update_field_dependencies(session, found_field, nj_col->table_ref->table);
4080
4080
  }
4081
4081
 
4082
4082
  *actual_table= nj_col->table_ref;
4090
4090
 
4091
4091
  SYNOPSIS
4092
4092
    find_field_in_table()
4093
 
    thd                         thread handler
 
4093
    session                             thread handler
4094
4094
    table                       table where to search for the field
4095
4095
    name                        name of field
4096
4096
    length                      length of name
4104
4104
*/
4105
4105
 
4106
4106
Field *
4107
 
find_field_in_table(Session *thd, Table *table, const char *name, uint32_t length,
 
4107
find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
4108
4108
                    bool allow_rowid, uint32_t *cached_field_index_ptr)
4109
4109
{
4110
4110
  Field **field_ptr, *field;
4141
4141
  {
4142
4142
    if ((*field_ptr)->vcol_info)
4143
4143
    {
4144
 
      if (thd->mark_used_columns != MARK_COLUMNS_NONE)
 
4144
      if (session->mark_used_columns != MARK_COLUMNS_NONE)
4145
4145
      {
4146
4146
        Item *vcol_item= (*field_ptr)->vcol_info->expr_item;
4147
4147
        assert(vcol_item);
4151
4151
          1) this procedure is called for a read-only operation (SELECT), and
4152
4152
          2) the virtual column is not phycically stored in the table
4153
4153
        */
4154
 
        if ((thd->mark_used_columns != MARK_COLUMNS_WRITE) && 
 
4154
        if ((session->mark_used_columns != MARK_COLUMNS_WRITE) && 
4155
4155
            (not (*field_ptr)->is_stored))
4156
4156
          bitmap_set_bit((*field_ptr)->table->write_set, (*field_ptr)->field_index);
4157
4157
      }
4168
4168
    field= table->field[table->s->rowid_field_offset-1];
4169
4169
  }
4170
4170
 
4171
 
  update_field_dependencies(thd, field, table);
 
4171
  update_field_dependencies(session, field, table);
4172
4172
 
4173
4173
  return(field);
4174
4174
}
4179
4179
 
4180
4180
  SYNOPSIS
4181
4181
    find_field_in_table_ref()
4182
 
    thd                    [in]  thread handler
 
4182
    session                        [in]  thread handler
4183
4183
    table_list             [in]  table reference to search
4184
4184
    name                   [in]  name of field
4185
4185
    length                 [in]  field length of name
4217
4217
*/
4218
4218
 
4219
4219
Field *
4220
 
find_field_in_table_ref(Session *thd, TableList *table_list,
 
4220
find_field_in_table_ref(Session *session, TableList *table_list,
4221
4221
                        const char *name, uint32_t length,
4222
4222
                        const char *item_name, const char *db_name,
4223
4223
                        const char *table_name, Item **ref,
4271
4271
  {
4272
4272
    /* 'table_list' is a stored table. */
4273
4273
    assert(table_list->table);
4274
 
    if ((fld= find_field_in_table(thd, table_list->table, name, length,
 
4274
    if ((fld= find_field_in_table(session, table_list->table, name, length,
4275
4275
                                  allow_rowid,
4276
4276
                                  cached_field_index_ptr)))
4277
4277
      *actual_table= table_list;
4291
4291
      TableList *table;
4292
4292
      while ((table= it++))
4293
4293
      {
4294
 
        if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
 
4294
        if ((fld= find_field_in_table_ref(session, table, name, length, item_name,
4295
4295
                                          db_name, table_name, ref,
4296
4296
                                          check_privileges, allow_rowid,
4297
4297
                                          cached_field_index_ptr,
4306
4306
      natural join, thus if the field is not qualified, we will search
4307
4307
      directly the top-most NATURAL/USING join.
4308
4308
    */
4309
 
    fld= find_field_in_natural_join(thd, table_list, name, length, ref,
 
4309
    fld= find_field_in_natural_join(session, table_list, name, length, ref,
4310
4310
                                    register_tree_change, actual_table);
4311
4311
  }
4312
4312
 
4313
4313
  if (fld)
4314
4314
  {
4315
 
      if (thd->mark_used_columns != MARK_COLUMNS_NONE)
 
4315
      if (session->mark_used_columns != MARK_COLUMNS_NONE)
4316
4316
      {
4317
4317
        /*
4318
4318
          Get rw_set correct for this field so that the handler
4327
4327
            field_to_set= ((Item_field*)it)->field;
4328
4328
          else
4329
4329
          {
4330
 
            if (thd->mark_used_columns == MARK_COLUMNS_READ)
 
4330
            if (session->mark_used_columns == MARK_COLUMNS_READ)
4331
4331
              it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4332
4332
          }
4333
4333
        }
4336
4336
        if (field_to_set)
4337
4337
        {
4338
4338
          Table *table= field_to_set->table;
4339
 
          if (thd->mark_used_columns == MARK_COLUMNS_READ)
 
4339
          if (session->mark_used_columns == MARK_COLUMNS_READ)
4340
4340
            bitmap_set_bit(table->read_set, field_to_set->field_index);
4341
4341
          else
4342
4342
            bitmap_set_bit(table->write_set, field_to_set->field_index);
4398
4398
 
4399
4399
  SYNOPSIS
4400
4400
    find_field_in_tables()
4401
 
    thd                   pointer to current thread structure
 
4401
    session                       pointer to current thread structure
4402
4402
    item                  field item that should be found
4403
4403
    first_table           list of tables to be searched for item
4404
4404
    last_table            end of the list of tables to search for item. If NULL
4428
4428
*/
4429
4429
 
4430
4430
Field *
4431
 
find_field_in_tables(Session *thd, Item_ident *item,
 
4431
find_field_in_tables(Session *session, Item_ident *item,
4432
4432
                     TableList *first_table, TableList *last_table,
4433
4433
                     Item **ref, find_item_error_report_type report_error,
4434
4434
                     bool check_privileges, bool register_tree_change)
4469
4469
      when table_ref->field_translation != NULL.
4470
4470
      */
4471
4471
    if (table_ref->table)
4472
 
      found= find_field_in_table(thd, table_ref->table, name, length,
 
4472
      found= find_field_in_table(session, table_ref->table, name, length,
4473
4473
                                 true, &(item->cached_field_index));
4474
4474
    else
4475
 
      found= find_field_in_table_ref(thd, table_ref, name, length, item->name,
 
4475
      found= find_field_in_table_ref(session, table_ref, name, length, item->name,
4476
4476
                                     NULL, NULL, ref, check_privileges,
4477
4477
                                     true, &(item->cached_field_index),
4478
4478
                                     register_tree_change,
4487
4487
        fields.
4488
4488
      */
4489
4489
      {
4490
 
        SELECT_LEX *current_sel= thd->lex->current_select;
 
4490
        SELECT_LEX *current_sel= session->lex->current_select;
4491
4491
        SELECT_LEX *last_select= table_ref->select_lex;
4492
4492
        /*
4493
4493
          If the field was an outer referencee, mark all selects using this
4494
4494
          sub query as dependent on the outer query
4495
4495
        */
4496
4496
        if (current_sel != last_select)
4497
 
          mark_select_range_as_dependent(thd, last_select, current_sel,
 
4497
          mark_select_range_as_dependent(session, last_select, current_sel,
4498
4498
                                         found, *ref, item);
4499
4499
      }
4500
4500
      return found;
4519
4519
  for (; cur_table != last_table ;
4520
4520
       cur_table= cur_table->next_name_resolution_table)
4521
4521
  {
4522
 
    Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length,
 
4522
    Field *cur_field= find_field_in_table_ref(session, cur_table, name, length,
4523
4523
                                              item->name, db, table_name, ref,
4524
 
                                              (thd->lex->sql_command ==
 
4524
                                              (session->lex->sql_command ==
4525
4525
                                               SQLCOM_SHOW_FIELDS)
4526
4526
                                              ? false : check_privileges,
4527
4527
                                              allow_rowid,
4532
4532
    {
4533
4533
      if (cur_field == WRONG_GRANT)
4534
4534
      {
4535
 
        if (thd->lex->sql_command != SQLCOM_SHOW_FIELDS)
 
4535
        if (session->lex->sql_command != SQLCOM_SHOW_FIELDS)
4536
4536
          return (Field*) 0;
4537
4537
 
4538
 
        thd->clear_error();
4539
 
        cur_field= find_field_in_table_ref(thd, cur_table, name, length,
 
4538
        session->clear_error();
 
4539
        cur_field= find_field_in_table_ref(session, cur_table, name, length,
4540
4540
                                           item->name, db, table_name, ref,
4541
4541
                                           false,
4542
4542
                                           allow_rowid,
4560
4560
      item->cached_table= (!actual_table->cacheable_table || found) ?
4561
4561
                          0 : actual_table;
4562
4562
 
4563
 
      assert(thd->where);
 
4563
      assert(session->where);
4564
4564
      /*
4565
4565
        If we found a fully qualified field we return it directly as it can't
4566
4566
        have duplicates.
4573
4573
        if (report_error == REPORT_ALL_ERRORS ||
4574
4574
            report_error == IGNORE_EXCEPT_NON_UNIQUE)
4575
4575
          my_error(ER_NON_UNIQ_ERROR, MYF(0),
4576
 
                   table_name ? item->full_name() : name, thd->where);
 
4576
                   table_name ? item->full_name() : name, session->where);
4577
4577
        return (Field*) 0;
4578
4578
      }
4579
4579
      found= cur_field;
4600
4600
      strxnmov(buff,sizeof(buff)-1,db,".",table_name,NULL);
4601
4601
      table_name=buff;
4602
4602
    }
4603
 
    my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
 
4603
    my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where);
4604
4604
  }
4605
4605
  else
4606
4606
  {
4607
4607
    if (report_error == REPORT_ALL_ERRORS ||
4608
4608
        report_error == REPORT_EXCEPT_NON_UNIQUE)
4609
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
 
4609
      my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where);
4610
4610
    else
4611
4611
      found= not_found_field;
4612
4612
  }
4732
4732
            */
4733
4733
            if (report_error != IGNORE_ERRORS)
4734
4734
              my_error(ER_NON_UNIQ_ERROR, MYF(0),
4735
 
                       find->full_name(), current_thd->where);
 
4735
                       find->full_name(), current_session->where);
4736
4736
            return (Item**) 0;
4737
4737
          }
4738
4738
          found_unaliased= li.ref();
4763
4763
              continue;                           // Same field twice
4764
4764
            if (report_error != IGNORE_ERRORS)
4765
4765
              my_error(ER_NON_UNIQ_ERROR, MYF(0),
4766
 
                       find->full_name(), current_thd->where);
 
4766
                       find->full_name(), current_session->where);
4767
4767
            return (Item**) 0;
4768
4768
          }
4769
4769
          found= li.ref();
4844
4844
    {
4845
4845
      if (report_error != IGNORE_ERRORS)
4846
4846
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
4847
 
                 find->full_name(), current_thd->where);
 
4847
                 find->full_name(), current_session->where);
4848
4848
      return (Item **) 0;
4849
4849
    }
4850
4850
    if (found_unaliased)
4860
4860
  {
4861
4861
    if (report_error == REPORT_ALL_ERRORS)
4862
4862
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
4863
 
               find->full_name(), current_thd->where);
 
4863
               find->full_name(), current_session->where);
4864
4864
    return (Item **) 0;
4865
4865
  }
4866
4866
  else
4908
4908
 
4909
4909
  SYNOPSIS
4910
4910
    set_new_item_local_context()
4911
 
    thd        pointer to current thread
 
4911
    session        pointer to current thread
4912
4912
    item       item for which new context is created and set
4913
4913
    table_ref  table ref where an item showld be resolved
4914
4914
 
4922
4922
*/
4923
4923
 
4924
4924
static bool
4925
 
set_new_item_local_context(Session *thd, Item_ident *item, TableList *table_ref)
 
4925
set_new_item_local_context(Session *session, Item_ident *item, TableList *table_ref)
4926
4926
{
4927
4927
  Name_resolution_context *context;
4928
 
  if (!(context= new (thd->mem_root) Name_resolution_context))
 
4928
  if (!(context= new (session->mem_root) Name_resolution_context))
4929
4929
    return true;
4930
4930
  context->init();
4931
4931
  context->first_name_resolution_table=
4940
4940
 
4941
4941
  SYNOPSIS
4942
4942
    mark_common_columns()
4943
 
    thd                [in] current thread
 
4943
    session                [in] current thread
4944
4944
    table_ref_1        [in] the first (left) join operand
4945
4945
    table_ref_2        [in] the second (right) join operand
4946
4946
    using_fields       [in] if the join is JOIN...USING - the join columns,
4967
4967
*/
4968
4968
 
4969
4969
static bool
4970
 
mark_common_columns(Session *thd, TableList *table_ref_1, TableList *table_ref_2,
 
4970
mark_common_columns(Session *session, TableList *table_ref_1, TableList *table_ref_2,
4971
4971
                    List<String> *using_fields, uint32_t *found_using_fields)
4972
4972
{
4973
4973
  Field_iterator_table_ref it_1, it_2;
5031
5031
        if (cur_nj_col_2->is_common ||
5032
5032
            (found && (!using_fields || is_using_column_1)))
5033
5033
        {
5034
 
          my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, thd->where);
 
5034
          my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where);
5035
5035
          goto err;
5036
5036
        }
5037
5037
        nj_col_2= cur_nj_col_2;
5057
5057
    */
5058
5058
    if (nj_col_2 && (!using_fields ||is_using_column_1))
5059
5059
    {
5060
 
      Item *item_1=   nj_col_1->create_item(thd);
5061
 
      Item *item_2=   nj_col_2->create_item(thd);
 
5060
      Item *item_1=   nj_col_1->create_item(session);
 
5061
      Item *item_2=   nj_col_2->create_item(session);
5062
5062
      Field *field_1= nj_col_1->field();
5063
5063
      Field *field_2= nj_col_2->field();
5064
5064
      Item_ident *item_ident_1, *item_ident_2;
5088
5088
        resolution of these items, and to enable proper name resolution of
5089
5089
        the items during the execute phase of PS.
5090
5090
      */
5091
 
      if (set_new_item_local_context(thd, item_ident_1, nj_col_1->table_ref) ||
5092
 
          set_new_item_local_context(thd, item_ident_2, nj_col_2->table_ref))
 
5091
      if (set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref) ||
 
5092
          set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref))
5093
5093
        goto err;
5094
5094
 
5095
5095
      if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
5150
5150
 
5151
5151
  SYNOPSIS
5152
5152
    store_natural_using_join_columns()
5153
 
    thd                current thread
 
5153
    session                current thread
5154
5154
    natural_using_join the table reference of the NATURAL/USING join
5155
5155
    table_ref_1        the first (left) operand (of a NATURAL/USING join).
5156
5156
    table_ref_2        the second (right) operand (of a NATURAL/USING join).
5181
5181
*/
5182
5182
 
5183
5183
static bool
5184
 
store_natural_using_join_columns(Session *thd __attribute__((unused)),
 
5184
store_natural_using_join_columns(Session *session __attribute__((unused)),
5185
5185
                                 TableList *natural_using_join,
5186
5186
                                 TableList *table_ref_1,
5187
5187
                                 TableList *table_ref_2,
5235
5235
        if (!(common_field= it++))
5236
5236
        {
5237
5237
          my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
5238
 
                   current_thd->where);
 
5238
                   current_session->where);
5239
5239
          goto err;
5240
5240
        }
5241
5241
        if (!my_strcasecmp(system_charset_info,
5274
5274
 
5275
5275
  SYNOPSIS
5276
5276
    store_top_level_join_columns()
5277
 
    thd            current thread
 
5277
    session            current thread
5278
5278
    table_ref      nested join or table in a FROM clause
5279
5279
    left_neighbor  neighbor table reference to the left of table_ref at the
5280
5280
                   same level in the join tree
5300
5300
*/
5301
5301
 
5302
5302
static bool
5303
 
store_top_level_join_columns(Session *thd, TableList *table_ref,
 
5303
store_top_level_join_columns(Session *session, TableList *table_ref,
5304
5304
                             TableList *left_neighbor,
5305
5305
                             TableList *right_neighbor)
5306
5306
{
5348
5348
                           same_level_right_neighbor : right_neighbor;
5349
5349
 
5350
5350
      if (cur_table_ref->nested_join &&
5351
 
          store_top_level_join_columns(thd, cur_table_ref,
 
5351
          store_top_level_join_columns(session, cur_table_ref,
5352
5352
                                       real_left_neighbor, real_right_neighbor))
5353
5353
        goto err;
5354
5354
      same_level_right_neighbor= cur_table_ref;
5380
5380
    */
5381
5381
    if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5382
5382
      std::swap(table_ref_1, table_ref_2);
5383
 
    if (mark_common_columns(thd, table_ref_1, table_ref_2,
 
5383
    if (mark_common_columns(session, table_ref_1, table_ref_2,
5384
5384
                            using_fields, &found_using_fields))
5385
5385
      goto err;
5386
5386
 
5391
5391
    */
5392
5392
    if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5393
5393
      std::swap(table_ref_1, table_ref_2);
5394
 
    if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
 
5394
    if (store_natural_using_join_columns(session, table_ref, table_ref_1,
5395
5395
                                         table_ref_2, using_fields,
5396
5396
                                         found_using_fields))
5397
5397
      goto err;
5438
5438
 
5439
5439
  SYNOPSIS
5440
5440
    setup_natural_join_row_types()
5441
 
    thd          current thread
 
5441
    session          current thread
5442
5442
    from_clause  list of top-level table references in a FROM clause
5443
5443
 
5444
5444
  DESCRIPTION
5456
5456
    true   Error
5457
5457
    false  OK
5458
5458
*/
5459
 
static bool setup_natural_join_row_types(Session *thd,
 
5459
static bool setup_natural_join_row_types(Session *session,
5460
5460
                                         List<TableList> *from_clause,
5461
5461
                                         Name_resolution_context *context)
5462
5462
{
5463
 
  thd->where= "from clause";
 
5463
  session->where= "from clause";
5464
5464
  if (from_clause->elements == 0)
5465
5465
    return false; /* We come here in the case of UNIONs. */
5466
5466
 
5476
5476
  {
5477
5477
    table_ref= left_neighbor;
5478
5478
    left_neighbor= table_ref_it++;
5479
 
    if (store_top_level_join_columns(thd, table_ref,
 
5479
    if (store_top_level_join_columns(session, table_ref,
5480
5480
                                     left_neighbor, right_neighbor))
5481
5481
      return true;
5482
5482
    if (left_neighbor)
5506
5506
** Expand all '*' in given fields
5507
5507
****************************************************************************/
5508
5508
 
5509
 
int setup_wild(Session *thd,
 
5509
int setup_wild(Session *session,
5510
5510
               TableList *tables __attribute__((unused)),
5511
5511
               List<Item> &fields,
5512
5512
               List<Item> *sum_func_list,
5518
5518
  Item *item;
5519
5519
  List_iterator<Item> it(fields);
5520
5520
 
5521
 
  thd->lex->current_select->cur_pos_in_select_list= 0;
 
5521
  session->lex->current_select->cur_pos_in_select_list= 0;
5522
5522
  while (wild_num && (item= it++))
5523
5523
  {
5524
5524
    if (item->type() == Item::FIELD_ITEM &&
5528
5528
    {
5529
5529
      uint32_t elem= fields.elements;
5530
5530
      bool any_privileges= ((Item_field *) item)->any_privileges;
5531
 
      Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
 
5531
      Item_subselect *subsel= session->lex->current_select->master_unit()->item;
5532
5532
      if (subsel &&
5533
5533
          subsel->substype() == Item_subselect::EXISTS_SUBS)
5534
5534
      {
5540
5540
        it.replace(new Item_int("Not_used", (int64_t) 1,
5541
5541
                                MY_INT64_NUM_DECIMAL_DIGITS));
5542
5542
      }
5543
 
      else if (insert_fields(thd, ((Item_field*) item)->context,
 
5543
      else if (insert_fields(session, ((Item_field*) item)->context,
5544
5544
                             ((Item_field*) item)->db_name,
5545
5545
                             ((Item_field*) item)->table_name, &it,
5546
5546
                             any_privileges))
5559
5559
      wild_num--;
5560
5560
    }
5561
5561
    else
5562
 
      thd->lex->current_select->cur_pos_in_select_list++;
 
5562
      session->lex->current_select->cur_pos_in_select_list++;
5563
5563
  }
5564
 
  thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
 
5564
  session->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5565
5565
  return(0);
5566
5566
}
5567
5567
 
5569
5569
** Check that all given fields exists and fill struct with current data
5570
5570
****************************************************************************/
5571
5571
 
5572
 
bool setup_fields(Session *thd, Item **ref_pointer_array,
 
5572
bool setup_fields(Session *session, Item **ref_pointer_array,
5573
5573
                  List<Item> &fields, enum_mark_columns mark_used_columns,
5574
5574
                  List<Item> *sum_func_list, bool allow_sum_func)
5575
5575
{
5576
5576
  register Item *item;
5577
 
  enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
5578
 
  nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
 
5577
  enum_mark_columns save_mark_used_columns= session->mark_used_columns;
 
5578
  nesting_map save_allow_sum_func= session->lex->allow_sum_func;
5579
5579
  List_iterator<Item> it(fields);
5580
5580
  bool save_is_item_list_lookup;
5581
5581
 
5582
 
  thd->mark_used_columns= mark_used_columns;
 
5582
  session->mark_used_columns= mark_used_columns;
5583
5583
  if (allow_sum_func)
5584
 
    thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
5585
 
  thd->where= Session::DEFAULT_WHERE;
5586
 
  save_is_item_list_lookup= thd->lex->current_select->is_item_list_lookup;
5587
 
  thd->lex->current_select->is_item_list_lookup= 0;
 
5584
    session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
5585
  session->where= Session::DEFAULT_WHERE;
 
5586
  save_is_item_list_lookup= session->lex->current_select->is_item_list_lookup;
 
5587
  session->lex->current_select->is_item_list_lookup= 0;
5588
5588
 
5589
5589
  /*
5590
5590
    To prevent fail on forward lookup we fill it with zerows,
5601
5601
    memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
5602
5602
 
5603
5603
  Item **ref= ref_pointer_array;
5604
 
  thd->lex->current_select->cur_pos_in_select_list= 0;
 
5604
  session->lex->current_select->cur_pos_in_select_list= 0;
5605
5605
  while ((item= it++))
5606
5606
  {
5607
 
    if ((!item->fixed && item->fix_fields(thd, it.ref())) || (item= *(it.ref()))->check_cols(1))
 
5607
    if ((!item->fixed && item->fix_fields(session, it.ref())) || (item= *(it.ref()))->check_cols(1))
5608
5608
    {
5609
 
      thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5610
 
      thd->lex->allow_sum_func= save_allow_sum_func;
5611
 
      thd->mark_used_columns= save_mark_used_columns;
 
5609
      session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
 
5610
      session->lex->allow_sum_func= save_allow_sum_func;
 
5611
      session->mark_used_columns= save_mark_used_columns;
5612
5612
      return(true); /* purecov: inspected */
5613
5613
    }
5614
5614
    if (ref)
5615
5615
      *(ref++)= item;
5616
5616
    if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
5617
5617
        sum_func_list)
5618
 
      item->split_sum_func(thd, ref_pointer_array, *sum_func_list);
5619
 
    thd->used_tables|= item->used_tables();
5620
 
    thd->lex->current_select->cur_pos_in_select_list++;
 
5618
      item->split_sum_func(session, ref_pointer_array, *sum_func_list);
 
5619
    session->used_tables|= item->used_tables();
 
5620
    session->lex->current_select->cur_pos_in_select_list++;
5621
5621
  }
5622
 
  thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5623
 
  thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
 
5622
  session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
 
5623
  session->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5624
5624
 
5625
 
  thd->lex->allow_sum_func= save_allow_sum_func;
5626
 
  thd->mark_used_columns= save_mark_used_columns;
5627
 
  return(test(thd->is_error()));
 
5625
  session->lex->allow_sum_func= save_allow_sum_func;
 
5626
  session->mark_used_columns= save_mark_used_columns;
 
5627
  return(test(session->is_error()));
5628
5628
}
5629
5629
 
5630
5630
 
5656
5656
 
5657
5657
  SYNOPSIS
5658
5658
    setup_tables()
5659
 
    thd           Thread handler
 
5659
    session               Thread handler
5660
5660
    context       name resolution contest to setup table list there
5661
5661
    from_clause   Top-level list of table references in the FROM clause
5662
5662
    tables        Table list (select_lex->table_list)
5679
5679
    true  error
5680
5680
*/
5681
5681
 
5682
 
bool setup_tables(Session *thd, Name_resolution_context *context,
 
5682
bool setup_tables(Session *session, Name_resolution_context *context,
5683
5683
                  List<TableList> *from_clause, TableList *tables,
5684
5684
                  TableList **leaves, bool select_insert)
5685
5685
{
5722
5722
  }
5723
5723
 
5724
5724
  /* Precompute and store the row types of NATURAL/USING joins. */
5725
 
  if (setup_natural_join_row_types(thd, from_clause, context))
 
5725
  if (setup_natural_join_row_types(session, from_clause, context))
5726
5726
    return(1);
5727
5727
 
5728
5728
  return(0);
5734
5734
 
5735
5735
  SYNOPSIS
5736
5736
    setup_tables_and_check_view_access()
5737
 
    thd           Thread handler
 
5737
    session               Thread handler
5738
5738
    context       name resolution contest to setup table list there
5739
5739
    from_clause   Top-level list of table references in the FROM clause
5740
5740
    tables        Table list (select_lex->table_list)
5752
5752
    false ok;  In this case *map will include the chosen index
5753
5753
    true  error
5754
5754
*/
5755
 
bool setup_tables_and_check_access(Session *thd, 
 
5755
bool setup_tables_and_check_access(Session *session, 
5756
5756
                                   Name_resolution_context *context,
5757
5757
                                   List<TableList> *from_clause,
5758
5758
                                   TableList *tables,
5762
5762
  TableList *leaves_tmp= NULL;
5763
5763
  bool first_table= true;
5764
5764
 
5765
 
  if (setup_tables(thd, context, from_clause, tables,
 
5765
  if (setup_tables(session, context, from_clause, tables,
5766
5766
                   &leaves_tmp, select_insert))
5767
5767
    return true;
5768
5768
 
5822
5822
 
5823
5823
  SYNOPSIS
5824
5824
    insert_fields()
5825
 
    thd                 Thread handler
 
5825
    session                     Thread handler
5826
5826
    context             Context for name resolution
5827
5827
    db_name             Database name in case of 'database_name.table_name.*'
5828
5828
    table_name          Table name in case of 'table_name.*'
5836
5836
*/
5837
5837
 
5838
5838
bool
5839
 
insert_fields(Session *thd, Name_resolution_context *context, const char *db_name,
 
5839
insert_fields(Session *session, Name_resolution_context *context, const char *db_name,
5840
5840
              const char *table_name, List_iterator<Item> *it,
5841
5841
              bool any_privileges __attribute__((unused)))
5842
5842
{
5884
5884
      views and natural joins this update is performed inside the loop below.
5885
5885
    */
5886
5886
    if (table)
5887
 
      thd->used_tables|= table->map;
 
5887
      session->used_tables|= table->map;
5888
5888
 
5889
5889
    /*
5890
5890
      Initialize a generic field iterator for the current table reference.
5898
5898
    {
5899
5899
      Item *item;
5900
5900
 
5901
 
      if (!(item= field_iterator.create_item(thd)))
 
5901
      if (!(item= field_iterator.create_item(session)))
5902
5902
        return(true);
5903
5903
 
5904
5904
      if (!found)
5943
5943
          field_table= nj_col->table_ref->table;
5944
5944
          if (field_table)
5945
5945
          {
5946
 
            thd->used_tables|= field_table->map;
 
5946
            session->used_tables|= field_table->map;
5947
5947
            field_table->covering_keys.intersect(field->part_of_key);
5948
5948
            field_table->merge_keys.merge(field->part_of_key);
5949
5949
            field_table->used_fields++;
5951
5951
        }
5952
5952
      }
5953
5953
      else
5954
 
        thd->used_tables|= item->used_tables();
5955
 
      thd->lex->current_select->cur_pos_in_select_list++;
 
5954
        session->used_tables|= item->used_tables();
 
5955
      session->lex->current_select->cur_pos_in_select_list++;
5956
5956
    }
5957
5957
    /*
5958
5958
      In case of stored tables, all fields are considered as used,
5985
5985
 
5986
5986
  SYNOPSIS
5987
5987
    setup_conds()
5988
 
    thd     thread handler
 
5988
    session     thread handler
5989
5989
    tables  list of tables for name resolving (select_lex->table_list)
5990
5990
    leaves  list of leaves of join table tree (select_lex->leaf_tables)
5991
5991
    conds   WHERE clause
5998
5998
    false if all is OK
5999
5999
*/
6000
6000
 
6001
 
int setup_conds(Session *thd, TableList *tables __attribute__((unused)),
 
6001
int setup_conds(Session *session, TableList *tables __attribute__((unused)),
6002
6002
                TableList *leaves,
6003
6003
                COND **conds)
6004
6004
{
6005
 
  SELECT_LEX *select_lex= thd->lex->current_select;
 
6005
  SELECT_LEX *select_lex= session->lex->current_select;
6006
6006
  TableList *table= NULL;       // For HP compilers
6007
 
  void *save_thd_marker= thd->thd_marker;
 
6007
  void *save_session_marker= session->session_marker;
6008
6008
  /*
6009
6009
    it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
6010
6010
    which belong to LEX, i.e. most up SELECT) will be updated by
6016
6016
  bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
6017
6017
  select_lex->is_item_list_lookup= 0;
6018
6018
 
6019
 
  thd->mark_used_columns= MARK_COLUMNS_READ;
 
6019
  session->mark_used_columns= MARK_COLUMNS_READ;
6020
6020
  select_lex->cond_count= 0;
6021
6021
  select_lex->between_count= 0;
6022
6022
  select_lex->max_equal_elems= 0;
6023
6023
 
6024
 
  thd->thd_marker= (void*)1;
 
6024
  session->session_marker= (void*)1;
6025
6025
  if (*conds)
6026
6026
  {
6027
 
    thd->where="where clause";
6028
 
    if ((!(*conds)->fixed && (*conds)->fix_fields(thd, conds)) ||
 
6027
    session->where="where clause";
 
6028
    if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
6029
6029
        (*conds)->check_cols(1))
6030
6030
      goto err_no_arena;
6031
6031
  }
6032
 
  thd->thd_marker= save_thd_marker;
 
6032
  session->session_marker= save_session_marker;
6033
6033
 
6034
6034
  /*
6035
6035
    Apply fix_fields() to all ON clauses at all levels of nesting,
6045
6045
      if (embedded->on_expr)
6046
6046
      {
6047
6047
        /* Make a join an a expression */
6048
 
        thd->thd_marker= (void*)embedded;
6049
 
        thd->where="on clause";
6050
 
        if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(thd, &embedded->on_expr)) ||
 
6048
        session->session_marker= (void*)embedded;
 
6049
        session->where="on clause";
 
6050
        if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(session, &embedded->on_expr)) ||
6051
6051
            embedded->on_expr->check_cols(1))
6052
6052
          goto err_no_arena;
6053
6053
        select_lex->cond_count++;
6058
6058
           embedding->nested_join->join_list.head() == embedded);
6059
6059
 
6060
6060
  }
6061
 
  thd->thd_marker= save_thd_marker;
 
6061
  session->session_marker= save_session_marker;
6062
6062
 
6063
 
  thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6064
 
  return(test(thd->is_error()));
 
6063
  session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
 
6064
  return(test(session->is_error()));
6065
6065
 
6066
6066
err_no_arena:
6067
6067
  select_lex->is_item_list_lookup= save_is_item_list_lookup;
6080
6080
 
6081
6081
  SYNOPSIS
6082
6082
    fill_record()
6083
 
    thd           thread handler
 
6083
    session           thread handler
6084
6084
    fields        Item_fields list to be filled
6085
6085
    values        values to fill with
6086
6086
    ignore_errors true if we should ignore errors
6096
6096
*/
6097
6097
 
6098
6098
bool
6099
 
fill_record(Session * thd, List<Item> &fields, List<Item> &values, bool ignore_errors)
 
6099
fill_record(Session * session, List<Item> &fields, List<Item> &values, bool ignore_errors)
6100
6100
{
6101
6101
  List_iterator_fast<Item> f(fields),v(values);
6102
6102
  Item *value, *fld;
6103
6103
  Item_field *field;
6104
6104
  Table *table= 0;
6105
6105
  List<Table> tbl_list;
6106
 
  bool abort_on_warning_saved= thd->abort_on_warning;
 
6106
  bool abort_on_warning_saved= session->abort_on_warning;
6107
6107
  tbl_list.empty();
6108
6108
 
6109
6109
  /*
6143
6143
        value->type() != Item::NULL_ITEM &&
6144
6144
        table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6145
6145
    {
6146
 
      thd->abort_on_warning= false;
6147
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
6146
      session->abort_on_warning= false;
 
6147
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6148
6148
                          ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6149
6149
                          ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6150
6150
                          rfield->field_name, table->s->table_name.str);
6151
 
      thd->abort_on_warning= abort_on_warning_saved;
 
6151
      session->abort_on_warning= abort_on_warning_saved;
6152
6152
    }
6153
6153
    if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
6154
6154
    {
6158
6158
    tbl_list.push_back(table);
6159
6159
  }
6160
6160
  /* Update virtual fields*/
6161
 
  thd->abort_on_warning= false;
 
6161
  session->abort_on_warning= false;
6162
6162
  if (tbl_list.head())
6163
6163
  {
6164
6164
    List_iterator_fast<Table> t(tbl_list);
6180
6180
      }
6181
6181
    }
6182
6182
  }
6183
 
  thd->abort_on_warning= abort_on_warning_saved;
6184
 
  return(thd->is_error());
 
6183
  session->abort_on_warning= abort_on_warning_saved;
 
6184
  return(session->is_error());
6185
6185
err:
6186
 
  thd->abort_on_warning= abort_on_warning_saved;
 
6186
  session->abort_on_warning= abort_on_warning_saved;
6187
6187
  if (table)
6188
6188
    table->auto_increment_field_not_null= false;
6189
6189
  return(true);
6195
6195
 
6196
6196
  SYNOPSIS
6197
6197
    fill_record()
6198
 
    thd           thread handler
 
6198
    session           thread handler
6199
6199
    ptr           pointer on pointer to record
6200
6200
    values        list of fields
6201
6201
    ignore_errors true if we should ignore errors
6211
6211
*/
6212
6212
 
6213
6213
bool
6214
 
fill_record(Session *thd, Field **ptr, List<Item> &values,
 
6214
fill_record(Session *session, Field **ptr, List<Item> &values,
6215
6215
            bool ignore_errors __attribute__((unused)))
6216
6216
{
6217
6217
  List_iterator_fast<Item> v(values);
6219
6219
  Table *table= 0;
6220
6220
  Field *field;
6221
6221
  List<Table> tbl_list;
6222
 
  bool abort_on_warning_saved= thd->abort_on_warning;
 
6222
  bool abort_on_warning_saved= session->abort_on_warning;
6223
6223
  
6224
6224
  tbl_list.empty();
6225
6225
  /*
6235
6235
    table= (*ptr)->table;
6236
6236
    table->auto_increment_field_not_null= false;
6237
6237
  }
6238
 
  while ((field = *ptr++) && ! thd->is_error())
 
6238
  while ((field = *ptr++) && ! session->is_error())
6239
6239
  {
6240
6240
    value=v++;
6241
6241
    table= field->table;
6246
6246
        value->type() != Item::NULL_ITEM &&
6247
6247
        table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6248
6248
    {
6249
 
      thd->abort_on_warning= false;
6250
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
6249
      session->abort_on_warning= false;
 
6250
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6251
6251
                          ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6252
6252
                          ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6253
6253
                          field->field_name, table->s->table_name.str);
6254
 
      thd->abort_on_warning= abort_on_warning_saved;
 
6254
      session->abort_on_warning= abort_on_warning_saved;
6255
6255
    }
6256
6256
    if (value->save_in_field(field, 0) < 0)
6257
6257
      goto err;
6258
6258
    tbl_list.push_back(table);
6259
6259
  }
6260
6260
  /* Update virtual fields*/
6261
 
  thd->abort_on_warning= false;
 
6261
  session->abort_on_warning= false;
6262
6262
  if (tbl_list.head())
6263
6263
  {
6264
6264
    List_iterator_fast<Table> t(tbl_list);
6282
6282
      }
6283
6283
    }
6284
6284
  }
6285
 
  thd->abort_on_warning= abort_on_warning_saved;
6286
 
  return(thd->is_error());
 
6285
  session->abort_on_warning= abort_on_warning_saved;
 
6286
  return(session->is_error());
6287
6287
 
6288
6288
err:
6289
 
  thd->abort_on_warning= abort_on_warning_saved;
 
6289
  session->abort_on_warning= abort_on_warning_saved;
6290
6290
  if (table)
6291
6291
    table->auto_increment_field_not_null= false;
6292
6292
  return(true);
6300
6300
  MY_DIR *dirp;
6301
6301
  FILEINFO *file;
6302
6302
  TABLE_SHARE share;
6303
 
  Session *thd;
 
6303
  Session *session;
6304
6304
 
6305
 
  if (!(thd= new Session))
 
6305
  if (!(session= new Session))
6306
6306
    return(1);
6307
 
  thd->thread_stack= (char*) &thd;
6308
 
  thd->store_globals();
 
6307
  session->thread_stack= (char*) &session;
 
6308
  session->store_globals();
6309
6309
 
6310
6310
  for (i=0; i<=mysql_tmpdir_list.max; i++)
6311
6311
  {
6338
6338
          /* We should cut file extention before deleting of table */
6339
6339
          memcpy(filePathCopy, filePath, filePath_len - ext_len);
6340
6340
          filePathCopy[filePath_len - ext_len]= 0;
6341
 
          init_tmp_table_share(thd, &share, "", 0, "", filePathCopy);
6342
 
          if (!open_table_def(thd, &share, 0) &&
6343
 
              ((handler_file= get_new_handler(&share, thd->mem_root,
 
6341
          init_tmp_table_share(session, &share, "", 0, "", filePathCopy);
 
6342
          if (!open_table_def(session, &share, 0) &&
 
6343
              ((handler_file= get_new_handler(&share, session->mem_root,
6344
6344
                                              share.db_type()))))
6345
6345
          {
6346
6346
            handler_file->ha_delete_table(filePathCopy);
6358
6358
    }
6359
6359
    my_dirend(dirp);
6360
6360
  }
6361
 
  delete thd;
 
6361
  delete session;
6362
6362
  pthread_setspecific(THR_Session,  0);
6363
6363
  return(0);
6364
6364
}
6431
6431
    1  Table is in use by another thread
6432
6432
*/
6433
6433
 
6434
 
bool remove_table_from_cache(Session *thd, const char *db, const char *table_name,
 
6434
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
6435
6435
                             uint32_t flags)
6436
6436
{
6437
6437
  char key[MAX_DBKEY_LENGTH];
6459
6459
      {
6460
6460
        relink_unused(table);
6461
6461
      }
6462
 
      else if (in_use != thd)
 
6462
      else if (in_use != session)
6463
6463
      {
6464
6464
        /*
6465
6465
          Mark that table is going to be deleted from cache. This will
6480
6480
          open_tables list. Aborting the MERGE lock after a child was
6481
6481
          closed and before the parent is closed would be fatal.
6482
6482
        */
6483
 
        for (Table *thd_table= in_use->open_tables;
6484
 
             thd_table ;
6485
 
             thd_table= thd_table->next)
 
6483
        for (Table *session_table= in_use->open_tables;
 
6484
             session_table ;
 
6485
             session_table= session_table->next)
6486
6486
        {
6487
6487
          /* Do not handle locks of MERGE children. */
6488
 
          if (thd_table->db_stat)       // If table is open
6489
 
            signalled|= mysql_lock_abort_for_thread(thd, thd_table);
 
6488
          if (session_table->db_stat)   // If table is open
 
6489
            signalled|= mysql_lock_abort_for_thread(session, session_table);
6490
6490
        }
6491
6491
      }
6492
6492
      else
6514
6514
        reopen their tables
6515
6515
      */
6516
6516
      broadcast_refresh();
6517
 
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !thd->killed)
 
6517
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !session->killed)
6518
6518
      {
6519
6519
        dropping_tables++;
6520
6520
        if (likely(signalled))