~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Nathan Williams
  • Date: 2009-06-25 00:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 1082.
  • Revision ID: nathanlws@gmail.com-20090625000126-fv99nqpec4edrchc
Converted last usage of cmin to std::min.

Removed cmin and cmax defines from global.h

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