~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2009-02-05 21:07:57 UTC
  • mto: This revision was merged to the branch mainline in revision 840.
  • Revision ID: mordred@inaugust.com-20090205210757-6487lf69y3mndcds
Fixed warnings badness in csv_alter_table test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
 
#include "sql_repl.h"
19
 
#include "rpl_filter.h"
20
 
#include "repl_failsafe.h"
21
 
#include <drizzled/drizzled_error_messages.h>
 
18
#include <libdrizzleclient/libdrizzle.h>
 
19
#include <mysys/hash.h>
 
20
#include <drizzled/logging.h>
 
21
#include <drizzled/db.h>
 
22
#include <drizzled/error.h>
 
23
#include <drizzled/nested_join.h>
 
24
#include <drizzled/query_id.h>
 
25
#include <drizzled/sql_parse.h>
 
26
#include <drizzled/data_home.h>
 
27
#include <drizzled/sql_base.h>
 
28
#include <drizzled/show.h>
 
29
#include <drizzled/rename.h>
 
30
#include <drizzled/function/time/unix_timestamp.h>
 
31
#include <drizzled/function/get_system_var.h>
 
32
#include <drizzled/item/cmpfunc.h>
 
33
#include <drizzled/item/null.h>
 
34
#include <drizzled/session.h>
 
35
#include <drizzled/sql_load.h>
 
36
#include <drizzled/connect.h>
 
37
#include <drizzled/lock.h>
 
38
#include <drizzled/select_send.h>
 
39
#include <bitset>
 
40
 
 
41
using namespace std;
22
42
 
23
43
/**
24
44
  @defgroup Runtime_Environment Runtime Environment
25
45
  @{
26
46
*/
27
47
 
28
 
 
 
48
extern size_t my_thread_stack_size;
 
49
extern const CHARSET_INFO *character_set_filesystem;
29
50
const char *any_db="*any*";     // Special symbol for check_access
30
51
 
31
52
const LEX_STRING command_name[COM_END+1]={
44
65
  { C_STRING_WITH_LEN("Ping") },
45
66
  { C_STRING_WITH_LEN("Time") },
46
67
  { C_STRING_WITH_LEN("Change user") },
47
 
  { C_STRING_WITH_LEN("Binlog Dump") },
48
68
  { C_STRING_WITH_LEN("Connect Out") },
49
 
  { C_STRING_WITH_LEN("Register Slave") },
50
69
  { C_STRING_WITH_LEN("Set option") },
51
70
  { C_STRING_WITH_LEN("Daemon") },
52
71
  { C_STRING_WITH_LEN("Error") }  // Last command number
56
75
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
57
76
};
58
77
 
59
 
static void unlock_locked_tables(THD *thd)
 
78
static void unlock_locked_tables(Session *session)
60
79
{
61
 
  if (thd->locked_tables)
 
80
  if (session->locked_tables)
62
81
  {
63
 
    thd->lock=thd->locked_tables;
64
 
    thd->locked_tables=0;                       // Will be automatically closed
65
 
    close_thread_tables(thd);                   // Free tables
 
82
    session->lock=session->locked_tables;
 
83
    session->locked_tables=0;                   // Will be automatically closed
 
84
    close_thread_tables(session);                       // Free tables
66
85
  }
67
86
}
68
87
 
69
88
 
70
 
bool end_active_trans(THD *thd)
 
89
bool end_active_trans(Session *session)
71
90
{
72
 
  int error=0;
73
 
  if (unlikely(thd->in_sub_stmt))
74
 
  {
75
 
    my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
76
 
    return(1);
77
 
  }
78
 
  if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
91
  int error= 0;
 
92
 
 
93
  if (session->transaction.xid_state.xa_state != XA_NOTR)
79
94
  {
80
95
    my_error(ER_XAER_RMFAIL, MYF(0),
81
 
             xa_state_names[thd->transaction.xid_state.xa_state]);
 
96
             xa_state_names[session->transaction.xid_state.xa_state]);
82
97
    return(1);
83
98
  }
84
 
  if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
 
99
  if (session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
85
100
                      OPTION_TABLE_LOCK))
86
101
  {
87
102
    /* Safety if one did "drop table" on locked tables */
88
 
    if (!thd->locked_tables)
89
 
      thd->options&= ~OPTION_TABLE_LOCK;
90
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
91
 
    if (ha_commit(thd))
 
103
    if (!session->locked_tables)
 
104
      session->options&= ~OPTION_TABLE_LOCK;
 
105
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
 
106
    if (ha_commit(session))
92
107
      error=1;
93
108
  }
94
 
  thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
95
 
  thd->transaction.all.modified_non_trans_table= false;
 
109
  session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
110
  session->transaction.all.modified_non_trans_table= false;
96
111
  return(error);
97
112
}
98
113
 
99
114
 
100
 
bool begin_trans(THD *thd)
 
115
bool begin_trans(Session *session)
101
116
{
102
 
  int error=0;
103
 
  if (unlikely(thd->in_sub_stmt))
104
 
  {
105
 
    my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
106
 
    return 1;
107
 
  }
108
 
  if (thd->locked_tables)
109
 
  {
110
 
    thd->lock=thd->locked_tables;
111
 
    thd->locked_tables=0;                       // Will be automatically closed
112
 
    close_thread_tables(thd);                   // Free tables
113
 
  }
114
 
  if (end_active_trans(thd))
 
117
  int error= 0;
 
118
  if (session->locked_tables)
 
119
  {
 
120
    session->lock=session->locked_tables;
 
121
    session->locked_tables=0;                   // Will be automatically closed
 
122
    close_thread_tables(session);                       // Free tables
 
123
  }
 
124
  if (end_active_trans(session))
115
125
    error= -1;
116
126
  else
117
127
  {
118
 
    LEX *lex= thd->lex;
119
 
    thd->options|= OPTION_BEGIN;
120
 
    thd->server_status|= SERVER_STATUS_IN_TRANS;
 
128
    LEX *lex= session->lex;
 
129
    session->options|= OPTION_BEGIN;
 
130
    session->server_status|= SERVER_STATUS_IN_TRANS;
121
131
    if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
122
 
      error= ha_start_consistent_snapshot(thd);
 
132
      error= ha_start_consistent_snapshot(session);
123
133
  }
124
134
  return error;
125
135
}
127
137
/**
128
138
  Returns true if all tables should be ignored.
129
139
*/
130
 
inline bool all_tables_not_ok(THD *thd, TABLE_LIST *tables)
131
 
{
132
 
  return rpl_filter->is_on() && tables &&
133
 
         !rpl_filter->tables_ok(thd->db, tables);
134
 
}
135
 
 
136
 
 
137
 
static bool some_non_temp_table_to_be_updated(THD *thd, TABLE_LIST *tables)
138
 
{
139
 
  for (TABLE_LIST *table= tables; table; table= table->next_global)
140
 
  {
141
 
    assert(table->db && table->table_name);
142
 
    if (table->updating &&
143
 
        !find_temporary_table(thd, table->db, table->table_name))
144
 
      return 1;
145
 
  }
146
 
  return 0;
147
 
}
148
 
 
 
140
inline bool all_tables_not_ok(Session *, TableList *)
 
141
{
 
142
  return false;
 
143
}
149
144
 
150
145
/**
151
146
  Mark all commands that somehow changes a table.
160
155
          a number of modified rows
161
156
*/
162
157
 
163
 
uint sql_command_flags[SQLCOM_END+1];
 
158
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
164
159
 
165
160
void init_update_queries(void)
166
161
{
167
 
  memset(&sql_command_flags, 0, sizeof(sql_command_flags));
 
162
  uint32_t x;
 
163
 
 
164
  for (x= 0; x <= SQLCOM_END; x++)
 
165
    sql_command_flags[x].reset();
168
166
 
169
167
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
170
168
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
192
190
  sql_command_flags[SQLCOM_SHOW_FIELDS]=      CF_STATUS_COMMAND;
193
191
  sql_command_flags[SQLCOM_SHOW_KEYS]=        CF_STATUS_COMMAND;
194
192
  sql_command_flags[SQLCOM_SHOW_VARIABLES]=   CF_STATUS_COMMAND;
195
 
  sql_command_flags[SQLCOM_SHOW_CHARSETS]=    CF_STATUS_COMMAND;
196
 
  sql_command_flags[SQLCOM_SHOW_COLLATIONS]=  CF_STATUS_COMMAND;
197
 
  sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
198
 
  sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
199
 
  sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
200
193
  sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
201
194
  sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
202
195
  sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
203
196
  sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
204
197
  sql_command_flags[SQLCOM_SHOW_CREATE_DB]=  CF_STATUS_COMMAND;
205
198
  sql_command_flags[SQLCOM_SHOW_CREATE]=  CF_STATUS_COMMAND;
206
 
  sql_command_flags[SQLCOM_SHOW_MASTER_STAT]=  CF_STATUS_COMMAND;
207
 
  sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]=  CF_STATUS_COMMAND;
208
199
 
209
200
   sql_command_flags[SQLCOM_SHOW_TABLES]=       (CF_STATUS_COMMAND |
210
201
                                               CF_SHOW_TABLE_COMMAND);
223
214
bool is_update_query(enum enum_sql_command command)
224
215
{
225
216
  assert(command >= 0 && command <= SQLCOM_END);
226
 
  return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
 
217
  return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
227
218
}
228
219
 
229
 
void execute_init_command(THD *thd, sys_var_str *init_command_var,
230
 
                          rw_lock_t *var_mutex)
 
220
void execute_init_command(Session *session, sys_var_str *init_command_var,
 
221
                          pthread_rwlock_t *var_mutex)
231
222
{
232
223
  Vio* save_vio;
233
224
  ulong save_client_capabilities;
234
225
 
235
 
  thd_proc_info(thd, "Execution of init_command");
 
226
  session->set_proc_info("Execution of init_command");
236
227
  /*
237
228
    We need to lock init_command_var because
238
229
    during execution of init_command_var query
239
230
    values of init_command_var can't be changed
240
231
  */
241
 
  rw_rdlock(var_mutex);
242
 
  save_client_capabilities= thd->client_capabilities;
243
 
  thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
232
  pthread_rwlock_rdlock(var_mutex);
 
233
  save_client_capabilities= session->client_capabilities;
 
234
  session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
244
235
  /*
245
236
    We don't need return result of execution to client side.
246
 
    To forbid this we should set thd->net.vio to 0.
 
237
    To forbid this we should set session->net.vio to 0.
247
238
  */
248
 
  save_vio= thd->net.vio;
249
 
  thd->net.vio= 0;
250
 
  dispatch_command(COM_QUERY, thd,
 
239
  save_vio= session->net.vio;
 
240
  session->net.vio= 0;
 
241
  dispatch_command(COM_QUERY, session,
251
242
                   init_command_var->value,
252
243
                   init_command_var->value_length);
253
 
  rw_unlock(var_mutex);
254
 
  thd->client_capabilities= save_client_capabilities;
255
 
  thd->net.vio= save_vio;
 
244
  pthread_rwlock_unlock(var_mutex);
 
245
  session->client_capabilities= save_client_capabilities;
 
246
  session->net.vio= save_vio;
256
247
}
257
248
 
258
249
/**
259
250
  Ends the current transaction and (maybe) begin the next.
260
251
 
261
 
  @param thd            Current thread
 
252
  @param session            Current thread
262
253
  @param completion     Completion type
263
254
 
264
255
  @retval
265
256
    0   OK
266
257
*/
267
258
 
268
 
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
 
259
int end_trans(Session *session, enum enum_mysql_completiontype completion)
269
260
{
270
261
  bool do_release= 0;
271
262
  int res= 0;
272
263
 
273
 
  if (unlikely(thd->in_sub_stmt))
274
 
  {
275
 
    my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
276
 
    return(1);
277
 
  }
278
 
  if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
264
  if (session->transaction.xid_state.xa_state != XA_NOTR)
279
265
  {
280
266
    my_error(ER_XAER_RMFAIL, MYF(0),
281
 
             xa_state_names[thd->transaction.xid_state.xa_state]);
 
267
             xa_state_names[session->transaction.xid_state.xa_state]);
282
268
    return(1);
283
269
  }
284
270
  switch (completion) {
288
274
     even if there is a problem with the OPTION_AUTO_COMMIT flag
289
275
     (Which of course should never happen...)
290
276
    */
291
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
292
 
    res= ha_commit(thd);
293
 
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
294
 
    thd->transaction.all.modified_non_trans_table= false;
 
277
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
 
278
    res= ha_commit(session);
 
279
    session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
280
    session->transaction.all.modified_non_trans_table= false;
295
281
    break;
296
282
  case COMMIT_RELEASE:
297
283
    do_release= 1; /* fall through */
298
284
  case COMMIT_AND_CHAIN:
299
 
    res= end_active_trans(thd);
 
285
    res= end_active_trans(session);
300
286
    if (!res && completion == COMMIT_AND_CHAIN)
301
 
      res= begin_trans(thd);
 
287
      res= begin_trans(session);
302
288
    break;
303
289
  case ROLLBACK_RELEASE:
304
290
    do_release= 1; /* fall through */
305
291
  case ROLLBACK:
306
292
  case ROLLBACK_AND_CHAIN:
307
293
  {
308
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
309
 
    if (ha_rollback(thd))
 
294
    session->server_status&= ~SERVER_STATUS_IN_TRANS;
 
295
    if (ha_rollback(session))
310
296
      res= -1;
311
 
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
312
 
    thd->transaction.all.modified_non_trans_table= false;
 
297
    session->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
 
298
    session->transaction.all.modified_non_trans_table= false;
313
299
    if (!res && (completion == ROLLBACK_AND_CHAIN))
314
 
      res= begin_trans(thd);
 
300
      res= begin_trans(session);
315
301
    break;
316
302
  }
317
303
  default:
321
307
  }
322
308
 
323
309
  if (res < 0)
324
 
    my_error(thd->killed_errno(), MYF(0));
 
310
    my_error(session->killed_errno(), MYF(0));
325
311
  else if ((res == 0) && do_release)
326
 
    thd->killed= THD::KILL_CONNECTION;
 
312
    session->killed= Session::KILL_CONNECTION;
327
313
 
328
314
  return(res);
329
315
}
341
327
    1  request of thread shutdown (see dispatch_command() description)
342
328
*/
343
329
 
344
 
bool do_command(THD *thd)
 
330
bool do_command(Session *session)
345
331
{
346
332
  bool return_value;
347
333
  char *packet= 0;
348
334
  ulong packet_length;
349
 
  NET *net= &thd->net;
 
335
  NET *net= &session->net;
350
336
  enum enum_server_command command;
351
337
 
352
338
  /*
353
339
    indicator of uninitialized lex => normal flow of errors handling
354
340
    (see my_message_sql)
355
341
  */
356
 
  thd->lex->current_select= 0;
 
342
  session->lex->current_select= 0;
357
343
 
358
344
  /*
359
345
    This thread will do a blocking read from the client which
361
347
    the client, the connection is closed or "net_wait_timeout"
362
348
    number of seconds has passed
363
349
  */
364
 
  my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
 
350
  my_net_set_read_timeout(net, session->variables.net_wait_timeout);
365
351
 
366
352
  /*
367
 
    XXX: this code is here only to clear possible errors of init_connect. 
 
353
    XXX: this code is here only to clear possible errors of init_connect.
368
354
    Consider moving to init_connect() instead.
369
355
  */
370
 
  thd->clear_error();                           // Clear error message
371
 
  thd->main_da.reset_diagnostics_area();
 
356
  session->clear_error();                               // Clear error message
 
357
  session->main_da.reset_diagnostics_area();
372
358
 
373
359
  net_new_transaction(net);
374
360
 
378
364
    /* Check if we can continue without closing the connection */
379
365
 
380
366
    /* The error must be set. */
381
 
    assert(thd->is_error());
382
 
    net_end_statement(thd);
 
367
    /* This assert is killing me - and tracking down why the error isn't
 
368
     * set here is a waste since the protocol lib is being replaced. */ 
 
369
    //assert(session->is_error());
 
370
    net_end_statement(session);
383
371
 
384
372
    if (net->error != 3)
385
373
    {
404
392
  if (packet_length == 0)                       /* safety */
405
393
  {
406
394
    /* Initialize with COM_SLEEP packet */
407
 
    packet[0]= (uchar) COM_SLEEP;
 
395
    packet[0]= (unsigned char) COM_SLEEP;
408
396
    packet_length= 1;
409
397
  }
410
398
  /* Do not rely on my_net_read, extra safety against programming errors. */
411
399
  packet[packet_length]= '\0';                  /* safety */
412
400
 
413
 
  command= (enum enum_server_command) (uchar) packet[0];
 
401
  command= (enum enum_server_command) (unsigned char) packet[0];
414
402
 
415
403
  if (command >= COM_END)
416
404
    command= COM_END;                           // Wrong command
417
405
 
418
406
  /* Restore read timeout value */
419
 
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
 
407
  my_net_set_read_timeout(net, session->variables.net_read_timeout);
420
408
 
421
409
  assert(packet_length);
422
 
  return_value= dispatch_command(command, thd, packet+1, (uint) (packet_length-1));
 
410
  return_value= dispatch_command(command, session, packet+1, (uint32_t) (packet_length-1));
423
411
 
424
412
out:
425
413
  return(return_value);
426
414
}
427
415
 
428
416
/**
429
 
  Determine if an attempt to update a non-temporary table while the
430
 
  read-only option was enabled has been made.
431
 
 
432
 
  This is a helper function to mysql_execute_command.
433
 
 
434
 
  @note SQLCOM_MULTI_UPDATE is an exception and dealt with elsewhere.
435
 
 
436
 
  @see mysql_execute_command
437
 
 
438
 
  @returns Status code
439
 
    @retval true The statement should be denied.
440
 
    @retval false The statement isn't updating any relevant tables.
441
 
*/
442
 
 
443
 
static bool deny_updates_if_read_only_option(THD *thd,
444
 
                                                TABLE_LIST *all_tables)
445
 
{
446
 
  if (!opt_readonly)
447
 
    return(false);
448
 
 
449
 
  LEX *lex= thd->lex;
450
 
 
451
 
  if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
452
 
    return(false);
453
 
 
454
 
  /* Multi update is an exception and is dealt with later. */
455
 
  if (lex->sql_command == SQLCOM_UPDATE_MULTI)
456
 
    return(false);
457
 
 
458
 
  const bool create_temp_tables= 
459
 
    (lex->sql_command == SQLCOM_CREATE_TABLE) &&
460
 
    (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
461
 
 
462
 
  const bool drop_temp_tables= 
463
 
    (lex->sql_command == SQLCOM_DROP_TABLE) &&
464
 
    lex->drop_temporary;
465
 
 
466
 
  const bool update_real_tables=
467
 
    some_non_temp_table_to_be_updated(thd, all_tables) &&
468
 
    !(create_temp_tables || drop_temp_tables);
469
 
 
470
 
 
471
 
  const bool create_or_drop_databases=
472
 
    (lex->sql_command == SQLCOM_CREATE_DB) ||
473
 
    (lex->sql_command == SQLCOM_DROP_DB);
474
 
 
475
 
  if (update_real_tables || create_or_drop_databases)
476
 
  {
477
 
      /*
478
 
        An attempt was made to modify one or more non-temporary tables.
479
 
      */
480
 
      return(true);
481
 
  }
482
 
 
483
 
 
484
 
  /* Assuming that only temporary tables are modified. */
485
 
  return(false);
486
 
}
487
 
 
488
 
/**
489
417
  Perform one connection-level (COM_XXXX) command.
490
418
 
491
419
  @param command         type of command to perform
492
 
  @param thd             connection handle
 
420
  @param session             connection handle
493
421
  @param packet          data for the command, packet is always null-terminated
494
422
  @param packet_length   length of packet + 1 (to show that data is
495
423
                         null-terminated) except for COM_SLEEP, where it
496
424
                         can be zero.
497
425
 
498
426
  @todo
499
 
    set thd->lex->sql_command to SQLCOM_END here.
 
427
    set session->lex->sql_command to SQLCOM_END here.
500
428
  @todo
501
429
    The following has to be changed to an 8 byte integer
502
430
 
506
434
    1   request of thread shutdown, i. e. if command is
507
435
        COM_QUIT/COM_SHUTDOWN
508
436
*/
509
 
bool dispatch_command(enum enum_server_command command, THD *thd,
510
 
                      char* packet, uint packet_length)
 
437
bool dispatch_command(enum enum_server_command command, Session *session,
 
438
                      char* packet, uint32_t packet_length)
511
439
{
512
 
  NET *net= &thd->net;
 
440
  NET *net= &session->net;
513
441
  bool error= 0;
 
442
  Query_id &query_id= Query_id::get_query_id();
514
443
 
515
 
  thd->command=command;
516
 
  /*
517
 
    Commands which always take a long time are logged into
518
 
    the slow log only if opt_log_slow_admin_statements is set.
519
 
  */
520
 
  thd->enable_slow_log= true;
521
 
  thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
522
 
  thd->set_time();
523
 
  VOID(pthread_mutex_lock(&LOCK_thread_count));
524
 
  thd->query_id= global_query_id;
 
444
  session->command=command;
 
445
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
446
  session->set_time();
 
447
  pthread_mutex_lock(&LOCK_thread_count);
 
448
  session->query_id= query_id.value();
525
449
 
526
450
  switch( command ) {
527
451
  /* Ignore these statements. */
529
453
    break;
530
454
  /* Increase id and count all other statements. */
531
455
  default:
532
 
    statistic_increment(thd->status_var.questions, &LOCK_status);
533
 
    next_query_id();
 
456
    statistic_increment(session->status_var.questions, &LOCK_status);
 
457
    query_id.next();
534
458
  }
535
459
 
536
460
  thread_running++;
537
 
  /* TODO: set thd->lex->sql_command to SQLCOM_END here */
538
 
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
539
 
 
540
 
  thd->server_status&=
 
461
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
462
  pthread_mutex_unlock(&LOCK_thread_count);
 
463
 
 
464
  logging_pre_do(session);
 
465
 
 
466
  session->server_status&=
541
467
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
542
468
  switch (command) {
543
469
  case COM_INIT_DB:
544
470
  {
545
471
    LEX_STRING tmp;
546
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
547
 
    thd->convert_string(&tmp, system_charset_info,
548
 
                        packet, packet_length, thd->charset());
549
 
    if (!mysql_change_db(thd, &tmp, false))
 
472
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
 
473
    session->convert_string(&tmp, system_charset_info,
 
474
                        packet, packet_length, session->charset());
 
475
    if (!mysql_change_db(session, &tmp, false))
550
476
    {
551
 
      general_log_write(thd, command, thd->db, thd->db_length);
552
 
      my_ok(thd);
 
477
      session->my_ok();
553
478
    }
554
479
    break;
555
480
  }
556
 
  case COM_REGISTER_SLAVE:
557
 
  {
558
 
    if (!register_slave(thd, (uchar*)packet, packet_length))
559
 
      my_ok(thd);
560
 
    break;
561
 
  }
562
481
  case COM_CHANGE_USER:
563
482
  {
564
 
    status_var_increment(thd->status_var.com_other);
 
483
    status_var_increment(session->status_var.com_other);
565
484
    char *user= (char*) packet, *packet_end= packet + packet_length;
566
485
    /* Safe because there is always a trailing \0 at the end of the packet */
567
 
    char *passwd= strend(user)+1;
568
 
 
569
 
 
570
 
    thd->clear_error();                         // if errors from rollback
 
486
    char *passwd= strchr(user, '\0')+1;
 
487
 
 
488
 
 
489
    session->clear_error();                         // if errors from rollback
571
490
 
572
491
    /*
573
492
      Old clients send null-terminated string ('\0' for empty string) for
575
494
      terminated, so also '\0' for empty string).
576
495
 
577
496
      Cast *passwd to an unsigned char, so that it doesn't extend the sign
578
 
      for *passwd > 127 and become 2**32-127 after casting to uint.
 
497
      for *passwd > 127 and become 2**32-127 after casting to uint32_t.
579
498
    */
580
499
    char db_buff[NAME_LEN+1];                 // buffer to store db in utf8
581
500
    char *db= passwd;
589
508
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
590
509
      break;
591
510
    }
592
 
    uint passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
593
 
                      (uchar)(*passwd++) : strlen(passwd));
594
 
    uint dummy_errors, save_db_length, db_length;
 
511
    uint32_t passwd_len= (session->client_capabilities & CLIENT_SECURE_CONNECTION ?
 
512
                      (unsigned char)(*passwd++) : strlen(passwd));
 
513
    uint32_t dummy_errors, save_db_length, db_length;
595
514
    int res;
596
 
    Security_context save_security_ctx= *thd->security_ctx;
597
515
    USER_CONN *save_user_connect;
 
516
    string old_username;
598
517
 
599
518
    db+= passwd_len + 1;
600
519
    /*
609
528
    db_length= strlen(db);
610
529
 
611
530
    char *ptr= db + db_length + 1;
612
 
    uint cs_number= 0;
 
531
    uint32_t cs_number= 0;
613
532
 
614
533
    if (ptr < packet_end)
615
534
    {
625
544
    /* Convert database name to utf8 */
626
545
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
627
546
                             system_charset_info, db, db_length,
628
 
                             thd->charset(), &dummy_errors)]= 0;
 
547
                             session->charset(), &dummy_errors)]= 0;
629
548
    db= db_buff;
630
549
 
631
550
    /* Save user and privileges */
632
 
    save_db_length= thd->db_length;
633
 
    save_db= thd->db;
634
 
    save_user_connect= thd->user_connect;
 
551
    save_db_length= session->db_length;
 
552
    save_db= session->db;
 
553
    save_user_connect= session->user_connect;
635
554
 
636
 
    if (!(thd->security_ctx->user= my_strdup(user, MYF(0))))
637
 
    {
638
 
      thd->security_ctx->user= save_security_ctx.user;
639
 
      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
640
 
      break;
641
 
    }
 
555
    old_username= session->security_ctx.user;
 
556
    session->security_ctx.user.assign(user);
642
557
 
643
558
    /* Clear variables that are allocated */
644
 
    thd->user_connect= 0;
645
 
    res= check_user(thd, COM_CHANGE_USER, passwd, passwd_len, db, false);
 
559
    session->user_connect= 0;
 
560
    res= check_user(session, passwd, passwd_len, db, false);
646
561
 
647
562
    if (res)
648
563
    {
649
 
      x_free(thd->security_ctx->user);
650
 
      *thd->security_ctx= save_security_ctx;
651
 
      thd->user_connect= save_user_connect;
652
 
      thd->db= save_db;
653
 
      thd->db_length= save_db_length;
 
564
      session->security_ctx.user= old_username;
 
565
      session->user_connect= save_user_connect;
 
566
      session->db= save_db;
 
567
      session->db_length= save_db_length;
654
568
    }
655
569
    else
656
570
    {
657
 
      x_free(save_db);
658
 
      x_free(save_security_ctx.user);
 
571
      if (save_db)
 
572
        free(save_db);
659
573
 
660
574
      if (cs_number)
661
575
      {
662
 
        thd_init_client_charset(thd, cs_number);
663
 
        thd->update_charset();
 
576
        session->update_charset();
664
577
      }
665
578
    }
666
579
    break;
667
580
  }
668
581
  case COM_QUERY:
669
582
  {
670
 
    if (alloc_query(thd, packet, packet_length))
 
583
    if (alloc_query(session, packet, packet_length))
671
584
      break;                                    // fatal error is set
672
 
    char *packet_end= thd->query + thd->query_length;
 
585
    char *packet_end= session->query + session->query_length;
673
586
    const char* end_of_stmt= NULL;
674
587
 
675
 
    general_log_write(thd, command, thd->query, thd->query_length);
676
 
 
677
 
    mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
678
 
 
679
 
    while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
 
588
    mysql_parse(session, session->query, session->query_length, &end_of_stmt);
 
589
 
 
590
    while (!session->killed && (end_of_stmt != NULL) && ! session->is_error())
680
591
    {
681
592
      char *beginning_of_next_stmt= (char*) end_of_stmt;
682
593
 
683
 
      net_end_statement(thd);
 
594
      net_end_statement(session);
684
595
      /*
685
596
        Multiple queries exits, execute them individually
686
597
      */
687
 
      close_thread_tables(thd);
 
598
      close_thread_tables(session);
688
599
      ulong length= (ulong)(packet_end - beginning_of_next_stmt);
689
600
 
690
 
      log_slow_statement(thd);
 
601
      log_slow_statement(session);
691
602
 
692
603
      /* Remove garbage at start of query */
693
 
      while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
 
604
      while (length > 0 && my_isspace(session->charset(), *beginning_of_next_stmt))
694
605
      {
695
606
        beginning_of_next_stmt++;
696
607
        length--;
697
608
      }
698
609
 
699
 
      VOID(pthread_mutex_lock(&LOCK_thread_count));
700
 
      thd->query_length= length;
701
 
      thd->query= beginning_of_next_stmt;
 
610
      pthread_mutex_lock(&LOCK_thread_count);
 
611
      session->query_length= length;
 
612
      session->query= beginning_of_next_stmt;
702
613
      /*
703
614
        Count each statement from the client.
704
615
      */
705
 
      statistic_increment(thd->status_var.questions, &LOCK_status);
706
 
      thd->query_id= next_query_id();
707
 
      thd->set_time(); /* Reset the query start time. */
708
 
      /* TODO: set thd->lex->sql_command to SQLCOM_END here */
709
 
      VOID(pthread_mutex_unlock(&LOCK_thread_count));
 
616
      statistic_increment(session->status_var.questions, &LOCK_status);
 
617
      session->query_id= query_id.next();
 
618
      session->set_time(); /* Reset the query start time. */
 
619
      /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
620
      pthread_mutex_unlock(&LOCK_thread_count);
710
621
 
711
 
      mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
 
622
      mysql_parse(session, beginning_of_next_stmt, length, &end_of_stmt);
712
623
    }
713
624
    break;
714
625
  }
716
627
  {
717
628
    char *fields, *packet_end= packet + packet_length, *arg_end;
718
629
    /* Locked closure of all tables */
719
 
    TABLE_LIST table_list;
 
630
    TableList table_list;
720
631
    LEX_STRING conv_name;
721
632
 
722
633
    /* used as fields initializator */
723
 
    lex_start(thd);
 
634
    lex_start(session);
724
635
 
725
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
 
636
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
726
637
    memset(&table_list, 0, sizeof(table_list));
727
 
    if (thd->copy_db_to(&table_list.db, &table_list.db_length))
 
638
    if (session->copy_db_to(&table_list.db, &table_list.db_length))
728
639
      break;
729
640
    /*
730
641
      We have name + wildcard in packet, separated by endzero
731
642
    */
732
 
    arg_end= strend(packet);
733
 
    thd->convert_string(&conv_name, system_charset_info,
734
 
                        packet, (uint) (arg_end - packet), thd->charset());
 
643
    arg_end= strchr(packet, '\0');
 
644
    session->convert_string(&conv_name, system_charset_info,
 
645
                        packet, (uint32_t) (arg_end - packet), session->charset());
735
646
    table_list.alias= table_list.table_name= conv_name.str;
736
647
    packet= arg_end + 1;
737
648
 
738
649
    if (!my_strcasecmp(system_charset_info, table_list.db,
739
 
                       INFORMATION_SCHEMA_NAME.str))
 
650
                       INFORMATION_SCHEMA_NAME.c_str()))
740
651
    {
741
 
      ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
 
652
      ST_SCHEMA_TABLE *schema_table= find_schema_table(session, table_list.alias);
742
653
      if (schema_table)
743
654
        table_list.schema_table= schema_table;
744
655
    }
745
656
 
746
 
    thd->query_length= (uint) (packet_end - packet); // Don't count end \0
747
 
    if (!(thd->query=fields= (char*) thd->memdup(packet,thd->query_length+1)))
 
657
    session->query_length= (uint32_t) (packet_end - packet); // Don't count end \0
 
658
    if (!(session->query=fields= (char*) session->memdup(packet,session->query_length+1)))
748
659
      break;
749
 
    general_log_print(thd, command, "%s %s", table_list.table_name, fields);
750
660
    if (lower_case_table_names)
751
661
      my_casedn_str(files_charset_info, table_list.table_name);
752
662
 
753
663
    /* init structures for VIEW processing */
754
 
    table_list.select_lex= &(thd->lex->select_lex);
755
 
 
756
 
    lex_start(thd);
757
 
    mysql_reset_thd_for_next_command(thd);
758
 
 
759
 
    thd->lex->
760
 
      select_lex.table_list.link_in_list((uchar*) &table_list,
761
 
                                         (uchar**) &table_list.next_local);
762
 
    thd->lex->add_to_query_tables(&table_list);
 
664
    table_list.select_lex= &(session->lex->select_lex);
 
665
 
 
666
    lex_start(session);
 
667
    session->reset_for_next_command();
 
668
 
 
669
    session->lex->
 
670
      select_lex.table_list.link_in_list((unsigned char*) &table_list,
 
671
                                         (unsigned char**) &table_list.next_local);
 
672
    session->lex->add_to_query_tables(&table_list);
763
673
 
764
674
    /* switch on VIEW optimisation: do not fill temporary tables */
765
 
    thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
766
 
    mysqld_list_fields(thd,&table_list,fields);
767
 
    thd->lex->unit.cleanup();
768
 
    thd->cleanup_after_query();
 
675
    session->lex->sql_command= SQLCOM_SHOW_FIELDS;
 
676
    mysqld_list_fields(session,&table_list,fields);
 
677
    session->lex->unit.cleanup();
 
678
    session->cleanup_after_query();
769
679
    break;
770
680
  }
771
681
  case COM_QUIT:
772
682
    /* We don't calculate statistics for this command */
773
 
    general_log_print(thd, command, NullS);
774
683
    net->error=0;                               // Don't give 'abort' message
775
 
    thd->main_da.disable_status();              // Don't send anything back
 
684
    session->main_da.disable_status();              // Don't send anything back
776
685
    error=true;                                 // End server
777
686
    break;
778
 
  case COM_BINLOG_DUMP:
779
 
    {
780
 
      ulong pos;
781
 
      ushort flags;
782
 
      uint32_t slave_server_id;
783
 
 
784
 
      status_var_increment(thd->status_var.com_other);
785
 
      thd->enable_slow_log= opt_log_slow_admin_statements;
786
 
      /* TODO: The following has to be changed to an 8 byte integer */
787
 
      pos = uint4korr(packet);
788
 
      flags = uint2korr(packet + 4);
789
 
      thd->server_id=0; /* avoid suicide */
790
 
      if ((slave_server_id= uint4korr(packet+6))) // mysqlbinlog.server_id==0
791
 
        kill_zombie_dump_threads(slave_server_id);
792
 
      thd->server_id = slave_server_id;
793
 
 
794
 
      general_log_print(thd, command, "Log: '%s'  Pos: %ld", packet+10,
795
 
                      (long) pos);
796
 
      mysql_binlog_send(thd, thd->strdup(packet + 10), (my_off_t) pos, flags);
797
 
      unregister_slave(thd,1,1);
798
 
      /*  fake COM_QUIT -- if we get here, the thread needs to terminate */
799
 
      error = true;
800
 
      break;
801
 
    }
802
687
  case COM_SHUTDOWN:
803
688
  {
804
 
    status_var_increment(thd->status_var.com_other);
805
 
    /*
806
 
      If the client is < 4.1.3, it is going to send us no argument; then
807
 
      packet_length is 0, packet[0] is the end 0 of the packet. Note that
808
 
      SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
809
 
      packet[0].
810
 
    */
811
 
    enum drizzle_enum_shutdown_level level=
812
 
      (enum drizzle_enum_shutdown_level) (uchar) packet[0];
813
 
    if (level == SHUTDOWN_DEFAULT)
814
 
      level= SHUTDOWN_WAIT_ALL_BUFFERS; // soon default will be configurable
815
 
    else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
816
 
    {
817
 
      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "this shutdown level");
818
 
      break;
819
 
    }
820
 
    general_log_print(thd, command, NullS);
821
 
    my_eof(thd);
822
 
    close_thread_tables(thd);                   // Free before kill
823
 
    kill_mysql();
 
689
    status_var_increment(session->status_var.com_other);
 
690
    session->my_eof();
 
691
    close_thread_tables(session);                       // Free before kill
 
692
    kill_drizzle();
824
693
    error=true;
825
694
    break;
826
695
  }
827
696
  case COM_PING:
828
 
    status_var_increment(thd->status_var.com_other);
829
 
    my_ok(thd);                         // Tell client we are alive
 
697
    status_var_increment(session->status_var.com_other);
 
698
    session->my_ok();                           // Tell client we are alive
830
699
    break;
831
700
  case COM_PROCESS_INFO:
832
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
833
 
    general_log_print(thd, command, NullS);
834
 
    mysqld_list_processes(thd, NullS, 0);
 
701
    status_var_increment(session->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
 
702
    mysqld_list_processes(session, NULL, 0);
835
703
    break;
836
704
  case COM_PROCESS_KILL:
837
705
  {
838
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
 
706
    status_var_increment(session->status_var.com_stat[SQLCOM_KILL]);
839
707
    ulong id=(ulong) uint4korr(packet);
840
 
    sql_kill(thd,id,false);
 
708
    sql_kill(session,id,false);
841
709
    break;
842
710
  }
843
711
  case COM_SET_OPTION:
844
712
  {
845
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
846
 
    uint opt_command= uint2korr(packet);
 
713
    status_var_increment(session->status_var.com_stat[SQLCOM_SET_OPTION]);
 
714
    uint32_t opt_command= uint2korr(packet);
847
715
 
848
716
    switch (opt_command) {
849
717
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
850
 
      thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
851
 
      my_eof(thd);
 
718
      session->client_capabilities|= CLIENT_MULTI_STATEMENTS;
 
719
      session->my_eof();
852
720
      break;
853
721
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
854
 
      thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
855
 
      my_eof(thd);
 
722
      session->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
 
723
      session->my_eof();
856
724
      break;
857
725
    default:
858
726
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
870
738
  }
871
739
 
872
740
  /* If commit fails, we should be able to reset the OK status. */
873
 
  thd->main_da.can_overwrite_status= true;
874
 
  ha_autocommit_or_rollback(thd, thd->is_error());
875
 
  thd->main_da.can_overwrite_status= false;
 
741
  session->main_da.can_overwrite_status= true;
 
742
  ha_autocommit_or_rollback(session, session->is_error());
 
743
  session->main_da.can_overwrite_status= false;
876
744
 
877
 
  thd->transaction.stmt.reset();
 
745
  session->transaction.stmt.reset();
878
746
 
879
747
 
880
748
  /* report error issued during command execution */
881
 
  if (thd->killed_errno())
882
 
  {
883
 
    if (! thd->main_da.is_set())
884
 
      thd->send_kill_message();
885
 
  }
886
 
  if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
887
 
  {
888
 
    thd->killed= THD::NOT_KILLED;
889
 
    thd->mysys_var->abort= 0;
890
 
  }
891
 
 
892
 
  net_end_statement(thd);
893
 
 
894
 
  thd->proc_info= "closing tables";
 
749
  if (session->killed_errno())
 
750
  {
 
751
    if (! session->main_da.is_set())
 
752
      session->send_kill_message();
 
753
  }
 
754
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
 
755
  {
 
756
    session->killed= Session::NOT_KILLED;
 
757
    session->mysys_var->abort= 0;
 
758
  }
 
759
 
 
760
  net_end_statement(session);
 
761
 
 
762
  session->set_proc_info("closing tables");
895
763
  /* Free tables */
896
 
  close_thread_tables(thd);
897
 
 
898
 
  log_slow_statement(thd);
899
 
 
900
 
  thd_proc_info(thd, "cleaning up");
901
 
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For process list
902
 
  thd_proc_info(thd, 0);
903
 
  thd->command=COM_SLEEP;
904
 
  thd->query=0;
905
 
  thd->query_length=0;
 
764
  close_thread_tables(session);
 
765
 
 
766
  log_slow_statement(session);
 
767
 
 
768
  session->set_proc_info("cleaning up");
 
769
  pthread_mutex_lock(&LOCK_thread_count); // For process list
 
770
  session->set_proc_info(0);
 
771
  session->command=COM_SLEEP;
 
772
  session->query=0;
 
773
  session->query_length=0;
906
774
  thread_running--;
907
 
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
908
 
  thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
909
 
  free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
 
775
  pthread_mutex_unlock(&LOCK_thread_count);
 
776
  session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
 
777
  free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
910
778
  return(error);
911
779
}
912
780
 
913
781
 
914
 
void log_slow_statement(THD *thd)
 
782
void log_slow_statement(Session *session)
915
783
{
916
 
  /*
917
 
    The following should never be true with our current code base,
918
 
    but better to keep this here so we don't accidently try to log a
919
 
    statement in a trigger or stored function
920
 
  */
921
 
  if (unlikely(thd->in_sub_stmt))
922
 
    return;                           // Don't set time for sub stmt
923
 
 
924
 
  /*
925
 
    Do not log administrative statements unless the appropriate option is
926
 
    set; do not log into slow log if reading from backup.
927
 
  */
928
 
  if (thd->enable_slow_log && !thd->user_time)
929
 
  {
930
 
    thd_proc_info(thd, "logging slow query");
931
 
    uint64_t end_utime_of_query= thd->current_utime();
932
 
 
933
 
    if (((end_utime_of_query - thd->utime_after_lock) >
934
 
         thd->variables.long_query_time ||
935
 
         ((thd->server_status &
936
 
           (SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
937
 
          opt_log_queries_not_using_indexes &&
938
 
           !(sql_command_flags[thd->lex->sql_command] & CF_STATUS_COMMAND))) &&
939
 
        thd->examined_row_count >= thd->variables.min_examined_row_limit)
940
 
    {
941
 
      thd_proc_info(thd, "logging slow query");
942
 
      thd->status_var.long_query_count++;
943
 
      slow_log_print(thd, thd->query, thd->query_length, end_utime_of_query);
944
 
    }
945
 
  }
 
784
  logging_post_do(session);
 
785
 
946
786
  return;
947
787
}
948
788
 
949
789
 
950
790
/**
951
 
  Create a TABLE_LIST object for an INFORMATION_SCHEMA table.
 
791
  Create a TableList object for an INFORMATION_SCHEMA table.
952
792
 
953
793
    This function is used in the parser to convert a SHOW or DESCRIBE
954
794
    table_name command to a SELECT from INFORMATION_SCHEMA.
955
 
    It prepares a SELECT_LEX and a TABLE_LIST object to represent the
 
795
    It prepares a SELECT_LEX and a TableList object to represent the
956
796
    given command as a SELECT parse tree.
957
797
 
958
 
  @param thd              thread handle
 
798
  @param session              thread handle
959
799
  @param lex              current lex
960
800
  @param table_ident      table alias if it's used
961
801
  @param schema_table_idx the type of the INFORMATION_SCHEMA table to be
973
813
                      in this version of the server.
974
814
*/
975
815
 
976
 
int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
 
816
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
977
817
                         enum enum_schema_tables schema_table_idx)
978
818
{
979
819
  SELECT_LEX *schema_select_lex= NULL;
1007
847
  case SCH_STATISTICS:
1008
848
  {
1009
849
    assert(table_ident);
1010
 
    TABLE_LIST **query_tables_last= lex->query_tables_last;
 
850
    TableList **query_tables_last= lex->query_tables_last;
1011
851
    schema_select_lex= new SELECT_LEX();
1012
852
    /* 'parent_lex' is used in init_query() so it must be before it. */
1013
853
    schema_select_lex->parent_lex= lex;
1014
854
    schema_select_lex->init_query();
1015
 
    if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
 
855
    if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
1016
856
      return(1);
1017
857
    lex->query_tables_last= query_tables_last;
1018
858
    break;
1028
868
  default:
1029
869
    break;
1030
870
  }
1031
 
  
 
871
 
1032
872
  SELECT_LEX *select_lex= lex->current_select;
1033
873
  assert(select_lex);
1034
 
  if (make_schema_select(thd, select_lex, schema_table_idx))
 
874
  if (make_schema_select(session, select_lex, schema_table_idx))
1035
875
  {
1036
876
    return(1);
1037
877
  }
1038
 
  TABLE_LIST *table_list= (TABLE_LIST*) select_lex->table_list.first;
 
878
  TableList *table_list= (TableList*) select_lex->table_list.first;
1039
879
  assert(table_list);
1040
880
  table_list->schema_select_lex= schema_select_lex;
1041
881
  table_list->schema_table_reformed= 1;
1044
884
 
1045
885
 
1046
886
/**
1047
 
  Read query from packet and store in thd->query.
 
887
  Read query from packet and store in session->query.
1048
888
  Used in COM_QUERY and COM_STMT_PREPARE.
1049
889
 
1050
 
    Sets the following THD variables:
 
890
    Sets the following Session variables:
1051
891
  - query
1052
892
  - query_length
1053
893
 
1054
894
  @retval
1055
895
    false ok
1056
896
  @retval
1057
 
    true  error;  In this case thd->fatal_error is set
 
897
    true  error;  In this case session->fatal_error is set
1058
898
*/
1059
899
 
1060
 
bool alloc_query(THD *thd, const char *packet, uint packet_length)
 
900
bool alloc_query(Session *session, const char *packet, uint32_t packet_length)
1061
901
{
1062
902
  /* Remove garbage at start and end of query */
1063
 
  while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
 
903
  while (packet_length > 0 && my_isspace(session->charset(), packet[0]))
1064
904
  {
1065
905
    packet++;
1066
906
    packet_length--;
1067
907
  }
1068
908
  const char *pos= packet + packet_length;     // Point at end null
1069
909
  while (packet_length > 0 &&
1070
 
         (pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
 
910
         (pos[-1] == ';' || my_isspace(session->charset() ,pos[-1])))
1071
911
  {
1072
912
    pos--;
1073
913
    packet_length--;
1074
914
  }
1075
915
  /* We must allocate some extra memory for query cache */
1076
 
  thd->query_length= 0;                        // Extra safety: Avoid races
1077
 
  if (!(thd->query= (char*) thd->memdup_w_gap((uchar*) (packet),
 
916
  session->query_length= 0;                        // Extra safety: Avoid races
 
917
  if (!(session->query= (char*) session->memdup_w_gap((unsigned char*) (packet),
1078
918
                                              packet_length,
1079
 
                                              thd->db_length+ 1)))
 
919
                                              session->db_length+ 1)))
1080
920
    return true;
1081
 
  thd->query[packet_length]=0;
1082
 
  thd->query_length= packet_length;
 
921
  session->query[packet_length]=0;
 
922
  session->query_length= packet_length;
1083
923
 
1084
924
  /* Reclaim some memory */
1085
 
  thd->packet.shrink(thd->variables.net_buffer_length);
1086
 
  thd->convert_buffer.shrink(thd->variables.net_buffer_length);
 
925
  session->packet.shrink(session->variables.net_buffer_length);
 
926
  session->convert_buffer.shrink(session->variables.net_buffer_length);
1087
927
 
1088
928
  return false;
1089
929
}
1090
930
 
1091
 
static void reset_one_shot_variables(THD *thd) 
1092
 
{
1093
 
  thd->variables.character_set_client=
1094
 
    global_system_variables.character_set_client;
1095
 
  thd->variables.collation_connection=
1096
 
    global_system_variables.collation_connection;
1097
 
  thd->variables.collation_database=
1098
 
    global_system_variables.collation_database;
1099
 
  thd->variables.collation_server=
1100
 
    global_system_variables.collation_server;
1101
 
  thd->update_charset();
1102
 
  thd->variables.time_zone=
1103
 
    global_system_variables.time_zone;
1104
 
  thd->variables.lc_time_names= &my_locale_en_US;
1105
 
  thd->one_shot_set= 0;
1106
 
}
1107
 
 
1108
 
 
1109
931
/**
1110
 
  Execute command saved in thd and lex->sql_command.
 
932
  Execute command saved in session and lex->sql_command.
1111
933
 
1112
934
    Before every operation that can request a write lock for a table
1113
935
    wait if a global read lock exists. However do not wait if this
1119
941
    global read lock when it succeeds. This needs to be released by
1120
942
    start_waiting_global_read_lock() after the operation.
1121
943
 
1122
 
  @param thd                       Thread handle
 
944
  @param session                       Thread handle
1123
945
 
1124
946
  @todo
1125
947
    - Invalidate the table in the query cache if something changed
1137
959
*/
1138
960
 
1139
961
int
1140
 
mysql_execute_command(THD *thd)
 
962
mysql_execute_command(Session *session)
1141
963
{
1142
964
  int res= false;
1143
965
  bool need_start_waiting= false; // have protection against global read lock
1144
 
  int  up_result= 0;
1145
 
  LEX  *lex= thd->lex;
 
966
  LEX  *lex= session->lex;
1146
967
  /* first SELECT_LEX (have special meaning for many of non-SELECTcommands) */
1147
968
  SELECT_LEX *select_lex= &lex->select_lex;
1148
969
  /* first table of first SELECT_LEX */
1149
 
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex->table_list.first;
 
970
  TableList *first_table= (TableList*) select_lex->table_list.first;
1150
971
  /* list of all tables in query */
1151
 
  TABLE_LIST *all_tables;
 
972
  TableList *all_tables;
1152
973
  /* most outer SELECT_LEX_UNIT of query */
1153
974
  SELECT_LEX_UNIT *unit= &lex->unit;
1154
975
  /* Saved variable value */
1155
976
 
1156
977
  /*
1157
978
    In many cases first table of main SELECT_LEX have special meaning =>
1158
 
    check that it is first table in global list and relink it first in 
 
979
    check that it is first table in global list and relink it first in
1159
980
    queries_tables list if it is necessary (we need such relinking only
1160
981
    for queries with subqueries in select list, in this case tables of
1161
982
    subqueries will go to global list first)
1173
994
  all_tables= lex->query_tables;
1174
995
  /* set context for commands which do not use setup_tables */
1175
996
  select_lex->
1176
 
    context.resolve_in_table_list_only((TABLE_LIST*)select_lex->
 
997
    context.resolve_in_table_list_only((TableList*)select_lex->
1177
998
                                       table_list.first);
1178
999
 
1179
1000
  /*
1184
1005
    Don't reset warnings when executing a stored routine.
1185
1006
  */
1186
1007
  if (all_tables || !lex->is_single_level_stmt())
1187
 
    drizzle_reset_errors(thd, 0);
1188
 
 
1189
 
  if (unlikely(thd->slave_thread))
1190
 
  {
1191
 
    /*
1192
 
      Check if statment should be skipped because of slave filtering
1193
 
      rules
1194
 
 
1195
 
      Exceptions are:
1196
 
      - UPDATE MULTI: For this statement, we want to check the filtering
1197
 
        rules later in the code
1198
 
      - SET: we always execute it (Not that many SET commands exists in
1199
 
        the binary log anyway -- only 4.1 masters write SET statements,
1200
 
        in 5.0 there are no SET statements in the binary log)
1201
 
      - DROP TEMPORARY TABLE IF EXISTS: we always execute it (otherwise we
1202
 
        have stale files on slave caused by exclusion of one tmp table).
1203
 
    */
1204
 
    if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
1205
 
        !(lex->sql_command == SQLCOM_SET_OPTION) &&
1206
 
        !(lex->sql_command == SQLCOM_DROP_TABLE &&
1207
 
          lex->drop_temporary && lex->drop_if_exists) &&
1208
 
        all_tables_not_ok(thd, all_tables))
1209
 
    {
1210
 
      /* we warn the slave SQL thread */
1211
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
1212
 
      if (thd->one_shot_set)
1213
 
      {
1214
 
        /*
1215
 
          It's ok to check thd->one_shot_set here:
1216
 
 
1217
 
          The charsets in a MySQL 5.0 slave can change by both a binlogged
1218
 
          SET ONE_SHOT statement and the event-internal charset setting, 
1219
 
          and these two ways to change charsets do not seems to work
1220
 
          together.
1221
 
 
1222
 
          At least there seems to be problems in the rli cache for
1223
 
          charsets if we are using ONE_SHOT.  Note that this is normally no
1224
 
          problem because either the >= 5.0 slave reads a 4.1 binlog (with
1225
 
          ONE_SHOT) *or* or 5.0 binlog (without ONE_SHOT) but never both."
1226
 
        */
1227
 
        reset_one_shot_variables(thd);
1228
 
      }
1229
 
      return(0);
1230
 
    }
1231
 
  }
1232
 
  else
1233
 
  {
1234
 
    /*
1235
 
      When option readonly is set deny operations which change non-temporary
1236
 
      tables. Except for the replication thread and the 'super' users.
1237
 
    */
1238
 
    if (deny_updates_if_read_only_option(thd, all_tables))
1239
 
    {
1240
 
      my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1241
 
      return(-1);
1242
 
    }
1243
 
  } /* endif unlikely slave */
1244
 
  status_var_increment(thd->status_var.com_stat[lex->sql_command]);
1245
 
 
1246
 
  assert(thd->transaction.stmt.modified_non_trans_table == false);
1247
 
  
 
1008
    drizzle_reset_errors(session, 0);
 
1009
 
 
1010
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
1011
 
 
1012
  assert(session->transaction.stmt.modified_non_trans_table == false);
 
1013
 
1248
1014
  switch (lex->sql_command) {
1249
1015
  case SQLCOM_SHOW_STATUS:
1250
1016
  {
1251
 
    system_status_var old_status_var= thd->status_var;
1252
 
    thd->initial_status_var= &old_status_var;
1253
 
    res= execute_sqlcom_select(thd, all_tables);
 
1017
    system_status_var old_status_var= session->status_var;
 
1018
    session->initial_status_var= &old_status_var;
 
1019
    res= execute_sqlcom_select(session, all_tables);
1254
1020
    /* Don't log SHOW STATUS commands to slow query log */
1255
 
    thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
 
1021
    session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1256
1022
                           SERVER_QUERY_NO_GOOD_INDEX_USED);
1257
1023
    /*
1258
1024
      restore status variables, as we don't want 'show status' to cause
1259
1025
      changes
1260
1026
    */
1261
1027
    pthread_mutex_lock(&LOCK_status);
1262
 
    add_diff_to_status(&global_status_var, &thd->status_var,
 
1028
    add_diff_to_status(&global_status_var, &session->status_var,
1263
1029
                       &old_status_var);
1264
 
    thd->status_var= old_status_var;
 
1030
    session->status_var= old_status_var;
1265
1031
    pthread_mutex_unlock(&LOCK_status);
1266
1032
    break;
1267
1033
  }
1272
1038
  case SQLCOM_SHOW_FIELDS:
1273
1039
  case SQLCOM_SHOW_KEYS:
1274
1040
  case SQLCOM_SHOW_VARIABLES:
1275
 
  case SQLCOM_SHOW_CHARSETS:
1276
 
  case SQLCOM_SHOW_COLLATIONS:
1277
1041
  case SQLCOM_SELECT:
1278
1042
  {
1279
 
    thd->status_var.last_query_cost= 0.0;
1280
 
    res= execute_sqlcom_select(thd, all_tables);
 
1043
    session->status_var.last_query_cost= 0.0;
 
1044
    res= execute_sqlcom_select(session, all_tables);
1281
1045
    break;
1282
1046
  }
1283
1047
  case SQLCOM_EMPTY_QUERY:
1284
 
    my_ok(thd);
1285
 
    break;
1286
 
 
1287
 
  case SQLCOM_PURGE:
1288
 
  {
1289
 
    res = purge_master_logs(thd, lex->to_log);
1290
 
    break;
1291
 
  }
1292
 
  case SQLCOM_PURGE_BEFORE:
1293
 
  {
1294
 
    Item *it;
1295
 
 
1296
 
    /* PURGE MASTER LOGS BEFORE 'data' */
1297
 
    it= (Item *)lex->value_list.head();
1298
 
    if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
1299
 
        it->check_cols(1))
1300
 
    {
1301
 
      my_error(ER_WRONG_ARGUMENTS, MYF(0), "PURGE LOGS BEFORE");
1302
 
      goto error;
1303
 
    }
1304
 
    it= new Item_func_unix_timestamp(it);
1305
 
    /*
1306
 
      it is OK only emulate fix_fieds, because we need only
1307
 
      value of constant
1308
 
    */
1309
 
    it->quick_fix_field();
1310
 
    res = purge_master_logs_before_date(thd, (ulong)it->val_int());
1311
 
    break;
1312
 
  }
 
1048
    session->my_ok();
 
1049
    break;
 
1050
 
1313
1051
  case SQLCOM_SHOW_WARNS:
1314
1052
  {
1315
 
    res= mysqld_show_warnings(thd, (ulong)
1316
 
                              ((1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1317
 
                               (1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1318
 
                               (1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
 
1053
    res= mysqld_show_warnings(session, (uint32_t)
 
1054
                              ((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
 
1055
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
 
1056
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1319
1057
                               ));
1320
1058
    break;
1321
1059
  }
1322
1060
  case SQLCOM_SHOW_ERRORS:
1323
1061
  {
1324
 
    res= mysqld_show_warnings(thd, (ulong)
1325
 
                              (1L << (uint) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1326
 
    break;
1327
 
  }
1328
 
  case SQLCOM_SHOW_SLAVE_HOSTS:
1329
 
  {
1330
 
    res = show_slave_hosts(thd);
1331
 
    break;
1332
 
  }
1333
 
  case SQLCOM_SHOW_BINLOG_EVENTS:
1334
 
  {
1335
 
    res = mysql_show_binlog_events(thd);
1336
 
    break;
1337
 
  }
1338
 
 
 
1062
    res= mysqld_show_warnings(session, (uint32_t)
 
1063
                              (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
 
1064
    break;
 
1065
  }
1339
1066
  case SQLCOM_ASSIGN_TO_KEYCACHE:
1340
1067
  {
1341
1068
    assert(first_table == all_tables && first_table != 0);
1342
 
    res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
1343
 
    break;
1344
 
  }
1345
 
  case SQLCOM_CHANGE_MASTER:
1346
 
  {
1347
 
    pthread_mutex_lock(&LOCK_active_mi);
1348
 
    res = change_master(thd,active_mi);
1349
 
    pthread_mutex_unlock(&LOCK_active_mi);
1350
 
    break;
1351
 
  }
1352
 
  case SQLCOM_SHOW_SLAVE_STAT:
1353
 
  {
1354
 
    pthread_mutex_lock(&LOCK_active_mi);
1355
 
    if (active_mi != NULL)
1356
 
    {
1357
 
      res = show_master_info(thd, active_mi);
1358
 
    }
1359
 
    else
1360
 
    {
1361
 
      push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1362
 
                   "the master info structure does not exist");
1363
 
      my_ok(thd);
1364
 
    }
1365
 
    pthread_mutex_unlock(&LOCK_active_mi);
1366
 
    break;
1367
 
  }
1368
 
  case SQLCOM_SHOW_MASTER_STAT:
1369
 
  {
1370
 
    res = show_binlog_info(thd);
1371
 
    break;
1372
 
  }
1373
 
 
 
1069
    res= mysql_assign_to_keycache(session, first_table, &lex->ident);
 
1070
    break;
 
1071
  }
1374
1072
  case SQLCOM_SHOW_ENGINE_STATUS:
1375
1073
    {
1376
 
      res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
 
1074
      res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1377
1075
      break;
1378
1076
    }
1379
1077
  case SQLCOM_CREATE_TABLE:
1381
1079
    /* If CREATE TABLE of non-temporary table, do implicit commit */
1382
1080
    if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1383
1081
    {
1384
 
      if (end_active_trans(thd))
 
1082
      if (end_active_trans(session))
1385
1083
      {
1386
1084
        res= -1;
1387
1085
        break;
1390
1088
    assert(first_table == all_tables && first_table != 0);
1391
1089
    bool link_to_local;
1392
1090
    // Skip first table, which is the table we are creating
1393
 
    TABLE_LIST *create_table= lex->unlink_first_table(&link_to_local);
1394
 
    TABLE_LIST *select_tables= lex->query_tables;
 
1091
    TableList *create_table= lex->unlink_first_table(&link_to_local);
 
1092
    TableList *select_tables= lex->query_tables;
1395
1093
    /*
1396
1094
      Code below (especially in mysql_create_table() and select_create
1397
1095
      methods) may modify HA_CREATE_INFO structure in LEX, so we have to
1405
1103
      safety, only in case of Alter_info we have to do (almost) a deep
1406
1104
      copy.
1407
1105
    */
1408
 
    Alter_info alter_info(lex->alter_info, thd->mem_root);
 
1106
    Alter_info alter_info(lex->alter_info, session->mem_root);
1409
1107
 
1410
 
    if (thd->is_fatal_error)
 
1108
    if (session->is_fatal_error)
1411
1109
    {
1412
1110
      /* If out of memory when creating a copy of alter_info. */
1413
1111
      res= 1;
1414
1112
      goto end_with_restore_list;
1415
1113
    }
1416
1114
 
1417
 
    if ((res= create_table_precheck(thd, select_tables, create_table)))
 
1115
    if ((res= create_table_precheck(session, select_tables, create_table)))
1418
1116
      goto end_with_restore_list;
1419
1117
 
1420
1118
    /* Might have been updated in create_table_precheck */
1422
1120
 
1423
1121
#ifdef HAVE_READLINK
1424
1122
    /* Fix names if symlinked tables */
1425
 
    if (append_file_to_dir(thd, &create_info.data_file_name,
 
1123
    if (append_file_to_dir(session, &create_info.data_file_name,
1426
1124
                           create_table->table_name) ||
1427
 
        append_file_to_dir(thd, &create_info.index_file_name,
 
1125
        append_file_to_dir(session, &create_info.index_file_name,
1428
1126
                           create_table->table_name))
1429
1127
      goto end_with_restore_list;
1430
1128
#endif
1454
1152
      TABLE in the same way. That way we avoid that a new table is
1455
1153
      created during a gobal read lock.
1456
1154
    */
1457
 
    if (!thd->locked_tables &&
1458
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1155
    if (!session->locked_tables &&
 
1156
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1459
1157
    {
1460
1158
      res= 1;
1461
1159
      goto end_with_restore_list;
1473
1171
        create_table->create= true;
1474
1172
      }
1475
1173
 
1476
 
      if (!(res= open_and_lock_tables(thd, lex->query_tables)))
 
1174
      if (!(res= open_and_lock_tables(session, lex->query_tables)))
1477
1175
      {
1478
1176
        /*
1479
1177
          Is table which we are changing used somewhere in other parts
1481
1179
        */
1482
1180
        if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
1483
1181
        {
1484
 
          TABLE_LIST *duplicate;
 
1182
          TableList *duplicate;
1485
1183
          create_table= lex->unlink_first_table(&link_to_local);
1486
 
          if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
 
1184
          if ((duplicate= unique_table(session, create_table, select_tables, 0)))
1487
1185
          {
1488
1186
            update_non_unique_table_error(create_table, "CREATE", duplicate);
1489
1187
            res= 1;
1507
1205
            CREATE from SELECT give its SELECT_LEX for SELECT,
1508
1206
            and item_list belong to SELECT
1509
1207
          */
1510
 
          res= handle_select(thd, lex, result, 0);
 
1208
          res= handle_select(session, lex, result, 0);
1511
1209
          delete result;
1512
1210
        }
1513
1211
      }
1519
1217
    {
1520
1218
      /* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1521
1219
      if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1522
 
        thd->options|= OPTION_KEEP_LOG;
 
1220
        session->options|= OPTION_KEEP_LOG;
1523
1221
      /* regular create */
1524
1222
      if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1525
 
        res= mysql_create_like_table(thd, create_table, select_tables,
 
1223
        res= mysql_create_like_table(session, create_table, select_tables,
1526
1224
                                     &create_info);
1527
1225
      else
1528
1226
      {
1529
 
        res= mysql_create_table(thd, create_table->db,
 
1227
        res= mysql_create_table(session, create_table->db,
1530
1228
                                create_table->table_name, &create_info,
1531
1229
                                &alter_info, 0, 0);
1532
1230
      }
1533
1231
      if (!res)
1534
 
        my_ok(thd);
 
1232
        session->my_ok();
1535
1233
    }
1536
1234
 
1537
1235
    /* put tables back for PS rexecuting */
1553
1251
  {
1554
1252
    /* Prepare stack copies to be re-execution safe */
1555
1253
    HA_CREATE_INFO create_info;
1556
 
    Alter_info alter_info(lex->alter_info, thd->mem_root);
 
1254
    Alter_info alter_info(lex->alter_info, session->mem_root);
1557
1255
 
1558
 
    if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
 
1256
    if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1559
1257
      goto error;
1560
1258
 
1561
1259
    assert(first_table == all_tables && first_table != 0);
1562
 
    if (end_active_trans(thd))
 
1260
    if (end_active_trans(session))
1563
1261
      goto error;
1564
 
    /*
1565
 
      Currently CREATE INDEX or DROP INDEX cause a full table rebuild
1566
 
      and thus classify as slow administrative statements just like
1567
 
      ALTER TABLE.
1568
 
    */
1569
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1570
1262
 
1571
1263
    memset(&create_info, 0, sizeof(create_info));
1572
1264
    create_info.db_type= 0;
1573
1265
    create_info.row_type= ROW_TYPE_NOT_USED;
1574
 
    create_info.default_table_charset= thd->variables.collation_database;
 
1266
    create_info.default_table_charset= session->variables.collation_database;
1575
1267
 
1576
 
    res= mysql_alter_table(thd, first_table->db, first_table->table_name,
 
1268
    res= mysql_alter_table(session, first_table->db, first_table->table_name,
1577
1269
                           &create_info, first_table, &alter_info,
1578
 
                           0, (ORDER*) 0, 0);
1579
 
    break;
1580
 
  }
1581
 
  case SQLCOM_SLAVE_START:
1582
 
  {
1583
 
    pthread_mutex_lock(&LOCK_active_mi);
1584
 
    start_slave(thd,active_mi,1 /* net report*/);
1585
 
    pthread_mutex_unlock(&LOCK_active_mi);
1586
 
    break;
1587
 
  }
1588
 
  case SQLCOM_SLAVE_STOP:
1589
 
  /*
1590
 
    If the client thread has locked tables, a deadlock is possible.
1591
 
    Assume that
1592
 
    - the client thread does LOCK TABLE t READ.
1593
 
    - then the master updates t.
1594
 
    - then the SQL slave thread wants to update t,
1595
 
      so it waits for the client thread because t is locked by it.
1596
 
    - then the client thread does SLAVE STOP.
1597
 
      SLAVE STOP waits for the SQL slave thread to terminate its
1598
 
      update t, which waits for the client thread because t is locked by it.
1599
 
    To prevent that, refuse SLAVE STOP if the
1600
 
    client thread has locked tables
1601
 
  */
1602
 
  if (thd->locked_tables || thd->active_transaction() || thd->global_read_lock)
1603
 
  {
1604
 
    my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1605
 
               ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1606
 
    goto error;
1607
 
  }
1608
 
  {
1609
 
    pthread_mutex_lock(&LOCK_active_mi);
1610
 
    stop_slave(thd,active_mi,1/* net report*/);
1611
 
    pthread_mutex_unlock(&LOCK_active_mi);
1612
 
    break;
1613
 
  }
1614
 
 
 
1270
                           0, (order_st*) 0, 0);
 
1271
    break;
 
1272
  }
1615
1273
  case SQLCOM_ALTER_TABLE:
1616
1274
    assert(first_table == all_tables && first_table != 0);
1617
1275
    {
1622
1280
        referenced from this structure will be modified.
1623
1281
      */
1624
1282
      HA_CREATE_INFO create_info(lex->create_info);
1625
 
      Alter_info alter_info(lex->alter_info, thd->mem_root);
 
1283
      Alter_info alter_info(lex->alter_info, session->mem_root);
1626
1284
 
1627
 
      if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
 
1285
      if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1628
1286
      {
1629
1287
        goto error;
1630
1288
      }
1633
1291
      assert(select_lex->db);
1634
1292
 
1635
1293
      { // Rename of table
1636
 
          TABLE_LIST tmp_table;
 
1294
          TableList tmp_table;
1637
1295
          memset(&tmp_table, 0, sizeof(tmp_table));
1638
1296
          tmp_table.table_name= lex->name.str;
1639
1297
          tmp_table.db=select_lex->db;
1641
1299
 
1642
1300
      /* Don't yet allow changing of symlinks with ALTER TABLE */
1643
1301
      if (create_info.data_file_name)
1644
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1302
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1645
1303
                     "DATA DIRECTORY option ignored");
1646
1304
      if (create_info.index_file_name)
1647
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
1305
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1648
1306
                     "INDEX DIRECTORY option ignored");
1649
1307
      create_info.data_file_name= create_info.index_file_name= NULL;
1650
1308
      /* ALTER TABLE ends previous transaction */
1651
 
      if (end_active_trans(thd))
 
1309
      if (end_active_trans(session))
1652
1310
        goto error;
1653
1311
 
1654
 
      if (!thd->locked_tables &&
1655
 
          !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1312
      if (!session->locked_tables &&
 
1313
          !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1656
1314
      {
1657
1315
        res= 1;
1658
1316
        break;
1659
1317
      }
1660
1318
 
1661
 
      thd->enable_slow_log= opt_log_slow_admin_statements;
1662
 
      res= mysql_alter_table(thd, select_lex->db, lex->name.str,
 
1319
      res= mysql_alter_table(session, select_lex->db, lex->name.str,
1663
1320
                             &create_info,
1664
1321
                             first_table,
1665
1322
                             &alter_info,
1666
1323
                             select_lex->order_list.elements,
1667
 
                             (ORDER *) select_lex->order_list.first,
 
1324
                             (order_st *) select_lex->order_list.first,
1668
1325
                             lex->ignore);
1669
1326
      break;
1670
1327
    }
1671
1328
  case SQLCOM_RENAME_TABLE:
1672
1329
  {
1673
1330
    assert(first_table == all_tables && first_table != 0);
1674
 
    TABLE_LIST *table;
 
1331
    TableList *table;
1675
1332
    for (table= first_table; table; table= table->next_local->next_local)
1676
1333
    {
1677
 
      TABLE_LIST old_list, new_list;
 
1334
      TableList old_list, new_list;
1678
1335
      /*
1679
1336
        we do not need initialize old_list and new_list because we will
1680
1337
        come table[0] and table->next[0] there
1683
1340
      new_list= table->next_local[0];
1684
1341
    }
1685
1342
 
1686
 
    if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
 
1343
    if (end_active_trans(session) || drizzle_rename_tables(session, first_table, 0))
1687
1344
      {
1688
1345
        goto error;
1689
1346
      }
1690
1347
    break;
1691
1348
  }
1692
 
  case SQLCOM_SHOW_BINLOGS:
1693
 
    {
1694
 
      res = show_binlogs(thd);
1695
 
      break;
1696
 
    }
1697
1349
  case SQLCOM_SHOW_CREATE:
1698
1350
    assert(first_table == all_tables && first_table != 0);
1699
1351
    {
1700
 
      res= mysqld_show_create(thd, first_table);
 
1352
      res= mysqld_show_create(session, first_table);
1701
1353
      break;
1702
1354
    }
1703
1355
  case SQLCOM_CHECKSUM:
1704
1356
  {
1705
1357
    assert(first_table == all_tables && first_table != 0);
1706
 
    res = mysql_checksum_table(thd, first_table, &lex->check_opt);
 
1358
    res = mysql_checksum_table(session, first_table, &lex->check_opt);
1707
1359
    break;
1708
1360
  }
1709
1361
  case SQLCOM_REPAIR:
1710
1362
  {
1711
1363
    assert(first_table == all_tables && first_table != 0);
1712
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1713
 
    res= mysql_repair_table(thd, first_table, &lex->check_opt);
 
1364
    res= mysql_repair_table(session, first_table, &lex->check_opt);
1714
1365
    /* ! we write after unlocking the table */
1715
 
    if (!res && !lex->no_write_to_binlog)
1716
 
    {
1717
 
      /*
1718
 
        Presumably, REPAIR and binlog writing doesn't require synchronization
1719
 
      */
1720
 
      write_bin_log(thd, true, thd->query, thd->query_length);
1721
 
    }
1722
 
    select_lex->table_list.first= (uchar*) first_table;
 
1366
    /*
 
1367
      Presumably, REPAIR and binlog writing doesn't require synchronization
 
1368
    */
 
1369
    write_bin_log(session, true, session->query, session->query_length);
 
1370
    select_lex->table_list.first= (unsigned char*) first_table;
1723
1371
    lex->query_tables=all_tables;
1724
1372
    break;
1725
1373
  }
1726
1374
  case SQLCOM_CHECK:
1727
1375
  {
1728
1376
    assert(first_table == all_tables && first_table != 0);
1729
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1730
 
    res = mysql_check_table(thd, first_table, &lex->check_opt);
1731
 
    select_lex->table_list.first= (uchar*) first_table;
 
1377
    res = mysql_check_table(session, first_table, &lex->check_opt);
 
1378
    select_lex->table_list.first= (unsigned char*) first_table;
1732
1379
    lex->query_tables=all_tables;
1733
1380
    break;
1734
1381
  }
1735
1382
  case SQLCOM_ANALYZE:
1736
1383
  {
1737
1384
    assert(first_table == all_tables && first_table != 0);
1738
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1739
 
    res= mysql_analyze_table(thd, first_table, &lex->check_opt);
 
1385
    res= mysql_analyze_table(session, first_table, &lex->check_opt);
1740
1386
    /* ! we write after unlocking the table */
1741
 
    if (!res && !lex->no_write_to_binlog)
1742
 
    {
1743
 
      /*
1744
 
        Presumably, ANALYZE and binlog writing doesn't require synchronization
1745
 
      */
1746
 
      write_bin_log(thd, true, thd->query, thd->query_length);
1747
 
    }
1748
 
    select_lex->table_list.first= (uchar*) first_table;
 
1387
    write_bin_log(session, true, session->query, session->query_length);
 
1388
    select_lex->table_list.first= (unsigned char*) first_table;
1749
1389
    lex->query_tables=all_tables;
1750
1390
    break;
1751
1391
  }
1753
1393
  case SQLCOM_OPTIMIZE:
1754
1394
  {
1755
1395
    assert(first_table == all_tables && first_table != 0);
1756
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1757
 
    res= (specialflag & (SPECIAL_SAFE_MODE | SPECIAL_NO_NEW_FUNC)) ?
1758
 
      mysql_recreate_table(thd, first_table) :
1759
 
      mysql_optimize_table(thd, first_table, &lex->check_opt);
 
1396
    res= mysql_optimize_table(session, first_table, &lex->check_opt);
1760
1397
    /* ! we write after unlocking the table */
1761
 
    if (!res && !lex->no_write_to_binlog)
1762
 
    {
1763
 
      /*
1764
 
        Presumably, OPTIMIZE and binlog writing doesn't require synchronization
1765
 
      */
1766
 
      write_bin_log(thd, true, thd->query, thd->query_length);
1767
 
    }
1768
 
    select_lex->table_list.first= (uchar*) first_table;
 
1398
    write_bin_log(session, true, session->query, session->query_length);
 
1399
    select_lex->table_list.first= (unsigned char*) first_table;
1769
1400
    lex->query_tables=all_tables;
1770
1401
    break;
1771
1402
  }
1772
1403
  case SQLCOM_UPDATE:
1773
1404
    assert(first_table == all_tables && first_table != 0);
1774
 
    if (update_precheck(thd, all_tables))
 
1405
    if ((res= update_precheck(session, all_tables)))
1775
1406
      break;
1776
1407
    assert(select_lex->offset_limit == 0);
1777
1408
    unit->set_limit(select_lex);
1778
 
    res= (up_result= mysql_update(thd, all_tables,
1779
 
                                  select_lex->item_list,
1780
 
                                  lex->value_list,
1781
 
                                  select_lex->where,
1782
 
                                  select_lex->order_list.elements,
1783
 
                                  (ORDER *) select_lex->order_list.first,
1784
 
                                  unit->select_limit_cnt,
1785
 
                                  lex->duplicates, lex->ignore));
1786
 
    /* mysql_update return 2 if we need to switch to multi-update */
1787
 
    if (up_result != 2)
1788
 
      break;
1789
 
    /* Fall through */
 
1409
    res= mysql_update(session, all_tables,
 
1410
                      select_lex->item_list,
 
1411
                      lex->value_list,
 
1412
                      select_lex->where,
 
1413
                      select_lex->order_list.elements,
 
1414
                      (order_st *) select_lex->order_list.first,
 
1415
                      unit->select_limit_cnt,
 
1416
                      lex->duplicates, lex->ignore);
 
1417
    break;
1790
1418
  case SQLCOM_UPDATE_MULTI:
1791
1419
  {
1792
1420
    assert(first_table == all_tables && first_table != 0);
1793
 
    /* if we switched from normal update, rights are checked */
1794
 
    if (up_result != 2)
1795
 
    {
1796
 
      if ((res= multi_update_precheck(thd, all_tables)))
1797
 
        break;
1798
 
    }
1799
 
    else
1800
 
      res= 0;
1801
 
 
1802
 
    res= mysql_multi_update_prepare(thd);
1803
 
 
1804
 
    /* Check slave filtering rules */
1805
 
    if (unlikely(thd->slave_thread))
1806
 
    {
1807
 
      if (all_tables_not_ok(thd, all_tables))
1808
 
      {
1809
 
        if (res!= 0)
1810
 
        {
1811
 
          res= 0;             /* don't care of prev failure  */
1812
 
          thd->clear_error(); /* filters are of highest prior */
1813
 
        }
1814
 
        /* we warn the slave SQL thread */
1815
 
        my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
1816
 
        break;
1817
 
      }
1818
 
      if (res)
1819
 
        break;
1820
 
    }
1821
 
    else
1822
 
    {
1823
 
      if (res)
1824
 
        break;
1825
 
      if (opt_readonly &&
1826
 
          some_non_temp_table_to_be_updated(thd, all_tables))
1827
 
      {
1828
 
        my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--read-only");
1829
 
        break;
1830
 
      }
1831
 
    }  /* unlikely */
1832
 
 
1833
 
    res= mysql_multi_update(thd, all_tables,
 
1421
    if ((res= update_precheck(session, all_tables)))
 
1422
      break;
 
1423
 
 
1424
    if ((res= mysql_multi_update_prepare(session)))
 
1425
      break;
 
1426
 
 
1427
    res= mysql_multi_update(session, all_tables,
1834
1428
                            &select_lex->item_list,
1835
1429
                            &lex->value_list,
1836
1430
                            select_lex->where,
1842
1436
  case SQLCOM_INSERT:
1843
1437
  {
1844
1438
    assert(first_table == all_tables && first_table != 0);
1845
 
    if ((res= insert_precheck(thd, all_tables)))
 
1439
    if ((res= insert_precheck(session, all_tables)))
1846
1440
      break;
1847
1441
 
1848
 
    if (!thd->locked_tables &&
1849
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1442
    if (!session->locked_tables &&
 
1443
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1850
1444
    {
1851
1445
      res= 1;
1852
1446
      break;
1853
1447
    }
1854
1448
 
1855
 
    res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
 
1449
    res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1856
1450
                      lex->update_list, lex->value_list,
1857
1451
                      lex->duplicates, lex->ignore);
1858
1452
 
1863
1457
  {
1864
1458
    select_result *sel_result;
1865
1459
    assert(first_table == all_tables && first_table != 0);
1866
 
    if ((res= insert_precheck(thd, all_tables)))
 
1460
    if ((res= insert_precheck(session, all_tables)))
1867
1461
      break;
1868
1462
 
1869
1463
    /* Fix lock for first table */
1875
1469
 
1876
1470
    unit->set_limit(select_lex);
1877
1471
 
1878
 
    if (! thd->locked_tables &&
1879
 
        ! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
 
1472
    if (! session->locked_tables &&
 
1473
        ! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1880
1474
    {
1881
1475
      res= 1;
1882
1476
      break;
1883
1477
    }
1884
1478
 
1885
 
    if (!(res= open_and_lock_tables(thd, all_tables)))
 
1479
    if (!(res= open_and_lock_tables(session, all_tables)))
1886
1480
    {
1887
1481
      /* Skip first table, which is the table we are inserting in */
1888
 
      TABLE_LIST *second_table= first_table->next_local;
1889
 
      select_lex->table_list.first= (uchar*) second_table;
1890
 
      select_lex->context.table_list= 
 
1482
      TableList *second_table= first_table->next_local;
 
1483
      select_lex->table_list.first= (unsigned char*) second_table;
 
1484
      select_lex->context.table_list=
1891
1485
        select_lex->context.first_name_resolution_table= second_table;
1892
 
      res= mysql_insert_select_prepare(thd);
 
1486
      res= mysql_insert_select_prepare(session);
1893
1487
      if (!res && (sel_result= new select_insert(first_table,
1894
1488
                                                 first_table->table,
1895
1489
                                                 &lex->field_list,
1898
1492
                                                 lex->duplicates,
1899
1493
                                                 lex->ignore)))
1900
1494
      {
1901
 
        res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
 
1495
        res= handle_select(session, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1902
1496
        /*
1903
1497
          Invalidate the table in the query cache if something changed
1904
1498
          after unlocking when changes become visible.
1906
1500
          the unlock procedure.
1907
1501
        */
1908
1502
        if (first_table->lock_type ==  TL_WRITE_CONCURRENT_INSERT &&
1909
 
            thd->lock)
 
1503
            session->lock)
1910
1504
        {
1911
1505
          /* INSERT ... SELECT should invalidate only the very first table */
1912
 
          TABLE_LIST *save_table= first_table->next_local;
 
1506
          TableList *save_table= first_table->next_local;
1913
1507
          first_table->next_local= 0;
1914
1508
          first_table->next_local= save_table;
1915
1509
        }
1916
1510
        delete sel_result;
1917
1511
      }
1918
1512
      /* revert changes for SP */
1919
 
      select_lex->table_list.first= (uchar*) first_table;
 
1513
      select_lex->table_list.first= (unsigned char*) first_table;
1920
1514
    }
1921
1515
 
1922
1516
    break;
1923
1517
  }
1924
1518
  case SQLCOM_TRUNCATE:
1925
 
    if (end_active_trans(thd))
 
1519
    if (end_active_trans(session))
1926
1520
    {
1927
1521
      res= -1;
1928
1522
      break;
1932
1526
      Don't allow this within a transaction because we want to use
1933
1527
      re-generate table
1934
1528
    */
1935
 
    if (thd->locked_tables || thd->active_transaction())
 
1529
    if (session->locked_tables || session->active_transaction())
1936
1530
    {
1937
1531
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1938
1532
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1939
1533
      goto error;
1940
1534
    }
1941
1535
 
1942
 
    res= mysql_truncate(thd, first_table, 0);
 
1536
    res= mysql_truncate(session, first_table, 0);
1943
1537
 
1944
1538
    break;
1945
1539
  case SQLCOM_DELETE:
1948
1542
    assert(select_lex->offset_limit == 0);
1949
1543
    unit->set_limit(select_lex);
1950
1544
 
1951
 
    if (!thd->locked_tables &&
1952
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1545
    if (!session->locked_tables &&
 
1546
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1953
1547
    {
1954
1548
      res= 1;
1955
1549
      break;
1956
1550
    }
1957
1551
 
1958
 
    res = mysql_delete(thd, all_tables, select_lex->where,
 
1552
    res = mysql_delete(session, all_tables, select_lex->where,
1959
1553
                       &select_lex->order_list,
1960
1554
                       unit->select_limit_cnt, select_lex->options,
1961
1555
                       false);
1964
1558
  case SQLCOM_DELETE_MULTI:
1965
1559
  {
1966
1560
    assert(first_table == all_tables && first_table != 0);
1967
 
    TABLE_LIST *aux_tables=
1968
 
      (TABLE_LIST *)thd->lex->auxiliary_table_list.first;
 
1561
    TableList *aux_tables=
 
1562
      (TableList *)session->lex->auxiliary_table_list.first;
1969
1563
    multi_delete *del_result;
1970
1564
 
1971
 
    if (!thd->locked_tables &&
1972
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1565
    if (!session->locked_tables &&
 
1566
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1973
1567
    {
1974
1568
      res= 1;
1975
1569
      break;
1976
1570
    }
1977
1571
 
1978
 
    if ((res= multi_delete_precheck(thd, all_tables)))
 
1572
    if ((res= multi_delete_precheck(session, all_tables)))
1979
1573
      break;
1980
1574
 
1981
1575
    /* condition will be true on SP re-excuting */
1982
1576
    if (select_lex->item_list.elements != 0)
1983
1577
      select_lex->item_list.empty();
1984
 
    if (add_item_to_list(thd, new Item_null()))
 
1578
    if (session->add_item_to_list(new Item_null()))
1985
1579
      goto error;
1986
1580
 
1987
 
    thd_proc_info(thd, "init");
1988
 
    if ((res= open_and_lock_tables(thd, all_tables)))
 
1581
    session->set_proc_info("init");
 
1582
    if ((res= open_and_lock_tables(session, all_tables)))
1989
1583
      break;
1990
1584
 
1991
 
    if ((res= mysql_multi_delete_prepare(thd)))
 
1585
    if ((res= mysql_multi_delete_prepare(session)))
1992
1586
      goto error;
1993
1587
 
1994
 
    if (!thd->is_fatal_error &&
 
1588
    if (!session->is_fatal_error &&
1995
1589
        (del_result= new multi_delete(aux_tables, lex->table_count)))
1996
1590
    {
1997
 
      res= mysql_select(thd, &select_lex->ref_pointer_array,
 
1591
      res= mysql_select(session, &select_lex->ref_pointer_array,
1998
1592
                        select_lex->get_table_list(),
1999
1593
                        select_lex->with_wild,
2000
1594
                        select_lex->item_list,
2001
1595
                        select_lex->where,
2002
 
                        0, (ORDER *)NULL, (ORDER *)NULL, (Item *)NULL,
2003
 
                        (ORDER *)NULL,
2004
 
                        select_lex->options | thd->options |
 
1596
                        0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
 
1597
                        (order_st *)NULL,
 
1598
                        select_lex->options | session->options |
2005
1599
                        SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
2006
1600
                        OPTION_SETUP_TABLES_DONE,
2007
1601
                        del_result, unit, select_lex);
2008
 
      res|= thd->is_error();
 
1602
      res|= session->is_error();
2009
1603
      if (res)
2010
1604
        del_result->abort();
2011
1605
      delete del_result;
2019
1613
    assert(first_table == all_tables && first_table != 0);
2020
1614
    if (!lex->drop_temporary)
2021
1615
    {
2022
 
      if (end_active_trans(thd))
 
1616
      if (end_active_trans(session))
2023
1617
        goto error;
2024
1618
    }
2025
1619
    else
2026
1620
    {
2027
 
      /*
2028
 
        If this is a slave thread, we may sometimes execute some 
2029
 
        DROP / * 40005 TEMPORARY * / TABLE
2030
 
        that come from parts of binlogs (likely if we use RESET SLAVE or CHANGE
2031
 
        MASTER TO), while the temporary table has already been dropped.
2032
 
        To not generate such irrelevant "table does not exist errors",
2033
 
        we silently add IF EXISTS if TEMPORARY was used.
2034
 
      */
2035
 
      if (thd->slave_thread)
2036
 
        lex->drop_if_exists= 1;
2037
 
 
2038
1621
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
2039
 
      thd->options|= OPTION_KEEP_LOG;
 
1622
      session->options|= OPTION_KEEP_LOG;
2040
1623
    }
2041
1624
    /* DDL and binlog write order protected by LOCK_open */
2042
 
    res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
 
1625
    res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
2043
1626
  }
2044
1627
  break;
2045
1628
  case SQLCOM_SHOW_PROCESSLIST:
2046
 
    mysqld_list_processes(thd, NullS, lex->verbose);
 
1629
    mysqld_list_processes(session, NULL, lex->verbose);
2047
1630
    break;
2048
1631
  case SQLCOM_SHOW_ENGINE_LOGS:
2049
1632
    {
2050
 
      res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
 
1633
      res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
2051
1634
      break;
2052
1635
    }
2053
1636
  case SQLCOM_CHANGE_DB:
2054
1637
  {
2055
1638
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2056
1639
 
2057
 
    if (!mysql_change_db(thd, &db_str, false))
2058
 
      my_ok(thd);
 
1640
    if (!mysql_change_db(session, &db_str, false))
 
1641
      session->my_ok();
2059
1642
 
2060
1643
    break;
2061
1644
  }
2065
1648
    assert(first_table == all_tables && first_table != 0);
2066
1649
    if (lex->local_file)
2067
1650
    {
2068
 
      if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
 
1651
      if (!(session->client_capabilities & CLIENT_LOCAL_FILES) ||
2069
1652
          !opt_local_infile)
2070
1653
      {
2071
1654
        my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2073
1656
      }
2074
1657
    }
2075
1658
 
2076
 
    res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
 
1659
    res= mysql_load(session, lex->exchange, first_table, lex->field_list,
2077
1660
                    lex->update_list, lex->value_list, lex->duplicates,
2078
1661
                    lex->ignore, (bool) lex->local_file);
2079
1662
    break;
2083
1666
  {
2084
1667
    List<set_var_base> *lex_var_list= &lex->var_list;
2085
1668
 
2086
 
    if (lex->autocommit && end_active_trans(thd))
 
1669
    if (lex->autocommit && end_active_trans(session))
2087
1670
      goto error;
2088
1671
 
2089
 
    if (open_and_lock_tables(thd, all_tables))
2090
 
      goto error;
2091
 
    if (lex->one_shot_set && not_all_support_one_shot(lex_var_list))
2092
 
    {
2093
 
      my_error(ER_RESERVED_SYNTAX, MYF(0), "SET ONE_SHOT");
2094
 
      goto error;
2095
 
    }
2096
 
    if (!(res= sql_set_variables(thd, lex_var_list)))
2097
 
    {
2098
 
      /*
2099
 
        If the previous command was a SET ONE_SHOT, we don't want to forget
2100
 
        about the ONE_SHOT property of that SET. So we use a |= instead of = .
2101
 
      */
2102
 
      thd->one_shot_set|= lex->one_shot_set;
2103
 
      my_ok(thd);
 
1672
    if (open_and_lock_tables(session, all_tables))
 
1673
      goto error;
 
1674
    if (!(res= sql_set_variables(session, lex_var_list)))
 
1675
    {
 
1676
      session->my_ok();
2104
1677
    }
2105
1678
    else
2106
1679
    {
2109
1682
        Send something semi-generic here since we don't know which
2110
1683
        assignment in the list caused the error.
2111
1684
      */
2112
 
      if (!thd->is_error())
 
1685
      if (!session->is_error())
2113
1686
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2114
1687
      goto error;
2115
1688
    }
2124
1697
      done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2125
1698
      false, mysqldump will not work.
2126
1699
    */
2127
 
    unlock_locked_tables(thd);
2128
 
    if (thd->options & OPTION_TABLE_LOCK)
 
1700
    unlock_locked_tables(session);
 
1701
    if (session->options & OPTION_TABLE_LOCK)
2129
1702
    {
2130
 
      end_active_trans(thd);
2131
 
      thd->options&= ~(OPTION_TABLE_LOCK);
 
1703
      end_active_trans(session);
 
1704
      session->options&= ~(OPTION_TABLE_LOCK);
2132
1705
    }
2133
 
    if (thd->global_read_lock)
2134
 
      unlock_global_read_lock(thd);
2135
 
    my_ok(thd);
 
1706
    if (session->global_read_lock)
 
1707
      unlock_global_read_lock(session);
 
1708
    session->my_ok();
2136
1709
    break;
2137
1710
  case SQLCOM_LOCK_TABLES:
2138
1711
    /*
2139
1712
      We try to take transactional locks if
2140
1713
      - only transactional locks are requested (lex->lock_transactional) and
2141
 
      - no non-transactional locks exist (!thd->locked_tables).
 
1714
      - no non-transactional locks exist (!session->locked_tables).
2142
1715
    */
2143
 
    if (lex->lock_transactional && !thd->locked_tables)
 
1716
    if (lex->lock_transactional && !session->locked_tables)
2144
1717
    {
2145
1718
      int rc;
2146
1719
      /*
2147
1720
        All requested locks are transactional and no non-transactional
2148
1721
        locks exist.
2149
1722
      */
2150
 
      if ((rc= try_transactional_lock(thd, all_tables)) == -1)
 
1723
      if ((rc= try_transactional_lock(session, all_tables)) == -1)
2151
1724
        goto error;
2152
1725
      if (rc == 0)
2153
1726
      {
2154
 
        my_ok(thd);
 
1727
        session->my_ok();
2155
1728
        break;
2156
1729
      }
2157
1730
      /*
2168
1741
      requested. If yes, warn about the conversion to non-transactional
2169
1742
      locks or abort in strict mode.
2170
1743
    */
2171
 
    if (check_transactional_lock(thd, all_tables))
 
1744
    if (check_transactional_lock(session, all_tables))
2172
1745
      goto error;
2173
 
    unlock_locked_tables(thd);
 
1746
    unlock_locked_tables(session);
2174
1747
    /* we must end the trasaction first, regardless of anything */
2175
 
    if (end_active_trans(thd))
 
1748
    if (end_active_trans(session))
2176
1749
      goto error;
2177
 
    thd->in_lock_tables=1;
2178
 
    thd->options|= OPTION_TABLE_LOCK;
 
1750
    session->in_lock_tables=1;
 
1751
    session->options|= OPTION_TABLE_LOCK;
2179
1752
 
2180
 
    if (!(res= simple_open_n_lock_tables(thd, all_tables)))
 
1753
    if (!(res= simple_open_n_lock_tables(session, all_tables)))
2181
1754
    {
2182
 
      thd->locked_tables=thd->lock;
2183
 
      thd->lock=0;
2184
 
      (void) set_handler_table_locks(thd, all_tables, false);
2185
 
      my_ok(thd);
 
1755
      session->locked_tables=session->lock;
 
1756
      session->lock=0;
 
1757
      (void) set_handler_table_locks(session, all_tables, false);
 
1758
      session->my_ok();
2186
1759
    }
2187
1760
    else
2188
1761
    {
2189
 
      /* 
 
1762
      /*
2190
1763
        Need to end the current transaction, so the storage engine (InnoDB)
2191
1764
        can free its locks if LOCK TABLES locked some tables before finding
2192
1765
        that it can't lock a table in its list
2193
1766
      */
2194
 
      ha_autocommit_or_rollback(thd, 1);
2195
 
      end_active_trans(thd);
2196
 
      thd->options&= ~(OPTION_TABLE_LOCK);
 
1767
      ha_autocommit_or_rollback(session, 1);
 
1768
      end_active_trans(session);
 
1769
      session->options&= ~(OPTION_TABLE_LOCK);
2197
1770
    }
2198
 
    thd->in_lock_tables=0;
 
1771
    session->in_lock_tables=0;
2199
1772
    break;
2200
1773
  case SQLCOM_CREATE_DB:
2201
1774
  {
2205
1778
      prepared statement- safe.
2206
1779
    */
2207
1780
    HA_CREATE_INFO create_info(lex->create_info);
2208
 
    if (end_active_trans(thd))
 
1781
    if (end_active_trans(session))
2209
1782
    {
2210
1783
      res= -1;
2211
1784
      break;
2212
1785
    }
2213
1786
    char *alias;
2214
 
    if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
 
1787
    if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2215
1788
        check_db_name(&lex->name))
2216
1789
    {
2217
1790
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2218
1791
      break;
2219
1792
    }
2220
 
    /*
2221
 
      If in a slave thread :
2222
 
      CREATE DATABASE DB was certainly not preceded by USE DB.
2223
 
      For that reason, db_ok() in sql/slave.cc did not check the
2224
 
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2225
 
      above was not called. So we have to check rules again here.
2226
 
    */
2227
 
    if (thd->slave_thread && 
2228
 
        (!rpl_filter->db_ok(lex->name.str) ||
2229
 
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
2230
 
    {
2231
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2232
 
      break;
2233
 
    }
2234
 
    res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
 
1793
    res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
2235
1794
                              lex->name.str), &create_info, 0);
2236
1795
    break;
2237
1796
  }
2238
1797
  case SQLCOM_DROP_DB:
2239
1798
  {
2240
 
    if (end_active_trans(thd))
 
1799
    if (end_active_trans(session))
2241
1800
    {
2242
1801
      res= -1;
2243
1802
      break;
2247
1806
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2248
1807
      break;
2249
1808
    }
2250
 
    /*
2251
 
      If in a slave thread :
2252
 
      DROP DATABASE DB may not be preceded by USE DB.
2253
 
      For that reason, maybe db_ok() in sql/slave.cc did not check the 
2254
 
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2255
 
      above was not called. So we have to check rules again here.
2256
 
    */
2257
 
    if (thd->slave_thread && 
2258
 
        (!rpl_filter->db_ok(lex->name.str) ||
2259
 
         !rpl_filter->db_ok_with_wild_table(lex->name.str)))
2260
 
    {
2261
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2262
 
      break;
2263
 
    }
2264
 
    if (thd->locked_tables || thd->active_transaction())
 
1809
    if (session->locked_tables || session->active_transaction())
2265
1810
    {
2266
1811
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2267
1812
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2268
1813
      goto error;
2269
1814
    }
2270
 
    res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
 
1815
    res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
2271
1816
    break;
2272
1817
  }
2273
1818
  case SQLCOM_ALTER_DB:
2279
1824
      my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2280
1825
      break;
2281
1826
    }
2282
 
    /*
2283
 
      If in a slave thread :
2284
 
      ALTER DATABASE DB may not be preceded by USE DB.
2285
 
      For that reason, maybe db_ok() in sql/slave.cc did not check the
2286
 
      do_db/ignore_db. And as this query involves no tables, tables_ok()
2287
 
      above was not called. So we have to check rules again here.
2288
 
    */
2289
 
    if (thd->slave_thread &&
2290
 
        (!rpl_filter->db_ok(db->str) ||
2291
 
         !rpl_filter->db_ok_with_wild_table(db->str)))
2292
 
    {
2293
 
      my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2294
 
      break;
2295
 
    }
2296
 
    if (thd->locked_tables || thd->active_transaction())
 
1827
    if (session->locked_tables || session->active_transaction())
2297
1828
    {
2298
1829
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2299
1830
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2300
1831
      goto error;
2301
1832
    }
2302
 
    res= mysql_alter_db(thd, db->str, &create_info);
 
1833
    res= mysql_alter_db(session, db->str, &create_info);
2303
1834
    break;
2304
1835
  }
2305
1836
  case SQLCOM_SHOW_CREATE_DB:
2309
1840
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2310
1841
      break;
2311
1842
    }
2312
 
    res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
 
1843
    res= mysqld_show_create_db(session, lex->name.str, &lex->create_info);
2313
1844
    break;
2314
1845
  }
2315
 
  case SQLCOM_RESET:
2316
 
    /*
2317
 
      RESET commands are never written to the binary log, so we have to
2318
 
      initialize this variable because RESET shares the same code as FLUSH
2319
 
    */
2320
 
    lex->no_write_to_binlog= 1;
2321
1846
  case SQLCOM_FLUSH:
2322
1847
  {
2323
1848
    bool write_to_binlog;
2326
1851
      reload_cache() will tell us if we are allowed to write to the
2327
1852
      binlog or not.
2328
1853
    */
2329
 
    if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
 
1854
    if (!reload_cache(session, lex->type, first_table, &write_to_binlog))
2330
1855
    {
2331
1856
      /*
2332
1857
        We WANT to write and we CAN write.
2335
1860
      /*
2336
1861
        Presumably, RESET and binlog writing doesn't require synchronization
2337
1862
      */
2338
 
      if (!lex->no_write_to_binlog && write_to_binlog)
2339
 
      {
2340
 
        write_bin_log(thd, false, thd->query, thd->query_length);
2341
 
      }
2342
 
      my_ok(thd);
2343
 
    } 
2344
 
    
 
1863
      write_bin_log(session, false, session->query, session->query_length);
 
1864
      session->my_ok();
 
1865
    }
 
1866
 
2345
1867
    break;
2346
1868
  }
2347
1869
  case SQLCOM_KILL:
2348
1870
  {
2349
1871
    Item *it= (Item *)lex->value_list.head();
2350
1872
 
2351
 
    if (lex->table_or_sp_used())
2352
 
    {
2353
 
      my_error(ER_NOT_SUPPORTED_YET, MYF(0), "Usage of subqueries or stored "
2354
 
               "function calls as part of this statement");
2355
 
      break;
2356
 
    }
2357
 
 
2358
 
    if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
 
1873
    if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2359
1874
    {
2360
1875
      my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2361
1876
                 MYF(0));
2362
1877
      goto error;
2363
1878
    }
2364
 
    sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
 
1879
    sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2365
1880
    break;
2366
1881
  }
2367
1882
  case SQLCOM_BEGIN:
2368
 
    if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
1883
    if (session->transaction.xid_state.xa_state != XA_NOTR)
2369
1884
    {
2370
1885
      my_error(ER_XAER_RMFAIL, MYF(0),
2371
 
               xa_state_names[thd->transaction.xid_state.xa_state]);
 
1886
               xa_state_names[session->transaction.xid_state.xa_state]);
2372
1887
      break;
2373
1888
    }
2374
1889
    /*
2375
1890
      Breakpoints for backup testing.
2376
1891
    */
2377
 
    if (begin_trans(thd))
 
1892
    if (begin_trans(session))
2378
1893
      goto error;
2379
 
    my_ok(thd);
 
1894
    session->my_ok();
2380
1895
    break;
2381
1896
  case SQLCOM_COMMIT:
2382
 
    if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
 
1897
    if (end_trans(session, lex->tx_release ? COMMIT_RELEASE :
2383
1898
                              lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2384
1899
      goto error;
2385
 
    my_ok(thd);
 
1900
    session->my_ok();
2386
1901
    break;
2387
1902
  case SQLCOM_ROLLBACK:
2388
 
    if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
 
1903
    if (end_trans(session, lex->tx_release ? ROLLBACK_RELEASE :
2389
1904
                              lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2390
1905
      goto error;
2391
 
    my_ok(thd);
 
1906
    session->my_ok();
2392
1907
    break;
2393
1908
  case SQLCOM_RELEASE_SAVEPOINT:
2394
1909
  {
2395
1910
    SAVEPOINT *sv;
2396
 
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
 
1911
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2397
1912
    {
2398
1913
      if (my_strnncoll(system_charset_info,
2399
 
                       (uchar *)lex->ident.str, lex->ident.length,
2400
 
                       (uchar *)sv->name, sv->length) == 0)
 
1914
                       (unsigned char *)lex->ident.str, lex->ident.length,
 
1915
                       (unsigned char *)sv->name, sv->length) == 0)
2401
1916
        break;
2402
1917
    }
2403
1918
    if (sv)
2404
1919
    {
2405
 
      if (ha_release_savepoint(thd, sv))
 
1920
      if (ha_release_savepoint(session, sv))
2406
1921
        res= true; // cannot happen
2407
1922
      else
2408
 
        my_ok(thd);
2409
 
      thd->transaction.savepoints=sv->prev;
 
1923
        session->my_ok();
 
1924
      session->transaction.savepoints=sv->prev;
2410
1925
    }
2411
1926
    else
2412
1927
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2415
1930
  case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2416
1931
  {
2417
1932
    SAVEPOINT *sv;
2418
 
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
 
1933
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2419
1934
    {
2420
1935
      if (my_strnncoll(system_charset_info,
2421
 
                       (uchar *)lex->ident.str, lex->ident.length,
2422
 
                       (uchar *)sv->name, sv->length) == 0)
 
1936
                       (unsigned char *)lex->ident.str, lex->ident.length,
 
1937
                       (unsigned char *)sv->name, sv->length) == 0)
2423
1938
        break;
2424
1939
    }
2425
1940
    if (sv)
2426
1941
    {
2427
 
      if (ha_rollback_to_savepoint(thd, sv))
 
1942
      if (ha_rollback_to_savepoint(session, sv))
2428
1943
        res= true; // cannot happen
2429
1944
      else
2430
1945
      {
2431
 
        if (((thd->options & OPTION_KEEP_LOG) || 
2432
 
             thd->transaction.all.modified_non_trans_table) &&
2433
 
            !thd->slave_thread)
2434
 
          push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1946
        if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
 
1947
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2435
1948
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
2436
1949
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2437
 
        my_ok(thd);
 
1950
        session->my_ok();
2438
1951
      }
2439
 
      thd->transaction.savepoints=sv;
 
1952
      session->transaction.savepoints=sv;
2440
1953
    }
2441
1954
    else
2442
1955
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2443
1956
    break;
2444
1957
  }
2445
1958
  case SQLCOM_SAVEPOINT:
2446
 
    if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2447
 
          thd->in_sub_stmt) || !opt_using_transactions)
2448
 
      my_ok(thd);
 
1959
    if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)) || !opt_using_transactions)
 
1960
      session->my_ok();
2449
1961
    else
2450
1962
    {
2451
1963
      SAVEPOINT **sv, *newsv;
2452
 
      for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
 
1964
      for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2453
1965
      {
2454
1966
        if (my_strnncoll(system_charset_info,
2455
 
                         (uchar *)lex->ident.str, lex->ident.length,
2456
 
                         (uchar *)(*sv)->name, (*sv)->length) == 0)
 
1967
                         (unsigned char *)lex->ident.str, lex->ident.length,
 
1968
                         (unsigned char *)(*sv)->name, (*sv)->length) == 0)
2457
1969
          break;
2458
1970
      }
2459
1971
      if (*sv) /* old savepoint of the same name exists */
2460
1972
      {
2461
1973
        newsv=*sv;
2462
 
        ha_release_savepoint(thd, *sv); // it cannot fail
 
1974
        ha_release_savepoint(session, *sv); // it cannot fail
2463
1975
        *sv=(*sv)->prev;
2464
1976
      }
2465
 
      else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
 
1977
      else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
2466
1978
                                               savepoint_alloc_size)) == 0)
2467
1979
      {
2468
1980
        my_error(ER_OUT_OF_RESOURCES, MYF(0));
2469
1981
        break;
2470
1982
      }
2471
 
      newsv->name=strmake_root(&thd->transaction.mem_root,
 
1983
      newsv->name=strmake_root(&session->transaction.mem_root,
2472
1984
                               lex->ident.str, lex->ident.length);
2473
1985
      newsv->length=lex->ident.length;
2474
1986
      /*
2476
1988
        we'll lose a little bit of memory in transaction mem_root, but it'll
2477
1989
        be free'd when transaction ends anyway
2478
1990
      */
2479
 
      if (ha_savepoint(thd, newsv))
 
1991
      if (ha_savepoint(session, newsv))
2480
1992
        res= true;
2481
1993
      else
2482
1994
      {
2483
 
        newsv->prev=thd->transaction.savepoints;
2484
 
        thd->transaction.savepoints=newsv;
2485
 
        my_ok(thd);
 
1995
        newsv->prev=session->transaction.savepoints;
 
1996
        session->transaction.savepoints=newsv;
 
1997
        session->my_ok();
2486
1998
      }
2487
1999
    }
2488
2000
    break;
2489
 
  case SQLCOM_BINLOG_BASE64_EVENT:
2490
 
  {
2491
 
    mysql_client_binlog_statement(thd);
2492
 
    break;
2493
 
  }
2494
2001
  default:
2495
2002
    assert(0);                             /* Impossible */
2496
 
    my_ok(thd);
 
2003
    session->my_ok();
2497
2004
    break;
2498
2005
  }
2499
 
  thd_proc_info(thd, "query end");
2500
 
 
2501
 
  /*
2502
 
    Binlog-related cleanup:
2503
 
    Reset system variables temporarily modified by SET ONE SHOT.
2504
 
 
2505
 
    Exception: If this is a SET, do nothing. This is to allow
2506
 
    mysqlbinlog to print many SET commands (in this case we want the
2507
 
    charset temp setting to live until the real query). This is also
2508
 
    needed so that SET CHARACTER_SET_CLIENT... does not cancel itself
2509
 
    immediately.
2510
 
  */
2511
 
  if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
2512
 
    reset_one_shot_variables(thd);
 
2006
  session->set_proc_info("query end");
2513
2007
 
2514
2008
  /*
2515
2009
    The return value for ROW_COUNT() is "implementation dependent" if the
2517
2011
    wants. We also keep the last value in case of SQLCOM_CALL or
2518
2012
    SQLCOM_EXECUTE.
2519
2013
  */
2520
 
  if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2521
 
    thd->row_count_func= -1;
 
2014
  if (!(sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
2015
    session->row_count_func= -1;
2522
2016
 
2523
2017
  goto finish;
2524
2018
 
2532
2026
      Release the protection against the global read lock and wake
2533
2027
      everyone, who might want to set a global read lock.
2534
2028
    */
2535
 
    start_waiting_global_read_lock(thd);
 
2029
    start_waiting_global_read_lock(session);
2536
2030
  }
2537
 
  return(res || thd->is_error());
 
2031
  return(res || session->is_error());
2538
2032
}
2539
2033
 
2540
 
bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
 
2034
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2541
2035
{
2542
 
  LEX   *lex= thd->lex;
 
2036
  LEX   *lex= session->lex;
2543
2037
  select_result *result=lex->result;
2544
2038
  bool res;
2545
2039
  /* assign global limit variable if limit is not given */
2547
2041
    SELECT_LEX *param= lex->unit.global_parameters;
2548
2042
    if (!param->explicit_limit)
2549
2043
      param->select_limit=
2550
 
        new Item_int((uint64_t) thd->variables.select_limit);
 
2044
        new Item_int((uint64_t) session->variables.select_limit);
2551
2045
  }
2552
 
  if (!(res= open_and_lock_tables(thd, all_tables)))
 
2046
  if (!(res= open_and_lock_tables(session, all_tables)))
2553
2047
  {
2554
2048
    if (lex->describe)
2555
2049
    {
2561
2055
      */
2562
2056
      if (!(result= new select_send()))
2563
2057
        return 1;                               /* purecov: inspected */
2564
 
      thd->send_explain_fields(result);
2565
 
      res= mysql_explain_union(thd, &thd->lex->unit, result);
 
2058
      session->send_explain_fields(result);
 
2059
      res= mysql_explain_union(session, &session->lex->unit, result);
2566
2060
      if (lex->describe & DESCRIBE_EXTENDED)
2567
2061
      {
2568
2062
        char buff[1024];
2569
2063
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2570
2064
        str.length(0);
2571
 
        thd->lex->unit.print(&str, QT_ORDINARY);
 
2065
        session->lex->unit.print(&str, QT_ORDINARY);
2572
2066
        str.append('\0');
2573
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2067
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2574
2068
                     ER_YES, str.ptr());
2575
2069
      }
2576
2070
      if (res)
2583
2077
    {
2584
2078
      if (!result && !(result= new select_send()))
2585
2079
        return 1;                               /* purecov: inspected */
2586
 
      res= handle_select(thd, lex, result, 0);
 
2080
      res= handle_select(session, lex, result, 0);
2587
2081
      if (result != lex->result)
2588
2082
        delete result;
2589
2083
    }
2591
2085
  return res;
2592
2086
}
2593
2087
 
2594
 
/****************************************************************************
2595
 
        Check stack size; Send error if there isn't enough stack to continue
2596
 
****************************************************************************/
2597
 
#if STACK_DIRECTION < 0
2598
 
#define used_stack(A,B) (long) (A - B)
2599
 
#else
2600
 
#define used_stack(A,B) (long) (B - A)
2601
 
#endif
2602
 
 
2603
 
/**
2604
 
  @note
2605
 
  Note: The 'buf' parameter is necessary, even if it is unused here.
2606
 
  - fix_fields functions has a "dummy" buffer large enough for the
2607
 
    corresponding exec. (Thus we only have to check in fix_fields.)
2608
 
  - Passing to check_stack_overrun() prevents the compiler from removing it.
2609
 
*/
2610
 
bool check_stack_overrun(THD *thd, long margin,
2611
 
                         uchar *buf __attribute__((unused)))
2612
 
{
2613
 
  long stack_used;
2614
 
  assert(thd == current_thd);
2615
 
  if ((stack_used=used_stack(thd->thread_stack,(char*) &stack_used)) >=
2616
 
      (long) (my_thread_stack_size - margin))
2617
 
  {
2618
 
    sprintf(errbuff[0],ER(ER_STACK_OVERRUN_NEED_MORE),
2619
 
            stack_used,my_thread_stack_size,margin);
2620
 
    my_message(ER_STACK_OVERRUN_NEED_MORE,errbuff[0],MYF(ME_FATALERROR));
2621
 
    return 1;
2622
 
  }
2623
 
  return 0;
2624
 
}
2625
2088
 
2626
2089
#define MY_YACC_INIT 1000                       // Start with big alloc
2627
2090
#define MY_YACC_MAX  32000                      // Because of 'short'
2628
2091
 
2629
2092
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2630
2093
{
2631
 
  LEX   *lex= current_thd->lex;
 
2094
  LEX   *lex= current_session->lex;
2632
2095
  ulong old_info=0;
2633
 
  if ((uint) *yystacksize >= MY_YACC_MAX)
 
2096
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2634
2097
    return 1;
2635
2098
  if (!lex->yacc_yyvs)
2636
2099
    old_info= *yystacksize;
2637
2100
  *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
2638
 
  if (!(lex->yacc_yyvs= (uchar*)
2639
 
        my_realloc(lex->yacc_yyvs,
2640
 
                   *yystacksize*sizeof(**yyvs),
2641
 
                   MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
2642
 
      !(lex->yacc_yyss= (uchar*)
2643
 
        my_realloc(lex->yacc_yyss,
2644
 
                   *yystacksize*sizeof(**yyss),
2645
 
                   MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
2646
 
    return 1;
 
2101
  unsigned char *tmpptr= NULL;
 
2102
  if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
 
2103
                                         *yystacksize* sizeof(**yyvs))))
 
2104
      return 1;
 
2105
  lex->yacc_yyvs= tmpptr;
 
2106
  tmpptr= NULL;
 
2107
  if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
 
2108
                                        *yystacksize* sizeof(**yyss))))
 
2109
      return 1;
 
2110
  lex->yacc_yyss= tmpptr;
2647
2111
  if (old_info)
2648
2112
  {                                             // Copy old info from stack
2649
2113
    memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2655
2119
}
2656
2120
 
2657
2121
 
2658
 
/**
2659
 
 Reset THD part responsible for command processing state.
2660
 
 
2661
 
   This needs to be called before execution of every statement
2662
 
   (prepared or conventional).
2663
 
   It is not called by substatements of routines.
2664
 
 
2665
 
  @todo
2666
 
   Make it a method of THD and align its name with the rest of
2667
 
   reset/end/start/init methods.
2668
 
  @todo
2669
 
   Call it after we use THD for queries, not before.
2670
 
*/
2671
 
 
2672
 
void mysql_reset_thd_for_next_command(THD *thd)
2673
 
{
2674
 
  assert(! thd->in_sub_stmt);
2675
 
  thd->free_list= 0;
2676
 
  thd->select_number= 1;
2677
 
  /*
2678
 
    Those two lines below are theoretically unneeded as
2679
 
    THD::cleanup_after_query() should take care of this already.
2680
 
  */
2681
 
  thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
2682
 
  thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
2683
 
 
2684
 
  thd->query_start_used= 0;
2685
 
  thd->is_fatal_error= thd->time_zone_used= 0;
2686
 
  thd->server_status&= ~ (SERVER_MORE_RESULTS_EXISTS | 
2687
 
                          SERVER_QUERY_NO_INDEX_USED |
2688
 
                          SERVER_QUERY_NO_GOOD_INDEX_USED);
2689
 
  /*
2690
 
    If in autocommit mode and not in a transaction, reset
2691
 
    OPTION_STATUS_NO_TRANS_UPDATE | OPTION_KEEP_LOG to not get warnings
2692
 
    in ha_rollback_trans() about some tables couldn't be rolled back.
2693
 
  */
2694
 
  if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
2695
 
  {
2696
 
    thd->options&= ~OPTION_KEEP_LOG;
2697
 
    thd->transaction.all.modified_non_trans_table= false;
2698
 
  }
2699
 
  assert(thd->security_ctx== &thd->main_security_ctx);
2700
 
  thd->thread_specific_used= false;
2701
 
 
2702
 
  if (opt_bin_log)
2703
 
  {
2704
 
    reset_dynamic(&thd->user_var_events);
2705
 
    thd->user_var_events_alloc= thd->mem_root;
2706
 
  }
2707
 
  thd->clear_error();
2708
 
  thd->main_da.reset_diagnostics_area();
2709
 
  thd->total_warn_count=0;                      // Warnings for this query
2710
 
  thd->rand_used= 0;
2711
 
  thd->sent_row_count= thd->examined_row_count= 0;
2712
 
 
2713
 
  /*
2714
 
    Because we come here only for start of top-statements, binlog format is
2715
 
    constant inside a complex statement (using stored functions) etc.
2716
 
  */
2717
 
  thd->reset_current_stmt_binlog_row_based();
2718
 
 
2719
 
  return;
2720
 
}
2721
 
 
2722
 
 
2723
2122
void
2724
2123
mysql_init_select(LEX *lex)
2725
2124
{
2738
2137
mysql_new_select(LEX *lex, bool move_down)
2739
2138
{
2740
2139
  SELECT_LEX *select_lex;
2741
 
  THD *thd= lex->thd;
 
2140
  Session *session= lex->session;
2742
2141
 
2743
 
  if (!(select_lex= new (thd->mem_root) SELECT_LEX()))
 
2142
  if (!(select_lex= new (session->mem_root) SELECT_LEX()))
2744
2143
    return(1);
2745
 
  select_lex->select_number= ++thd->select_number;
 
2144
  select_lex->select_number= ++session->select_number;
2746
2145
  select_lex->parent_lex= lex; /* Used in init_query. */
2747
2146
  select_lex->init_query();
2748
2147
  select_lex->init_select();
2758
2157
    SELECT_LEX_UNIT *unit;
2759
2158
    lex->subqueries= true;
2760
2159
    /* first select_lex of subselect or derived table */
2761
 
    if (!(unit= new (thd->mem_root) SELECT_LEX_UNIT()))
 
2160
    if (!(unit= new (session->mem_root) SELECT_LEX_UNIT()))
2762
2161
      return(1);
2763
2162
 
2764
2163
    unit->init_query();
2765
2164
    unit->init_select();
2766
 
    unit->thd= thd;
 
2165
    unit->session= session;
2767
2166
    unit->include_down(lex->current_select);
2768
2167
    unit->link_next= 0;
2769
2168
    unit->link_prev= 0;
2779
2178
  {
2780
2179
    if (lex->current_select->order_list.first && !lex->current_select->braces)
2781
2180
    {
2782
 
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "ORDER BY");
 
2181
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
2783
2182
      return(1);
2784
2183
    }
2785
2184
    select_lex->include_neighbour(lex->current_select);
2786
 
    SELECT_LEX_UNIT *unit= select_lex->master_unit();                              
2787
 
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
 
2185
    SELECT_LEX_UNIT *unit= select_lex->master_unit();
 
2186
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
2788
2187
      return(1);
2789
 
    select_lex->context.outer_context= 
 
2188
    select_lex->context.outer_context=
2790
2189
                unit->first_select()->context.outer_context;
2791
2190
  }
2792
2191
 
2813
2212
 
2814
2213
void create_select_for_variable(const char *var_name)
2815
2214
{
2816
 
  THD *thd;
 
2215
  Session *session;
2817
2216
  LEX *lex;
2818
2217
  LEX_STRING tmp, null_lex_string;
2819
2218
  Item *var;
2820
 
  char buff[MAX_SYS_VAR_LENGTH*2+4+8], *end;
 
2219
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
 
2220
  char *end= buff;
2821
2221
 
2822
 
  thd= current_thd;
2823
 
  lex= thd->lex;
 
2222
  session= current_session;
 
2223
  lex= session->lex;
2824
2224
  mysql_init_select(lex);
2825
2225
  lex->sql_command= SQLCOM_SELECT;
2826
2226
  tmp.str= (char*) var_name;
2830
2230
    We set the name of Item to @@session.var_name because that then is used
2831
2231
    as the column name in the output.
2832
2232
  */
2833
 
  if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
 
2233
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
2834
2234
  {
2835
 
    end= strxmov(buff, "@@session.", var_name, NullS);
 
2235
    end+= sprintf(buff, "@@session.%s", var_name);
2836
2236
    var->set_name(buff, end-buff, system_charset_info);
2837
 
    add_item_to_list(thd, var);
 
2237
    session->add_item_to_list(var);
2838
2238
  }
2839
2239
  return;
2840
2240
}
2847
2247
  lex->select_lex.select_limit= 0;
2848
2248
  lex->unit.select_limit_cnt= HA_POS_ERROR;
2849
2249
  lex->select_lex.table_list.save_and_clear(&lex->auxiliary_table_list);
2850
 
  lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
 
2250
  lex->lock_option= TL_READ;
2851
2251
  lex->query_tables= 0;
2852
2252
  lex->query_tables_last= &lex->query_tables;
2853
2253
}
2861
2261
/**
2862
2262
  Parse a query.
2863
2263
 
2864
 
  @param       thd     Current thread
 
2264
  @param       session     Current thread
2865
2265
  @param       inBuf   Begining of the query text
2866
2266
  @param       length  Length of the query text
2867
2267
  @param[out]  found_semicolon For multi queries, position of the character of
2868
2268
                               the next query in the query text.
2869
2269
*/
2870
2270
 
2871
 
void mysql_parse(THD *thd, const char *inBuf, uint length,
 
2271
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
2872
2272
                 const char ** found_semicolon)
2873
2273
{
2874
2274
  /*
2879
2279
    - first, call query_cache_send_result_to_client,
2880
2280
    - second, if caching failed, initialise the lexical and syntactic parser.
2881
2281
    The problem is that the query cache depends on a clean initialization
2882
 
    of (among others) lex->safe_to_cache_query and thd->server_status,
 
2282
    of (among others) lex->safe_to_cache_query and session->server_status,
2883
2283
    which are reset respectively in
2884
2284
    - lex_start()
2885
 
    - mysql_reset_thd_for_next_command()
 
2285
    - mysql_reset_session_for_next_command()
2886
2286
    So, initializing the lexical analyser *before* using the query cache
2887
2287
    is required for the cache to work properly.
2888
2288
    FIXME: cleanup the dependencies in the code to simplify this.
2889
2289
  */
2890
 
  lex_start(thd);
2891
 
  mysql_reset_thd_for_next_command(thd);
 
2290
  lex_start(session);
 
2291
  session->reset_for_next_command();
2892
2292
 
2893
2293
  {
2894
 
    LEX *lex= thd->lex;
2895
 
 
2896
 
    Lex_input_stream lip(thd, inBuf, length);
2897
 
 
2898
 
    bool err= parse_sql(thd, &lip);
 
2294
    LEX *lex= session->lex;
 
2295
 
 
2296
    Lex_input_stream lip(session, inBuf, length);
 
2297
 
 
2298
    bool err= parse_sql(session, &lip);
2899
2299
    *found_semicolon= lip.found_semicolon;
2900
2300
 
2901
2301
    if (!err)
2902
2302
    {
2903
2303
      {
2904
 
        if (! thd->is_error())
 
2304
        if (! session->is_error())
2905
2305
        {
2906
2306
          /*
2907
 
            Binlog logs a string starting from thd->query and having length
2908
 
            thd->query_length; so we set thd->query_length correctly (to not
 
2307
            Binlog logs a string starting from session->query and having length
 
2308
            session->query_length; so we set session->query_length correctly (to not
2909
2309
            log several statements in one event, when we executed only first).
2910
2310
            We set it to not see the ';' (otherwise it would get into binlog
2911
2311
            and Query_log_event::print() would give ';;' output).
2914
2314
            Note that we don't need LOCK_thread_count to modify query_length.
2915
2315
          */
2916
2316
          if (*found_semicolon &&
2917
 
              (thd->query_length= (ulong)(*found_semicolon - thd->query)))
2918
 
            thd->query_length--;
 
2317
              (session->query_length= (ulong)(*found_semicolon - session->query)))
 
2318
            session->query_length--;
2919
2319
          /* Actually execute the query */
2920
 
          mysql_execute_command(thd);
 
2320
          mysql_execute_command(session);
2921
2321
        }
2922
2322
      }
2923
2323
    }
2924
2324
    else
2925
2325
    {
2926
 
      assert(thd->is_error());
 
2326
      assert(session->is_error());
2927
2327
    }
2928
2328
    lex->unit.cleanup();
2929
 
    thd_proc_info(thd, "freeing items");
2930
 
    thd->end_statement();
2931
 
    thd->cleanup_after_query();
2932
 
    assert(thd->change_list.is_empty());
 
2329
    session->set_proc_info("freeing items");
 
2330
    session->end_statement();
 
2331
    session->cleanup_after_query();
 
2332
    assert(session->change_list.is_empty());
2933
2333
  }
2934
2334
 
2935
2335
  return;
2946
2346
    1   can be ignored
2947
2347
*/
2948
2348
 
2949
 
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint length)
 
2349
bool mysql_test_parse_for_slave(Session *session, char *inBuf, uint32_t length)
2950
2350
{
2951
 
  LEX *lex= thd->lex;
 
2351
  LEX *lex= session->lex;
2952
2352
  bool error= 0;
2953
2353
 
2954
 
  Lex_input_stream lip(thd, inBuf, length);
2955
 
  lex_start(thd);
2956
 
  mysql_reset_thd_for_next_command(thd);
 
2354
  Lex_input_stream lip(session, inBuf, length);
 
2355
  lex_start(session);
 
2356
  session->reset_for_next_command();
2957
2357
 
2958
 
  if (!parse_sql(thd, &lip) &&
2959
 
      all_tables_not_ok(thd,(TABLE_LIST*) lex->select_lex.table_list.first))
 
2358
  if (!parse_sql(session, &lip) &&
 
2359
      all_tables_not_ok(session,(TableList*) lex->select_lex.table_list.first))
2960
2360
    error= 1;                  /* Ignore question */
2961
 
  thd->end_statement();
2962
 
  thd->cleanup_after_query();
 
2361
  session->end_statement();
 
2362
  session->cleanup_after_query();
2963
2363
  return(error);
2964
2364
}
2965
2365
 
2972
2372
    Return 0 if ok
2973
2373
*/
2974
2374
 
2975
 
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
 
2375
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
2976
2376
                       char *length, char *decimals,
2977
 
                       uint type_modifier,
 
2377
                       uint32_t type_modifier,
2978
2378
                       enum column_format_type column_format,
2979
2379
                       Item *default_value, Item *on_update_value,
2980
2380
                       LEX_STRING *comment,
2981
2381
                       char *change,
2982
 
                       List<String> *interval_list, const CHARSET_INFO * const cs)
 
2382
                       List<String> *interval_list, const CHARSET_INFO * const cs,
 
2383
                       virtual_column_info *vcol_info)
2983
2384
{
2984
2385
  register Create_field *new_field;
2985
 
  LEX  *lex= thd->lex;
 
2386
  LEX  *lex= session->lex;
2986
2387
 
2987
2388
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
2988
2389
    return(1);                          /* purecov: inspected */
3010
2411
 
3011
2412
  if (default_value)
3012
2413
  {
3013
 
    /* 
 
2414
    /*
3014
2415
      Default value should be literal => basic constants =>
3015
2416
      no need fix_fields()
3016
 
      
3017
 
      We allow only one function as part of default value - 
 
2417
 
 
2418
      We allow only one function as part of default value -
3018
2419
      NOW() as default for TIMESTAMP type.
3019
2420
    */
3020
 
    if (default_value->type() == Item::FUNC_ITEM && 
 
2421
    if (default_value->type() == Item::FUNC_ITEM &&
3021
2422
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
3022
2423
         type == DRIZZLE_TYPE_TIMESTAMP))
3023
2424
    {
3048
2449
  }
3049
2450
 
3050
2451
  if (!(new_field= new Create_field()) ||
3051
 
      new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
 
2452
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
3052
2453
                      default_value, on_update_value, comment, change,
3053
 
                      interval_list, cs, 0, column_format))
 
2454
                      interval_list, cs, 0, column_format,
 
2455
                      vcol_info))
3054
2456
    return(1);
3055
2457
 
3056
2458
  lex->alter_info.create_list.push_back(new_field);
3063
2465
 
3064
2466
void store_position_for_column(const char *name)
3065
2467
{
3066
 
  current_thd->lex->last_field->after=my_const_cast(char*) (name);
 
2468
  current_session->lex->last_field->after=const_cast<char*> (name);
3067
2469
}
3068
2470
 
3069
2471
bool
3070
 
add_proc_to_list(THD* thd, Item *item)
 
2472
add_proc_to_list(Session* session, Item *item)
3071
2473
{
3072
 
  ORDER *order;
 
2474
  order_st *order;
3073
2475
  Item  **item_ptr;
3074
2476
 
3075
 
  if (!(order = (ORDER *) thd->alloc(sizeof(ORDER)+sizeof(Item*))))
 
2477
  if (!(order = (order_st *) session->alloc(sizeof(order_st)+sizeof(Item*))))
3076
2478
    return 1;
3077
2479
  item_ptr = (Item**) (order+1);
3078
2480
  *item_ptr= item;
3079
2481
  order->item=item_ptr;
3080
2482
  order->free_me=0;
3081
 
  thd->lex->proc_list.link_in_list((uchar*) order,(uchar**) &order->next);
 
2483
  session->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
3082
2484
  return 0;
3083
2485
}
3084
2486
 
3087
2489
  save order by and tables in own lists.
3088
2490
*/
3089
2491
 
3090
 
bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc)
 
2492
bool add_to_list(Session *session, SQL_LIST &list,Item *item,bool asc)
3091
2493
{
3092
 
  ORDER *order;
3093
 
  if (!(order = (ORDER *) thd->alloc(sizeof(ORDER))))
 
2494
  order_st *order;
 
2495
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
3094
2496
    return(1);
3095
2497
  order->item_ptr= item;
3096
2498
  order->item= &order->item_ptr;
3098
2500
  order->free_me=0;
3099
2501
  order->used=0;
3100
2502
  order->counter_used= 0;
3101
 
  list.link_in_list((uchar*) order,(uchar**) &order->next);
 
2503
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
3102
2504
  return(0);
3103
2505
}
3104
2506
 
3119
2521
  @retval
3120
2522
      0         Error
3121
2523
  @retval
3122
 
    \#  Pointer to TABLE_LIST element added to the total table list
 
2524
    \#  Pointer to TableList element added to the total table list
3123
2525
*/
3124
2526
 
3125
 
TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
 
2527
TableList *st_select_lex::add_table_to_list(Session *session,
3126
2528
                                             Table_ident *table,
3127
2529
                                             LEX_STRING *alias,
3128
2530
                                             uint32_t table_options,
3130
2532
                                             List<Index_hint> *index_hints_arg,
3131
2533
                                             LEX_STRING *option)
3132
2534
{
3133
 
  register TABLE_LIST *ptr;
3134
 
  TABLE_LIST *previous_table_ref; /* The table preceding the current one. */
 
2535
  register TableList *ptr;
 
2536
  TableList *previous_table_ref; /* The table preceding the current one. */
3135
2537
  char *alias_str;
3136
 
  LEX *lex= thd->lex;
 
2538
  LEX *lex= session->lex;
3137
2539
 
3138
2540
  if (!table)
3139
2541
    return(0);                          // End of memory
3140
2542
  alias_str= alias ? alias->str : table->table.str;
3141
 
  if (!test(table_options & TL_OPTION_ALIAS) && 
 
2543
  if (!test(table_options & TL_OPTION_ALIAS) &&
3142
2544
      check_table_name(table->table.str, table->table.length))
3143
2545
  {
3144
2546
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
3160
2562
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
3161
2563
      return(0);
3162
2564
    }
3163
 
    if (!(alias_str= (char*) thd->memdup(alias_str,table->table.length+1)))
 
2565
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
3164
2566
      return(0);
3165
2567
  }
3166
 
  if (!(ptr = (TABLE_LIST *) thd->calloc(sizeof(TABLE_LIST))))
 
2568
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
3167
2569
    return(0);                          /* purecov: inspected */
3168
2570
  if (table->db.str)
3169
2571
  {
3190
2592
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
3191
2593
  ptr->derived=     table->sel;
3192
2594
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
3193
 
                                      INFORMATION_SCHEMA_NAME.str))
 
2595
                                      INFORMATION_SCHEMA_NAME.c_str()))
3194
2596
  {
3195
 
    ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
 
2597
    ST_SCHEMA_TABLE *schema_table= find_schema_table(session, ptr->table_name);
3196
2598
    if (!schema_table ||
3197
 
        (schema_table->hidden && 
3198
 
         ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 || 
 
2599
        (schema_table->hidden &&
 
2600
         ((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
3199
2601
          /*
3200
2602
            this check is used for show columns|keys from I_S hidden table
3201
2603
          */
3203
2605
          lex->sql_command == SQLCOM_SHOW_KEYS)))
3204
2606
    {
3205
2607
      my_error(ER_UNKNOWN_TABLE, MYF(0),
3206
 
               ptr->table_name, INFORMATION_SCHEMA_NAME.str);
 
2608
               ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
3207
2609
      return(0);
3208
2610
    }
3209
2611
    ptr->schema_table_name= ptr->table_name;
3216
2618
  /* check that used name is unique */
3217
2619
  if (lock_type != TL_IGNORE)
3218
2620
  {
3219
 
    TABLE_LIST *first_table= (TABLE_LIST*) table_list.first;
3220
 
    for (TABLE_LIST *tables= first_table ;
 
2621
    TableList *first_table= (TableList*) table_list.first;
 
2622
    for (TableList *tables= first_table ;
3221
2623
         tables ;
3222
2624
         tables=tables->next_local)
3223
2625
    {
3233
2635
  if (table_list.elements > 0)
3234
2636
  {
3235
2637
    /*
3236
 
      table_list.next points to the last inserted TABLE_LIST->next_local'
 
2638
      table_list.next points to the last inserted TableList->next_local'
3237
2639
      element
3238
2640
      We don't use the offsetof() macro here to avoid warnings from gcc
3239
2641
    */
3240
 
    previous_table_ref= (TABLE_LIST*) ((char*) table_list.next -
 
2642
    previous_table_ref= (TableList*) ((char*) table_list.next -
3241
2643
                                       ((char*) &(ptr->next_local) -
3242
2644
                                        (char*) ptr));
3243
2645
    /*
3244
2646
      Set next_name_resolution_table of the previous table reference to point
3245
2647
      to the current table reference. In effect the list
3246
 
      TABLE_LIST::next_name_resolution_table coincides with
3247
 
      TABLE_LIST::next_local. Later this may be changed in
 
2648
      TableList::next_name_resolution_table coincides with
 
2649
      TableList::next_local. Later this may be changed in
3248
2650
      store_top_level_join_columns() for NATURAL/USING joins.
3249
2651
    */
3250
2652
    previous_table_ref->next_name_resolution_table= ptr;
3256
2658
    previous table reference to 'ptr'. Here we also add one element to the
3257
2659
    list 'table_list'.
3258
2660
  */
3259
 
  table_list.link_in_list((uchar*) ptr, (uchar**) &ptr->next_local);
 
2661
  table_list.link_in_list((unsigned char*) ptr, (unsigned char**) &ptr->next_local);
3260
2662
  ptr->next_name_resolution_table= NULL;
3261
2663
  /* Link table in global list (all used tables) */
3262
2664
  lex->add_to_query_tables(ptr);
3267
2669
/**
3268
2670
  Initialize a new table list for a nested join.
3269
2671
 
3270
 
    The function initializes a structure of the TABLE_LIST type
 
2672
    The function initializes a structure of the TableList type
3271
2673
    for a nested join. It sets up its nested join list as empty.
3272
2674
    The created structure is added to the front of the current
3273
2675
    join list in the st_select_lex object. Then the function
3275
2677
    created empty list after having saved the info on the old level
3276
2678
    in the initialized structure.
3277
2679
 
3278
 
  @param thd         current thread
 
2680
  @param session         current thread
3279
2681
 
3280
2682
  @retval
3281
2683
    0   if success
3283
2685
    1   otherwise
3284
2686
*/
3285
2687
 
3286
 
bool st_select_lex::init_nested_join(THD *thd)
 
2688
bool st_select_lex::init_nested_join(Session *session)
3287
2689
{
3288
 
  TABLE_LIST *ptr;
3289
 
  NESTED_JOIN *nested_join;
 
2690
  TableList *ptr;
 
2691
  nested_join_st *nested_join;
3290
2692
 
3291
 
  if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
3292
 
                                       sizeof(NESTED_JOIN))))
 
2693
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2694
                                       sizeof(nested_join_st))))
3293
2695
    return(1);
3294
2696
  nested_join= ptr->nested_join=
3295
 
    ((NESTED_JOIN*) ((uchar*) ptr + ALIGN_SIZE(sizeof(TABLE_LIST))));
 
2697
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
3296
2698
 
3297
2699
  join_list->push_front(ptr);
3298
2700
  ptr->embedding= embedding;
3312
2714
    If the current level contains only one member, the function
3313
2715
    moves it one level up, eliminating the nest.
3314
2716
 
3315
 
  @param thd         current thread
 
2717
  @param session         current thread
3316
2718
 
3317
2719
  @return
3318
 
    - Pointer to TABLE_LIST element added to the total table list, if success
 
2720
    - Pointer to TableList element added to the total table list, if success
3319
2721
    - 0, otherwise
3320
2722
*/
3321
2723
 
3322
 
TABLE_LIST *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
 
2724
TableList *st_select_lex::end_nested_join(Session *)
3323
2725
{
3324
 
  TABLE_LIST *ptr;
3325
 
  NESTED_JOIN *nested_join;
 
2726
  TableList *ptr;
 
2727
  nested_join_st *nested_join;
3326
2728
 
3327
2729
  assert(embedding);
3328
2730
  ptr= embedding;
3331
2733
  nested_join= ptr->nested_join;
3332
2734
  if (nested_join->join_list.elements == 1)
3333
2735
  {
3334
 
    TABLE_LIST *embedded= nested_join->join_list.head();
 
2736
    TableList *embedded= nested_join->join_list.head();
3335
2737
    join_list->pop();
3336
2738
    embedded->join_list= join_list;
3337
2739
    embedded->embedding= embedding;
3352
2754
 
3353
2755
    The function nest last join operation as if it was enclosed in braces.
3354
2756
 
3355
 
  @param thd         current thread
 
2757
  @param session         current thread
3356
2758
 
3357
2759
  @retval
3358
2760
    0  Error
3359
2761
  @retval
3360
 
    \#  Pointer to TABLE_LIST element created for the new nested join
 
2762
    \#  Pointer to TableList element created for the new nested join
3361
2763
*/
3362
2764
 
3363
 
TABLE_LIST *st_select_lex::nest_last_join(THD *thd)
 
2765
TableList *st_select_lex::nest_last_join(Session *session)
3364
2766
{
3365
 
  TABLE_LIST *ptr;
3366
 
  NESTED_JOIN *nested_join;
3367
 
  List<TABLE_LIST> *embedded_list;
 
2767
  TableList *ptr;
 
2768
  nested_join_st *nested_join;
 
2769
  List<TableList> *embedded_list;
3368
2770
 
3369
 
  if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
3370
 
                                       sizeof(NESTED_JOIN))))
 
2771
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2772
                                       sizeof(nested_join_st))))
3371
2773
    return(0);
3372
2774
  nested_join= ptr->nested_join=
3373
 
    ((NESTED_JOIN*) ((uchar*) ptr + ALIGN_SIZE(sizeof(TABLE_LIST))));
 
2775
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
3374
2776
 
3375
2777
  ptr->embedding= embedding;
3376
2778
  ptr->join_list= join_list;
3378
2780
  embedded_list= &nested_join->join_list;
3379
2781
  embedded_list->empty();
3380
2782
 
3381
 
  for (uint i=0; i < 2; i++)
 
2783
  for (uint32_t i=0; i < 2; i++)
3382
2784
  {
3383
 
    TABLE_LIST *table= join_list->pop();
 
2785
    TableList *table= join_list->pop();
3384
2786
    table->join_list= embedded_list;
3385
2787
    table->embedding= ptr;
3386
2788
    embedded_list->push_back(table);
3415
2817
    None
3416
2818
*/
3417
2819
 
3418
 
void st_select_lex::add_joined_table(TABLE_LIST *table)
 
2820
void st_select_lex::add_joined_table(TableList *table)
3419
2821
{
3420
2822
  join_list->push_front(table);
3421
2823
  table->join_list= join_list;
3448
2850
      SELECT * FROM t3 LEFT JOIN (t1 LEFT JOIN t2 ON on_expr2) ON on_expr1
3449
2851
   @endverbatim
3450
2852
 
3451
 
  @param thd         current thread
 
2853
  @param session         current thread
3452
2854
 
3453
2855
  @return
3454
2856
    - Pointer to the table representing the inner table, if success
3455
2857
    - 0, otherwise
3456
2858
*/
3457
2859
 
3458
 
TABLE_LIST *st_select_lex::convert_right_join()
 
2860
TableList *st_select_lex::convert_right_join()
3459
2861
{
3460
 
  TABLE_LIST *tab2= join_list->pop();
3461
 
  TABLE_LIST *tab1= join_list->pop();
 
2862
  TableList *tab2= join_list->pop();
 
2863
  TableList *tab1= join_list->pop();
3462
2864
 
3463
2865
  join_list->push_front(tab2);
3464
2866
  join_list->push_front(tab1);
3482
2884
{
3483
2885
  bool for_update= lock_type >= TL_READ_NO_INSERT;
3484
2886
 
3485
 
  for (TABLE_LIST *tables= (TABLE_LIST*) table_list.first;
 
2887
  for (TableList *tables= (TableList*) table_list.first;
3486
2888
       tables;
3487
2889
       tables= tables->next_local)
3488
2890
  {
3500
2902
    This object is created for any union construct containing a union
3501
2903
    operation and also for any single select union construct of the form
3502
2904
    @verbatim
3503
 
    (SELECT ... ORDER BY order_list [LIMIT n]) ORDER BY ... 
 
2905
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ...
3504
2906
    @endvarbatim
3505
2907
    or of the form
3506
2908
    @varbatim
3507
 
    (SELECT ... ORDER BY LIMIT n) ORDER BY ...
 
2909
    (SELECT ... order_st BY LIMIT n) order_st BY ...
3508
2910
    @endvarbatim
3509
 
  
3510
 
  @param thd_arg                   thread handle
 
2911
 
 
2912
  @param session_arg               thread handle
3511
2913
 
3512
2914
  @note
3513
2915
    The object is used to retrieve rows from the temporary table
3519
2921
    0     on success
3520
2922
*/
3521
2923
 
3522
 
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
 
2924
bool st_select_lex_unit::add_fake_select_lex(Session *session_arg)
3523
2925
{
3524
2926
  SELECT_LEX *first_sl= first_select();
3525
2927
  assert(!fake_select_lex);
3526
2928
 
3527
 
  if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
 
2929
  if (!(fake_select_lex= new (session_arg->mem_root) SELECT_LEX()))
3528
2930
      return(1);
3529
 
  fake_select_lex->include_standalone(this, 
 
2931
  fake_select_lex->include_standalone(this,
3530
2932
                                      (SELECT_LEX_NODE**)&fake_select_lex);
3531
2933
  fake_select_lex->select_number= INT_MAX;
3532
 
  fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
 
2934
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3533
2935
  fake_select_lex->make_empty_select();
3534
2936
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3535
2937
  fake_select_lex->select_limit= 0;
3536
2938
 
3537
2939
  fake_select_lex->context.outer_context=first_sl->context.outer_context;
3538
 
  /* allow item list resolving in fake select for ORDER BY */
 
2940
  /* allow item list resolving in fake select for order_st BY */
3539
2941
  fake_select_lex->context.resolve_in_select_list= true;
3540
2942
  fake_select_lex->context.select_lex= fake_select_lex;
3541
2943
 
3542
2944
  if (!is_union())
3543
2945
  {
3544
 
    /* 
3545
 
      This works only for 
3546
 
      (SELECT ... ORDER BY list [LIMIT n]) ORDER BY order_list [LIMIT m],
3547
 
      (SELECT ... LIMIT n) ORDER BY order_list [LIMIT m]
 
2946
    /*
 
2947
      This works only for
 
2948
      (SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
 
2949
      (SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
3548
2950
      just before the parser starts processing order_list
3549
 
    */ 
 
2951
    */
3550
2952
    global_parameters= fake_select_lex;
3551
2953
    fake_select_lex->no_table_names_allowed= 1;
3552
 
    thd_arg->lex->current_select= fake_select_lex;
 
2954
    session_arg->lex->current_select= fake_select_lex;
3553
2955
  }
3554
 
  thd_arg->lex->pop_context();
 
2956
  session_arg->lex->pop_context();
3555
2957
  return(0);
3556
2958
}
3557
2959
 
3565
2967
    to be used for name resolution, and push the newly created
3566
2968
    context to the stack of contexts of the query.
3567
2969
 
3568
 
  @param thd       pointer to current thread
 
2970
  @param session       pointer to current thread
3569
2971
  @param left_op   left  operand of the JOIN
3570
2972
  @param right_op  rigth operand of the JOIN
3571
2973
 
3576
2978
*/
3577
2979
 
3578
2980
bool
3579
 
push_new_name_resolution_context(THD *thd,
3580
 
                                 TABLE_LIST *left_op, TABLE_LIST *right_op)
 
2981
push_new_name_resolution_context(Session *session,
 
2982
                                 TableList *left_op, TableList *right_op)
3581
2983
{
3582
2984
  Name_resolution_context *on_context;
3583
 
  if (!(on_context= new (thd->mem_root) Name_resolution_context))
 
2985
  if (!(on_context= new (session->mem_root) Name_resolution_context))
3584
2986
    return true;
3585
2987
  on_context->init();
3586
2988
  on_context->first_name_resolution_table=
3587
2989
    left_op->first_leaf_for_name_resolution();
3588
2990
  on_context->last_name_resolution_table=
3589
2991
    right_op->last_leaf_for_name_resolution();
3590
 
  return thd->lex->push_context(on_context);
 
2992
  return session->lex->push_context(on_context);
3591
2993
}
3592
2994
 
3593
2995
 
3605
3007
    true   if all is OK
3606
3008
*/
3607
3009
 
3608
 
void add_join_on(TABLE_LIST *b, Item *expr)
 
3010
void add_join_on(TableList *b, Item *expr)
3609
3011
{
3610
3012
  if (expr)
3611
3013
  {
3658
3060
  @param using_fields    Field names from USING clause
3659
3061
*/
3660
3062
 
3661
 
void add_join_natural(TABLE_LIST *a, TABLE_LIST *b, List<String> *using_fields,
 
3063
void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
3662
3064
                      SELECT_LEX *lex)
3663
3065
{
3664
3066
  b->natural_join= a;
3669
3071
/**
3670
3072
  Reload/resets privileges and the different caches.
3671
3073
 
3672
 
  @param thd Thread handler (can be NULL!)
 
3074
  @param session Thread handler (can be NULL!)
3673
3075
  @param options What should be reset/reloaded (tables, privileges, slave...)
3674
3076
  @param tables Tables to flush (if any)
3675
3077
  @param write_to_binlog True if we can write to the binlog.
3676
 
               
 
3078
 
3677
3079
  @note Depending on 'options', it may be very bad to write the
3678
3080
    query to the binlog (e.g. FLUSH SLAVE); this is a
3679
3081
    pointer where reload_cache() will put 0 if
3682
3084
 
3683
3085
  @return Error status code
3684
3086
    @retval 0 Ok
3685
 
    @retval !=0  Error; thd->killed is set or thd->is_error() is true
 
3087
    @retval !=0  Error; session->killed is set or session->is_error() is true
3686
3088
*/
3687
3089
 
3688
 
bool reload_cache(THD *thd, ulong options, TABLE_LIST *tables,
 
3090
bool reload_cache(Session *session, ulong options, TableList *tables,
3689
3091
                          bool *write_to_binlog)
3690
3092
{
3691
3093
  bool result=0;
3692
3094
  select_errors=0;                              /* Write if more errors */
3693
3095
  bool tmp_write_to_binlog= 1;
3694
3096
 
3695
 
  assert(!thd || !thd->in_sub_stmt);
3696
 
 
3697
3097
  if (options & REFRESH_LOG)
3698
3098
  {
3699
3099
    /*
3709
3109
      than it would help them)
3710
3110
    */
3711
3111
    tmp_write_to_binlog= 0;
3712
 
    if( mysql_bin_log.is_open() )
3713
 
    {
3714
 
      mysql_bin_log.rotate_and_purge(RP_FORCE_ROTATE);
3715
 
    }
3716
 
    pthread_mutex_lock(&LOCK_active_mi);
3717
 
    rotate_relay_log(active_mi);
3718
 
    pthread_mutex_unlock(&LOCK_active_mi);
3719
 
 
3720
 
    /* flush slow and general logs */
3721
 
    logger.flush_logs(thd);
3722
3112
 
3723
3113
    if (ha_flush_logs(NULL))
3724
3114
      result=1;
3725
 
    if (flush_error_log())
3726
 
      result=1;
3727
3115
  }
3728
3116
  /*
3729
3117
    Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
3730
3118
    (see sql_yacc.yy)
3731
3119
  */
3732
 
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK)) 
 
3120
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3733
3121
  {
3734
 
    if ((options & REFRESH_READ_LOCK) && thd)
 
3122
    if ((options & REFRESH_READ_LOCK) && session)
3735
3123
    {
3736
3124
      /*
3737
3125
        We must not try to aspire a global read lock if we have a write
3738
3126
        locked table. This would lead to a deadlock when trying to
3739
3127
        reopen (and re-lock) the table after the flush.
3740
3128
      */
3741
 
      if (thd->locked_tables)
 
3129
      if (session->locked_tables)
3742
3130
      {
3743
 
        THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
3744
 
        THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
 
3131
        THR_LOCK_DATA **lock_p= session->locked_tables->locks;
 
3132
        THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
3745
3133
 
3746
3134
        for (; lock_p < end_p; lock_p++)
3747
3135
        {
3757
3145
        UNLOCK TABLES
3758
3146
      */
3759
3147
      tmp_write_to_binlog= 0;
3760
 
      if (lock_global_read_lock(thd))
 
3148
      if (lock_global_read_lock(session))
3761
3149
        return 1;                               // Killed
3762
 
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
 
3150
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3763
3151
                                  false : true, true);
3764
 
      if (make_global_read_lock_block_commit(thd)) // Killed
 
3152
      if (make_global_read_lock_block_commit(session)) // Killed
3765
3153
      {
3766
3154
        /* Don't leave things in a half-locked state */
3767
 
        unlock_global_read_lock(thd);
 
3155
        unlock_global_read_lock(session);
3768
3156
        return 1;
3769
3157
      }
3770
3158
    }
3771
3159
    else
3772
 
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
 
3160
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
3773
3161
                                  false : true, false);
3774
 
    my_dbopt_cleanup();
3775
 
  }
3776
 
  if (thd && (options & REFRESH_STATUS))
3777
 
    refresh_status(thd);
3778
 
  if (options & REFRESH_THREADS)
3779
 
    flush_thread_cache();
3780
 
  if (options & REFRESH_MASTER)
3781
 
  {
3782
 
    assert(thd);
3783
 
    tmp_write_to_binlog= 0;
3784
 
    if (reset_master(thd))
3785
 
    {
3786
 
      result=1;
3787
 
    }
3788
 
  }
3789
 
 if (options & REFRESH_SLAVE)
3790
 
 {
3791
 
   tmp_write_to_binlog= 0;
3792
 
   pthread_mutex_lock(&LOCK_active_mi);
3793
 
   if (reset_slave(thd, active_mi))
3794
 
     result=1;
3795
 
   pthread_mutex_unlock(&LOCK_active_mi);
3796
 
 }
 
3162
  }
 
3163
  if (session && (options & REFRESH_STATUS))
 
3164
    refresh_status(session);
3797
3165
 *write_to_binlog= tmp_write_to_binlog;
3798
3166
 return result;
3799
3167
}
3802
3170
/**
3803
3171
  kill on thread.
3804
3172
 
3805
 
  @param thd                    Thread class
 
3173
  @param session                        Thread class
3806
3174
  @param id                     Thread id
3807
3175
  @param only_kill_query        Should it kill the query or the connection
3808
3176
 
3811
3179
*/
3812
3180
 
3813
3181
static unsigned int
3814
 
kill_one_thread(THD *thd __attribute__((unused)),
3815
 
                ulong id, bool only_kill_query)
 
3182
kill_one_thread(Session *, ulong id, bool only_kill_query)
3816
3183
{
3817
 
  THD *tmp;
3818
 
  uint error=ER_NO_SUCH_THREAD;
3819
 
  VOID(pthread_mutex_lock(&LOCK_thread_count)); // For unlink from list
3820
 
  I_List_iterator<THD> it(threads);
 
3184
  Session *tmp;
 
3185
  uint32_t error=ER_NO_SUCH_THREAD;
 
3186
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
3187
  I_List_iterator<Session> it(threads);
3821
3188
  while ((tmp=it++))
3822
3189
  {
3823
3190
    if (tmp->command == COM_DAEMON)
3828
3195
      break;
3829
3196
    }
3830
3197
  }
3831
 
  VOID(pthread_mutex_unlock(&LOCK_thread_count));
 
3198
  pthread_mutex_unlock(&LOCK_thread_count);
3832
3199
  if (tmp)
3833
3200
  {
3834
 
    tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
 
3201
    tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
3835
3202
    error=0;
3836
3203
    pthread_mutex_unlock(&tmp->LOCK_delete);
3837
3204
  }
3844
3211
 
3845
3212
  SYNOPSIS
3846
3213
    sql_kill()
3847
 
    thd                 Thread class
 
3214
    session                     Thread class
3848
3215
    id                  Thread id
3849
3216
    only_kill_query     Should it kill the query or the connection
3850
3217
*/
3851
3218
 
3852
 
void sql_kill(THD *thd, ulong id, bool only_kill_query)
 
3219
void sql_kill(Session *session, ulong id, bool only_kill_query)
3853
3220
{
3854
 
  uint error;
3855
 
  if (!(error= kill_one_thread(thd, id, only_kill_query)))
3856
 
    my_ok(thd);
 
3221
  uint32_t error;
 
3222
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
3223
    session->my_ok();
3857
3224
  else
3858
3225
    my_error(error, MYF(0), id);
3859
3226
}
3861
3228
 
3862
3229
/** If pointer is not a null pointer, append filename to it. */
3863
3230
 
3864
 
bool append_file_to_dir(THD *thd, const char **filename_ptr,
 
3231
bool append_file_to_dir(Session *session, const char **filename_ptr,
3865
3232
                        const char *table_name)
3866
3233
{
3867
3234
  char buff[FN_REFLEN],*ptr, *end;
3876
3243
    return 1;
3877
3244
  }
3878
3245
  /* Fix is using unix filename format on dos */
3879
 
  stpcpy(buff,*filename_ptr);
3880
 
  end=convert_dirname(buff, *filename_ptr, NullS);
3881
 
  if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
 
3246
  strcpy(buff,*filename_ptr);
 
3247
  end=convert_dirname(buff, *filename_ptr, NULL);
 
3248
  if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3882
3249
    return 1;                                   // End of memory
3883
3250
  *filename_ptr=ptr;
3884
 
  strxmov(ptr,buff,table_name,NullS);
 
3251
  sprintf(ptr,"%s%s",buff,table_name);
3885
3252
  return 0;
3886
3253
}
3887
3254
 
3897
3264
 
3898
3265
bool check_simple_select()
3899
3266
{
3900
 
  THD *thd= current_thd;
3901
 
  LEX *lex= thd->lex;
 
3267
  Session *session= current_session;
 
3268
  LEX *lex= session->lex;
3902
3269
  if (lex->current_select != &lex->select_lex)
3903
3270
  {
3904
3271
    char command[80];
3905
 
    Lex_input_stream *lip= thd->m_lip;
3906
 
    strmake(command, lip->yylval->symbol.str,
3907
 
            min((ulong)lip->yylval->symbol.length, sizeof(command)-1));
 
3272
    Lex_input_stream *lip= session->m_lip;
 
3273
    strncpy(command, lip->yylval->symbol.str,
 
3274
            cmin(lip->yylval->symbol.length, sizeof(command)-1));
 
3275
    command[cmin(lip->yylval->symbol.length, sizeof(command)-1)]=0;
3908
3276
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3909
3277
    return 1;
3910
3278
  }
3912
3280
}
3913
3281
 
3914
3282
 
3915
 
Comp_creator *comp_eq_creator(bool invert)
3916
 
{
3917
 
  return invert?(Comp_creator *)&ne_creator:(Comp_creator *)&eq_creator;
3918
 
}
3919
 
 
3920
 
 
3921
 
Comp_creator *comp_ge_creator(bool invert)
3922
 
{
3923
 
  return invert?(Comp_creator *)&lt_creator:(Comp_creator *)&ge_creator;
3924
 
}
3925
 
 
3926
 
 
3927
 
Comp_creator *comp_gt_creator(bool invert)
3928
 
{
3929
 
  return invert?(Comp_creator *)&le_creator:(Comp_creator *)&gt_creator;
3930
 
}
3931
 
 
3932
 
 
3933
 
Comp_creator *comp_le_creator(bool invert)
3934
 
{
3935
 
  return invert?(Comp_creator *)&gt_creator:(Comp_creator *)&le_creator;
3936
 
}
3937
 
 
3938
 
 
3939
 
Comp_creator *comp_lt_creator(bool invert)
3940
 
{
3941
 
  return invert?(Comp_creator *)&ge_creator:(Comp_creator *)&lt_creator;
3942
 
}
3943
 
 
3944
 
 
3945
 
Comp_creator *comp_ne_creator(bool invert)
3946
 
{
3947
 
  return invert?(Comp_creator *)&eq_creator:(Comp_creator *)&ne_creator;
3948
 
}
3949
 
 
3950
 
 
3951
3283
/**
3952
3284
  Construct ALL/ANY/SOME subquery Item.
3953
3285
 
3960
3292
    constructed Item (or 0 if out of memory)
3961
3293
*/
3962
3294
Item * all_any_subquery_creator(Item *left_expr,
3963
 
                                chooser_compare_func_creator cmp,
3964
 
                                bool all,
3965
 
                                SELECT_LEX *select_lex)
 
3295
                                chooser_compare_func_creator cmp,
 
3296
                                bool all,
 
3297
                                SELECT_LEX *select_lex)
3966
3298
{
3967
3299
  if ((cmp == &comp_eq_creator) && !all)       //  = ANY <=> IN
3968
3300
    return new Item_in_subselect(left_expr, select_lex);
3980
3312
 
3981
3313
 
3982
3314
/**
3983
 
  Multi update query pre-check.
 
3315
  Update query pre-check.
3984
3316
 
3985
 
  @param thd            Thread handler
 
3317
  @param session                Thread handler
3986
3318
  @param tables Global/local table list (have to be the same)
3987
3319
 
3988
3320
  @retval
3991
3323
    true  Error
3992
3324
*/
3993
3325
 
3994
 
bool multi_update_precheck(THD *thd,
3995
 
                           TABLE_LIST *tables __attribute__((unused)))
 
3326
bool update_precheck(Session *session, TableList *)
3996
3327
{
3997
3328
  const char *msg= 0;
3998
 
  LEX *lex= thd->lex;
 
3329
  LEX *lex= session->lex;
3999
3330
  SELECT_LEX *select_lex= &lex->select_lex;
4000
3331
 
4001
 
  if (select_lex->item_list.elements != lex->value_list.elements)
 
3332
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
4002
3333
  {
4003
3334
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
4004
3335
    return(true);
4005
3336
  }
4006
3337
 
4007
 
  if (select_lex->order_list.elements)
4008
 
    msg= "ORDER BY";
4009
 
  else if (select_lex->select_limit)
4010
 
    msg= "LIMIT";
4011
 
  if (msg)
 
3338
  if (session->lex->select_lex.table_list.elements > 1)
4012
3339
  {
4013
 
    my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
4014
 
    return(true);
 
3340
    if (select_lex->order_list.elements)
 
3341
      msg= "ORDER BY";
 
3342
    else if (select_lex->select_limit)
 
3343
      msg= "LIMIT";
 
3344
    if (msg)
 
3345
    {
 
3346
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
 
3347
      return(true);
 
3348
    }
4015
3349
  }
4016
3350
  return(false);
4017
3351
}
4019
3353
/**
4020
3354
  Multi delete query pre-check.
4021
3355
 
4022
 
  @param thd                    Thread handler
 
3356
  @param session                        Thread handler
4023
3357
  @param tables         Global/local table list
4024
3358
 
4025
3359
  @retval
4028
3362
    true  error
4029
3363
*/
4030
3364
 
4031
 
bool multi_delete_precheck(THD *thd,
4032
 
                           TABLE_LIST *tables __attribute__((unused)))
 
3365
bool multi_delete_precheck(Session *session, TableList *)
4033
3366
{
4034
 
  SELECT_LEX *select_lex= &thd->lex->select_lex;
4035
 
  TABLE_LIST **save_query_tables_own_last= thd->lex->query_tables_own_last;
4036
 
 
4037
 
  thd->lex->query_tables_own_last= 0;
4038
 
  thd->lex->query_tables_own_last= save_query_tables_own_last;
4039
 
 
4040
 
  if ((thd->options & OPTION_SAFE_UPDATES) && !select_lex->where)
 
3367
  SELECT_LEX *select_lex= &session->lex->select_lex;
 
3368
  TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
 
3369
 
 
3370
  session->lex->query_tables_own_last= 0;
 
3371
  session->lex->query_tables_own_last= save_query_tables_own_last;
 
3372
 
 
3373
  if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
4041
3374
  {
4042
3375
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
4043
3376
               ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
4064
3397
  @return Matching table, NULL otherwise.
4065
3398
*/
4066
3399
 
4067
 
static TABLE_LIST *multi_delete_table_match(LEX *lex __attribute__((unused)),
4068
 
                                            TABLE_LIST *tbl,
4069
 
                                            TABLE_LIST *tables)
 
3400
static TableList *multi_delete_table_match(LEX *, TableList *tbl,
 
3401
                                           TableList *tables)
4070
3402
{
4071
 
  TABLE_LIST *match= NULL;
 
3403
  TableList *match= NULL;
4072
3404
 
4073
 
  for (TABLE_LIST *elem= tables; elem; elem= elem->next_local)
 
3405
  for (TableList *elem= tables; elem; elem= elem->next_local)
4074
3406
  {
4075
3407
    int cmp;
4076
3408
 
4118
3450
 
4119
3451
bool multi_delete_set_locks_and_link_aux_tables(LEX *lex)
4120
3452
{
4121
 
  TABLE_LIST *tables= (TABLE_LIST*)lex->select_lex.table_list.first;
4122
 
  TABLE_LIST *target_tbl;
 
3453
  TableList *tables= (TableList*)lex->select_lex.table_list.first;
 
3454
  TableList *target_tbl;
4123
3455
 
4124
3456
  lex->table_count= 0;
4125
3457
 
4126
 
  for (target_tbl= (TABLE_LIST *)lex->auxiliary_table_list.first;
 
3458
  for (target_tbl= (TableList *)lex->auxiliary_table_list.first;
4127
3459
       target_tbl; target_tbl= target_tbl->next_local)
4128
3460
  {
4129
3461
    lex->table_count++;
4130
3462
    /* All tables in aux_tables must be found in FROM PART */
4131
 
    TABLE_LIST *walk= multi_delete_table_match(lex, target_tbl, tables);
 
3463
    TableList *walk= multi_delete_table_match(lex, target_tbl, tables);
4132
3464
    if (!walk)
4133
3465
      return(true);
4134
3466
    if (!walk->derived)
4145
3477
 
4146
3478
 
4147
3479
/**
4148
 
  simple UPDATE query pre-check.
4149
 
 
4150
 
  @param thd            Thread handler
4151
 
  @param tables Global table list
4152
 
 
4153
 
  @retval
4154
 
    false OK
4155
 
  @retval
4156
 
    true  Error
4157
 
*/
4158
 
 
4159
 
bool update_precheck(THD *thd, TABLE_LIST *tables __attribute__((unused)))
4160
 
{
4161
 
  if (thd->lex->select_lex.item_list.elements != thd->lex->value_list.elements)
4162
 
  {
4163
 
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
4164
 
    return(true);
4165
 
  }
4166
 
  return(false);
4167
 
}
4168
 
 
4169
 
 
4170
 
/**
4171
3480
  simple INSERT query pre-check.
4172
3481
 
4173
 
  @param thd            Thread handler
 
3482
  @param session                Thread handler
4174
3483
  @param tables Global table list
4175
3484
 
4176
3485
  @retval
4179
3488
    true   error
4180
3489
*/
4181
3490
 
4182
 
bool insert_precheck(THD *thd, TABLE_LIST *tables __attribute__((unused)))
 
3491
bool insert_precheck(Session *session, TableList *)
4183
3492
{
4184
 
  LEX *lex= thd->lex;
 
3493
  LEX *lex= session->lex;
4185
3494
 
4186
3495
  /*
4187
3496
    Check that we have modify privileges for the first table and
4199
3508
/**
4200
3509
  CREATE TABLE query pre-check.
4201
3510
 
4202
 
  @param thd                    Thread handler
 
3511
  @param session                        Thread handler
4203
3512
  @param tables         Global table list
4204
3513
  @param create_table           Table which will be created
4205
3514
 
4209
3518
    true   Error
4210
3519
*/
4211
3520
 
4212
 
bool create_table_precheck(THD *thd,
4213
 
                           TABLE_LIST *tables __attribute__((unused)),
4214
 
                           TABLE_LIST *create_table)
 
3521
bool create_table_precheck(Session *, TableList *,
 
3522
                           TableList *create_table)
4215
3523
{
4216
 
  LEX *lex= thd->lex;
4217
 
  SELECT_LEX *select_lex= &lex->select_lex;
4218
3524
  bool error= true;                                 // Error message is given
4219
3525
 
4220
3526
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
4221
3527
  {
4222
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
 
3528
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
4223
3529
    return(true);
4224
3530
  }
4225
3531
 
4226
 
  if (select_lex->item_list.elements)
4227
 
  {
4228
 
    /* Check permissions for used tables in CREATE TABLE ... SELECT */
4229
 
 
4230
 
#ifdef NOT_NECESSARY_TO_CHECK_CREATE_TABLE_EXIST_WHEN_PREPARING_STATEMENT
4231
 
    /* This code throws an ill error for CREATE TABLE t1 SELECT * FROM t1 */
4232
 
    /*
4233
 
      Only do the check for PS, because we on execute we have to check that
4234
 
      against the opened tables to ensure we don't use a table that is part
4235
 
      of the view (which can only be done after the table has been opened).
4236
 
    */
4237
 
    if (thd->stmt_arena->is_stmt_prepare_or_first_sp_execute())
4238
 
    {
4239
 
      /*
4240
 
        For temporary tables we don't have to check if the created table exists
4241
 
      */
4242
 
      if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) &&
4243
 
          find_table_in_global_list(tables, create_table->db,
4244
 
                                    create_table->table_name))
4245
 
      {
4246
 
        error= false;
4247
 
        goto err;
4248
 
      }
4249
 
    }
4250
 
#endif
4251
 
  }
4252
3532
  error= false;
4253
3533
 
4254
3534
  return(error);
4258
3538
/**
4259
3539
  negate given expression.
4260
3540
 
4261
 
  @param thd  thread handler
 
3541
  @param session  thread handler
4262
3542
  @param expr expression for negation
4263
3543
 
4264
3544
  @return
4265
3545
    negated expression
4266
3546
*/
4267
3547
 
4268
 
Item *negate_expression(THD *thd, Item *expr)
 
3548
Item *negate_expression(Session *session, Item *expr)
4269
3549
{
4270
3550
  Item *negated;
4271
3551
  if (expr->type() == Item::FUNC_ITEM &&
4273
3553
  {
4274
3554
    /* it is NOT(NOT( ... )) */
4275
3555
    Item *arg= ((Item_func *) expr)->arguments()[0];
4276
 
    enum_parsing_place place= thd->lex->current_select->parsing_place;
 
3556
    enum_parsing_place place= session->lex->current_select->parsing_place;
4277
3557
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
4278
3558
      return arg;
4279
3559
    /*
4283
3563
    return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
4284
3564
  }
4285
3565
 
4286
 
  if ((negated= expr->neg_transformer(thd)) != 0)
 
3566
  if ((negated= expr->neg_transformer(session)) != 0)
4287
3567
    return negated;
4288
3568
  return new Item_func_not(expr);
4289
3569
}
4290
3570
 
4291
3571
 
4292
 
/**
4293
 
  Check that byte length of a string does not exceed some limit.
4294
 
 
4295
 
  @param str         string to be checked
4296
 
  @param err_msg     error message to be displayed if the string is too long
4297
 
  @param max_length  max length
4298
 
 
4299
 
  @retval
4300
 
    false   the passed string is not longer than max_length
4301
 
  @retval
4302
 
    true    the passed string is longer than max_length
4303
 
 
4304
 
  NOTE
4305
 
    The function is not used in existing code but can be useful later?
4306
 
*/
4307
 
 
4308
 
bool check_string_byte_length(LEX_STRING *str, const char *err_msg,
4309
 
                              uint max_byte_length)
4310
 
{
4311
 
  if (str->length <= max_byte_length)
4312
 
    return false;
4313
 
 
4314
 
  my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
4315
 
 
4316
 
  return true;
4317
 
}
4318
 
 
4319
 
 
4320
3572
/*
4321
3573
  Check that char length of a string does not exceed some limit.
4322
3574
 
4334
3586
 
4335
3587
 
4336
3588
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
4337
 
                              uint max_char_length, const CHARSET_INFO * const cs,
 
3589
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
4338
3590
                              bool no_error)
4339
3591
{
4340
3592
  int well_formed_error;
4341
 
  uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
 
3593
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4342
3594
                                      max_char_length, &well_formed_error);
4343
3595
 
4344
3596
  if (!well_formed_error &&  str->length == res)
4350
3602
}
4351
3603
 
4352
3604
 
4353
 
bool check_identifier_name(LEX_STRING *str, uint max_char_length,
4354
 
                           uint err_code, const char *param_for_err_msg)
 
3605
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
 
3606
                           uint32_t max_char_length,
 
3607
                           const char *param_for_err_msg)
4355
3608
{
4356
 
#ifdef HAVE_CHARSET_utf8mb3
4357
3609
  /*
4358
3610
    We don't support non-BMP characters in identifiers at the moment,
4359
3611
    so they should be prohibited until such support is done.
4360
3612
    This is why we use the 3-byte utf8 to check well-formedness here.
4361
3613
  */
4362
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
4363
 
#else
4364
 
  const CHARSET_INFO * const cs= system_charset_info;
4365
 
#endif
 
3614
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
3615
 
4366
3616
  int well_formed_error;
4367
 
  uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
 
3617
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4368
3618
                                      max_char_length, &well_formed_error);
4369
3619
 
4370
3620
  if (well_formed_error)
4372
3622
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
4373
3623
    return true;
4374
3624
  }
4375
 
  
 
3625
 
4376
3626
  if (str->length == res)
4377
3627
    return false;
4378
3628
 
4404
3654
 
4405
3655
  RETURN VALUES
4406
3656
    0   ok
4407
 
    1   error  
 
3657
    1   error
4408
3658
*/
4409
3659
 
4410
3660
bool test_if_data_home_dir(const char *dir)
4411
3661
{
4412
3662
  char path[FN_REFLEN], conv_path[FN_REFLEN];
4413
 
  uint dir_len, home_dir_len= strlen(mysql_unpacked_real_data_home);
 
3663
  uint32_t dir_len, home_dir_len= strlen(drizzle_unpacked_real_data_home);
4414
3664
 
4415
3665
  if (!dir)
4416
3666
    return(0);
4422
3672
  if (home_dir_len < dir_len)
4423
3673
  {
4424
3674
    if (!my_strnncoll(character_set_filesystem,
4425
 
                      (const uchar*) conv_path, home_dir_len,
4426
 
                      (const uchar*) mysql_unpacked_real_data_home,
 
3675
                      (const unsigned char*) conv_path, home_dir_len,
 
3676
                      (const unsigned char*) drizzle_unpacked_real_data_home,
4427
3677
                      home_dir_len))
4428
3678
      return(1);
4429
3679
  }
4431
3681
}
4432
3682
 
4433
3683
 
4434
 
extern int MYSQLparse(void *thd); // from sql_yacc.cc
 
3684
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
4435
3685
 
4436
3686
 
4437
3687
/**
4438
 
  This is a wrapper of MYSQLparse(). All the code should call parse_sql()
4439
 
  instead of MYSQLparse().
 
3688
  This is a wrapper of DRIZZLEparse(). All the code should call parse_sql()
 
3689
  instead of DRIZZLEparse().
4440
3690
 
4441
 
  @param thd Thread context.
 
3691
  @param session Thread context.
4442
3692
  @param lip Lexer context.
4443
3693
 
4444
3694
  @return Error status.
4446
3696
    @retval true on parsing error.
4447
3697
*/
4448
3698
 
4449
 
bool parse_sql(THD *thd, Lex_input_stream *lip)
 
3699
bool parse_sql(Session *session, Lex_input_stream *lip)
4450
3700
{
4451
 
  assert(thd->m_lip == NULL);
 
3701
  assert(session->m_lip == NULL);
4452
3702
 
4453
3703
  /* Set Lex_input_stream. */
4454
3704
 
4455
 
  thd->m_lip= lip;
 
3705
  session->m_lip= lip;
4456
3706
 
4457
3707
  /* Parse the query. */
4458
3708
 
4459
 
  bool mysql_parse_status= MYSQLparse(thd) != 0;
4460
 
 
4461
 
  /* Check that if MYSQLparse() failed, thd->is_error() is set. */
4462
 
 
4463
 
  assert(!mysql_parse_status || thd->is_error());
 
3709
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
 
3710
 
 
3711
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
 
3712
 
 
3713
  assert(!mysql_parse_status || session->is_error());
4464
3714
 
4465
3715
  /* Reset Lex_input_stream. */
4466
3716
 
4467
 
  thd->m_lip= NULL;
 
3717
  session->m_lip= NULL;
4468
3718
 
4469
3719
  /* That's it. */
4470
3720
 
4471
 
  return mysql_parse_status || thd->is_fatal_error;
 
3721
  return mysql_parse_status || session->is_fatal_error;
4472
3722
}
4473
3723
 
4474
3724
/**