~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Mark Atwood
  • Date: 2008-10-16 11:33:16 UTC
  • mto: (520.1.13 drizzle)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: mark@fallenpegasus.com-20081016113316-ff6jdt31ck90sjdh
an implemention of the errmsg plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
 
#include <drizzled/replication/replication.h>
19
 
#include <libdrizzle/libdrizzle.h>
20
 
#include <mysys/hash.h>
21
 
#include <drizzled/replication/binlog.h>
22
 
#include <drizzled/logging.h>
23
 
#include <drizzled/db.h>
24
 
#include <drizzled/error.h>
25
 
#include <drizzled/nested_join.h>
26
 
#include <drizzled/query_id.h>
27
 
#include <drizzled/sql_parse.h>
28
 
#include <drizzled/data_home.h>
29
 
#include <drizzled/sql_base.h>
30
 
#include <drizzled/show.h>
31
 
#include <drizzled/rename.h>
32
 
#include <drizzled/functions/time/unix_timestamp.h>
33
 
#include <drizzled/item/cmpfunc.h>
34
 
#include <drizzled/session.h>
35
 
#include <drizzled/sql_load.h>
36
 
#include <bitset>
37
 
 
38
 
using namespace std;
 
18
#include "sql_repl.h"
 
19
#include "rpl_filter.h"
 
20
#include "logging.h"
 
21
#include <drizzled/drizzled_error_messages.h>
39
22
 
40
23
/**
41
24
  @defgroup Runtime_Environment Runtime Environment
42
25
  @{
43
26
*/
44
27
 
45
 
extern const CHARSET_INFO *character_set_filesystem;
 
28
 
46
29
const char *any_db="*any*";     // Special symbol for check_access
47
30
 
48
31
const LEX_STRING command_name[COM_END+1]={
73
56
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
74
57
};
75
58
 
76
 
static void unlock_locked_tables(Session *session)
 
59
static void unlock_locked_tables(THD *thd)
77
60
{
78
 
  if (session->locked_tables)
 
61
  if (thd->locked_tables)
79
62
  {
80
 
    session->lock=session->locked_tables;
81
 
    session->locked_tables=0;                   // Will be automatically closed
82
 
    close_thread_tables(session);                       // Free tables
 
63
    thd->lock=thd->locked_tables;
 
64
    thd->locked_tables=0;                       // Will be automatically closed
 
65
    close_thread_tables(thd);                   // Free tables
83
66
  }
84
67
}
85
68
 
86
69
 
87
 
bool end_active_trans(Session *session)
 
70
bool end_active_trans(THD *thd)
88
71
{
89
72
  int error= 0;
90
73
 
91
 
  if (session->transaction.xid_state.xa_state != XA_NOTR)
 
74
  if (thd->transaction.xid_state.xa_state != XA_NOTR)
92
75
  {
93
76
    my_error(ER_XAER_RMFAIL, MYF(0),
94
 
             xa_state_names[session->transaction.xid_state.xa_state]);
 
77
             xa_state_names[thd->transaction.xid_state.xa_state]);
95
78
    return(1);
96
79
  }
97
 
  if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
 
80
  if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
98
81
                      OPTION_TABLE_LOCK))
99
82
  {
100
83
    /* Safety if one did "drop table" on locked tables */
101
 
    if (!session->locked_tables)
102
 
      session->options&= ~OPTION_TABLE_LOCK;
103
 
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
104
 
    if (ha_commit(session))
 
84
    if (!thd->locked_tables)
 
85
      thd->options&= ~OPTION_TABLE_LOCK;
 
86
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
 
87
    if (ha_commit(thd))
105
88
      error=1;
106
89
  }
107
 
  session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
108
 
  session->transaction.all.modified_non_trans_table= false;
 
90
  thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
91
  thd->transaction.all.modified_non_trans_table= false;
109
92
  return(error);
110
93
}
111
94
 
112
95
 
113
 
bool begin_trans(Session *session)
 
96
bool begin_trans(THD *thd)
114
97
{
115
98
  int error= 0;
116
 
  if (session->locked_tables)
 
99
  if (thd->locked_tables)
117
100
  {
118
 
    session->lock=session->locked_tables;
119
 
    session->locked_tables=0;                   // Will be automatically closed
120
 
    close_thread_tables(session);                       // Free tables
 
101
    thd->lock=thd->locked_tables;
 
102
    thd->locked_tables=0;                       // Will be automatically closed
 
103
    close_thread_tables(thd);                   // Free tables
121
104
  }
122
 
  if (end_active_trans(session))
 
105
  if (end_active_trans(thd))
123
106
    error= -1;
124
107
  else
125
108
  {
126
 
    LEX *lex= session->lex;
127
 
    session->options|= OPTION_BEGIN;
128
 
    session->server_status|= SERVER_STATUS_IN_TRANS;
 
109
    LEX *lex= thd->lex;
 
110
    thd->options|= OPTION_BEGIN;
 
111
    thd->server_status|= SERVER_STATUS_IN_TRANS;
129
112
    if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
130
 
      error= ha_start_consistent_snapshot(session);
 
113
      error= ha_start_consistent_snapshot(thd);
131
114
  }
132
115
  return error;
133
116
}
135
118
/**
136
119
  Returns true if all tables should be ignored.
137
120
*/
138
 
inline bool all_tables_not_ok(Session *, TableList *)
 
121
inline bool all_tables_not_ok(THD *thd, TableList *tables)
139
122
{
140
 
  return false;
 
123
  return rpl_filter->is_on() && tables &&
 
124
         !rpl_filter->tables_ok(thd->db, tables);
141
125
}
142
126
 
143
127
 
144
 
static bool some_non_temp_table_to_be_updated(Session *session, TableList *tables)
 
128
static bool some_non_temp_table_to_be_updated(THD *thd, TableList *tables)
145
129
{
146
130
  for (TableList *table= tables; table; table= table->next_global)
147
131
  {
148
132
    assert(table->db && table->table_name);
149
133
    if (table->updating &&
150
 
        !find_temporary_table(session, table->db, table->table_name))
 
134
        !find_temporary_table(thd, table->db, table->table_name))
151
135
      return 1;
152
136
  }
153
137
  return 0;
167
151
          a number of modified rows
168
152
*/
169
153
 
170
 
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
 
154
uint32_t sql_command_flags[SQLCOM_END+1];
171
155
 
172
156
void init_update_queries(void)
173
157
{
174
 
  uint32_t x;
175
 
 
176
 
  for (x= 0; x <= SQLCOM_END; x++)
177
 
    sql_command_flags[x].reset();
 
158
  memset(&sql_command_flags, 0, sizeof(sql_command_flags));
178
159
 
179
160
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
180
161
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
229
210
bool is_update_query(enum enum_sql_command command)
230
211
{
231
212
  assert(command >= 0 && command <= SQLCOM_END);
232
 
  return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
 
213
  return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
233
214
}
234
215
 
235
 
void execute_init_command(Session *session, sys_var_str *init_command_var,
 
216
void execute_init_command(THD *thd, sys_var_str *init_command_var,
236
217
                          rw_lock_t *var_mutex)
237
218
{
238
219
  Vio* save_vio;
239
220
  ulong save_client_capabilities;
240
221
 
241
 
  session->set_proc_info("Execution of init_command");
 
222
  thd->set_proc_info("Execution of init_command");
242
223
  /*
243
224
    We need to lock init_command_var because
244
225
    during execution of init_command_var query
245
226
    values of init_command_var can't be changed
246
227
  */
247
228
  rw_rdlock(var_mutex);
248
 
  save_client_capabilities= session->client_capabilities;
249
 
  session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
229
  save_client_capabilities= thd->client_capabilities;
 
230
  thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
250
231
  /*
251
232
    We don't need return result of execution to client side.
252
 
    To forbid this we should set session->net.vio to 0.
 
233
    To forbid this we should set thd->net.vio to 0.
253
234
  */
254
 
  save_vio= session->net.vio;
255
 
  session->net.vio= 0;
256
 
  dispatch_command(COM_QUERY, session,
 
235
  save_vio= thd->net.vio;
 
236
  thd->net.vio= 0;
 
237
  dispatch_command(COM_QUERY, thd,
257
238
                   init_command_var->value,
258
239
                   init_command_var->value_length);
259
240
  rw_unlock(var_mutex);
260
 
  session->client_capabilities= save_client_capabilities;
261
 
  session->net.vio= save_vio;
 
241
  thd->client_capabilities= save_client_capabilities;
 
242
  thd->net.vio= save_vio;
262
243
}
263
244
 
264
245
/**
265
246
  Ends the current transaction and (maybe) begin the next.
266
247
 
267
 
  @param session            Current thread
 
248
  @param thd            Current thread
268
249
  @param completion     Completion type
269
250
 
270
251
  @retval
271
252
    0   OK
272
253
*/
273
254
 
274
 
int end_trans(Session *session, enum enum_mysql_completiontype completion)
 
255
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
275
256
{
276
257
  bool do_release= 0;
277
258
  int res= 0;
278
259
 
279
 
  if (session->transaction.xid_state.xa_state != XA_NOTR)
 
260
  if (thd->transaction.xid_state.xa_state != XA_NOTR)
280
261
  {
281
262
    my_error(ER_XAER_RMFAIL, MYF(0),
282
 
             xa_state_names[session->transaction.xid_state.xa_state]);
 
263
             xa_state_names[thd->transaction.xid_state.xa_state]);
283
264
    return(1);
284
265
  }
285
266
  switch (completion) {
289
270
     even if there is a problem with the OPTION_AUTO_COMMIT flag
290
271
     (Which of course should never happen...)
291
272
    */
292
 
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
293
 
    res= ha_commit(session);
294
 
    session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
295
 
    session->transaction.all.modified_non_trans_table= false;
 
273
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
 
274
    res= ha_commit(thd);
 
275
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
276
    thd->transaction.all.modified_non_trans_table= false;
296
277
    break;
297
278
  case COMMIT_RELEASE:
298
279
    do_release= 1; /* fall through */
299
280
  case COMMIT_AND_CHAIN:
300
 
    res= end_active_trans(session);
 
281
    res= end_active_trans(thd);
301
282
    if (!res && completion == COMMIT_AND_CHAIN)
302
 
      res= begin_trans(session);
 
283
      res= begin_trans(thd);
303
284
    break;
304
285
  case ROLLBACK_RELEASE:
305
286
    do_release= 1; /* fall through */
306
287
  case ROLLBACK:
307
288
  case ROLLBACK_AND_CHAIN:
308
289
  {
309
 
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
310
 
    if (ha_rollback(session))
 
290
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
 
291
    if (ha_rollback(thd))
311
292
      res= -1;
312
 
    session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
 
    session->transaction.all.modified_non_trans_table= false;
 
293
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
294
    thd->transaction.all.modified_non_trans_table= false;
314
295
    if (!res && (completion == ROLLBACK_AND_CHAIN))
315
 
      res= begin_trans(session);
 
296
      res= begin_trans(thd);
316
297
    break;
317
298
  }
318
299
  default:
322
303
  }
323
304
 
324
305
  if (res < 0)
325
 
    my_error(session->killed_errno(), MYF(0));
 
306
    my_error(thd->killed_errno(), MYF(0));
326
307
  else if ((res == 0) && do_release)
327
 
    session->killed= Session::KILL_CONNECTION;
 
308
    thd->killed= THD::KILL_CONNECTION;
328
309
 
329
310
  return(res);
330
311
}
342
323
    1  request of thread shutdown (see dispatch_command() description)
343
324
*/
344
325
 
345
 
bool do_command(Session *session)
 
326
bool do_command(THD *thd)
346
327
{
347
328
  bool return_value;
348
329
  char *packet= 0;
349
330
  ulong packet_length;
350
 
  NET *net= &session->net;
 
331
  NET *net= &thd->net;
351
332
  enum enum_server_command command;
352
333
 
353
334
  /*
354
335
    indicator of uninitialized lex => normal flow of errors handling
355
336
    (see my_message_sql)
356
337
  */
357
 
  session->lex->current_select= 0;
 
338
  thd->lex->current_select= 0;
358
339
 
359
340
  /*
360
341
    This thread will do a blocking read from the client which
362
343
    the client, the connection is closed or "net_wait_timeout"
363
344
    number of seconds has passed
364
345
  */
365
 
  my_net_set_read_timeout(net, session->variables.net_wait_timeout);
 
346
  my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
366
347
 
367
348
  /*
368
349
    XXX: this code is here only to clear possible errors of init_connect. 
369
350
    Consider moving to init_connect() instead.
370
351
  */
371
 
  session->clear_error();                               // Clear error message
372
 
  session->main_da.reset_diagnostics_area();
 
352
  thd->clear_error();                           // Clear error message
 
353
  thd->main_da.reset_diagnostics_area();
373
354
 
374
355
  net_new_transaction(net);
375
356
 
379
360
    /* Check if we can continue without closing the connection */
380
361
 
381
362
    /* The error must be set. */
382
 
    assert(session->is_error());
383
 
    net_end_statement(session);
 
363
    assert(thd->is_error());
 
364
    net_end_statement(thd);
384
365
 
385
366
    if (net->error != 3)
386
367
    {
417
398
    command= COM_END;                           // Wrong command
418
399
 
419
400
  /* Restore read timeout value */
420
 
  my_net_set_read_timeout(net, session->variables.net_read_timeout);
 
401
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
421
402
 
422
403
  assert(packet_length);
423
 
  return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
 
404
  return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
424
405
 
425
406
out:
426
407
  return(return_value);
441
422
    @retval false The statement isn't updating any relevant tables.
442
423
*/
443
424
 
444
 
static bool deny_updates_if_read_only_option(Session *session,
 
425
static bool deny_updates_if_read_only_option(THD *thd,
445
426
                                                TableList *all_tables)
446
427
{
447
428
  if (!opt_readonly)
448
429
    return(false);
449
430
 
450
 
  LEX *lex= session->lex;
 
431
  LEX *lex= thd->lex;
451
432
 
452
 
  if (!(sql_command_flags[lex->sql_command].test(CF_BIT_CHANGES_DATA)))
 
433
  if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
453
434
    return(false);
454
435
 
455
436
  /* Multi update is an exception and is dealt with later. */
465
446
    lex->drop_temporary;
466
447
 
467
448
  const bool update_real_tables=
468
 
    some_non_temp_table_to_be_updated(session, all_tables) &&
 
449
    some_non_temp_table_to_be_updated(thd, all_tables) &&
469
450
    !(create_temp_tables || drop_temp_tables);
470
451
 
471
452
 
490
471
  Perform one connection-level (COM_XXXX) command.
491
472
 
492
473
  @param command         type of command to perform
493
 
  @param session             connection handle
 
474
  @param thd             connection handle
494
475
  @param packet          data for the command, packet is always null-terminated
495
476
  @param packet_length   length of packet + 1 (to show that data is
496
477
                         null-terminated) except for COM_SLEEP, where it
497
478
                         can be zero.
498
479
 
499
480
  @todo
500
 
    set session->lex->sql_command to SQLCOM_END here.
 
481
    set thd->lex->sql_command to SQLCOM_END here.
501
482
  @todo
502
483
    The following has to be changed to an 8 byte integer
503
484
 
507
488
    1   request of thread shutdown, i. e. if command is
508
489
        COM_QUIT/COM_SHUTDOWN
509
490
*/
510
 
bool dispatch_command(enum enum_server_command command, Session *session,
511
 
                      char* packet, uint32_t packet_length)
 
491
bool dispatch_command(enum enum_server_command command, THD *thd,
 
492
                      char* packet, uint32_t packet_length)
512
493
{
513
 
  NET *net= &session->net;
 
494
  NET *net= &thd->net;
514
495
  bool error= 0;
515
 
  Query_id &query_id= Query_id::get_query_id();
516
496
 
517
 
  session->command=command;
518
 
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
519
 
  session->set_time();
 
497
  thd->command=command;
 
498
  thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
499
  thd->set_time();
520
500
  pthread_mutex_lock(&LOCK_thread_count);
521
 
  session->query_id= query_id.value();
 
501
  thd->query_id= global_query_id;
522
502
 
523
503
  switch( command ) {
524
504
  /* Ignore these statements. */
526
506
    break;
527
507
  /* Increase id and count all other statements. */
528
508
  default:
529
 
    statistic_increment(session->status_var.questions, &LOCK_status);
530
 
    query_id.next();
 
509
    statistic_increment(thd->status_var.questions, &LOCK_status);
 
510
    next_query_id();
531
511
  }
532
512
 
533
513
  thread_running++;
534
 
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
514
  /* TODO: set thd->lex->sql_command to SQLCOM_END here */
535
515
  pthread_mutex_unlock(&LOCK_thread_count);
536
516
 
537
 
  logging_pre_do(session);
 
517
  logging_pre_do(thd);
538
518
 
539
 
  session->server_status&=
 
519
  thd->server_status&=
540
520
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
541
521
  switch (command) {
542
522
  case COM_INIT_DB:
543
523
  {
544
524
    LEX_STRING tmp;
545
 
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
546
 
    session->convert_string(&tmp, system_charset_info,
547
 
                        packet, packet_length, session->charset());
548
 
    if (!mysql_change_db(session, &tmp, false))
 
525
    status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
 
526
    thd->convert_string(&tmp, system_charset_info,
 
527
                        packet, packet_length, thd->charset());
 
528
    if (!mysql_change_db(thd, &tmp, false))
549
529
    {
550
 
      my_ok(session);
 
530
      my_ok(thd);
551
531
    }
552
532
    break;
553
533
  }
554
534
  case COM_CHANGE_USER:
555
535
  {
556
 
    status_var_increment(session->status_var.com_other);
 
536
    status_var_increment(thd->status_var.com_other);
557
537
    char *user= (char*) packet, *packet_end= packet + packet_length;
558
538
    /* Safe because there is always a trailing \0 at the end of the packet */
559
539
    char *passwd= strchr(user, '\0')+1;
560
540
 
561
541
 
562
 
    session->clear_error();                         // if errors from rollback
 
542
    thd->clear_error();                         // if errors from rollback
563
543
 
564
544
    /*
565
545
      Old clients send null-terminated string ('\0' for empty string) for
581
561
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
582
562
      break;
583
563
    }
584
 
    uint32_t passwd_len= (session->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
564
    uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
585
565
                      (unsigned char)(*passwd++) : strlen(passwd));
586
566
    uint32_t dummy_errors, save_db_length, db_length;
587
567
    int res;
588
 
    Security_context save_security_ctx= *session->security_ctx;
 
568
    Security_context save_security_ctx= *thd->security_ctx;
589
569
    USER_CONN *save_user_connect;
590
570
 
591
571
    db+= passwd_len + 1;
617
597
    /* Convert database name to utf8 */
618
598
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
619
599
                             system_charset_info, db, db_length,
620
 
                             session->charset(), &dummy_errors)]= 0;
 
600
                             thd->charset(), &dummy_errors)]= 0;
621
601
    db= db_buff;
622
602
 
623
603
    /* Save user and privileges */
624
 
    save_db_length= session->db_length;
625
 
    save_db= session->db;
626
 
    save_user_connect= session->user_connect;
 
604
    save_db_length= thd->db_length;
 
605
    save_db= thd->db;
 
606
    save_user_connect= thd->user_connect;
627
607
 
628
 
    if (!(session->security_ctx->user= my_strdup(user, MYF(0))))
 
608
    if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
629
609
    {
630
 
      session->security_ctx->user= save_security_ctx.user;
 
610
      thd->security_ctx->user= save_security_ctx.user;
631
611
      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
632
612
      break;
633
613
    }
634
614
 
635
615
    /* Clear variables that are allocated */
636
 
    session->user_connect= 0;
637
 
    res= check_user(session, passwd, passwd_len, db, false);
 
616
    thd->user_connect= 0;
 
617
    res= check_user(thd, passwd, passwd_len, db, false);
638
618
 
639
619
    if (res)
640
620
    {
641
 
      if (session->security_ctx->user)
642
 
        free(session->security_ctx->user);
643
 
      *session->security_ctx= save_security_ctx;
644
 
      session->user_connect= save_user_connect;
645
 
      session->db= save_db;
646
 
      session->db_length= save_db_length;
 
621
      if (thd->security_ctx->user)
 
622
        free(thd->security_ctx->user);
 
623
      *thd->security_ctx= save_security_ctx;
 
624
      thd->user_connect= save_user_connect;
 
625
      thd->db= save_db;
 
626
      thd->db_length= save_db_length;
647
627
    }
648
628
    else
649
629
    {
654
634
 
655
635
      if (cs_number)
656
636
      {
657
 
        session_init_client_charset(session, cs_number);
658
 
        session->update_charset();
 
637
        thd_init_client_charset(thd, cs_number);
 
638
        thd->update_charset();
659
639
      }
660
640
    }
661
641
    break;
662
642
  }
663
643
  case COM_QUERY:
664
644
  {
665
 
    if (alloc_query(session, packet, packet_length))
 
645
    if (alloc_query(thd, packet, packet_length))
666
646
      break;                                    // fatal error is set
667
 
    char *packet_end= session->query + session->query_length;
 
647
    char *packet_end= thd->query + thd->query_length;
668
648
    const char* end_of_stmt= NULL;
669
649
 
670
 
    mysql_parse(session, session->query, session->query_length, &end_of_stmt);
 
650
    mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
671
651
 
672
 
    while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
 
652
    while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
673
653
    {
674
654
      char *beginning_of_next_stmt= (char*) end_of_stmt;
675
655
 
676
 
      net_end_statement(session);
 
656
      net_end_statement(thd);
677
657
      /*
678
658
        Multiple queries exits, execute them individually
679
659
      */
680
 
      close_thread_tables(session);
 
660
      close_thread_tables(thd);
681
661
      ulong length= (ulong)(packet_end - beginning_of_next_stmt);
682
662
 
683
 
      log_slow_statement(session);
 
663
      log_slow_statement(thd);
684
664
 
685
665
      /* Remove garbage at start of query */
686
 
      while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
 
666
      while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
687
667
      {
688
668
        beginning_of_next_stmt++;
689
669
        length--;
690
670
      }
691
671
 
692
672
      pthread_mutex_lock(&LOCK_thread_count);
693
 
      session->query_length= length;
694
 
      session->query= beginning_of_next_stmt;
 
673
      thd->query_length= length;
 
674
      thd->query= beginning_of_next_stmt;
695
675
      /*
696
676
        Count each statement from the client.
697
677
      */
698
 
      statistic_increment(session->status_var.questions, &LOCK_status);
699
 
      session->query_id= query_id.next();
700
 
      session->set_time(); /* Reset the query start time. */
701
 
      /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
678
      statistic_increment(thd->status_var.questions, &LOCK_status);
 
679
      thd->query_id= next_query_id();
 
680
      thd->set_time(); /* Reset the query start time. */
 
681
      /* TODO: set thd->lex->sql_command to SQLCOM_END here */
702
682
      pthread_mutex_unlock(&LOCK_thread_count);
703
683
 
704
 
      mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
 
684
      mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
705
685
    }
706
686
    break;
707
687
  }
713
693
    LEX_STRING conv_name;
714
694
 
715
695
    /* used as fields initializator */
716
 
    lex_start(session);
 
696
    lex_start(thd);
717
697
 
718
 
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
 
698
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
719
699
    memset(&table_list, 0, sizeof(table_list));
720
 
    if (session->copy_db_to(&table_list.db, &table_list.db_length))
 
700
    if (thd->copy_db_to(&table_list.db, &table_list.db_length))
721
701
      break;
722
702
    /*
723
703
      We have name + wildcard in packet, separated by endzero
724
704
    */
725
705
    arg_end= strchr(packet, '\0');
726
 
    session->convert_string(&conv_name, system_charset_info,
727
 
                        packet, (uint32_t) (arg_end - packet), session->charset());
 
706
    thd->convert_string(&conv_name, system_charset_info,
 
707
                        packet, (uint32_t) (arg_end - packet), thd->charset());
728
708
    table_list.alias= table_list.table_name= conv_name.str;
729
709
    packet= arg_end + 1;
730
710
 
731
711
    if (!my_strcasecmp(system_charset_info, table_list.db,
732
 
                       INFORMATION_SCHEMA_NAME.c_str()))
 
712
                       INFORMATION_SCHEMA_NAME.str))
733
713
    {
734
 
      ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
 
714
      ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
735
715
      if (schema_table)
736
716
        table_list.schema_table= schema_table;
737
717
    }
738
718
 
739
 
    session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
740
 
    if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
 
719
    thd->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
 
720
    if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
741
721
      break;
742
722
    if (lower_case_table_names)
743
723
      my_casedn_str(files_charset_info, table_list.table_name);
744
724
 
745
725
    /* init structures for VIEW processing */
746
 
    table_list.select_lex= &(session->lex->select_lex);
747
 
 
748
 
    lex_start(session);
749
 
    mysql_reset_session_for_next_command(session);
750
 
 
751
 
    session->lex->
 
726
    table_list.select_lex= &(thd->lex->select_lex);
 
727
 
 
728
    lex_start(thd);
 
729
    mysql_reset_thd_for_next_command(thd);
 
730
 
 
731
    thd->lex->
752
732
      select_lex.table_list.link_in_list((unsigned char*) &table_list,
753
733
                                         (unsigned char**) &table_list.next_local);
754
 
    session->lex->add_to_query_tables(&table_list);
 
734
    thd->lex->add_to_query_tables(&table_list);
755
735
 
756
736
    /* switch on VIEW optimisation: do not fill temporary tables */
757
 
    session->lex->sql_command= SQLCOM_SHOW_FIELDS;
758
 
    mysqld_list_fields(session,&table_list,fields);
759
 
    session->lex->unit.cleanup();
760
 
    session->cleanup_after_query();
 
737
    thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
 
738
    mysqld_list_fields(thd,&table_list,fields);
 
739
    thd->lex->unit.cleanup();
 
740
    thd->cleanup_after_query();
761
741
    break;
762
742
  }
763
743
  case COM_QUIT:
764
744
    /* We don't calculate statistics for this command */
765
745
    net->error=0;                               // Don't give 'abort' message
766
 
    session->main_da.disable_status();              // Don't send anything back
 
746
    thd->main_da.disable_status();              // Don't send anything back
767
747
    error=true;                                 // End server
768
748
    break;
769
749
  case COM_BINLOG_DUMP:
772
752
      uint16_t flags;
773
753
      uint32_t slave_server_id;
774
754
 
775
 
      status_var_increment(session->status_var.com_other);
 
755
      status_var_increment(thd->status_var.com_other);
776
756
      /* TODO: The following has to be changed to an 8 byte integer */
777
757
      pos = uint4korr(packet);
778
758
      flags = uint2korr(packet + 4);
779
 
      session->server_id=0; /* avoid suicide */
 
759
      thd->server_id=0; /* avoid suicide */
780
760
      if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
781
761
        kill_zombie_dump_threads(slave_server_id);
782
 
      session->server_id = slave_server_id;
 
762
      thd->server_id = slave_server_id;
783
763
 
784
 
      mysql_binlog_send(session, session->strdup(packet + 10), (my_off_t) pos, flags);
 
764
      mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
785
765
      /*  fake COM_QUIT -- if we get here, the thread needs to terminate */
786
766
      error = true;
787
767
      break;
788
768
    }
789
769
  case COM_SHUTDOWN:
790
770
  {
791
 
    status_var_increment(session->status_var.com_other);
792
 
    my_eof(session);
793
 
    close_thread_tables(session);                       // Free before kill
794
 
    kill_drizzle();
 
771
    status_var_increment(thd->status_var.com_other);
 
772
    my_eof(thd);
 
773
    close_thread_tables(thd);                   // Free before kill
 
774
    kill_mysql();
795
775
    error=true;
796
776
    break;
797
777
  }
798
778
  case COM_PING:
799
 
    status_var_increment(session->status_var.com_other);
800
 
    my_ok(session);                             // Tell client we are alive
 
779
    status_var_increment(thd->status_var.com_other);
 
780
    my_ok(thd);                         // Tell client we are alive
801
781
    break;
802
782
  case COM_PROCESS_INFO:
803
 
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
804
 
    mysqld_list_processes(session, NULL, 0);
 
783
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
 
784
    mysqld_list_processes(thd, NULL, 0);
805
785
    break;
806
786
  case COM_PROCESS_KILL:
807
787
  {
808
 
    status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
 
788
    status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
809
789
    ulong id=(ulong) uint4korr(packet);
810
 
    sql_kill(session,id,false);
 
790
    sql_kill(thd,id,false);
811
791
    break;
812
792
  }
813
793
  case COM_SET_OPTION:
814
794
  {
815
 
    status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
 
795
    status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
816
796
    uint32_t opt_command= uint2korr(packet);
817
797
 
818
798
    switch (opt_command) {
819
799
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
820
 
      session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
821
 
      my_eof(session);
 
800
      thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
801
      my_eof(thd);
822
802
      break;
823
803
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
824
 
      session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
825
 
      my_eof(session);
 
804
      thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
 
805
      my_eof(thd);
826
806
      break;
827
807
    default:
828
808
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
840
820
  }
841
821
 
842
822
  /* If commit fails, we should be able to reset the OK status. */
843
 
  session->main_da.can_overwrite_status= true;
844
 
  ha_autocommit_or_rollback(session, session->is_error());
845
 
  session->main_da.can_overwrite_status= false;
 
823
  thd->main_da.can_overwrite_status= true;
 
824
  ha_autocommit_or_rollback(thd, thd->is_error());
 
825
  thd->main_da.can_overwrite_status= false;
846
826
 
847
 
  session->transaction.stmt.reset();
 
827
  thd->transaction.stmt.reset();
848
828
 
849
829
 
850
830
  /* report error issued during command execution */
851
 
  if (session->killed_errno())
852
 
  {
853
 
    if (! session->main_da.is_set())
854
 
      session->send_kill_message();
855
 
  }
856
 
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
857
 
  {
858
 
    session->killed= Session::NOT_KILLED;
859
 
    session->mysys_var->abort= 0;
860
 
  }
861
 
 
862
 
  net_end_statement(session);
863
 
 
864
 
  session->set_proc_info("closing tables");
 
831
  if (thd->killed_errno())
 
832
  {
 
833
    if (! thd->main_da.is_set())
 
834
      thd->send_kill_message();
 
835
  }
 
836
  if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
 
837
  {
 
838
    thd->killed= THD::NOT_KILLED;
 
839
    thd->mysys_var->abort= 0;
 
840
  }
 
841
 
 
842
  net_end_statement(thd);
 
843
 
 
844
  thd->set_proc_info("closing tables");
865
845
  /* Free tables */
866
 
  close_thread_tables(session);
867
 
 
868
 
  log_slow_statement(session);
869
 
 
870
 
  session->set_proc_info("cleaning up");
 
846
  close_thread_tables(thd);
 
847
 
 
848
  log_slow_statement(thd);
 
849
 
 
850
  thd->set_proc_info("cleaning up");
871
851
  pthread_mutex_lock(&LOCK_thread_count); // For process list
872
 
  session->set_proc_info(0);
873
 
  session->command=COM_SLEEP;
874
 
  session->query=0;
875
 
  session->query_length=0;
 
852
  thd->set_proc_info(0);
 
853
  thd->command=COM_SLEEP;
 
854
  thd->query=0;
 
855
  thd->query_length=0;
876
856
  thread_running--;
877
857
  pthread_mutex_unlock(&LOCK_thread_count);
878
 
  session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
879
 
  free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
 
858
  thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
 
859
  free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
880
860
  return(error);
881
861
}
882
862
 
883
863
 
884
 
void log_slow_statement(Session *session)
 
864
void log_slow_statement(THD *thd)
885
865
{
886
 
  logging_post_do(session);
 
866
  logging_post_do(thd);
887
867
 
888
868
  return;
889
869
}
897
877
    It prepares a SELECT_LEX and a TableList object to represent the
898
878
    given command as a SELECT parse tree.
899
879
 
900
 
  @param session              thread handle
 
880
  @param thd              thread handle
901
881
  @param lex              current lex
902
882
  @param table_ident      table alias if it's used
903
883
  @param schema_table_idx the type of the INFORMATION_SCHEMA table to be
915
895
                      in this version of the server.
916
896
*/
917
897
 
918
 
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
 
898
int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
919
899
                         enum enum_schema_tables schema_table_idx)
920
900
{
921
901
  SELECT_LEX *schema_select_lex= NULL;
954
934
    /* 'parent_lex' is used in init_query() so it must be before it. */
955
935
    schema_select_lex->parent_lex= lex;
956
936
    schema_select_lex->init_query();
957
 
    if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
 
937
    if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
958
938
      return(1);
959
939
    lex->query_tables_last= query_tables_last;
960
940
    break;
973
953
  
974
954
  SELECT_LEX *select_lex= lex->current_select;
975
955
  assert(select_lex);
976
 
  if (make_schema_select(session, select_lex, schema_table_idx))
 
956
  if (make_schema_select(thd, select_lex, schema_table_idx))
977
957
  {
978
958
    return(1);
979
959
  }
986
966
 
987
967
 
988
968
/**
989
 
  Read query from packet and store in session->query.
 
969
  Read query from packet and store in thd->query.
990
970
  Used in COM_QUERY and COM_STMT_PREPARE.
991
971
 
992
 
    Sets the following Session variables:
 
972
    Sets the following THD variables:
993
973
  - query
994
974
  - query_length
995
975
 
996
976
  @retval
997
977
    false ok
998
978
  @retval
999
 
    true  error;  In this case session->fatal_error is set
 
979
    true  error;  In this case thd->fatal_error is set
1000
980
*/
1001
981
 
1002
 
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
 
982
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
1003
983
{
1004
984
  /* Remove garbage at start and end of query */
1005
 
  while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
 
985
  while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
1006
986
  {
1007
987
    packet++;
1008
988
    packet_length--;
1009
989
  }
1010
990
  const char *pos= packet + packet_length;     // Point at end null
1011
991
  while (packet_length > 0 &&
1012
 
         (pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
 
992
         (pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
1013
993
  {
1014
994
    pos--;
1015
995
    packet_length--;
1016
996
  }
1017
997
  /* We must allocate some extra memory for query cache */
1018
 
  session->query_length= 0;                        // Extra safety: Avoid races
1019
 
  if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
 
998
  thd->query_length= 0;                        // Extra safety: Avoid races
 
999
  if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
1020
1000
                                              packet_length,
1021
 
                                              session->db_length+ 1)))
 
1001
                                              thd->db_length+ 1)))
1022
1002
    return true;
1023
 
  session->query[packet_length]=0;
1024
 
  session->query_length= packet_length;
 
1003
  thd->query[packet_length]=0;
 
1004
  thd->query_length= packet_length;
1025
1005
 
1026
1006
  /* Reclaim some memory */
1027
 
  session->packet.shrink(session->variables.net_buffer_length);
1028
 
  session->convert_buffer.shrink(session->variables.net_buffer_length);
 
1007
  thd->packet.shrink(thd->variables.net_buffer_length);
 
1008
  thd->convert_buffer.shrink(thd->variables.net_buffer_length);
1029
1009
 
1030
1010
  return false;
1031
1011
}
1032
1012
 
 
1013
static void reset_one_shot_variables(THD *thd) 
 
1014
{
 
1015
  thd->variables.character_set_client=
 
1016
    global_system_variables.character_set_client;
 
1017
  thd->variables.collation_connection=
 
1018
    global_system_variables.collation_connection;
 
1019
  thd->variables.collation_database=
 
1020
    global_system_variables.collation_database;
 
1021
  thd->variables.collation_server=
 
1022
    global_system_variables.collation_server;
 
1023
  thd->update_charset();
 
1024
  thd->variables.time_zone=
 
1025
    global_system_variables.time_zone;
 
1026
  thd->variables.lc_time_names= &my_locale_en_US;
 
1027
  thd->one_shot_set= 0;
 
1028
}
 
1029
 
 
1030
 
1033
1031
/**
1034
 
  Execute command saved in session and lex->sql_command.
 
1032
  Execute command saved in thd and lex->sql_command.
1035
1033
 
1036
1034
    Before every operation that can request a write lock for a table
1037
1035
    wait if a global read lock exists. However do not wait if this
1043
1041
    global read lock when it succeeds. This needs to be released by
1044
1042
    start_waiting_global_read_lock() after the operation.
1045
1043
 
1046
 
  @param session                       Thread handle
 
1044
  @param thd                       Thread handle
1047
1045
 
1048
1046
  @todo
1049
1047
    - Invalidate the table in the query cache if something changed
1061
1059
*/
1062
1060
 
1063
1061
int
1064
 
mysql_execute_command(Session *session)
 
1062
mysql_execute_command(THD *thd)
1065
1063
{
1066
1064
  int res= false;
1067
1065
  bool need_start_waiting= false; // have protection against global read lock
1068
1066
  int  up_result= 0;
1069
 
  LEX  *lex= session->lex;
 
1067
  LEX  *lex= thd->lex;
1070
1068
  /* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1071
1069
  SELECT_LEX *select_lex= &lex->select_lex;
1072
1070
  /* first table of first SELECT_LEX */
1108
1106
    Don't reset warnings when executing a stored routine.
1109
1107
  */
1110
1108
  if (all_tables || !lex->is_single_level_stmt())
1111
 
    drizzle_reset_errors(session, 0);
 
1109
    drizzle_reset_errors(thd, 0);
1112
1110
 
1113
 
  if (unlikely(session->slave_thread))
 
1111
  if (unlikely(thd->slave_thread))
1114
1112
  {
1115
1113
    /*
1116
1114
      Check if statment should be skipped because of slave filtering
1129
1127
        !(lex->sql_command == SQLCOM_SET_OPTION) &&
1130
1128
        !(lex->sql_command == SQLCOM_DROP_TABLE &&
1131
1129
          lex->drop_temporary && lex->drop_if_exists) &&
1132
 
        all_tables_not_ok(session, all_tables))
 
1130
        all_tables_not_ok(thd, all_tables))
1133
1131
    {
1134
1132
      /* we warn the slave SQL thread */
1135
1133
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
 
1134
      if (thd->one_shot_set)
 
1135
      {
 
1136
        /*
 
1137
          It's ok to check thd->one_shot_set here:
 
1138
 
 
1139
          The charsets in a MySQL 5.0 slave can change by both a binlogged
 
1140
          SET ONE_SHOT statement and the event-internal charset setting, 
 
1141
          and these two ways to change charsets do not seems to work
 
1142
          together.
 
1143
 
 
1144
          At least there seems to be problems in the rli cache for
 
1145
          charsets if we are using ONE_SHOT.  Note that this is normally no
 
1146
          problem because either the >= 5.0 slave reads a 4.1 binlog (with
 
1147
          ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
 
1148
        */
 
1149
        reset_one_shot_variables(thd);
 
1150
      }
1136
1151
      return(0);
1137
1152
    }
1138
1153
  }
1142
1157
      When option readonly is set deny operations which change non-temporary
1143
1158
      tables. Except for the replication thread and the 'super' users.
1144
1159
    */
1145
 
    if (deny_updates_if_read_only_option(session, all_tables))
 
1160
    if (deny_updates_if_read_only_option(thd, all_tables))
1146
1161
    {
1147
1162
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1148
1163
      return(-1);
1149
1164
    }
1150
1165
  } /* endif unlikely slave */
1151
 
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
1166
  status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1152
1167
 
1153
 
  assert(session->transaction.stmt.modified_non_trans_table == false);
 
1168
  assert(thd->transaction.stmt.modified_non_trans_table == false);
1154
1169
  
1155
1170
  switch (lex->sql_command) {
1156
1171
  case SQLCOM_SHOW_STATUS:
1157
1172
  {
1158
 
    system_status_var old_status_var= session->status_var;
1159
 
    session->initial_status_var= &old_status_var;
1160
 
    res= execute_sqlcom_select(session, all_tables);
 
1173
    system_status_var old_status_var= thd->status_var;
 
1174
    thd->initial_status_var= &old_status_var;
 
1175
    res= execute_sqlcom_select(thd, all_tables);
1161
1176
    /* Don't log SHOW STATUS commands to slow query log */
1162
 
    session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
 
1177
    thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1163
1178
                           SERVER_QUERY_NO_GOOD_INDEX_USED);
1164
1179
    /*
1165
1180
      restore status variables, as we don't want 'show status' to cause
1166
1181
      changes
1167
1182
    */
1168
1183
    pthread_mutex_lock(&LOCK_status);
1169
 
    add_diff_to_status(&global_status_var, &session->status_var,
 
1184
    add_diff_to_status(&global_status_var, &thd->status_var,
1170
1185
                       &old_status_var);
1171
 
    session->status_var= old_status_var;
 
1186
    thd->status_var= old_status_var;
1172
1187
    pthread_mutex_unlock(&LOCK_status);
1173
1188
    break;
1174
1189
  }
1181
1196
  case SQLCOM_SHOW_VARIABLES:
1182
1197
  case SQLCOM_SELECT:
1183
1198
  {
1184
 
    session->status_var.last_query_cost= 0.0;
1185
 
    res= execute_sqlcom_select(session, all_tables);
 
1199
    thd->status_var.last_query_cost= 0.0;
 
1200
    res= execute_sqlcom_select(thd, all_tables);
1186
1201
    break;
1187
1202
  }
1188
1203
  case SQLCOM_EMPTY_QUERY:
1189
 
    my_ok(session);
 
1204
    my_ok(thd);
1190
1205
    break;
1191
1206
 
1192
1207
  case SQLCOM_PURGE:
1193
1208
  {
1194
 
    res = purge_master_logs(session, lex->to_log);
 
1209
    res = purge_master_logs(thd, lex->to_log);
1195
1210
    break;
1196
1211
  }
1197
1212
  case SQLCOM_PURGE_BEFORE:
1200
1215
 
1201
1216
    /* PURGE MASTER LOGS BEFORE 'data' */
1202
1217
    it= (Item *)lex->value_list.head();
1203
 
    if ((!it->fixed && it->fix_fields(lex->session, &it)) ||
 
1218
    if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1204
1219
        it->check_cols(1))
1205
1220
    {
1206
1221
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1212
1227
      value of constant
1213
1228
    */
1214
1229
    it->quick_fix_field();
1215
 
    res = purge_master_logs_before_date(session, (ulong)it->val_int());
 
1230
    res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1216
1231
    break;
1217
1232
  }
1218
1233
  case SQLCOM_SHOW_WARNS:
1219
1234
  {
1220
 
    res= mysqld_show_warnings(session, (uint32_t)
 
1235
    res= mysqld_show_warnings(thd, (uint32_t)
1221
1236
                              ((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1222
1237
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1223
1238
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1226
1241
  }
1227
1242
  case SQLCOM_SHOW_ERRORS:
1228
1243
  {
1229
 
    res= mysqld_show_warnings(session, (uint32_t)
 
1244
    res= mysqld_show_warnings(thd, (uint32_t)
1230
1245
                              (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1231
1246
    break;
1232
1247
  }
1233
1248
  case SQLCOM_ASSIGN_TO_KEYCACHE:
1234
1249
  {
1235
1250
    assert(first_table == all_tables && first_table != 0);
1236
 
    res= mysql_assign_to_keycache(session, first_table, &lex->ident);
 
1251
    res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1237
1252
    break;
1238
1253
  }
1239
1254
  case SQLCOM_CHANGE_MASTER:
1240
1255
  {
1241
1256
    pthread_mutex_lock(&LOCK_active_mi);
1242
 
    res = change_master(session,active_mi);
 
1257
    res = change_master(thd,active_mi);
1243
1258
    pthread_mutex_unlock(&LOCK_active_mi);
1244
1259
    break;
1245
1260
  }
1248
1263
    pthread_mutex_lock(&LOCK_active_mi);
1249
1264
    if (active_mi != NULL)
1250
1265
    {
1251
 
      res = show_master_info(session, active_mi);
 
1266
      res = show_master_info(thd, active_mi);
1252
1267
    }
1253
1268
    else
1254
1269
    {
1255
 
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1270
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1256
1271
                   "the master info structure does not exist");
1257
 
      my_ok(session);
 
1272
      my_ok(thd);
1258
1273
    }
1259
1274
    pthread_mutex_unlock(&LOCK_active_mi);
1260
1275
    break;
1261
1276
  }
1262
1277
  case SQLCOM_SHOW_MASTER_STAT:
1263
1278
  {
1264
 
    res = show_binlog_info(session);
 
1279
    res = show_binlog_info(thd);
1265
1280
    break;
1266
1281
  }
1267
1282
 
1268
1283
  case SQLCOM_SHOW_ENGINE_STATUS:
1269
1284
    {
1270
 
      res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
 
1285
      res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
1271
1286
      break;
1272
1287
    }
1273
1288
  case SQLCOM_CREATE_TABLE:
1275
1290
    /* If CREATE TABLE of non-temporary table, do implicit commit */
1276
1291
    if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1277
1292
    {
1278
 
      if (end_active_trans(session))
 
1293
      if (end_active_trans(thd))
1279
1294
      {
1280
1295
        res= -1;
1281
1296
        break;
1299
1314
      safety, only in case of Alter_info we have to do (almost) a deep
1300
1315
      copy.
1301
1316
    */
1302
 
    Alter_info alter_info(lex->alter_info, session->mem_root);
 
1317
    Alter_info alter_info(lex->alter_info, thd->mem_root);
1303
1318
 
1304
 
    if (session->is_fatal_error)
 
1319
    if (thd->is_fatal_error)
1305
1320
    {
1306
1321
      /* If out of memory when creating a copy of alter_info. */
1307
1322
      res= 1;
1308
1323
      goto end_with_restore_list;
1309
1324
    }
1310
1325
 
1311
 
    if ((res= create_table_precheck(session, select_tables, create_table)))
 
1326
    if ((res= create_table_precheck(thd, select_tables, create_table)))
1312
1327
      goto end_with_restore_list;
1313
1328
 
1314
1329
    /* Might have been updated in create_table_precheck */
1316
1331
 
1317
1332
#ifdef HAVE_READLINK
1318
1333
    /* Fix names if symlinked tables */
1319
 
    if (append_file_to_dir(session, &create_info.data_file_name,
 
1334
    if (append_file_to_dir(thd, &create_info.data_file_name,
1320
1335
                           create_table->table_name) ||
1321
 
        append_file_to_dir(session, &create_info.index_file_name,
 
1336
        append_file_to_dir(thd, &create_info.index_file_name,
1322
1337
                           create_table->table_name))
1323
1338
      goto end_with_restore_list;
1324
1339
#endif
1348
1363
      TABLE in the same way. That way we avoid that a new table is
1349
1364
      created during a gobal read lock.
1350
1365
    */
1351
 
    if (!session->locked_tables &&
1352
 
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
1366
    if (!thd->locked_tables &&
 
1367
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1353
1368
    {
1354
1369
      res= 1;
1355
1370
      goto end_with_restore_list;
1367
1382
        create_table->create= true;
1368
1383
      }
1369
1384
 
1370
 
      if (!(res= open_and_lock_tables(session, lex->query_tables)))
 
1385
      if (!(res= open_and_lock_tables(thd, lex->query_tables)))
1371
1386
      {
1372
1387
        /*
1373
1388
          Is table which we are changing used somewhere in other parts
1377
1392
        {
1378
1393
          TableList *duplicate;
1379
1394
          create_table= lex->unlink_first_table(&link_to_local);
1380
 
          if ((duplicate= unique_table(session, create_table, select_tables, 0)))
 
1395
          if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
1381
1396
          {
1382
1397
            update_non_unique_table_error(create_table, "CREATE", duplicate);
1383
1398
            res= 1;
1401
1416
            CREATE from SELECT give its SELECT_LEX for SELECT,
1402
1417
            and item_list belong to SELECT
1403
1418
          */
1404
 
          res= handle_select(session, lex, result, 0);
 
1419
          res= handle_select(thd, lex, result, 0);
1405
1420
          delete result;
1406
1421
        }
1407
1422
      }
1413
1428
    {
1414
1429
      /* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1415
1430
      if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1416
 
        session->options|= OPTION_KEEP_LOG;
 
1431
        thd->options|= OPTION_KEEP_LOG;
1417
1432
      /* regular create */
1418
1433
      if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1419
 
        res= mysql_create_like_table(session, create_table, select_tables,
 
1434
        res= mysql_create_like_table(thd, create_table, select_tables,
1420
1435
                                     &create_info);
1421
1436
      else
1422
1437
      {
1423
 
        res= mysql_create_table(session, create_table->db,
 
1438
        res= mysql_create_table(thd, create_table->db,
1424
1439
                                create_table->table_name, &create_info,
1425
1440
                                &alter_info, 0, 0);
1426
1441
      }
1427
1442
      if (!res)
1428
 
        my_ok(session);
 
1443
        my_ok(thd);
1429
1444
    }
1430
1445
 
1431
1446
    /* put tables back for PS rexecuting */
1447
1462
  {
1448
1463
    /* Prepare stack copies to be re-execution safe */
1449
1464
    HA_CREATE_INFO create_info;
1450
 
    Alter_info alter_info(lex->alter_info, session->mem_root);
 
1465
    Alter_info alter_info(lex->alter_info, thd->mem_root);
1451
1466
 
1452
 
    if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
 
1467
    if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1453
1468
      goto error;
1454
1469
 
1455
1470
    assert(first_table == all_tables && first_table != 0);
1456
 
    if (end_active_trans(session))
 
1471
    if (end_active_trans(thd))
1457
1472
      goto error;
1458
1473
 
1459
1474
    memset(&create_info, 0, sizeof(create_info));
1460
1475
    create_info.db_type= 0;
1461
1476
    create_info.row_type= ROW_TYPE_NOT_USED;
1462
 
    create_info.default_table_charset= session->variables.collation_database;
 
1477
    create_info.default_table_charset= thd->variables.collation_database;
1463
1478
 
1464
 
    res= mysql_alter_table(session, first_table->db, first_table->table_name,
 
1479
    res= mysql_alter_table(thd, first_table->db, first_table->table_name,
1465
1480
                           &create_info, first_table, &alter_info,
1466
1481
                           0, (order_st*) 0, 0);
1467
1482
    break;
1469
1484
  case SQLCOM_SLAVE_START:
1470
1485
  {
1471
1486
    pthread_mutex_lock(&LOCK_active_mi);
1472
 
    start_slave(session,active_mi,1 /* net report*/);
 
1487
    start_slave(thd,active_mi,1 /* net report*/);
1473
1488
    pthread_mutex_unlock(&LOCK_active_mi);
1474
1489
    break;
1475
1490
  }
1487
1502
    To prevent that, refuse SLAVE STOP if the
1488
1503
    client thread has locked tables
1489
1504
  */
1490
 
  if (session->locked_tables || session->active_transaction() || session->global_read_lock)
 
1505
  if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1491
1506
  {
1492
1507
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1493
1508
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1495
1510
  }
1496
1511
  {
1497
1512
    pthread_mutex_lock(&LOCK_active_mi);
1498
 
    stop_slave(session,active_mi,1/* net report*/);
 
1513
    stop_slave(thd,active_mi,1/* net report*/);
1499
1514
    pthread_mutex_unlock(&LOCK_active_mi);
1500
1515
    break;
1501
1516
  }
1510
1525
        referenced from this structure will be modified.
1511
1526
      */
1512
1527
      HA_CREATE_INFO create_info(lex->create_info);
1513
 
      Alter_info alter_info(lex->alter_info, session->mem_root);
 
1528
      Alter_info alter_info(lex->alter_info, thd->mem_root);
1514
1529
 
1515
 
      if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
 
1530
      if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
1516
1531
      {
1517
1532
        goto error;
1518
1533
      }
1529
1544
 
1530
1545
      /* Don't yet allow changing of symlinks with ALTER TABLE */
1531
1546
      if (create_info.data_file_name)
1532
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1547
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1533
1548
                     "DATA DIRECTORY option ignored");
1534
1549
      if (create_info.index_file_name)
1535
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1550
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1536
1551
                     "INDEX DIRECTORY option ignored");
1537
1552
      create_info.data_file_name= create_info.index_file_name= NULL;
1538
1553
      /* ALTER TABLE ends previous transaction */
1539
 
      if (end_active_trans(session))
 
1554
      if (end_active_trans(thd))
1540
1555
        goto error;
1541
1556
 
1542
 
      if (!session->locked_tables &&
1543
 
          !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
1557
      if (!thd->locked_tables &&
 
1558
          !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1544
1559
      {
1545
1560
        res= 1;
1546
1561
        break;
1547
1562
      }
1548
1563
 
1549
 
      res= mysql_alter_table(session, select_lex->db, lex->name.str,
 
1564
      res= mysql_alter_table(thd, select_lex->db, lex->name.str,
1550
1565
                             &create_info,
1551
1566
                             first_table,
1552
1567
                             &alter_info,
1570
1585
      new_list= table->next_local[0];
1571
1586
    }
1572
1587
 
1573
 
    if (end_active_trans(session) || drizzle_rename_tables(session, first_table, 0))
 
1588
    if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1574
1589
      {
1575
1590
        goto error;
1576
1591
      }
1578
1593
  }
1579
1594
  case SQLCOM_SHOW_BINLOGS:
1580
1595
    {
1581
 
      res = show_binlogs(session);
 
1596
      res = show_binlogs(thd);
1582
1597
      break;
1583
1598
    }
1584
1599
  case SQLCOM_SHOW_CREATE:
1585
1600
    assert(first_table == all_tables && first_table != 0);
1586
1601
    {
1587
 
      res= mysqld_show_create(session, first_table);
 
1602
      res= mysqld_show_create(thd, first_table);
1588
1603
      break;
1589
1604
    }
1590
1605
  case SQLCOM_CHECKSUM:
1591
1606
  {
1592
1607
    assert(first_table == all_tables && first_table != 0);
1593
 
    res = mysql_checksum_table(session, first_table, &lex->check_opt);
 
1608
    res = mysql_checksum_table(thd, first_table, &lex->check_opt);
1594
1609
    break;
1595
1610
  }
1596
1611
  case SQLCOM_REPAIR:
1597
1612
  {
1598
1613
    assert(first_table == all_tables && first_table != 0);
1599
 
    res= mysql_repair_table(session, first_table, &lex->check_opt);
 
1614
    res= mysql_repair_table(thd, first_table, &lex->check_opt);
1600
1615
    /* ! we write after unlocking the table */
1601
1616
    /*
1602
1617
      Presumably, REPAIR and binlog writing doesn't require synchronization
1603
1618
    */
1604
 
    write_bin_log(session, true, session->query, session->query_length);
 
1619
    write_bin_log(thd, true, thd->query, thd->query_length);
1605
1620
    select_lex->table_list.first= (unsigned char*) first_table;
1606
1621
    lex->query_tables=all_tables;
1607
1622
    break;
1609
1624
  case SQLCOM_CHECK:
1610
1625
  {
1611
1626
    assert(first_table == all_tables && first_table != 0);
1612
 
    res = mysql_check_table(session, first_table, &lex->check_opt);
 
1627
    res = mysql_check_table(thd, first_table, &lex->check_opt);
1613
1628
    select_lex->table_list.first= (unsigned char*) first_table;
1614
1629
    lex->query_tables=all_tables;
1615
1630
    break;
1617
1632
  case SQLCOM_ANALYZE:
1618
1633
  {
1619
1634
    assert(first_table == all_tables && first_table != 0);
1620
 
    res= mysql_analyze_table(session, first_table, &lex->check_opt);
 
1635
    res= mysql_analyze_table(thd, first_table, &lex->check_opt);
1621
1636
    /* ! we write after unlocking the table */
1622
 
    write_bin_log(session, true, session->query, session->query_length);
 
1637
    write_bin_log(thd, true, thd->query, thd->query_length);
1623
1638
    select_lex->table_list.first= (unsigned char*) first_table;
1624
1639
    lex->query_tables=all_tables;
1625
1640
    break;
1628
1643
  case SQLCOM_OPTIMIZE:
1629
1644
  {
1630
1645
    assert(first_table == all_tables && first_table != 0);
1631
 
    res= mysql_optimize_table(session, first_table, &lex->check_opt);
 
1646
    res= mysql_optimize_table(thd, first_table, &lex->check_opt);
1632
1647
    /* ! we write after unlocking the table */
1633
 
    write_bin_log(session, true, session->query, session->query_length);
 
1648
    write_bin_log(thd, true, thd->query, thd->query_length);
1634
1649
    select_lex->table_list.first= (unsigned char*) first_table;
1635
1650
    lex->query_tables=all_tables;
1636
1651
    break;
1637
1652
  }
1638
1653
  case SQLCOM_UPDATE:
1639
1654
    assert(first_table == all_tables && first_table != 0);
1640
 
    if (update_precheck(session, all_tables))
 
1655
    if (update_precheck(thd, all_tables))
1641
1656
      break;
1642
1657
    assert(select_lex->offset_limit == 0);
1643
1658
    unit->set_limit(select_lex);
1644
 
    res= (up_result= mysql_update(session, all_tables,
 
1659
    res= (up_result= mysql_update(thd, all_tables,
1645
1660
                                  select_lex->item_list,
1646
1661
                                  lex->value_list,
1647
1662
                                  select_lex->where,
1659
1674
    /* if we switched from normal update, rights are checked */
1660
1675
    if (up_result != 2)
1661
1676
    {
1662
 
      if ((res= multi_update_precheck(session, all_tables)))
 
1677
      if ((res= multi_update_precheck(thd, all_tables)))
1663
1678
        break;
1664
1679
    }
1665
1680
    else
1666
1681
      res= 0;
1667
1682
 
1668
 
    res= mysql_multi_update_prepare(session);
 
1683
    res= mysql_multi_update_prepare(thd);
1669
1684
 
1670
1685
    /* Check slave filtering rules */
1671
 
    if (unlikely(session->slave_thread))
 
1686
    if (unlikely(thd->slave_thread))
1672
1687
    {
1673
 
      if (all_tables_not_ok(session, all_tables))
 
1688
      if (all_tables_not_ok(thd, all_tables))
1674
1689
      {
1675
1690
        if (res!= 0)
1676
1691
        {
1677
1692
          res= 0;             /* don't care of prev failure  */
1678
 
          session->clear_error(); /* filters are of highest prior */
 
1693
          thd->clear_error(); /* filters are of highest prior */
1679
1694
        }
1680
1695
        /* we warn the slave SQL thread */
1681
1696
        my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1689
1704
      if (res)
1690
1705
        break;
1691
1706
      if (opt_readonly &&
1692
 
          some_non_temp_table_to_be_updated(session, all_tables))
 
1707
          some_non_temp_table_to_be_updated(thd, all_tables))
1693
1708
      {
1694
1709
        my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1695
1710
        break;
1696
1711
      }
1697
1712
    }  /* unlikely */
1698
1713
 
1699
 
    res= mysql_multi_update(session, all_tables,
 
1714
    res= mysql_multi_update(thd, all_tables,
1700
1715
                            &select_lex->item_list,
1701
1716
                            &lex->value_list,
1702
1717
                            select_lex->where,
1708
1723
  case SQLCOM_INSERT:
1709
1724
  {
1710
1725
    assert(first_table == all_tables && first_table != 0);
1711
 
    if ((res= insert_precheck(session, all_tables)))
 
1726
    if ((res= insert_precheck(thd, all_tables)))
1712
1727
      break;
1713
1728
 
1714
 
    if (!session->locked_tables &&
1715
 
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
1729
    if (!thd->locked_tables &&
 
1730
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1716
1731
    {
1717
1732
      res= 1;
1718
1733
      break;
1719
1734
    }
1720
1735
 
1721
 
    res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
 
1736
    res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
1722
1737
                      lex->update_list, lex->value_list,
1723
1738
                      lex->duplicates, lex->ignore);
1724
1739
 
1729
1744
  {
1730
1745
    select_result *sel_result;
1731
1746
    assert(first_table == all_tables && first_table != 0);
1732
 
    if ((res= insert_precheck(session, all_tables)))
 
1747
    if ((res= insert_precheck(thd, all_tables)))
1733
1748
      break;
1734
1749
 
1735
1750
    /* Fix lock for first table */
1741
1756
 
1742
1757
    unit->set_limit(select_lex);
1743
1758
 
1744
 
    if (! session->locked_tables &&
1745
 
        ! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
 
1759
    if (! thd->locked_tables &&
 
1760
        ! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
1746
1761
    {
1747
1762
      res= 1;
1748
1763
      break;
1749
1764
    }
1750
1765
 
1751
 
    if (!(res= open_and_lock_tables(session, all_tables)))
 
1766
    if (!(res= open_and_lock_tables(thd, all_tables)))
1752
1767
    {
1753
1768
      /* Skip first table, which is the table we are inserting in */
1754
1769
      TableList *second_table= first_table->next_local;
1755
1770
      select_lex->table_list.first= (unsigned char*) second_table;
1756
1771
      select_lex->context.table_list= 
1757
1772
        select_lex->context.first_name_resolution_table= second_table;
1758
 
      res= mysql_insert_select_prepare(session);
 
1773
      res= mysql_insert_select_prepare(thd);
1759
1774
      if (!res && (sel_result= new select_insert(first_table,
1760
1775
                                                 first_table->table,
1761
1776
                                                 &lex->field_list,
1764
1779
                                                 lex->duplicates,
1765
1780
                                                 lex->ignore)))
1766
1781
      {
1767
 
        res= handle_select(session, lex, sel_result, OPTION_SETUP_TABLES_DONE);
 
1782
        res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1768
1783
        /*
1769
1784
          Invalidate the table in the query cache if something changed
1770
1785
          after unlocking when changes become visible.
1772
1787
          the unlock procedure.
1773
1788
        */
1774
1789
        if (first_table->lock_type ==  TL_WRITE_CONCURRENT_INSERT &&
1775
 
            session->lock)
 
1790
            thd->lock)
1776
1791
        {
1777
1792
          /* INSERT ... SELECT should invalidate only the very first table */
1778
1793
          TableList *save_table= first_table->next_local;
1788
1803
    break;
1789
1804
  }
1790
1805
  case SQLCOM_TRUNCATE:
1791
 
    if (end_active_trans(session))
 
1806
    if (end_active_trans(thd))
1792
1807
    {
1793
1808
      res= -1;
1794
1809
      break;
1798
1813
      Don't allow this within a transaction because we want to use
1799
1814
      re-generate table
1800
1815
    */
1801
 
    if (session->locked_tables || session->active_transaction())
 
1816
    if (thd->locked_tables || thd->active_transaction())
1802
1817
    {
1803
1818
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1804
1819
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1805
1820
      goto error;
1806
1821
    }
1807
1822
 
1808
 
    res= mysql_truncate(session, first_table, 0);
 
1823
    res= mysql_truncate(thd, first_table, 0);
1809
1824
 
1810
1825
    break;
1811
1826
  case SQLCOM_DELETE:
1814
1829
    assert(select_lex->offset_limit == 0);
1815
1830
    unit->set_limit(select_lex);
1816
1831
 
1817
 
    if (!session->locked_tables &&
1818
 
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
1832
    if (!thd->locked_tables &&
 
1833
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1819
1834
    {
1820
1835
      res= 1;
1821
1836
      break;
1822
1837
    }
1823
1838
 
1824
 
    res = mysql_delete(session, all_tables, select_lex->where,
 
1839
    res = mysql_delete(thd, all_tables, select_lex->where,
1825
1840
                       &select_lex->order_list,
1826
1841
                       unit->select_limit_cnt, select_lex->options,
1827
1842
                       false);
1831
1846
  {
1832
1847
    assert(first_table == all_tables && first_table != 0);
1833
1848
    TableList *aux_tables=
1834
 
      (TableList *)session->lex->auxiliary_table_list.first;
 
1849
      (TableList *)thd->lex->auxiliary_table_list.first;
1835
1850
    multi_delete *del_result;
1836
1851
 
1837
 
    if (!session->locked_tables &&
1838
 
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
 
1852
    if (!thd->locked_tables &&
 
1853
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
1839
1854
    {
1840
1855
      res= 1;
1841
1856
      break;
1842
1857
    }
1843
1858
 
1844
 
    if ((res= multi_delete_precheck(session, all_tables)))
 
1859
    if ((res= multi_delete_precheck(thd, all_tables)))
1845
1860
      break;
1846
1861
 
1847
1862
    /* condition will be true on SP re-excuting */
1848
1863
    if (select_lex->item_list.elements != 0)
1849
1864
      select_lex->item_list.empty();
1850
 
    if (add_item_to_list(session, new Item_null()))
 
1865
    if (add_item_to_list(thd, new Item_null()))
1851
1866
      goto error;
1852
1867
 
1853
 
    session->set_proc_info("init");
1854
 
    if ((res= open_and_lock_tables(session, all_tables)))
 
1868
    thd->set_proc_info("init");
 
1869
    if ((res= open_and_lock_tables(thd, all_tables)))
1855
1870
      break;
1856
1871
 
1857
 
    if ((res= mysql_multi_delete_prepare(session)))
 
1872
    if ((res= mysql_multi_delete_prepare(thd)))
1858
1873
      goto error;
1859
1874
 
1860
 
    if (!session->is_fatal_error &&
 
1875
    if (!thd->is_fatal_error &&
1861
1876
        (del_result= new multi_delete(aux_tables, lex->table_count)))
1862
1877
    {
1863
 
      res= mysql_select(session, &select_lex->ref_pointer_array,
 
1878
      res= mysql_select(thd, &select_lex->ref_pointer_array,
1864
1879
                        select_lex->get_table_list(),
1865
1880
                        select_lex->with_wild,
1866
1881
                        select_lex->item_list,
1867
1882
                        select_lex->where,
1868
1883
                        0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1869
1884
                        (order_st *)NULL,
1870
 
                        select_lex->options | session->options |
 
1885
                        select_lex->options | thd->options |
1871
1886
                        SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1872
1887
                        OPTION_SETUP_TABLES_DONE,
1873
1888
                        del_result, unit, select_lex);
1874
 
      res|= session->is_error();
 
1889
      res|= thd->is_error();
1875
1890
      if (res)
1876
1891
        del_result->abort();
1877
1892
      delete del_result;
1885
1900
    assert(first_table == all_tables && first_table != 0);
1886
1901
    if (!lex->drop_temporary)
1887
1902
    {
1888
 
      if (end_active_trans(session))
 
1903
      if (end_active_trans(thd))
1889
1904
        goto error;
1890
1905
    }
1891
1906
    else
1898
1913
        To not generate such irrelevant "table does not exist errors",
1899
1914
        we silently add IF EXISTS if TEMPORARY was used.
1900
1915
      */
1901
 
      if (session->slave_thread)
 
1916
      if (thd->slave_thread)
1902
1917
        lex->drop_if_exists= 1;
1903
1918
 
1904
1919
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1905
 
      session->options|= OPTION_KEEP_LOG;
 
1920
      thd->options|= OPTION_KEEP_LOG;
1906
1921
    }
1907
1922
    /* DDL and binlog write order protected by LOCK_open */
1908
 
    res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
 
1923
    res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
1909
1924
  }
1910
1925
  break;
1911
1926
  case SQLCOM_SHOW_PROCESSLIST:
1912
 
    mysqld_list_processes(session, NULL, lex->verbose);
 
1927
    mysqld_list_processes(thd, NULL, lex->verbose);
1913
1928
    break;
1914
1929
  case SQLCOM_SHOW_ENGINE_LOGS:
1915
1930
    {
1916
 
      res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
 
1931
      res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
1917
1932
      break;
1918
1933
    }
1919
1934
  case SQLCOM_CHANGE_DB:
1920
1935
  {
1921
1936
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
1922
1937
 
1923
 
    if (!mysql_change_db(session, &db_str, false))
1924
 
      my_ok(session);
 
1938
    if (!mysql_change_db(thd, &db_str, false))
 
1939
      my_ok(thd);
1925
1940
 
1926
1941
    break;
1927
1942
  }
1931
1946
    assert(first_table == all_tables && first_table != 0);
1932
1947
    if (lex->local_file)
1933
1948
    {
1934
 
      if (!(session->client_capabilities & CLIENT_LOCAL_FILES) ||
 
1949
      if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
1935
1950
          !opt_local_infile)
1936
1951
      {
1937
1952
        my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
1939
1954
      }
1940
1955
    }
1941
1956
 
1942
 
    res= mysql_load(session, lex->exchange, first_table, lex->field_list,
 
1957
    res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
1943
1958
                    lex->update_list, lex->value_list, lex->duplicates,
1944
1959
                    lex->ignore, (bool) lex->local_file);
1945
1960
    break;
1949
1964
  {
1950
1965
    List<set_var_base> *lex_var_list= &lex->var_list;
1951
1966
 
1952
 
    if (lex->autocommit && end_active_trans(session))
 
1967
    if (lex->autocommit && end_active_trans(thd))
1953
1968
      goto error;
1954
1969
 
1955
 
    if (open_and_lock_tables(session, all_tables))
1956
 
      goto error;
1957
 
    if (!(res= sql_set_variables(session, lex_var_list)))
1958
 
    {
1959
 
      my_ok(session);
 
1970
    if (open_and_lock_tables(thd, all_tables))
 
1971
      goto error;
 
1972
    if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
 
1973
    {
 
1974
      my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
 
1975
      goto error;
 
1976
    }
 
1977
    if (!(res= sql_set_variables(thd, lex_var_list)))
 
1978
    {
 
1979
      /*
 
1980
        If the previous command was a SET ONE_SHOT, we don't want to forget
 
1981
        about the ONE_SHOT property of that SET. So we use a |= instead of = .
 
1982
      */
 
1983
      thd->one_shot_set|= lex->one_shot_set;
 
1984
      my_ok(thd);
1960
1985
    }
1961
1986
    else
1962
1987
    {
1965
1990
        Send something semi-generic here since we don't know which
1966
1991
        assignment in the list caused the error.
1967
1992
      */
1968
 
      if (!session->is_error())
 
1993
      if (!thd->is_error())
1969
1994
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
1970
1995
      goto error;
1971
1996
    }
1980
2005
      done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
1981
2006
      false, mysqldump will not work.
1982
2007
    */
1983
 
    unlock_locked_tables(session);
1984
 
    if (session->options & OPTION_TABLE_LOCK)
 
2008
    unlock_locked_tables(thd);
 
2009
    if (thd->options & OPTION_TABLE_LOCK)
1985
2010
    {
1986
 
      end_active_trans(session);
1987
 
      session->options&= ~(OPTION_TABLE_LOCK);
 
2011
      end_active_trans(thd);
 
2012
      thd->options&= ~(OPTION_TABLE_LOCK);
1988
2013
    }
1989
 
    if (session->global_read_lock)
1990
 
      unlock_global_read_lock(session);
1991
 
    my_ok(session);
 
2014
    if (thd->global_read_lock)
 
2015
      unlock_global_read_lock(thd);
 
2016
    my_ok(thd);
1992
2017
    break;
1993
2018
  case SQLCOM_LOCK_TABLES:
1994
2019
    /*
1995
2020
      We try to take transactional locks if
1996
2021
      - only transactional locks are requested (lex->lock_transactional) and
1997
 
      - no non-transactional locks exist (!session->locked_tables).
 
2022
      - no non-transactional locks exist (!thd->locked_tables).
1998
2023
    */
1999
 
    if (lex->lock_transactional && !session->locked_tables)
 
2024
    if (lex->lock_transactional && !thd->locked_tables)
2000
2025
    {
2001
2026
      int rc;
2002
2027
      /*
2003
2028
        All requested locks are transactional and no non-transactional
2004
2029
        locks exist.
2005
2030
      */
2006
 
      if ((rc= try_transactional_lock(session, all_tables)) == -1)
 
2031
      if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2007
2032
        goto error;
2008
2033
      if (rc == 0)
2009
2034
      {
2010
 
        my_ok(session);
 
2035
        my_ok(thd);
2011
2036
        break;
2012
2037
      }
2013
2038
      /*
2024
2049
      requested. If yes, warn about the conversion to non-transactional
2025
2050
      locks or abort in strict mode.
2026
2051
    */
2027
 
    if (check_transactional_lock(session, all_tables))
 
2052
    if (check_transactional_lock(thd, all_tables))
2028
2053
      goto error;
2029
 
    unlock_locked_tables(session);
 
2054
    unlock_locked_tables(thd);
2030
2055
    /* we must end the trasaction first, regardless of anything */
2031
 
    if (end_active_trans(session))
 
2056
    if (end_active_trans(thd))
2032
2057
      goto error;
2033
 
    session->in_lock_tables=1;
2034
 
    session->options|= OPTION_TABLE_LOCK;
 
2058
    thd->in_lock_tables=1;
 
2059
    thd->options|= OPTION_TABLE_LOCK;
2035
2060
 
2036
 
    if (!(res= simple_open_n_lock_tables(session, all_tables)))
 
2061
    if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2037
2062
    {
2038
 
      session->locked_tables=session->lock;
2039
 
      session->lock=0;
2040
 
      (void) set_handler_table_locks(session, all_tables, false);
2041
 
      my_ok(session);
 
2063
      thd->locked_tables=thd->lock;
 
2064
      thd->lock=0;
 
2065
      (void) set_handler_table_locks(thd, all_tables, false);
 
2066
      my_ok(thd);
2042
2067
    }
2043
2068
    else
2044
2069
    {
2047
2072
        can free its locks if LOCK TABLES locked some tables before finding
2048
2073
        that it can't lock a table in its list
2049
2074
      */
2050
 
      ha_autocommit_or_rollback(session, 1);
2051
 
      end_active_trans(session);
2052
 
      session->options&= ~(OPTION_TABLE_LOCK);
 
2075
      ha_autocommit_or_rollback(thd, 1);
 
2076
      end_active_trans(thd);
 
2077
      thd->options&= ~(OPTION_TABLE_LOCK);
2053
2078
    }
2054
 
    session->in_lock_tables=0;
 
2079
    thd->in_lock_tables=0;
2055
2080
    break;
2056
2081
  case SQLCOM_CREATE_DB:
2057
2082
  {
2061
2086
      prepared statement- safe.
2062
2087
    */
2063
2088
    HA_CREATE_INFO create_info(lex->create_info);
2064
 
    if (end_active_trans(session))
 
2089
    if (end_active_trans(thd))
2065
2090
    {
2066
2091
      res= -1;
2067
2092
      break;
2068
2093
    }
2069
2094
    char *alias;
2070
 
    if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
 
2095
    if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
2071
2096
        check_db_name(&lex->name))
2072
2097
    {
2073
2098
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2074
2099
      break;
2075
2100
    }
2076
 
    res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
 
2101
    /*
 
2102
      If in a slave thread :
 
2103
      CREATE DATABASE DB was certainly not preceded by USE DB.
 
2104
      For that reason, db_ok() in sql/slave.cc did not check the
 
2105
      do_db/ignore_db. And as this query involves no tables, tables_ok()
 
2106
      above was not called. So we have to check rules again here.
 
2107
    */
 
2108
    if (thd->slave_thread && 
 
2109
        (!rpl_filter->db_ok(lex->name.str) ||
 
2110
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
 
2111
    {
 
2112
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
 
2113
      break;
 
2114
    }
 
2115
    res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
2077
2116
                              lex->name.str), &create_info, 0);
2078
2117
    break;
2079
2118
  }
2080
2119
  case SQLCOM_DROP_DB:
2081
2120
  {
2082
 
    if (end_active_trans(session))
 
2121
    if (end_active_trans(thd))
2083
2122
    {
2084
2123
      res= -1;
2085
2124
      break;
2089
2128
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2090
2129
      break;
2091
2130
    }
2092
 
    if (session->locked_tables || session->active_transaction())
 
2131
    /*
 
2132
      If in a slave thread :
 
2133
      DROP DATABASE DB may not be preceded by USE DB.
 
2134
      For that reason, maybe db_ok() in sql/slave.cc did not check the 
 
2135
      do_db/ignore_db. And as this query involves no tables, tables_ok()
 
2136
      above was not called. So we have to check rules again here.
 
2137
    */
 
2138
    if (thd->slave_thread && 
 
2139
        (!rpl_filter->db_ok(lex->name.str) ||
 
2140
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
 
2141
    {
 
2142
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
 
2143
      break;
 
2144
    }
 
2145
    if (thd->locked_tables || thd->active_transaction())
2093
2146
    {
2094
2147
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2095
2148
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2096
2149
      goto error;
2097
2150
    }
2098
 
    res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
 
2151
    res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
2099
2152
    break;
2100
2153
  }
2101
2154
  case SQLCOM_ALTER_DB:
2107
2160
      my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2108
2161
      break;
2109
2162
    }
2110
 
    if (session->locked_tables || session->active_transaction())
 
2163
    /*
 
2164
      If in a slave thread :
 
2165
      ALTER DATABASE DB may not be preceded by USE DB.
 
2166
      For that reason, maybe db_ok() in sql/slave.cc did not check the
 
2167
      do_db/ignore_db. And as this query involves no tables, tables_ok()
 
2168
      above was not called. So we have to check rules again here.
 
2169
    */
 
2170
    if (thd->slave_thread &&
 
2171
        (!rpl_filter->db_ok(db->str) ||
 
2172
         !rpl_filter->db_ok_with_wild_table(db->str)))
 
2173
    {
 
2174
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
 
2175
      break;
 
2176
    }
 
2177
    if (thd->locked_tables || thd->active_transaction())
2111
2178
    {
2112
2179
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2113
2180
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2114
2181
      goto error;
2115
2182
    }
2116
 
    res= mysql_alter_db(session, db->str, &create_info);
 
2183
    res= mysql_alter_db(thd, db->str, &create_info);
2117
2184
    break;
2118
2185
  }
2119
2186
  case SQLCOM_SHOW_CREATE_DB:
2123
2190
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2124
2191
      break;
2125
2192
    }
2126
 
    res= mysqld_show_create_db(session, lex->name.str, &lex->create_info);
 
2193
    res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
2127
2194
    break;
2128
2195
  }
2129
2196
  case SQLCOM_RESET:
2135
2202
      reload_cache() will tell us if we are allowed to write to the
2136
2203
      binlog or not.
2137
2204
    */
2138
 
    if (!reload_cache(session, lex->type, first_table, &write_to_binlog))
 
2205
    if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
2139
2206
    {
2140
2207
      /*
2141
2208
        We WANT to write and we CAN write.
2144
2211
      /*
2145
2212
        Presumably, RESET and binlog writing doesn't require synchronization
2146
2213
      */
2147
 
      write_bin_log(session, false, session->query, session->query_length);
2148
 
      my_ok(session);
 
2214
      write_bin_log(thd, false, thd->query, thd->query_length);
 
2215
      my_ok(thd);
2149
2216
    } 
2150
2217
    
2151
2218
    break;
2161
2228
      break;
2162
2229
    }
2163
2230
 
2164
 
    if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
 
2231
    if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
2165
2232
    {
2166
2233
      my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2167
2234
                 MYF(0));
2168
2235
      goto error;
2169
2236
    }
2170
 
    sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
 
2237
    sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2171
2238
    break;
2172
2239
  }
2173
2240
  case SQLCOM_BEGIN:
2174
 
    if (session->transaction.xid_state.xa_state != XA_NOTR)
 
2241
    if (thd->transaction.xid_state.xa_state != XA_NOTR)
2175
2242
    {
2176
2243
      my_error(ER_XAER_RMFAIL, MYF(0),
2177
 
               xa_state_names[session->transaction.xid_state.xa_state]);
 
2244
               xa_state_names[thd->transaction.xid_state.xa_state]);
2178
2245
      break;
2179
2246
    }
2180
2247
    /*
2181
2248
      Breakpoints for backup testing.
2182
2249
    */
2183
 
    if (begin_trans(session))
 
2250
    if (begin_trans(thd))
2184
2251
      goto error;
2185
 
    my_ok(session);
 
2252
    my_ok(thd);
2186
2253
    break;
2187
2254
  case SQLCOM_COMMIT:
2188
 
    if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
 
2255
    if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2189
2256
                              lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2190
2257
      goto error;
2191
 
    my_ok(session);
 
2258
    my_ok(thd);
2192
2259
    break;
2193
2260
  case SQLCOM_ROLLBACK:
2194
 
    if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
 
2261
    if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2195
2262
                              lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2196
2263
      goto error;
2197
 
    my_ok(session);
 
2264
    my_ok(thd);
2198
2265
    break;
2199
2266
  case SQLCOM_RELEASE_SAVEPOINT:
2200
2267
  {
2201
2268
    SAVEPOINT *sv;
2202
 
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
 
2269
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2203
2270
    {
2204
2271
      if (my_strnncoll(system_charset_info,
2205
2272
                       (unsigned char *)lex->ident.str, lex->ident.length,
2208
2275
    }
2209
2276
    if (sv)
2210
2277
    {
2211
 
      if (ha_release_savepoint(session, sv))
 
2278
      if (ha_release_savepoint(thd, sv))
2212
2279
        res= true; // cannot happen
2213
2280
      else
2214
 
        my_ok(session);
2215
 
      session->transaction.savepoints=sv->prev;
 
2281
        my_ok(thd);
 
2282
      thd->transaction.savepoints=sv->prev;
2216
2283
    }
2217
2284
    else
2218
2285
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2221
2288
  case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2222
2289
  {
2223
2290
    SAVEPOINT *sv;
2224
 
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
 
2291
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
2225
2292
    {
2226
2293
      if (my_strnncoll(system_charset_info,
2227
2294
                       (unsigned char *)lex->ident.str, lex->ident.length,
2230
2297
    }
2231
2298
    if (sv)
2232
2299
    {
2233
 
      if (ha_rollback_to_savepoint(session, sv))
 
2300
      if (ha_rollback_to_savepoint(thd, sv))
2234
2301
        res= true; // cannot happen
2235
2302
      else
2236
2303
      {
2237
 
        if (((session->options & OPTION_KEEP_LOG) || 
2238
 
             session->transaction.all.modified_non_trans_table) &&
2239
 
            !session->slave_thread)
2240
 
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2304
        if (((thd->options & OPTION_KEEP_LOG) || 
 
2305
             thd->transaction.all.modified_non_trans_table) &&
 
2306
            !thd->slave_thread)
 
2307
          push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2241
2308
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
2242
2309
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2243
 
        my_ok(session);
 
2310
        my_ok(thd);
2244
2311
      }
2245
 
      session->transaction.savepoints=sv;
 
2312
      thd->transaction.savepoints=sv;
2246
2313
    }
2247
2314
    else
2248
2315
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2249
2316
    break;
2250
2317
  }
2251
2318
  case SQLCOM_SAVEPOINT:
2252
 
    if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
2253
 
      my_ok(session);
 
2319
    if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
 
2320
      my_ok(thd);
2254
2321
    else
2255
2322
    {
2256
2323
      SAVEPOINT **sv, *newsv;
2257
 
      for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
 
2324
      for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
2258
2325
      {
2259
2326
        if (my_strnncoll(system_charset_info,
2260
2327
                         (unsigned char *)lex->ident.str, lex->ident.length,
2264
2331
      if (*sv) /* old savepoint of the same name exists */
2265
2332
      {
2266
2333
        newsv=*sv;
2267
 
        ha_release_savepoint(session, *sv); // it cannot fail
 
2334
        ha_release_savepoint(thd, *sv); // it cannot fail
2268
2335
        *sv=(*sv)->prev;
2269
2336
      }
2270
 
      else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
 
2337
      else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
2271
2338
                                               savepoint_alloc_size)) == 0)
2272
2339
      {
2273
2340
        my_error(ER_OUT_OF_RESOURCES, MYF(0));
2274
2341
        break;
2275
2342
      }
2276
 
      newsv->name=strmake_root(&session->transaction.mem_root,
 
2343
      newsv->name=strmake_root(&thd->transaction.mem_root,
2277
2344
                               lex->ident.str, lex->ident.length);
2278
2345
      newsv->length=lex->ident.length;
2279
2346
      /*
2281
2348
        we'll lose a little bit of memory in transaction mem_root, but it'll
2282
2349
        be free'd when transaction ends anyway
2283
2350
      */
2284
 
      if (ha_savepoint(session, newsv))
 
2351
      if (ha_savepoint(thd, newsv))
2285
2352
        res= true;
2286
2353
      else
2287
2354
      {
2288
 
        newsv->prev=session->transaction.savepoints;
2289
 
        session->transaction.savepoints=newsv;
2290
 
        my_ok(session);
 
2355
        newsv->prev=thd->transaction.savepoints;
 
2356
        thd->transaction.savepoints=newsv;
 
2357
        my_ok(thd);
2291
2358
      }
2292
2359
    }
2293
2360
    break;
2294
2361
  case SQLCOM_BINLOG_BASE64_EVENT:
2295
2362
  {
2296
 
    mysql_client_binlog_statement(session);
 
2363
    mysql_client_binlog_statement(thd);
2297
2364
    break;
2298
2365
  }
2299
2366
  default:
2300
2367
    assert(0);                             /* Impossible */
2301
 
    my_ok(session);
 
2368
    my_ok(thd);
2302
2369
    break;
2303
2370
  }
2304
 
  session->set_proc_info("query end");
 
2371
  thd->set_proc_info("query end");
 
2372
 
 
2373
  /*
 
2374
    Binlog-related cleanup:
 
2375
    Reset system variables temporarily modified by SET ONE SHOT.
 
2376
 
 
2377
    Exception: If this is a SET, do nothing. This is to allow
 
2378
    mysqlbinlog to print many SET commands (in this case we want the
 
2379
    charset temp setting to live until the real query). This is also
 
2380
    needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
 
2381
    immediately.
 
2382
  */
 
2383
  if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
 
2384
    reset_one_shot_variables(thd);
2305
2385
 
2306
2386
  /*
2307
2387
    The return value for ROW_COUNT() is "implementation dependent" if the
2309
2389
    wants. We also keep the last value in case of SQLCOM_CALL or
2310
2390
    SQLCOM_EXECUTE.
2311
2391
  */
2312
 
  if (!(sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
2313
 
    session->row_count_func= -1;
 
2392
  if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
 
2393
    thd->row_count_func= -1;
2314
2394
 
2315
2395
  goto finish;
2316
2396
 
2324
2404
      Release the protection against the global read lock and wake
2325
2405
      everyone, who might want to set a global read lock.
2326
2406
    */
2327
 
    start_waiting_global_read_lock(session);
 
2407
    start_waiting_global_read_lock(thd);
2328
2408
  }
2329
 
  return(res || session->is_error());
 
2409
  return(res || thd->is_error());
2330
2410
}
2331
2411
 
2332
 
bool execute_sqlcom_select(Session *session, TableList *all_tables)
 
2412
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
2333
2413
{
2334
 
  LEX   *lex= session->lex;
 
2414
  LEX   *lex= thd->lex;
2335
2415
  select_result *result=lex->result;
2336
2416
  bool res;
2337
2417
  /* assign global limit variable if limit is not given */
2339
2419
    SELECT_LEX *param= lex->unit.global_parameters;
2340
2420
    if (!param->explicit_limit)
2341
2421
      param->select_limit=
2342
 
        new Item_int((uint64_t) session->variables.select_limit);
 
2422
        new Item_int((uint64_t) thd->variables.select_limit);
2343
2423
  }
2344
 
  if (!(res= open_and_lock_tables(session, all_tables)))
 
2424
  if (!(res= open_and_lock_tables(thd, all_tables)))
2345
2425
  {
2346
2426
    if (lex->describe)
2347
2427
    {
2353
2433
      */
2354
2434
      if (!(result= new select_send()))
2355
2435
        return 1;                               /* purecov: inspected */
2356
 
      session->send_explain_fields(result);
2357
 
      res= mysql_explain_union(session, &session->lex->unit, result);
 
2436
      thd->send_explain_fields(result);
 
2437
      res= mysql_explain_union(thd, &thd->lex->unit, result);
2358
2438
      if (lex->describe & DESCRIBE_EXTENDED)
2359
2439
      {
2360
2440
        char buff[1024];
2361
2441
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2362
2442
        str.length(0);
2363
 
        session->lex->unit.print(&str, QT_ORDINARY);
 
2443
        thd->lex->unit.print(&str, QT_ORDINARY);
2364
2444
        str.append('\0');
2365
 
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2445
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2366
2446
                     ER_YES, str.ptr());
2367
2447
      }
2368
2448
      if (res)
2375
2455
    {
2376
2456
      if (!result && !(result= new select_send()))
2377
2457
        return 1;                               /* purecov: inspected */
2378
 
      res= handle_select(session, lex, result, 0);
 
2458
      res= handle_select(thd, lex, result, 0);
2379
2459
      if (result != lex->result)
2380
2460
        delete result;
2381
2461
    }
2399
2479
    corresponding exec. (Thus we only have to check in fix_fields.)
2400
2480
  - Passing to check_stack_overrun() prevents the compiler from removing it.
2401
2481
*/
2402
 
bool check_stack_overrun(Session *session, long margin, unsigned char *)
 
2482
bool check_stack_overrun(THD *thd, long margin,
 
2483
                         unsigned char *buf __attribute__((unused)))
2403
2484
{
2404
2485
  long stack_used;
2405
 
  assert(session == current_session);
2406
 
  if ((stack_used=used_stack(session->thread_stack,(char*) &stack_used)) >=
 
2486
  assert(thd == current_thd);
 
2487
  if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
2407
2488
      (long) (my_thread_stack_size - margin))
2408
2489
  {
2409
2490
    sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2419
2500
 
2420
2501
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2421
2502
{
2422
 
  LEX   *lex= current_session->lex;
 
2503
  LEX   *lex= current_thd->lex;
2423
2504
  ulong old_info=0;
2424
2505
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2425
2506
    return 1;
2447
2528
 
2448
2529
 
2449
2530
/**
2450
 
 Reset Session part responsible for command processing state.
 
2531
 Reset THD part responsible for command processing state.
2451
2532
 
2452
2533
   This needs to be called before execution of every statement
2453
2534
   (prepared or conventional).
2454
2535
   It is not called by substatements of routines.
2455
2536
 
2456
2537
  @todo
2457
 
   Make it a method of Session and align its name with the rest of
 
2538
   Make it a method of THD and align its name with the rest of
2458
2539
   reset/end/start/init methods.
2459
2540
  @todo
2460
 
   Call it after we use Session for queries, not before.
 
2541
   Call it after we use THD for queries, not before.
2461
2542
*/
2462
2543
 
2463
 
void mysql_reset_session_for_next_command(Session *session)
 
2544
void mysql_reset_thd_for_next_command(THD *thd)
2464
2545
{
2465
 
  session->free_list= 0;
2466
 
  session->select_number= 1;
 
2546
  thd->free_list= 0;
 
2547
  thd->select_number= 1;
2467
2548
  /*
2468
2549
    Those two lines below are theoretically unneeded as
2469
 
    Session::cleanup_after_query() should take care of this already.
 
2550
    THD::cleanup_after_query() should take care of this already.
2470
2551
  */
2471
 
  session->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
 
2552
  thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
 
2553
  thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2472
2554
 
2473
 
  session->query_start_used= 0;
2474
 
  session->is_fatal_error= 0;
2475
 
  session->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS | 
 
2555
  thd->query_start_used= 0;
 
2556
  thd->is_fatal_error= thd->time_zone_used= 0;
 
2557
  thd->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS | 
2476
2558
                          SERVER_QUERY_NO_INDEX_USED |
2477
2559
                          SERVER_QUERY_NO_GOOD_INDEX_USED);
2478
2560
  /*
2480
2562
    OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2481
2563
    in ha_rollback_trans() about some tables couldn't be rolled back.
2482
2564
  */
2483
 
  if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
 
2565
  if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2484
2566
  {
2485
 
    session->options&= ~OPTION_KEEP_LOG;
2486
 
    session->transaction.all.modified_non_trans_table= false;
 
2567
    thd->options&= ~OPTION_KEEP_LOG;
 
2568
    thd->transaction.all.modified_non_trans_table= false;
2487
2569
  }
2488
 
  assert(session->security_ctx== &session->main_security_ctx);
2489
 
  session->thread_specific_used= false;
 
2570
  assert(thd->security_ctx== &thd->main_security_ctx);
 
2571
  thd->thread_specific_used= false;
2490
2572
 
2491
2573
  if (opt_bin_log)
2492
2574
  {
2493
 
    reset_dynamic(&session->user_var_events);
2494
 
    session->user_var_events_alloc= session->mem_root;
 
2575
    reset_dynamic(&thd->user_var_events);
 
2576
    thd->user_var_events_alloc= thd->mem_root;
2495
2577
  }
2496
 
  session->clear_error();
2497
 
  session->main_da.reset_diagnostics_area();
2498
 
  session->total_warn_count=0;                  // Warnings for this query
2499
 
  session->sent_row_count= session->examined_row_count= 0;
 
2578
  thd->clear_error();
 
2579
  thd->main_da.reset_diagnostics_area();
 
2580
  thd->total_warn_count=0;                      // Warnings for this query
 
2581
  thd->rand_used= 0;
 
2582
  thd->sent_row_count= thd->examined_row_count= 0;
 
2583
 
 
2584
  /*
 
2585
    Because we come here only for start of top-statements, binlog format is
 
2586
    constant inside a complex statement (using stored functions) etc.
 
2587
  */
 
2588
  thd->reset_current_stmt_binlog_row_based();
2500
2589
 
2501
2590
  return;
2502
2591
}
2520
2609
mysql_new_select(LEX *lex, bool move_down)
2521
2610
{
2522
2611
  SELECT_LEX *select_lex;
2523
 
  Session *session= lex->session;
 
2612
  THD *thd= lex->thd;
2524
2613
 
2525
 
  if (!(select_lex= new (session->mem_root) SELECT_LEX()))
 
2614
  if (!(select_lex= new (thd->mem_root) SELECT_LEX()))
2526
2615
    return(1);
2527
 
  select_lex->select_number= ++session->select_number;
 
2616
  select_lex->select_number= ++thd->select_number;
2528
2617
  select_lex->parent_lex= lex; /* Used in init_query. */
2529
2618
  select_lex->init_query();
2530
2619
  select_lex->init_select();
2540
2629
    SELECT_LEX_UNIT *unit;
2541
2630
    lex->subqueries= true;
2542
2631
    /* first select_lex of subselect or derived table */
2543
 
    if (!(unit= new (session->mem_root) SELECT_LEX_UNIT()))
 
2632
    if (!(unit= new (thd->mem_root) SELECT_LEX_UNIT()))
2544
2633
      return(1);
2545
2634
 
2546
2635
    unit->init_query();
2547
2636
    unit->init_select();
2548
 
    unit->session= session;
 
2637
    unit->thd= thd;
2549
2638
    unit->include_down(lex->current_select);
2550
2639
    unit->link_next= 0;
2551
2640
    unit->link_prev= 0;
2566
2655
    }
2567
2656
    select_lex->include_neighbour(lex->current_select);
2568
2657
    SELECT_LEX_UNIT *unit= select_lex->master_unit();                              
2569
 
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
2658
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
2570
2659
      return(1);
2571
2660
    select_lex->context.outer_context= 
2572
2661
                unit->first_select()->context.outer_context;
2595
2684
 
2596
2685
void create_select_for_variable(const char *var_name)
2597
2686
{
2598
 
  Session *session;
 
2687
  THD *thd;
2599
2688
  LEX *lex;
2600
2689
  LEX_STRING tmp, null_lex_string;
2601
2690
  Item *var;
2602
2691
  char buff[MAX_SYS_VAR_LENGTH*2+4+8], *end;
2603
2692
 
2604
 
  session= current_session;
2605
 
  lex= session->lex;
 
2693
  thd= current_thd;
 
2694
  lex= thd->lex;
2606
2695
  mysql_init_select(lex);
2607
2696
  lex->sql_command= SQLCOM_SELECT;
2608
2697
  tmp.str= (char*) var_name;
2612
2701
    We set the name of Item to @@session.var_name because that then is used
2613
2702
    as the column name in the output.
2614
2703
  */
2615
 
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
 
2704
  if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
2616
2705
  {
2617
2706
    end= strxmov(buff, "@@session.", var_name, NULL);
2618
2707
    var->set_name(buff, end-buff, system_charset_info);
2619
 
    add_item_to_list(session, var);
 
2708
    add_item_to_list(thd, var);
2620
2709
  }
2621
2710
  return;
2622
2711
}
2629
2718
  lex->select_lex.select_limit= 0;
2630
2719
  lex->unit.select_limit_cnt= HA_POS_ERROR;
2631
2720
  lex->select_lex.table_list.save_and_clear(&lex->auxiliary_table_list);
2632
 
  lex->lock_option= TL_READ;
 
2721
  lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
2633
2722
  lex->query_tables= 0;
2634
2723
  lex->query_tables_last= &lex->query_tables;
2635
2724
}
2643
2732
/**
2644
2733
  Parse a query.
2645
2734
 
2646
 
  @param       session     Current thread
 
2735
  @param       thd     Current thread
2647
2736
  @param       inBuf   Begining of the query text
2648
2737
  @param       length  Length of the query text
2649
2738
  @param[out]  found_semicolon For multi queries, position of the character of
2650
2739
                               the next query in the query text.
2651
2740
*/
2652
2741
 
2653
 
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
 
2742
void mysql_parse(THD *thd, const char *inBuf, uint32_t length,
2654
2743
                 const char ** found_semicolon)
2655
2744
{
2656
2745
  /*
2661
2750
    - first, call query_cache_send_result_to_client,
2662
2751
    - second, if caching failed, initialise the lexical and syntactic parser.
2663
2752
    The problem is that the query cache depends on a clean initialization
2664
 
    of (among others) lex->safe_to_cache_query and session->server_status,
 
2753
    of (among others) lex->safe_to_cache_query and thd->server_status,
2665
2754
    which are reset respectively in
2666
2755
    - lex_start()
2667
 
    - mysql_reset_session_for_next_command()
 
2756
    - mysql_reset_thd_for_next_command()
2668
2757
    So, initializing the lexical analyser *before* using the query cache
2669
2758
    is required for the cache to work properly.
2670
2759
    FIXME: cleanup the dependencies in the code to simplify this.
2671
2760
  */
2672
 
  lex_start(session);
2673
 
  mysql_reset_session_for_next_command(session);
 
2761
  lex_start(thd);
 
2762
  mysql_reset_thd_for_next_command(thd);
2674
2763
 
2675
2764
  {
2676
 
    LEX *lex= session->lex;
2677
 
 
2678
 
    Lex_input_stream lip(session, inBuf, length);
2679
 
 
2680
 
    bool err= parse_sql(session, &lip);
 
2765
    LEX *lex= thd->lex;
 
2766
 
 
2767
    Lex_input_stream lip(thd, inBuf, length);
 
2768
 
 
2769
    bool err= parse_sql(thd, &lip);
2681
2770
    *found_semicolon= lip.found_semicolon;
2682
2771
 
2683
2772
    if (!err)
2684
2773
    {
2685
2774
      {
2686
 
        if (! session->is_error())
 
2775
        if (! thd->is_error())
2687
2776
        {
2688
2777
          /*
2689
 
            Binlog logs a string starting from session->query and having length
2690
 
            session->query_length; so we set session->query_length correctly (to not
 
2778
            Binlog logs a string starting from thd->query and having length
 
2779
            thd->query_length; so we set thd->query_length correctly (to not
2691
2780
            log several statements in one event, when we executed only first).
2692
2781
            We set it to not see the ';' (otherwise it would get into binlog
2693
2782
            and Query_log_event::print() would give ';;' output).
2696
2785
            Note that we don't need LOCK_thread_count to modify query_length.
2697
2786
          */
2698
2787
          if (*found_semicolon &&
2699
 
              (session->query_length= (ulong)(*found_semicolon - session->query)))
2700
 
            session->query_length--;
 
2788
              (thd->query_length= (ulong)(*found_semicolon - thd->query)))
 
2789
            thd->query_length--;
2701
2790
          /* Actually execute the query */
2702
 
          mysql_execute_command(session);
 
2791
          mysql_execute_command(thd);
2703
2792
        }
2704
2793
      }
2705
2794
    }
2706
2795
    else
2707
2796
    {
2708
 
      assert(session->is_error());
 
2797
      assert(thd->is_error());
2709
2798
    }
2710
2799
    lex->unit.cleanup();
2711
 
    session->set_proc_info("freeing items");
2712
 
    session->end_statement();
2713
 
    session->cleanup_after_query();
2714
 
    assert(session->change_list.is_empty());
 
2800
    thd->set_proc_info("freeing items");
 
2801
    thd->end_statement();
 
2802
    thd->cleanup_after_query();
 
2803
    assert(thd->change_list.is_empty());
2715
2804
  }
2716
2805
 
2717
2806
  return;
2728
2817
    1   can be ignored
2729
2818
*/
2730
2819
 
2731
 
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
 
2820
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
2732
2821
{
2733
 
  LEX *lex= session->lex;
 
2822
  LEX *lex= thd->lex;
2734
2823
  bool error= 0;
2735
2824
 
2736
 
  Lex_input_stream lip(session, inBuf, length);
2737
 
  lex_start(session);
2738
 
  mysql_reset_session_for_next_command(session);
 
2825
  Lex_input_stream lip(thd, inBuf, length);
 
2826
  lex_start(thd);
 
2827
  mysql_reset_thd_for_next_command(thd);
2739
2828
 
2740
 
  if (!parse_sql(session, &lip) &&
2741
 
      all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
 
2829
  if (!parse_sql(thd, &lip) &&
 
2830
      all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
2742
2831
    error= 1;                  /* Ignore question */
2743
 
  session->end_statement();
2744
 
  session->cleanup_after_query();
 
2832
  thd->end_statement();
 
2833
  thd->cleanup_after_query();
2745
2834
  return(error);
2746
2835
}
2747
2836
 
2754
2843
    Return 0 if ok
2755
2844
*/
2756
2845
 
2757
 
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
 
2846
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
2758
2847
                       char *length, char *decimals,
2759
2848
                       uint32_t type_modifier,
2760
2849
                       enum column_format_type column_format,
2765
2854
                       virtual_column_info *vcol_info)
2766
2855
{
2767
2856
  register Create_field *new_field;
2768
 
  LEX  *lex= session->lex;
 
2857
  LEX  *lex= thd->lex;
2769
2858
 
2770
2859
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
2771
2860
    return(1);                          /* purecov: inspected */
2831
2920
  }
2832
2921
 
2833
2922
  if (!(new_field= new Create_field()) ||
2834
 
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
 
2923
      new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
2835
2924
                      default_value, on_update_value, comment, change,
2836
2925
                      interval_list, cs, 0, column_format,
2837
2926
                      vcol_info))
2847
2936
 
2848
2937
void store_position_for_column(const char *name)
2849
2938
{
2850
 
  current_session->lex->last_field->after=const_cast<char*> (name);
 
2939
  current_thd->lex->last_field->after=const_cast<char*> (name);
2851
2940
}
2852
2941
 
2853
2942
bool
2854
 
add_proc_to_list(Session* session, Item *item)
 
2943
add_proc_to_list(THD* thd, Item *item)
2855
2944
{
2856
2945
  order_st *order;
2857
2946
  Item  **item_ptr;
2858
2947
 
2859
 
  if (!(order = (order_st *) session->alloc(sizeof(order_st)+sizeof(Item*))))
 
2948
  if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
2860
2949
    return 1;
2861
2950
  item_ptr = (Item**) (order+1);
2862
2951
  *item_ptr= item;
2863
2952
  order->item=item_ptr;
2864
2953
  order->free_me=0;
2865
 
  session->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
2954
  thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
2866
2955
  return 0;
2867
2956
}
2868
2957
 
2871
2960
  save order by and tables in own lists.
2872
2961
*/
2873
2962
 
2874
 
bool add_to_list(Session *session, SQL_LIST &list,Item *item,bool asc)
 
2963
bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc)
2875
2964
{
2876
2965
  order_st *order;
2877
 
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
 
2966
  if (!(order = (order_st *) thd->alloc(sizeof(order_st))))
2878
2967
    return(1);
2879
2968
  order->item_ptr= item;
2880
2969
  order->item= &order->item_ptr;
2906
2995
    \#  Pointer to TableList element added to the total table list
2907
2996
*/
2908
2997
 
2909
 
TableList *st_select_lex::add_table_to_list(Session *session,
 
2998
TableList *st_select_lex::add_table_to_list(THD *thd,
2910
2999
                                             Table_ident *table,
2911
3000
                                             LEX_STRING *alias,
2912
3001
                                             uint32_t table_options,
2917
3006
  register TableList *ptr;
2918
3007
  TableList *previous_table_ref; /* The table preceding the current one. */
2919
3008
  char *alias_str;
2920
 
  LEX *lex= session->lex;
 
3009
  LEX *lex= thd->lex;
2921
3010
 
2922
3011
  if (!table)
2923
3012
    return(0);                          // End of memory
2944
3033
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
2945
3034
      return(0);
2946
3035
    }
2947
 
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
 
3036
    if (!(alias_str= (char*) thd->memdup(alias_str,table->table.length+1)))
2948
3037
      return(0);
2949
3038
  }
2950
 
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
 
3039
  if (!(ptr = (TableList *) thd->calloc(sizeof(TableList))))
2951
3040
    return(0);                          /* purecov: inspected */
2952
3041
  if (table->db.str)
2953
3042
  {
2974
3063
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
2975
3064
  ptr->derived=     table->sel;
2976
3065
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
2977
 
                                      INFORMATION_SCHEMA_NAME.c_str()))
 
3066
                                      INFORMATION_SCHEMA_NAME.str))
2978
3067
  {
2979
 
    ST_SCHEMA_TABLE *schema_table= find_schema_table(session, ptr->table_name);
 
3068
    ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
2980
3069
    if (!schema_table ||
2981
3070
        (schema_table->hidden && 
2982
 
         ((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
 
3071
         ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 || 
2983
3072
          /*
2984
3073
            this check is used for show columns|keys from I_S hidden table
2985
3074
          */
2987
3076
          lex->sql_command == SQLCOM_SHOW_KEYS)))
2988
3077
    {
2989
3078
      my_error(ER_UNKNOWN_TABLE, MYF(0),
2990
 
               ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
 
3079
               ptr->table_name, INFORMATION_SCHEMA_NAME.str);
2991
3080
      return(0);
2992
3081
    }
2993
3082
    ptr->schema_table_name= ptr->table_name;
3059
3148
    created empty list after having saved the info on the old level
3060
3149
    in the initialized structure.
3061
3150
 
3062
 
  @param session         current thread
 
3151
  @param thd         current thread
3063
3152
 
3064
3153
  @retval
3065
3154
    0   if success
3067
3156
    1   otherwise
3068
3157
*/
3069
3158
 
3070
 
bool st_select_lex::init_nested_join(Session *session)
 
3159
bool st_select_lex::init_nested_join(THD *thd)
3071
3160
{
3072
3161
  TableList *ptr;
3073
3162
  nested_join_st *nested_join;
3074
3163
 
3075
 
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
3164
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3076
3165
                                       sizeof(nested_join_st))))
3077
3166
    return(1);
3078
3167
  nested_join= ptr->nested_join=
3096
3185
    If the current level contains only one member, the function
3097
3186
    moves it one level up, eliminating the nest.
3098
3187
 
3099
 
  @param session         current thread
 
3188
  @param thd         current thread
3100
3189
 
3101
3190
  @return
3102
3191
    - Pointer to TableList element added to the total table list, if success
3103
3192
    - 0, otherwise
3104
3193
*/
3105
3194
 
3106
 
TableList *st_select_lex::end_nested_join(Session *)
 
3195
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
3107
3196
{
3108
3197
  TableList *ptr;
3109
3198
  nested_join_st *nested_join;
3136
3225
 
3137
3226
    The function nest last join operation as if it was enclosed in braces.
3138
3227
 
3139
 
  @param session         current thread
 
3228
  @param thd         current thread
3140
3229
 
3141
3230
  @retval
3142
3231
    0  Error
3144
3233
    \#  Pointer to TableList element created for the new nested join
3145
3234
*/
3146
3235
 
3147
 
TableList *st_select_lex::nest_last_join(Session *session)
 
3236
TableList *st_select_lex::nest_last_join(THD *thd)
3148
3237
{
3149
3238
  TableList *ptr;
3150
3239
  nested_join_st *nested_join;
3151
3240
  List<TableList> *embedded_list;
3152
3241
 
3153
 
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
3242
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
3154
3243
                                       sizeof(nested_join_st))))
3155
3244
    return(0);
3156
3245
  nested_join= ptr->nested_join=
3232
3321
      SELECT * FROM t3 LEFT JOIN (t1 LEFT JOIN t2 ON on_expr2) ON on_expr1
3233
3322
   @endverbatim
3234
3323
 
3235
 
  @param session         current thread
 
3324
  @param thd         current thread
3236
3325
 
3237
3326
  @return
3238
3327
    - Pointer to the table representing the inner table, if success
3291
3380
    (SELECT ... order_st BY LIMIT n) order_st BY ...
3292
3381
    @endvarbatim
3293
3382
  
3294
 
  @param session_arg               thread handle
 
3383
  @param thd_arg                   thread handle
3295
3384
 
3296
3385
  @note
3297
3386
    The object is used to retrieve rows from the temporary table
3303
3392
    0     on success
3304
3393
*/
3305
3394
 
3306
 
bool st_select_lex_unit::add_fake_select_lex(Session *session_arg)
 
3395
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
3307
3396
{
3308
3397
  SELECT_LEX *first_sl= first_select();
3309
3398
  assert(!fake_select_lex);
3310
3399
 
3311
 
  if (!(fake_select_lex= new (session_arg->mem_root) SELECT_LEX()))
 
3400
  if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
3312
3401
      return(1);
3313
3402
  fake_select_lex->include_standalone(this, 
3314
3403
                                      (SELECT_LEX_NODE**)&fake_select_lex);
3315
3404
  fake_select_lex->select_number= INT_MAX;
3316
 
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
 
3405
  fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
3317
3406
  fake_select_lex->make_empty_select();
3318
3407
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3319
3408
  fake_select_lex->select_limit= 0;
3333
3422
    */ 
3334
3423
    global_parameters= fake_select_lex;
3335
3424
    fake_select_lex->no_table_names_allowed= 1;
3336
 
    session_arg->lex->current_select= fake_select_lex;
 
3425
    thd_arg->lex->current_select= fake_select_lex;
3337
3426
  }
3338
 
  session_arg->lex->pop_context();
 
3427
  thd_arg->lex->pop_context();
3339
3428
  return(0);
3340
3429
}
3341
3430
 
3349
3438
    to be used for name resolution, and push the newly created
3350
3439
    context to the stack of contexts of the query.
3351
3440
 
3352
 
  @param session       pointer to current thread
 
3441
  @param thd       pointer to current thread
3353
3442
  @param left_op   left  operand of the JOIN
3354
3443
  @param right_op  rigth operand of the JOIN
3355
3444
 
3360
3449
*/
3361
3450
 
3362
3451
bool
3363
 
push_new_name_resolution_context(Session *session,
 
3452
push_new_name_resolution_context(THD *thd,
3364
3453
                                 TableList *left_op, TableList *right_op)
3365
3454
{
3366
3455
  Name_resolution_context *on_context;
3367
 
  if (!(on_context= new (session->mem_root) Name_resolution_context))
 
3456
  if (!(on_context= new (thd->mem_root) Name_resolution_context))
3368
3457
    return true;
3369
3458
  on_context->init();
3370
3459
  on_context->first_name_resolution_table=
3371
3460
    left_op->first_leaf_for_name_resolution();
3372
3461
  on_context->last_name_resolution_table=
3373
3462
    right_op->last_leaf_for_name_resolution();
3374
 
  return session->lex->push_context(on_context);
 
3463
  return thd->lex->push_context(on_context);
3375
3464
}
3376
3465
 
3377
3466
 
3453
3542
/**
3454
3543
  Reload/resets privileges and the different caches.
3455
3544
 
3456
 
  @param session Thread handler (can be NULL!)
 
3545
  @param thd Thread handler (can be NULL!)
3457
3546
  @param options What should be reset/reloaded (tables, privileges, slave...)
3458
3547
  @param tables Tables to flush (if any)
3459
3548
  @param write_to_binlog True if we can write to the binlog.
3466
3555
 
3467
3556
  @return Error status code
3468
3557
    @retval 0 Ok
3469
 
    @retval !=0  Error; session->killed is set or session->is_error() is true
 
3558
    @retval !=0  Error; thd->killed is set or thd->is_error() is true
3470
3559
*/
3471
3560
 
3472
 
bool reload_cache(Session *session, ulong options, TableList *tables,
 
3561
bool reload_cache(THD *thd, ulong options, TableList *tables,
3473
3562
                          bool *write_to_binlog)
3474
3563
{
3475
3564
  bool result=0;
3491
3580
      than it would help them)
3492
3581
    */
3493
3582
    tmp_write_to_binlog= 0;
3494
 
    if (drizzle_bin_log.is_open())
 
3583
    if( mysql_bin_log.is_open() )
3495
3584
    {
3496
 
      drizzle_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
 
3585
      mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
3497
3586
    }
3498
3587
    pthread_mutex_lock(&LOCK_active_mi);
3499
3588
    rotate_relay_log(active_mi);
3500
3589
    pthread_mutex_unlock(&LOCK_active_mi);
3501
3590
 
3502
3591
    /* flush slow and general logs */
3503
 
    logger.flush_logs(session);
 
3592
    logger.flush_logs(thd);
3504
3593
 
3505
3594
    if (ha_flush_logs(NULL))
3506
3595
      result=1;
3513
3602
  */
3514
3603
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK)) 
3515
3604
  {
3516
 
    if ((options & REFRESH_READ_LOCK) && session)
 
3605
    if ((options & REFRESH_READ_LOCK) && thd)
3517
3606
    {
3518
3607
      /*
3519
3608
        We must not try to aspire a global read lock if we have a write
3520
3609
        locked table. This would lead to a deadlock when trying to
3521
3610
        reopen (and re-lock) the table after the flush.
3522
3611
      */
3523
 
      if (session->locked_tables)
 
3612
      if (thd->locked_tables)
3524
3613
      {
3525
 
        THR_LOCK_DATA **lock_p= session->locked_tables->locks;
3526
 
        THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
 
3614
        THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
 
3615
        THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
3527
3616
 
3528
3617
        for (; lock_p < end_p; lock_p++)
3529
3618
        {
3539
3628
        UNLOCK TABLES
3540
3629
      */
3541
3630
      tmp_write_to_binlog= 0;
3542
 
      if (lock_global_read_lock(session))
 
3631
      if (lock_global_read_lock(thd))
3543
3632
        return 1;                               // Killed
3544
 
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
 
3633
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3545
3634
                                  false : true, true);
3546
 
      if (make_global_read_lock_block_commit(session)) // Killed
 
3635
      if (make_global_read_lock_block_commit(thd)) // Killed
3547
3636
      {
3548
3637
        /* Don't leave things in a half-locked state */
3549
 
        unlock_global_read_lock(session);
 
3638
        unlock_global_read_lock(thd);
3550
3639
        return 1;
3551
3640
      }
3552
3641
    }
3553
3642
    else
3554
 
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
 
3643
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
3555
3644
                                  false : true, false);
3556
3645
    my_dbopt_cleanup();
3557
3646
  }
3558
 
  if (session && (options & REFRESH_STATUS))
3559
 
    refresh_status(session);
 
3647
  if (thd && (options & REFRESH_STATUS))
 
3648
    refresh_status(thd);
 
3649
  if (options & REFRESH_THREADS)
 
3650
    flush_thread_cache();
3560
3651
  if (options & REFRESH_MASTER)
3561
3652
  {
3562
 
    assert(session);
 
3653
    assert(thd);
3563
3654
    tmp_write_to_binlog= 0;
3564
 
    if (reset_master(session))
 
3655
    if (reset_master(thd))
3565
3656
    {
3566
3657
      result=1;
3567
3658
    }
3570
3661
 {
3571
3662
   tmp_write_to_binlog= 0;
3572
3663
   pthread_mutex_lock(&LOCK_active_mi);
3573
 
   if (reset_slave(session, active_mi))
 
3664
   if (reset_slave(thd, active_mi))
3574
3665
     result=1;
3575
3666
   pthread_mutex_unlock(&LOCK_active_mi);
3576
3667
 }
3582
3673
/**
3583
3674
  kill on thread.
3584
3675
 
3585
 
  @param session                        Thread class
 
3676
  @param thd                    Thread class
3586
3677
  @param id                     Thread id
3587
3678
  @param only_kill_query        Should it kill the query or the connection
3588
3679
 
3591
3682
*/
3592
3683
 
3593
3684
static unsigned int
3594
 
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
3685
kill_one_thread(THD *thd __attribute__((unused)),
 
3686
                ulong id, bool only_kill_query)
3595
3687
{
3596
 
  Session *tmp;
 
3688
  THD *tmp;
3597
3689
  uint32_t error=ER_NO_SUCH_THREAD;
3598
3690
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
3599
 
  I_List_iterator<Session> it(threads);
 
3691
  I_List_iterator<THD> it(threads);
3600
3692
  while ((tmp=it++))
3601
3693
  {
3602
3694
    if (tmp->command == COM_DAEMON)
3610
3702
  pthread_mutex_unlock(&LOCK_thread_count);
3611
3703
  if (tmp)
3612
3704
  {
3613
 
    tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
3705
    tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
3614
3706
    error=0;
3615
3707
    pthread_mutex_unlock(&tmp->LOCK_delete);
3616
3708
  }
3623
3715
 
3624
3716
  SYNOPSIS
3625
3717
    sql_kill()
3626
 
    session                     Thread class
 
3718
    thd                 Thread class
3627
3719
    id                  Thread id
3628
3720
    only_kill_query     Should it kill the query or the connection
3629
3721
*/
3630
3722
 
3631
 
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
3723
void sql_kill(THD *thd, ulong id, bool only_kill_query)
3632
3724
{
3633
3725
  uint32_t error;
3634
 
  if (!(error= kill_one_thread(session, id, only_kill_query)))
3635
 
    my_ok(session);
 
3726
  if (!(error= kill_one_thread(thd, id, only_kill_query)))
 
3727
    my_ok(thd);
3636
3728
  else
3637
3729
    my_error(error, MYF(0), id);
3638
3730
}
3640
3732
 
3641
3733
/** If pointer is not a null pointer, append filename to it. */
3642
3734
 
3643
 
bool append_file_to_dir(Session *session, const char **filename_ptr,
 
3735
bool append_file_to_dir(THD *thd, const char **filename_ptr,
3644
3736
                        const char *table_name)
3645
3737
{
3646
3738
  char buff[FN_REFLEN],*ptr, *end;
3657
3749
  /* Fix is using unix filename format on dos */
3658
3750
  my_stpcpy(buff,*filename_ptr);
3659
3751
  end=convert_dirname(buff, *filename_ptr, NULL);
3660
 
  if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
 
3752
  if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3661
3753
    return 1;                                   // End of memory
3662
3754
  *filename_ptr=ptr;
3663
3755
  strxmov(ptr,buff,table_name,NULL);
3676
3768
 
3677
3769
bool check_simple_select()
3678
3770
{
3679
 
  Session *session= current_session;
3680
 
  LEX *lex= session->lex;
 
3771
  THD *thd= current_thd;
 
3772
  LEX *lex= thd->lex;
3681
3773
  if (lex->current_select != &lex->select_lex)
3682
3774
  {
3683
3775
    char command[80];
3684
 
    Lex_input_stream *lip= session->m_lip;
 
3776
    Lex_input_stream *lip= thd->m_lip;
3685
3777
    strmake(command, lip->yylval->symbol.str,
3686
3778
            cmin((ulong)lip->yylval->symbol.length, sizeof(command)-1));
3687
3779
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3691
3783
}
3692
3784
 
3693
3785
 
 
3786
Comp_creator *comp_eq_creator(bool invert)
 
3787
{
 
3788
  return invert?(Comp_creator *)&ne_creator:(Comp_creator *)&eq_creator;
 
3789
}
 
3790
 
 
3791
 
 
3792
Comp_creator *comp_ge_creator(bool invert)
 
3793
{
 
3794
  return invert?(Comp_creator *)&lt_creator:(Comp_creator *)&ge_creator;
 
3795
}
 
3796
 
 
3797
 
 
3798
Comp_creator *comp_gt_creator(bool invert)
 
3799
{
 
3800
  return invert?(Comp_creator *)&le_creator:(Comp_creator *)&gt_creator;
 
3801
}
 
3802
 
 
3803
 
 
3804
Comp_creator *comp_le_creator(bool invert)
 
3805
{
 
3806
  return invert?(Comp_creator *)&gt_creator:(Comp_creator *)&le_creator;
 
3807
}
 
3808
 
 
3809
 
 
3810
Comp_creator *comp_lt_creator(bool invert)
 
3811
{
 
3812
  return invert?(Comp_creator *)&ge_creator:(Comp_creator *)&lt_creator;
 
3813
}
 
3814
 
 
3815
 
 
3816
Comp_creator *comp_ne_creator(bool invert)
 
3817
{
 
3818
  return invert?(Comp_creator *)&eq_creator:(Comp_creator *)&ne_creator;
 
3819
}
 
3820
 
 
3821
 
3694
3822
/**
3695
3823
  Construct ALL/ANY/SOME subquery Item.
3696
3824
 
3703
3831
    constructed Item (or 0 if out of memory)
3704
3832
*/
3705
3833
Item * all_any_subquery_creator(Item *left_expr,
3706
 
                                chooser_compare_func_creator cmp,
3707
 
                                bool all,
3708
 
                                SELECT_LEX *select_lex)
 
3834
                                chooser_compare_func_creator cmp,
 
3835
                                bool all,
 
3836
                                SELECT_LEX *select_lex)
3709
3837
{
3710
3838
  if ((cmp == &comp_eq_creator) && !all)       //  = ANY <=> IN
3711
3839
    return new Item_in_subselect(left_expr, select_lex);
3725
3853
/**
3726
3854
  Multi update query pre-check.
3727
3855
 
3728
 
  @param session                Thread handler
 
3856
  @param thd            Thread handler
3729
3857
  @param tables Global/local table list (have to be the same)
3730
3858
 
3731
3859
  @retval
3734
3862
    true  Error
3735
3863
*/
3736
3864
 
3737
 
bool multi_update_precheck(Session *session, TableList *)
 
3865
bool multi_update_precheck(THD *thd,
 
3866
                           TableList *tables __attribute__((unused)))
3738
3867
{
3739
3868
  const char *msg= 0;
3740
 
  LEX *lex= session->lex;
 
3869
  LEX *lex= thd->lex;
3741
3870
  SELECT_LEX *select_lex= &lex->select_lex;
3742
3871
 
3743
3872
  if (select_lex->item_list.elements != lex->value_list.elements)
3761
3890
/**
3762
3891
  Multi delete query pre-check.
3763
3892
 
3764
 
  @param session                        Thread handler
 
3893
  @param thd                    Thread handler
3765
3894
  @param tables         Global/local table list
3766
3895
 
3767
3896
  @retval
3770
3899
    true  error
3771
3900
*/
3772
3901
 
3773
 
bool multi_delete_precheck(Session *session, TableList *)
 
3902
bool multi_delete_precheck(THD *thd,
 
3903
                           TableList *tables __attribute__((unused)))
3774
3904
{
3775
 
  SELECT_LEX *select_lex= &session->lex->select_lex;
3776
 
  TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
3777
 
 
3778
 
  session->lex->query_tables_own_last= 0;
3779
 
  session->lex->query_tables_own_last= save_query_tables_own_last;
3780
 
 
3781
 
  if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
 
3905
  SELECT_LEX *select_lex= &thd->lex->select_lex;
 
3906
  TableList **save_query_tables_own_last= thd->lex->query_tables_own_last;
 
3907
 
 
3908
  thd->lex->query_tables_own_last= 0;
 
3909
  thd->lex->query_tables_own_last= save_query_tables_own_last;
 
3910
 
 
3911
  if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3782
3912
  {
3783
3913
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
3784
3914
               ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
3805
3935
  @return Matching table, NULL otherwise.
3806
3936
*/
3807
3937
 
3808
 
static TableList *multi_delete_table_match(LEX *, TableList *tbl,
3809
 
                                           TableList *tables)
 
3938
static TableList *multi_delete_table_match(LEX *lex __attribute__((unused)),
 
3939
                                            TableList *tbl,
 
3940
                                            TableList *tables)
3810
3941
{
3811
3942
  TableList *match= NULL;
3812
3943
 
3887
4018
/**
3888
4019
  simple UPDATE query pre-check.
3889
4020
 
3890
 
  @param session                Thread handler
 
4021
  @param thd            Thread handler
3891
4022
  @param tables Global table list
3892
4023
 
3893
4024
  @retval
3896
4027
    true  Error
3897
4028
*/
3898
4029
 
3899
 
bool update_precheck(Session *session, TableList *)
 
4030
bool update_precheck(THD *thd, TableList *tables __attribute__((unused)))
3900
4031
{
3901
 
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
 
4032
  if (thd->lex->select_lex.item_list.elements != thd->lex->value_list.elements)
3902
4033
  {
3903
4034
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
3904
4035
    return(true);
3910
4041
/**
3911
4042
  simple INSERT query pre-check.
3912
4043
 
3913
 
  @param session                Thread handler
 
4044
  @param thd            Thread handler
3914
4045
  @param tables Global table list
3915
4046
 
3916
4047
  @retval
3919
4050
    true   error
3920
4051
*/
3921
4052
 
3922
 
bool insert_precheck(Session *session, TableList *)
 
4053
bool insert_precheck(THD *thd, TableList *tables __attribute__((unused)))
3923
4054
{
3924
 
  LEX *lex= session->lex;
 
4055
  LEX *lex= thd->lex;
3925
4056
 
3926
4057
  /*
3927
4058
    Check that we have modify privileges for the first table and
3939
4070
/**
3940
4071
  CREATE TABLE query pre-check.
3941
4072
 
3942
 
  @param session                        Thread handler
 
4073
  @param thd                    Thread handler
3943
4074
  @param tables         Global table list
3944
4075
  @param create_table           Table which will be created
3945
4076
 
3949
4080
    true   Error
3950
4081
*/
3951
4082
 
3952
 
bool create_table_precheck(Session *, TableList *,
 
4083
bool create_table_precheck(THD *thd __attribute__((unused)),
 
4084
                           TableList *tables __attribute__((unused)),
3953
4085
                           TableList *create_table)
3954
4086
{
3955
4087
  bool error= true;                                 // Error message is given
3956
4088
 
3957
4089
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
3958
4090
  {
3959
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
 
4091
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
3960
4092
    return(true);
3961
4093
  }
3962
4094
 
3969
4101
/**
3970
4102
  negate given expression.
3971
4103
 
3972
 
  @param session  thread handler
 
4104
  @param thd  thread handler
3973
4105
  @param expr expression for negation
3974
4106
 
3975
4107
  @return
3976
4108
    negated expression
3977
4109
*/
3978
4110
 
3979
 
Item *negate_expression(Session *session, Item *expr)
 
4111
Item *negate_expression(THD *thd, Item *expr)
3980
4112
{
3981
4113
  Item *negated;
3982
4114
  if (expr->type() == Item::FUNC_ITEM &&
3984
4116
  {
3985
4117
    /* it is NOT(NOT( ... )) */
3986
4118
    Item *arg= ((Item_func *) expr)->arguments()[0];
3987
 
    enum_parsing_place place= session->lex->current_select->parsing_place;
 
4119
    enum_parsing_place place= thd->lex->current_select->parsing_place;
3988
4120
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
3989
4121
      return arg;
3990
4122
    /*
3994
4126
    return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
3995
4127
  }
3996
4128
 
3997
 
  if ((negated= expr->neg_transformer(session)) != 0)
 
4129
  if ((negated= expr->neg_transformer(thd)) != 0)
3998
4130
    return negated;
3999
4131
  return new Item_func_not(expr);
4000
4132
}
4001
4133
 
4002
4134
 
 
4135
/**
 
4136
  Check that byte length of a string does not exceed some limit.
 
4137
 
 
4138
  @param str         string to be checked
 
4139
  @param err_msg     error message to be displayed if the string is too long
 
4140
  @param max_length  max length
 
4141
 
 
4142
  @retval
 
4143
    false   the passed string is not longer than max_length
 
4144
  @retval
 
4145
    true    the passed string is longer than max_length
 
4146
 
 
4147
  NOTE
 
4148
    The function is not used in existing code but can be useful later?
 
4149
*/
 
4150
 
 
4151
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
 
4152
                              uint32_t max_byte_length)
 
4153
{
 
4154
  if (str->length <= max_byte_length)
 
4155
    return false;
 
4156
 
 
4157
  my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
 
4158
 
 
4159
  return true;
 
4160
}
 
4161
 
 
4162
 
4003
4163
/*
4004
4164
  Check that char length of a string does not exceed some limit.
4005
4165
 
4033
4193
}
4034
4194
 
4035
4195
 
4036
 
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
4037
 
                           uint32_t max_char_length,
4038
 
                           const char *param_for_err_msg)
 
4196
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
 
4197
                           uint32_t err_code, const char *param_for_err_msg)
4039
4198
{
4040
4199
#ifdef HAVE_CHARSET_utf8mb3
4041
4200
  /*
4094
4253
bool test_if_data_home_dir(const char *dir)
4095
4254
{
4096
4255
  char path[FN_REFLEN], conv_path[FN_REFLEN];
4097
 
  uint32_t dir_len, home_dir_len= strlen(drizzle_unpacked_real_data_home);
 
4256
  uint32_t dir_len, home_dir_len= strlen(mysql_unpacked_real_data_home);
4098
4257
 
4099
4258
  if (!dir)
4100
4259
    return(0);
4107
4266
  {
4108
4267
    if (!my_strnncoll(character_set_filesystem,
4109
4268
                      (const unsigned char*) conv_path, home_dir_len,
4110
 
                      (const unsigned char*) drizzle_unpacked_real_data_home,
 
4269
                      (const unsigned char*) mysql_unpacked_real_data_home,
4111
4270
                      home_dir_len))
4112
4271
      return(1);
4113
4272
  }
4115
4274
}
4116
4275
 
4117
4276
 
4118
 
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
 
4277
extern int MYSQLparse(void *thd); // from sql_yacc.cc
4119
4278
 
4120
4279
 
4121
4280
/**
4122
 
  This is a wrapper of DRIZZLEparse(). All the code should call parse_sql()
4123
 
  instead of DRIZZLEparse().
 
4281
  This is a wrapper of MYSQLparse(). All the code should call parse_sql()
 
4282
  instead of MYSQLparse().
4124
4283
 
4125
 
  @param session Thread context.
 
4284
  @param thd Thread context.
4126
4285
  @param lip Lexer context.
4127
4286
 
4128
4287
  @return Error status.
4130
4289
    @retval true on parsing error.
4131
4290
*/
4132
4291
 
4133
 
bool parse_sql(Session *session, Lex_input_stream *lip)
 
4292
bool parse_sql(THD *thd, Lex_input_stream *lip)
4134
4293
{
4135
 
  assert(session->m_lip == NULL);
 
4294
  assert(thd->m_lip == NULL);
4136
4295
 
4137
4296
  /* Set Lex_input_stream. */
4138
4297
 
4139
 
  session->m_lip= lip;
 
4298
  thd->m_lip= lip;
4140
4299
 
4141
4300
  /* Parse the query. */
4142
4301
 
4143
 
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
4144
 
 
4145
 
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
4146
 
 
4147
 
  assert(!mysql_parse_status || session->is_error());
 
4302
  bool mysql_parse_status= MYSQLparse(thd) != 0;
 
4303
 
 
4304
  /* Check that if MYSQLparse() failed, thd->is_error() is set. */
 
4305
 
 
4306
  assert(!mysql_parse_status || thd->is_error());
4148
4307
 
4149
4308
  /* Reset Lex_input_stream. */
4150
4309
 
4151
 
  session->m_lip= NULL;
 
4310
  thd->m_lip= NULL;
4152
4311
 
4153
4312
  /* That's it. */
4154
4313
 
4155
 
  return mysql_parse_status || session->is_fatal_error;
 
4314
  return mysql_parse_status || thd->is_fatal_error;
4156
4315
}
4157
4316
 
4158
4317
/**