~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2009-02-20 22:48:37 UTC
  • Revision ID: brian@tangent.org-20090220224837-fw5wrf46n4ru3e6a
First pass of stripping uint

Show diffs side-by-side

added added

removed removed

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