~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

Merged from jay.

Show diffs side-by-side

added added

removed removed

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