~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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