~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

Show diffs side-by-side

added added

removed removed

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