~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Mats Kindahl
  • Date: 2008-08-26 07:32:59 UTC
  • mto: (489.1.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: mats@mysql.com-20080826073259-9k4evtajgldgolli
Replaced use of thd_proc_info() macro with calls to
set_proc_info() and get_proc_info() internally.  Introduced
functions set_thd_proc_info() and get_thd_proc_info() for
external users, i.e., plug-ins.

The set_thd_proc_info() accepted callers info that can be used to
print debug output, but the information was not used. The return
value was changed to void and the old value is not fetched any
more. To be able to get the value of proc_info for external
users, the function get_thd_proc_info() was introduced.

The thd_proc_info() macro called set_thd_proc_info() but almost
never used the return value of set_thd_proc_info() so the macro
was replaced with a call of THD::set_proc_info().

Show diffs side-by-side

added added

removed removed

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