~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Stewart Smith
  • Date: 2009-06-16 03:02:59 UTC
  • mto: This revision was merged to the branch mainline in revision 1065.
  • Revision ID: stewart@flamingspork.com-20090616030259-tn2thqrajk6cappd
ER_NISAMCHK is unused, mark it as so. Thanks to Paul DuBois for researching this for MySQL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#define DRIZZLE_LEX 1
17
17
#include <drizzled/server_includes.h>
18
 
#include "sql_repl.h"
19
 
#include "rpl_filter.h"
20
 
#include "repl_failsafe.h"
21
 
#include "logging.h"
22
 
#include <drizzled/drizzled_error_messages.h>
 
18
#include <mysys/hash.h>
 
19
#include <drizzled/logging.h>
 
20
#include <drizzled/db.h>
 
21
#include <drizzled/error.h>
 
22
#include <drizzled/nested_join.h>
 
23
#include <drizzled/query_id.h>
 
24
#include <drizzled/sql_parse.h>
 
25
#include <drizzled/data_home.h>
 
26
#include <drizzled/sql_base.h>
 
27
#include <drizzled/show.h>
 
28
#include <drizzled/rename.h>
 
29
#include <drizzled/function/time/unix_timestamp.h>
 
30
#include <drizzled/function/get_system_var.h>
 
31
#include <drizzled/item/cmpfunc.h>
 
32
#include <drizzled/item/null.h>
 
33
#include <drizzled/session.h>
 
34
#include <drizzled/sql_load.h>
 
35
#include <drizzled/connect.h>
 
36
#include <drizzled/lock.h>
 
37
#include <drizzled/select_send.h>
 
38
#include <bitset>
 
39
 
 
40
using namespace std;
 
41
 
 
42
/* Prototypes */
 
43
static bool append_file_to_dir(Session *session, const char **filename_ptr,
 
44
                               const char *table_name);
23
45
 
24
46
/**
25
47
  @defgroup Runtime_Environment Runtime Environment
26
48
  @{
27
49
*/
28
50
 
29
 
 
 
51
extern size_t my_thread_stack_size;
 
52
extern const CHARSET_INFO *character_set_filesystem;
30
53
const char *any_db="*any*";     // Special symbol for check_access
31
54
 
32
55
const LEX_STRING command_name[COM_END+1]={
34
57
  { C_STRING_WITH_LEN("Quit") },
35
58
  { C_STRING_WITH_LEN("Init DB") },
36
59
  { C_STRING_WITH_LEN("Query") },
37
 
  { C_STRING_WITH_LEN("Field List") },
38
 
  { C_STRING_WITH_LEN("Create DB") },
39
 
  { C_STRING_WITH_LEN("Drop DB") },
40
 
  { C_STRING_WITH_LEN("Refresh") },
41
60
  { C_STRING_WITH_LEN("Shutdown") },
42
 
  { C_STRING_WITH_LEN("Processlist") },
43
61
  { C_STRING_WITH_LEN("Connect") },
44
 
  { C_STRING_WITH_LEN("Kill") },
45
62
  { C_STRING_WITH_LEN("Ping") },
46
 
  { C_STRING_WITH_LEN("Time") },
47
 
  { C_STRING_WITH_LEN("Change user") },
48
 
  { C_STRING_WITH_LEN("Binlog Dump") },
49
 
  { C_STRING_WITH_LEN("Connect Out") },
50
 
  { C_STRING_WITH_LEN("Register Slave") },
51
 
  { C_STRING_WITH_LEN("Set option") },
52
 
  { C_STRING_WITH_LEN("Daemon") },
53
63
  { C_STRING_WITH_LEN("Error") }  // Last command number
54
64
};
55
65
 
57
67
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
58
68
};
59
69
 
60
 
static void unlock_locked_tables(THD *thd)
61
 
{
62
 
  if (thd->locked_tables)
63
 
  {
64
 
    thd->lock=thd->locked_tables;
65
 
    thd->locked_tables=0;                       // Will be automatically closed
66
 
    close_thread_tables(thd);                   // Free tables
67
 
  }
68
 
}
69
 
 
70
 
 
71
 
bool end_active_trans(THD *thd)
72
 
{
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)
80
 
  {
81
 
    my_error(ER_XAER_RMFAIL, MYF(0),
82
 
             xa_state_names[thd->transaction.xid_state.xa_state]);
83
 
    return(1);
84
 
  }
85
 
  if (thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN |
86
 
                      OPTION_TABLE_LOCK))
87
 
  {
88
 
    /* Safety if one did "drop table" on locked tables */
89
 
    if (!thd->locked_tables)
90
 
      thd->options&= ~OPTION_TABLE_LOCK;
91
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
92
 
    if (ha_commit(thd))
93
 
      error=1;
94
 
  }
95
 
  thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
96
 
  thd->transaction.all.modified_non_trans_table= false;
97
 
  return(error);
98
 
}
99
 
 
100
 
 
101
 
bool begin_trans(THD *thd)
102
 
{
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))
116
 
    error= -1;
117
 
  else
118
 
  {
119
 
    LEX *lex= thd->lex;
120
 
    thd->options|= OPTION_BEGIN;
121
 
    thd->server_status|= SERVER_STATUS_IN_TRANS;
122
 
    if (lex->start_transaction_opt & DRIZZLE_START_TRANS_OPT_WITH_CONS_SNAPSHOT)
123
 
      error= ha_start_consistent_snapshot(thd);
124
 
  }
125
 
  return error;
126
 
}
127
 
 
128
 
/**
129
 
  Returns true if all tables should be ignored.
130
 
*/
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
 
 
150
 
 
151
70
/**
152
71
  Mark all commands that somehow changes a table.
153
72
 
160
79
     2  - query that returns meaningful ROW_COUNT() -
161
80
          a number of modified rows
162
81
*/
163
 
 
164
 
uint32_t sql_command_flags[SQLCOM_END+1];
 
82
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
165
83
 
166
84
void init_update_queries(void)
167
85
{
168
 
  memset(&sql_command_flags, 0, sizeof(sql_command_flags));
 
86
  uint32_t x;
 
87
 
 
88
  for (x= 0; x <= SQLCOM_END; x++)
 
89
    sql_command_flags[x].reset();
169
90
 
170
91
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
171
92
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
193
114
  sql_command_flags[SQLCOM_SHOW_FIELDS]=      CF_STATUS_COMMAND;
194
115
  sql_command_flags[SQLCOM_SHOW_KEYS]=        CF_STATUS_COMMAND;
195
116
  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;
201
117
  sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
202
118
  sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
203
119
  sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
204
120
  sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
205
121
  sql_command_flags[SQLCOM_SHOW_CREATE_DB]=  CF_STATUS_COMMAND;
206
122
  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;
209
123
 
210
124
   sql_command_flags[SQLCOM_SHOW_TABLES]=       (CF_STATUS_COMMAND |
211
125
                                               CF_SHOW_TABLE_COMMAND);
224
138
bool is_update_query(enum enum_sql_command command)
225
139
{
226
140
  assert(command >= 0 && command <= SQLCOM_END);
227
 
  return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
228
 
}
229
 
 
230
 
void execute_init_command(THD *thd, sys_var_str *init_command_var,
231
 
                          rw_lock_t *var_mutex)
232
 
{
233
 
  Vio* save_vio;
234
 
  ulong save_client_capabilities;
235
 
 
236
 
  thd_proc_info(thd, "Execution of init_command");
237
 
  /*
238
 
    We need to lock init_command_var because
239
 
    during execution of init_command_var query
240
 
    values of init_command_var can't be changed
241
 
  */
242
 
  rw_rdlock(var_mutex);
243
 
  save_client_capabilities= thd->client_capabilities;
244
 
  thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
245
 
  /*
246
 
    We don't need return result of execution to client side.
247
 
    To forbid this we should set thd->net.vio to 0.
248
 
  */
249
 
  save_vio= thd->net.vio;
250
 
  thd->net.vio= 0;
251
 
  dispatch_command(COM_QUERY, thd,
252
 
                   init_command_var->value,
253
 
                   init_command_var->value_length);
254
 
  rw_unlock(var_mutex);
255
 
  thd->client_capabilities= save_client_capabilities;
256
 
  thd->net.vio= save_vio;
257
 
}
258
 
 
259
 
/**
260
 
  Ends the current transaction and (maybe) begin the next.
261
 
 
262
 
  @param thd            Current thread
263
 
  @param completion     Completion type
264
 
 
265
 
  @retval
266
 
    0   OK
267
 
*/
268
 
 
269
 
int end_trans(THD *thd, enum enum_mysql_completiontype completion)
270
 
{
271
 
  bool do_release= 0;
272
 
  int res= 0;
273
 
 
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)
280
 
  {
281
 
    my_error(ER_XAER_RMFAIL, MYF(0),
282
 
             xa_state_names[thd->transaction.xid_state.xa_state]);
283
 
    return(1);
284
 
  }
285
 
  switch (completion) {
286
 
  case COMMIT:
287
 
    /*
288
 
     We don't use end_active_trans() here to ensure that this works
289
 
     even if there is a problem with the OPTION_AUTO_COMMIT flag
290
 
     (Which of course should never happen...)
291
 
    */
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;
296
 
    break;
297
 
  case COMMIT_RELEASE:
298
 
    do_release= 1; /* fall through */
299
 
  case COMMIT_AND_CHAIN:
300
 
    res= end_active_trans(thd);
301
 
    if (!res && completion == COMMIT_AND_CHAIN)
302
 
      res= begin_trans(thd);
303
 
    break;
304
 
  case ROLLBACK_RELEASE:
305
 
    do_release= 1; /* fall through */
306
 
  case ROLLBACK:
307
 
  case ROLLBACK_AND_CHAIN:
308
 
  {
309
 
    thd->server_status&= ~SERVER_STATUS_IN_TRANS;
310
 
    if (ha_rollback(thd))
311
 
      res= -1;
312
 
    thd->options&= ~(OPTION_BEGIN | OPTION_KEEP_LOG);
313
 
    thd->transaction.all.modified_non_trans_table= false;
314
 
    if (!res && (completion == ROLLBACK_AND_CHAIN))
315
 
      res= begin_trans(thd);
316
 
    break;
317
 
  }
318
 
  default:
319
 
    res= -1;
320
 
    my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
321
 
    return(-1);
322
 
  }
323
 
 
324
 
  if (res < 0)
325
 
    my_error(thd->killed_errno(), MYF(0));
326
 
  else if ((res == 0) && do_release)
327
 
    thd->killed= THD::KILL_CONNECTION;
328
 
 
329
 
  return(res);
330
 
}
331
 
 
332
 
 
333
 
/**
334
 
  Read one command from connection and execute it (query or simple command).
335
 
  This function is called in loop from thread function.
336
 
 
337
 
  For profiling to work, it must never be called recursively.
338
 
 
339
 
  @retval
340
 
    0  success
341
 
  @retval
342
 
    1  request of thread shutdown (see dispatch_command() description)
343
 
*/
344
 
 
345
 
bool do_command(THD *thd)
346
 
{
347
 
  bool return_value;
348
 
  char *packet= 0;
349
 
  ulong packet_length;
350
 
  NET *net= &thd->net;
351
 
  enum enum_server_command command;
352
 
 
353
 
  /*
354
 
    indicator of uninitialized lex => normal flow of errors handling
355
 
    (see my_message_sql)
356
 
  */
357
 
  thd->lex->current_select= 0;
358
 
 
359
 
  /*
360
 
    This thread will do a blocking read from the client which
361
 
    will be interrupted when the next command is received from
362
 
    the client, the connection is closed or "net_wait_timeout"
363
 
    number of seconds has passed
364
 
  */
365
 
  my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
366
 
 
367
 
  /*
368
 
    XXX: this code is here only to clear possible errors of init_connect. 
369
 
    Consider moving to init_connect() instead.
370
 
  */
371
 
  thd->clear_error();                           // Clear error message
372
 
  thd->main_da.reset_diagnostics_area();
373
 
 
374
 
  net_new_transaction(net);
375
 
 
376
 
  packet_length= my_net_read(net);
377
 
  if (packet_length == packet_error)
378
 
  {
379
 
    /* Check if we can continue without closing the connection */
380
 
 
381
 
    /* The error must be set. */
382
 
    assert(thd->is_error());
383
 
    net_end_statement(thd);
384
 
 
385
 
    if (net->error != 3)
386
 
    {
387
 
      return_value= true;                       // We have to close it.
388
 
      goto out;
389
 
    }
390
 
 
391
 
    net->error= 0;
392
 
    return_value= false;
393
 
    goto out;
394
 
  }
395
 
 
396
 
  packet= (char*) net->read_pos;
397
 
  /*
398
 
    'packet_length' contains length of data, as it was stored in packet
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
401
 
    number of bytes was actually read from network.
402
 
    There is also an extra safety measure in my_net_read:
403
 
    it sets packet[packet_length]= 0, but only for non-zero packets.
404
 
  */
405
 
  if (packet_length == 0)                       /* safety */
406
 
  {
407
 
    /* Initialize with COM_SLEEP packet */
408
 
    packet[0]= (unsigned char) COM_SLEEP;
409
 
    packet_length= 1;
410
 
  }
411
 
  /* Do not rely on my_net_read, extra safety against programming errors. */
412
 
  packet[packet_length]= '\0';                  /* safety */
413
 
 
414
 
  command= (enum enum_server_command) (unsigned char) packet[0];
415
 
 
416
 
  if (command >= COM_END)
417
 
    command= COM_END;                           // Wrong command
418
 
 
419
 
  /* Restore read timeout value */
420
 
  my_net_set_read_timeout(net, thd->variables.net_read_timeout);
421
 
 
422
 
  assert(packet_length);
423
 
  return_value= dispatch_command(command, thd, packet+1, (uint32_t) (packet_length-1));
424
 
 
425
 
out:
426
 
  return(return_value);
427
 
}
428
 
 
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);
 
141
  return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
487
142
}
488
143
 
489
144
/**
490
145
  Perform one connection-level (COM_XXXX) command.
491
146
 
492
147
  @param command         type of command to perform
493
 
  @param thd             connection handle
 
148
  @param session             connection handle
494
149
  @param packet          data for the command, packet is always null-terminated
495
150
  @param packet_length   length of packet + 1 (to show that data is
496
151
                         null-terminated) except for COM_SLEEP, where it
497
152
                         can be zero.
498
153
 
499
154
  @todo
500
 
    set thd->lex->sql_command to SQLCOM_END here.
 
155
    set session->lex->sql_command to SQLCOM_END here.
501
156
  @todo
502
157
    The following has to be changed to an 8 byte integer
503
158
 
507
162
    1   request of thread shutdown, i. e. if command is
508
163
        COM_QUIT/COM_SHUTDOWN
509
164
*/
510
 
bool dispatch_command(enum enum_server_command command, THD *thd,
511
 
                      char* packet, uint32_t packet_length)
 
165
bool dispatch_command(enum enum_server_command command, Session *session,
 
166
                      char* packet, uint32_t packet_length)
512
167
{
513
 
  NET *net= &thd->net;
514
168
  bool error= 0;
 
169
  Query_id &query_id= Query_id::get_query_id();
515
170
 
516
 
  thd->command=command;
517
 
  /*
518
 
    Commands which always take a long time are logged into
519
 
    the slow log only if opt_log_slow_admin_statements is set.
520
 
  */
521
 
  thd->enable_slow_log= true;
522
 
  thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
523
 
  thd->set_time();
524
 
  pthread_mutex_lock(&LOCK_thread_count);
525
 
  thd->query_id= global_query_id;
 
171
  session->command=command;
 
172
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
173
  session->set_time();
 
174
  session->query_id= query_id.value();
526
175
 
527
176
  switch( command ) {
528
177
  /* Ignore these statements. */
530
179
    break;
531
180
  /* Increase id and count all other statements. */
532
181
  default:
533
 
    statistic_increment(thd->status_var.questions, &LOCK_status);
534
 
    next_query_id();
 
182
    statistic_increment(session->status_var.questions, &LOCK_status);
 
183
    query_id.next();
535
184
  }
536
185
 
537
 
  thread_running++;
538
 
  /* TODO: set thd->lex->sql_command to SQLCOM_END here */
539
 
  pthread_mutex_unlock(&LOCK_thread_count);
540
 
 
541
 
  logging_pre_do(thd);
542
 
 
543
 
  thd->server_status&=
 
186
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
187
 
 
188
  logging_pre_do(session);
 
189
 
 
190
  session->server_status&=
544
191
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
545
192
  switch (command) {
546
193
  case COM_INIT_DB:
547
194
  {
548
195
    LEX_STRING tmp;
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))
553
 
    {
554
 
      my_ok(thd);
555
 
    }
556
 
    break;
557
 
  }
558
 
  case COM_REGISTER_SLAVE:
559
 
  {
560
 
    if (!register_slave(thd, (unsigned char*)packet, packet_length))
561
 
      my_ok(thd);
562
 
    break;
563
 
  }
564
 
  case COM_CHANGE_USER:
565
 
  {
566
 
    status_var_increment(thd->status_var.com_other);
567
 
    char *user= (char*) packet, *packet_end= packet + packet_length;
568
 
    /* Safe because there is always a trailing \0 at the end of the packet */
569
 
    char *passwd= strchr(user, '\0')+1;
570
 
 
571
 
 
572
 
    thd->clear_error();                         // if errors from rollback
573
 
 
574
 
    /*
575
 
      Old clients send null-terminated string ('\0' for empty string) for
576
 
      password.  New clients send the size (1 byte) + string (not null
577
 
      terminated, so also '\0' for empty string).
578
 
 
579
 
      Cast *passwd to an unsigned char, so that it doesn't extend the sign
580
 
      for *passwd > 127 and become 2**32-127 after casting to uint32_t.
581
 
    */
582
 
    char db_buff[NAME_LEN+1];                 // buffer to store db in utf8
583
 
    char *db= passwd;
584
 
    char *save_db;
585
 
    /*
586
 
      If there is no password supplied, the packet must contain '\0',
587
 
      in any type of handshake (4.1 or pre-4.1).
588
 
     */
589
 
    if (passwd >= packet_end)
590
 
    {
591
 
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
592
 
      break;
593
 
    }
594
 
    uint32_t passwd_len= (thd->client_capabilities & CLIENT_SECURE_CONNECTION ?
595
 
                      (unsigned char)(*passwd++) : strlen(passwd));
596
 
    uint32_t dummy_errors, save_db_length, db_length;
597
 
    int res;
598
 
    Security_context save_security_ctx= *thd->security_ctx;
599
 
    USER_CONN *save_user_connect;
600
 
 
601
 
    db+= passwd_len + 1;
602
 
    /*
603
 
      Database name is always NUL-terminated, so in case of empty database
604
 
      the packet must contain at least the trailing '\0'.
605
 
    */
606
 
    if (db >= packet_end)
607
 
    {
608
 
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
609
 
      break;
610
 
    }
611
 
    db_length= strlen(db);
612
 
 
613
 
    char *ptr= db + db_length + 1;
614
 
    uint32_t cs_number= 0;
615
 
 
616
 
    if (ptr < packet_end)
617
 
    {
618
 
      if (ptr + 2 > packet_end)
619
 
      {
620
 
        my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
621
 
        break;
622
 
      }
623
 
 
624
 
      cs_number= uint2korr(ptr);
625
 
    }
626
 
 
627
 
    /* Convert database name to utf8 */
628
 
    db_buff[copy_and_convert(db_buff, sizeof(db_buff)-1,
629
 
                             system_charset_info, db, db_length,
630
 
                             thd->charset(), &dummy_errors)]= 0;
631
 
    db= db_buff;
632
 
 
633
 
    /* Save user and privileges */
634
 
    save_db_length= thd->db_length;
635
 
    save_db= thd->db;
636
 
    save_user_connect= thd->user_connect;
637
 
 
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
 
    }
644
 
 
645
 
    /* Clear variables that are allocated */
646
 
    thd->user_connect= 0;
647
 
    res= check_user(thd, passwd, passwd_len, db, false);
648
 
 
649
 
    if (res)
650
 
    {
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;
657
 
    }
658
 
    else
659
 
    {
660
 
      if (save_db)
661
 
        free(save_db);
662
 
      if (save_security_ctx.user)
663
 
        free(save_security_ctx.user);
664
 
 
665
 
      if (cs_number)
666
 
      {
667
 
        thd_init_client_charset(thd, cs_number);
668
 
        thd->update_charset();
669
 
      }
 
196
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
 
197
    tmp.str= packet;
 
198
    tmp.length= packet_length;
 
199
    if (!mysql_change_db(session, &tmp, false))
 
200
    {
 
201
      session->my_ok();
670
202
    }
671
203
    break;
672
204
  }
673
205
  case COM_QUERY:
674
206
  {
675
 
    if (alloc_query(thd, packet, packet_length))
 
207
    if (! session->readAndStoreQuery(packet, packet_length))
676
208
      break;                                    // fatal error is set
677
 
    char *packet_end= thd->query + thd->query_length;
678
209
    const char* end_of_stmt= NULL;
679
210
 
680
 
    mysql_parse(thd, thd->query, thd->query_length, &end_of_stmt);
681
 
 
682
 
    while (!thd->killed && (end_of_stmt != NULL) && ! thd->is_error())
683
 
    {
684
 
      char *beginning_of_next_stmt= (char*) end_of_stmt;
685
 
 
686
 
      net_end_statement(thd);
687
 
      /*
688
 
        Multiple queries exits, execute them individually
689
 
      */
690
 
      close_thread_tables(thd);
691
 
      ulong length= (ulong)(packet_end - beginning_of_next_stmt);
692
 
 
693
 
      log_slow_statement(thd);
694
 
 
695
 
      /* Remove garbage at start of query */
696
 
      while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
697
 
      {
698
 
        beginning_of_next_stmt++;
699
 
        length--;
700
 
      }
701
 
 
702
 
      pthread_mutex_lock(&LOCK_thread_count);
703
 
      thd->query_length= length;
704
 
      thd->query= beginning_of_next_stmt;
705
 
      /*
706
 
        Count each statement from the client.
707
 
      */
708
 
      statistic_increment(thd->status_var.questions, &LOCK_status);
709
 
      thd->query_id= next_query_id();
710
 
      thd->set_time(); /* Reset the query start time. */
711
 
      /* TODO: set thd->lex->sql_command to SQLCOM_END here */
712
 
      pthread_mutex_unlock(&LOCK_thread_count);
713
 
 
714
 
      mysql_parse(thd, beginning_of_next_stmt, length, &end_of_stmt);
715
 
    }
716
 
    break;
717
 
  }
718
 
  case COM_FIELD_LIST:                          // This isn't actually needed
719
 
  {
720
 
    char *fields, *packet_end= packet + packet_length, *arg_end;
721
 
    /* Locked closure of all tables */
722
 
    TableList table_list;
723
 
    LEX_STRING conv_name;
724
 
 
725
 
    /* used as fields initializator */
726
 
    lex_start(thd);
727
 
 
728
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
729
 
    memset(&table_list, 0, sizeof(table_list));
730
 
    if (thd->copy_db_to(&table_list.db, &table_list.db_length))
731
 
      break;
732
 
    /*
733
 
      We have name + wildcard in packet, separated by endzero
734
 
    */
735
 
    arg_end= strchr(packet, '\0');
736
 
    thd->convert_string(&conv_name, system_charset_info,
737
 
                        packet, (uint32_t) (arg_end - packet), thd->charset());
738
 
    table_list.alias= table_list.table_name= conv_name.str;
739
 
    packet= arg_end + 1;
740
 
 
741
 
    if (!my_strcasecmp(system_charset_info, table_list.db,
742
 
                       INFORMATION_SCHEMA_NAME.str))
743
 
    {
744
 
      ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
745
 
      if (schema_table)
746
 
        table_list.schema_table= schema_table;
747
 
    }
748
 
 
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)))
751
 
      break;
752
 
    if (lower_case_table_names)
753
 
      my_casedn_str(files_charset_info, table_list.table_name);
754
 
 
755
 
    /* init structures for VIEW processing */
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->
762
 
      select_lex.table_list.link_in_list((unsigned char*) &table_list,
763
 
                                         (unsigned char**) &table_list.next_local);
764
 
    thd->lex->add_to_query_tables(&table_list);
765
 
 
766
 
    /* switch on VIEW optimisation: do not fill temporary tables */
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();
 
211
    mysql_parse(session, session->query, session->query_length, &end_of_stmt);
 
212
 
771
213
    break;
772
214
  }
773
215
  case COM_QUIT:
774
216
    /* We don't calculate statistics for this command */
775
 
    net->error=0;                               // Don't give 'abort' message
776
 
    thd->main_da.disable_status();              // Don't send anything back
 
217
    session->protocol->setError(0);
 
218
    session->main_da.disable_status();              // Don't send anything back
777
219
    error=true;                                 // End server
778
220
    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
 
    }
801
221
  case COM_SHUTDOWN:
802
222
  {
803
 
    status_var_increment(thd->status_var.com_other);
804
 
    my_eof(thd);
805
 
    close_thread_tables(thd);                   // Free before kill
806
 
    kill_mysql();
 
223
    status_var_increment(session->status_var.com_other);
 
224
    session->my_eof();
 
225
    session->close_thread_tables();                     // Free before kill
 
226
    kill_drizzle();
807
227
    error=true;
808
228
    break;
809
229
  }
810
230
  case COM_PING:
811
 
    status_var_increment(thd->status_var.com_other);
812
 
    my_ok(thd);                         // Tell client we are alive
813
 
    break;
814
 
  case COM_PROCESS_INFO:
815
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
816
 
    mysqld_list_processes(thd, NULL, 0);
817
 
    break;
818
 
  case COM_PROCESS_KILL:
819
 
  {
820
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
821
 
    ulong id=(ulong) uint4korr(packet);
822
 
    sql_kill(thd,id,false);
823
 
    break;
824
 
  }
825
 
  case COM_SET_OPTION:
826
 
  {
827
 
    status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
828
 
    uint32_t opt_command= uint2korr(packet);
829
 
 
830
 
    switch (opt_command) {
831
 
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_ON:
832
 
      thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
833
 
      my_eof(thd);
834
 
      break;
835
 
    case (int) DRIZZLE_OPTION_MULTI_STATEMENTS_OFF:
836
 
      thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
837
 
      my_eof(thd);
838
 
      break;
839
 
    default:
840
 
      my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
841
 
      break;
842
 
    }
843
 
    break;
844
 
  }
 
231
    status_var_increment(session->status_var.com_other);
 
232
    session->my_ok();                           // Tell client we are alive
 
233
    break;
845
234
  case COM_SLEEP:
846
235
  case COM_CONNECT:                             // Impossible here
847
 
  case COM_TIME:                                // Impossible from client
848
236
  case COM_END:
849
237
  default:
850
238
    my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
852
240
  }
853
241
 
854
242
  /* If commit fails, we should be able to reset the OK status. */
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;
 
243
  session->main_da.can_overwrite_status= true;
 
244
  ha_autocommit_or_rollback(session, session->is_error());
 
245
  session->main_da.can_overwrite_status= false;
858
246
 
859
 
  thd->transaction.stmt.reset();
 
247
  session->transaction.stmt.reset();
860
248
 
861
249
 
862
250
  /* report error issued during command execution */
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");
 
251
  if (session->killed_errno())
 
252
  {
 
253
    if (! session->main_da.is_set())
 
254
      session->send_kill_message();
 
255
  }
 
256
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
 
257
  {
 
258
    session->killed= Session::NOT_KILLED;
 
259
    session->mysys_var->abort= 0;
 
260
  }
 
261
 
 
262
  /* Can not be true, but do not take chances in production. */
 
263
  assert(! session->main_da.is_sent);
 
264
 
 
265
  switch (session->main_da.status())
 
266
  {
 
267
  case Diagnostics_area::DA_ERROR:
 
268
    /* The query failed, send error to log and abort bootstrap. */
 
269
    session->protocol->sendError(session->main_da.sql_errno(),
 
270
                                 session->main_da.message());
 
271
    break;
 
272
 
 
273
  case Diagnostics_area::DA_EOF:
 
274
    session->protocol->sendEOF();
 
275
    break;
 
276
 
 
277
  case Diagnostics_area::DA_OK:
 
278
    session->protocol->sendOK();
 
279
    break;
 
280
 
 
281
  case Diagnostics_area::DA_DISABLED:
 
282
    break;
 
283
 
 
284
  case Diagnostics_area::DA_EMPTY:
 
285
  default:
 
286
    session->protocol->sendOK();
 
287
    break;
 
288
  }
 
289
 
 
290
  session->main_da.is_sent= true;
 
291
 
 
292
  session->set_proc_info("closing tables");
877
293
  /* Free tables */
878
 
  close_thread_tables(thd);
879
 
 
880
 
  log_slow_statement(thd);
881
 
 
882
 
  thd_proc_info(thd, "cleaning up");
883
 
  pthread_mutex_lock(&LOCK_thread_count); // For process list
884
 
  thd_proc_info(thd, 0);
885
 
  thd->command=COM_SLEEP;
886
 
  thd->query=0;
887
 
  thd->query_length=0;
888
 
  thread_running--;
889
 
  pthread_mutex_unlock(&LOCK_thread_count);
890
 
  thd->packet.shrink(thd->variables.net_buffer_length); // Reclaim some memory
891
 
  free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
 
294
  session->close_thread_tables();
 
295
 
 
296
  logging_post_do(session);
 
297
 
 
298
  /* Store temp state for processlist */
 
299
  session->set_proc_info("cleaning up");
 
300
  session->command=COM_SLEEP;
 
301
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
302
  session->query=0;
 
303
  session->query_length=0;
 
304
 
 
305
  session->set_proc_info(NULL);
 
306
  session->packet.shrink(session->variables.net_buffer_length); // Reclaim some memory
 
307
  free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
892
308
  return(error);
893
309
}
894
310
 
895
311
 
896
 
void log_slow_statement(THD *thd)
897
 
{
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);
907
 
 
908
 
  return;
909
 
}
910
 
 
911
 
 
912
312
/**
913
313
  Create a TableList object for an INFORMATION_SCHEMA table.
914
314
 
915
315
    This function is used in the parser to convert a SHOW or DESCRIBE
916
316
    table_name command to a SELECT from INFORMATION_SCHEMA.
917
 
    It prepares a SELECT_LEX and a TableList object to represent the
 
317
    It prepares a Select_Lex and a TableList object to represent the
918
318
    given command as a SELECT parse tree.
919
319
 
920
 
  @param thd              thread handle
 
320
  @param session              thread handle
921
321
  @param lex              current lex
922
322
  @param table_ident      table alias if it's used
923
323
  @param schema_table_idx the type of the INFORMATION_SCHEMA table to be
935
335
                      in this version of the server.
936
336
*/
937
337
 
938
 
int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
 
338
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
939
339
                         enum enum_schema_tables schema_table_idx)
940
340
{
941
 
  SELECT_LEX *schema_select_lex= NULL;
 
341
  Select_Lex *schema_select_lex= NULL;
942
342
 
943
343
  switch (schema_table_idx) {
944
344
  case SCH_SCHEMATA:
953
353
      {
954
354
        return(1);
955
355
      }
956
 
      schema_select_lex= new SELECT_LEX();
 
356
      schema_select_lex= new Select_Lex();
957
357
      db.str= schema_select_lex->db= lex->select_lex.db;
958
358
      schema_select_lex->table_list.first= NULL;
959
359
      db.length= strlen(db.str);
970
370
  {
971
371
    assert(table_ident);
972
372
    TableList **query_tables_last= lex->query_tables_last;
973
 
    schema_select_lex= new SELECT_LEX();
 
373
    schema_select_lex= new Select_Lex();
974
374
    /* 'parent_lex' is used in init_query() so it must be before it. */
975
375
    schema_select_lex->parent_lex= lex;
976
376
    schema_select_lex->init_query();
977
 
    if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ))
 
377
    if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
978
378
      return(1);
979
379
    lex->query_tables_last= query_tables_last;
980
380
    break;
990
390
  default:
991
391
    break;
992
392
  }
993
 
  
994
 
  SELECT_LEX *select_lex= lex->current_select;
 
393
 
 
394
  Select_Lex *select_lex= lex->current_select;
995
395
  assert(select_lex);
996
 
  if (make_schema_select(thd, select_lex, schema_table_idx))
 
396
  if (make_schema_select(session, select_lex, schema_table_idx))
997
397
  {
998
398
    return(1);
999
399
  }
1000
400
  TableList *table_list= (TableList*) select_lex->table_list.first;
1001
401
  assert(table_list);
1002
402
  table_list->schema_select_lex= schema_select_lex;
1003
 
  table_list->schema_table_reformed= 1;
1004
 
  return(0);
1005
 
}
1006
 
 
1007
 
 
1008
 
/**
1009
 
  Read query from packet and store in thd->query.
1010
 
  Used in COM_QUERY and COM_STMT_PREPARE.
1011
 
 
1012
 
    Sets the following THD variables:
1013
 
  - query
1014
 
  - query_length
1015
 
 
1016
 
  @retval
1017
 
    false ok
1018
 
  @retval
1019
 
    true  error;  In this case thd->fatal_error is set
1020
 
*/
1021
 
 
1022
 
bool alloc_query(THD *thd, const char *packet, uint32_t packet_length)
1023
 
{
1024
 
  /* Remove garbage at start and end of query */
1025
 
  while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
1026
 
  {
1027
 
    packet++;
1028
 
    packet_length--;
1029
 
  }
1030
 
  const char *pos= packet + packet_length;     // Point at end null
1031
 
  while (packet_length > 0 &&
1032
 
         (pos[-1] == ';' || my_isspace(thd->charset() ,pos[-1])))
1033
 
  {
1034
 
    pos--;
1035
 
    packet_length--;
1036
 
  }
1037
 
  /* We must allocate some extra memory for query cache */
1038
 
  thd->query_length= 0;                        // Extra safety: Avoid races
1039
 
  if (!(thd->query= (char*) thd->memdup_w_gap((unsigned char*) (packet),
1040
 
                                              packet_length,
1041
 
                                              thd->db_length+ 1)))
1042
 
    return true;
1043
 
  thd->query[packet_length]=0;
1044
 
  thd->query_length= packet_length;
1045
 
 
1046
 
  /* Reclaim some memory */
1047
 
  thd->packet.shrink(thd->variables.net_buffer_length);
1048
 
  thd->convert_buffer.shrink(thd->variables.net_buffer_length);
1049
 
 
1050
 
  return false;
1051
 
}
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
 
 
1071
 
/**
1072
 
  Execute command saved in thd and lex->sql_command.
 
403
 
 
404
  return 0;
 
405
}
 
406
 
 
407
/**
 
408
  Execute command saved in session and lex->sql_command.
1073
409
 
1074
410
    Before every operation that can request a write lock for a table
1075
411
    wait if a global read lock exists. However do not wait if this
1081
417
    global read lock when it succeeds. This needs to be released by
1082
418
    start_waiting_global_read_lock() after the operation.
1083
419
 
1084
 
  @param thd                       Thread handle
 
420
  @param session                       Thread handle
1085
421
 
1086
422
  @todo
1087
423
    - Invalidate the table in the query cache if something changed
1099
435
*/
1100
436
 
1101
437
int
1102
 
mysql_execute_command(THD *thd)
 
438
mysql_execute_command(Session *session)
1103
439
{
1104
440
  int res= false;
1105
441
  bool need_start_waiting= false; // have protection against global read lock
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 */
 
442
  LEX  *lex= session->lex;
 
443
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
 
444
  Select_Lex *select_lex= &lex->select_lex;
 
445
  /* first table of first Select_Lex */
1111
446
  TableList *first_table= (TableList*) select_lex->table_list.first;
1112
447
  /* list of all tables in query */
1113
448
  TableList *all_tables;
1114
 
  /* most outer SELECT_LEX_UNIT of query */
1115
 
  SELECT_LEX_UNIT *unit= &lex->unit;
1116
 
  /* Saved variable value */
 
449
  /* most outer Select_Lex_Unit of query */
 
450
  Select_Lex_Unit *unit= &lex->unit;
 
451
  /* A peek into the query string */
 
452
  size_t proc_info_len= session->query_length > PROCESS_LIST_WIDTH ?
 
453
                        PROCESS_LIST_WIDTH : session->query_length;
 
454
 
 
455
  memcpy(session->process_list_info, session->query, proc_info_len);
 
456
  session->process_list_info[proc_info_len]= '\0';
1117
457
 
1118
458
  /*
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 
 
459
    In many cases first table of main Select_Lex have special meaning =>
 
460
    check that it is first table in global list and relink it first in
1121
461
    queries_tables list if it is necessary (we need such relinking only
1122
462
    for queries with subqueries in select list, in this case tables of
1123
463
    subqueries will go to global list first)
1124
464
 
1125
 
    all_tables will differ from first_table only if most upper SELECT_LEX
 
465
    all_tables will differ from first_table only if most upper Select_Lex
1126
466
    do not contain tables.
1127
467
 
1128
468
    Because of above in place where should be at least one table in most
1129
 
    outer SELECT_LEX we have following check:
 
469
    outer Select_Lex we have following check:
1130
470
    assert(first_table == all_tables);
1131
471
    assert(first_table == all_tables && first_table != 0);
1132
472
  */
1146
486
    Don't reset warnings when executing a stored routine.
1147
487
  */
1148
488
  if (all_tables || !lex->is_single_level_stmt())
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
 
  
 
489
    drizzle_reset_errors(session, 0);
 
490
 
 
491
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
492
 
 
493
  assert(session->transaction.stmt.modified_non_trans_table == false);
 
494
 
1210
495
  switch (lex->sql_command) {
1211
496
  case SQLCOM_SHOW_STATUS:
1212
497
  {
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);
 
498
    system_status_var old_status_var= session->status_var;
 
499
    session->initial_status_var= &old_status_var;
 
500
    res= execute_sqlcom_select(session, all_tables);
1216
501
    /* Don't log SHOW STATUS commands to slow query log */
1217
 
    thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
 
502
    session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1218
503
                           SERVER_QUERY_NO_GOOD_INDEX_USED);
1219
504
    /*
1220
505
      restore status variables, as we don't want 'show status' to cause
1221
506
      changes
1222
507
    */
1223
508
    pthread_mutex_lock(&LOCK_status);
1224
 
    add_diff_to_status(&global_status_var, &thd->status_var,
 
509
    add_diff_to_status(&global_status_var, &session->status_var,
1225
510
                       &old_status_var);
1226
 
    thd->status_var= old_status_var;
 
511
    session->status_var= old_status_var;
1227
512
    pthread_mutex_unlock(&LOCK_status);
1228
513
    break;
1229
514
  }
1234
519
  case SQLCOM_SHOW_FIELDS:
1235
520
  case SQLCOM_SHOW_KEYS:
1236
521
  case SQLCOM_SHOW_VARIABLES:
1237
 
  case SQLCOM_SHOW_CHARSETS:
1238
 
  case SQLCOM_SHOW_COLLATIONS:
1239
522
  case SQLCOM_SELECT:
1240
523
  {
1241
 
    thd->status_var.last_query_cost= 0.0;
1242
 
    res= execute_sqlcom_select(thd, all_tables);
 
524
    session->status_var.last_query_cost= 0.0;
 
525
    res= execute_sqlcom_select(session, all_tables);
1243
526
    break;
1244
527
  }
1245
528
  case SQLCOM_EMPTY_QUERY:
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
 
  }
 
529
    session->my_ok();
 
530
    break;
 
531
 
1275
532
  case SQLCOM_SHOW_WARNS:
1276
533
  {
1277
 
    res= mysqld_show_warnings(thd, (uint32_t)
 
534
    res= mysqld_show_warnings(session, (uint32_t)
1278
535
                              ((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
1279
536
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
1280
537
                               (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1283
540
  }
1284
541
  case SQLCOM_SHOW_ERRORS:
1285
542
  {
1286
 
    res= mysqld_show_warnings(thd, (uint32_t)
 
543
    res= mysqld_show_warnings(session, (uint32_t)
1287
544
                              (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1288
545
    break;
1289
546
  }
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
 
 
1301
547
  case SQLCOM_ASSIGN_TO_KEYCACHE:
1302
548
  {
1303
549
    assert(first_table == all_tables && first_table != 0);
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
 
 
 
550
    res= mysql_assign_to_keycache(session, first_table, &lex->ident);
 
551
    break;
 
552
  }
1336
553
  case SQLCOM_SHOW_ENGINE_STATUS:
1337
554
    {
1338
 
      res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
 
555
      res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1339
556
      break;
1340
557
    }
1341
558
  case SQLCOM_CREATE_TABLE:
1343
560
    /* If CREATE TABLE of non-temporary table, do implicit commit */
1344
561
    if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
1345
562
    {
1346
 
      if (end_active_trans(thd))
 
563
      if (! session->endActiveTransaction())
1347
564
      {
1348
 
        res= -1;
1349
 
        break;
 
565
        res= -1;
 
566
        break;
1350
567
      }
1351
568
    }
1352
569
    assert(first_table == all_tables && first_table != 0);
1367
584
      safety, only in case of Alter_info we have to do (almost) a deep
1368
585
      copy.
1369
586
    */
1370
 
    Alter_info alter_info(lex->alter_info, thd->mem_root);
 
587
    Alter_info alter_info(lex->alter_info, session->mem_root);
1371
588
 
1372
 
    if (thd->is_fatal_error)
 
589
    if (session->is_fatal_error)
1373
590
    {
1374
591
      /* If out of memory when creating a copy of alter_info. */
1375
592
      res= 1;
1376
593
      goto end_with_restore_list;
1377
594
    }
1378
595
 
1379
 
    if ((res= create_table_precheck(thd, select_tables, create_table)))
 
596
    if ((res= create_table_precheck(session, select_tables, create_table)))
1380
597
      goto end_with_restore_list;
1381
598
 
1382
599
    /* Might have been updated in create_table_precheck */
1384
601
 
1385
602
#ifdef HAVE_READLINK
1386
603
    /* Fix names if symlinked tables */
1387
 
    if (append_file_to_dir(thd, &create_info.data_file_name,
 
604
    if (append_file_to_dir(session, &create_info.data_file_name,
1388
605
                           create_table->table_name) ||
1389
 
        append_file_to_dir(thd, &create_info.index_file_name,
 
606
        append_file_to_dir(session, &create_info.index_file_name,
1390
607
                           create_table->table_name))
1391
608
      goto end_with_restore_list;
1392
609
#endif
1393
610
    /*
1394
 
      If we are using SET CHARSET without DEFAULT, add an implicit
1395
 
      DEFAULT to not confuse old users. (This may change).
1396
 
    */
1397
 
    if ((create_info.used_fields &
1398
 
         (HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
1399
 
        HA_CREATE_USED_CHARSET)
1400
 
    {
1401
 
      create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
1402
 
      create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
1403
 
      create_info.default_table_charset= create_info.table_charset;
1404
 
      create_info.table_charset= 0;
1405
 
    }
1406
 
    /*
1407
611
      The create-select command will open and read-lock the select table
1408
612
      and then create, open and write-lock the new table. If a global
1409
613
      read lock steps in, we get a deadlock. The write lock waits for
1416
620
      TABLE in the same way. That way we avoid that a new table is
1417
621
      created during a gobal read lock.
1418
622
    */
1419
 
    if (!thd->locked_tables &&
1420
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
623
    if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1421
624
    {
1422
625
      res= 1;
1423
626
      goto end_with_restore_list;
1435
638
        create_table->create= true;
1436
639
      }
1437
640
 
1438
 
      if (!(res= open_and_lock_tables(thd, lex->query_tables)))
 
641
      if (!(res= session->open_and_lock_tables(lex->query_tables)))
1439
642
      {
1440
643
        /*
1441
644
          Is table which we are changing used somewhere in other parts
1445
648
        {
1446
649
          TableList *duplicate;
1447
650
          create_table= lex->unlink_first_table(&link_to_local);
1448
 
          if ((duplicate= unique_table(thd, create_table, select_tables, 0)))
 
651
          if ((duplicate= unique_table(session, create_table, select_tables, 0)))
1449
652
          {
1450
 
            update_non_unique_table_error(create_table, "CREATE", duplicate);
 
653
            my_error(ER_UPDATE_TABLE_USED, MYF(0), create_table->alias);
1451
654
            res= 1;
1452
655
            goto end_with_restore_list;
1453
656
          }
1466
669
                                       select_tables)))
1467
670
        {
1468
671
          /*
1469
 
            CREATE from SELECT give its SELECT_LEX for SELECT,
 
672
            CREATE from SELECT give its Select_Lex for SELECT,
1470
673
            and item_list belong to SELECT
1471
674
          */
1472
 
          res= handle_select(thd, lex, result, 0);
 
675
          res= handle_select(session, lex, result, 0);
1473
676
          delete result;
1474
677
        }
1475
678
      }
1481
684
    {
1482
685
      /* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
1483
686
      if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
1484
 
        thd->options|= OPTION_KEEP_LOG;
 
687
        session->options|= OPTION_KEEP_LOG;
1485
688
      /* regular create */
1486
689
      if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
1487
 
        res= mysql_create_like_table(thd, create_table, select_tables,
 
690
        res= mysql_create_like_table(session, create_table, select_tables,
1488
691
                                     &create_info);
1489
692
      else
1490
693
      {
1491
 
        res= mysql_create_table(thd, create_table->db,
 
694
        res= mysql_create_table(session, create_table->db,
1492
695
                                create_table->table_name, &create_info,
 
696
                                lex->create_table_proto,
1493
697
                                &alter_info, 0, 0);
1494
698
      }
1495
699
      if (!res)
1496
 
        my_ok(thd);
 
700
        session->my_ok();
1497
701
    }
1498
702
 
1499
703
    /* put tables back for PS rexecuting */
1515
719
  {
1516
720
    /* Prepare stack copies to be re-execution safe */
1517
721
    HA_CREATE_INFO create_info;
1518
 
    Alter_info alter_info(lex->alter_info, thd->mem_root);
 
722
    Alter_info alter_info(lex->alter_info, session->mem_root);
1519
723
 
1520
 
    if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
 
724
    if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1521
725
      goto error;
1522
726
 
1523
727
    assert(first_table == all_tables && first_table != 0);
1524
 
    if (end_active_trans(thd))
 
728
    if (! session->endActiveTransaction())
1525
729
      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;
1532
730
 
1533
731
    memset(&create_info, 0, sizeof(create_info));
1534
732
    create_info.db_type= 0;
1535
733
    create_info.row_type= ROW_TYPE_NOT_USED;
1536
 
    create_info.default_table_charset= thd->variables.collation_database;
 
734
    create_info.default_table_charset= get_default_db_collation(session->db);
1537
735
 
1538
 
    res= mysql_alter_table(thd, first_table->db, first_table->table_name,
 
736
    res= mysql_alter_table(session, first_table->db, first_table->table_name,
1539
737
                           &create_info, first_table, &alter_info,
1540
738
                           0, (order_st*) 0, 0);
1541
739
    break;
1542
740
  }
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
 
 
1577
741
  case SQLCOM_ALTER_TABLE:
1578
742
    assert(first_table == all_tables && first_table != 0);
1579
743
    {
1584
748
        referenced from this structure will be modified.
1585
749
      */
1586
750
      HA_CREATE_INFO create_info(lex->create_info);
1587
 
      Alter_info alter_info(lex->alter_info, thd->mem_root);
 
751
      Alter_info alter_info(lex->alter_info, session->mem_root);
1588
752
 
1589
 
      if (thd->is_fatal_error) /* out of memory creating a copy of alter_info */
 
753
      if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1590
754
      {
1591
755
        goto error;
1592
756
      }
1603
767
 
1604
768
      /* Don't yet allow changing of symlinks with ALTER TABLE */
1605
769
      if (create_info.data_file_name)
1606
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
770
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1607
771
                     "DATA DIRECTORY option ignored");
1608
772
      if (create_info.index_file_name)
1609
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
 
773
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1610
774
                     "INDEX DIRECTORY option ignored");
1611
775
      create_info.data_file_name= create_info.index_file_name= NULL;
1612
776
      /* ALTER TABLE ends previous transaction */
1613
 
      if (end_active_trans(thd))
 
777
      if (! session->endActiveTransaction())
1614
778
        goto error;
1615
779
 
1616
 
      if (!thd->locked_tables &&
1617
 
          !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
780
      if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1618
781
      {
1619
782
        res= 1;
1620
783
        break;
1621
784
      }
1622
785
 
1623
 
      thd->enable_slow_log= opt_log_slow_admin_statements;
1624
 
      res= mysql_alter_table(thd, select_lex->db, lex->name.str,
 
786
      res= mysql_alter_table(session, select_lex->db, lex->name.str,
1625
787
                             &create_info,
1626
788
                             first_table,
1627
789
                             &alter_info,
1645
807
      new_list= table->next_local[0];
1646
808
    }
1647
809
 
1648
 
    if (end_active_trans(thd) || mysql_rename_tables(thd, first_table, 0))
1649
 
      {
1650
 
        goto error;
1651
 
      }
 
810
    if (! session->endActiveTransaction() || drizzle_rename_tables(session, first_table))
 
811
    {
 
812
      goto error;
 
813
    }
1652
814
    break;
1653
815
  }
1654
 
  case SQLCOM_SHOW_BINLOGS:
1655
 
    {
1656
 
      res = show_binlogs(thd);
1657
 
      break;
1658
 
    }
1659
816
  case SQLCOM_SHOW_CREATE:
1660
817
    assert(first_table == all_tables && first_table != 0);
1661
818
    {
1662
 
      res= mysqld_show_create(thd, first_table);
 
819
      res= drizzled_show_create(session, first_table);
1663
820
      break;
1664
821
    }
1665
822
  case SQLCOM_CHECKSUM:
1666
823
  {
1667
824
    assert(first_table == all_tables && first_table != 0);
1668
 
    res = mysql_checksum_table(thd, first_table, &lex->check_opt);
 
825
    res = mysql_checksum_table(session, first_table, &lex->check_opt);
1669
826
    break;
1670
827
  }
1671
828
  case SQLCOM_REPAIR:
1672
829
  {
1673
830
    assert(first_table == all_tables && first_table != 0);
1674
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1675
 
    res= mysql_repair_table(thd, first_table, &lex->check_opt);
 
831
    res= mysql_repair_table(session, first_table, &lex->check_opt);
1676
832
    /* ! we write after unlocking the table */
1677
833
    /*
1678
834
      Presumably, REPAIR and binlog writing doesn't require synchronization
1679
835
    */
1680
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
836
    write_bin_log(session, true, session->query, session->query_length);
1681
837
    select_lex->table_list.first= (unsigned char*) first_table;
1682
838
    lex->query_tables=all_tables;
1683
839
    break;
1685
841
  case SQLCOM_CHECK:
1686
842
  {
1687
843
    assert(first_table == all_tables && first_table != 0);
1688
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1689
 
    res = mysql_check_table(thd, first_table, &lex->check_opt);
 
844
    res = mysql_check_table(session, first_table, &lex->check_opt);
1690
845
    select_lex->table_list.first= (unsigned char*) first_table;
1691
846
    lex->query_tables=all_tables;
1692
847
    break;
1694
849
  case SQLCOM_ANALYZE:
1695
850
  {
1696
851
    assert(first_table == all_tables && first_table != 0);
1697
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1698
 
    res= mysql_analyze_table(thd, first_table, &lex->check_opt);
 
852
    res= mysql_analyze_table(session, first_table, &lex->check_opt);
1699
853
    /* ! we write after unlocking the table */
1700
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
854
    write_bin_log(session, true, session->query, session->query_length);
1701
855
    select_lex->table_list.first= (unsigned char*) first_table;
1702
856
    lex->query_tables=all_tables;
1703
857
    break;
1706
860
  case SQLCOM_OPTIMIZE:
1707
861
  {
1708
862
    assert(first_table == all_tables && first_table != 0);
1709
 
    thd->enable_slow_log= opt_log_slow_admin_statements;
1710
 
    res= mysql_optimize_table(thd, first_table, &lex->check_opt);
 
863
    res= mysql_optimize_table(session, first_table, &lex->check_opt);
1711
864
    /* ! we write after unlocking the table */
1712
 
    write_bin_log(thd, true, thd->query, thd->query_length);
 
865
    write_bin_log(session, true, session->query, session->query_length);
1713
866
    select_lex->table_list.first= (unsigned char*) first_table;
1714
867
    lex->query_tables=all_tables;
1715
868
    break;
1716
869
  }
1717
870
  case SQLCOM_UPDATE:
1718
871
    assert(first_table == all_tables && first_table != 0);
1719
 
    if (update_precheck(thd, all_tables))
 
872
    if ((res= update_precheck(session, all_tables)))
1720
873
      break;
1721
874
    assert(select_lex->offset_limit == 0);
1722
875
    unit->set_limit(select_lex);
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 */
 
876
    res= mysql_update(session, all_tables,
 
877
                      select_lex->item_list,
 
878
                      lex->value_list,
 
879
                      select_lex->where,
 
880
                      select_lex->order_list.elements,
 
881
                      (order_st *) select_lex->order_list.first,
 
882
                      unit->select_limit_cnt,
 
883
                      lex->duplicates, lex->ignore);
 
884
    break;
1735
885
  case SQLCOM_UPDATE_MULTI:
1736
886
  {
1737
887
    assert(first_table == all_tables && first_table != 0);
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,
 
888
    if ((res= update_precheck(session, all_tables)))
 
889
      break;
 
890
 
 
891
    if ((res= mysql_multi_update_prepare(session)))
 
892
      break;
 
893
 
 
894
    res= mysql_multi_update(session, all_tables,
1779
895
                            &select_lex->item_list,
1780
896
                            &lex->value_list,
1781
897
                            select_lex->where,
1787
903
  case SQLCOM_INSERT:
1788
904
  {
1789
905
    assert(first_table == all_tables && first_table != 0);
1790
 
    if ((res= insert_precheck(thd, all_tables)))
 
906
    if ((res= insert_precheck(session, all_tables)))
1791
907
      break;
1792
908
 
1793
 
    if (!thd->locked_tables &&
1794
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
909
    if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1795
910
    {
1796
911
      res= 1;
1797
912
      break;
1798
913
    }
1799
914
 
1800
 
    res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
 
915
    res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1801
916
                      lex->update_list, lex->value_list,
1802
917
                      lex->duplicates, lex->ignore);
1803
918
 
1808
923
  {
1809
924
    select_result *sel_result;
1810
925
    assert(first_table == all_tables && first_table != 0);
1811
 
    if ((res= insert_precheck(thd, all_tables)))
 
926
    if ((res= insert_precheck(session, all_tables)))
1812
927
      break;
1813
928
 
1814
 
    /* Fix lock for first table */
1815
 
    if (first_table->lock_type == TL_WRITE_DELAYED)
1816
 
      first_table->lock_type= TL_WRITE;
1817
 
 
1818
929
    /* Don't unlock tables until command is written to binary log */
1819
930
    select_lex->options|= SELECT_NO_UNLOCK;
1820
931
 
1821
932
    unit->set_limit(select_lex);
1822
933
 
1823
 
    if (! thd->locked_tables &&
1824
 
        ! (need_start_waiting= ! wait_if_global_read_lock(thd, 0, 1)))
 
934
    if (! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1825
935
    {
1826
936
      res= 1;
1827
937
      break;
1828
938
    }
1829
939
 
1830
 
    if (!(res= open_and_lock_tables(thd, all_tables)))
 
940
    if (!(res= session->open_and_lock_tables(all_tables)))
1831
941
    {
1832
942
      /* Skip first table, which is the table we are inserting in */
1833
943
      TableList *second_table= first_table->next_local;
1834
944
      select_lex->table_list.first= (unsigned char*) second_table;
1835
 
      select_lex->context.table_list= 
 
945
      select_lex->context.table_list=
1836
946
        select_lex->context.first_name_resolution_table= second_table;
1837
 
      res= mysql_insert_select_prepare(thd);
 
947
      res= mysql_insert_select_prepare(session);
1838
948
      if (!res && (sel_result= new select_insert(first_table,
1839
949
                                                 first_table->table,
1840
950
                                                 &lex->field_list,
1843
953
                                                 lex->duplicates,
1844
954
                                                 lex->ignore)))
1845
955
      {
1846
 
        res= handle_select(thd, lex, sel_result, OPTION_SETUP_TABLES_DONE);
 
956
        res= handle_select(session, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1847
957
        /*
1848
958
          Invalidate the table in the query cache if something changed
1849
959
          after unlocking when changes become visible.
1851
961
          the unlock procedure.
1852
962
        */
1853
963
        if (first_table->lock_type ==  TL_WRITE_CONCURRENT_INSERT &&
1854
 
            thd->lock)
 
964
            session->lock)
1855
965
        {
1856
966
          /* INSERT ... SELECT should invalidate only the very first table */
1857
967
          TableList *save_table= first_table->next_local;
1867
977
    break;
1868
978
  }
1869
979
  case SQLCOM_TRUNCATE:
1870
 
    if (end_active_trans(thd))
 
980
    if (! session->endActiveTransaction())
1871
981
    {
1872
982
      res= -1;
1873
983
      break;
1877
987
      Don't allow this within a transaction because we want to use
1878
988
      re-generate table
1879
989
    */
1880
 
    if (thd->locked_tables || thd->active_transaction())
 
990
    if (session->inTransaction())
1881
991
    {
1882
 
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
1883
 
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
992
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1884
993
      goto error;
1885
994
    }
1886
995
 
1887
 
    res= mysql_truncate(thd, first_table, 0);
 
996
    res= mysql_truncate(session, first_table, 0);
1888
997
 
1889
998
    break;
1890
999
  case SQLCOM_DELETE:
1893
1002
    assert(select_lex->offset_limit == 0);
1894
1003
    unit->set_limit(select_lex);
1895
1004
 
1896
 
    if (!thd->locked_tables &&
1897
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1005
    if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1898
1006
    {
1899
1007
      res= 1;
1900
1008
      break;
1901
1009
    }
1902
1010
 
1903
 
    res = mysql_delete(thd, all_tables, select_lex->where,
 
1011
    res = mysql_delete(session, all_tables, select_lex->where,
1904
1012
                       &select_lex->order_list,
1905
1013
                       unit->select_limit_cnt, select_lex->options,
1906
1014
                       false);
1910
1018
  {
1911
1019
    assert(first_table == all_tables && first_table != 0);
1912
1020
    TableList *aux_tables=
1913
 
      (TableList *)thd->lex->auxiliary_table_list.first;
 
1021
      (TableList *)session->lex->auxiliary_table_list.first;
1914
1022
    multi_delete *del_result;
1915
1023
 
1916
 
    if (!thd->locked_tables &&
1917
 
        !(need_start_waiting= !wait_if_global_read_lock(thd, 0, 1)))
 
1024
    if (!(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1918
1025
    {
1919
1026
      res= 1;
1920
1027
      break;
1921
1028
    }
1922
1029
 
1923
 
    if ((res= multi_delete_precheck(thd, all_tables)))
 
1030
    if ((res= multi_delete_precheck(session, all_tables)))
1924
1031
      break;
1925
1032
 
1926
1033
    /* condition will be true on SP re-excuting */
1927
1034
    if (select_lex->item_list.elements != 0)
1928
1035
      select_lex->item_list.empty();
1929
 
    if (add_item_to_list(thd, new Item_null()))
 
1036
    if (session->add_item_to_list(new Item_null()))
1930
1037
      goto error;
1931
1038
 
1932
 
    thd_proc_info(thd, "init");
1933
 
    if ((res= open_and_lock_tables(thd, all_tables)))
 
1039
    session->set_proc_info("init");
 
1040
    if ((res= session->open_and_lock_tables(all_tables)))
1934
1041
      break;
1935
1042
 
1936
 
    if ((res= mysql_multi_delete_prepare(thd)))
 
1043
    if ((res= mysql_multi_delete_prepare(session)))
1937
1044
      goto error;
1938
1045
 
1939
 
    if (!thd->is_fatal_error &&
 
1046
    if (!session->is_fatal_error &&
1940
1047
        (del_result= new multi_delete(aux_tables, lex->table_count)))
1941
1048
    {
1942
 
      res= mysql_select(thd, &select_lex->ref_pointer_array,
 
1049
      res= mysql_select(session, &select_lex->ref_pointer_array,
1943
1050
                        select_lex->get_table_list(),
1944
1051
                        select_lex->with_wild,
1945
1052
                        select_lex->item_list,
1946
1053
                        select_lex->where,
1947
1054
                        0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
1948
 
                        (order_st *)NULL,
1949
 
                        select_lex->options | thd->options |
1950
 
                        SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1951
 
                        OPTION_SETUP_TABLES_DONE,
 
1055
                        select_lex->options | session->options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK | OPTION_SETUP_TABLES_DONE,
1952
1056
                        del_result, unit, select_lex);
1953
 
      res|= thd->is_error();
 
1057
      res|= session->is_error();
1954
1058
      if (res)
1955
1059
        del_result->abort();
1956
1060
      delete del_result;
1964
1068
    assert(first_table == all_tables && first_table != 0);
1965
1069
    if (!lex->drop_temporary)
1966
1070
    {
1967
 
      if (end_active_trans(thd))
 
1071
      if (! session->endActiveTransaction())
1968
1072
        goto error;
1969
1073
    }
1970
1074
    else
1971
1075
    {
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
 
 
1983
1076
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
1984
 
      thd->options|= OPTION_KEEP_LOG;
 
1077
      session->options|= OPTION_KEEP_LOG;
1985
1078
    }
1986
1079
    /* DDL and binlog write order protected by LOCK_open */
1987
 
    res= mysql_rm_table(thd, first_table, lex->drop_if_exists, lex->drop_temporary);
 
1080
    res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1988
1081
  }
1989
1082
  break;
1990
1083
  case SQLCOM_SHOW_PROCESSLIST:
1991
 
    mysqld_list_processes(thd, NULL, lex->verbose);
 
1084
    mysqld_list_processes(session, NULL, lex->verbose);
1992
1085
    break;
1993
1086
  case SQLCOM_SHOW_ENGINE_LOGS:
1994
1087
    {
1995
 
      res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
 
1088
      res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1996
1089
      break;
1997
1090
    }
1998
1091
  case SQLCOM_CHANGE_DB:
1999
1092
  {
2000
1093
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
2001
1094
 
2002
 
    if (!mysql_change_db(thd, &db_str, false))
2003
 
      my_ok(thd);
 
1095
    if (!mysql_change_db(session, &db_str, false))
 
1096
      session->my_ok();
2004
1097
 
2005
1098
    break;
2006
1099
  }
2008
1101
  case SQLCOM_LOAD:
2009
1102
  {
2010
1103
    assert(first_table == all_tables && first_table != 0);
2011
 
    if (lex->local_file)
2012
 
    {
2013
 
      if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
2014
 
          !opt_local_infile)
2015
 
      {
2016
 
        my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
2017
 
        goto error;
2018
 
      }
2019
 
    }
2020
 
 
2021
 
    res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
2022
 
                    lex->update_list, lex->value_list, lex->duplicates,
2023
 
                    lex->ignore, (bool) lex->local_file);
 
1104
    res= mysql_load(session, lex->exchange, first_table, lex->field_list,
 
1105
                    lex->update_list, lex->value_list, lex->duplicates, lex->ignore);
2024
1106
    break;
2025
1107
  }
2026
1108
 
2028
1110
  {
2029
1111
    List<set_var_base> *lex_var_list= &lex->var_list;
2030
1112
 
2031
 
    if (lex->autocommit && end_active_trans(thd))
2032
 
      goto error;
2033
 
 
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);
 
1113
    if (session->open_and_lock_tables(all_tables))
 
1114
      goto error;
 
1115
    if (!(res= sql_set_variables(session, lex_var_list)))
 
1116
    {
 
1117
      session->my_ok();
2049
1118
    }
2050
1119
    else
2051
1120
    {
2054
1123
        Send something semi-generic here since we don't know which
2055
1124
        assignment in the list caused the error.
2056
1125
      */
2057
 
      if (!thd->is_error())
 
1126
      if (!session->is_error())
2058
1127
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
2059
1128
      goto error;
2060
1129
    }
2069
1138
      done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
2070
1139
      false, mysqldump will not work.
2071
1140
    */
2072
 
    unlock_locked_tables(thd);
2073
 
    if (thd->options & OPTION_TABLE_LOCK)
2074
 
    {
2075
 
      end_active_trans(thd);
2076
 
      thd->options&= ~(OPTION_TABLE_LOCK);
2077
 
    }
2078
 
    if (thd->global_read_lock)
2079
 
      unlock_global_read_lock(thd);
2080
 
    my_ok(thd);
2081
 
    break;
2082
 
  case SQLCOM_LOCK_TABLES:
2083
 
    /*
2084
 
      We try to take transactional locks if
2085
 
      - only transactional locks are requested (lex->lock_transactional) and
2086
 
      - no non-transactional locks exist (!thd->locked_tables).
2087
 
    */
2088
 
    if (lex->lock_transactional && !thd->locked_tables)
2089
 
    {
2090
 
      int rc;
2091
 
      /*
2092
 
        All requested locks are transactional and no non-transactional
2093
 
        locks exist.
2094
 
      */
2095
 
      if ((rc= try_transactional_lock(thd, all_tables)) == -1)
2096
 
        goto error;
2097
 
      if (rc == 0)
2098
 
      {
2099
 
        my_ok(thd);
2100
 
        break;
2101
 
      }
2102
 
      /*
2103
 
        Non-transactional locking has been requested or
2104
 
        non-transactional locks exist already or transactional locks are
2105
 
        not supported by all storage engines. Take non-transactional
2106
 
        locks.
2107
 
      */
2108
 
    }
2109
 
    /*
2110
 
      One or more requested locks are non-transactional and/or
2111
 
      non-transactional locks exist or a storage engine does not support
2112
 
      transactional locks. Check if at least one transactional lock is
2113
 
      requested. If yes, warn about the conversion to non-transactional
2114
 
      locks or abort in strict mode.
2115
 
    */
2116
 
    if (check_transactional_lock(thd, all_tables))
2117
 
      goto error;
2118
 
    unlock_locked_tables(thd);
2119
 
    /* we must end the trasaction first, regardless of anything */
2120
 
    if (end_active_trans(thd))
2121
 
      goto error;
2122
 
    thd->in_lock_tables=1;
2123
 
    thd->options|= OPTION_TABLE_LOCK;
2124
 
 
2125
 
    if (!(res= simple_open_n_lock_tables(thd, all_tables)))
2126
 
    {
2127
 
      thd->locked_tables=thd->lock;
2128
 
      thd->lock=0;
2129
 
      (void) set_handler_table_locks(thd, all_tables, false);
2130
 
      my_ok(thd);
2131
 
    }
2132
 
    else
2133
 
    {
2134
 
      /* 
2135
 
        Need to end the current transaction, so the storage engine (InnoDB)
2136
 
        can free its locks if LOCK TABLES locked some tables before finding
2137
 
        that it can't lock a table in its list
2138
 
      */
2139
 
      ha_autocommit_or_rollback(thd, 1);
2140
 
      end_active_trans(thd);
2141
 
      thd->options&= ~(OPTION_TABLE_LOCK);
2142
 
    }
2143
 
    thd->in_lock_tables=0;
 
1141
    if (session->global_read_lock)
 
1142
      unlock_global_read_lock(session);
 
1143
    session->my_ok();
2144
1144
    break;
2145
1145
  case SQLCOM_CREATE_DB:
2146
1146
  {
2150
1150
      prepared statement- safe.
2151
1151
    */
2152
1152
    HA_CREATE_INFO create_info(lex->create_info);
2153
 
    if (end_active_trans(thd))
 
1153
    if (! session->endActiveTransaction())
2154
1154
    {
2155
1155
      res= -1;
2156
1156
      break;
2157
1157
    }
2158
1158
    char *alias;
2159
 
    if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
 
1159
    if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
2160
1160
        check_db_name(&lex->name))
2161
1161
    {
2162
1162
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2163
1163
      break;
2164
1164
    }
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 :
2180
 
                              lex->name.str), &create_info, 0);
 
1165
    res= mysql_create_db(session,(lex->name.str), &create_info);
2181
1166
    break;
2182
1167
  }
2183
1168
  case SQLCOM_DROP_DB:
2184
1169
  {
2185
 
    if (end_active_trans(thd))
 
1170
    if (! session->endActiveTransaction())
2186
1171
    {
2187
1172
      res= -1;
2188
1173
      break;
2192
1177
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2193
1178
      break;
2194
1179
    }
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())
2210
 
    {
2211
 
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2212
 
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
1180
    if (session->inTransaction())
 
1181
    {
 
1182
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2213
1183
      goto error;
2214
1184
    }
2215
 
    res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
 
1185
    res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists);
2216
1186
    break;
2217
1187
  }
2218
1188
  case SQLCOM_ALTER_DB:
2224
1194
      my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
2225
1195
      break;
2226
1196
    }
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())
2242
 
    {
2243
 
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
2244
 
                 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
 
1197
    if (session->inTransaction())
 
1198
    {
 
1199
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
2245
1200
      goto error;
2246
1201
    }
2247
 
    res= mysql_alter_db(thd, db->str, &create_info);
 
1202
    res= mysql_alter_db(session, db->str, &create_info);
2248
1203
    break;
2249
1204
  }
2250
1205
  case SQLCOM_SHOW_CREATE_DB:
2254
1209
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
2255
1210
      break;
2256
1211
    }
2257
 
    res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
 
1212
    res= mysqld_show_create_db(session, lex->name.str, &lex->create_info);
2258
1213
    break;
2259
1214
  }
2260
 
  case SQLCOM_RESET:
2261
1215
  case SQLCOM_FLUSH:
2262
1216
  {
2263
 
    bool write_to_binlog;
2264
 
 
2265
1217
    /*
2266
1218
      reload_cache() will tell us if we are allowed to write to the
2267
1219
      binlog or not.
2268
1220
    */
2269
 
    if (!reload_cache(thd, lex->type, first_table, &write_to_binlog))
 
1221
    if (!reload_cache(session, lex->type, first_table))
2270
1222
    {
2271
1223
      /*
2272
1224
        We WANT to write and we CAN write.
2275
1227
      /*
2276
1228
        Presumably, RESET and binlog writing doesn't require synchronization
2277
1229
      */
2278
 
      write_bin_log(thd, false, thd->query, thd->query_length);
2279
 
      my_ok(thd);
2280
 
    } 
2281
 
    
 
1230
      write_bin_log(session, false, session->query, session->query_length);
 
1231
      session->my_ok();
 
1232
    }
 
1233
 
2282
1234
    break;
2283
1235
  }
2284
1236
  case SQLCOM_KILL:
2285
1237
  {
2286
1238
    Item *it= (Item *)lex->value_list.head();
2287
1239
 
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))
 
1240
    if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
2296
1241
    {
2297
1242
      my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
2298
1243
                 MYF(0));
2299
1244
      goto error;
2300
1245
    }
2301
 
    sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
 
1246
    sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
2302
1247
    break;
2303
1248
  }
2304
1249
  case SQLCOM_BEGIN:
2305
 
    if (thd->transaction.xid_state.xa_state != XA_NOTR)
 
1250
    if (session->transaction.xid_state.xa_state != XA_NOTR)
2306
1251
    {
2307
1252
      my_error(ER_XAER_RMFAIL, MYF(0),
2308
 
               xa_state_names[thd->transaction.xid_state.xa_state]);
 
1253
               xa_state_names[session->transaction.xid_state.xa_state]);
2309
1254
      break;
2310
1255
    }
2311
1256
    /*
2312
1257
      Breakpoints for backup testing.
2313
1258
    */
2314
 
    if (begin_trans(thd))
 
1259
    if (! session->startTransaction())
2315
1260
      goto error;
2316
 
    my_ok(thd);
 
1261
    session->my_ok();
2317
1262
    break;
2318
1263
  case SQLCOM_COMMIT:
2319
 
    if (end_trans(thd, lex->tx_release ? COMMIT_RELEASE :
2320
 
                              lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
 
1264
    if (! session->endTransaction(lex->tx_release ? COMMIT_RELEASE : lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
2321
1265
      goto error;
2322
 
    my_ok(thd);
 
1266
    session->my_ok();
2323
1267
    break;
2324
1268
  case SQLCOM_ROLLBACK:
2325
 
    if (end_trans(thd, lex->tx_release ? ROLLBACK_RELEASE :
2326
 
                              lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
 
1269
    if (! session->endTransaction(lex->tx_release ? ROLLBACK_RELEASE : lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
2327
1270
      goto error;
2328
 
    my_ok(thd);
 
1271
    session->my_ok();
2329
1272
    break;
2330
1273
  case SQLCOM_RELEASE_SAVEPOINT:
2331
1274
  {
2332
1275
    SAVEPOINT *sv;
2333
 
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
 
1276
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2334
1277
    {
2335
1278
      if (my_strnncoll(system_charset_info,
2336
1279
                       (unsigned char *)lex->ident.str, lex->ident.length,
2339
1282
    }
2340
1283
    if (sv)
2341
1284
    {
2342
 
      if (ha_release_savepoint(thd, sv))
 
1285
      if (ha_release_savepoint(session, sv))
2343
1286
        res= true; // cannot happen
2344
1287
      else
2345
 
        my_ok(thd);
2346
 
      thd->transaction.savepoints=sv->prev;
 
1288
        session->my_ok();
 
1289
      session->transaction.savepoints=sv->prev;
2347
1290
    }
2348
1291
    else
2349
1292
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2352
1295
  case SQLCOM_ROLLBACK_TO_SAVEPOINT:
2353
1296
  {
2354
1297
    SAVEPOINT *sv;
2355
 
    for (sv=thd->transaction.savepoints; sv; sv=sv->prev)
 
1298
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
2356
1299
    {
2357
1300
      if (my_strnncoll(system_charset_info,
2358
1301
                       (unsigned char *)lex->ident.str, lex->ident.length,
2361
1304
    }
2362
1305
    if (sv)
2363
1306
    {
2364
 
      if (ha_rollback_to_savepoint(thd, sv))
 
1307
      if (ha_rollback_to_savepoint(session, sv))
2365
1308
        res= true; // cannot happen
2366
1309
      else
2367
1310
      {
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,
 
1311
        if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
 
1312
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2372
1313
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
2373
1314
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
2374
 
        my_ok(thd);
 
1315
        session->my_ok();
2375
1316
      }
2376
 
      thd->transaction.savepoints=sv;
 
1317
      session->transaction.savepoints=sv;
2377
1318
    }
2378
1319
    else
2379
1320
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
2380
1321
    break;
2381
1322
  }
2382
1323
  case SQLCOM_SAVEPOINT:
2383
 
    if (!(thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN) ||
2384
 
          thd->in_sub_stmt) || !opt_using_transactions)
2385
 
      my_ok(thd);
 
1324
    if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
 
1325
      session->my_ok();
2386
1326
    else
2387
1327
    {
2388
1328
      SAVEPOINT **sv, *newsv;
2389
 
      for (sv=&thd->transaction.savepoints; *sv; sv=&(*sv)->prev)
 
1329
      for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
2390
1330
      {
2391
1331
        if (my_strnncoll(system_charset_info,
2392
1332
                         (unsigned char *)lex->ident.str, lex->ident.length,
2396
1336
      if (*sv) /* old savepoint of the same name exists */
2397
1337
      {
2398
1338
        newsv=*sv;
2399
 
        ha_release_savepoint(thd, *sv); // it cannot fail
 
1339
        ha_release_savepoint(session, *sv); // it cannot fail
2400
1340
        *sv=(*sv)->prev;
2401
1341
      }
2402
 
      else if ((newsv=(SAVEPOINT *) alloc_root(&thd->transaction.mem_root,
 
1342
      else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
2403
1343
                                               savepoint_alloc_size)) == 0)
2404
1344
      {
2405
1345
        my_error(ER_OUT_OF_RESOURCES, MYF(0));
2406
1346
        break;
2407
1347
      }
2408
 
      newsv->name=strmake_root(&thd->transaction.mem_root,
 
1348
      newsv->name=strmake_root(&session->transaction.mem_root,
2409
1349
                               lex->ident.str, lex->ident.length);
2410
1350
      newsv->length=lex->ident.length;
2411
1351
      /*
2413
1353
        we'll lose a little bit of memory in transaction mem_root, but it'll
2414
1354
        be free'd when transaction ends anyway
2415
1355
      */
2416
 
      if (ha_savepoint(thd, newsv))
 
1356
      if (ha_savepoint(session, newsv))
2417
1357
        res= true;
2418
1358
      else
2419
1359
      {
2420
 
        newsv->prev=thd->transaction.savepoints;
2421
 
        thd->transaction.savepoints=newsv;
2422
 
        my_ok(thd);
 
1360
        newsv->prev=session->transaction.savepoints;
 
1361
        session->transaction.savepoints=newsv;
 
1362
        session->my_ok();
2423
1363
      }
2424
1364
    }
2425
1365
    break;
2426
 
  case SQLCOM_BINLOG_BASE64_EVENT:
2427
 
  {
2428
 
    mysql_client_binlog_statement(thd);
2429
 
    break;
2430
 
  }
2431
1366
  default:
2432
1367
    assert(0);                             /* Impossible */
2433
 
    my_ok(thd);
 
1368
    session->my_ok();
2434
1369
    break;
2435
1370
  }
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);
 
1371
  session->set_proc_info("query end");
2450
1372
 
2451
1373
  /*
2452
1374
    The return value for ROW_COUNT() is "implementation dependent" if the
2454
1376
    wants. We also keep the last value in case of SQLCOM_CALL or
2455
1377
    SQLCOM_EXECUTE.
2456
1378
  */
2457
 
  if (!(sql_command_flags[lex->sql_command] & CF_HAS_ROW_COUNT))
2458
 
    thd->row_count_func= -1;
 
1379
  if (!(sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
1380
    session->row_count_func= -1;
2459
1381
 
2460
1382
  goto finish;
2461
1383
 
2469
1391
      Release the protection against the global read lock and wake
2470
1392
      everyone, who might want to set a global read lock.
2471
1393
    */
2472
 
    start_waiting_global_read_lock(thd);
 
1394
    start_waiting_global_read_lock(session);
2473
1395
  }
2474
 
  return(res || thd->is_error());
 
1396
  return(res || session->is_error());
2475
1397
}
2476
1398
 
2477
 
bool execute_sqlcom_select(THD *thd, TableList *all_tables)
 
1399
bool execute_sqlcom_select(Session *session, TableList *all_tables)
2478
1400
{
2479
 
  LEX   *lex= thd->lex;
 
1401
  LEX   *lex= session->lex;
2480
1402
  select_result *result=lex->result;
2481
 
  bool res;
 
1403
  bool res= false;
2482
1404
  /* assign global limit variable if limit is not given */
2483
1405
  {
2484
 
    SELECT_LEX *param= lex->unit.global_parameters;
 
1406
    Select_Lex *param= lex->unit.global_parameters;
2485
1407
    if (!param->explicit_limit)
2486
1408
      param->select_limit=
2487
 
        new Item_int((uint64_t) thd->variables.select_limit);
 
1409
        new Item_int((uint64_t) session->variables.select_limit);
2488
1410
  }
2489
 
  if (!(res= open_and_lock_tables(thd, all_tables)))
 
1411
  if (!(res= session->open_and_lock_tables(all_tables)))
2490
1412
  {
2491
1413
    if (lex->describe)
2492
1414
    {
2497
1419
        even if the query itself redirects the output.
2498
1420
      */
2499
1421
      if (!(result= new select_send()))
2500
 
        return 1;                               /* purecov: inspected */
2501
 
      thd->send_explain_fields(result);
2502
 
      res= mysql_explain_union(thd, &thd->lex->unit, result);
 
1422
        return true;                               /* purecov: inspected */
 
1423
      session->send_explain_fields(result);
 
1424
      res= mysql_explain_union(session, &session->lex->unit, result);
2503
1425
      if (lex->describe & DESCRIBE_EXTENDED)
2504
1426
      {
2505
1427
        char buff[1024];
2506
1428
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
2507
1429
        str.length(0);
2508
 
        thd->lex->unit.print(&str, QT_ORDINARY);
 
1430
        session->lex->unit.print(&str, QT_ORDINARY);
2509
1431
        str.append('\0');
2510
 
        push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
1432
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2511
1433
                     ER_YES, str.ptr());
2512
1434
      }
2513
1435
      if (res)
2519
1441
    else
2520
1442
    {
2521
1443
      if (!result && !(result= new select_send()))
2522
 
        return 1;                               /* purecov: inspected */
2523
 
      res= handle_select(thd, lex, result, 0);
 
1444
        return true;                               /* purecov: inspected */
 
1445
      res= handle_select(session, lex, result, 0);
2524
1446
      if (result != lex->result)
2525
1447
        delete result;
2526
1448
    }
2528
1450
  return res;
2529
1451
}
2530
1452
 
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
 
}
2562
1453
 
2563
1454
#define MY_YACC_INIT 1000                       // Start with big alloc
2564
1455
#define MY_YACC_MAX  32000                      // Because of 'short'
2565
1456
 
2566
1457
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
2567
1458
{
2568
 
  LEX   *lex= current_thd->lex;
 
1459
  LEX   *lex= current_session->lex;
2569
1460
  ulong old_info=0;
2570
1461
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
2571
1462
    return 1;
2572
1463
  if (!lex->yacc_yyvs)
2573
1464
    old_info= *yystacksize;
2574
1465
  *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
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;
 
1466
  unsigned char *tmpptr= NULL;
 
1467
  if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
 
1468
                                         *yystacksize* sizeof(**yyvs))))
 
1469
      return 1;
 
1470
  lex->yacc_yyvs= tmpptr;
 
1471
  tmpptr= NULL;
 
1472
  if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
 
1473
                                        *yystacksize* sizeof(**yyss))))
 
1474
      return 1;
 
1475
  lex->yacc_yyss= tmpptr;
2584
1476
  if (old_info)
2585
1477
  {                                             // Copy old info from stack
2586
1478
    memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
2592
1484
}
2593
1485
 
2594
1486
 
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
 
 
2660
1487
void
2661
1488
mysql_init_select(LEX *lex)
2662
1489
{
2663
 
  SELECT_LEX *select_lex= lex->current_select;
 
1490
  Select_Lex *select_lex= lex->current_select;
2664
1491
  select_lex->init_select();
2665
1492
  lex->wild= 0;
2666
1493
  if (select_lex == &lex->select_lex)
2674
1501
bool
2675
1502
mysql_new_select(LEX *lex, bool move_down)
2676
1503
{
2677
 
  SELECT_LEX *select_lex;
2678
 
  THD *thd= lex->thd;
 
1504
  Select_Lex *select_lex;
 
1505
  Session *session= lex->session;
2679
1506
 
2680
 
  if (!(select_lex= new (thd->mem_root) SELECT_LEX()))
 
1507
  if (!(select_lex= new (session->mem_root) Select_Lex()))
2681
1508
    return(1);
2682
 
  select_lex->select_number= ++thd->select_number;
 
1509
  select_lex->select_number= ++session->select_number;
2683
1510
  select_lex->parent_lex= lex; /* Used in init_query. */
2684
1511
  select_lex->init_query();
2685
1512
  select_lex->init_select();
2692
1519
  select_lex->nest_level= lex->nest_level;
2693
1520
  if (move_down)
2694
1521
  {
2695
 
    SELECT_LEX_UNIT *unit;
2696
 
    lex->subqueries= true;
 
1522
    Select_Lex_Unit *unit;
2697
1523
    /* first select_lex of subselect or derived table */
2698
 
    if (!(unit= new (thd->mem_root) SELECT_LEX_UNIT()))
 
1524
    if (!(unit= new (session->mem_root) Select_Lex_Unit()))
2699
1525
      return(1);
2700
1526
 
2701
1527
    unit->init_query();
2702
1528
    unit->init_select();
2703
 
    unit->thd= thd;
 
1529
    unit->session= session;
2704
1530
    unit->include_down(lex->current_select);
2705
1531
    unit->link_next= 0;
2706
1532
    unit->link_prev= 0;
2720
1546
      return(1);
2721
1547
    }
2722
1548
    select_lex->include_neighbour(lex->current_select);
2723
 
    SELECT_LEX_UNIT *unit= select_lex->master_unit();                              
2724
 
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
 
1549
    Select_Lex_Unit *unit= select_lex->master_unit();
 
1550
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
2725
1551
      return(1);
2726
 
    select_lex->context.outer_context= 
 
1552
    select_lex->context.outer_context=
2727
1553
                unit->first_select()->context.outer_context;
2728
1554
  }
2729
1555
 
2730
1556
  select_lex->master_unit()->global_parameters= select_lex;
2731
 
  select_lex->include_global((st_select_lex_node**)&lex->all_selects_list);
 
1557
  select_lex->include_global((Select_Lex_Node**)&lex->all_selects_list);
2732
1558
  lex->current_select= select_lex;
2733
1559
  /*
2734
1560
    in subquery is SELECT query and we allow resolution of names in SELECT
2750
1576
 
2751
1577
void create_select_for_variable(const char *var_name)
2752
1578
{
2753
 
  THD *thd;
 
1579
  Session *session;
2754
1580
  LEX *lex;
2755
1581
  LEX_STRING tmp, null_lex_string;
2756
1582
  Item *var;
2757
 
  char buff[MAX_SYS_VAR_LENGTH*2+4+8], *end;
 
1583
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
 
1584
  char *end= buff;
2758
1585
 
2759
 
  thd= current_thd;
2760
 
  lex= thd->lex;
 
1586
  session= current_session;
 
1587
  lex= session->lex;
2761
1588
  mysql_init_select(lex);
2762
1589
  lex->sql_command= SQLCOM_SELECT;
2763
1590
  tmp.str= (char*) var_name;
2767
1594
    We set the name of Item to @@session.var_name because that then is used
2768
1595
    as the column name in the output.
2769
1596
  */
2770
 
  if ((var= get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
 
1597
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
2771
1598
  {
2772
 
    end= strxmov(buff, "@@session.", var_name, NULL);
 
1599
    end+= sprintf(buff, "@@session.%s", var_name);
2773
1600
    var->set_name(buff, end-buff, system_charset_info);
2774
 
    add_item_to_list(thd, var);
 
1601
    session->add_item_to_list(var);
2775
1602
  }
2776
1603
  return;
2777
1604
}
2784
1611
  lex->select_lex.select_limit= 0;
2785
1612
  lex->unit.select_limit_cnt= HA_POS_ERROR;
2786
1613
  lex->select_lex.table_list.save_and_clear(&lex->auxiliary_table_list);
2787
 
  lex->lock_option= using_update_log ? TL_READ_NO_INSERT : TL_READ;
 
1614
  lex->lock_option= TL_READ;
2788
1615
  lex->query_tables= 0;
2789
1616
  lex->query_tables_last= &lex->query_tables;
2790
1617
}
2791
1618
 
2792
1619
 
2793
 
/*
2794
 
  When you modify mysql_parse(), you may need to mofify
2795
 
  mysql_test_parse_for_slave() in this same file.
2796
 
*/
2797
 
 
2798
1620
/**
2799
1621
  Parse a query.
2800
1622
 
2801
 
  @param       thd     Current thread
 
1623
  @param       session     Current thread
2802
1624
  @param       inBuf   Begining of the query text
2803
1625
  @param       length  Length of the query text
2804
1626
  @param[out]  found_semicolon For multi queries, position of the character of
2805
1627
                               the next query in the query text.
2806
1628
*/
2807
1629
 
2808
 
void mysql_parse(THD *thd, const char *inBuf, uint32_t length,
 
1630
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
2809
1631
                 const char ** found_semicolon)
2810
1632
{
2811
1633
  /*
2816
1638
    - first, call query_cache_send_result_to_client,
2817
1639
    - second, if caching failed, initialise the lexical and syntactic parser.
2818
1640
    The problem is that the query cache depends on a clean initialization
2819
 
    of (among others) lex->safe_to_cache_query and thd->server_status,
 
1641
    of (among others) lex->safe_to_cache_query and session->server_status,
2820
1642
    which are reset respectively in
2821
1643
    - lex_start()
2822
 
    - mysql_reset_thd_for_next_command()
 
1644
    - mysql_reset_session_for_next_command()
2823
1645
    So, initializing the lexical analyser *before* using the query cache
2824
1646
    is required for the cache to work properly.
2825
1647
    FIXME: cleanup the dependencies in the code to simplify this.
2826
1648
  */
2827
 
  lex_start(thd);
2828
 
  mysql_reset_thd_for_next_command(thd);
 
1649
  lex_start(session);
 
1650
  session->reset_for_next_command();
2829
1651
 
2830
1652
  {
2831
 
    LEX *lex= thd->lex;
2832
 
 
2833
 
    Lex_input_stream lip(thd, inBuf, length);
2834
 
 
2835
 
    bool err= parse_sql(thd, &lip);
 
1653
    LEX *lex= session->lex;
 
1654
 
 
1655
    Lex_input_stream lip(session, inBuf, length);
 
1656
 
 
1657
    bool err= parse_sql(session, &lip);
2836
1658
    *found_semicolon= lip.found_semicolon;
2837
1659
 
2838
1660
    if (!err)
2839
1661
    {
2840
1662
      {
2841
 
        if (! thd->is_error())
 
1663
        if (! session->is_error())
2842
1664
        {
2843
1665
          /*
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
 
1666
            Binlog logs a string starting from session->query and having length
 
1667
            session->query_length; so we set session->query_length correctly (to not
2846
1668
            log several statements in one event, when we executed only first).
2847
1669
            We set it to not see the ';' (otherwise it would get into binlog
2848
1670
            and Query_log_event::print() would give ';;' output).
2851
1673
            Note that we don't need LOCK_thread_count to modify query_length.
2852
1674
          */
2853
1675
          if (*found_semicolon &&
2854
 
              (thd->query_length= (ulong)(*found_semicolon - thd->query)))
2855
 
            thd->query_length--;
 
1676
              (session->query_length= (ulong)(*found_semicolon - session->query)))
 
1677
            session->query_length--;
2856
1678
          /* Actually execute the query */
2857
 
          mysql_execute_command(thd);
 
1679
          mysql_execute_command(session);
2858
1680
        }
2859
1681
      }
2860
1682
    }
2861
1683
    else
2862
1684
    {
2863
 
      assert(thd->is_error());
 
1685
      assert(session->is_error());
2864
1686
    }
2865
1687
    lex->unit.cleanup();
2866
 
    thd_proc_info(thd, "freeing items");
2867
 
    thd->end_statement();
2868
 
    thd->cleanup_after_query();
2869
 
    assert(thd->change_list.is_empty());
 
1688
    session->set_proc_info("freeing items");
 
1689
    session->end_statement();
 
1690
    session->cleanup_after_query();
2870
1691
  }
2871
1692
 
2872
1693
  return;
2873
1694
}
2874
1695
 
2875
1696
 
2876
 
/*
2877
 
  Usable by the replication SQL thread only: just parse a query to know if it
2878
 
  can be ignored because of replicate-*-table rules.
2879
 
 
2880
 
  @retval
2881
 
    0   cannot be ignored
2882
 
  @retval
2883
 
    1   can be ignored
2884
 
*/
2885
 
 
2886
 
bool mysql_test_parse_for_slave(THD *thd, char *inBuf, uint32_t length)
2887
 
{
2888
 
  LEX *lex= thd->lex;
2889
 
  bool error= 0;
2890
 
 
2891
 
  Lex_input_stream lip(thd, inBuf, length);
2892
 
  lex_start(thd);
2893
 
  mysql_reset_thd_for_next_command(thd);
2894
 
 
2895
 
  if (!parse_sql(thd, &lip) &&
2896
 
      all_tables_not_ok(thd,(TableList*) lex->select_lex.table_list.first))
2897
 
    error= 1;                  /* Ignore question */
2898
 
  thd->end_statement();
2899
 
  thd->cleanup_after_query();
2900
 
  return(error);
2901
 
}
2902
 
 
2903
 
 
2904
1697
 
2905
1698
/**
2906
1699
  Store field definition for create.
2909
1702
    Return 0 if ok
2910
1703
*/
2911
1704
 
2912
 
bool add_field_to_list(THD *thd, LEX_STRING *field_name, enum_field_types type,
 
1705
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
2913
1706
                       char *length, char *decimals,
2914
1707
                       uint32_t type_modifier,
2915
1708
                       enum column_format_type column_format,
2918
1711
                       char *change,
2919
1712
                       List<String> *interval_list, const CHARSET_INFO * const cs)
2920
1713
{
2921
 
  register Create_field *new_field;
2922
 
  LEX  *lex= thd->lex;
 
1714
  register CreateField *new_field;
 
1715
  LEX  *lex= session->lex;
2923
1716
 
2924
1717
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
2925
1718
    return(1);                          /* purecov: inspected */
2947
1740
 
2948
1741
  if (default_value)
2949
1742
  {
2950
 
    /* 
 
1743
    /*
2951
1744
      Default value should be literal => basic constants =>
2952
1745
      no need fix_fields()
2953
 
      
2954
 
      We allow only one function as part of default value - 
 
1746
 
 
1747
      We allow only one function as part of default value -
2955
1748
      NOW() as default for TIMESTAMP type.
2956
1749
    */
2957
 
    if (default_value->type() == Item::FUNC_ITEM && 
 
1750
    if (default_value->type() == Item::FUNC_ITEM &&
2958
1751
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
2959
1752
         type == DRIZZLE_TYPE_TIMESTAMP))
2960
1753
    {
2984
1777
    return(1);
2985
1778
  }
2986
1779
 
2987
 
  if (!(new_field= new Create_field()) ||
2988
 
      new_field->init(thd, field_name->str, type, length, decimals, type_modifier,
 
1780
  if (!(new_field= new CreateField()) ||
 
1781
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
2989
1782
                      default_value, on_update_value, comment, change,
2990
1783
                      interval_list, cs, 0, column_format))
2991
1784
    return(1);
3000
1793
 
3001
1794
void store_position_for_column(const char *name)
3002
1795
{
3003
 
  current_thd->lex->last_field->after=const_cast<char*> (name);
3004
 
}
3005
 
 
3006
 
bool
3007
 
add_proc_to_list(THD* thd, Item *item)
3008
 
{
3009
 
  order_st *order;
3010
 
  Item  **item_ptr;
3011
 
 
3012
 
  if (!(order = (order_st *) thd->alloc(sizeof(order_st)+sizeof(Item*))))
3013
 
    return 1;
3014
 
  item_ptr = (Item**) (order+1);
3015
 
  *item_ptr= item;
3016
 
  order->item=item_ptr;
3017
 
  order->free_me=0;
3018
 
  thd->lex->proc_list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
3019
 
  return 0;
3020
 
}
3021
 
 
 
1796
  current_session->lex->last_field->after=const_cast<char*> (name);
 
1797
}
3022
1798
 
3023
1799
/**
3024
1800
  save order by and tables in own lists.
3025
1801
*/
3026
1802
 
3027
 
bool add_to_list(THD *thd, SQL_LIST &list,Item *item,bool asc)
 
1803
bool add_to_list(Session *session, SQL_LIST &list,Item *item,bool asc)
3028
1804
{
3029
1805
  order_st *order;
3030
 
  if (!(order = (order_st *) thd->alloc(sizeof(order_st))))
 
1806
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
3031
1807
    return(1);
3032
1808
  order->item_ptr= item;
3033
1809
  order->item= &order->item_ptr;
3059
1835
    \#  Pointer to TableList element added to the total table list
3060
1836
*/
3061
1837
 
3062
 
TableList *st_select_lex::add_table_to_list(THD *thd,
 
1838
TableList *Select_Lex::add_table_to_list(Session *session,
3063
1839
                                             Table_ident *table,
3064
1840
                                             LEX_STRING *alias,
3065
1841
                                             uint32_t table_options,
3070
1846
  register TableList *ptr;
3071
1847
  TableList *previous_table_ref; /* The table preceding the current one. */
3072
1848
  char *alias_str;
3073
 
  LEX *lex= thd->lex;
 
1849
  LEX *lex= session->lex;
3074
1850
 
3075
1851
  if (!table)
3076
 
    return(0);                          // End of memory
 
1852
    return NULL;                                // End of memory
3077
1853
  alias_str= alias ? alias->str : table->table.str;
3078
 
  if (!test(table_options & TL_OPTION_ALIAS) && 
 
1854
  if (!test(table_options & TL_OPTION_ALIAS) &&
3079
1855
      check_table_name(table->table.str, table->table.length))
3080
1856
  {
3081
1857
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
3082
 
    return(0);
 
1858
    return NULL;
3083
1859
  }
3084
1860
 
3085
1861
  if (table->is_derived_table() == false && table->db.str &&
3086
1862
      check_db_name(&table->db))
3087
1863
  {
3088
1864
    my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
3089
 
    return(0);
 
1865
    return NULL;
3090
1866
  }
3091
1867
 
3092
1868
  if (!alias)                                   /* Alias is case sensitive */
3095
1871
    {
3096
1872
      my_message(ER_DERIVED_MUST_HAVE_ALIAS,
3097
1873
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
3098
 
      return(0);
 
1874
      return NULL;
3099
1875
    }
3100
 
    if (!(alias_str= (char*) thd->memdup(alias_str,table->table.length+1)))
3101
 
      return(0);
 
1876
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
 
1877
      return NULL;
3102
1878
  }
3103
 
  if (!(ptr = (TableList *) thd->calloc(sizeof(TableList))))
3104
 
    return(0);                          /* purecov: inspected */
 
1879
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
 
1880
    return NULL;                                /* purecov: inspected */
3105
1881
  if (table->db.str)
3106
1882
  {
3107
1883
    ptr->is_fqtn= true;
3109
1885
    ptr->db_length= table->db.length;
3110
1886
  }
3111
1887
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
3112
 
    return(0);
 
1888
    return NULL;
3113
1889
  else
3114
1890
    ptr->is_fqtn= false;
3115
1891
 
3116
1892
  ptr->alias= alias_str;
3117
1893
  ptr->is_alias= alias ? true : false;
3118
 
  if (lower_case_table_names && table->table.length)
 
1894
  if (table->table.length)
3119
1895
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
3120
1896
  ptr->table_name=table->table.str;
3121
1897
  ptr->table_name_length=table->table.length;
3122
1898
  ptr->lock_type=   lock_type;
3123
 
  ptr->lock_timeout= -1;      /* default timeout */
3124
 
  ptr->lock_transactional= 1; /* allow transactional locks */
3125
1899
  ptr->updating=    test(table_options & TL_OPTION_UPDATING);
3126
1900
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
3127
1901
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
3128
1902
  ptr->derived=     table->sel;
3129
1903
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
3130
 
                                      INFORMATION_SCHEMA_NAME.str))
 
1904
                                      INFORMATION_SCHEMA_NAME.c_str()))
3131
1905
  {
3132
 
    ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, ptr->table_name);
 
1906
    InfoSchemaTable *schema_table= find_schema_table(ptr->table_name);
3133
1907
    if (!schema_table ||
3134
 
        (schema_table->hidden && 
3135
 
         ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 || 
 
1908
        (schema_table->hidden &&
 
1909
         ((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
3136
1910
          /*
3137
1911
            this check is used for show columns|keys from I_S hidden table
3138
1912
          */
3140
1914
          lex->sql_command == SQLCOM_SHOW_KEYS)))
3141
1915
    {
3142
1916
      my_error(ER_UNKNOWN_TABLE, MYF(0),
3143
 
               ptr->table_name, INFORMATION_SCHEMA_NAME.str);
3144
 
      return(0);
 
1917
               ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
 
1918
      return NULL;
3145
1919
    }
3146
1920
    ptr->schema_table_name= ptr->table_name;
3147
1921
    ptr->schema_table= schema_table;
3148
1922
  }
3149
1923
  ptr->select_lex=  lex->current_select;
3150
 
  ptr->cacheable_table= 1;
3151
1924
  ptr->index_hints= index_hints_arg;
3152
1925
  ptr->option= option ? option->str : 0;
3153
1926
  /* check that used name is unique */
3162
1935
          !strcmp(ptr->db, tables->db))
3163
1936
      {
3164
1937
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str); /* purecov: tested */
3165
 
        return(0);                              /* purecov: tested */
 
1938
        return NULL;                            /* purecov: tested */
3166
1939
      }
3167
1940
    }
3168
1941
  }
3197
1970
  ptr->next_name_resolution_table= NULL;
3198
1971
  /* Link table in global list (all used tables) */
3199
1972
  lex->add_to_query_tables(ptr);
3200
 
  return(ptr);
 
1973
  return ptr;
3201
1974
}
3202
1975
 
3203
1976
 
3207
1980
    The function initializes a structure of the TableList type
3208
1981
    for a nested join. It sets up its nested join list as empty.
3209
1982
    The created structure is added to the front of the current
3210
 
    join list in the st_select_lex object. Then the function
 
1983
    join list in the Select_Lex object. Then the function
3211
1984
    changes the current nest level for joins to refer to the newly
3212
1985
    created empty list after having saved the info on the old level
3213
1986
    in the initialized structure.
3214
1987
 
3215
 
  @param thd         current thread
 
1988
  @param session         current thread
3216
1989
 
3217
1990
  @retval
3218
1991
    0   if success
3220
1993
    1   otherwise
3221
1994
*/
3222
1995
 
3223
 
bool st_select_lex::init_nested_join(THD *thd)
 
1996
bool Select_Lex::init_nested_join(Session *session)
3224
1997
{
3225
1998
  TableList *ptr;
3226
1999
  nested_join_st *nested_join;
3227
2000
 
3228
 
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2001
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
3229
2002
                                       sizeof(nested_join_st))))
3230
 
    return(1);
 
2003
    return true;
3231
2004
  nested_join= ptr->nested_join=
3232
2005
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
3233
2006
 
3238
2011
  embedding= ptr;
3239
2012
  join_list= &nested_join->join_list;
3240
2013
  join_list->empty();
3241
 
  return(0);
 
2014
  return false;
3242
2015
}
3243
2016
 
3244
2017
 
3249
2022
    If the current level contains only one member, the function
3250
2023
    moves it one level up, eliminating the nest.
3251
2024
 
3252
 
  @param thd         current thread
 
2025
  @param session         current thread
3253
2026
 
3254
2027
  @return
3255
2028
    - Pointer to TableList element added to the total table list, if success
3256
2029
    - 0, otherwise
3257
2030
*/
3258
2031
 
3259
 
TableList *st_select_lex::end_nested_join(THD *thd __attribute__((unused)))
 
2032
TableList *Select_Lex::end_nested_join(Session *)
3260
2033
{
3261
2034
  TableList *ptr;
3262
2035
  nested_join_st *nested_join;
3278
2051
  else if (nested_join->join_list.elements == 0)
3279
2052
  {
3280
2053
    join_list->pop();
3281
 
    ptr= 0;                                     // return value
 
2054
    ptr= NULL;                                     // return value
3282
2055
  }
3283
 
  return(ptr);
 
2056
  return ptr;
3284
2057
}
3285
2058
 
3286
2059
 
3289
2062
 
3290
2063
    The function nest last join operation as if it was enclosed in braces.
3291
2064
 
3292
 
  @param thd         current thread
 
2065
  @param session         current thread
3293
2066
 
3294
2067
  @retval
3295
2068
    0  Error
3297
2070
    \#  Pointer to TableList element created for the new nested join
3298
2071
*/
3299
2072
 
3300
 
TableList *st_select_lex::nest_last_join(THD *thd)
 
2073
TableList *Select_Lex::nest_last_join(Session *session)
3301
2074
{
3302
2075
  TableList *ptr;
3303
2076
  nested_join_st *nested_join;
3304
2077
  List<TableList> *embedded_list;
3305
2078
 
3306
 
  if (!(ptr= (TableList*) thd->calloc(ALIGN_SIZE(sizeof(TableList))+
 
2079
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
3307
2080
                                       sizeof(nested_join_st))))
3308
 
    return(0);
 
2081
    return NULL;
3309
2082
  nested_join= ptr->nested_join=
3310
2083
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
3311
2084
 
3334
2107
  }
3335
2108
  join_list->push_front(ptr);
3336
2109
  nested_join->used_tables= nested_join->not_null_tables= (table_map) 0;
3337
 
  return(ptr);
 
2110
  return ptr;
3338
2111
}
3339
2112
 
3340
2113
 
3342
2115
  Add a table to the current join list.
3343
2116
 
3344
2117
    The function puts a table in front of the current join list
3345
 
    of st_select_lex object.
 
2118
    of Select_Lex object.
3346
2119
    Thus, joined tables are put into this list in the reverse order
3347
2120
    (the most outer join operation follows first).
3348
2121
 
3352
2125
    None
3353
2126
*/
3354
2127
 
3355
 
void st_select_lex::add_joined_table(TableList *table)
 
2128
void Select_Lex::add_joined_table(TableList *table)
3356
2129
{
3357
2130
  join_list->push_front(table);
3358
2131
  table->join_list= join_list;
3359
2132
  table->embedding= embedding;
3360
 
  return;
3361
2133
}
3362
2134
 
3363
2135
 
3385
2157
      SELECT * FROM t3 LEFT JOIN (t1 LEFT JOIN t2 ON on_expr2) ON on_expr1
3386
2158
   @endverbatim
3387
2159
 
3388
 
  @param thd         current thread
 
2160
  @param session         current thread
3389
2161
 
3390
2162
  @return
3391
2163
    - Pointer to the table representing the inner table, if success
3392
2164
    - 0, otherwise
3393
2165
*/
3394
2166
 
3395
 
TableList *st_select_lex::convert_right_join()
 
2167
TableList *Select_Lex::convert_right_join()
3396
2168
{
3397
2169
  TableList *tab2= join_list->pop();
3398
2170
  TableList *tab1= join_list->pop();
3401
2173
  join_list->push_front(tab1);
3402
2174
  tab1->outer_join|= JOIN_TYPE_RIGHT;
3403
2175
 
3404
 
  return(tab1);
 
2176
  return tab1;
3405
2177
}
3406
2178
 
3407
2179
/**
3415
2187
    query
3416
2188
*/
3417
2189
 
3418
 
void st_select_lex::set_lock_for_tables(thr_lock_type lock_type)
 
2190
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
3419
2191
{
3420
2192
  bool for_update= lock_type >= TL_READ_NO_INSERT;
3421
2193
 
3431
2203
 
3432
2204
 
3433
2205
/**
3434
 
  Create a fake SELECT_LEX for a unit.
 
2206
  Create a fake Select_Lex for a unit.
3435
2207
 
3436
 
    The method create a fake SELECT_LEX object for a unit.
 
2208
    The method create a fake Select_Lex object for a unit.
3437
2209
    This object is created for any union construct containing a union
3438
2210
    operation and also for any single select union construct of the form
3439
2211
    @verbatim
3440
 
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ... 
 
2212
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ...
3441
2213
    @endvarbatim
3442
2214
    or of the form
3443
2215
    @varbatim
3444
2216
    (SELECT ... order_st BY LIMIT n) order_st BY ...
3445
2217
    @endvarbatim
3446
 
  
3447
 
  @param thd_arg                   thread handle
 
2218
 
 
2219
  @param session_arg               thread handle
3448
2220
 
3449
2221
  @note
3450
2222
    The object is used to retrieve rows from the temporary table
3456
2228
    0     on success
3457
2229
*/
3458
2230
 
3459
 
bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
 
2231
bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
3460
2232
{
3461
 
  SELECT_LEX *first_sl= first_select();
 
2233
  Select_Lex *first_sl= first_select();
3462
2234
  assert(!fake_select_lex);
3463
2235
 
3464
 
  if (!(fake_select_lex= new (thd_arg->mem_root) SELECT_LEX()))
 
2236
  if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
3465
2237
      return(1);
3466
 
  fake_select_lex->include_standalone(this, 
3467
 
                                      (SELECT_LEX_NODE**)&fake_select_lex);
 
2238
  fake_select_lex->include_standalone(this,
 
2239
                                      (Select_Lex_Node**)&fake_select_lex);
3468
2240
  fake_select_lex->select_number= INT_MAX;
3469
 
  fake_select_lex->parent_lex= thd_arg->lex; /* Used in init_query. */
 
2241
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
3470
2242
  fake_select_lex->make_empty_select();
3471
2243
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
3472
2244
  fake_select_lex->select_limit= 0;
3478
2250
 
3479
2251
  if (!is_union())
3480
2252
  {
3481
 
    /* 
3482
 
      This works only for 
 
2253
    /*
 
2254
      This works only for
3483
2255
      (SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
3484
2256
      (SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
3485
2257
      just before the parser starts processing order_list
3486
 
    */ 
 
2258
    */
3487
2259
    global_parameters= fake_select_lex;
3488
2260
    fake_select_lex->no_table_names_allowed= 1;
3489
 
    thd_arg->lex->current_select= fake_select_lex;
 
2261
    session_arg->lex->current_select= fake_select_lex;
3490
2262
  }
3491
 
  thd_arg->lex->pop_context();
 
2263
  session_arg->lex->pop_context();
3492
2264
  return(0);
3493
2265
}
3494
2266
 
3502
2274
    to be used for name resolution, and push the newly created
3503
2275
    context to the stack of contexts of the query.
3504
2276
 
3505
 
  @param thd       pointer to current thread
 
2277
  @param session       pointer to current thread
3506
2278
  @param left_op   left  operand of the JOIN
3507
2279
  @param right_op  rigth operand of the JOIN
3508
2280
 
3513
2285
*/
3514
2286
 
3515
2287
bool
3516
 
push_new_name_resolution_context(THD *thd,
 
2288
push_new_name_resolution_context(Session *session,
3517
2289
                                 TableList *left_op, TableList *right_op)
3518
2290
{
3519
2291
  Name_resolution_context *on_context;
3520
 
  if (!(on_context= new (thd->mem_root) Name_resolution_context))
 
2292
  if (!(on_context= new (session->mem_root) Name_resolution_context))
3521
2293
    return true;
3522
2294
  on_context->init();
3523
2295
  on_context->first_name_resolution_table=
3524
2296
    left_op->first_leaf_for_name_resolution();
3525
2297
  on_context->last_name_resolution_table=
3526
2298
    right_op->last_leaf_for_name_resolution();
3527
 
  return thd->lex->push_context(on_context);
 
2299
  return session->lex->push_context(on_context);
3528
2300
}
3529
2301
 
3530
2302
 
3596
2368
*/
3597
2369
 
3598
2370
void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
3599
 
                      SELECT_LEX *lex)
 
2371
                      Select_Lex *lex)
3600
2372
{
3601
2373
  b->natural_join= a;
3602
2374
  lex->prev_join_using= using_fields;
3606
2378
/**
3607
2379
  Reload/resets privileges and the different caches.
3608
2380
 
3609
 
  @param thd Thread handler (can be NULL!)
 
2381
  @param session Thread handler (can be NULL!)
3610
2382
  @param options What should be reset/reloaded (tables, privileges, slave...)
3611
2383
  @param tables Tables to flush (if any)
3612
2384
  @param write_to_binlog True if we can write to the binlog.
3613
 
               
 
2385
 
3614
2386
  @note Depending on 'options', it may be very bad to write the
3615
2387
    query to the binlog (e.g. FLUSH SLAVE); this is a
3616
2388
    pointer where reload_cache() will put 0 if
3619
2391
 
3620
2392
  @return Error status code
3621
2393
    @retval 0 Ok
3622
 
    @retval !=0  Error; thd->killed is set or thd->is_error() is true
 
2394
    @retval !=0  Error; session->killed is set or session->is_error() is true
3623
2395
*/
3624
2396
 
3625
 
bool reload_cache(THD *thd, ulong options, TableList *tables,
3626
 
                          bool *write_to_binlog)
 
2397
bool reload_cache(Session *session, ulong options, TableList *tables)
3627
2398
{
3628
2399
  bool result=0;
3629
2400
  select_errors=0;                              /* Write if more errors */
3630
 
  bool tmp_write_to_binlog= 1;
3631
 
 
3632
 
  assert(!thd || !thd->in_sub_stmt);
3633
2401
 
3634
2402
  if (options & REFRESH_LOG)
3635
2403
  {
3636
 
    /*
3637
 
      Flush the normal query log, the update log, the binary log,
3638
 
      the slow query log, the relay log (if it exists) and the log
3639
 
      tables.
3640
 
    */
3641
 
 
3642
 
    /*
3643
 
      Writing this command to the binlog may result in infinite loops
3644
 
      when doing mysqlbinlog|mysql, and anyway it does not really make
3645
 
      sense to log it automatically (would cause more trouble to users
3646
 
      than it would help them)
3647
 
    */
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);
3659
 
 
3660
2404
    if (ha_flush_logs(NULL))
3661
2405
      result=1;
3662
 
    if (flush_error_log())
3663
 
      result=1;
3664
2406
  }
3665
2407
  /*
3666
2408
    Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
3667
2409
    (see sql_yacc.yy)
3668
2410
  */
3669
 
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK)) 
 
2411
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
3670
2412
  {
3671
 
    if ((options & REFRESH_READ_LOCK) && thd)
 
2413
    if ((options & REFRESH_READ_LOCK) && session)
3672
2414
    {
3673
 
      /*
3674
 
        We must not try to aspire a global read lock if we have a write
3675
 
        locked table. This would lead to a deadlock when trying to
3676
 
        reopen (and re-lock) the table after the flush.
3677
 
      */
3678
 
      if (thd->locked_tables)
3679
 
      {
3680
 
        THR_LOCK_DATA **lock_p= thd->locked_tables->locks;
3681
 
        THR_LOCK_DATA **end_p= lock_p + thd->locked_tables->lock_count;
3682
 
 
3683
 
        for (; lock_p < end_p; lock_p++)
3684
 
        {
3685
 
          if ((*lock_p)->type >= TL_WRITE_ALLOW_WRITE)
3686
 
          {
3687
 
            my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
3688
 
            return 1;
3689
 
          }
3690
 
        }
3691
 
      }
3692
 
      /*
3693
 
        Writing to the binlog could cause deadlocks, as we don't log
3694
 
        UNLOCK TABLES
3695
 
      */
3696
 
      tmp_write_to_binlog= 0;
3697
 
      if (lock_global_read_lock(thd))
3698
 
        return 1;                               // Killed
3699
 
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
 
2415
      if (lock_global_read_lock(session))
 
2416
        return true;                               // Killed
 
2417
      result= close_cached_tables(session, tables, (options & REFRESH_FAST) ?
3700
2418
                                  false : true, true);
3701
 
      if (make_global_read_lock_block_commit(thd)) // Killed
 
2419
      if (make_global_read_lock_block_commit(session)) // Killed
3702
2420
      {
3703
2421
        /* Don't leave things in a half-locked state */
3704
 
        unlock_global_read_lock(thd);
3705
 
        return 1;
 
2422
        unlock_global_read_lock(session);
 
2423
 
 
2424
        return true;
3706
2425
      }
3707
2426
    }
3708
2427
    else
3709
 
      result= close_cached_tables(thd, tables, false, (options & REFRESH_FAST) ?
 
2428
      result= close_cached_tables(session, tables, (options & REFRESH_FAST) ?
3710
2429
                                  false : true, false);
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
 
 }
3734
 
 *write_to_binlog= tmp_write_to_binlog;
 
2430
  }
 
2431
  if (session && (options & REFRESH_STATUS))
 
2432
    session->refresh_status();
 
2433
 
3735
2434
 return result;
3736
2435
}
3737
2436
 
3739
2438
/**
3740
2439
  kill on thread.
3741
2440
 
3742
 
  @param thd                    Thread class
 
2441
  @param session                        Thread class
3743
2442
  @param id                     Thread id
3744
2443
  @param only_kill_query        Should it kill the query or the connection
3745
2444
 
3748
2447
*/
3749
2448
 
3750
2449
static unsigned int
3751
 
kill_one_thread(THD *thd __attribute__((unused)),
3752
 
                ulong id, bool only_kill_query)
 
2450
kill_one_thread(Session *, ulong id, bool only_kill_query)
3753
2451
{
3754
 
  THD *tmp;
 
2452
  Session *tmp= NULL;
3755
2453
  uint32_t error=ER_NO_SUCH_THREAD;
3756
2454
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
3757
 
  I_List_iterator<THD> it(threads);
3758
 
  while ((tmp=it++))
 
2455
  
 
2456
  for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
3759
2457
  {
3760
 
    if (tmp->command == COM_DAEMON)
3761
 
      continue;
3762
 
    if (tmp->thread_id == id)
 
2458
    if ((*it)->thread_id == id)
3763
2459
    {
 
2460
      tmp= *it;
3764
2461
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
3765
2462
      break;
3766
2463
    }
3768
2465
  pthread_mutex_unlock(&LOCK_thread_count);
3769
2466
  if (tmp)
3770
2467
  {
3771
 
    tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
 
2468
    tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
3772
2469
    error=0;
3773
2470
    pthread_mutex_unlock(&tmp->LOCK_delete);
3774
2471
  }
3781
2478
 
3782
2479
  SYNOPSIS
3783
2480
    sql_kill()
3784
 
    thd                 Thread class
 
2481
    session                     Thread class
3785
2482
    id                  Thread id
3786
2483
    only_kill_query     Should it kill the query or the connection
3787
2484
*/
3788
2485
 
3789
 
void sql_kill(THD *thd, ulong id, bool only_kill_query)
 
2486
void sql_kill(Session *session, ulong id, bool only_kill_query)
3790
2487
{
3791
2488
  uint32_t error;
3792
 
  if (!(error= kill_one_thread(thd, id, only_kill_query)))
3793
 
    my_ok(thd);
 
2489
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
2490
    session->my_ok();
3794
2491
  else
3795
2492
    my_error(error, MYF(0), id);
3796
2493
}
3798
2495
 
3799
2496
/** If pointer is not a null pointer, append filename to it. */
3800
2497
 
3801
 
bool append_file_to_dir(THD *thd, const char **filename_ptr,
3802
 
                        const char *table_name)
 
2498
static bool append_file_to_dir(Session *session, const char **filename_ptr,
 
2499
                               const char *table_name)
3803
2500
{
3804
2501
  char buff[FN_REFLEN],*ptr, *end;
3805
2502
  if (!*filename_ptr)
3813
2510
    return 1;
3814
2511
  }
3815
2512
  /* Fix is using unix filename format on dos */
3816
 
  my_stpcpy(buff,*filename_ptr);
 
2513
  strcpy(buff,*filename_ptr);
3817
2514
  end=convert_dirname(buff, *filename_ptr, NULL);
3818
 
  if (!(ptr= (char*) thd->alloc((size_t) (end-buff) + strlen(table_name)+1)))
 
2515
  if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
3819
2516
    return 1;                                   // End of memory
3820
2517
  *filename_ptr=ptr;
3821
 
  strxmov(ptr,buff,table_name,NULL);
 
2518
  sprintf(ptr,"%s%s",buff,table_name);
3822
2519
  return 0;
3823
2520
}
3824
2521
 
3834
2531
 
3835
2532
bool check_simple_select()
3836
2533
{
3837
 
  THD *thd= current_thd;
3838
 
  LEX *lex= thd->lex;
 
2534
  Session *session= current_session;
 
2535
  LEX *lex= session->lex;
3839
2536
  if (lex->current_select != &lex->select_lex)
3840
2537
  {
3841
2538
    char command[80];
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));
 
2539
    Lex_input_stream *lip= session->m_lip;
 
2540
    strncpy(command, lip->yylval->symbol.str,
 
2541
            cmin(lip->yylval->symbol.length, sizeof(command)-1));
 
2542
    command[cmin(lip->yylval->symbol.length, sizeof(command)-1)]=0;
3845
2543
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
3846
2544
    return 1;
3847
2545
  }
3849
2547
}
3850
2548
 
3851
2549
 
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
 
 
3888
2550
/**
3889
2551
  Construct ALL/ANY/SOME subquery Item.
3890
2552
 
3897
2559
    constructed Item (or 0 if out of memory)
3898
2560
*/
3899
2561
Item * all_any_subquery_creator(Item *left_expr,
3900
 
                                chooser_compare_func_creator cmp,
3901
 
                                bool all,
3902
 
                                SELECT_LEX *select_lex)
 
2562
                                chooser_compare_func_creator cmp,
 
2563
                                bool all,
 
2564
                                Select_Lex *select_lex)
3903
2565
{
3904
2566
  if ((cmp == &comp_eq_creator) && !all)       //  = ANY <=> IN
3905
2567
    return new Item_in_subselect(left_expr, select_lex);
3917
2579
 
3918
2580
 
3919
2581
/**
3920
 
  Multi update query pre-check.
 
2582
  Update query pre-check.
3921
2583
 
3922
 
  @param thd            Thread handler
 
2584
  @param session                Thread handler
3923
2585
  @param tables Global/local table list (have to be the same)
3924
2586
 
3925
2587
  @retval
3928
2590
    true  Error
3929
2591
*/
3930
2592
 
3931
 
bool multi_update_precheck(THD *thd,
3932
 
                           TableList *tables __attribute__((unused)))
 
2593
bool update_precheck(Session *session, TableList *)
3933
2594
{
3934
2595
  const char *msg= 0;
3935
 
  LEX *lex= thd->lex;
3936
 
  SELECT_LEX *select_lex= &lex->select_lex;
 
2596
  LEX *lex= session->lex;
 
2597
  Select_Lex *select_lex= &lex->select_lex;
3937
2598
 
3938
 
  if (select_lex->item_list.elements != lex->value_list.elements)
 
2599
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
3939
2600
  {
3940
2601
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
3941
2602
    return(true);
3942
2603
  }
3943
2604
 
3944
 
  if (select_lex->order_list.elements)
3945
 
    msg= "ORDER BY";
3946
 
  else if (select_lex->select_limit)
3947
 
    msg= "LIMIT";
3948
 
  if (msg)
 
2605
  if (session->lex->select_lex.table_list.elements > 1)
3949
2606
  {
3950
 
    my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
3951
 
    return(true);
 
2607
    if (select_lex->order_list.elements)
 
2608
      msg= "ORDER BY";
 
2609
    else if (select_lex->select_limit)
 
2610
      msg= "LIMIT";
 
2611
    if (msg)
 
2612
    {
 
2613
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
 
2614
      return(true);
 
2615
    }
3952
2616
  }
3953
2617
  return(false);
3954
2618
}
3956
2620
/**
3957
2621
  Multi delete query pre-check.
3958
2622
 
3959
 
  @param thd                    Thread handler
 
2623
  @param session                        Thread handler
3960
2624
  @param tables         Global/local table list
3961
2625
 
3962
2626
  @retval
3965
2629
    true  error
3966
2630
*/
3967
2631
 
3968
 
bool multi_delete_precheck(THD *thd,
3969
 
                           TableList *tables __attribute__((unused)))
 
2632
bool multi_delete_precheck(Session *session, TableList *)
3970
2633
{
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)
 
2634
  Select_Lex *select_lex= &session->lex->select_lex;
 
2635
  TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
 
2636
 
 
2637
  session->lex->query_tables_own_last= 0;
 
2638
  session->lex->query_tables_own_last= save_query_tables_own_last;
 
2639
 
 
2640
  if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
3978
2641
  {
3979
2642
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
3980
2643
               ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
4001
2664
  @return Matching table, NULL otherwise.
4002
2665
*/
4003
2666
 
4004
 
static TableList *multi_delete_table_match(LEX *lex __attribute__((unused)),
4005
 
                                            TableList *tbl,
4006
 
                                            TableList *tables)
 
2667
static TableList *multi_delete_table_match(LEX *, TableList *tbl,
 
2668
                                           TableList *tables)
4007
2669
{
4008
2670
  TableList *match= NULL;
4009
2671
 
4028
2690
    if (match)
4029
2691
    {
4030
2692
      my_error(ER_NONUNIQ_TABLE, MYF(0), elem->alias);
4031
 
      return(NULL);
 
2693
      return NULL;
4032
2694
    }
4033
2695
 
4034
2696
    match= elem;
4082
2744
 
4083
2745
 
4084
2746
/**
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
 
/**
4108
2747
  simple INSERT query pre-check.
4109
2748
 
4110
 
  @param thd            Thread handler
 
2749
  @param session                Thread handler
4111
2750
  @param tables Global table list
4112
2751
 
4113
2752
  @retval
4116
2755
    true   error
4117
2756
*/
4118
2757
 
4119
 
bool insert_precheck(THD *thd, TableList *tables __attribute__((unused)))
 
2758
bool insert_precheck(Session *session, TableList *)
4120
2759
{
4121
 
  LEX *lex= thd->lex;
 
2760
  LEX *lex= session->lex;
4122
2761
 
4123
2762
  /*
4124
2763
    Check that we have modify privileges for the first table and
4136
2775
/**
4137
2776
  CREATE TABLE query pre-check.
4138
2777
 
4139
 
  @param thd                    Thread handler
 
2778
  @param session                        Thread handler
4140
2779
  @param tables         Global table list
4141
2780
  @param create_table           Table which will be created
4142
2781
 
4146
2785
    true   Error
4147
2786
*/
4148
2787
 
4149
 
bool create_table_precheck(THD *thd __attribute__((unused)),
4150
 
                           TableList *tables __attribute__((unused)),
 
2788
bool create_table_precheck(Session *, TableList *,
4151
2789
                           TableList *create_table)
4152
2790
{
4153
2791
  bool error= true;                                 // Error message is given
4154
2792
 
4155
2793
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
4156
2794
  {
4157
 
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.str);
 
2795
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
4158
2796
    return(true);
4159
2797
  }
4160
2798
 
4167
2805
/**
4168
2806
  negate given expression.
4169
2807
 
4170
 
  @param thd  thread handler
 
2808
  @param session  thread handler
4171
2809
  @param expr expression for negation
4172
2810
 
4173
2811
  @return
4174
2812
    negated expression
4175
2813
*/
4176
2814
 
4177
 
Item *negate_expression(THD *thd, Item *expr)
 
2815
Item *negate_expression(Session *session, Item *expr)
4178
2816
{
4179
2817
  Item *negated;
4180
2818
  if (expr->type() == Item::FUNC_ITEM &&
4182
2820
  {
4183
2821
    /* it is NOT(NOT( ... )) */
4184
2822
    Item *arg= ((Item_func *) expr)->arguments()[0];
4185
 
    enum_parsing_place place= thd->lex->current_select->parsing_place;
 
2823
    enum_parsing_place place= session->lex->current_select->parsing_place;
4186
2824
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
4187
2825
      return arg;
4188
2826
    /*
4192
2830
    return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
4193
2831
  }
4194
2832
 
4195
 
  if ((negated= expr->neg_transformer(thd)) != 0)
 
2833
  if ((negated= expr->neg_transformer(session)) != 0)
4196
2834
    return negated;
4197
2835
  return new Item_func_not(expr);
4198
2836
}
4199
2837
 
4200
2838
 
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
 
 
4229
2839
/*
4230
2840
  Check that char length of a string does not exceed some limit.
4231
2841
 
4259
2869
}
4260
2870
 
4261
2871
 
4262
 
bool check_identifier_name(LEX_STRING *str, uint32_t max_char_length,
4263
 
                           uint32_t err_code, const char *param_for_err_msg)
 
2872
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
 
2873
                           uint32_t max_char_length,
 
2874
                           const char *param_for_err_msg)
4264
2875
{
4265
 
#ifdef HAVE_CHARSET_utf8mb3
4266
2876
  /*
4267
2877
    We don't support non-BMP characters in identifiers at the moment,
4268
2878
    so they should be prohibited until such support is done.
4269
2879
    This is why we use the 3-byte utf8 to check well-formedness here.
4270
2880
  */
4271
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb3_general_ci;
4272
 
#else
4273
 
  const CHARSET_INFO * const cs= system_charset_info;
4274
 
#endif
 
2881
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
2882
 
4275
2883
  int well_formed_error;
4276
2884
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
4277
2885
                                      max_char_length, &well_formed_error);
4281
2889
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
4282
2890
    return true;
4283
2891
  }
4284
 
  
 
2892
 
4285
2893
  if (str->length == res)
4286
2894
    return false;
4287
2895
 
4313
2921
 
4314
2922
  RETURN VALUES
4315
2923
    0   ok
4316
 
    1   error  
 
2924
    1   error
4317
2925
*/
4318
2926
 
4319
2927
bool test_if_data_home_dir(const char *dir)
4320
2928
{
4321
2929
  char path[FN_REFLEN], conv_path[FN_REFLEN];
4322
 
  uint32_t dir_len, home_dir_len= strlen(mysql_unpacked_real_data_home);
 
2930
  uint32_t dir_len, home_dir_len= strlen(drizzle_unpacked_real_data_home);
4323
2931
 
4324
2932
  if (!dir)
4325
2933
    return(0);
4332
2940
  {
4333
2941
    if (!my_strnncoll(character_set_filesystem,
4334
2942
                      (const unsigned char*) conv_path, home_dir_len,
4335
 
                      (const unsigned char*) mysql_unpacked_real_data_home,
 
2943
                      (const unsigned char*) drizzle_unpacked_real_data_home,
4336
2944
                      home_dir_len))
4337
2945
      return(1);
4338
2946
  }
4340
2948
}
4341
2949
 
4342
2950
 
4343
 
extern int MYSQLparse(void *thd); // from sql_yacc.cc
 
2951
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
4344
2952
 
4345
2953
 
4346
2954
/**
4347
 
  This is a wrapper of MYSQLparse(). All the code should call parse_sql()
4348
 
  instead of MYSQLparse().
 
2955
  This is a wrapper of DRIZZLEparse(). All the code should call parse_sql()
 
2956
  instead of DRIZZLEparse().
4349
2957
 
4350
 
  @param thd Thread context.
 
2958
  @param session Thread context.
4351
2959
  @param lip Lexer context.
4352
2960
 
4353
2961
  @return Error status.
4355
2963
    @retval true on parsing error.
4356
2964
*/
4357
2965
 
4358
 
bool parse_sql(THD *thd, Lex_input_stream *lip)
 
2966
bool parse_sql(Session *session, Lex_input_stream *lip)
4359
2967
{
4360
 
  assert(thd->m_lip == NULL);
 
2968
  assert(session->m_lip == NULL);
4361
2969
 
4362
2970
  /* Set Lex_input_stream. */
4363
2971
 
4364
 
  thd->m_lip= lip;
 
2972
  session->m_lip= lip;
4365
2973
 
4366
2974
  /* Parse the query. */
4367
2975
 
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());
 
2976
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
 
2977
 
 
2978
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
 
2979
 
 
2980
  assert(!mysql_parse_status || session->is_error());
4373
2981
 
4374
2982
  /* Reset Lex_input_stream. */
4375
2983
 
4376
 
  thd->m_lip= NULL;
 
2984
  session->m_lip= NULL;
4377
2985
 
4378
2986
  /* That's it. */
4379
2987
 
4380
 
  return mysql_parse_status || thd->is_fatal_error;
 
2988
  return mysql_parse_status || session->is_fatal_error;
4381
2989
}
4382
2990
 
4383
2991
/**