~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_class.cc

update to latest from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
char internal_table_name[2]= "*";
38
38
char empty_c_string[1]= {0};    /* used for not defined db */
39
39
 
40
 
const char * const THD::DEFAULT_WHERE= "field list";
 
40
const char * const Session::DEFAULT_WHERE= "field list";
41
41
 
42
42
 
43
43
/*****************************************************************************
86
86
  Construct an (almost) deep copy of this key. Only those
87
87
  elements that are known to never change are not copied.
88
88
  If out of memory, a partial copy is returned and an error is set
89
 
  in THD.
 
89
  in Session.
90
90
*/
91
91
 
92
92
Key::Key(const Key &rhs, MEM_ROOT *mem_root)
103
103
  Construct an (almost) deep copy of this foreign key. Only those
104
104
  elements that are known to never change are not copied.
105
105
  If out of memory, a partial copy is returned and an error is set
106
 
  in THD.
 
106
  in Session.
107
107
*/
108
108
 
109
109
Foreign_key::Foreign_key(const Foreign_key &rhs, MEM_ROOT *mem_root)
265
265
 
266
266
 
267
267
extern "C"
268
 
int thd_in_lock_tables(const THD *thd)
 
268
int session_in_lock_tables(const Session *session)
269
269
{
270
 
  return test(thd->in_lock_tables);
 
270
  return test(session->in_lock_tables);
271
271
}
272
272
 
273
273
 
274
274
extern "C"
275
 
int thd_tablespace_op(const THD *thd)
 
275
int session_tablespace_op(const Session *session)
276
276
{
277
 
  return test(thd->tablespace_op);
 
277
  return test(session->tablespace_op);
278
278
}
279
279
 
280
280
 
281
281
/**
282
 
   Set the process info field of the THD structure.
 
282
   Set the process info field of the Session structure.
283
283
 
284
284
   This function is used by plug-ins. Internally, the
285
 
   THD::set_proc_info() function should be used.
 
285
   Session::set_proc_info() function should be used.
286
286
 
287
 
   @see THD::set_proc_info
 
287
   @see Session::set_proc_info
288
288
 */
289
289
extern "C" void
290
 
set_thd_proc_info(THD *thd, const char *info)
291
 
{
292
 
  thd->set_proc_info(info);
293
 
}
294
 
 
295
 
extern "C"
296
 
const char *get_thd_proc_info(THD *thd)
297
 
{
298
 
  return thd->get_proc_info();
299
 
}
300
 
 
301
 
extern "C"
302
 
void **thd_ha_data(const THD *thd, const struct handlerton *hton)
303
 
{
304
 
  return (void **) &thd->ha_data[hton->slot].ha_ptr;
305
 
}
306
 
 
307
 
extern "C"
308
 
int64_t thd_test_options(const THD *thd, int64_t test_options)
309
 
{
310
 
  return thd->options & test_options;
311
 
}
312
 
 
313
 
extern "C"
314
 
int thd_sql_command(const THD *thd)
315
 
{
316
 
  return (int) thd->lex->sql_command;
317
 
}
318
 
 
319
 
extern "C"
320
 
int thd_tx_isolation(const THD *thd)
321
 
{
322
 
  return (int) thd->variables.tx_isolation;
323
 
}
324
 
 
325
 
extern "C"
326
 
void thd_inc_row_count(THD *thd)
327
 
{
328
 
  thd->row_count++;
 
290
set_session_proc_info(Session *session, const char *info)
 
291
{
 
292
  session->set_proc_info(info);
 
293
}
 
294
 
 
295
extern "C"
 
296
const char *get_session_proc_info(Session *session)
 
297
{
 
298
  return session->get_proc_info();
 
299
}
 
300
 
 
301
extern "C"
 
302
void **session_ha_data(const Session *session, const struct handlerton *hton)
 
303
{
 
304
  return (void **) &session->ha_data[hton->slot].ha_ptr;
 
305
}
 
306
 
 
307
extern "C"
 
308
int64_t session_test_options(const Session *session, int64_t test_options)
 
309
{
 
310
  return session->options & test_options;
 
311
}
 
312
 
 
313
extern "C"
 
314
int session_sql_command(const Session *session)
 
315
{
 
316
  return (int) session->lex->sql_command;
 
317
}
 
318
 
 
319
extern "C"
 
320
int session_tx_isolation(const Session *session)
 
321
{
 
322
  return (int) session->variables.tx_isolation;
 
323
}
 
324
 
 
325
extern "C"
 
326
void session_inc_row_count(Session *session)
 
327
{
 
328
  session->row_count++;
329
329
}
330
330
 
331
331
/**
357
357
*/
358
358
 
359
359
void
360
 
Diagnostics_area::set_ok_status(THD *thd, ha_rows affected_rows_arg,
 
360
Diagnostics_area::set_ok_status(Session *session, ha_rows affected_rows_arg,
361
361
                                uint64_t last_insert_id_arg,
362
362
                                const char *message_arg)
363
363
{
370
370
    return;
371
371
  /** Only allowed to report success if has not yet reported an error */
372
372
 
373
 
  m_server_status= thd->server_status;
374
 
  m_total_warn_count= thd->total_warn_count;
 
373
  m_server_status= session->server_status;
 
374
  m_total_warn_count= session->total_warn_count;
375
375
  m_affected_rows= affected_rows_arg;
376
376
  m_last_insert_id= last_insert_id_arg;
377
377
  if (message_arg)
387
387
*/
388
388
 
389
389
void
390
 
Diagnostics_area::set_eof_status(THD *thd)
 
390
Diagnostics_area::set_eof_status(Session *session)
391
391
{
392
392
  /** Only allowed to report eof if has not yet reported an error */
393
393
 
399
399
  if (is_error() || is_disabled())
400
400
    return;
401
401
 
402
 
  m_server_status= thd->server_status;
 
402
  m_server_status= session->server_status;
403
403
  /*
404
404
    If inside a stored procedure, do not return the total
405
405
    number of warnings, since they are not available to the client
406
406
    anyway.
407
407
  */
408
 
  m_total_warn_count= thd->total_warn_count;
 
408
  m_total_warn_count= session->total_warn_count;
409
409
 
410
410
  m_status= DA_EOF;
411
411
}
415
415
*/
416
416
 
417
417
void
418
 
Diagnostics_area::set_error_status(THD *thd __attribute__((unused)),
 
418
Diagnostics_area::set_error_status(Session *session __attribute__((unused)),
419
419
                                   uint32_t sql_errno_arg,
420
420
                                   const char *message_arg)
421
421
{
455
455
}
456
456
 
457
457
 
458
 
THD::THD()
 
458
Session::Session()
459
459
   :Statement(&main_lex, &main_mem_root,
460
460
              /* statement id */ 0),
461
461
   Open_tables_state(refresh_version), rli_fake(0),
502
502
  limit_found_rows= 0;
503
503
  row_count_func= -1;
504
504
  statement_id_counter= 0UL;
505
 
  // Must be reset to handle error with THD's created for init of mysqld
 
505
  // Must be reset to handle error with Session's created for init of mysqld
506
506
  lex->current_select= 0;
507
507
  start_time=(time_t) 0;
508
508
  start_utime= 0L;
519
519
  memset(ha_data, 0, sizeof(ha_data));
520
520
  mysys_var=0;
521
521
  binlog_evt_union.do_union= false;
522
 
  dbug_sentry=THD_SENTRY_MAGIC;
 
522
  dbug_sentry=Session_SENTRY_MAGIC;
523
523
  net.vio=0;
524
524
  client_capabilities= 0;                       // minimalistic client
525
525
  system_thread= NON_SYSTEM_THREAD;
531
531
 
532
532
  /* Variables with default values */
533
533
  proc_info="login";
534
 
  where= THD::DEFAULT_WHERE;
 
534
  where= Session::DEFAULT_WHERE;
535
535
  server_id = ::server_id;
536
536
  slave_net = 0;
537
537
  command=COM_CONNECT;
567
567
}
568
568
 
569
569
 
570
 
void THD::push_internal_handler(Internal_error_handler *handler)
 
570
void Session::push_internal_handler(Internal_error_handler *handler)
571
571
{
572
572
  /*
573
573
    TODO: The current implementation is limited to 1 handler at a time only.
574
 
    THD and sp_rcontext need to be modified to use a common handler stack.
 
574
    Session and sp_rcontext need to be modified to use a common handler stack.
575
575
  */
576
576
  assert(m_internal_handler == NULL);
577
577
  m_internal_handler= handler;
578
578
}
579
579
 
580
580
 
581
 
bool THD::handle_error(uint32_t sql_errno, const char *message,
 
581
bool Session::handle_error(uint32_t sql_errno, const char *message,
582
582
                       DRIZZLE_ERROR::enum_warning_level level)
583
583
{
584
584
  if (m_internal_handler)
590
590
}
591
591
 
592
592
 
593
 
void THD::pop_internal_handler()
 
593
void Session::pop_internal_handler()
594
594
{
595
595
  assert(m_internal_handler != NULL);
596
596
  m_internal_handler= NULL;
597
597
}
598
598
 
599
599
extern "C"
600
 
void *thd_alloc(THD *thd, unsigned int size)
601
 
{
602
 
  return thd->alloc(size);
603
 
}
604
 
 
605
 
extern "C"
606
 
void *thd_calloc(THD *thd, unsigned int size)
607
 
{
608
 
  return thd->calloc(size);
609
 
}
610
 
 
611
 
extern "C"
612
 
char *thd_strdup(THD *thd, const char *str)
613
 
{
614
 
  return thd->strdup(str);
615
 
}
616
 
 
617
 
extern "C"
618
 
char *thd_strmake(THD *thd, const char *str, unsigned int size)
619
 
{
620
 
  return thd->strmake(str, size);
621
 
}
622
 
 
623
 
extern "C"
624
 
LEX_STRING *thd_make_lex_string(THD *thd, LEX_STRING *lex_str,
 
600
void *session_alloc(Session *session, unsigned int size)
 
601
{
 
602
  return session->alloc(size);
 
603
}
 
604
 
 
605
extern "C"
 
606
void *session_calloc(Session *session, unsigned int size)
 
607
{
 
608
  return session->calloc(size);
 
609
}
 
610
 
 
611
extern "C"
 
612
char *session_strdup(Session *session, const char *str)
 
613
{
 
614
  return session->strdup(str);
 
615
}
 
616
 
 
617
extern "C"
 
618
char *session_strmake(Session *session, const char *str, unsigned int size)
 
619
{
 
620
  return session->strmake(str, size);
 
621
}
 
622
 
 
623
extern "C"
 
624
LEX_STRING *session_make_lex_string(Session *session, LEX_STRING *lex_str,
625
625
                                const char *str, unsigned int size,
626
626
                                int allocate_lex_string)
627
627
{
628
 
  return thd->make_lex_string(lex_str, str, size,
 
628
  return session->make_lex_string(lex_str, str, size,
629
629
                              (bool) allocate_lex_string);
630
630
}
631
631
 
632
632
extern "C"
633
 
void *thd_memdup(THD *thd, const void* str, unsigned int size)
 
633
void *session_memdup(Session *session, const void* str, unsigned int size)
634
634
{
635
 
  return thd->memdup(str, size);
 
635
  return session->memdup(str, size);
636
636
}
637
637
 
638
638
extern "C"
639
 
void thd_get_xid(const THD *thd, DRIZZLE_XID *xid)
 
639
void session_get_xid(const Session *session, DRIZZLE_XID *xid)
640
640
{
641
 
  *xid = *(DRIZZLE_XID *) &thd->transaction.xid_state.xid;
 
641
  *xid = *(DRIZZLE_XID *) &session->transaction.xid_state.xid;
642
642
}
643
643
 
644
644
/*
645
645
  Init common variables that has to be reset on start and on change_user
646
646
*/
647
647
 
648
 
void THD::init(void)
 
648
void Session::init(void)
649
649
{
650
650
  pthread_mutex_lock(&LOCK_global_system_variables);
651
 
  plugin_thdvar_init(this);
652
 
  variables.time_format= date_time_format_copy((THD*) 0,
 
651
  plugin_sessionvar_init(this);
 
652
  variables.time_format= date_time_format_copy((Session*) 0,
653
653
                                               variables.time_format);
654
 
  variables.date_format= date_time_format_copy((THD*) 0,
 
654
  variables.date_format= date_time_format_copy((Session*) 0,
655
655
                                               variables.date_format);
656
 
  variables.datetime_format= date_time_format_copy((THD*) 0,
 
656
  variables.datetime_format= date_time_format_copy((Session*) 0,
657
657
                                                   variables.datetime_format);
658
658
  /*
659
659
    variables= global_system_variables above has reset
663
663
  variables.pseudo_thread_id= thread_id;
664
664
  pthread_mutex_unlock(&LOCK_global_system_variables);
665
665
  server_status= SERVER_STATUS_AUTOCOMMIT;
666
 
  options= thd_startup_options;
 
666
  options= session_startup_options;
667
667
 
668
668
  if (variables.max_join_size == HA_POS_ERROR)
669
669
    options |= OPTION_BIG_SELECTS;
686
686
 
687
687
 
688
688
/*
689
 
  Init THD for query processing.
 
689
  Init Session for query processing.
690
690
  This has to be called once before we call mysql_parse.
691
691
  See also comments in sql_class.h.
692
692
*/
693
693
 
694
 
void THD::init_for_queries()
 
694
void Session::init_for_queries()
695
695
{
696
696
  set_time(); 
697
697
  ha_enable_transaction(this,true);
702
702
                      variables.trans_alloc_block_size,
703
703
                      variables.trans_prealloc_size);
704
704
  transaction.xid_state.xid.null();
705
 
  transaction.xid_state.in_thd=1;
 
705
  transaction.xid_state.in_session=1;
706
706
}
707
707
 
708
708
 
709
709
/* Do operations that may take a long time */
710
710
 
711
 
void THD::cleanup(void)
 
711
void Session::cleanup(void)
712
712
{
713
713
  assert(cleanup_done == 0);
714
714
 
743
743
  return;
744
744
}
745
745
 
746
 
THD::~THD()
 
746
Session::~Session()
747
747
{
748
 
  THD_CHECK_SENTRY(this);
749
 
  /* Ensure that no one is using THD */
 
748
  Session_CHECK_SENTRY(this);
 
749
  /* Ensure that no one is using Session */
750
750
  pthread_mutex_lock(&LOCK_delete);
751
751
  pthread_mutex_unlock(&LOCK_delete);
752
752
  add_to_status(&global_status_var, &status_var);
761
761
    cleanup();
762
762
 
763
763
  ha_close_connection(this);
764
 
  plugin_thdvar_cleanup(this);
 
764
  plugin_sessionvar_cleanup(this);
765
765
 
766
766
  main_security_ctx.destroy();
767
767
  if (db)
773
773
  free_root(&transaction.mem_root,MYF(0));
774
774
  mysys_var=0;                                  // Safety (shouldn't be needed)
775
775
  pthread_mutex_destroy(&LOCK_delete);
776
 
  dbug_sentry= THD_SENTRY_GONE;
 
776
  dbug_sentry= Session_SENTRY_GONE;
777
777
  if (rli_fake)
778
778
  {
779
779
    delete rli_fake;
836
836
}
837
837
 
838
838
 
839
 
void THD::awake(THD::killed_state state_to_set)
 
839
void Session::awake(Session::killed_state state_to_set)
840
840
{
841
 
  THD_CHECK_SENTRY(this);
 
841
  Session_CHECK_SENTRY(this);
842
842
  safe_mutex_assert_owner(&LOCK_delete); 
843
843
 
844
844
  killed= state_to_set;
845
 
  if (state_to_set != THD::KILL_QUERY)
 
845
  if (state_to_set != Session::KILL_QUERY)
846
846
  {
847
847
    thr_alarm_kill(thread_id);
848
848
    if (!slave_thread)
869
869
      current_cond and current_mutex are 0), then the victim will not get
870
870
      a signal and it may wait "forever" on the cond (until
871
871
      we issue a second KILL or the status it's waiting for happens).
872
 
      It's true that we have set its thd->killed but it may not
 
872
      It's true that we have set its session->killed but it may not
873
873
      see it immediately and so may have time to reach the cond_wait().
874
874
    */
875
875
    if (mysys_var->current_cond && mysys_var->current_mutex)
888
888
  sql_alloc() and the structure for the net buffer
889
889
*/
890
890
 
891
 
bool THD::store_globals()
 
891
bool Session::store_globals()
892
892
{
893
893
  /*
894
894
    Assert that thread_stack is initialized: it's necessary to be able
896
896
  */
897
897
  assert(thread_stack);
898
898
 
899
 
  if (pthread_setspecific(THR_THD,  this) ||
 
899
  if (pthread_setspecific(THR_Session,  this) ||
900
900
      pthread_setspecific(THR_MALLOC, &mem_root))
901
901
    return 1;
902
902
  mysys_var=my_thread_var;
903
903
  /*
904
904
    Let mysqld define the thread id (not mysys)
905
 
    This allows us to move THD to different threads if needed.
 
905
    This allows us to move Session to different threads if needed.
906
906
  */
907
907
  mysys_var->id= thread_id;
908
908
  real_id= pthread_self();                      // For debugging
909
909
 
910
910
  /*
911
 
    We have to call thr_lock_info_init() again here as THD may have been
 
911
    We have to call thr_lock_info_init() again here as Session may have been
912
912
    created in another thread
913
913
  */
914
914
  thr_lock_info_init(&lock_info);
920
920
  Cleanup after query.
921
921
 
922
922
  SYNOPSIS
923
 
    THD::cleanup_after_query()
 
923
    Session::cleanup_after_query()
924
924
 
925
925
  DESCRIPTION
926
926
    This function is used to reset thread data to its default state.
932
932
    slave.
933
933
*/
934
934
 
935
 
void THD::cleanup_after_query()
 
935
void Session::cleanup_after_query()
936
936
{
937
937
  /*
938
938
    Reset rand_used so that detection of calls to rand() will save random 
956
956
  /* Free Items that were created during this execution */
957
957
  free_items();
958
958
  /* Reset where. */
959
 
  where= THD::DEFAULT_WHERE;
 
959
  where= Session::DEFAULT_WHERE;
960
960
}
961
961
 
962
962
 
970
970
                              instead of using lex_str value
971
971
  @return  NULL on failure, or pointer to the LEX_STRING object
972
972
*/
973
 
LEX_STRING *THD::make_lex_string(LEX_STRING *lex_str,
 
973
LEX_STRING *Session::make_lex_string(LEX_STRING *lex_str,
974
974
                                 const char* str, uint32_t length,
975
975
                                 bool allocate_lex_string)
976
976
{
1004
1004
        In this case to->str will point to 0 and to->length will be 0.
1005
1005
*/
1006
1006
 
1007
 
bool THD::convert_string(LEX_STRING *to, const CHARSET_INFO * const to_cs,
 
1007
bool Session::convert_string(LEX_STRING *to, const CHARSET_INFO * const to_cs,
1008
1008
                         const char *from, uint32_t from_length,
1009
1009
                         const CHARSET_INFO * const from_cs)
1010
1010
{
1026
1026
  Convert string from source character set to target character set inplace.
1027
1027
 
1028
1028
  SYNOPSIS
1029
 
    THD::convert_string
 
1029
    Session::convert_string
1030
1030
 
1031
1031
  DESCRIPTION
1032
1032
    Convert string using convert_buffer - buffer for character set 
1037
1037
   !0   out of memory
1038
1038
*/
1039
1039
 
1040
 
bool THD::convert_string(String *s, const CHARSET_INFO * const from_cs,
 
1040
bool Session::convert_string(String *s, const CHARSET_INFO * const from_cs,
1041
1041
                         const CHARSET_INFO * const to_cs)
1042
1042
{
1043
1043
  uint32_t dummy_errors;
1058
1058
  Update some cache variables when character set changes
1059
1059
*/
1060
1060
 
1061
 
void THD::update_charset()
 
1061
void Session::update_charset()
1062
1062
{
1063
1063
  uint32_t not_used;
1064
1064
  charset_is_system_charset= !String::needs_conversion(0,charset(),
1088
1088
 
1089
1089
/* add table to list of changed in transaction tables */
1090
1090
 
1091
 
void THD::add_changed_table(Table *table)
 
1091
void Session::add_changed_table(Table *table)
1092
1092
{
1093
1093
  assert((options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) &&
1094
1094
              table->file->has_transactions());
1098
1098
}
1099
1099
 
1100
1100
 
1101
 
void THD::add_changed_table(const char *key, long key_length)
 
1101
void Session::add_changed_table(const char *key, long key_length)
1102
1102
{
1103
1103
  CHANGED_TableList **prev_changed = &transaction.changed_tables;
1104
1104
  CHANGED_TableList *curr = transaction.changed_tables;
1130
1130
}
1131
1131
 
1132
1132
 
1133
 
CHANGED_TableList* THD::changed_table_dup(const char *key, long key_length)
 
1133
CHANGED_TableList* Session::changed_table_dup(const char *key, long key_length)
1134
1134
{
1135
1135
  CHANGED_TableList* new_table = 
1136
1136
    (CHANGED_TableList*) trans_alloc(ALIGN_SIZE(sizeof(CHANGED_TableList))+
1151
1151
}
1152
1152
 
1153
1153
 
1154
 
int THD::send_explain_fields(select_result *result)
 
1154
int Session::send_explain_fields(select_result *result)
1155
1155
{
1156
1156
  List<Item> field_list;
1157
1157
  Item *item;
1211
1211
/*
1212
1212
  Register an item tree tree transformation, performed by the query
1213
1213
  optimizer. We need a pointer to runtime_memroot because it may be !=
1214
 
  thd->mem_root (this may no longer be a true statement)
 
1214
  session->mem_root (this may no longer be a true statement)
1215
1215
*/
1216
1216
 
1217
 
void THD::nocheck_register_item_tree_change(Item **place, Item *old_value,
 
1217
void Session::nocheck_register_item_tree_change(Item **place, Item *old_value,
1218
1218
                                            MEM_ROOT *runtime_memroot)
1219
1219
{
1220
1220
  Item_change_record *change;
1227
1227
  if (change_mem == 0)
1228
1228
  {
1229
1229
    /*
1230
 
      OOM, thd->fatal_error() is called by the error handler of the
 
1230
      OOM, session->fatal_error() is called by the error handler of the
1231
1231
      memroot. Just return.
1232
1232
    */
1233
1233
    return;
1239
1239
}
1240
1240
 
1241
1241
 
1242
 
void THD::rollback_item_tree_changes()
 
1242
void Session::rollback_item_tree_changes()
1243
1243
{
1244
1244
  I_List_iterator<Item_change_record> it(change_list);
1245
1245
  Item_change_record *change;
1258
1258
 
1259
1259
select_result::select_result()
1260
1260
{
1261
 
  thd=current_thd;
 
1261
  session=current_session;
1262
1262
}
1263
1263
 
1264
1264
void select_result::send_error(uint32_t errcode,const char *err)
1298
1298
bool select_send::send_fields(List<Item> &list, uint32_t flags)
1299
1299
{
1300
1300
  bool res;
1301
 
  if (!(res= thd->protocol->send_fields(&list, flags)))
 
1301
  if (!(res= session->protocol->send_fields(&list, flags)))
1302
1302
    is_result_set_started= 1;
1303
1303
  return res;
1304
1304
}
1333
1333
  /*
1334
1334
    We may be passing the control from mysqld to the client: release the
1335
1335
    InnoDB adaptive hash S-latch to avoid thread deadlocks if it was reserved
1336
 
    by thd
 
1336
    by session
1337
1337
  */
1338
 
  ha_release_temporary_latches(thd);
 
1338
  ha_release_temporary_latches(session);
1339
1339
 
1340
1340
  List_iterator_fast<Item> li(items);
1341
 
  Protocol *protocol= thd->protocol;
 
1341
  Protocol *protocol= session->protocol;
1342
1342
  char buff[MAX_FIELD_WIDTH];
1343
1343
  String buffer(buff, sizeof(buff), &my_charset_bin);
1344
1344
 
1353
1353
      break;
1354
1354
    }
1355
1355
  }
1356
 
  thd->sent_row_count++;
1357
 
  if (thd->is_error())
 
1356
  session->sent_row_count++;
 
1357
  if (session->is_error())
1358
1358
  {
1359
1359
    protocol->remove_last_row();
1360
1360
    return(1);
1361
1361
  }
1362
 
  if (thd->vio_ok())
 
1362
  if (session->vio_ok())
1363
1363
    return(protocol->write());
1364
1364
  return(0);
1365
1365
}
1369
1369
  /* 
1370
1370
    We may be passing the control from mysqld to the client: release the
1371
1371
    InnoDB adaptive hash S-latch to avoid thread deadlocks if it was reserved
1372
 
    by thd 
 
1372
    by session 
1373
1373
  */
1374
 
  ha_release_temporary_latches(thd);
 
1374
  ha_release_temporary_latches(session);
1375
1375
 
1376
1376
  /* Unlock tables before sending packet to gain some speed */
1377
 
  if (thd->lock)
 
1377
  if (session->lock)
1378
1378
  {
1379
 
    mysql_unlock_tables(thd, thd->lock);
1380
 
    thd->lock=0;
 
1379
    mysql_unlock_tables(session, session->lock);
 
1380
    session->lock=0;
1381
1381
  }
1382
 
  ::my_eof(thd);
 
1382
  ::my_eof(session);
1383
1383
  is_result_set_started= 0;
1384
1384
  return false;
1385
1385
}
1414
1414
      function, SELECT INTO has to have an own SQLCOM.
1415
1415
      TODO: split from SQLCOM_SELECT
1416
1416
    */
1417
 
    ::my_ok(thd,row_count);
 
1417
    ::my_ok(session,row_count);
1418
1418
  }
1419
1419
  file= -1;
1420
1420
  return error;
1451
1451
 
1452
1452
select_export::~select_export()
1453
1453
{
1454
 
  thd->sent_row_count=row_count;
 
1454
  session->sent_row_count=row_count;
1455
1455
}
1456
1456
 
1457
1457
 
1460
1460
 
1461
1461
  SYNOPSIS
1462
1462
    create_file()
1463
 
    thd                 Thread handle
 
1463
    session                     Thread handle
1464
1464
    path                File name
1465
1465
    exchange            Excange class
1466
1466
    cache               IO cache
1471
1471
*/
1472
1472
 
1473
1473
 
1474
 
static File create_file(THD *thd, char *path, sql_exchange *exchange,
 
1474
static File create_file(Session *session, char *path, sql_exchange *exchange,
1475
1475
                        IO_CACHE *cache)
1476
1476
{
1477
1477
  File file;
1483
1483
 
1484
1484
  if (!dirname_length(exchange->file_name))
1485
1485
  {
1486
 
    strxnmov(path, FN_REFLEN-1, mysql_real_data_home, thd->db ? thd->db : "",
 
1486
    strxnmov(path, FN_REFLEN-1, mysql_real_data_home, session->db ? session->db : "",
1487
1487
             NULL);
1488
1488
    (void) fn_format(path, exchange->file_name, path, "", option);
1489
1489
  }
1530
1530
  if ((uint) strlen(exchange->file_name) + NAME_LEN >= FN_REFLEN)
1531
1531
    strmake(path,exchange->file_name,FN_REFLEN-1);
1532
1532
 
1533
 
  if ((file= create_file(thd, path, exchange, &cache)) < 0)
 
1533
  if ((file= create_file(session, path, exchange, &cache)) < 0)
1534
1534
    return 1;
1535
1535
  /* Check if there is any blobs in data */
1536
1536
  {
1573
1573
      (exchange->opt_enclosed && non_string_results &&
1574
1574
       field_term_length && strchr(NUMERIC_CHARS, field_term_char)))
1575
1575
  {
1576
 
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1576
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1577
1577
                 ER_AMBIGUOUS_FIELD_TERM, ER(ER_AMBIGUOUS_FIELD_TERM));
1578
1578
    is_ambiguous_field_term= true;
1579
1579
  }
1652
1652
      {
1653
1653
        char *pos, *start, *end;
1654
1654
        const CHARSET_INFO * const res_charset= res->charset();
1655
 
        const CHARSET_INFO * const character_set_client= thd->variables.
 
1655
        const CHARSET_INFO * const character_set_client= session->variables.
1656
1656
                                                            character_set_client;
1657
1657
        bool check_second_byte= (res_charset == &my_charset_bin) &&
1658
1658
                                 character_set_client->
1788
1788
                     SELECT_LEX_UNIT *u)
1789
1789
{
1790
1790
  unit= u;
1791
 
  return (int) ((file= create_file(thd, path, exchange, &cache)) < 0);
 
1791
  return (int) ((file= create_file(session, path, exchange, &cache)) < 0);
1792
1792
}
1793
1793
 
1794
1794
 
2048
2048
  Don't free mem_root, as mem_root is freed in the end of dispatch_command
2049
2049
  (once for any command).
2050
2050
*/
2051
 
void THD::end_statement()
 
2051
void Session::end_statement()
2052
2052
{
2053
2053
  /* Cleanup SQL processing state to reuse this statement in next query. */
2054
2054
  lex_end(lex);
2055
2055
}
2056
2056
 
2057
2057
 
2058
 
bool THD::copy_db_to(char **p_db, size_t *p_db_length)
 
2058
bool Session::copy_db_to(char **p_db, size_t *p_db_length)
2059
2059
{
2060
2060
  if (db == NULL)
2061
2061
  {
2090
2090
    if (mv->local == 0)
2091
2091
    {
2092
2092
      Item_func_set_user_var *suv= new Item_func_set_user_var(mv->s, item);
2093
 
      suv->fix_fields(thd, 0);
 
2093
      suv->fix_fields(session, 0);
2094
2094
      suv->check(0);
2095
2095
      suv->update();
2096
2096
    }
2097
2097
  }
2098
 
  return(thd->is_error());
 
2098
  return(session->is_error());
2099
2099
}
2100
2100
 
2101
2101
bool select_dumpvar::send_eof()
2102
2102
{
2103
2103
  if (! row_count)
2104
 
    push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2104
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2105
2105
                 ER_SP_FETCH_NO_DATA, ER(ER_SP_FETCH_NO_DATA));
2106
2106
  /*
2107
2107
    In order to remember the value of affected rows for ROW_COUNT()
2108
2108
    function, SELECT INTO has to have an own SQLCOM.
2109
2109
    TODO: split from SQLCOM_SELECT
2110
2110
  */
2111
 
  ::my_ok(thd,row_count);
 
2111
  ::my_ok(session,row_count);
2112
2112
  return 0;
2113
2113
}
2114
2114
 
2128
2128
}
2129
2129
 
2130
2130
 
2131
 
void thd_increment_bytes_sent(ulong length)
 
2131
void session_increment_bytes_sent(ulong length)
2132
2132
{
2133
 
  THD *thd=current_thd;
2134
 
  if (likely(thd != 0))
2135
 
  { /* current_thd==0 when close_connection() calls net_send_error() */
2136
 
    thd->status_var.bytes_sent+= length;
 
2133
  Session *session=current_session;
 
2134
  if (likely(session != 0))
 
2135
  { /* current_session==0 when close_connection() calls net_send_error() */
 
2136
    session->status_var.bytes_sent+= length;
2137
2137
  }
2138
2138
}
2139
2139
 
2140
2140
 
2141
 
void thd_increment_bytes_received(ulong length)
2142
 
{
2143
 
  current_thd->status_var.bytes_received+= length;
2144
 
}
2145
 
 
2146
 
 
2147
 
void thd_increment_net_big_packet_count(ulong length)
2148
 
{
2149
 
  current_thd->status_var.net_big_packet_count+= length;
2150
 
}
2151
 
 
2152
 
void THD::send_kill_message() const
 
2141
void session_increment_bytes_received(ulong length)
 
2142
{
 
2143
  current_session->status_var.bytes_received+= length;
 
2144
}
 
2145
 
 
2146
 
 
2147
void session_increment_net_big_packet_count(ulong length)
 
2148
{
 
2149
  current_session->status_var.net_big_packet_count+= length;
 
2150
}
 
2151
 
 
2152
void Session::send_kill_message() const
2153
2153
{
2154
2154
  int err= killed_errno();
2155
2155
  if (err)
2156
2156
    my_message(err, ER(err), MYF(0));
2157
2157
}
2158
2158
 
2159
 
void THD::set_status_var_init()
 
2159
void Session::set_status_var_init()
2160
2160
{
2161
2161
  memset(&status_var, 0, sizeof(status_var));
2162
2162
}
2198
2198
  access to mysql.proc table to find definitions of stored routines.
2199
2199
****************************************************************************/
2200
2200
 
2201
 
void THD::reset_n_backup_open_tables_state(Open_tables_state *backup)
 
2201
void Session::reset_n_backup_open_tables_state(Open_tables_state *backup)
2202
2202
{
2203
2203
  backup->set_open_tables_state(this);
2204
2204
  reset_open_tables_state();
2207
2207
}
2208
2208
 
2209
2209
 
2210
 
void THD::restore_backup_open_tables_state(Open_tables_state *backup)
 
2210
void Session::restore_backup_open_tables_state(Open_tables_state *backup)
2211
2211
{
2212
2212
  /*
2213
2213
    Before we will throw away current open tables state we want
2222
2222
 
2223
2223
/**
2224
2224
  Check the killed state of a user thread
2225
 
  @param thd  user thread
 
2225
  @param session  user thread
2226
2226
  @retval 0 the user thread is active
2227
2227
  @retval 1 the user thread has been killed
2228
2228
*/
2229
 
extern "C" int thd_killed(const THD *thd)
 
2229
extern "C" int session_killed(const Session *session)
2230
2230
{
2231
 
  return(thd->killed);
 
2231
  return(session->killed);
2232
2232
}
2233
2233
 
2234
2234
/**
2235
2235
  Return the thread id of a user thread
2236
 
  @param thd user thread
 
2236
  @param session user thread
2237
2237
  @return thread id
2238
2238
*/
2239
 
extern "C" unsigned long thd_get_thread_id(const THD *thd)
 
2239
extern "C" unsigned long session_get_thread_id(const Session *session)
2240
2240
{
2241
 
  return((unsigned long)thd->thread_id);
 
2241
  return((unsigned long)session->thread_id);
2242
2242
}
2243
2243
 
2244
2244
 
2245
2245
#ifdef INNODB_COMPATIBILITY_HOOKS
2246
 
extern "C" const struct charset_info_st *thd_charset(THD *thd)
2247
 
{
2248
 
  return(thd->charset());
2249
 
}
2250
 
 
2251
 
extern "C" char **thd_query(THD *thd)
2252
 
{
2253
 
  return(&thd->query);
2254
 
}
2255
 
 
2256
 
extern "C" int thd_slave_thread(const THD *thd)
2257
 
{
2258
 
  return(thd->slave_thread);
2259
 
}
2260
 
 
2261
 
extern "C" int thd_non_transactional_update(const THD *thd)
2262
 
{
2263
 
  return(thd->transaction.all.modified_non_trans_table);
2264
 
}
2265
 
 
2266
 
extern "C" int thd_binlog_format(const THD *thd)
2267
 
{
2268
 
  return (int) thd->variables.binlog_format;
2269
 
}
2270
 
 
2271
 
extern "C" void thd_mark_transaction_to_rollback(THD *thd, bool all)
2272
 
{
2273
 
  mark_transaction_to_rollback(thd, all);
 
2246
extern "C" const struct charset_info_st *session_charset(Session *session)
 
2247
{
 
2248
  return(session->charset());
 
2249
}
 
2250
 
 
2251
extern "C" char **session_query(Session *session)
 
2252
{
 
2253
  return(&session->query);
 
2254
}
 
2255
 
 
2256
extern "C" int session_slave_thread(const Session *session)
 
2257
{
 
2258
  return(session->slave_thread);
 
2259
}
 
2260
 
 
2261
extern "C" int session_non_transactional_update(const Session *session)
 
2262
{
 
2263
  return(session->transaction.all.modified_non_trans_table);
 
2264
}
 
2265
 
 
2266
extern "C" int session_binlog_format(const Session *session)
 
2267
{
 
2268
  return (int) session->variables.binlog_format;
 
2269
}
 
2270
 
 
2271
extern "C" void session_mark_transaction_to_rollback(Session *session, bool all)
 
2272
{
 
2273
  mark_transaction_to_rollback(session, all);
2274
2274
}
2275
2275
#endif // INNODB_COMPATIBILITY_HOOKS */
2276
2276
 
2278
2278
/**
2279
2279
  Mark transaction to rollback and mark error as fatal to a sub-statement.
2280
2280
 
2281
 
  @param  thd   Thread handle
 
2281
  @param  session   Thread handle
2282
2282
  @param  all   true <=> rollback main transaction.
2283
2283
*/
2284
2284
 
2285
 
void mark_transaction_to_rollback(THD *thd, bool all)
 
2285
void mark_transaction_to_rollback(Session *session, bool all)
2286
2286
{
2287
 
  if (thd)
 
2287
  if (session)
2288
2288
  {
2289
 
    thd->is_fatal_sub_stmt_error= true;
2290
 
    thd->transaction_rollback_request= all;
 
2289
    session->is_fatal_sub_stmt_error= true;
 
2290
    session->transaction_rollback_request= all;
2291
2291
  }
2292
2292
}
2293
2293
/***************************************************************************
2309
2309
 
2310
2310
void xid_free_hash(void *ptr)
2311
2311
{
2312
 
  if (!((XID_STATE*)ptr)->in_thd)
 
2312
  if (!((XID_STATE*)ptr)->in_session)
2313
2313
    free((unsigned char*)ptr);
2314
2314
}
2315
2315
 
2351
2351
  {
2352
2352
    xs->xa_state=xa_state;
2353
2353
    xs->xid.set(xid);
2354
 
    xs->in_thd=0;
 
2354
    xs->in_session=0;
2355
2355
    res=my_hash_insert(&xid_cache, (unsigned char*)xs);
2356
2356
  }
2357
2357
  pthread_mutex_unlock(&LOCK_xid_cache);
2392
2392
    - Events of type 'RowEventT' have the type code 'type_code'.
2393
2393
    
2394
2394
  POST CONDITION:
2395
 
    If a non-NULL pointer is returned, the pending event for thread 'thd' will
 
2395
    If a non-NULL pointer is returned, the pending event for thread 'session' will
2396
2396
    be an event of type 'RowEventT' (which have the type code 'type_code')
2397
2397
    will either empty or have enough space to hold 'needed' bytes.  In
2398
2398
    addition, the columns bitmap will be correct for the row, meaning that
2406
2406
 */
2407
2407
 
2408
2408
template <class RowsEventT> Rows_log_event* 
2409
 
THD::binlog_prepare_pending_rows_event(Table* table, uint32_t serv_id,
 
2409
Session::binlog_prepare_pending_rows_event(Table* table, uint32_t serv_id,
2410
2410
                                       size_t needed,
2411
2411
                                       bool is_transactional,
2412
2412
                                       RowsEventT *hint __attribute__((unused)))
2482
2482
  compiling option.
2483
2483
*/
2484
2484
template Rows_log_event*
2485
 
THD::binlog_prepare_pending_rows_event(Table*, uint32_t, size_t, bool,
 
2485
Session::binlog_prepare_pending_rows_event(Table*, uint32_t, size_t, bool,
2486
2486
                                       Write_rows_log_event*);
2487
2487
 
2488
2488
template Rows_log_event*
2489
 
THD::binlog_prepare_pending_rows_event(Table*, uint32_t, size_t, bool,
 
2489
Session::binlog_prepare_pending_rows_event(Table*, uint32_t, size_t, bool,
2490
2490
                                       Delete_rows_log_event *);
2491
2491
 
2492
2492
template Rows_log_event* 
2493
 
THD::binlog_prepare_pending_rows_event(Table*, uint32_t, size_t, bool,
 
2493
Session::binlog_prepare_pending_rows_event(Table*, uint32_t, size_t, bool,
2494
2494
                                       Update_rows_log_event *);
2495
2495
#endif
2496
2496
 
2608
2608
}
2609
2609
 
2610
2610
 
2611
 
int THD::binlog_write_row(Table* table, bool is_trans, 
 
2611
int Session::binlog_write_row(Table* table, bool is_trans, 
2612
2612
                          unsigned char const *record) 
2613
2613
2614
2614
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2635
2635
  return ev->add_row_data(row_data, len);
2636
2636
}
2637
2637
 
2638
 
int THD::binlog_update_row(Table* table, bool is_trans,
 
2638
int Session::binlog_update_row(Table* table, bool is_trans,
2639
2639
                           const unsigned char *before_record,
2640
2640
                           const unsigned char *after_record)
2641
2641
2669
2669
    ev->add_row_data(after_row, after_size);
2670
2670
}
2671
2671
 
2672
 
int THD::binlog_delete_row(Table* table, bool is_trans, 
 
2672
int Session::binlog_delete_row(Table* table, bool is_trans, 
2673
2673
                           unsigned char const *record)
2674
2674
2675
2675
  assert(current_stmt_binlog_row_based && mysql_bin_log.is_open());
2697
2697
}
2698
2698
 
2699
2699
 
2700
 
int THD::binlog_flush_pending_rows_event(bool stmt_end)
 
2700
int Session::binlog_flush_pending_rows_event(bool stmt_end)
2701
2701
{
2702
2702
  /*
2703
2703
    We shall flush the pending event even if we are not in row-based
2751
2751
  RETURN VALUE
2752
2752
    Error code, or 0 if no error.
2753
2753
*/
2754
 
int THD::binlog_query(THD::enum_binlog_query_type qtype, char const *query_arg,
 
2754
int Session::binlog_query(Session::enum_binlog_query_type qtype, char const *query_arg,
2755
2755
                      ulong query_len, bool is_trans, bool suppress_use,
2756
 
                      THD::killed_state killed_status_arg)
 
2756
                      Session::killed_state killed_status_arg)
2757
2757
{
2758
2758
  assert(query_arg && mysql_bin_log.is_open());
2759
2759
 
2782
2782
  }
2783
2783
 
2784
2784
  switch (qtype) {
2785
 
  case THD::ROW_QUERY_TYPE:
 
2785
  case Session::ROW_QUERY_TYPE:
2786
2786
    if (current_stmt_binlog_row_based)
2787
2787
      return(0);
2788
2788
    /* Otherwise, we fall through */
2789
 
  case THD::DRIZZLE_QUERY_TYPE:
 
2789
  case Session::DRIZZLE_QUERY_TYPE:
2790
2790
    /*
2791
2791
      Using this query type is a conveniece hack, since we have been
2792
2792
      moving back and forth between using RBR for replication of
2795
2795
      Make sure to change in check_table_binlog_row_based() according
2796
2796
      to how you treat this.
2797
2797
    */
2798
 
  case THD::STMT_QUERY_TYPE:
 
2798
  case Session::STMT_QUERY_TYPE:
2799
2799
    /*
2800
2800
      The DRIZZLE_LOG::write() function will set the STMT_END_F flag and
2801
2801
      flush the pending rows event if necessary.
2816
2816
    }
2817
2817
    break;
2818
2818
 
2819
 
  case THD::QUERY_TYPE_COUNT:
 
2819
  case Session::QUERY_TYPE_COUNT:
2820
2820
  default:
2821
2821
    assert(0 <= qtype && qtype < QUERY_TYPE_COUNT);
2822
2822
  }