~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: mordred
  • Date: 2008-11-01 00:46:20 UTC
  • mto: (572.1.1 devel) (575.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: mordred@opensolaris-20081101004620-vd0kzsl9k40hvf4p
Some updates to dtrace support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
 
#include "sql_repl.h"
19
 
#include "rpl_filter.h"
20
 
#include "repl_failsafe.h"
21
 
#include "logging.h"
22
 
#include <drizzled/drizzled_error_messages.h>
 
18
#include <drizzled/sql_repl.h>
 
19
#include <drizzled/rpl_filter.h>
 
20
#include <drizzled/logging.h>
 
21
#include <drizzled/error.h>
 
22
#include <drizzled/nested_join.h>
 
23
#include <drizzled/query_id.h>
 
24
#include <drizzled/sql_parse.h>
 
25
#include <drizzled/data_home.h>
23
26
 
24
27
/**
25
28
  @defgroup Runtime_Environment Runtime Environment
26
29
  @{
27
30
*/
28
31
 
29
 
 
 
32
extern const CHARSET_INFO *character_set_filesystem;
30
33
const char *any_db="*any*";     // Special symbol for check_access
31
34
 
32
35
const LEX_STRING command_name[COM_END+1]={
57
60
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
58
61
};
59
62
 
60
 
static void unlock_locked_tables(THD *thd)
 
63
static void unlock_locked_tables(Session *session)
61
64
{
62
 
  if (thd->locked_tables)
 
65
  if (session->locked_tables)
63
66
  {
64
 
    thd->lock=thd->locked_tables;
65
 
    thd->locked_tables=0;                       // Will be automatically closed
66
 
    close_thread_tables(thd);                   // Free tables
 
67
    session->lock=session->locked_tables;
 
68
    session->locked_tables=0;                   // Will be automatically closed
 
69
    close_thread_tables(session);                       // Free tables
67
70
  }
68
71
}
69
72
 
70
73
 
71
 
bool end_active_trans(THD *thd)
 
74
bool end_active_trans(Session *session)
72
75
{
73
 
  int error=0;
74
 
  if (unlikely(thd->in_sub_stmt))
75
 
  {
76
 
    my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
77
 
    return(1);
78
 
  }
79
 
  if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
76
  int error= 0;
 
77
 
 
78
  if (session->transaction.xid_state.xa_state != XA_NOTR)
80
79
  {
81
80
    my_error(ER_XAER_RMFAIL, MYF(0),
82
 
             xa_state_names[thd->transaction.xid_state.xa_state]);
 
81
             xa_state_names[session->transaction.xid_state.xa_state]);
83
82
    return(1);
84
83
  }
85
 
  if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
 
84
  if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
86
85
                      OPTION_TABLE_LOCK))
87
86
  {
88
87
    /* Safety if one did "drop table" on locked tables */
89
 
    if (!thd->locked_tables)
90
 
      thd->options&= ~OPTION_TABLE_LOCK;
91
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
92
 
    if (ha_commit(thd))
 
88
    if (!session->locked_tables)
 
89
      session->options&= ~OPTION_TABLE_LOCK;
 
90
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
 
91
    if (ha_commit(session))
93
92
      error=1;
94
93
  }
95
 
  thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
96
 
  thd->transaction.all.modified_non_trans_table= false;
 
94
  session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
95
  session->transaction.all.modified_non_trans_table= false;
97
96
  return(error);
98
97
}
99
98
 
100
99
 
101
 
bool begin_trans(THD *thd)
 
100
bool begin_trans(Session *session)
102
101
{
103
 
  int error=0;
104
 
  if (unlikely(thd->in_sub_stmt))
105
 
  {
106
 
    my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
107
 
    return 1;
108
 
  }
109
 
  if (thd->locked_tables)
110
 
  {
111
 
    thd->lock=thd->locked_tables;
112
 
    thd->locked_tables=0;                       // Will be automatically closed
113
 
    close_thread_tables(thd);                   // Free tables
114
 
  }
115
 
  if (end_active_trans(thd))
 
102
  int error= 0;
 
103
  if (session->locked_tables)
 
104
  {
 
105
    session->lock=session->locked_tables;
 
106
    session->locked_tables=0;                   // Will be automatically closed
 
107
    close_thread_tables(session);                       // Free tables
 
108
  }
 
109
  if (end_active_trans(session))
116
110
    error= -1;
117
111
  else
118
112
  {
119
 
    LEX *lex= thd->lex;
120
 
    thd->options|= OPTION_BEGIN;
121
 
    thd->server_status|= SERVER_STATUS_IN_TRANS;
 
113
    LEX *lex= session->lex;
 
114
    session->options|= OPTION_BEGIN;
 
115
    session->server_status|= SERVER_STATUS_IN_TRANS;
122
116
    if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
123
 
      error= ha_start_consistent_snapshot(thd);
 
117
      error= ha_start_consistent_snapshot(session);
124
118
  }
125
119
  return error;
126
120
}
128
122
/**
129
123
  Returns true if all tables should be ignored.
130
124
*/
131
 
inline bool all_tables_not_ok(THD *thd, TableList *tables)
 
125
inline bool all_tables_not_ok(Session *session, TableList *tables)
132
126
{
133
127
  return rpl_filter->is_on() && tables &&
134
 
         !rpl_filter->tables_ok(thd->db, tables);
 
128
         !rpl_filter->tables_ok(session->db, tables);
135
129
}
136
130
 
137
131
 
138
 
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
 
132
static bool some_non_temp_table_to_be_updated(Session *session, TableList *tables)
139
133
{
140
134
  for (TableList *table= tables; table; table= table->next_global)
141
135
  {
142
136
    assert(table->db && table->table_name);
143
137
    if (table->updating &&
144
 
        !find_temporary_table(thd, table->db, table->table_name))
 
138
        !find_temporary_table(session, table->db, table->table_name))
145
139
      return 1;
146
140
  }
147
141
  return 0;
193
187
  sql_command_flags[SQLCOM_SHOW_FIELDS]=      CF_STATUS_COMMAND;
194
188
  sql_command_flags[SQLCOM_SHOW_KEYS]=        CF_STATUS_COMMAND;
195
189
  sql_command_flags[SQLCOM_SHOW_VARIABLES]=   CF_STATUS_COMMAND;
196
 
  sql_command_flags[SQLCOM_SHOW_CHARSETS]=    CF_STATUS_COMMAND;
197
 
  sql_command_flags[SQLCOM_SHOW_COLLATIONS]=  CF_STATUS_COMMAND;
198
190
  sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
199
 
  sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
200
 
  sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
201
191
  sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
202
192
  sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
203
193
  sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
227
217
  return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
228
218
}
229
219
 
230
 
void execute_init_command(THD *thd, sys_var_str *init_command_var,
 
220
void execute_init_command(Session *session, sys_var_str *init_command_var,
231
221
                          rw_lock_t *var_mutex)
232
222
{
233
223
  Vio* save_vio;
234
224
  ulong save_client_capabilities;
235
225
 
236
 
  thd_proc_info(thd, "Execution of init_command");
 
226
  session->set_proc_info("Execution of init_command");
237
227
  /*
238
228
    We need to lock init_command_var because
239
229
    during execution of init_command_var query
240
230
    values of init_command_var can't be changed
241
231
  */
242
232
  rw_rdlock(var_mutex);
243
 
  save_client_capabilities= thd->client_capabilities;
244
 
  thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
233
  save_client_capabilities= session->client_capabilities;
 
234
  session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
245
235
  /*
246
236
    We don't need return result of execution to client side.
247
 
    To forbid this we should set thd->net.vio to 0.
 
237
    To forbid this we should set session->net.vio to 0.
248
238
  */
249
 
  save_vio= thd->net.vio;
250
 
  thd->net.vio= 0;
251
 
  dispatch_command(COM_QUERY, thd,
 
239
  save_vio= session->net.vio;
 
240
  session->net.vio= 0;
 
241
  dispatch_command(COM_QUERY, session,
252
242
                   init_command_var->value,
253
243
                   init_command_var->value_length);
254
244
  rw_unlock(var_mutex);
255
 
  thd->client_capabilities= save_client_capabilities;
256
 
  thd->net.vio= save_vio;
 
245
  session->client_capabilities= save_client_capabilities;
 
246
  session->net.vio= save_vio;
257
247
}
258
248
 
259
249
/**
260
250
  Ends the current transaction and (maybe) begin the next.
261
251
 
262
 
  @param thd            Current thread
 
252
  @param session            Current thread
263
253
  @param completion     Completion type
264
254
 
265
255
  @retval
266
256
    0   OK
267
257
*/
268
258
 
269
 
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
 
259
int end_trans(Session *session, enum enum_mysql_completiontype completion)
270
260
{
271
261
  bool do_release= 0;
272
262
  int res= 0;
273
263
 
274
 
  if (unlikely(thd->in_sub_stmt))
275
 
  {
276
 
    my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
277
 
    return(1);
278
 
  }
279
 
  if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
264
  if (session->transaction.xid_state.xa_state != XA_NOTR)
280
265
  {
281
266
    my_error(ER_XAER_RMFAIL, MYF(0),
282
 
             xa_state_names[thd->transaction.xid_state.xa_state]);
 
267
             xa_state_names[session->transaction.xid_state.xa_state]);
283
268
    return(1);
284
269
  }
285
270
  switch (completion) {
289
274
     even if there is a problem with the OPTION_AUTO_COMMIT flag
290
275
     (Which of course should never happen...)
291
276
    */
292
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
293
 
    res= ha_commit(thd);
294
 
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
295
 
    thd->transaction.all.modified_non_trans_table= false;
 
277
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
 
278
    res= ha_commit(session);
 
279
    session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
280
    session->transaction.all.modified_non_trans_table= false;
296
281
    break;
297
282
  case COMMIT_RELEASE:
298
283
    do_release= 1; /* fall through */
299
284
  case COMMIT_AND_CHAIN:
300
 
    res= end_active_trans(thd);
 
285
    res= end_active_trans(session);
301
286
    if (!res && completion == COMMIT_AND_CHAIN)
302
 
      res= begin_trans(thd);
 
287
      res= begin_trans(session);
303
288
    break;
304
289
  case ROLLBACK_RELEASE:
305
290
    do_release= 1; /* fall through */
306
291
  case ROLLBACK:
307
292
  case ROLLBACK_AND_CHAIN:
308
293
  {
309
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
310
 
    if (ha_rollback(thd))
 
294
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
 
295
    if (ha_rollback(session))
311
296
      res= -1;
312
 
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
 
    thd->transaction.all.modified_non_trans_table= false;
 
297
    session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
298
    session->transaction.all.modified_non_trans_table= false;
314
299
    if (!res && (completion == ROLLBACK_AND_CHAIN))
315
 
      res= begin_trans(thd);
 
300
      res= begin_trans(session);
316
301
    break;
317
302
  }
318
303
  default:
322
307
  }
323
308
 
324
309
  if (res < 0)
325
 
    my_error(thd->killed_errno(), MYF(0));
 
310
    my_error(session->killed_errno(), MYF(0));
326
311
  else if ((res == 0) && do_release)
327
 
    thd->killed= THD::KILL_CONNECTION;
 
312
    session->killed= Session::KILL_CONNECTION;
328
313
 
329
314
  return(res);
330
315
}
342
327
    1  request of thread shutdown (see dispatch_command() description)
343
328
*/
344
329
 
345
 
bool do_command(THD *thd)
 
330
bool do_command(Session *session)
346
331
{
347
332
  bool return_value;
348
333
  char *packet= 0;
349
334
  ulong packet_length;
350
 
  NET *net= &thd->net;
 
335
  NET *net= &session->net;
351
336
  enum enum_server_command command;
352
337
 
353
338
  /*
354
339
    indicator of uninitialized lex => normal flow of errors handling
355
340
    (see my_message_sql)
356
341
  */
357
 
  thd->lex->current_select= 0;
 
342
  session->lex->current_select= 0;
358
343
 
359
344
  /*
360
345
    This thread will do a blocking read from the client which
362
347
    the client, the connection is closed or "net_wait_timeout"
363
348
    number of seconds has passed
364
349
  */
365
 
  my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
 
350
  my_net_set_read_timeout(net, session->variables.net_wait_timeout);
366
351
 
367
352
  /*
368
353
    XXX: this code is here only to clear possible errors of init_connect. 
369
354
    Consider moving to init_connect() instead.
370
355
  */
371
 
  thd->clear_error();                           // Clear error message
372
 
  thd->main_da.reset_diagnostics_area();
 
356
  session->clear_error();                               // Clear error message
 
357
  session->main_da.reset_diagnostics_area();
373
358
 
374
359
  net_new_transaction(net);
375
360
 
379
364
    /* Check if we can continue without closing the connection */
380
365
 
381
366
    /* The error must be set. */
382
 
    assert(thd->is_error());
383
 
    net_end_statement(thd);
 
367
    assert(session->is_error());
 
368
    net_end_statement(session);
384
369
 
385
370
    if (net->error != 3)
386
371
    {
417
402
    command= COM_END;                           // Wrong command
418
403
 
419
404
  /* Restore read timeout value */
420
 
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
 
405
  my_net_set_read_timeout(net, session->variables.net_read_timeout);
421
406
 
422
407
  assert(packet_length);
423
 
  return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
 
408
  return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
424
409
 
425
410
out:
426
411
  return(return_value);
441
426
    @retval false The statement isn't updating any relevant tables.
442
427
*/
443
428
 
444
 
static bool deny_updates_if_read_only_option(THD *thd,
 
429
static bool deny_updates_if_read_only_option(Session *session,
445
430
                                                TableList *all_tables)
446
431
{
447
432
  if (!opt_readonly)
448
433
    return(false);
449
434
 
450
 
  LEX *lex= thd->lex;
 
435
  LEX *lex= session->lex;
451
436
 
452
437
  if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
453
438
    return(false);
465
450
    lex->drop_temporary;
466
451
 
467
452
  const bool update_real_tables=
468
 
    some_non_temp_table_to_be_updated(thd, all_tables) &&
 
453
    some_non_temp_table_to_be_updated(session, all_tables) &&
469
454
    !(create_temp_tables || drop_temp_tables);
470
455
 
471
456
 
490
475
  Perform one connection-level (COM_XXXX) command.
491
476
 
492
477
  @param command         type of command to perform
493
 
  @param thd             connection handle
 
478
  @param session             connection handle
494
479
  @param packet          data for the command, packet is always null-terminated
495
480
  @param packet_length   length of packet + 1 (to show that data is
496
481
                         null-terminated) except for COM_SLEEP, where it
497
482
                         can be zero.
498
483
 
499
484
  @todo
500
 
    set thd->lex->sql_command to SQLCOM_END here.
 
485
    set session->lex->sql_command to SQLCOM_END here.
501
486
  @todo
502
487
    The following has to be changed to an 8 byte integer
503
488
 
507
492
    1   request of thread shutdown, i. e. if command is
508
493
        COM_QUIT/COM_SHUTDOWN
509
494
*/
510
 
bool dispatch_command(enum enum_server_command command, THD *thd,
511
 
                      char* packet, uint32_t packet_length)
 
495
bool dispatch_command(enum enum_server_command command, Session *session,
 
496
                      char* packet, uint32_t packet_length)
512
497
{
513
 
  NET *net= &thd->net;
 
498
  NET *net= &session->net;
514
499
  bool error= 0;
 
500
  Query_id &query_id= Query_id::get_query_id();
515
501
 
516
 
  thd->command=command;
517
 
  /*
518
 
    Commands which always take a long time are logged into
519
 
    the slow log only if opt_log_slow_admin_statements is set.
520
 
  */
521
 
  thd->enable_slow_log= true;
522
 
  thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
523
 
  thd->set_time();
 
502
  session->command=command;
 
503
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
504
  session->set_time();
524
505
  pthread_mutex_lock(&LOCK_thread_count);
525
 
  thd->query_id= global_query_id;
 
506
  session->query_id= query_id.value();
526
507
 
527
508
  switch( command ) {
528
509
  /* Ignore these statements. */
530
511
    break;
531
512
  /* Increase id and count all other statements. */
532
513
  default:
533
 
    statistic_increment(thd->status_var.questions, &LOCK_status);
534
 
    next_query_id();
 
514
    statistic_increment(session->status_var.questions, &LOCK_status);
 
515
    query_id.next();
535
516
  }
536
517
 
537
518
  thread_running++;
538
 
  /* TODO: set thd->lex->sql_command to SQLCOM_END here */
 
519
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
539
520
  pthread_mutex_unlock(&LOCK_thread_count);
540
521
 
541
 
  logging_pre_do(thd);
 
522
  logging_pre_do(session);
542
523
 
543
 
  thd->server_status&=
 
524
  session->server_status&=
544
525
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
545
526
  switch (command) {
546
527
  case COM_INIT_DB:
547
528
  {
548
529
    LEX_STRING tmp;
549
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
550
 
    thd->convert_string(&tmp, system_charset_info,
551
 
                        packet, packet_length, thd->charset());
552
 
    if (!mysql_change_db(thd, &tmp, false))
 
530
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
 
531
    session->convert_string(&tmp, system_charset_info,
 
532
                        packet, packet_length, session->charset());
 
533
    if (!mysql_change_db(session, &tmp, false))
553
534
    {
554
 
      my_ok(thd);
 
535
      my_ok(session);
555
536
    }
556
537
    break;
557
538
  }
558
 
  case COM_REGISTER_SLAVE:
559
 
  {
560
 
    if (!register_slave(thd, (unsigned char*)packet, packet_length))
561
 
      my_ok(thd);
562
 
    break;
563
 
  }
564
539
  case COM_CHANGE_USER:
565
540
  {
566
 
    status_var_increment(thd->status_var.com_other);
 
541
    status_var_increment(session->status_var.com_other);
567
542
    char *user= (char*) packet, *packet_end= packet + packet_length;
568
543
    /* Safe because there is always a trailing \0 at the end of the packet */
569
544
    char *passwd= strchr(user, '\0')+1;
570
545
 
571
546
 
572
 
    thd->clear_error();                         // if errors from rollback
 
547
    session->clear_error();                         // if errors from rollback
573
548
 
574
549
    /*
575
550
      Old clients send null-terminated string ('\0' for empty string) for
591
566
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
592
567
      break;
593
568
    }
594
 
    uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
569
    uint32_t passwd_len= (session->client_capabilities & CLIENT_SECURE_CONNECTION ?
595
570
                      (unsigned char)(*passwd++) : strlen(passwd));
596
571
    uint32_t dummy_errors, save_db_length, db_length;
597
572
    int res;
598
 
    Security_context save_security_ctx= *thd->security_ctx;
 
573
    Security_context save_security_ctx= *session->security_ctx;
599
574
    USER_CONN *save_user_connect;
600
575
 
601
576
    db+= passwd_len + 1;
627
602
    /* Convert database name to utf8 */
628
603
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
629
604
                             system_charset_info, db, db_length,
630
 
                             thd->charset(), &dummy_errors)]= 0;
 
605
                             session->charset(), &dummy_errors)]= 0;
631
606
    db= db_buff;
632
607
 
633
608
    /* Save user and privileges */
634
 
    save_db_length= thd->db_length;
635
 
    save_db= thd->db;
636
 
    save_user_connect= thd->user_connect;
 
609
    save_db_length= session->db_length;
 
610
    save_db= session->db;
 
611
    save_user_connect= session->user_connect;
637
612
 
638
 
    if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
 
613
    if (!(session->security_ctx->user= my_strdup(user, MYF(0))))
639
614
    {
640
 
      thd->security_ctx->user= save_security_ctx.user;
 
615
      session->security_ctx->user= save_security_ctx.user;
641
616
      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
642
617
      break;
643
618
    }
644
619
 
645
620
    /* Clear variables that are allocated */
646
 
    thd->user_connect= 0;
647
 
    res= check_user(thd, passwd, passwd_len, db, false);
 
621
    session->user_connect= 0;
 
622
    res= check_user(session, passwd, passwd_len, db, false);
648
623
 
649
624
    if (res)
650
625
    {
651
 
      if (thd->security_ctx->user)
652
 
        free(thd->security_ctx->user);
653
 
      *thd->security_ctx= save_security_ctx;
654
 
      thd->user_connect= save_user_connect;
655
 
      thd->db= save_db;
656
 
      thd->db_length= save_db_length;
 
626
      if (session->security_ctx->user)
 
627
        free(session->security_ctx->user);
 
628
      *session->security_ctx= save_security_ctx;
 
629
      session->user_connect= save_user_connect;
 
630
      session->db= save_db;
 
631
      session->db_length= save_db_length;
657
632
    }
658
633
    else
659
634
    {
664
639
 
665
640
      if (cs_number)
666
641
      {
667
 
        thd_init_client_charset(thd, cs_number);
668
 
        thd->update_charset();
 
642
        session_init_client_charset(session, cs_number);
 
643
        session->update_charset();
669
644
      }
670
645
    }
671
646
    break;
672
647
  }
673
648
  case COM_QUERY:
674
649
  {
675
 
    if (alloc_query(thd, packet, packet_length))
 
650
    if (alloc_query(session, packet, packet_length))
676
651
      break;                                    // fatal error is set
677
 
    char *packet_end= thd->query + thd->query_length;
 
652
    char *packet_end= session->query + session->query_length;
678
653
    const char* end_of_stmt= NULL;
679
654
 
680
 
    mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
 
655
    mysql_parse(session, session->query, session->query_length, &end_of_stmt);
681
656
 
682
 
    while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
 
657
    while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
683
658
    {
684
659
      char *beginning_of_next_stmt= (char*) end_of_stmt;
685
660
 
686
 
      net_end_statement(thd);
 
661
      net_end_statement(session);
687
662
      /*
688
663
        Multiple queries exits, execute them individually
689
664
      */
690
 
      close_thread_tables(thd);
 
665
      close_thread_tables(session);
691
666
      ulong length= (ulong)(packet_end - beginning_of_next_stmt);
692
667
 
693
 
      log_slow_statement(thd);
 
668
      log_slow_statement(session);
694
669
 
695
670
      /* Remove garbage at start of query */
696
 
      while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
 
671
      while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
697
672
      {
698
673
        beginning_of_next_stmt++;
699
674
        length--;
700
675
      }
701
676
 
702
677
      pthread_mutex_lock(&LOCK_thread_count);
703
 
      thd->query_length= length;
704
 
      thd->query= beginning_of_next_stmt;
 
678
      session->query_length= length;
 
679
      session->query= beginning_of_next_stmt;
705
680
      /*
706
681
        Count each statement from the client.
707
682
      */
708
 
      statistic_increment(thd->status_var.questions, &LOCK_status);
709
 
      thd->query_id= next_query_id();
710
 
      thd->set_time(); /* Reset the query start time. */
711
 
      /* TODO: set thd->lex->sql_command to SQLCOM_END here */
 
683
      statistic_increment(session->status_var.questions, &LOCK_status);
 
684
      session->query_id= query_id.next();
 
685
      session->set_time(); /* Reset the query start time. */
 
686
      /* TODO: set session->lex->sql_command to SQLCOM_END here */
712
687
      pthread_mutex_unlock(&LOCK_thread_count);
713
688
 
714
 
      mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
 
689
      mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
715
690
    }
716
691
    break;
717
692
  }
723
698
    LEX_STRING conv_name;
724
699
 
725
700
    /* used as fields initializator */
726
 
    lex_start(thd);
 
701
    lex_start(session);
727
702
 
728
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
 
703
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
704
    memset(&table_list, 0, sizeof(table_list));
730
 
    if (thd->copy_db_to(&table_list.db, &table_list.db_length))
 
705
    if (session->copy_db_to(&table_list.db, &table_list.db_length))
731
706
      break;
732
707
    /*
733
708
      We have name + wildcard in packet, separated by endzero
734
709
    */
735
710
    arg_end= strchr(packet, '\0');
736
 
    thd->convert_string(&conv_name, system_charset_info,
737
 
                        packet, (uint32_t) (arg_end - packet), thd->charset());
 
711
    session->convert_string(&conv_name, system_charset_info,
 
712
                        packet, (uint32_t) (arg_end - packet), session->charset());
738
713
    table_list.alias= table_list.table_name= conv_name.str;
739
714
    packet= arg_end + 1;
740
715
 
741
716
    if (!my_strcasecmp(system_charset_info, table_list.db,
742
717
                       INFORMATION_SCHEMA_NAME.str))
743
718
    {
744
 
      ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
 
719
      ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
745
720
      if (schema_table)
746
721
        table_list.schema_table= schema_table;
747
722
    }
748
723
 
749
 
    thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
750
 
    if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
 
724
    session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
 
725
    if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
751
726
      break;
752
727
    if (lower_case_table_names)
753
728
      my_casedn_str(files_charset_info, table_list.table_name);
754
729
 
755
730
    /* init structures for VIEW processing */
756
 
    table_list.select_lex= &(thd->lex->select_lex);
757
 
 
758
 
    lex_start(thd);
759
 
    mysql_reset_thd_for_next_command(thd);
760
 
 
761
 
    thd->lex->
 
731
    table_list.select_lex= &(session->lex->select_lex);
 
732
 
 
733
    lex_start(session);
 
734
    mysql_reset_session_for_next_command(session);
 
735
 
 
736
    session->lex->
762
737
      select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
738
                                         (unsigned char**) &table_list.next_local);
764
 
    thd->lex->add_to_query_tables(&table_list);
 
739
    session->lex->add_to_query_tables(&table_list);
765
740
 
766
741
    /* switch on VIEW optimisation: do not fill temporary tables */
767
 
    thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
768
 
    mysqld_list_fields(thd,&table_list,fields);
769
 
    thd->lex->unit.cleanup();
770
 
    thd->cleanup_after_query();
 
742
    session->lex->sql_command= SQLCOM_SHOW_FIELDS;
 
743
    mysqld_list_fields(session,&table_list,fields);
 
744
    session->lex->unit.cleanup();
 
745
    session->cleanup_after_query();
771
746
    break;
772
747
  }
773
748
  case COM_QUIT:
774
749
    /* We don't calculate statistics for this command */
775
750
    net->error=0;                               // Don't give 'abort' message
776
 
    thd->main_da.disable_status();              // Don't send anything back
 
751
    session->main_da.disable_status();              // Don't send anything back
777
752
    error=true;                                 // End server
778
753
    break;
779
754
  case COM_BINLOG_DUMP:
782
757
      uint16_t flags;
783
758
      uint32_t slave_server_id;
784
759
 
785
 
      status_var_increment(thd->status_var.com_other);
786
 
      thd->enable_slow_log= opt_log_slow_admin_statements;
 
760
      status_var_increment(session->status_var.com_other);
787
761
      /* TODO: The following has to be changed to an 8 byte integer */
788
762
      pos = uint4korr(packet);
789
763
      flags = uint2korr(packet + 4);
790
 
      thd->server_id=0; /* avoid suicide */
 
764
      session->server_id=0; /* avoid suicide */
791
765
      if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
792
766
        kill_zombie_dump_threads(slave_server_id);
793
 
      thd->server_id = slave_server_id;
 
767
      session->server_id = slave_server_id;
794
768
 
795
 
      mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
796
 
      unregister_slave(thd,1,1);
 
769
      mysql_binlog_send(session, session->strdup(packet + 10), (my_off_t) pos, flags);
797
770
      /*  fake COM_QUIT -- if we get here, the thread needs to terminate */
798
771
      error = true;
799
772
      break;
800
773
    }
801
774
  case COM_SHUTDOWN:
802
775
  {
803
 
    status_var_increment(thd->status_var.com_other);
804
 
    my_eof(thd);
805
 
    close_thread_tables(thd);                   // Free before kill
 
776
    status_var_increment(session->status_var.com_other);
 
777
    my_eof(session);
 
778
    close_thread_tables(session);                       // Free before kill
806
779
    kill_mysql();
807
780
    error=true;
808
781
    break;
809
782
  }
810
783
  case COM_PING:
811
 
    status_var_increment(thd->status_var.com_other);
812
 
    my_ok(thd);                         // Tell client we are alive
 
784
    status_var_increment(session->status_var.com_other);
 
785
    my_ok(session);                             // Tell client we are alive
813
786
    break;
814
787
  case COM_PROCESS_INFO:
815
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
 
    mysqld_list_processes(thd, NULL, 0);
 
788
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
 
789
    mysqld_list_processes(session, NULL, 0);
817
790
    break;
818
791
  case COM_PROCESS_KILL:
819
792
  {
820
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
 
793
    status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
821
794
    ulong id=(ulong) uint4korr(packet);
822
 
    sql_kill(thd,id,false);
 
795
    sql_kill(session,id,false);
823
796
    break;
824
797
  }
825
798
  case COM_SET_OPTION:
826
799
  {
827
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
 
800
    status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
828
801
    uint32_t opt_command= uint2korr(packet);
829
802
 
830
803
    switch (opt_command) {
831
804
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
832
 
      thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
833
 
      my_eof(thd);
 
805
      session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
806
      my_eof(session);
834
807
      break;
835
808
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
836
 
      thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
837
 
      my_eof(thd);
 
809
      session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
 
810
      my_eof(session);
838
811
      break;
839
812
    default:
840
813
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
852
825
  }
853
826
 
854
827
  /* If commit fails, we should be able to reset the OK status. */
855
 
  thd->main_da.can_overwrite_status= true;
856
 
  ha_autocommit_or_rollback(thd, thd->is_error());
857
 
  thd->main_da.can_overwrite_status= false;
 
828
  session->main_da.can_overwrite_status= true;
 
829
  ha_autocommit_or_rollback(session, session->is_error());
 
830
  session->main_da.can_overwrite_status= false;
858
831
 
859
 
  thd->transaction.stmt.reset();
 
832
  session->transaction.stmt.reset();
860
833
 
861
834
 
862
835
  /* report error issued during command execution */
863
 
  if (thd->killed_errno())
864
 
  {
865
 
    if (! thd->main_da.is_set())
866
 
      thd->send_kill_message();
867
 
  }
868
 
  if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
869
 
  {
870
 
    thd->killed= THD::NOT_KILLED;
871
 
    thd->mysys_var->abort= 0;
872
 
  }
873
 
 
874
 
  net_end_statement(thd);
875
 
 
876
 
  thd->set_proc_info("closing tables");
 
836
  if (session->killed_errno())
 
837
  {
 
838
    if (! session->main_da.is_set())
 
839
      session->send_kill_message();
 
840
  }
 
841
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
 
842
  {
 
843
    session->killed= Session::NOT_KILLED;
 
844
    session->mysys_var->abort= 0;
 
845
  }
 
846
 
 
847
  net_end_statement(session);
 
848
 
 
849
  session->set_proc_info("closing tables");
877
850
  /* Free tables */
878
 
  close_thread_tables(thd);
879
 
 
880
 
  log_slow_statement(thd);
881
 
 
882
 
  thd_proc_info(thd, "cleaning up");
 
851
  close_thread_tables(session);
 
852
 
 
853
  log_slow_statement(session);
 
854
 
 
855
  session->set_proc_info("cleaning up");
883
856
  pthread_mutex_lock(&LOCK_thread_count); // For process list
884
 
  thd_proc_info(thd, 0);
885
 
  thd->command=COM_SLEEP;
886
 
  thd->query=0;
887
 
  thd->query_length=0;
 
857
  session->set_proc_info(0);
 
858
  session->command=COM_SLEEP;
 
859
  session->query=0;
 
860
  session->query_length=0;
888
861
  thread_running--;
889
862
  pthread_mutex_unlock(&LOCK_thread_count);
890
 
  thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
891
 
  free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
 
863
  session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
 
864
  free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
892
865
  return(error);
893
866
}
894
867
 
895
868
 
896
 
void log_slow_statement(THD *thd)
 
869
void log_slow_statement(Session *session)
897
870
{
898
 
  /*
899
 
    The following should never be true with our current code base,
900
 
    but better to keep this here so we don't accidently try to log a
901
 
    statement in a trigger or stored function
902
 
  */
903
 
  if (unlikely(thd->in_sub_stmt))
904
 
    return;                           // Don't set time for sub stmt
905
 
 
906
 
  logging_post_do(thd);
 
871
  logging_post_do(session);
907
872
 
908
873
  return;
909
874
}
917
882
    It prepares a SELECT_LEX and a TableList object to represent the
918
883
    given command as a SELECT parse tree.
919
884
 
920
 
  @param thd              thread handle
 
885
  @param session              thread handle
921
886
  @param lex              current lex
922
887
  @param table_ident      table alias if it's used
923
888
  @param schema_table_idx the type of the INFORMATION_SCHEMA table to be
935
900
                      in this version of the server.
936
901
*/
937
902
 
938
 
int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
 
903
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
939
904
                         enum enum_schema_tables schema_table_idx)
940
905
{
941
906
  SELECT_LEX *schema_select_lex= NULL;
974
939
    /* 'parent_lex' is used in init_query() so it must be before it. */
975
940
    schema_select_lex->parent_lex= lex;
976
941
    schema_select_lex->init_query();
977
 
    if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
 
942
    if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
978
943
      return(1);
979
944
    lex->query_tables_last= query_tables_last;
980
945
    break;
993
958
  
994
959
  SELECT_LEX *select_lex= lex->current_select;
995
960
  assert(select_lex);
996
 
  if (make_schema_select(thd, select_lex, schema_table_idx))
 
961
  if (make_schema_select(session, select_lex, schema_table_idx))
997
962
  {
998
963
    return(1);
999
964
  }
1006
971
 
1007
972
 
1008
973
/**
1009
 
  Read query from packet and store in thd->query.
 
974
  Read query from packet and store in session->query.
1010
975
  Used in COM_QUERY and COM_STMT_PREPARE.
1011
976
 
1012
 
    Sets the following THD variables:
 
977
    Sets the following Session variables:
1013
978
  - query
1014
979
  - query_length
1015
980
 
1016
981
  @retval
1017
982
    false ok
1018
983
  @retval
1019
 
    true  error;  In this case thd->fatal_error is set
 
984
    true  error;  In this case session->fatal_error is set
1020
985
*/
1021
986
 
1022
 
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
 
987
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
1023
988
{
1024
989
  /* Remove garbage at start and end of query */
1025
 
  while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
 
990
  while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
1026
991
  {
1027
992
    packet++;
1028
993
    packet_length--;
1029
994
  }
1030
995
  const char *pos= packet + packet_length;     // Point at end null
1031
996
  while (packet_length > 0 &&
1032
 
         (pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
 
997
         (pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
1033
998
  {
1034
999
    pos--;
1035
1000
    packet_length--;
1036
1001
  }
1037
1002
  /* We must allocate some extra memory for query cache */
1038
 
  thd->query_length= 0;                        // Extra safety: Avoid races
1039
 
  if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
 
1003
  session->query_length= 0;                        // Extra safety: Avoid races
 
1004
  if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
1040
1005
                                              packet_length,
1041
 
                                              thd->db_length+ 1)))
 
1006
                                              session->db_length+ 1)))
1042
1007
    return true;
1043
 
  thd->query[packet_length]=0;
1044
 
  thd->query_length= packet_length;
 
1008
  session->query[packet_length]=0;
 
1009
  session->query_length= packet_length;
1045
1010
 
1046
1011
  /* Reclaim some memory */
1047
 
  thd->packet.shrink(thd->variables.net_buffer_length);
1048
 
  thd->convert_buffer.shrink(thd->variables.net_buffer_length);
 
1012
  session->packet.shrink(session->variables.net_buffer_length);
 
1013
  session->convert_buffer.shrink(session->variables.net_buffer_length);
1049
1014
 
1050
1015
  return false;
1051
1016
}
1052
1017
 
1053
 
static void reset_one_shot_variables(THD *thd) 
 
1018
static void reset_one_shot_variables(Session *session) 
1054
1019
{
1055
 
  thd->variables.character_set_client=
 
1020
  session->variables.character_set_client=
1056
1021
    global_system_variables.character_set_client;
1057
 
  thd->variables.collation_connection=
 
1022
  session->variables.collation_connection=
1058
1023
    global_system_variables.collation_connection;
1059
 
  thd->variables.collation_database=
 
1024
  session->variables.collation_database=
1060
1025
    global_system_variables.collation_database;
1061
 
  thd->variables.collation_server=
 
1026
  session->variables.collation_server=
1062
1027
    global_system_variables.collation_server;
1063
 
  thd->update_charset();
1064
 
  thd->variables.time_zone=
 
1028
  session->update_charset();
 
1029
  session->variables.time_zone=
1065
1030
    global_system_variables.time_zone;
1066
 
  thd->variables.lc_time_names= &my_locale_en_US;
1067
 
  thd->one_shot_set= 0;
 
1031
  session->variables.lc_time_names= &my_locale_en_US;
 
1032
  session->one_shot_set= 0;
1068
1033
}
1069
1034
 
1070
1035
 
1071
1036
/**
1072
 
  Execute command saved in thd and lex->sql_command.
 
1037
  Execute command saved in session and lex->sql_command.
1073
1038
 
1074
1039
    Before every operation that can request a write lock for a table
1075
1040
    wait if a global read lock exists. However do not wait if this
1081
1046
    global read lock when it succeeds. This needs to be released by
1082
1047
    start_waiting_global_read_lock() after the operation.
1083
1048
 
1084
 
  @param thd                       Thread handle
 
1049
  @param session                       Thread handle
1085
1050
 
1086
1051
  @todo
1087
1052
    - Invalidate the table in the query cache if something changed
1099
1064
*/
1100
1065
 
1101
1066
int
1102
 
mysql_execute_command(THD *thd)
 
1067
mysql_execute_command(Session *session)
1103
1068
{
1104
1069
  int res= false;
1105
1070
  bool need_start_waiting= false; // have protection against global read lock
1106
1071
  int  up_result= 0;
1107
 
  LEX  *lex= thd->lex;
 
1072
  LEX  *lex= session->lex;
1108
1073
  /* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1109
1074
  SELECT_LEX *select_lex= &lex->select_lex;
1110
1075
  /* first table of first SELECT_LEX */
1146
1111
    Don't reset warnings when executing a stored routine.
1147
1112
  */
1148
1113
  if (all_tables || !lex->is_single_level_stmt())
1149
 
    drizzle_reset_errors(thd, 0);
 
1114
    drizzle_reset_errors(session, 0);
1150
1115
 
1151
 
  if (unlikely(thd->slave_thread))
 
1116
  if (unlikely(session->slave_thread))
1152
1117
  {
1153
1118
    /*
1154
1119
      Check if statment should be skipped because of slave filtering
1167
1132
        !(lex->sql_command == SQLCOM_SET_OPTION) &&
1168
1133
        !(lex->sql_command == SQLCOM_DROP_TABLE &&
1169
1134
          lex->drop_temporary && lex->drop_if_exists) &&
1170
 
        all_tables_not_ok(thd, all_tables))
 
1135
        all_tables_not_ok(session, all_tables))
1171
1136
    {
1172
1137
      /* we warn the slave SQL thread */
1173
1138
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1174
 
      if (thd->one_shot_set)
 
1139
      if (session->one_shot_set)
1175
1140
      {
1176
1141
        /*
1177
 
          It's ok to check thd->one_shot_set here:
 
1142
          It's ok to check session->one_shot_set here:
1178
1143
 
1179
1144
          The charsets in a MySQL 5.0 slave can change by both a binlogged
1180
1145
          SET ONE_SHOT statement and the event-internal charset setting, 
1186
1151
          problem because either the >= 5.0 slave reads a 4.1 binlog (with
1187
1152
          ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1188
1153
        */
1189
 
        reset_one_shot_variables(thd);
 
1154
        reset_one_shot_variables(session);
1190
1155
      }
1191
1156
      return(0);
1192
1157
    }
1197
1162
      When option readonly is set deny operations which change non-temporary
1198
1163
      tables. Except for the replication thread and the 'super' users.
1199
1164
    */
1200
 
    if (deny_updates_if_read_only_option(thd, all_tables))
 
1165
    if (deny_updates_if_read_only_option(session, all_tables))
1201
1166
    {
1202
1167
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1203
1168
      return(-1);
1204
1169
    }
1205
1170
  } /* endif unlikely slave */
1206
 
  status_var_increment(thd->status_var.com_stat[lex->sql_command]);
 
1171
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
1207
1172
 
1208
 
  assert(thd->transaction.stmt.modified_non_trans_table == false);
 
1173
  assert(session->transaction.stmt.modified_non_trans_table == false);
1209
1174
  
1210
1175
  switch (lex->sql_command) {
1211
1176
  case SQLCOM_SHOW_STATUS:
1212
1177
  {
1213
 
    system_status_var old_status_var= thd->status_var;
1214
 
    thd->initial_status_var= &old_status_var;
1215
 
    res= execute_sqlcom_select(thd, all_tables);
 
1178
    system_status_var old_status_var= session->status_var;
 
1179
    session->initial_status_var= &old_status_var;
 
1180
    res= execute_sqlcom_select(session, all_tables);
1216
1181
    /* Don't log SHOW STATUS commands to slow query log */
1217
 
    thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
 
1182
    session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1218
1183
                           SERVER_QUERY_NO_GOOD_INDEX_USED);
1219
1184
    /*
1220
1185
      restore status variables, as we don't want 'show status' to cause
1221
1186
      changes
1222
1187
    */
1223
1188
    pthread_mutex_lock(&LOCK_status);
1224
 
    add_diff_to_status(&global_status_var, &thd->status_var,
 
1189
    add_diff_to_status(&global_status_var, &session->status_var,
1225
1190
                       &old_status_var);
1226
 
    thd->status_var= old_status_var;
 
1191
    session->status_var= old_status_var;
1227
1192
    pthread_mutex_unlock(&LOCK_status);
1228
1193
    break;
1229
1194
  }
1234
1199
  case SQLCOM_SHOW_FIELDS:
1235
1200
  case SQLCOM_SHOW_KEYS:
1236
1201
  case SQLCOM_SHOW_VARIABLES:
1237
 
  case SQLCOM_SHOW_CHARSETS:
1238
 
  case SQLCOM_SHOW_COLLATIONS:
1239
1202
  case SQLCOM_SELECT:
1240
1203
  {
1241
 
    thd->status_var.last_query_cost= 0.0;
1242
 
    res= execute_sqlcom_select(thd, all_tables);
 
1204
    session->status_var.last_query_cost= 0.0;
 
1205
    res= execute_sqlcom_select(session, all_tables);
1243
1206
    break;
1244
1207
  }
1245
1208
  case SQLCOM_EMPTY_QUERY:
1246
 
    my_ok(thd);
 
1209
    my_ok(session);
1247
1210
    break;
1248
1211
 
1249
1212
  case SQLCOM_PURGE:
1250
1213
  {
1251
 
    res = purge_master_logs(thd, lex->to_log);
 
1214
    res = purge_master_logs(session, lex->to_log);
1252
1215
    break;
1253
1216
  }
1254
1217
  case SQLCOM_PURGE_BEFORE:
1257
1220
 
1258
1221
    /* PURGE MASTER LOGS BEFORE 'data' */
1259
1222
    it= (Item *)lex->value_list.head();
1260
 
    if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
 
1223
    if ((!it->fixed && it->fix_fields(lex->session, &it)) ||
1261
1224
        it->check_cols(1))
1262
1225
    {
1263
1226
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1269
1232
      value of constant
1270
1233
    */
1271
1234
    it->quick_fix_field();
1272
 
    res = purge_master_logs_before_date(thd, (ulong)it->val_int());
 
1235
    res = purge_master_logs_before_date(session, (ulong)it->val_int());
1273
1236
    break;
1274
1237
  }
1275
1238
  case SQLCOM_SHOW_WARNS:
1276
1239
  {
1277
 
    res= mysqld_show_warnings(thd, (uint32_t)
 
1240
    res= mysqld_show_warnings(session, (uint32_t)
1278
1241
                              ((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1279
1242
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1280
1243
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1283
1246
  }
1284
1247
  case SQLCOM_SHOW_ERRORS:
1285
1248
  {
1286
 
    res= mysqld_show_warnings(thd, (uint32_t)
 
1249
    res= mysqld_show_warnings(session, (uint32_t)
1287
1250
                              (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1288
1251
    break;
1289
1252
  }
1290
 
  case SQLCOM_SHOW_SLAVE_HOSTS:
1291
 
  {
1292
 
    res = show_slave_hosts(thd);
1293
 
    break;
1294
 
  }
1295
 
  case SQLCOM_SHOW_BINLOG_EVENTS:
1296
 
  {
1297
 
    res = mysql_show_binlog_events(thd);
1298
 
    break;
1299
 
  }
1300
 
 
1301
1253
  case SQLCOM_ASSIGN_TO_KEYCACHE:
1302
1254
  {
1303
1255
    assert(first_table == all_tables && first_table != 0);
1304
 
    res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
 
1256
    res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1305
1257
    break;
1306
1258
  }
1307
1259
  case SQLCOM_CHANGE_MASTER:
1308
1260
  {
1309
1261
    pthread_mutex_lock(&LOCK_active_mi);
1310
 
    res = change_master(thd,active_mi);
 
1262
    res = change_master(session,active_mi);
1311
1263
    pthread_mutex_unlock(&LOCK_active_mi);
1312
1264
    break;
1313
1265
  }
1316
1268
    pthread_mutex_lock(&LOCK_active_mi);
1317
1269
    if (active_mi != NULL)
1318
1270
    {
1319
 
      res = show_master_info(thd, active_mi);
 
1271
      res = show_master_info(session, active_mi);
1320
1272
    }
1321
1273
    else
1322
1274
    {
1323
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1275
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1324
1276
                   "the master info structure does not exist");
1325
 
      my_ok(thd);
 
1277
      my_ok(session);
1326
1278
    }
1327
1279
    pthread_mutex_unlock(&LOCK_active_mi);
1328
1280
    break;
1329
1281
  }
1330
1282
  case SQLCOM_SHOW_MASTER_STAT:
1331
1283
  {
1332
 
    res = show_binlog_info(thd);
 
1284
    res = show_binlog_info(session);
1333
1285
    break;
1334
1286
  }
1335
1287
 
1336
1288
  case SQLCOM_SHOW_ENGINE_STATUS:
1337
1289
    {
1338
 
      res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
 
1290
      res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1339
1291
      break;
1340
1292
    }
1341
1293
  case SQLCOM_CREATE_TABLE:
1343
1295
    /* If CREATE TABLE of non-temporary table, do implicit commit */
1344
1296
    if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1345
1297
    {
1346
 
      if (end_active_trans(thd))
 
1298
      if (end_active_trans(session))
1347
1299
      {
1348
1300
        res= -1;
1349
1301
        break;
1367
1319
      safety, only in case of Alter_info we have to do (almost) a deep
1368
1320
      copy.
1369
1321
    */
1370
 
    Alter_info alter_info(lex->alter_info, thd->mem_root);
 
1322
    Alter_info alter_info(lex->alter_info, session->mem_root);
1371
1323
 
1372
 
    if (thd->is_fatal_error)
 
1324
    if (session->is_fatal_error)
1373
1325
    {
1374
1326
      /* If out of memory when creating a copy of alter_info. */
1375
1327
      res= 1;
1376
1328
      goto end_with_restore_list;
1377
1329
    }
1378
1330
 
1379
 
    if ((res= create_table_precheck(thd, select_tables, create_table)))
 
1331
    if ((res= create_table_precheck(session, select_tables, create_table)))
1380
1332
      goto end_with_restore_list;
1381
1333
 
1382
1334
    /* Might have been updated in create_table_precheck */
1384
1336
 
1385
1337
#ifdef HAVE_READLINK
1386
1338
    /* Fix names if symlinked tables */
1387
 
    if (append_file_to_dir(thd, &create_info.data_file_name,
 
1339
    if (append_file_to_dir(session, &create_info.data_file_name,
1388
1340
                           create_table->table_name) ||
1389
 
        append_file_to_dir(thd, &create_info.index_file_name,
 
1341
        append_file_to_dir(session, &create_info.index_file_name,
1390
1342
                           create_table->table_name))
1391
1343
      goto end_with_restore_list;
1392
1344
#endif
1416
1368
      TABLE in the same way. That way we avoid that a new table is
1417
1369
      created during a gobal read lock.
1418
1370
    */
1419
 
    if (!thd->locked_tables &&
1420
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1371
    if (!session->locked_tables &&
 
1372
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1421
1373
    {
1422
1374
      res= 1;
1423
1375
      goto end_with_restore_list;
1435
1387
        create_table->create= true;
1436
1388
      }
1437
1389
 
1438
 
      if (!(res= open_and_lock_tables(thd, lex->query_tables)))
 
1390
      if (!(res= open_and_lock_tables(session, lex->query_tables)))
1439
1391
      {
1440
1392
        /*
1441
1393
          Is table which we are changing used somewhere in other parts
1445
1397
        {
1446
1398
          TableList *duplicate;
1447
1399
          create_table= lex->unlink_first_table(&link_to_local);
1448
 
          if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
 
1400
          if ((duplicate= unique_table(session, create_table, select_tables, 0)))
1449
1401
          {
1450
1402
            update_non_unique_table_error(create_table, "CREATE", duplicate);
1451
1403
            res= 1;
1469
1421
            CREATE from SELECT give its SELECT_LEX for SELECT,
1470
1422
            and item_list belong to SELECT
1471
1423
          */
1472
 
          res= handle_select(thd, lex, result, 0);
 
1424
          res= handle_select(session, lex, result, 0);
1473
1425
          delete result;
1474
1426
        }
1475
1427
      }
1481
1433
    {
1482
1434
      /* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1483
1435
      if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1484
 
        thd->options|= OPTION_KEEP_LOG;
 
1436
        session->options|= OPTION_KEEP_LOG;
1485
1437
      /* regular create */
1486
1438
      if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1487
 
        res= mysql_create_like_table(thd, create_table, select_tables,
 
1439
        res= mysql_create_like_table(session, create_table, select_tables,
1488
1440
                                     &create_info);
1489
1441
      else
1490
1442
      {
1491
 
        res= mysql_create_table(thd, create_table->db,
 
1443
        res= mysql_create_table(session, create_table->db,
1492
1444
                                create_table->table_name, &create_info,
1493
1445
                                &alter_info, 0, 0);
1494
1446
      }
1495
1447
      if (!res)
1496
 
        my_ok(thd);
 
1448
        my_ok(session);
1497
1449
    }
1498
1450
 
1499
1451
    /* put tables back for PS rexecuting */
1515
1467
  {
1516
1468
    /* Prepare stack copies to be re-execution safe */
1517
1469
    HA_CREATE_INFO create_info;
1518
 
    Alter_info alter_info(lex->alter_info, thd->mem_root);
 
1470
    Alter_info alter_info(lex->alter_info, session->mem_root);
1519
1471
 
1520
 
    if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
 
1472
    if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1521
1473
      goto error;
1522
1474
 
1523
1475
    assert(first_table == all_tables && first_table != 0);
1524
 
    if (end_active_trans(thd))
 
1476
    if (end_active_trans(session))
1525
1477
      goto error;
1526
 
    /*
1527
 
      Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1528
 
      and thus classify as slow administrative statements just like
1529
 
      ALTER TABLE.
1530
 
    */
1531
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1532
1478
 
1533
1479
    memset(&create_info, 0, sizeof(create_info));
1534
1480
    create_info.db_type= 0;
1535
1481
    create_info.row_type= ROW_TYPE_NOT_USED;
1536
 
    create_info.default_table_charset= thd->variables.collation_database;
 
1482
    create_info.default_table_charset= session->variables.collation_database;
1537
1483
 
1538
 
    res= mysql_alter_table(thd, first_table->db, first_table->table_name,
 
1484
    res= mysql_alter_table(session, first_table->db, first_table->table_name,
1539
1485
                           &create_info, first_table, &alter_info,
1540
1486
                           0, (order_st*) 0, 0);
1541
1487
    break;
1543
1489
  case SQLCOM_SLAVE_START:
1544
1490
  {
1545
1491
    pthread_mutex_lock(&LOCK_active_mi);
1546
 
    start_slave(thd,active_mi,1 /* net report*/);
 
1492
    start_slave(session,active_mi,1 /* net report*/);
1547
1493
    pthread_mutex_unlock(&LOCK_active_mi);
1548
1494
    break;
1549
1495
  }
1561
1507
    To prevent that, refuse SLAVE STOP if the
1562
1508
    client thread has locked tables
1563
1509
  */
1564
 
  if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
 
1510
  if (session->locked_tables || session->active_transaction() || session->global_read_lock)
1565
1511
  {
1566
1512
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1567
1513
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1569
1515
  }
1570
1516
  {
1571
1517
    pthread_mutex_lock(&LOCK_active_mi);
1572
 
    stop_slave(thd,active_mi,1/* net report*/);
 
1518
    stop_slave(session,active_mi,1/* net report*/);
1573
1519
    pthread_mutex_unlock(&LOCK_active_mi);
1574
1520
    break;
1575
1521
  }
1584
1530
        referenced from this structure will be modified.
1585
1531
      */
1586
1532
      HA_CREATE_INFO create_info(lex->create_info);
1587
 
      Alter_info alter_info(lex->alter_info, thd->mem_root);
 
1533
      Alter_info alter_info(lex->alter_info, session->mem_root);
1588
1534
 
1589
 
      if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
 
1535
      if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1590
1536
      {
1591
1537
        goto error;
1592
1538
      }
1603
1549
 
1604
1550
      /* Don't yet allow changing of symlinks with ALTER TABLE */
1605
1551
      if (create_info.data_file_name)
1606
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1552
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1607
1553
                     "DATA DIRECTORY option ignored");
1608
1554
      if (create_info.index_file_name)
1609
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1555
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1610
1556
                     "INDEX DIRECTORY option ignored");
1611
1557
      create_info.data_file_name= create_info.index_file_name= NULL;
1612
1558
      /* ALTER TABLE ends previous transaction */
1613
 
      if (end_active_trans(thd))
 
1559
      if (end_active_trans(session))
1614
1560
        goto error;
1615
1561
 
1616
 
      if (!thd->locked_tables &&
1617
 
          !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1562
      if (!session->locked_tables &&
 
1563
          !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1618
1564
      {
1619
1565
        res= 1;
1620
1566
        break;
1621
1567
      }
1622
1568
 
1623
 
      thd->enable_slow_log= opt_log_slow_admin_statements;
1624
 
      res= mysql_alter_table(thd, select_lex->db, lex->name.str,
 
1569
      res= mysql_alter_table(session, select_lex->db, lex->name.str,
1625
1570
                             &create_info,
1626
1571
                             first_table,
1627
1572
                             &alter_info,
1645
1590
      new_list= table->next_local[0];
1646
1591
    }
1647
1592
 
1648
 
    if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
 
1593
    if (end_active_trans(session) || mysql_rename_tables(session, first_table, 0))
1649
1594
      {
1650
1595
        goto error;
1651
1596
      }
1653
1598
  }
1654
1599
  case SQLCOM_SHOW_BINLOGS:
1655
1600
    {
1656
 
      res = show_binlogs(thd);
 
1601
      res = show_binlogs(session);
1657
1602
      break;
1658
1603
    }
1659
1604
  case SQLCOM_SHOW_CREATE:
1660
1605
    assert(first_table == all_tables && first_table != 0);
1661
1606
    {
1662
 
      res= mysqld_show_create(thd, first_table);
 
1607
      res= mysqld_show_create(session, first_table);
1663
1608
      break;
1664
1609
    }
1665
1610
  case SQLCOM_CHECKSUM:
1666
1611
  {
1667
1612
    assert(first_table == all_tables && first_table != 0);
1668
 
    res = mysql_checksum_table(thd, first_table, &lex->check_opt);
 
1613
    res = mysql_checksum_table(session, first_table, &lex->check_opt);
1669
1614
    break;
1670
1615
  }
1671
1616
  case SQLCOM_REPAIR:
1672
1617
  {
1673
1618
    assert(first_table == all_tables && first_table != 0);
1674
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1675
 
    res= mysql_repair_table(thd, first_table, &lex->check_opt);
 
1619
    res= mysql_repair_table(session, first_table, &lex->check_opt);
1676
1620
    /* ! we write after unlocking the table */
1677
1621
    /*
1678
1622
      Presumably, REPAIR and binlog writing doesn't require synchronization
1679
1623
    */
1680
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
1624
    write_bin_log(session, true, session->query, session->query_length);
1681
1625
    select_lex->table_list.first= (unsigned char*) first_table;
1682
1626
    lex->query_tables=all_tables;
1683
1627
    break;
1685
1629
  case SQLCOM_CHECK:
1686
1630
  {
1687
1631
    assert(first_table == all_tables && first_table != 0);
1688
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1689
 
    res = mysql_check_table(thd, first_table, &lex->check_opt);
 
1632
    res = mysql_check_table(session, first_table, &lex->check_opt);
1690
1633
    select_lex->table_list.first= (unsigned char*) first_table;
1691
1634
    lex->query_tables=all_tables;
1692
1635
    break;
1694
1637
  case SQLCOM_ANALYZE:
1695
1638
  {
1696
1639
    assert(first_table == all_tables && first_table != 0);
1697
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1698
 
    res= mysql_analyze_table(thd, first_table, &lex->check_opt);
 
1640
    res= mysql_analyze_table(session, first_table, &lex->check_opt);
1699
1641
    /* ! we write after unlocking the table */
1700
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
1642
    write_bin_log(session, true, session->query, session->query_length);
1701
1643
    select_lex->table_list.first= (unsigned char*) first_table;
1702
1644
    lex->query_tables=all_tables;
1703
1645
    break;
1706
1648
  case SQLCOM_OPTIMIZE:
1707
1649
  {
1708
1650
    assert(first_table == all_tables && first_table != 0);
1709
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1710
 
    res= mysql_optimize_table(thd, first_table, &lex->check_opt);
 
1651
    res= mysql_optimize_table(session, first_table, &lex->check_opt);
1711
1652
    /* ! we write after unlocking the table */
1712
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
1653
    write_bin_log(session, true, session->query, session->query_length);
1713
1654
    select_lex->table_list.first= (unsigned char*) first_table;
1714
1655
    lex->query_tables=all_tables;
1715
1656
    break;
1716
1657
  }
1717
1658
  case SQLCOM_UPDATE:
1718
1659
    assert(first_table == all_tables && first_table != 0);
1719
 
    if (update_precheck(thd, all_tables))
 
1660
    if (update_precheck(session, all_tables))
1720
1661
      break;
1721
1662
    assert(select_lex->offset_limit == 0);
1722
1663
    unit->set_limit(select_lex);
1723
 
    res= (up_result= mysql_update(thd, all_tables,
 
1664
    res= (up_result= mysql_update(session, all_tables,
1724
1665
                                  select_lex->item_list,
1725
1666
                                  lex->value_list,
1726
1667
                                  select_lex->where,
1738
1679
    /* if we switched from normal update, rights are checked */
1739
1680
    if (up_result != 2)
1740
1681
    {
1741
 
      if ((res= multi_update_precheck(thd, all_tables)))
 
1682
      if ((res= multi_update_precheck(session, all_tables)))
1742
1683
        break;
1743
1684
    }
1744
1685
    else
1745
1686
      res= 0;
1746
1687
 
1747
 
    res= mysql_multi_update_prepare(thd);
 
1688
    res= mysql_multi_update_prepare(session);
1748
1689
 
1749
1690
    /* Check slave filtering rules */
1750
 
    if (unlikely(thd->slave_thread))
 
1691
    if (unlikely(session->slave_thread))
1751
1692
    {
1752
 
      if (all_tables_not_ok(thd, all_tables))
 
1693
      if (all_tables_not_ok(session, all_tables))
1753
1694
      {
1754
1695
        if (res!= 0)
1755
1696
        {
1756
1697
          res= 0;             /* don't care of prev failure  */
1757
 
          thd->clear_error(); /* filters are of highest prior */
 
1698
          session->clear_error(); /* filters are of highest prior */
1758
1699
        }
1759
1700
        /* we warn the slave SQL thread */
1760
1701
        my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1768
1709
      if (res)
1769
1710
        break;
1770
1711
      if (opt_readonly &&
1771
 
          some_non_temp_table_to_be_updated(thd, all_tables))
 
1712
          some_non_temp_table_to_be_updated(session, all_tables))
1772
1713
      {
1773
1714
        my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1774
1715
        break;
1775
1716
      }
1776
1717
    }  /* unlikely */
1777
1718
 
1778
 
    res= mysql_multi_update(thd, all_tables,
 
1719
    res= mysql_multi_update(session, all_tables,
1779
1720
                            &select_lex->item_list,
1780
1721
                            &lex->value_list,
1781
1722
                            select_lex->where,
1787
1728
  case SQLCOM_INSERT:
1788
1729
  {
1789
1730
    assert(first_table == all_tables && first_table != 0);
1790
 
    if ((res= insert_precheck(thd, all_tables)))
 
1731
    if ((res= insert_precheck(session, all_tables)))
1791
1732
      break;
1792
1733
 
1793
 
    if (!thd->locked_tables &&
1794
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1734
    if (!session->locked_tables &&
 
1735
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1795
1736
    {
1796
1737
      res= 1;
1797
1738
      break;
1798
1739
    }
1799
1740
 
1800
 
    res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
 
1741
    res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1801
1742
                      lex->update_list, lex->value_list,
1802
1743
                      lex->duplicates, lex->ignore);
1803
1744
 
1808
1749
  {
1809
1750
    select_result *sel_result;
1810
1751
    assert(first_table == all_tables && first_table != 0);
1811
 
    if ((res= insert_precheck(thd, all_tables)))
 
1752
    if ((res= insert_precheck(session, all_tables)))
1812
1753
      break;
1813
1754
 
1814
1755
    /* Fix lock for first table */
1820
1761
 
1821
1762
    unit->set_limit(select_lex);
1822
1763
 
1823
 
    if (! thd->locked_tables &&
1824
 
        ! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
 
1764
    if (! session->locked_tables &&
 
1765
        ! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1825
1766
    {
1826
1767
      res= 1;
1827
1768
      break;
1828
1769
    }
1829
1770
 
1830
 
    if (!(res= open_and_lock_tables(thd, all_tables)))
 
1771
    if (!(res= open_and_lock_tables(session, all_tables)))
1831
1772
    {
1832
1773
      /* Skip first table, which is the table we are inserting in */
1833
1774
      TableList *second_table= first_table->next_local;
1834
1775
      select_lex->table_list.first= (unsigned char*) second_table;
1835
1776
      select_lex->context.table_list= 
1836
1777
        select_lex->context.first_name_resolution_table= second_table;
1837
 
      res= mysql_insert_select_prepare(thd);
 
1778
      res= mysql_insert_select_prepare(session);
1838
1779
      if (!res && (sel_result= new select_insert(first_table,
1839
1780
                                                 first_table->table,
1840
1781
                                                 &lex->field_list,
1843
1784
                                                 lex->duplicates,
1844
1785
                                                 lex->ignore)))
1845
1786
      {
1846
 
        res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
 
1787
        res= handle_select(session, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1847
1788
        /*
1848
1789
          Invalidate the table in the query cache if something changed
1849
1790
          after unlocking when changes become visible.
1851
1792
          the unlock procedure.
1852
1793
        */
1853
1794
        if (first_table->lock_type ==  TL_WRITE_CONCURRENT_INSERT &&
1854
 
            thd->lock)
 
1795
            session->lock)
1855
1796
        {
1856
1797
          /* INSERT ... SELECT should invalidate only the very first table */
1857
1798
          TableList *save_table= first_table->next_local;
1867
1808
    break;
1868
1809
  }
1869
1810
  case SQLCOM_TRUNCATE:
1870
 
    if (end_active_trans(thd))
 
1811
    if (end_active_trans(session))
1871
1812
    {
1872
1813
      res= -1;
1873
1814
      break;
1877
1818
      Don't allow this within a transaction because we want to use
1878
1819
      re-generate table
1879
1820
    */
1880
 
    if (thd->locked_tables || thd->active_transaction())
 
1821
    if (session->locked_tables || session->active_transaction())
1881
1822
    {
1882
1823
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1883
1824
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1884
1825
      goto error;
1885
1826
    }
1886
1827
 
1887
 
    res= mysql_truncate(thd, first_table, 0);
 
1828
    res= mysql_truncate(session, first_table, 0);
1888
1829
 
1889
1830
    break;
1890
1831
  case SQLCOM_DELETE:
1893
1834
    assert(select_lex->offset_limit == 0);
1894
1835
    unit->set_limit(select_lex);
1895
1836
 
1896
 
    if (!thd->locked_tables &&
1897
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1837
    if (!session->locked_tables &&
 
1838
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1898
1839
    {
1899
1840
      res= 1;
1900
1841
      break;
1901
1842
    }
1902
1843
 
1903
 
    res = mysql_delete(thd, all_tables, select_lex->where,
 
1844
    res = mysql_delete(session, all_tables, select_lex->where,
1904
1845
                       &select_lex->order_list,
1905
1846
                       unit->select_limit_cnt, select_lex->options,
1906
1847
                       false);
1910
1851
  {
1911
1852
    assert(first_table == all_tables && first_table != 0);
1912
1853
    TableList *aux_tables=
1913
 
      (TableList *)thd->lex->auxiliary_table_list.first;
 
1854
      (TableList *)session->lex->auxiliary_table_list.first;
1914
1855
    multi_delete *del_result;
1915
1856
 
1916
 
    if (!thd->locked_tables &&
1917
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1857
    if (!session->locked_tables &&
 
1858
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1918
1859
    {
1919
1860
      res= 1;
1920
1861
      break;
1921
1862
    }
1922
1863
 
1923
 
    if ((res= multi_delete_precheck(thd, all_tables)))
 
1864
    if ((res= multi_delete_precheck(session, all_tables)))
1924
1865
      break;
1925
1866
 
1926
1867
    /* condition will be true on SP re-excuting */
1927
1868
    if (select_lex->item_list.elements != 0)
1928
1869
      select_lex->item_list.empty();
1929
 
    if (add_item_to_list(thd, new Item_null()))
 
1870
    if (add_item_to_list(session, new Item_null()))
1930
1871
      goto error;
1931
1872
 
1932
 
    thd_proc_info(thd, "init");
1933
 
    if ((res= open_and_lock_tables(thd, all_tables)))
 
1873
    session->set_proc_info("init");
 
1874
    if ((res= open_and_lock_tables(session, all_tables)))
1934
1875
      break;
1935
1876
 
1936
 
    if ((res= mysql_multi_delete_prepare(thd)))
 
1877
    if ((res= mysql_multi_delete_prepare(session)))
1937
1878
      goto error;
1938
1879
 
1939
 
    if (!thd->is_fatal_error &&
 
1880
    if (!session->is_fatal_error &&
1940
1881
        (del_result= new multi_delete(aux_tables, lex->table_count)))
1941
1882
    {
1942
 
      res= mysql_select(thd, &select_lex->ref_pointer_array,
 
1883
      res= mysql_select(session, &select_lex->ref_pointer_array,
1943
1884
                        select_lex->get_table_list(),
1944
1885
                        select_lex->with_wild,
1945
1886
                        select_lex->item_list,
1946
1887
                        select_lex->where,
1947
1888
                        0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1948
1889
                        (order_st *)NULL,
1949
 
                        select_lex->options | thd->options |
 
1890
                        select_lex->options | session->options |
1950
1891
                        SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
1892
                        OPTION_SETUP_TABLES_DONE,
1952
1893
                        del_result, unit, select_lex);
1953
 
      res|= thd->is_error();
 
1894
      res|= session->is_error();
1954
1895
      if (res)
1955
1896
        del_result->abort();
1956
1897
      delete del_result;
1964
1905
    assert(first_table == all_tables && first_table != 0);
1965
1906
    if (!lex->drop_temporary)
1966
1907
    {
1967
 
      if (end_active_trans(thd))
 
1908
      if (end_active_trans(session))
1968
1909
        goto error;
1969
1910
    }
1970
1911
    else
1977
1918
        To not generate such irrelevant "table does not exist errors",
1978
1919
        we silently add IF EXISTS if TEMPORARY was used.
1979
1920
      */
1980
 
      if (thd->slave_thread)
 
1921
      if (session->slave_thread)
1981
1922
        lex->drop_if_exists= 1;
1982
1923
 
1983
1924
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1984
 
      thd->options|= OPTION_KEEP_LOG;
 
1925
      session->options|= OPTION_KEEP_LOG;
1985
1926
    }
1986
1927
    /* DDL and binlog write order protected by LOCK_open */
1987
 
    res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
 
1928
    res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1988
1929
  }
1989
1930
  break;
1990
1931
  case SQLCOM_SHOW_PROCESSLIST:
1991
 
    mysqld_list_processes(thd, NULL, lex->verbose);
 
1932
    mysqld_list_processes(session, NULL, lex->verbose);
1992
1933
    break;
1993
1934
  case SQLCOM_SHOW_ENGINE_LOGS:
1994
1935
    {
1995
 
      res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
 
1936
      res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1996
1937
      break;
1997
1938
    }
1998
1939
  case SQLCOM_CHANGE_DB:
1999
1940
  {
2000
1941
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2001
1942
 
2002
 
    if (!mysql_change_db(thd, &db_str, false))
2003
 
      my_ok(thd);
 
1943
    if (!mysql_change_db(session, &db_str, false))
 
1944
      my_ok(session);
2004
1945
 
2005
1946
    break;
2006
1947
  }
2010
1951
    assert(first_table == all_tables && first_table != 0);
2011
1952
    if (lex->local_file)
2012
1953
    {
2013
 
      if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
 
1954
      if (!(session->client_capabilities & CLIENT_LOCAL_FILES) ||
2014
1955
          !opt_local_infile)
2015
1956
      {
2016
1957
        my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2018
1959
      }
2019
1960
    }
2020
1961
 
2021
 
    res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
 
1962
    res= mysql_load(session, lex->exchange, first_table, lex->field_list,
2022
1963
                    lex->update_list, lex->value_list, lex->duplicates,
2023
1964
                    lex->ignore, (bool) lex->local_file);
2024
1965
    break;
2028
1969
  {
2029
1970
    List<set_var_base> *lex_var_list= &lex->var_list;
2030
1971
 
2031
 
    if (lex->autocommit && end_active_trans(thd))
 
1972
    if (lex->autocommit && end_active_trans(session))
2032
1973
      goto error;
2033
1974
 
2034
 
    if (open_and_lock_tables(thd, all_tables))
 
1975
    if (open_and_lock_tables(session, all_tables))
2035
1976
      goto error;
2036
1977
    if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2037
1978
    {
2038
1979
      my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2039
1980
      goto error;
2040
1981
    }
2041
 
    if (!(res= sql_set_variables(thd, lex_var_list)))
 
1982
    if (!(res= sql_set_variables(session, lex_var_list)))
2042
1983
    {
2043
1984
      /*
2044
1985
        If the previous command was a SET ONE_SHOT, we don't want to forget
2045
1986
        about the ONE_SHOT property of that SET. So we use a |= instead of = .
2046
1987
      */
2047
 
      thd->one_shot_set|= lex->one_shot_set;
2048
 
      my_ok(thd);
 
1988
      session->one_shot_set|= lex->one_shot_set;
 
1989
      my_ok(session);
2049
1990
    }
2050
1991
    else
2051
1992
    {
2054
1995
        Send something semi-generic here since we don't know which
2055
1996
        assignment in the list caused the error.
2056
1997
      */
2057
 
      if (!thd->is_error())
 
1998
      if (!session->is_error())
2058
1999
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2059
2000
      goto error;
2060
2001
    }
2069
2010
      done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2070
2011
      false, mysqldump will not work.
2071
2012
    */
2072
 
    unlock_locked_tables(thd);
2073
 
    if (thd->options & OPTION_TABLE_LOCK)
 
2013
    unlock_locked_tables(session);
 
2014
    if (session->options & OPTION_TABLE_LOCK)
2074
2015
    {
2075
 
      end_active_trans(thd);
2076
 
      thd->options&= ~(OPTION_TABLE_LOCK);
 
2016
      end_active_trans(session);
 
2017
      session->options&= ~(OPTION_TABLE_LOCK);
2077
2018
    }
2078
 
    if (thd->global_read_lock)
2079
 
      unlock_global_read_lock(thd);
2080
 
    my_ok(thd);
 
2019
    if (session->global_read_lock)
 
2020
      unlock_global_read_lock(session);
 
2021
    my_ok(session);
2081
2022
    break;
2082
2023
  case SQLCOM_LOCK_TABLES:
2083
2024
    /*
2084
2025
      We try to take transactional locks if
2085
2026
      - only transactional locks are requested (lex->lock_transactional) and
2086
 
      - no non-transactional locks exist (!thd->locked_tables).
 
2027
      - no non-transactional locks exist (!session->locked_tables).
2087
2028
    */
2088
 
    if (lex->lock_transactional && !thd->locked_tables)
 
2029
    if (lex->lock_transactional && !session->locked_tables)
2089
2030
    {
2090
2031
      int rc;
2091
2032
      /*
2092
2033
        All requested locks are transactional and no non-transactional
2093
2034
        locks exist.
2094
2035
      */
2095
 
      if ((rc= try_transactional_lock(thd, all_tables)) == -1)
 
2036
      if ((rc= try_transactional_lock(session, all_tables)) == -1)
2096
2037
        goto error;
2097
2038
      if (rc == 0)
2098
2039
      {
2099
 
        my_ok(thd);
 
2040
        my_ok(session);
2100
2041
        break;
2101
2042
      }
2102
2043
      /*
2113
2054
      requested. If yes, warn about the conversion to non-transactional
2114
2055
      locks or abort in strict mode.
2115
2056
    */
2116
 
    if (check_transactional_lock(thd, all_tables))
 
2057
    if (check_transactional_lock(session, all_tables))
2117
2058
      goto error;
2118
 
    unlock_locked_tables(thd);
 
2059
    unlock_locked_tables(session);
2119
2060
    /* we must end the trasaction first, regardless of anything */
2120
 
    if (end_active_trans(thd))
 
2061
    if (end_active_trans(session))
2121
2062
      goto error;
2122
 
    thd->in_lock_tables=1;
2123
 
    thd->options|= OPTION_TABLE_LOCK;
 
2063
    session->in_lock_tables=1;
 
2064
    session->options|= OPTION_TABLE_LOCK;
2124
2065
 
2125
 
    if (!(res= simple_open_n_lock_tables(thd, all_tables)))
 
2066
    if (!(res= simple_open_n_lock_tables(session, all_tables)))
2126
2067
    {
2127
 
      thd->locked_tables=thd->lock;
2128
 
      thd->lock=0;
2129
 
      (void) set_handler_table_locks(thd, all_tables, false);
2130
 
      my_ok(thd);
 
2068
      session->locked_tables=session->lock;
 
2069
      session->lock=0;
 
2070
      (void) set_handler_table_locks(session, all_tables, false);
 
2071
      my_ok(session);
2131
2072
    }
2132
2073
    else
2133
2074
    {
2136
2077
        can free its locks if LOCK TABLES locked some tables before finding
2137
2078
        that it can't lock a table in its list
2138
2079
      */
2139
 
      ha_autocommit_or_rollback(thd, 1);
2140
 
      end_active_trans(thd);
2141
 
      thd->options&= ~(OPTION_TABLE_LOCK);
 
2080
      ha_autocommit_or_rollback(session, 1);
 
2081
      end_active_trans(session);
 
2082
      session->options&= ~(OPTION_TABLE_LOCK);
2142
2083
    }
2143
 
    thd->in_lock_tables=0;
 
2084
    session->in_lock_tables=0;
2144
2085
    break;
2145
2086
  case SQLCOM_CREATE_DB:
2146
2087
  {
2150
2091
      prepared statement- safe.
2151
2092
    */
2152
2093
    HA_CREATE_INFO create_info(lex->create_info);
2153
 
    if (end_active_trans(thd))
 
2094
    if (end_active_trans(session))
2154
2095
    {
2155
2096
      res= -1;
2156
2097
      break;
2157
2098
    }
2158
2099
    char *alias;
2159
 
    if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
 
2100
    if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2160
2101
        check_db_name(&lex->name))
2161
2102
    {
2162
2103
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2169
2110
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2170
2111
      above was not called. So we have to check rules again here.
2171
2112
    */
2172
 
    if (thd->slave_thread && 
 
2113
    if (session->slave_thread && 
2173
2114
        (!rpl_filter->db_ok(lex->name.str) ||
2174
2115
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
2175
2116
    {
2176
2117
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2177
2118
      break;
2178
2119
    }
2179
 
    res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
 
2120
    res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2180
2121
                              lex->name.str), &create_info, 0);
2181
2122
    break;
2182
2123
  }
2183
2124
  case SQLCOM_DROP_DB:
2184
2125
  {
2185
 
    if (end_active_trans(thd))
 
2126
    if (end_active_trans(session))
2186
2127
    {
2187
2128
      res= -1;
2188
2129
      break;
2199
2140
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2200
2141
      above was not called. So we have to check rules again here.
2201
2142
    */
2202
 
    if (thd->slave_thread && 
 
2143
    if (session->slave_thread && 
2203
2144
        (!rpl_filter->db_ok(lex->name.str) ||
2204
2145
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
2205
2146
    {
2206
2147
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2207
2148
      break;
2208
2149
    }
2209
 
    if (thd->locked_tables || thd->active_transaction())
 
2150
    if (session->locked_tables || session->active_transaction())
2210
2151
    {
2211
2152
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2212
2153
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2213
2154
      goto error;
2214
2155
    }
2215
 
    res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
 
2156
    res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
2216
2157
    break;
2217
2158
  }
2218
2159
  case SQLCOM_ALTER_DB:
2231
2172
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2232
2173
      above was not called. So we have to check rules again here.
2233
2174
    */
2234
 
    if (thd->slave_thread &&
 
2175
    if (session->slave_thread &&
2235
2176
        (!rpl_filter->db_ok(db->str) ||
2236
2177
         !rpl_filter->db_ok_with_wild_table(db->str)))
2237
2178
    {
2238
2179
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2239
2180
      break;
2240
2181
    }
2241
 
    if (thd->locked_tables || thd->active_transaction())
 
2182
    if (session->locked_tables || session->active_transaction())
2242
2183
    {
2243
2184
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2244
2185
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2245
2186
      goto error;
2246
2187
    }
2247
 
    res= mysql_alter_db(thd, db->str, &create_info);
 
2188
    res= mysql_alter_db(session, db->str, &create_info);
2248
2189
    break;
2249
2190
  }
2250
2191
  case SQLCOM_SHOW_CREATE_DB:
2254
2195
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2255
2196
      break;
2256
2197
    }
2257
 
    res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
 
2198
    res= mysqld_show_create_db(session, lex->name.str, &lex->create_info);
2258
2199
    break;
2259
2200
  }
2260
2201
  case SQLCOM_RESET:
2266
2207
      reload_cache() will tell us if we are allowed to write to the
2267
2208
      binlog or not.
2268
2209
    */
2269
 
    if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
 
2210
    if (!reload_cache(session, lex->type, first_table, &write_to_binlog))
2270
2211
    {
2271
2212
      /*
2272
2213
        We WANT to write and we CAN write.
2275
2216
      /*
2276
2217
        Presumably, RESET and binlog writing doesn't require synchronization
2277
2218
      */
2278
 
      write_bin_log(thd, false, thd->query, thd->query_length);
2279
 
      my_ok(thd);
 
2219
      write_bin_log(session, false, session->query, session->query_length);
 
2220
      my_ok(session);
2280
2221
    } 
2281
2222
    
2282
2223
    break;
2292
2233
      break;
2293
2234
    }
2294
2235
 
2295
 
    if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
 
2236
    if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2296
2237
    {
2297
2238
      my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2298
2239
                 MYF(0));
2299
2240
      goto error;
2300
2241
    }
2301
 
    sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
 
2242
    sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2302
2243
    break;
2303
2244
  }
2304
2245
  case SQLCOM_BEGIN:
2305
 
    if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
2246
    if (session->transaction.xid_state.xa_state != XA_NOTR)
2306
2247
    {
2307
2248
      my_error(ER_XAER_RMFAIL, MYF(0),
2308
 
               xa_state_names[thd->transaction.xid_state.xa_state]);
 
2249
               xa_state_names[session->transaction.xid_state.xa_state]);
2309
2250
      break;
2310
2251
    }
2311
2252
    /*
2312
2253
      Breakpoints for backup testing.
2313
2254
    */
2314
 
    if (begin_trans(thd))
 
2255
    if (begin_trans(session))
2315
2256
      goto error;
2316
 
    my_ok(thd);
 
2257
    my_ok(session);
2317
2258
    break;
2318
2259
  case SQLCOM_COMMIT:
2319
 
    if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
 
2260
    if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2320
2261
                              lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2321
2262
      goto error;
2322
 
    my_ok(thd);
 
2263
    my_ok(session);
2323
2264
    break;
2324
2265
  case SQLCOM_ROLLBACK:
2325
 
    if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
 
2266
    if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2326
2267
                              lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2327
2268
      goto error;
2328
 
    my_ok(thd);
 
2269
    my_ok(session);
2329
2270
    break;
2330
2271
  case SQLCOM_RELEASE_SAVEPOINT:
2331
2272
  {
2332
2273
    SAVEPOINT *sv;
2333
 
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
 
2274
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2334
2275
    {
2335
2276
      if (my_strnncoll(system_charset_info,
2336
2277
                       (unsigned char *)lex->ident.str, lex->ident.length,
2339
2280
    }
2340
2281
    if (sv)
2341
2282
    {
2342
 
      if (ha_release_savepoint(thd, sv))
 
2283
      if (ha_release_savepoint(session, sv))
2343
2284
        res= true; // cannot happen
2344
2285
      else
2345
 
        my_ok(thd);
2346
 
      thd->transaction.savepoints=sv->prev;
 
2286
        my_ok(session);
 
2287
      session->transaction.savepoints=sv->prev;
2347
2288
    }
2348
2289
    else
2349
2290
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2352
2293
  case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2353
2294
  {
2354
2295
    SAVEPOINT *sv;
2355
 
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
 
2296
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2356
2297
    {
2357
2298
      if (my_strnncoll(system_charset_info,
2358
2299
                       (unsigned char *)lex->ident.str, lex->ident.length,
2361
2302
    }
2362
2303
    if (sv)
2363
2304
    {
2364
 
      if (ha_rollback_to_savepoint(thd, sv))
 
2305
      if (ha_rollback_to_savepoint(session, sv))
2365
2306
        res= true; // cannot happen
2366
2307
      else
2367
2308
      {
2368
 
        if (((thd->options & OPTION_KEEP_LOG) || 
2369
 
             thd->transaction.all.modified_non_trans_table) &&
2370
 
            !thd->slave_thread)
2371
 
          push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2309
        if (((session->options & OPTION_KEEP_LOG) || 
 
2310
             session->transaction.all.modified_non_trans_table) &&
 
2311
            !session->slave_thread)
 
2312
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2372
2313
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
2373
2314
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2374
 
        my_ok(thd);
 
2315
        my_ok(session);
2375
2316
      }
2376
 
      thd->transaction.savepoints=sv;
 
2317
      session->transaction.savepoints=sv;
2377
2318
    }
2378
2319
    else
2379
2320
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2380
2321
    break;
2381
2322
  }
2382
2323
  case SQLCOM_SAVEPOINT:
2383
 
    if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2384
 
          thd->in_sub_stmt) || !opt_using_transactions)
2385
 
      my_ok(thd);
 
2324
    if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
 
2325
      my_ok(session);
2386
2326
    else
2387
2327
    {
2388
2328
      SAVEPOINT **sv, *newsv;
2389
 
      for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
 
2329
      for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2390
2330
      {
2391
2331
        if (my_strnncoll(system_charset_info,
2392
2332
                         (unsigned char *)lex->ident.str, lex->ident.length,
2396
2336
      if (*sv) /* old savepoint of the same name exists */
2397
2337
      {
2398
2338
        newsv=*sv;
2399
 
        ha_release_savepoint(thd, *sv); // it cannot fail
 
2339
        ha_release_savepoint(session, *sv); // it cannot fail
2400
2340
        *sv=(*sv)->prev;
2401
2341
      }
2402
 
      else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
 
2342
      else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
2403
2343
                                               savepoint_alloc_size)) == 0)
2404
2344
      {
2405
2345
        my_error(ER_OUT_OF_RESOURCES, MYF(0));
2406
2346
        break;
2407
2347
      }
2408
 
      newsv->name=strmake_root(&thd->transaction.mem_root,
 
2348
      newsv->name=strmake_root(&session->transaction.mem_root,
2409
2349
                               lex->ident.str, lex->ident.length);
2410
2350
      newsv->length=lex->ident.length;
2411
2351
      /*
2413
2353
        we'll lose a little bit of memory in transaction mem_root, but it'll
2414
2354
        be free'd when transaction ends anyway
2415
2355
      */
2416
 
      if (ha_savepoint(thd, newsv))
 
2356
      if (ha_savepoint(session, newsv))
2417
2357
        res= true;
2418
2358
      else
2419
2359
      {
2420
 
        newsv->prev=thd->transaction.savepoints;
2421
 
        thd->transaction.savepoints=newsv;
2422
 
        my_ok(thd);
 
2360
        newsv->prev=session->transaction.savepoints;
 
2361
        session->transaction.savepoints=newsv;
 
2362
        my_ok(session);
2423
2363
      }
2424
2364
    }
2425
2365
    break;
2426
2366
  case SQLCOM_BINLOG_BASE64_EVENT:
2427
2367
  {
2428
 
    mysql_client_binlog_statement(thd);
 
2368
    mysql_client_binlog_statement(session);
2429
2369
    break;
2430
2370
  }
2431
2371
  default:
2432
2372
    assert(0);                             /* Impossible */
2433
 
    my_ok(thd);
 
2373
    my_ok(session);
2434
2374
    break;
2435
2375
  }
2436
 
  thd_proc_info(thd, "query end");
 
2376
  session->set_proc_info("query end");
2437
2377
 
2438
2378
  /*
2439
2379
    Binlog-related cleanup:
2445
2385
    needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2446
2386
    immediately.
2447
2387
  */
2448
 
  if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2449
 
    reset_one_shot_variables(thd);
 
2388
  if (session->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
 
2389
    reset_one_shot_variables(session);
2450
2390
 
2451
2391
  /*
2452
2392
    The return value for ROW_COUNT() is "implementation dependent" if the
2455
2395
    SQLCOM_EXECUTE.
2456
2396
  */
2457
2397
  if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2458
 
    thd->row_count_func= -1;
 
2398
    session->row_count_func= -1;
2459
2399
 
2460
2400
  goto finish;
2461
2401
 
2469
2409
      Release the protection against the global read lock and wake
2470
2410
      everyone, who might want to set a global read lock.
2471
2411
    */
2472
 
    start_waiting_global_read_lock(thd);
 
2412
    start_waiting_global_read_lock(session);
2473
2413
  }
2474
 
  return(res || thd->is_error());
 
2414
  return(res || session->is_error());
2475
2415
}
2476
2416
 
2477
 
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
 
2417
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2478
2418
{
2479
 
  LEX   *lex= thd->lex;
 
2419
  LEX   *lex= session->lex;
2480
2420
  select_result *result=lex->result;
2481
2421
  bool res;
2482
2422
  /* assign global limit variable if limit is not given */
2484
2424
    SELECT_LEX *param= lex->unit.global_parameters;
2485
2425
    if (!param->explicit_limit)
2486
2426
      param->select_limit=
2487
 
        new Item_int((uint64_t) thd->variables.select_limit);
 
2427
        new Item_int((uint64_t) session->variables.select_limit);
2488
2428
  }
2489
 
  if (!(res= open_and_lock_tables(thd, all_tables)))
 
2429
  if (!(res= open_and_lock_tables(session, all_tables)))
2490
2430
  {
2491
2431
    if (lex->describe)
2492
2432
    {
2498
2438
      */
2499
2439
      if (!(result= new select_send()))
2500
2440
        return 1;                               /* purecov: inspected */
2501
 
      thd->send_explain_fields(result);
2502
 
      res= mysql_explain_union(thd, &thd->lex->unit, result);
 
2441
      session->send_explain_fields(result);
 
2442
      res= mysql_explain_union(session, &session->lex->unit, result);
2503
2443
      if (lex->describe & DESCRIBE_EXTENDED)
2504
2444
      {
2505
2445
        char buff[1024];
2506
2446
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2507
2447
        str.length(0);
2508
 
        thd->lex->unit.print(&str, QT_ORDINARY);
 
2448
        session->lex->unit.print(&str, QT_ORDINARY);
2509
2449
        str.append('\0');
2510
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2450
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2511
2451
                     ER_YES, str.ptr());
2512
2452
      }
2513
2453
      if (res)
2520
2460
    {
2521
2461
      if (!result && !(result= new select_send()))
2522
2462
        return 1;                               /* purecov: inspected */
2523
 
      res= handle_select(thd, lex, result, 0);
 
2463
      res= handle_select(session, lex, result, 0);
2524
2464
      if (result != lex->result)
2525
2465
        delete result;
2526
2466
    }
2544
2484
    corresponding exec. (Thus we only have to check in fix_fields.)
2545
2485
  - Passing to check_stack_overrun() prevents the compiler from removing it.
2546
2486
*/
2547
 
bool check_stack_overrun(THD *thd, long margin,
 
2487
bool check_stack_overrun(Session *session, long margin,
2548
2488
                         unsigned char *buf __attribute__((unused)))
2549
2489
{
2550
2490
  long stack_used;
2551
 
  assert(thd == current_thd);
2552
 
  if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
 
2491
  assert(session == current_session);
 
2492
  if ((stack_used=used_stack(session->thread_stack,(char*) &stack_used)) >=
2553
2493
      (long) (my_thread_stack_size - margin))
2554
2494
  {
2555
2495
    sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2565
2505
 
2566
2506
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2567
2507
{
2568
 
  LEX   *lex= current_thd->lex;
 
2508
  LEX   *lex= current_session->lex;
2569
2509
  ulong old_info=0;
2570
2510
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2571
2511
    return 1;
2593
2533
 
2594
2534
 
2595
2535
/**
2596
 
 Reset THD part responsible for command processing state.
 
2536
 Reset Session part responsible for command processing state.
2597
2537
 
2598
2538
   This needs to be called before execution of every statement
2599
2539
   (prepared or conventional).
2600
2540
   It is not called by substatements of routines.
2601
2541
 
2602
2542
  @todo
2603
 
   Make it a method of THD and align its name with the rest of
 
2543
   Make it a method of Session and align its name with the rest of
2604
2544
   reset/end/start/init methods.
2605
2545
  @todo
2606
 
   Call it after we use THD for queries, not before.
 
2546
   Call it after we use Session for queries, not before.
2607
2547
*/
2608
2548
 
2609
 
void mysql_reset_thd_for_next_command(THD *thd)
 
2549
void mysql_reset_session_for_next_command(Session *session)
2610
2550
{
2611
 
  assert(! thd->in_sub_stmt);
2612
 
  thd->free_list= 0;
2613
 
  thd->select_number= 1;
 
2551
  session->free_list= 0;
 
2552
  session->select_number= 1;
2614
2553
  /*
2615
2554
    Those two lines below are theoretically unneeded as
2616
 
    THD::cleanup_after_query() should take care of this already.
 
2555
    Session::cleanup_after_query() should take care of this already.
2617
2556
  */
2618
 
  thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2619
 
  thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
 
2557
  session->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
 
2558
  session->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2620
2559
 
2621
 
  thd->query_start_used= 0;
2622
 
  thd->is_fatal_error= thd->time_zone_used= 0;
2623
 
  thd->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS | 
 
2560
  session->query_start_used= 0;
 
2561
  session->is_fatal_error= session->time_zone_used= 0;
 
2562
  session->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS | 
2624
2563
                          SERVER_QUERY_NO_INDEX_USED |
2625
2564
                          SERVER_QUERY_NO_GOOD_INDEX_USED);
2626
2565
  /*
2628
2567
    OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2629
2568
    in ha_rollback_trans() about some tables couldn't be rolled back.
2630
2569
  */
2631
 
  if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
 
2570
  if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2632
2571
  {
2633
 
    thd->options&= ~OPTION_KEEP_LOG;
2634
 
    thd->transaction.all.modified_non_trans_table= false;
 
2572
    session->options&= ~OPTION_KEEP_LOG;
 
2573
    session->transaction.all.modified_non_trans_table= false;
2635
2574
  }
2636
 
  assert(thd->security_ctx== &thd->main_security_ctx);
2637
 
  thd->thread_specific_used= false;
 
2575
  assert(session->security_ctx== &session->main_security_ctx);
 
2576
  session->thread_specific_used= false;
2638
2577
 
2639
2578
  if (opt_bin_log)
2640
2579
  {
2641
 
    reset_dynamic(&thd->user_var_events);
2642
 
    thd->user_var_events_alloc= thd->mem_root;
 
2580
    reset_dynamic(&session->user_var_events);
 
2581
    session->user_var_events_alloc= session->mem_root;
2643
2582
  }
2644
 
  thd->clear_error();
2645
 
  thd->main_da.reset_diagnostics_area();
2646
 
  thd->total_warn_count=0;                      // Warnings for this query
2647
 
  thd->rand_used= 0;
2648
 
  thd->sent_row_count= thd->examined_row_count= 0;
 
2583
  session->clear_error();
 
2584
  session->main_da.reset_diagnostics_area();
 
2585
  session->total_warn_count=0;                  // Warnings for this query
 
2586
  session->rand_used= 0;
 
2587
  session->sent_row_count= session->examined_row_count= 0;
2649
2588
 
2650
2589
  /*
2651
2590
    Because we come here only for start of top-statements, binlog format is
2652
2591
    constant inside a complex statement (using stored functions) etc.
2653
2592
  */
2654
 
  thd->reset_current_stmt_binlog_row_based();
 
2593
  session->reset_current_stmt_binlog_row_based();
2655
2594
 
2656
2595
  return;
2657
2596
}
2675
2614
mysql_new_select(LEX *lex, bool move_down)
2676
2615
{
2677
2616
  SELECT_LEX *select_lex;
2678
 
  THD *thd= lex->thd;
 
2617
  Session *session= lex->session;
2679
2618
 
2680
 
  if (!(select_lex= new (thd->mem_root) SELECT_LEX()))
 
2619
  if (!(select_lex= new (session->mem_root) SELECT_LEX()))
2681
2620
    return(1);
2682
 
  select_lex->select_number= ++thd->select_number;
 
2621
  select_lex->select_number= ++session->select_number;
2683
2622
  select_lex->parent_lex= lex; /* Used in init_query. */
2684
2623
  select_lex->init_query();
2685
2624
  select_lex->init_select();
2695
2634
    SELECT_LEX_UNIT *unit;
2696
2635
    lex->subqueries= true;
2697
2636
    /* first select_lex of subselect or derived table */
2698
 
    if (!(unit= new (thd->mem_root) SELECT_LEX_UNIT()))
 
2637
    if (!(unit= new (session->mem_root) SELECT_LEX_UNIT()))
2699
2638
      return(1);
2700
2639
 
2701
2640
    unit->init_query();
2702
2641
    unit->init_select();
2703
 
    unit->thd= thd;
 
2642
    unit->session= session;
2704
2643
    unit->include_down(lex->current_select);
2705
2644
    unit->link_next= 0;
2706
2645
    unit->link_prev= 0;
2721
2660
    }
2722
2661
    select_lex->include_neighbour(lex->current_select);
2723
2662
    SELECT_LEX_UNIT *unit= select_lex->master_unit();                              
2724
 
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
 
2663
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
2725
2664
      return(1);
2726
2665
    select_lex->context.outer_context= 
2727
2666
                unit->first_select()->context.outer_context;
2750
2689
 
2751
2690
void create_select_for_variable(const char *var_name)
2752
2691
{
2753
 
  THD *thd;
 
2692
  Session *session;
2754
2693
  LEX *lex;
2755
2694
  LEX_STRING tmp, null_lex_string;
2756
2695
  Item *var;
2757
2696
  char buff[MAX_SYS_VAR_LENGTH*2+4+8], *end;
2758
2697
 
2759
 
  thd= current_thd;
2760
 
  lex= thd->lex;
 
2698
  session= current_session;
 
2699
  lex= session->lex;
2761
2700
  mysql_init_select(lex);
2762
2701
  lex->sql_command= SQLCOM_SELECT;
2763
2702
  tmp.str= (char*) var_name;
2767
2706
    We set the name of Item to @@session.var_name because that then is used
2768
2707
    as the column name in the output.
2769
2708
  */
2770
 
  if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
 
2709
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
2771
2710
  {
2772
2711
    end= strxmov(buff, "@@session.", var_name, NULL);
2773
2712
    var->set_name(buff, end-buff, system_charset_info);
2774
 
    add_item_to_list(thd, var);
 
2713
    add_item_to_list(session, var);
2775
2714
  }
2776
2715
  return;
2777
2716
}
2798
2737
/**
2799
2738
  Parse a query.
2800
2739
 
2801
 
  @param       thd     Current thread
 
2740
  @param       session     Current thread
2802
2741
  @param       inBuf   Begining of the query text
2803
2742
  @param       length  Length of the query text
2804
2743
  @param[out]  found_semicolon For multi queries, position of the character of
2805
2744
                               the next query in the query text.
2806
2745
*/
2807
2746
 
2808
 
void mysql_parse(THD *thd, const char *inBuf, uint32_t length,
 
2747
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
2809
2748
                 const char ** found_semicolon)
2810
2749
{
2811
2750
  /*
2816
2755
    - first, call query_cache_send_result_to_client,
2817
2756
    - second, if caching failed, initialise the lexical and syntactic parser.
2818
2757
    The problem is that the query cache depends on a clean initialization
2819
 
    of (among others) lex->safe_to_cache_query and thd->server_status,
 
2758
    of (among others) lex->safe_to_cache_query and session->server_status,
2820
2759
    which are reset respectively in
2821
2760
    - lex_start()
2822
 
    - mysql_reset_thd_for_next_command()
 
2761
    - mysql_reset_session_for_next_command()
2823
2762
    So, initializing the lexical analyser *before* using the query cache
2824
2763
    is required for the cache to work properly.
2825
2764
    FIXME: cleanup the dependencies in the code to simplify this.
2826
2765
  */
2827
 
  lex_start(thd);
2828
 
  mysql_reset_thd_for_next_command(thd);
 
2766
  lex_start(session);
 
2767
  mysql_reset_session_for_next_command(session);
2829
2768
 
2830
2769
  {
2831
 
    LEX *lex= thd->lex;
2832
 
 
2833
 
    Lex_input_stream lip(thd, inBuf, length);
2834
 
 
2835
 
    bool err= parse_sql(thd, &lip);
 
2770
    LEX *lex= session->lex;
 
2771
 
 
2772
    Lex_input_stream lip(session, inBuf, length);
 
2773
 
 
2774
    bool err= parse_sql(session, &lip);
2836
2775
    *found_semicolon= lip.found_semicolon;
2837
2776
 
2838
2777
    if (!err)
2839
2778
    {
2840
2779
      {
2841
 
        if (! thd->is_error())
 
2780
        if (! session->is_error())
2842
2781
        {
2843
2782
          /*
2844
 
            Binlog logs a string starting from thd->query and having length
2845
 
            thd->query_length; so we set thd->query_length correctly (to not
 
2783
            Binlog logs a string starting from session->query and having length
 
2784
            session->query_length; so we set session->query_length correctly (to not
2846
2785
            log several statements in one event, when we executed only first).
2847
2786
            We set it to not see the ';' (otherwise it would get into binlog
2848
2787
            and Query_log_event::print() would give ';;' output).
2851
2790
            Note that we don't need LOCK_thread_count to modify query_length.
2852
2791
          */
2853
2792
          if (*found_semicolon &&
2854
 
              (thd->query_length= (ulong)(*found_semicolon - thd->query)))
2855
 
            thd->query_length--;
 
2793
              (session->query_length= (ulong)(*found_semicolon - session->query)))
 
2794
            session->query_length--;
2856
2795
          /* Actually execute the query */
2857
 
          mysql_execute_command(thd);
 
2796
          mysql_execute_command(session);
2858
2797
        }
2859
2798
      }
2860
2799
    }
2861
2800
    else
2862
2801
    {
2863
 
      assert(thd->is_error());
 
2802
      assert(session->is_error());
2864
2803
    }
2865
2804
    lex->unit.cleanup();
2866
 
    thd_proc_info(thd, "freeing items");
2867
 
    thd->end_statement();
2868
 
    thd->cleanup_after_query();
2869
 
    assert(thd->change_list.is_empty());
 
2805
    session->set_proc_info("freeing items");
 
2806
    session->end_statement();
 
2807
    session->cleanup_after_query();
 
2808
    assert(session->change_list.is_empty());
2870
2809
  }
2871
2810
 
2872
2811
  return;
2883
2822
    1   can be ignored
2884
2823
*/
2885
2824
 
2886
 
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
 
2825
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
2887
2826
{
2888
 
  LEX *lex= thd->lex;
 
2827
  LEX *lex= session->lex;
2889
2828
  bool error= 0;
2890
2829
 
2891
 
  Lex_input_stream lip(thd, inBuf, length);
2892
 
  lex_start(thd);
2893
 
  mysql_reset_thd_for_next_command(thd);
 
2830
  Lex_input_stream lip(session, inBuf, length);
 
2831
  lex_start(session);
 
2832
  mysql_reset_session_for_next_command(session);
2894
2833
 
2895
 
  if (!parse_sql(thd, &lip) &&
2896
 
      all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
 
2834
  if (!parse_sql(session, &lip) &&
 
2835
      all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2897
2836
    error= 1;                  /* Ignore question */
2898
 
  thd->end_statement();
2899
 
  thd->cleanup_after_query();
 
2837
  session->end_statement();
 
2838
  session->cleanup_after_query();
2900
2839
  return(error);
2901
2840
}
2902
2841
 
2909
2848
    Return 0 if ok
2910
2849
*/
2911
2850
 
2912
 
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
 
2851
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
2913
2852
                       char *length, char *decimals,
2914
2853
                       uint32_t type_modifier,
2915
2854
                       enum column_format_type column_format,
2916
2855
                       Item *default_value, Item *on_update_value,
2917
2856
                       LEX_STRING *comment,
2918
2857
                       char *change,
2919
 
                       List<String> *interval_list, const CHARSET_INFO * const cs)
 
2858
                       List<String> *interval_list, const CHARSET_INFO * const cs,
 
2859
                       virtual_column_info *vcol_info)
2920
2860
{
2921
2861
  register Create_field *new_field;
2922
 
  LEX  *lex= thd->lex;
 
2862
  LEX  *lex= session->lex;
2923
2863
 
2924
2864
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
2925
2865
    return(1);                          /* purecov: inspected */
2985
2925
  }
2986
2926
 
2987
2927
  if (!(new_field= new Create_field()) ||
2988
 
      new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
 
2928
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
2989
2929
                      default_value, on_update_value, comment, change,
2990
 
                      interval_list, cs, 0, column_format))
 
2930
                      interval_list, cs, 0, column_format,
 
2931
                      vcol_info))
2991
2932
    return(1);
2992
2933
 
2993
2934
  lex->alter_info.create_list.push_back(new_field);
3000
2941
 
3001
2942
void store_position_for_column(const char *name)
3002
2943
{
3003
 
  current_thd->lex->last_field->after=const_cast<char*> (name);
 
2944
  current_session->lex->last_field->after=const_cast<char*> (name);
3004
2945
}
3005
2946
 
3006
2947
bool
3007
 
add_proc_to_list(THD* thd, Item *item)
 
2948
add_proc_to_list(Session* session, Item *item)
3008
2949
{
3009
2950
  order_st *order;
3010
2951
  Item  **item_ptr;
3011
2952
 
3012
 
  if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
 
2953
  if (!(order = (order_st *) session->alloc(sizeof(order_st)+sizeof(Item*))))
3013
2954
    return 1;
3014
2955
  item_ptr = (Item**) (order+1);
3015
2956
  *item_ptr= item;
3016
2957
  order->item=item_ptr;
3017
2958
  order->free_me=0;
3018
 
  thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
2959
  session->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
3019
2960
  return 0;
3020
2961
}
3021
2962
 
3024
2965
  save order by and tables in own lists.
3025
2966
*/
3026
2967
 
3027
 
bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc)
 
2968
bool add_to_list(Session *session, SQL_LIST &list,Item *item,bool asc)
3028
2969
{
3029
2970
  order_st *order;
3030
 
  if (!(order = (order_st *) thd->alloc(sizeof(order_st))))
 
2971
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
3031
2972
    return(1);
3032
2973
  order->item_ptr= item;
3033
2974
  order->item= &order->item_ptr;
3059
3000
    \#  Pointer to TableList element added to the total table list
3060
3001
*/
3061
3002
 
3062
 
TableList *st_select_lex::add_table_to_list(THD *thd,
 
3003
TableList *st_select_lex::add_table_to_list(Session *session,
3063
3004
                                             Table_ident *table,
3064
3005
                                             LEX_STRING *alias,
3065
3006
                                             uint32_t table_options,
3070
3011
  register TableList *ptr;
3071
3012
  TableList *previous_table_ref; /* The table preceding the current one. */
3072
3013
  char *alias_str;
3073
 
  LEX *lex= thd->lex;
 
3014
  LEX *lex= session->lex;
3074
3015
 
3075
3016
  if (!table)
3076
3017
    return(0);                          // End of memory
3097
3038
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
3098
3039
      return(0);
3099
3040
    }
3100
 
    if (!(alias_str= (char*) thd->memdup(alias_str,table->table.length+1)))
 
3041
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
3101
3042
      return(0);
3102
3043
  }
3103
 
  if (!(ptr = (TableList *) thd->calloc(sizeof(TableList))))
 
3044
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
3104
3045
    return(0);                          /* purecov: inspected */
3105
3046
  if (table->db.str)
3106
3047
  {
3129
3070
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
3130
3071
                                      INFORMATION_SCHEMA_NAME.str))
3131
3072
  {
3132
 
    ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
 
3073
    ST_SCHEMA_TABLE *schema_table= find_schema_table(session, ptr->table_name);
3133
3074
    if (!schema_table ||
3134
3075
        (schema_table->hidden && 
3135
3076
         ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 || 
3212
3153
    created empty list after having saved the info on the old level
3213
3154
    in the initialized structure.
3214
3155
 
3215
 
  @param thd         current thread
 
3156
  @param session         current thread
3216
3157
 
3217
3158
  @retval
3218
3159
    0   if success
3220
3161
    1   otherwise
3221
3162
*/
3222
3163
 
3223
 
bool st_select_lex::init_nested_join(THD *thd)
 
3164
bool st_select_lex::init_nested_join(Session *session)
3224
3165
{
3225
3166
  TableList *ptr;
3226
3167
  nested_join_st *nested_join;
3227
3168
 
3228
 
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
3169
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
3229
3170
                                       sizeof(nested_join_st))))
3230
3171
    return(1);
3231
3172
  nested_join= ptr->nested_join=
3249
3190
    If the current level contains only one member, the function
3250
3191
    moves it one level up, eliminating the nest.
3251
3192
 
3252
 
  @param thd         current thread
 
3193
  @param session         current thread
3253
3194
 
3254
3195
  @return
3255
3196
    - Pointer to TableList element added to the total table list, if success
3256
3197
    - 0, otherwise
3257
3198
*/
3258
3199
 
3259
 
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
 
3200
TableList *st_select_lex::end_nested_join(Session *session __attribute__((unused)))
3260
3201
{
3261
3202
  TableList *ptr;
3262
3203
  nested_join_st *nested_join;
3289
3230
 
3290
3231
    The function nest last join operation as if it was enclosed in braces.
3291
3232
 
3292
 
  @param thd         current thread
 
3233
  @param session         current thread
3293
3234
 
3294
3235
  @retval
3295
3236
    0  Error
3297
3238
    \#  Pointer to TableList element created for the new nested join
3298
3239
*/
3299
3240
 
3300
 
TableList *st_select_lex::nest_last_join(THD *thd)
 
3241
TableList *st_select_lex::nest_last_join(Session *session)
3301
3242
{
3302
3243
  TableList *ptr;
3303
3244
  nested_join_st *nested_join;
3304
3245
  List<TableList> *embedded_list;
3305
3246
 
3306
 
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
3247
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
3307
3248
                                       sizeof(nested_join_st))))
3308
3249
    return(0);
3309
3250
  nested_join= ptr->nested_join=
3385
3326
      SELECT * FROM t3 LEFT JOIN (t1 LEFT JOIN t2 ON on_expr2) ON on_expr1
3386
3327
   @endverbatim
3387
3328
 
3388
 
  @param thd         current thread
 
3329
  @param session         current thread
3389
3330
 
3390
3331
  @return
3391
3332
    - Pointer to the table representing the inner table, if success
3444
3385
    (SELECT ... order_st BY LIMIT n) order_st BY ...
3445
3386
    @endvarbatim
3446
3387
  
3447
 
  @param thd_arg                   thread handle
 
3388
  @param session_arg               thread handle
3448
3389
 
3449
3390
  @note
3450
3391
    The object is used to retrieve rows from the temporary table
3456
3397
    0     on success
3457
3398
*/
3458
3399
 
3459
 
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
 
3400
bool st_select_lex_unit::add_fake_select_lex(Session *session_arg)
3460
3401
{
3461
3402
  SELECT_LEX *first_sl= first_select();
3462
3403
  assert(!fake_select_lex);
3463
3404
 
3464
 
  if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
 
3405
  if (!(fake_select_lex= new (session_arg->mem_root) SELECT_LEX()))
3465
3406
      return(1);
3466
3407
  fake_select_lex->include_standalone(this, 
3467
3408
                                      (SELECT_LEX_NODE**)&fake_select_lex);
3468
3409
  fake_select_lex->select_number= INT_MAX;
3469
 
  fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
 
3410
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3470
3411
  fake_select_lex->make_empty_select();
3471
3412
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3472
3413
  fake_select_lex->select_limit= 0;
3486
3427
    */ 
3487
3428
    global_parameters= fake_select_lex;
3488
3429
    fake_select_lex->no_table_names_allowed= 1;
3489
 
    thd_arg->lex->current_select= fake_select_lex;
 
3430
    session_arg->lex->current_select= fake_select_lex;
3490
3431
  }
3491
 
  thd_arg->lex->pop_context();
 
3432
  session_arg->lex->pop_context();
3492
3433
  return(0);
3493
3434
}
3494
3435
 
3502
3443
    to be used for name resolution, and push the newly created
3503
3444
    context to the stack of contexts of the query.
3504
3445
 
3505
 
  @param thd       pointer to current thread
 
3446
  @param session       pointer to current thread
3506
3447
  @param left_op   left  operand of the JOIN
3507
3448
  @param right_op  rigth operand of the JOIN
3508
3449
 
3513
3454
*/
3514
3455
 
3515
3456
bool
3516
 
push_new_name_resolution_context(THD *thd,
 
3457
push_new_name_resolution_context(Session *session,
3517
3458
                                 TableList *left_op, TableList *right_op)
3518
3459
{
3519
3460
  Name_resolution_context *on_context;
3520
 
  if (!(on_context= new (thd->mem_root) Name_resolution_context))
 
3461
  if (!(on_context= new (session->mem_root) Name_resolution_context))
3521
3462
    return true;
3522
3463
  on_context->init();
3523
3464
  on_context->first_name_resolution_table=
3524
3465
    left_op->first_leaf_for_name_resolution();
3525
3466
  on_context->last_name_resolution_table=
3526
3467
    right_op->last_leaf_for_name_resolution();
3527
 
  return thd->lex->push_context(on_context);
 
3468
  return session->lex->push_context(on_context);
3528
3469
}
3529
3470
 
3530
3471
 
3606
3547
/**
3607
3548
  Reload/resets privileges and the different caches.
3608
3549
 
3609
 
  @param thd Thread handler (can be NULL!)
 
3550
  @param session Thread handler (can be NULL!)
3610
3551
  @param options What should be reset/reloaded (tables, privileges, slave...)
3611
3552
  @param tables Tables to flush (if any)
3612
3553
  @param write_to_binlog True if we can write to the binlog.
3619
3560
 
3620
3561
  @return Error status code
3621
3562
    @retval 0 Ok
3622
 
    @retval !=0  Error; thd->killed is set or thd->is_error() is true
 
3563
    @retval !=0  Error; session->killed is set or session->is_error() is true
3623
3564
*/
3624
3565
 
3625
 
bool reload_cache(THD *thd, ulong options, TableList *tables,
 
3566
bool reload_cache(Session *session, ulong options, TableList *tables,
3626
3567
                          bool *write_to_binlog)
3627
3568
{
3628
3569
  bool result=0;
3629
3570
  select_errors=0;                              /* Write if more errors */
3630
3571
  bool tmp_write_to_binlog= 1;
3631
3572
 
3632
 
  assert(!thd || !thd->in_sub_stmt);
3633
 
 
3634
3573
  if (options & REFRESH_LOG)
3635
3574
  {
3636
3575
    /*
3655
3594
    pthread_mutex_unlock(&LOCK_active_mi);
3656
3595
 
3657
3596
    /* flush slow and general logs */
3658
 
    logger.flush_logs(thd);
 
3597
    logger.flush_logs(session);
3659
3598
 
3660
3599
    if (ha_flush_logs(NULL))
3661
3600
      result=1;
3668
3607
  */
3669
3608
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK)) 
3670
3609
  {
3671
 
    if ((options & REFRESH_READ_LOCK) && thd)
 
3610
    if ((options & REFRESH_READ_LOCK) && session)
3672
3611
    {
3673
3612
      /*
3674
3613
        We must not try to aspire a global read lock if we have a write
3675
3614
        locked table. This would lead to a deadlock when trying to
3676
3615
        reopen (and re-lock) the table after the flush.
3677
3616
      */
3678
 
      if (thd->locked_tables)
 
3617
      if (session->locked_tables)
3679
3618
      {
3680
 
        THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
3681
 
        THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
 
3619
        THR_LOCK_DATA **lock_p= session->locked_tables->locks;
 
3620
        THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
3682
3621
 
3683
3622
        for (; lock_p < end_p; lock_p++)
3684
3623
        {
3694
3633
        UNLOCK TABLES
3695
3634
      */
3696
3635
      tmp_write_to_binlog= 0;
3697
 
      if (lock_global_read_lock(thd))
 
3636
      if (lock_global_read_lock(session))
3698
3637
        return 1;                               // Killed
3699
 
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
 
3638
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3700
3639
                                  false : true, true);
3701
 
      if (make_global_read_lock_block_commit(thd)) // Killed
 
3640
      if (make_global_read_lock_block_commit(session)) // Killed
3702
3641
      {
3703
3642
        /* Don't leave things in a half-locked state */
3704
 
        unlock_global_read_lock(thd);
 
3643
        unlock_global_read_lock(session);
3705
3644
        return 1;
3706
3645
      }
3707
3646
    }
3708
3647
    else
3709
 
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
 
3648
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3710
3649
                                  false : true, false);
3711
3650
    my_dbopt_cleanup();
3712
3651
  }
3713
 
  if (thd && (options & REFRESH_STATUS))
3714
 
    refresh_status(thd);
3715
 
  if (options & REFRESH_THREADS)
3716
 
    flush_thread_cache();
 
3652
  if (session && (options & REFRESH_STATUS))
 
3653
    refresh_status(session);
3717
3654
  if (options & REFRESH_MASTER)
3718
3655
  {
3719
 
    assert(thd);
 
3656
    assert(session);
3720
3657
    tmp_write_to_binlog= 0;
3721
 
    if (reset_master(thd))
 
3658
    if (reset_master(session))
3722
3659
    {
3723
3660
      result=1;
3724
3661
    }
3727
3664
 {
3728
3665
   tmp_write_to_binlog= 0;
3729
3666
   pthread_mutex_lock(&LOCK_active_mi);
3730
 
   if (reset_slave(thd, active_mi))
 
3667
   if (reset_slave(session, active_mi))
3731
3668
     result=1;
3732
3669
   pthread_mutex_unlock(&LOCK_active_mi);
3733
3670
 }
3739
3676
/**
3740
3677
  kill on thread.
3741
3678
 
3742
 
  @param thd                    Thread class
 
3679
  @param session                        Thread class
3743
3680
  @param id                     Thread id
3744
3681
  @param only_kill_query        Should it kill the query or the connection
3745
3682
 
3748
3685
*/
3749
3686
 
3750
3687
static unsigned int
3751
 
kill_one_thread(THD *thd __attribute__((unused)),
 
3688
kill_one_thread(Session *session __attribute__((unused)),
3752
3689
                ulong id, bool only_kill_query)
3753
3690
{
3754
 
  THD *tmp;
 
3691
  Session *tmp;
3755
3692
  uint32_t error=ER_NO_SUCH_THREAD;
3756
3693
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
3757
 
  I_List_iterator<THD> it(threads);
 
3694
  I_List_iterator<Session> it(threads);
3758
3695
  while ((tmp=it++))
3759
3696
  {
3760
3697
    if (tmp->command == COM_DAEMON)
3768
3705
  pthread_mutex_unlock(&LOCK_thread_count);
3769
3706
  if (tmp)
3770
3707
  {
3771
 
    tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
 
3708
    tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
3772
3709
    error=0;
3773
3710
    pthread_mutex_unlock(&tmp->LOCK_delete);
3774
3711
  }
3781
3718
 
3782
3719
  SYNOPSIS
3783
3720
    sql_kill()
3784
 
    thd                 Thread class
 
3721
    session                     Thread class
3785
3722
    id                  Thread id
3786
3723
    only_kill_query     Should it kill the query or the connection
3787
3724
*/
3788
3725
 
3789
 
void sql_kill(THD *thd, ulong id, bool only_kill_query)
 
3726
void sql_kill(Session *session, ulong id, bool only_kill_query)
3790
3727
{
3791
3728
  uint32_t error;
3792
 
  if (!(error= kill_one_thread(thd, id, only_kill_query)))
3793
 
    my_ok(thd);
 
3729
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
3730
    my_ok(session);
3794
3731
  else
3795
3732
    my_error(error, MYF(0), id);
3796
3733
}
3798
3735
 
3799
3736
/** If pointer is not a null pointer, append filename to it. */
3800
3737
 
3801
 
bool append_file_to_dir(THD *thd, const char **filename_ptr,
 
3738
bool append_file_to_dir(Session *session, const char **filename_ptr,
3802
3739
                        const char *table_name)
3803
3740
{
3804
3741
  char buff[FN_REFLEN],*ptr, *end;
3815
3752
  /* Fix is using unix filename format on dos */
3816
3753
  my_stpcpy(buff,*filename_ptr);
3817
3754
  end=convert_dirname(buff, *filename_ptr, NULL);
3818
 
  if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
 
3755
  if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3819
3756
    return 1;                                   // End of memory
3820
3757
  *filename_ptr=ptr;
3821
3758
  strxmov(ptr,buff,table_name,NULL);
3834
3771
 
3835
3772
bool check_simple_select()
3836
3773
{
3837
 
  THD *thd= current_thd;
3838
 
  LEX *lex= thd->lex;
 
3774
  Session *session= current_session;
 
3775
  LEX *lex= session->lex;
3839
3776
  if (lex->current_select != &lex->select_lex)
3840
3777
  {
3841
3778
    char command[80];
3842
 
    Lex_input_stream *lip= thd->m_lip;
 
3779
    Lex_input_stream *lip= session->m_lip;
3843
3780
    strmake(command, lip->yylval->symbol.str,
3844
3781
            cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
3845
3782
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3919
3856
/**
3920
3857
  Multi update query pre-check.
3921
3858
 
3922
 
  @param thd            Thread handler
 
3859
  @param session                Thread handler
3923
3860
  @param tables Global/local table list (have to be the same)
3924
3861
 
3925
3862
  @retval
3928
3865
    true  Error
3929
3866
*/
3930
3867
 
3931
 
bool multi_update_precheck(THD *thd,
 
3868
bool multi_update_precheck(Session *session,
3932
3869
                           TableList *tables __attribute__((unused)))
3933
3870
{
3934
3871
  const char *msg= 0;
3935
 
  LEX *lex= thd->lex;
 
3872
  LEX *lex= session->lex;
3936
3873
  SELECT_LEX *select_lex= &lex->select_lex;
3937
3874
 
3938
3875
  if (select_lex->item_list.elements != lex->value_list.elements)
3956
3893
/**
3957
3894
  Multi delete query pre-check.
3958
3895
 
3959
 
  @param thd                    Thread handler
 
3896
  @param session                        Thread handler
3960
3897
  @param tables         Global/local table list
3961
3898
 
3962
3899
  @retval
3965
3902
    true  error
3966
3903
*/
3967
3904
 
3968
 
bool multi_delete_precheck(THD *thd,
 
3905
bool multi_delete_precheck(Session *session,
3969
3906
                           TableList *tables __attribute__((unused)))
3970
3907
{
3971
 
  SELECT_LEX *select_lex= &thd->lex->select_lex;
3972
 
  TableList **save_query_tables_own_last= thd->lex->query_tables_own_last;
3973
 
 
3974
 
  thd->lex->query_tables_own_last= 0;
3975
 
  thd->lex->query_tables_own_last= save_query_tables_own_last;
3976
 
 
3977
 
  if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where)
 
3908
  SELECT_LEX *select_lex= &session->lex->select_lex;
 
3909
  TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
 
3910
 
 
3911
  session->lex->query_tables_own_last= 0;
 
3912
  session->lex->query_tables_own_last= save_query_tables_own_last;
 
3913
 
 
3914
  if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3978
3915
  {
3979
3916
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
3980
3917
               ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
4084
4021
/**
4085
4022
  simple UPDATE query pre-check.
4086
4023
 
4087
 
  @param thd            Thread handler
 
4024
  @param session                Thread handler
4088
4025
  @param tables Global table list
4089
4026
 
4090
4027
  @retval
4093
4030
    true  Error
4094
4031
*/
4095
4032
 
4096
 
bool update_precheck(THD *thd, TableList *tables __attribute__((unused)))
 
4033
bool update_precheck(Session *session, TableList *tables __attribute__((unused)))
4097
4034
{
4098
 
  if (thd->lex->select_lex.item_list.elements != thd->lex->value_list.elements)
 
4035
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
4099
4036
  {
4100
4037
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
4101
4038
    return(true);
4107
4044
/**
4108
4045
  simple INSERT query pre-check.
4109
4046
 
4110
 
  @param thd            Thread handler
 
4047
  @param session                Thread handler
4111
4048
  @param tables Global table list
4112
4049
 
4113
4050
  @retval
4116
4053
    true   error
4117
4054
*/
4118
4055
 
4119
 
bool insert_precheck(THD *thd, TableList *tables __attribute__((unused)))
 
4056
bool insert_precheck(Session *session, TableList *tables __attribute__((unused)))
4120
4057
{
4121
 
  LEX *lex= thd->lex;
 
4058
  LEX *lex= session->lex;
4122
4059
 
4123
4060
  /*
4124
4061
    Check that we have modify privileges for the first table and
4136
4073
/**
4137
4074
  CREATE TABLE query pre-check.
4138
4075
 
4139
 
  @param thd                    Thread handler
 
4076
  @param session                        Thread handler
4140
4077
  @param tables         Global table list
4141
4078
  @param create_table           Table which will be created
4142
4079
 
4146
4083
    true   Error
4147
4084
*/
4148
4085
 
4149
 
bool create_table_precheck(THD *thd __attribute__((unused)),
 
4086
bool create_table_precheck(Session *session __attribute__((unused)),
4150
4087
                           TableList *tables __attribute__((unused)),
4151
4088
                           TableList *create_table)
4152
4089
{
4167
4104
/**
4168
4105
  negate given expression.
4169
4106
 
4170
 
  @param thd  thread handler
 
4107
  @param session  thread handler
4171
4108
  @param expr expression for negation
4172
4109
 
4173
4110
  @return
4174
4111
    negated expression
4175
4112
*/
4176
4113
 
4177
 
Item *negate_expression(THD *thd, Item *expr)
 
4114
Item *negate_expression(Session *session, Item *expr)
4178
4115
{
4179
4116
  Item *negated;
4180
4117
  if (expr->type() == Item::FUNC_ITEM &&
4182
4119
  {
4183
4120
    /* it is NOT(NOT( ... )) */
4184
4121
    Item *arg= ((Item_func *) expr)->arguments()[0];
4185
 
    enum_parsing_place place= thd->lex->current_select->parsing_place;
 
4122
    enum_parsing_place place= session->lex->current_select->parsing_place;
4186
4123
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
4187
4124
      return arg;
4188
4125
    /*
4192
4129
    return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
4193
4130
  }
4194
4131
 
4195
 
  if ((negated= expr->neg_transformer(thd)) != 0)
 
4132
  if ((negated= expr->neg_transformer(session)) != 0)
4196
4133
    return negated;
4197
4134
  return new Item_func_not(expr);
4198
4135
}
4340
4277
}
4341
4278
 
4342
4279
 
4343
 
extern int MYSQLparse(void *thd); // from sql_yacc.cc
 
4280
extern int MYSQLparse(void *session); // from sql_yacc.cc
4344
4281
 
4345
4282
 
4346
4283
/**
4347
4284
  This is a wrapper of MYSQLparse(). All the code should call parse_sql()
4348
4285
  instead of MYSQLparse().
4349
4286
 
4350
 
  @param thd Thread context.
 
4287
  @param session Thread context.
4351
4288
  @param lip Lexer context.
4352
4289
 
4353
4290
  @return Error status.
4355
4292
    @retval true on parsing error.
4356
4293
*/
4357
4294
 
4358
 
bool parse_sql(THD *thd, Lex_input_stream *lip)
 
4295
bool parse_sql(Session *session, Lex_input_stream *lip)
4359
4296
{
4360
 
  assert(thd->m_lip == NULL);
 
4297
  assert(session->m_lip == NULL);
4361
4298
 
4362
4299
  /* Set Lex_input_stream. */
4363
4300
 
4364
 
  thd->m_lip= lip;
 
4301
  session->m_lip= lip;
4365
4302
 
4366
4303
  /* Parse the query. */
4367
4304
 
4368
 
  bool mysql_parse_status= MYSQLparse(thd) != 0;
4369
 
 
4370
 
  /* Check that if MYSQLparse() failed, thd->is_error() is set. */
4371
 
 
4372
 
  assert(!mysql_parse_status || thd->is_error());
 
4305
  bool mysql_parse_status= MYSQLparse(session) != 0;
 
4306
 
 
4307
  /* Check that if MYSQLparse() failed, session->is_error() is set. */
 
4308
 
 
4309
  assert(!mysql_parse_status || session->is_error());
4373
4310
 
4374
4311
  /* Reset Lex_input_stream. */
4375
4312
 
4376
 
  thd->m_lip= NULL;
 
4313
  session->m_lip= NULL;
4377
4314
 
4378
4315
  /* That's it. */
4379
4316
 
4380
 
  return mysql_parse_status || thd->is_fatal_error;
 
4317
  return mysql_parse_status || session->is_fatal_error;
4381
4318
}
4382
4319
 
4383
4320
/**