~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2008-09-15 17:24:04 UTC
  • Revision ID: monty@inaugust.com-20080915172404-ygh6hiyu0q7qpa9x
Removed strndup calls.

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