~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
16
#define DRIZZLE_LEX 1
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
17
#include <drizzled/server_includes.h>
614 by Brian Aker
Remove filtering (wrong layer, belongs in plugin).
18
#include <mysys/hash.h>
520.8.2 by Monty Taylor
Moved sql_parse.h and sql_error.h out of common_includes.
19
#include <drizzled/logging.h>
575.4.6 by Monty Taylor
Removed my_getwd.
20
#include <drizzled/db.h>
549 by Monty Taylor
Took gettext.h out of header files.
21
#include <drizzled/error.h>
553.1.3 by Monty Taylor
Split out nested_join.h.
22
#include <drizzled/nested_join.h>
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
23
#include <drizzled/query_id.h>
520.8.2 by Monty Taylor
Moved sql_parse.h and sql_error.h out of common_includes.
24
#include <drizzled/sql_parse.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
25
#include <drizzled/data_home.h>
575.4.7 by Monty Taylor
More header cleanup.
26
#include <drizzled/sql_base.h>
27
#include <drizzled/show.h>
28
#include <drizzled/rename.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
29
#include <drizzled/function/time/unix_timestamp.h>
30
#include <drizzled/function/get_system_var.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
31
#include <drizzled/item/cmpfunc.h>
642.1.20 by Lee
header file clean up
32
#include <drizzled/item/null.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
33
#include <drizzled/session.h>
34
#include <drizzled/sql_load.h>
670.2.3 by Monty Taylor
Moved connect.h out of server_includes.
35
#include <drizzled/connect.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
36
#include <drizzled/lock.h>
837 by Brian Aker
Reworked some classes out of session.h
37
#include <drizzled/select_send.h>
590.2.18 by Monty Taylor
Changed the bitset<32> to a bitset<5>. Made the int constants into bitset constants.
38
#include <bitset>
39
40
using namespace std;
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
41
1 by brian
clean slate
42
/**
43
  @defgroup Runtime_Environment Runtime Environment
44
  @{
45
*/
46
629.2.7 by Monty Taylor
Fixed a couple of memory buffer size issues.
47
extern size_t my_thread_stack_size;
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
48
extern const CHARSET_INFO *character_set_filesystem;
1 by brian
clean slate
49
const char *any_db="*any*";	// Special symbol for check_access
50
230.1.8 by Monty Taylor
Set length to catch errors in adding or removing COM_* constants.
51
const LEX_STRING command_name[COM_END+1]={
1 by brian
clean slate
52
  { C_STRING_WITH_LEN("Sleep") },
53
  { C_STRING_WITH_LEN("Quit") },
54
  { C_STRING_WITH_LEN("Init DB") },
55
  { C_STRING_WITH_LEN("Query") },
56
  { C_STRING_WITH_LEN("Shutdown") },
57
  { C_STRING_WITH_LEN("Connect") },
58
  { C_STRING_WITH_LEN("Ping") },
59
  { C_STRING_WITH_LEN("Error") }  // Last command number
60
};
61
62
const char *xa_state_names[]={
63
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
64
};
65
520.1.22 by Brian Aker
Second pass of thd cleanup
66
static void unlock_locked_tables(Session *session)
1 by brian
clean slate
67
{
520.1.22 by Brian Aker
Second pass of thd cleanup
68
  if (session->locked_tables)
1 by brian
clean slate
69
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
70
    session->lock=session->locked_tables;
71
    session->locked_tables=0;			// Will be automatically closed
72
    close_thread_tables(session);			// Free tables
1 by brian
clean slate
73
  }
74
}
75
76
/**
77
  Mark all commands that somehow changes a table.
78
79
  This is used to check number of updates / hour.
80
81
  sql_command is actually set to SQLCOM_END sometimes
82
  so we need the +1 to include it in the array.
83
84
  See COMMAND_FLAG_xxx for different type of commands
85
     2  - query that returns meaningful ROW_COUNT() -
86
          a number of modified rows
87
*/
590.2.18 by Monty Taylor
Changed the bitset<32> to a bitset<5>. Made the int constants into bitset constants.
88
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
1 by brian
clean slate
89
90
void init_update_queries(void)
91
{
610 by Brian Aker
Merge of Monty's (plus fix for increment style/useless +1)
92
  uint32_t x;
93
94
  for (x= 0; x <= SQLCOM_END; x++)
95
    sql_command_flags[x].reset();
1 by brian
clean slate
96
97
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
98
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
99
  sql_command_flags[SQLCOM_ALTER_TABLE]=    CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND;
100
  sql_command_flags[SQLCOM_TRUNCATE]=       CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND;
101
  sql_command_flags[SQLCOM_DROP_TABLE]=     CF_CHANGES_DATA;
102
  sql_command_flags[SQLCOM_LOAD]=           CF_CHANGES_DATA;
103
  sql_command_flags[SQLCOM_CREATE_DB]=      CF_CHANGES_DATA;
104
  sql_command_flags[SQLCOM_DROP_DB]=        CF_CHANGES_DATA;
105
  sql_command_flags[SQLCOM_RENAME_TABLE]=   CF_CHANGES_DATA;
106
  sql_command_flags[SQLCOM_DROP_INDEX]=     CF_CHANGES_DATA;
107
108
  sql_command_flags[SQLCOM_UPDATE]=	    CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
109
  sql_command_flags[SQLCOM_UPDATE_MULTI]=   CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
110
  sql_command_flags[SQLCOM_INSERT]=	    CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
111
  sql_command_flags[SQLCOM_INSERT_SELECT]=  CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
112
  sql_command_flags[SQLCOM_DELETE]=         CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
113
  sql_command_flags[SQLCOM_DELETE_MULTI]=   CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
114
  sql_command_flags[SQLCOM_REPLACE]=        CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
115
  sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
116
117
  sql_command_flags[SQLCOM_SHOW_STATUS]=      CF_STATUS_COMMAND;
118
  sql_command_flags[SQLCOM_SHOW_DATABASES]=   CF_STATUS_COMMAND;
119
  sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND;
120
  sql_command_flags[SQLCOM_SHOW_FIELDS]=      CF_STATUS_COMMAND;
121
  sql_command_flags[SQLCOM_SHOW_KEYS]=        CF_STATUS_COMMAND;
122
  sql_command_flags[SQLCOM_SHOW_VARIABLES]=   CF_STATUS_COMMAND;
123
  sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
124
  sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
125
  sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
126
  sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
127
  sql_command_flags[SQLCOM_SHOW_CREATE_DB]=  CF_STATUS_COMMAND;
128
  sql_command_flags[SQLCOM_SHOW_CREATE]=  CF_STATUS_COMMAND;
129
130
   sql_command_flags[SQLCOM_SHOW_TABLES]=       (CF_STATUS_COMMAND |
131
                                               CF_SHOW_TABLE_COMMAND);
132
  sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
133
                                                CF_SHOW_TABLE_COMMAND);
134
  /*
135
    The following admin table operations are allowed
136
    on log tables.
137
  */
138
  sql_command_flags[SQLCOM_REPAIR]=           CF_WRITE_LOGS_COMMAND;
139
  sql_command_flags[SQLCOM_OPTIMIZE]=         CF_WRITE_LOGS_COMMAND;
140
  sql_command_flags[SQLCOM_ANALYZE]=          CF_WRITE_LOGS_COMMAND;
141
}
142
143
144
bool is_update_query(enum enum_sql_command command)
145
{
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
146
  assert(command >= 0 && command <= SQLCOM_END);
602.2.2 by Yoshinori Sano
Fix bugs in params of sql_command_flags[...].test(). Previously, the value such as, 0, 1, 2, 4, 8 and 16 is given to std::bitset.test() in that place, which is misunderstanding of the spec of the test(). The test(size_t n) returns true if bit n is set.
147
  return (sql_command_flags[command].test(CF_BIT_CHANGES_DATA));
1 by brian
clean slate
148
}
149
150
/**
151
  Perform one connection-level (COM_XXXX) command.
152
153
  @param command         type of command to perform
520.1.22 by Brian Aker
Second pass of thd cleanup
154
  @param session             connection handle
1 by brian
clean slate
155
  @param packet          data for the command, packet is always null-terminated
156
  @param packet_length   length of packet + 1 (to show that data is
157
                         null-terminated) except for COM_SLEEP, where it
158
                         can be zero.
159
160
  @todo
520.1.22 by Brian Aker
Second pass of thd cleanup
161
    set session->lex->sql_command to SQLCOM_END here.
1 by brian
clean slate
162
  @todo
163
    The following has to be changed to an 8 byte integer
164
165
  @retval
166
    0   ok
167
  @retval
168
    1   request of thread shutdown, i. e. if command is
169
        COM_QUIT/COM_SHUTDOWN
170
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
171
bool dispatch_command(enum enum_server_command command, Session *session,
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
172
                      char* packet, uint32_t packet_length)
1 by brian
clean slate
173
{
174
  bool error= 0;
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
175
  Query_id &query_id= Query_id::get_query_id();
1 by brian
clean slate
176
520.1.22 by Brian Aker
Second pass of thd cleanup
177
  session->command=command;
178
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
179
  session->set_time();
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
180
  session->query_id= query_id.value();
1 by brian
clean slate
181
182
  switch( command ) {
183
  /* Ignore these statements. */
184
  case COM_PING:
185
    break;
186
  /* Increase id and count all other statements. */
187
  default:
520.1.22 by Brian Aker
Second pass of thd cleanup
188
    statistic_increment(session->status_var.questions, &LOCK_status);
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
189
    query_id.next();
1 by brian
clean slate
190
  }
191
520.1.22 by Brian Aker
Second pass of thd cleanup
192
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
1 by brian
clean slate
193
520.1.22 by Brian Aker
Second pass of thd cleanup
194
  logging_pre_do(session);
383.6.4 by Mark Atwood
more make plug logging work
195
520.1.22 by Brian Aker
Second pass of thd cleanup
196
  session->server_status&=
1 by brian
clean slate
197
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
198
  switch (command) {
199
  case COM_INIT_DB:
200
  {
201
    LEX_STRING tmp;
520.1.22 by Brian Aker
Second pass of thd cleanup
202
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
203
    session->convert_string(&tmp, system_charset_info,
204
                        packet, packet_length, session->charset());
205
    if (!mysql_change_db(session, &tmp, false))
1 by brian
clean slate
206
    {
836 by Brian Aker
Fixed session call from function to method.
207
      session->my_ok();
1 by brian
clean slate
208
    }
209
    break;
210
  }
211
  case COM_QUERY:
212
  {
934.2.9 by Jay Pipes
Pulls alloc_query() C function out of sql_parse.cc and adds readAndStoreQuery() member method of Session class.
213
    if (! session->readAndStoreQuery(packet, packet_length))
1 by brian
clean slate
214
      break;					// fatal error is set
215
    const char* end_of_stmt= NULL;
216
520.1.22 by Brian Aker
Second pass of thd cleanup
217
    mysql_parse(session, session->query, session->query_length, &end_of_stmt);
1 by brian
clean slate
218
219
    break;
220
  }
221
  case COM_QUIT:
222
    /* We don't calculate statistics for this command */
971.3.19 by Eric Day
Finished first pass at Protocol cleanup, still some things to remove but they are a bit more involved.
223
    session->protocol->setError(0);
520.1.22 by Brian Aker
Second pass of thd cleanup
224
    session->main_da.disable_status();              // Don't send anything back
55 by brian
Update for using real bool types.
225
    error=true;					// End server
1 by brian
clean slate
226
    break;
227
  case COM_SHUTDOWN:
228
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
229
    status_var_increment(session->status_var.com_other);
836 by Brian Aker
Fixed session call from function to method.
230
    session->my_eof();
520.1.22 by Brian Aker
Second pass of thd cleanup
231
    close_thread_tables(session);			// Free before kill
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
232
    kill_drizzle();
55 by brian
Update for using real bool types.
233
    error=true;
1 by brian
clean slate
234
    break;
235
  }
236
  case COM_PING:
520.1.22 by Brian Aker
Second pass of thd cleanup
237
    status_var_increment(session->status_var.com_other);
836 by Brian Aker
Fixed session call from function to method.
238
    session->my_ok();				// Tell client we are alive
1 by brian
clean slate
239
    break;
240
  case COM_SLEEP:
241
  case COM_CONNECT:				// Impossible here
242
  case COM_END:
243
  default:
244
    my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
245
    break;
246
  }
247
248
  /* If commit fails, we should be able to reset the OK status. */
520.1.22 by Brian Aker
Second pass of thd cleanup
249
  session->main_da.can_overwrite_status= true;
250
  ha_autocommit_or_rollback(session, session->is_error());
251
  session->main_da.can_overwrite_status= false;
1 by brian
clean slate
252
520.1.22 by Brian Aker
Second pass of thd cleanup
253
  session->transaction.stmt.reset();
1 by brian
clean slate
254
255
256
  /* report error issued during command execution */
520.1.22 by Brian Aker
Second pass of thd cleanup
257
  if (session->killed_errno())
258
  {
259
    if (! session->main_da.is_set())
260
      session->send_kill_message();
261
  }
262
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
263
  {
264
    session->killed= Session::NOT_KILLED;
265
    session->mysys_var->abort= 0;
266
  }
267
971.3.12 by Eric Day
Started abstracting Protocol, removed init_connect, init_file.
268
  /* Can not be true, but do not take chances in production. */
269
  assert(! session->main_da.is_sent);
270
271
  switch (session->main_da.status())
272
  {
273
  case Diagnostics_area::DA_ERROR:
274
    /* The query failed, send error to log and abort bootstrap. */
275
    session->protocol->sendError(session->main_da.sql_errno(),
276
                                 session->main_da.message());
277
    break;
278
279
  case Diagnostics_area::DA_EOF:
280
    session->protocol->sendEOF();
281
    break;
282
283
  case Diagnostics_area::DA_OK:
284
    session->protocol->sendOK();
285
    break;
286
287
  case Diagnostics_area::DA_DISABLED:
288
    break;
289
290
  case Diagnostics_area::DA_EMPTY:
291
  default:
292
    session->protocol->sendOK();
293
    break;
294
  }
295
296
  session->main_da.is_sent= true;
520.1.22 by Brian Aker
Second pass of thd cleanup
297
298
  session->set_proc_info("closing tables");
1 by brian
clean slate
299
  /* Free tables */
520.1.22 by Brian Aker
Second pass of thd cleanup
300
  close_thread_tables(session);
301
302
  log_slow_statement(session);
303
961.1.4 by Brian Aker
Remove another lock for processlist.
304
  /* Store temp state for processlist */
520.1.22 by Brian Aker
Second pass of thd cleanup
305
  session->set_proc_info("cleaning up");
306
  session->command=COM_SLEEP;
961.1.4 by Brian Aker
Remove another lock for processlist.
307
  session->process_list_info[0]= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
308
  session->query=0;
309
  session->query_length=0;
961.1.4 by Brian Aker
Remove another lock for processlist.
310
311
  session->set_proc_info(NULL);
520.1.22 by Brian Aker
Second pass of thd cleanup
312
  session->packet.shrink(session->variables.net_buffer_length);	// Reclaim some memory
313
  free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
314
  return(error);
1 by brian
clean slate
315
}
316
317
520.1.22 by Brian Aker
Second pass of thd cleanup
318
void log_slow_statement(Session *session)
1 by brian
clean slate
319
{
520.1.22 by Brian Aker
Second pass of thd cleanup
320
  logging_post_do(session);
383.6.4 by Mark Atwood
more make plug logging work
321
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
322
  return;
1 by brian
clean slate
323
}
324
325
326
/**
327.2.4 by Brian Aker
Refactoring table.h
327
  Create a TableList object for an INFORMATION_SCHEMA table.
1 by brian
clean slate
328
329
    This function is used in the parser to convert a SHOW or DESCRIBE
330
    table_name command to a SELECT from INFORMATION_SCHEMA.
846 by Brian Aker
Removing on typedeffed class.
331
    It prepares a Select_Lex and a TableList object to represent the
1 by brian
clean slate
332
    given command as a SELECT parse tree.
333
520.1.22 by Brian Aker
Second pass of thd cleanup
334
  @param session              thread handle
1 by brian
clean slate
335
  @param lex              current lex
336
  @param table_ident      table alias if it's used
337
  @param schema_table_idx the type of the INFORMATION_SCHEMA table to be
338
                          created
339
340
  @note
341
    Due to the way this function works with memory and LEX it cannot
342
    be used outside the parser (parse tree transformations outside
343
    the parser break PS and SP).
344
345
  @retval
346
    0                 success
347
  @retval
348
    1                 out of memory or SHOW commands are not allowed
349
                      in this version of the server.
350
*/
351
520.1.22 by Brian Aker
Second pass of thd cleanup
352
int prepare_schema_table(Session *session, LEX *lex, Table_ident *table_ident,
1 by brian
clean slate
353
                         enum enum_schema_tables schema_table_idx)
354
{
846 by Brian Aker
Removing on typedeffed class.
355
  Select_Lex *schema_select_lex= NULL;
1 by brian
clean slate
356
357
  switch (schema_table_idx) {
358
  case SCH_SCHEMATA:
359
    break;
360
  case SCH_TABLE_NAMES:
361
  case SCH_TABLES:
362
    {
363
      LEX_STRING db;
364
      size_t dummy;
365
      if (lex->select_lex.db == NULL &&
366
          lex->copy_db_to(&lex->select_lex.db, &dummy))
367
      {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
368
        return(1);
1 by brian
clean slate
369
      }
846 by Brian Aker
Removing on typedeffed class.
370
      schema_select_lex= new Select_Lex();
1 by brian
clean slate
371
      db.str= schema_select_lex->db= lex->select_lex.db;
372
      schema_select_lex->table_list.first= NULL;
373
      db.length= strlen(db.str);
374
375
      if (check_db_name(&db))
376
      {
377
        my_error(ER_WRONG_DB_NAME, MYF(0), db.str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
378
        return(1);
1 by brian
clean slate
379
      }
380
      break;
381
    }
382
  case SCH_COLUMNS:
383
  case SCH_STATISTICS:
384
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
385
    assert(table_ident);
327.2.4 by Brian Aker
Refactoring table.h
386
    TableList **query_tables_last= lex->query_tables_last;
846 by Brian Aker
Removing on typedeffed class.
387
    schema_select_lex= new Select_Lex();
1 by brian
clean slate
388
    /* 'parent_lex' is used in init_query() so it must be before it. */
389
    schema_select_lex->parent_lex= lex;
390
    schema_select_lex->init_query();
520.1.22 by Brian Aker
Second pass of thd cleanup
391
    if (!schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
392
      return(1);
1 by brian
clean slate
393
    lex->query_tables_last= query_tables_last;
394
    break;
395
  }
396
  case SCH_OPEN_TABLES:
397
  case SCH_VARIABLES:
398
  case SCH_STATUS:
399
  case SCH_CHARSETS:
400
  case SCH_COLLATIONS:
401
  case SCH_COLLATION_CHARACTER_SET_APPLICABILITY:
402
  case SCH_TABLE_CONSTRAINTS:
403
  case SCH_KEY_COLUMN_USAGE:
404
  default:
405
    break;
406
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
407
846 by Brian Aker
Removing on typedeffed class.
408
  Select_Lex *select_lex= lex->current_select;
1 by brian
clean slate
409
  assert(select_lex);
520.1.22 by Brian Aker
Second pass of thd cleanup
410
  if (make_schema_select(session, select_lex, schema_table_idx))
1 by brian
clean slate
411
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
412
    return(1);
1 by brian
clean slate
413
  }
327.2.4 by Brian Aker
Refactoring table.h
414
  TableList *table_list= (TableList*) select_lex->table_list.first;
1 by brian
clean slate
415
  assert(table_list);
416
  table_list->schema_select_lex= schema_select_lex;
417
  table_list->schema_table_reformed= 1;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
418
  return(0);
1 by brian
clean slate
419
}
420
421
/**
520.1.22 by Brian Aker
Second pass of thd cleanup
422
  Execute command saved in session and lex->sql_command.
1 by brian
clean slate
423
424
    Before every operation that can request a write lock for a table
425
    wait if a global read lock exists. However do not wait if this
426
    thread has locked tables already. No new locks can be requested
427
    until the other locks are released. The thread that requests the
428
    global read lock waits for write locked tables to become unlocked.
429
430
    Note that wait_if_global_read_lock() sets a protection against a new
431
    global read lock when it succeeds. This needs to be released by
432
    start_waiting_global_read_lock() after the operation.
433
520.1.22 by Brian Aker
Second pass of thd cleanup
434
  @param session                       Thread handle
1 by brian
clean slate
435
436
  @todo
437
    - Invalidate the table in the query cache if something changed
438
    after unlocking when changes become visible.
439
    TODO: this is workaround. right way will be move invalidating in
440
    the unlock procedure.
441
    - TODO: use check_change_password()
442
    - JOIN is not supported yet. TODO
443
    - SUSPEND and FOR MIGRATE are not supported yet. TODO
444
445
  @retval
55 by brian
Update for using real bool types.
446
    false       OK
1 by brian
clean slate
447
  @retval
55 by brian
Update for using real bool types.
448
    true        Error
1 by brian
clean slate
449
*/
450
451
int
520.1.22 by Brian Aker
Second pass of thd cleanup
452
mysql_execute_command(Session *session)
1 by brian
clean slate
453
{
55 by brian
Update for using real bool types.
454
  int res= false;
455
  bool need_start_waiting= false; // have protection against global read lock
520.1.22 by Brian Aker
Second pass of thd cleanup
456
  LEX  *lex= session->lex;
846 by Brian Aker
Removing on typedeffed class.
457
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
458
  Select_Lex *select_lex= &lex->select_lex;
459
  /* first table of first Select_Lex */
327.2.4 by Brian Aker
Refactoring table.h
460
  TableList *first_table= (TableList*) select_lex->table_list.first;
1 by brian
clean slate
461
  /* list of all tables in query */
327.2.4 by Brian Aker
Refactoring table.h
462
  TableList *all_tables;
848 by Brian Aker
typdef class removal (just... use the name of the class).
463
  /* most outer Select_Lex_Unit of query */
464
  Select_Lex_Unit *unit= &lex->unit;
1 by brian
clean slate
465
  /* Saved variable value */
466
467
  /*
846 by Brian Aker
Removing on typedeffed class.
468
    In many cases first table of main Select_Lex have special meaning =>
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
469
    check that it is first table in global list and relink it first in
1 by brian
clean slate
470
    queries_tables list if it is necessary (we need such relinking only
471
    for queries with subqueries in select list, in this case tables of
472
    subqueries will go to global list first)
473
846 by Brian Aker
Removing on typedeffed class.
474
    all_tables will differ from first_table only if most upper Select_Lex
1 by brian
clean slate
475
    do not contain tables.
476
477
    Because of above in place where should be at least one table in most
846 by Brian Aker
Removing on typedeffed class.
478
    outer Select_Lex we have following check:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
479
    assert(first_table == all_tables);
480
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
481
  */
482
  lex->first_lists_tables_same();
483
  /* should be assigned after making first tables same */
484
  all_tables= lex->query_tables;
485
  /* set context for commands which do not use setup_tables */
486
  select_lex->
327.2.4 by Brian Aker
Refactoring table.h
487
    context.resolve_in_table_list_only((TableList*)select_lex->
1 by brian
clean slate
488
                                       table_list.first);
489
490
  /*
491
    Reset warning count for each query that uses tables
492
    A better approach would be to reset this for any commands
493
    that is not a SHOW command or a select that only access local
494
    variables, but for now this is probably good enough.
495
    Don't reset warnings when executing a stored routine.
496
  */
497
  if (all_tables || !lex->is_single_level_stmt())
520.1.22 by Brian Aker
Second pass of thd cleanup
498
    drizzle_reset_errors(session, 0);
1 by brian
clean slate
499
520.1.22 by Brian Aker
Second pass of thd cleanup
500
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
1 by brian
clean slate
501
520.1.22 by Brian Aker
Second pass of thd cleanup
502
  assert(session->transaction.stmt.modified_non_trans_table == false);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
503
1 by brian
clean slate
504
  switch (lex->sql_command) {
505
  case SQLCOM_SHOW_STATUS:
506
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
507
    system_status_var old_status_var= session->status_var;
508
    session->initial_status_var= &old_status_var;
509
    res= execute_sqlcom_select(session, all_tables);
1 by brian
clean slate
510
    /* Don't log SHOW STATUS commands to slow query log */
520.1.22 by Brian Aker
Second pass of thd cleanup
511
    session->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
1 by brian
clean slate
512
                           SERVER_QUERY_NO_GOOD_INDEX_USED);
513
    /*
514
      restore status variables, as we don't want 'show status' to cause
515
      changes
516
    */
517
    pthread_mutex_lock(&LOCK_status);
520.1.22 by Brian Aker
Second pass of thd cleanup
518
    add_diff_to_status(&global_status_var, &session->status_var,
1 by brian
clean slate
519
                       &old_status_var);
520.1.22 by Brian Aker
Second pass of thd cleanup
520
    session->status_var= old_status_var;
1 by brian
clean slate
521
    pthread_mutex_unlock(&LOCK_status);
522
    break;
523
  }
524
  case SQLCOM_SHOW_DATABASES:
525
  case SQLCOM_SHOW_TABLES:
526
  case SQLCOM_SHOW_TABLE_STATUS:
527
  case SQLCOM_SHOW_OPEN_TABLES:
528
  case SQLCOM_SHOW_FIELDS:
529
  case SQLCOM_SHOW_KEYS:
530
  case SQLCOM_SHOW_VARIABLES:
531
  case SQLCOM_SELECT:
532
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
533
    session->status_var.last_query_cost= 0.0;
534
    res= execute_sqlcom_select(session, all_tables);
1 by brian
clean slate
535
    break;
536
  }
537
  case SQLCOM_EMPTY_QUERY:
836 by Brian Aker
Fixed session call from function to method.
538
    session->my_ok();
1 by brian
clean slate
539
    break;
540
541
  case SQLCOM_SHOW_WARNS:
542
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
543
    res= mysqld_show_warnings(session, (uint32_t)
438.1.13 by Brian Aker
uint cleanup.
544
			      ((1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_NOTE) |
545
			       (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_WARN) |
546
			       (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR)
1 by brian
clean slate
547
			       ));
548
    break;
549
  }
550
  case SQLCOM_SHOW_ERRORS:
551
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
552
    res= mysqld_show_warnings(session, (uint32_t)
438.1.13 by Brian Aker
uint cleanup.
553
			      (1L << (uint32_t) DRIZZLE_ERROR::WARN_LEVEL_ERROR));
1 by brian
clean slate
554
    break;
555
  }
556
  case SQLCOM_ASSIGN_TO_KEYCACHE:
557
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
558
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
559
    res= mysql_assign_to_keycache(session, first_table, &lex->ident);
1 by brian
clean slate
560
    break;
561
  }
562
  case SQLCOM_SHOW_ENGINE_STATUS:
563
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
564
      res = ha_show_status(session, lex->create_info.db_type, HA_ENGINE_STATUS);
1 by brian
clean slate
565
      break;
566
    }
567
  case SQLCOM_CREATE_TABLE:
568
  {
569
    /* If CREATE TABLE of non-temporary table, do implicit commit */
570
    if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
571
    {
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
572
      if (! session->endActiveTransaction())
1 by brian
clean slate
573
      {
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
574
        res= -1;
575
        break;
1 by brian
clean slate
576
      }
577
    }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
578
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
579
    bool link_to_local;
580
    // Skip first table, which is the table we are creating
327.2.4 by Brian Aker
Refactoring table.h
581
    TableList *create_table= lex->unlink_first_table(&link_to_local);
582
    TableList *select_tables= lex->query_tables;
1 by brian
clean slate
583
    /*
584
      Code below (especially in mysql_create_table() and select_create
585
      methods) may modify HA_CREATE_INFO structure in LEX, so we have to
586
      use a copy of this structure to make execution prepared statement-
587
      safe. A shallow copy is enough as this code won't modify any memory
588
      referenced from this structure.
589
    */
590
    HA_CREATE_INFO create_info(lex->create_info);
591
    /*
592
      We need to copy alter_info for the same reasons of re-execution
593
      safety, only in case of Alter_info we have to do (almost) a deep
594
      copy.
595
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
596
    Alter_info alter_info(lex->alter_info, session->mem_root);
1 by brian
clean slate
597
520.1.22 by Brian Aker
Second pass of thd cleanup
598
    if (session->is_fatal_error)
1 by brian
clean slate
599
    {
600
      /* If out of memory when creating a copy of alter_info. */
601
      res= 1;
602
      goto end_with_restore_list;
603
    }
604
520.1.22 by Brian Aker
Second pass of thd cleanup
605
    if ((res= create_table_precheck(session, select_tables, create_table)))
1 by brian
clean slate
606
      goto end_with_restore_list;
607
608
    /* Might have been updated in create_table_precheck */
609
    create_info.alias= create_table->alias;
610
611
#ifdef HAVE_READLINK
612
    /* Fix names if symlinked tables */
520.1.22 by Brian Aker
Second pass of thd cleanup
613
    if (append_file_to_dir(session, &create_info.data_file_name,
1 by brian
clean slate
614
			   create_table->table_name) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
615
	append_file_to_dir(session, &create_info.index_file_name,
1 by brian
clean slate
616
			   create_table->table_name))
617
      goto end_with_restore_list;
618
#endif
619
    /*
620
      The create-select command will open and read-lock the select table
621
      and then create, open and write-lock the new table. If a global
622
      read lock steps in, we get a deadlock. The write lock waits for
623
      the global read lock, while the global read lock waits for the
624
      select table to be closed. So we wait until the global readlock is
625
      gone before starting both steps. Note that
626
      wait_if_global_read_lock() sets a protection against a new global
627
      read lock when it succeeds. This needs to be released by
628
      start_waiting_global_read_lock(). We protect the normal CREATE
629
      TABLE in the same way. That way we avoid that a new table is
630
      created during a gobal read lock.
631
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
632
    if (!session->locked_tables &&
633
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1 by brian
clean slate
634
    {
635
      res= 1;
636
      goto end_with_restore_list;
637
    }
638
    if (select_lex->item_list.elements)		// With select
639
    {
640
      select_result *result;
641
642
      select_lex->options|= SELECT_NO_UNLOCK;
643
      unit->set_limit(select_lex);
644
645
      if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
646
      {
647
        lex->link_first_table_back(create_table, link_to_local);
55 by brian
Update for using real bool types.
648
        create_table->create= true;
1 by brian
clean slate
649
      }
650
520.1.22 by Brian Aker
Second pass of thd cleanup
651
      if (!(res= open_and_lock_tables(session, lex->query_tables)))
1 by brian
clean slate
652
      {
653
        /*
654
          Is table which we are changing used somewhere in other parts
655
          of query
656
        */
657
        if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
658
        {
327.2.4 by Brian Aker
Refactoring table.h
659
          TableList *duplicate;
1 by brian
clean slate
660
          create_table= lex->unlink_first_table(&link_to_local);
520.1.22 by Brian Aker
Second pass of thd cleanup
661
          if ((duplicate= unique_table(session, create_table, select_tables, 0)))
1 by brian
clean slate
662
          {
663
            update_non_unique_table_error(create_table, "CREATE", duplicate);
664
            res= 1;
665
            goto end_with_restore_list;
666
          }
667
        }
668
669
        /*
670
          select_create is currently not re-execution friendly and
671
          needs to be created for every execution of a PS/SP.
672
        */
673
        if ((result= new select_create(create_table,
674
                                       &create_info,
675
                                       &alter_info,
676
                                       select_lex->item_list,
677
                                       lex->duplicates,
678
                                       lex->ignore,
679
                                       select_tables)))
680
        {
681
          /*
846 by Brian Aker
Removing on typedeffed class.
682
            CREATE from SELECT give its Select_Lex for SELECT,
1 by brian
clean slate
683
            and item_list belong to SELECT
684
          */
520.1.22 by Brian Aker
Second pass of thd cleanup
685
          res= handle_select(session, lex, result, 0);
1 by brian
clean slate
686
          delete result;
687
        }
688
      }
689
      else if (!(create_info.options & HA_LEX_CREATE_TMP_TABLE))
690
        create_table= lex->unlink_first_table(&link_to_local);
691
692
    }
693
    else
694
    {
695
      /* So that CREATE TEMPORARY TABLE gets to binlog at commit/rollback */
696
      if (create_info.options & HA_LEX_CREATE_TMP_TABLE)
520.1.22 by Brian Aker
Second pass of thd cleanup
697
        session->options|= OPTION_KEEP_LOG;
1 by brian
clean slate
698
      /* regular create */
699
      if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
520.1.22 by Brian Aker
Second pass of thd cleanup
700
        res= mysql_create_like_table(session, create_table, select_tables,
1 by brian
clean slate
701
                                     &create_info);
702
      else
703
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
704
        res= mysql_create_table(session, create_table->db,
1 by brian
clean slate
705
                                create_table->table_name, &create_info,
706
                                &alter_info, 0, 0);
707
      }
708
      if (!res)
836 by Brian Aker
Fixed session call from function to method.
709
	session->my_ok();
1 by brian
clean slate
710
    }
711
712
    /* put tables back for PS rexecuting */
713
end_with_restore_list:
714
    lex->link_first_table_back(create_table, link_to_local);
715
    break;
716
  }
717
  case SQLCOM_CREATE_INDEX:
718
    /* Fall through */
719
  case SQLCOM_DROP_INDEX:
720
  /*
721
    CREATE INDEX and DROP INDEX are implemented by calling ALTER
722
    TABLE with proper arguments.
723
724
    In the future ALTER TABLE will notice that the request is to
725
    only add indexes and create these one by one for the existing
726
    table without having to do a full rebuild.
727
  */
728
  {
729
    /* Prepare stack copies to be re-execution safe */
730
    HA_CREATE_INFO create_info;
520.1.22 by Brian Aker
Second pass of thd cleanup
731
    Alter_info alter_info(lex->alter_info, session->mem_root);
1 by brian
clean slate
732
520.1.22 by Brian Aker
Second pass of thd cleanup
733
    if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1 by brian
clean slate
734
      goto error;
735
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
736
    assert(first_table == all_tables && first_table != 0);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
737
    if (! session->endActiveTransaction())
1 by brian
clean slate
738
      goto error;
739
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
740
    memset(&create_info, 0, sizeof(create_info));
1 by brian
clean slate
741
    create_info.db_type= 0;
742
    create_info.row_type= ROW_TYPE_NOT_USED;
520.1.22 by Brian Aker
Second pass of thd cleanup
743
    create_info.default_table_charset= session->variables.collation_database;
1 by brian
clean slate
744
520.1.22 by Brian Aker
Second pass of thd cleanup
745
    res= mysql_alter_table(session, first_table->db, first_table->table_name,
1 by brian
clean slate
746
                           &create_info, first_table, &alter_info,
327.2.3 by Brian Aker
Refactoring of class Table
747
                           0, (order_st*) 0, 0);
1 by brian
clean slate
748
    break;
749
  }
750
  case SQLCOM_ALTER_TABLE:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
751
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
752
    {
753
      /*
754
        Code in mysql_alter_table() may modify its HA_CREATE_INFO argument,
755
        so we have to use a copy of this structure to make execution
756
        prepared statement- safe. A shallow copy is enough as no memory
757
        referenced from this structure will be modified.
758
      */
759
      HA_CREATE_INFO create_info(lex->create_info);
520.1.22 by Brian Aker
Second pass of thd cleanup
760
      Alter_info alter_info(lex->alter_info, session->mem_root);
1 by brian
clean slate
761
520.1.22 by Brian Aker
Second pass of thd cleanup
762
      if (session->is_fatal_error) /* out of memory creating a copy of alter_info */
1 by brian
clean slate
763
      {
764
        goto error;
765
      }
766
767
      /* Must be set in the parser */
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
768
      assert(select_lex->db);
1 by brian
clean slate
769
770
      { // Rename of table
327.2.4 by Brian Aker
Refactoring table.h
771
          TableList tmp_table;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
772
          memset(&tmp_table, 0, sizeof(tmp_table));
1 by brian
clean slate
773
          tmp_table.table_name= lex->name.str;
774
          tmp_table.db=select_lex->db;
775
      }
776
777
      /* Don't yet allow changing of symlinks with ALTER TABLE */
778
      if (create_info.data_file_name)
520.1.22 by Brian Aker
Second pass of thd cleanup
779
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1 by brian
clean slate
780
                     "DATA DIRECTORY option ignored");
781
      if (create_info.index_file_name)
520.1.22 by Brian Aker
Second pass of thd cleanup
782
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, 0,
1 by brian
clean slate
783
                     "INDEX DIRECTORY option ignored");
784
      create_info.data_file_name= create_info.index_file_name= NULL;
785
      /* ALTER TABLE ends previous transaction */
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
786
      if (! session->endActiveTransaction())
1 by brian
clean slate
787
	goto error;
788
520.1.22 by Brian Aker
Second pass of thd cleanup
789
      if (!session->locked_tables &&
790
          !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1 by brian
clean slate
791
      {
792
        res= 1;
793
        break;
794
      }
795
520.1.22 by Brian Aker
Second pass of thd cleanup
796
      res= mysql_alter_table(session, select_lex->db, lex->name.str,
1 by brian
clean slate
797
                             &create_info,
798
                             first_table,
799
                             &alter_info,
800
                             select_lex->order_list.elements,
327.2.3 by Brian Aker
Refactoring of class Table
801
                             (order_st *) select_lex->order_list.first,
1 by brian
clean slate
802
                             lex->ignore);
803
      break;
804
    }
805
  case SQLCOM_RENAME_TABLE:
806
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
807
    assert(first_table == all_tables && first_table != 0);
327.2.4 by Brian Aker
Refactoring table.h
808
    TableList *table;
1 by brian
clean slate
809
    for (table= first_table; table; table= table->next_local->next_local)
810
    {
327.2.4 by Brian Aker
Refactoring table.h
811
      TableList old_list, new_list;
1 by brian
clean slate
812
      /*
813
        we do not need initialize old_list and new_list because we will
814
        come table[0] and table->next[0] there
815
      */
816
      old_list= table[0];
817
      new_list= table->next_local[0];
818
    }
819
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
820
    if (! session->endActiveTransaction() || drizzle_rename_tables(session, first_table, 0))
821
    {
822
      goto error;
823
    }
1 by brian
clean slate
824
    break;
825
  }
826
  case SQLCOM_SHOW_CREATE:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
827
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
828
    {
884.2.1 by Toru Maesaka
Fixed the bug of SHOW CREATE TABLE not outputting DEFAULT values. Killed some tabular indents on the way too
829
      res= drizzled_show_create(session, first_table);
1 by brian
clean slate
830
      break;
831
    }
832
  case SQLCOM_CHECKSUM:
833
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
834
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
835
    res = mysql_checksum_table(session, first_table, &lex->check_opt);
1 by brian
clean slate
836
    break;
837
  }
838
  case SQLCOM_REPAIR:
839
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
840
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
841
    res= mysql_repair_table(session, first_table, &lex->check_opt);
1 by brian
clean slate
842
    /* ! we write after unlocking the table */
383.1.26 by Brian Aker
Removed option for single command replication and have now left disable of
843
    /*
844
      Presumably, REPAIR and binlog writing doesn't require synchronization
845
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
846
    write_bin_log(session, true, session->query, session->query_length);
481 by Brian Aker
Remove all of uchar.
847
    select_lex->table_list.first= (unsigned char*) first_table;
1 by brian
clean slate
848
    lex->query_tables=all_tables;
849
    break;
850
  }
851
  case SQLCOM_CHECK:
852
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
853
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
854
    res = mysql_check_table(session, first_table, &lex->check_opt);
481 by Brian Aker
Remove all of uchar.
855
    select_lex->table_list.first= (unsigned char*) first_table;
1 by brian
clean slate
856
    lex->query_tables=all_tables;
857
    break;
858
  }
859
  case SQLCOM_ANALYZE:
860
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
861
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
862
    res= mysql_analyze_table(session, first_table, &lex->check_opt);
1 by brian
clean slate
863
    /* ! we write after unlocking the table */
520.1.22 by Brian Aker
Second pass of thd cleanup
864
    write_bin_log(session, true, session->query, session->query_length);
481 by Brian Aker
Remove all of uchar.
865
    select_lex->table_list.first= (unsigned char*) first_table;
1 by brian
clean slate
866
    lex->query_tables=all_tables;
867
    break;
868
  }
869
870
  case SQLCOM_OPTIMIZE:
871
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
872
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
873
    res= mysql_optimize_table(session, first_table, &lex->check_opt);
1 by brian
clean slate
874
    /* ! we write after unlocking the table */
520.1.22 by Brian Aker
Second pass of thd cleanup
875
    write_bin_log(session, true, session->query, session->query_length);
481 by Brian Aker
Remove all of uchar.
876
    select_lex->table_list.first= (unsigned char*) first_table;
1 by brian
clean slate
877
    lex->query_tables=all_tables;
878
    break;
879
  }
880
  case SQLCOM_UPDATE:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
881
    assert(first_table == all_tables && first_table != 0);
826 by Brian Aker
Simplify update
882
    if ((res= update_precheck(session, all_tables)))
1 by brian
clean slate
883
      break;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
884
    assert(select_lex->offset_limit == 0);
1 by brian
clean slate
885
    unit->set_limit(select_lex);
825 by Brian Aker
Cleaned up interface notes on UPDATE
886
    res= mysql_update(session, all_tables,
887
                      select_lex->item_list,
888
                      lex->value_list,
889
                      select_lex->where,
890
                      select_lex->order_list.elements,
891
                      (order_st *) select_lex->order_list.first,
892
                      unit->select_limit_cnt,
893
                      lex->duplicates, lex->ignore);
894
    break;
1 by brian
clean slate
895
  case SQLCOM_UPDATE_MULTI:
896
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
897
    assert(first_table == all_tables && first_table != 0);
826 by Brian Aker
Simplify update
898
    if ((res= update_precheck(session, all_tables)))
825 by Brian Aker
Cleaned up interface notes on UPDATE
899
      break;
900
901
    if ((res= mysql_multi_update_prepare(session)))
902
      break;
1 by brian
clean slate
903
520.1.22 by Brian Aker
Second pass of thd cleanup
904
    res= mysql_multi_update(session, all_tables,
1 by brian
clean slate
905
                            &select_lex->item_list,
906
                            &lex->value_list,
907
                            select_lex->where,
908
                            select_lex->options,
909
                            lex->duplicates, lex->ignore, unit, select_lex);
910
    break;
911
  }
912
  case SQLCOM_REPLACE:
913
  case SQLCOM_INSERT:
914
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
915
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
916
    if ((res= insert_precheck(session, all_tables)))
1 by brian
clean slate
917
      break;
918
520.1.22 by Brian Aker
Second pass of thd cleanup
919
    if (!session->locked_tables &&
920
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1 by brian
clean slate
921
    {
922
      res= 1;
923
      break;
924
    }
925
520.1.22 by Brian Aker
Second pass of thd cleanup
926
    res= mysql_insert(session, all_tables, lex->field_list, lex->many_values,
1 by brian
clean slate
927
		      lex->update_list, lex->value_list,
928
                      lex->duplicates, lex->ignore);
929
930
    break;
931
  }
932
  case SQLCOM_REPLACE_SELECT:
933
  case SQLCOM_INSERT_SELECT:
934
  {
935
    select_result *sel_result;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
936
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
937
    if ((res= insert_precheck(session, all_tables)))
1 by brian
clean slate
938
      break;
939
940
    /* Fix lock for first table */
941
    if (first_table->lock_type == TL_WRITE_DELAYED)
942
      first_table->lock_type= TL_WRITE;
943
944
    /* Don't unlock tables until command is written to binary log */
945
    select_lex->options|= SELECT_NO_UNLOCK;
946
947
    unit->set_limit(select_lex);
948
520.1.22 by Brian Aker
Second pass of thd cleanup
949
    if (! session->locked_tables &&
950
        ! (need_start_waiting= ! wait_if_global_read_lock(session, 0, 1)))
1 by brian
clean slate
951
    {
952
      res= 1;
953
      break;
954
    }
955
520.1.22 by Brian Aker
Second pass of thd cleanup
956
    if (!(res= open_and_lock_tables(session, all_tables)))
1 by brian
clean slate
957
    {
958
      /* Skip first table, which is the table we are inserting in */
327.2.4 by Brian Aker
Refactoring table.h
959
      TableList *second_table= first_table->next_local;
481 by Brian Aker
Remove all of uchar.
960
      select_lex->table_list.first= (unsigned char*) second_table;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
961
      select_lex->context.table_list=
1 by brian
clean slate
962
        select_lex->context.first_name_resolution_table= second_table;
520.1.22 by Brian Aker
Second pass of thd cleanup
963
      res= mysql_insert_select_prepare(session);
1 by brian
clean slate
964
      if (!res && (sel_result= new select_insert(first_table,
965
                                                 first_table->table,
966
                                                 &lex->field_list,
967
                                                 &lex->update_list,
968
                                                 &lex->value_list,
969
                                                 lex->duplicates,
970
                                                 lex->ignore)))
971
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
972
	res= handle_select(session, lex, sel_result, OPTION_SETUP_TABLES_DONE);
1 by brian
clean slate
973
        /*
974
          Invalidate the table in the query cache if something changed
975
          after unlocking when changes become visible.
976
          TODO: this is workaround. right way will be move invalidating in
977
          the unlock procedure.
978
        */
979
        if (first_table->lock_type ==  TL_WRITE_CONCURRENT_INSERT &&
520.1.22 by Brian Aker
Second pass of thd cleanup
980
            session->lock)
1 by brian
clean slate
981
        {
982
          /* INSERT ... SELECT should invalidate only the very first table */
327.2.4 by Brian Aker
Refactoring table.h
983
          TableList *save_table= first_table->next_local;
1 by brian
clean slate
984
          first_table->next_local= 0;
985
          first_table->next_local= save_table;
986
        }
987
        delete sel_result;
988
      }
989
      /* revert changes for SP */
481 by Brian Aker
Remove all of uchar.
990
      select_lex->table_list.first= (unsigned char*) first_table;
1 by brian
clean slate
991
    }
992
993
    break;
994
  }
995
  case SQLCOM_TRUNCATE:
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
996
    if (! session->endActiveTransaction())
1 by brian
clean slate
997
    {
998
      res= -1;
999
      break;
1000
    }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1001
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
1002
    /*
1003
      Don't allow this within a transaction because we want to use
1004
      re-generate table
1005
    */
934.2.28 by Jay Pipes
Renamed some things to our standards and made private methods private...
1006
    if (session->locked_tables || session->inTransaction())
1 by brian
clean slate
1007
    {
934.2.28 by Jay Pipes
Renamed some things to our standards and made private methods private...
1008
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1 by brian
clean slate
1009
      goto error;
1010
    }
1011
520.1.22 by Brian Aker
Second pass of thd cleanup
1012
    res= mysql_truncate(session, first_table, 0);
1 by brian
clean slate
1013
1014
    break;
1015
  case SQLCOM_DELETE:
1016
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1017
    assert(first_table == all_tables && first_table != 0);
1018
    assert(select_lex->offset_limit == 0);
1 by brian
clean slate
1019
    unit->set_limit(select_lex);
1020
520.1.22 by Brian Aker
Second pass of thd cleanup
1021
    if (!session->locked_tables &&
1022
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1 by brian
clean slate
1023
    {
1024
      res= 1;
1025
      break;
1026
    }
1027
520.1.22 by Brian Aker
Second pass of thd cleanup
1028
    res = mysql_delete(session, all_tables, select_lex->where,
1 by brian
clean slate
1029
                       &select_lex->order_list,
1030
                       unit->select_limit_cnt, select_lex->options,
55 by brian
Update for using real bool types.
1031
                       false);
1 by brian
clean slate
1032
    break;
1033
  }
1034
  case SQLCOM_DELETE_MULTI:
1035
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1036
    assert(first_table == all_tables && first_table != 0);
327.2.4 by Brian Aker
Refactoring table.h
1037
    TableList *aux_tables=
520.1.22 by Brian Aker
Second pass of thd cleanup
1038
      (TableList *)session->lex->auxiliary_table_list.first;
1 by brian
clean slate
1039
    multi_delete *del_result;
1040
520.1.22 by Brian Aker
Second pass of thd cleanup
1041
    if (!session->locked_tables &&
1042
        !(need_start_waiting= !wait_if_global_read_lock(session, 0, 1)))
1 by brian
clean slate
1043
    {
1044
      res= 1;
1045
      break;
1046
    }
1047
520.1.22 by Brian Aker
Second pass of thd cleanup
1048
    if ((res= multi_delete_precheck(session, all_tables)))
1 by brian
clean slate
1049
      break;
1050
55 by brian
Update for using real bool types.
1051
    /* condition will be true on SP re-excuting */
1 by brian
clean slate
1052
    if (select_lex->item_list.elements != 0)
1053
      select_lex->item_list.empty();
838 by Brian Aker
More class adoption/method
1054
    if (session->add_item_to_list(new Item_null()))
1 by brian
clean slate
1055
      goto error;
1056
520.1.22 by Brian Aker
Second pass of thd cleanup
1057
    session->set_proc_info("init");
1058
    if ((res= open_and_lock_tables(session, all_tables)))
1 by brian
clean slate
1059
      break;
1060
520.1.22 by Brian Aker
Second pass of thd cleanup
1061
    if ((res= mysql_multi_delete_prepare(session)))
1 by brian
clean slate
1062
      goto error;
1063
520.1.22 by Brian Aker
Second pass of thd cleanup
1064
    if (!session->is_fatal_error &&
1 by brian
clean slate
1065
        (del_result= new multi_delete(aux_tables, lex->table_count)))
1066
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1067
      res= mysql_select(session, &select_lex->ref_pointer_array,
1 by brian
clean slate
1068
			select_lex->get_table_list(),
1069
			select_lex->with_wild,
1070
			select_lex->item_list,
1071
			select_lex->where,
327.2.3 by Brian Aker
Refactoring of class Table
1072
			0, (order_st *)NULL, (order_st *)NULL, (Item *)NULL,
923.1.10 by Brian Aker
Remove dead code around old procedures.
1073
			select_lex->options | session->options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK | OPTION_SETUP_TABLES_DONE,
1 by brian
clean slate
1074
			del_result, unit, select_lex);
520.1.22 by Brian Aker
Second pass of thd cleanup
1075
      res|= session->is_error();
1 by brian
clean slate
1076
      if (res)
1077
        del_result->abort();
1078
      delete del_result;
1079
    }
1080
    else
55 by brian
Update for using real bool types.
1081
      res= true;                                // Error
1 by brian
clean slate
1082
    break;
1083
  }
1084
  case SQLCOM_DROP_TABLE:
1085
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1086
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
1087
    if (!lex->drop_temporary)
1088
    {
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1089
      if (! session->endActiveTransaction())
1 by brian
clean slate
1090
        goto error;
1091
    }
1092
    else
1093
    {
1094
      /* So that DROP TEMPORARY TABLE gets to binlog at commit/rollback */
520.1.22 by Brian Aker
Second pass of thd cleanup
1095
      session->options|= OPTION_KEEP_LOG;
1 by brian
clean slate
1096
    }
1097
    /* DDL and binlog write order protected by LOCK_open */
520.1.22 by Brian Aker
Second pass of thd cleanup
1098
    res= mysql_rm_table(session, first_table, lex->drop_if_exists, lex->drop_temporary);
1 by brian
clean slate
1099
  }
1100
  break;
1101
  case SQLCOM_SHOW_PROCESSLIST:
520.1.22 by Brian Aker
Second pass of thd cleanup
1102
    mysqld_list_processes(session, NULL, lex->verbose);
1 by brian
clean slate
1103
    break;
1104
  case SQLCOM_SHOW_ENGINE_LOGS:
1105
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1106
      res= ha_show_status(session, lex->create_info.db_type, HA_ENGINE_LOGS);
1 by brian
clean slate
1107
      break;
1108
    }
1109
  case SQLCOM_CHANGE_DB:
1110
  {
1111
    LEX_STRING db_str= { (char *) select_lex->db, strlen(select_lex->db) };
1112
520.1.22 by Brian Aker
Second pass of thd cleanup
1113
    if (!mysql_change_db(session, &db_str, false))
836 by Brian Aker
Fixed session call from function to method.
1114
      session->my_ok();
1 by brian
clean slate
1115
1116
    break;
1117
  }
1118
1119
  case SQLCOM_LOAD:
1120
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1121
    assert(first_table == all_tables && first_table != 0);
520.1.22 by Brian Aker
Second pass of thd cleanup
1122
    res= mysql_load(session, lex->exchange, first_table, lex->field_list,
919.1.2 by Brian Aker
Comitting the removal of server code for LOAD DATA "LOCAL" INFILE
1123
                    lex->update_list, lex->value_list, lex->duplicates, lex->ignore);
1 by brian
clean slate
1124
    break;
1125
  }
1126
1127
  case SQLCOM_SET_OPTION:
1128
  {
1129
    List<set_var_base> *lex_var_list= &lex->var_list;
1130
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1131
    if (lex->autocommit && ! session->endActiveTransaction())
1 by brian
clean slate
1132
      goto error;
1133
520.1.22 by Brian Aker
Second pass of thd cleanup
1134
    if (open_and_lock_tables(session, all_tables))
1 by brian
clean slate
1135
      goto error;
520.1.22 by Brian Aker
Second pass of thd cleanup
1136
    if (!(res= sql_set_variables(session, lex_var_list)))
1 by brian
clean slate
1137
    {
836 by Brian Aker
Fixed session call from function to method.
1138
      session->my_ok();
1 by brian
clean slate
1139
    }
1140
    else
1141
    {
1142
      /*
1143
        We encountered some sort of error, but no message was sent.
1144
        Send something semi-generic here since we don't know which
1145
        assignment in the list caused the error.
1146
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1147
      if (!session->is_error())
1 by brian
clean slate
1148
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"SET");
1149
      goto error;
1150
    }
1151
1152
    break;
1153
  }
1154
1155
  case SQLCOM_UNLOCK_TABLES:
1156
    /*
1157
      It is critical for mysqldump --single-transaction --master-data that
1158
      UNLOCK TABLES does not implicitely commit a connection which has only
1159
      done FLUSH TABLES WITH READ LOCK + BEGIN. If this assumption becomes
1160
      false, mysqldump will not work.
1161
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1162
    unlock_locked_tables(session);
1163
    if (session->options & OPTION_TABLE_LOCK)
1 by brian
clean slate
1164
    {
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1165
      (void) session->endActiveTransaction();
520.1.22 by Brian Aker
Second pass of thd cleanup
1166
      session->options&= ~(OPTION_TABLE_LOCK);
1 by brian
clean slate
1167
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1168
    if (session->global_read_lock)
1169
      unlock_global_read_lock(session);
836 by Brian Aker
Fixed session call from function to method.
1170
    session->my_ok();
1 by brian
clean slate
1171
    break;
1172
  case SQLCOM_LOCK_TABLES:
1173
    /*
1174
      We try to take transactional locks if
1175
      - only transactional locks are requested (lex->lock_transactional) and
520.1.22 by Brian Aker
Second pass of thd cleanup
1176
      - no non-transactional locks exist (!session->locked_tables).
1 by brian
clean slate
1177
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1178
    if (lex->lock_transactional && !session->locked_tables)
1 by brian
clean slate
1179
    {
1180
      int rc;
1181
      /*
1182
        All requested locks are transactional and no non-transactional
1183
        locks exist.
1184
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1185
      if ((rc= try_transactional_lock(session, all_tables)) == -1)
1 by brian
clean slate
1186
        goto error;
1187
      if (rc == 0)
1188
      {
836 by Brian Aker
Fixed session call from function to method.
1189
        session->my_ok();
1 by brian
clean slate
1190
        break;
1191
      }
1192
      /*
1193
        Non-transactional locking has been requested or
1194
        non-transactional locks exist already or transactional locks are
1195
        not supported by all storage engines. Take non-transactional
1196
        locks.
1197
      */
1198
    }
1199
    /*
1200
      One or more requested locks are non-transactional and/or
1201
      non-transactional locks exist or a storage engine does not support
1202
      transactional locks. Check if at least one transactional lock is
1203
      requested. If yes, warn about the conversion to non-transactional
1204
      locks or abort in strict mode.
1205
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1206
    if (check_transactional_lock(session, all_tables))
1 by brian
clean slate
1207
      goto error;
520.1.22 by Brian Aker
Second pass of thd cleanup
1208
    unlock_locked_tables(session);
1 by brian
clean slate
1209
    /* we must end the trasaction first, regardless of anything */
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1210
    if (! session->endActiveTransaction())
1 by brian
clean slate
1211
      goto error;
520.1.22 by Brian Aker
Second pass of thd cleanup
1212
    session->in_lock_tables=1;
1213
    session->options|= OPTION_TABLE_LOCK;
1 by brian
clean slate
1214
520.1.22 by Brian Aker
Second pass of thd cleanup
1215
    if (!(res= simple_open_n_lock_tables(session, all_tables)))
1 by brian
clean slate
1216
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1217
      session->locked_tables=session->lock;
1218
      session->lock=0;
1219
      (void) set_handler_table_locks(session, all_tables, false);
836 by Brian Aker
Fixed session call from function to method.
1220
      session->my_ok();
1 by brian
clean slate
1221
    }
1222
    else
1223
    {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1224
      /*
1 by brian
clean slate
1225
        Need to end the current transaction, so the storage engine (InnoDB)
1226
        can free its locks if LOCK TABLES locked some tables before finding
1227
        that it can't lock a table in its list
1228
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1229
      ha_autocommit_or_rollback(session, 1);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1230
      (void) session->endActiveTransaction();
520.1.22 by Brian Aker
Second pass of thd cleanup
1231
      session->options&= ~(OPTION_TABLE_LOCK);
1 by brian
clean slate
1232
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1233
    session->in_lock_tables=0;
1 by brian
clean slate
1234
    break;
1235
  case SQLCOM_CREATE_DB:
1236
  {
1237
    /*
1238
      As mysql_create_db() may modify HA_CREATE_INFO structure passed to
1239
      it, we need to use a copy of LEX::create_info to make execution
1240
      prepared statement- safe.
1241
    */
1242
    HA_CREATE_INFO create_info(lex->create_info);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1243
    if (! session->endActiveTransaction())
1 by brian
clean slate
1244
    {
1245
      res= -1;
1246
      break;
1247
    }
1248
    char *alias;
520.1.22 by Brian Aker
Second pass of thd cleanup
1249
    if (!(alias=session->strmake(lex->name.str, lex->name.length)) ||
1 by brian
clean slate
1250
        check_db_name(&lex->name))
1251
    {
1252
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
1253
      break;
1254
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1255
    res= mysql_create_db(session,(lower_case_table_names == 2 ? alias :
1 by brian
clean slate
1256
                              lex->name.str), &create_info, 0);
1257
    break;
1258
  }
1259
  case SQLCOM_DROP_DB:
1260
  {
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1261
    if (! session->endActiveTransaction())
1 by brian
clean slate
1262
    {
1263
      res= -1;
1264
      break;
1265
    }
1266
    if (check_db_name(&lex->name))
1267
    {
1268
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
1269
      break;
1270
    }
934.2.28 by Jay Pipes
Renamed some things to our standards and made private methods private...
1271
    if (session->locked_tables || session->inTransaction())
1 by brian
clean slate
1272
    {
934.2.28 by Jay Pipes
Renamed some things to our standards and made private methods private...
1273
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1 by brian
clean slate
1274
      goto error;
1275
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1276
    res= mysql_rm_db(session, lex->name.str, lex->drop_if_exists, 0);
1 by brian
clean slate
1277
    break;
1278
  }
1279
  case SQLCOM_ALTER_DB:
1280
  {
1281
    LEX_STRING *db= &lex->name;
1282
    HA_CREATE_INFO create_info(lex->create_info);
1283
    if (check_db_name(db))
1284
    {
1285
      my_error(ER_WRONG_DB_NAME, MYF(0), db->str);
1286
      break;
1287
    }
934.2.28 by Jay Pipes
Renamed some things to our standards and made private methods private...
1288
    if (session->locked_tables || session->inTransaction())
1 by brian
clean slate
1289
    {
934.2.28 by Jay Pipes
Renamed some things to our standards and made private methods private...
1290
      my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
1 by brian
clean slate
1291
      goto error;
1292
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1293
    res= mysql_alter_db(session, db->str, &create_info);
1 by brian
clean slate
1294
    break;
1295
  }
1296
  case SQLCOM_SHOW_CREATE_DB:
1297
  {
1298
    if (check_db_name(&lex->name))
1299
    {
1300
      my_error(ER_WRONG_DB_NAME, MYF(0), lex->name.str);
1301
      break;
1302
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1303
    res= mysqld_show_create_db(session, lex->name.str, &lex->create_info);
1 by brian
clean slate
1304
    break;
1305
  }
1306
  case SQLCOM_FLUSH:
1307
  {
1308
    bool write_to_binlog;
1309
1310
    /*
1311
      reload_cache() will tell us if we are allowed to write to the
1312
      binlog or not.
1313
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1314
    if (!reload_cache(session, lex->type, first_table, &write_to_binlog))
1 by brian
clean slate
1315
    {
1316
      /*
1317
        We WANT to write and we CAN write.
1318
        ! we write after unlocking the table.
1319
      */
1320
      /*
1321
        Presumably, RESET and binlog writing doesn't require synchronization
1322
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1323
      write_bin_log(session, false, session->query, session->query_length);
836 by Brian Aker
Fixed session call from function to method.
1324
      session->my_ok();
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1325
    }
1326
1 by brian
clean slate
1327
    break;
1328
  }
1329
  case SQLCOM_KILL:
1330
  {
1331
    Item *it= (Item *)lex->value_list.head();
1332
520.1.22 by Brian Aker
Second pass of thd cleanup
1333
    if ((!it->fixed && it->fix_fields(lex->session, &it)) || it->check_cols(1))
1 by brian
clean slate
1334
    {
1335
      my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
1336
		 MYF(0));
1337
      goto error;
1338
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1339
    sql_kill(session, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
1 by brian
clean slate
1340
    break;
1341
  }
1342
  case SQLCOM_BEGIN:
520.1.22 by Brian Aker
Second pass of thd cleanup
1343
    if (session->transaction.xid_state.xa_state != XA_NOTR)
1 by brian
clean slate
1344
    {
1345
      my_error(ER_XAER_RMFAIL, MYF(0),
520.1.22 by Brian Aker
Second pass of thd cleanup
1346
               xa_state_names[session->transaction.xid_state.xa_state]);
1 by brian
clean slate
1347
      break;
1348
    }
1349
    /*
1350
      Breakpoints for backup testing.
1351
    */
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1352
    if (! session->startTransaction())
1 by brian
clean slate
1353
      goto error;
836 by Brian Aker
Fixed session call from function to method.
1354
    session->my_ok();
1 by brian
clean slate
1355
    break;
1356
  case SQLCOM_COMMIT:
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1357
    if (! session->endTransaction(lex->tx_release ? COMMIT_RELEASE : lex->tx_chain ? COMMIT_AND_CHAIN : COMMIT))
1 by brian
clean slate
1358
      goto error;
836 by Brian Aker
Fixed session call from function to method.
1359
    session->my_ok();
1 by brian
clean slate
1360
    break;
1361
  case SQLCOM_ROLLBACK:
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1362
    if (! session->endTransaction(lex->tx_release ? ROLLBACK_RELEASE : lex->tx_chain ? ROLLBACK_AND_CHAIN : ROLLBACK))
1 by brian
clean slate
1363
      goto error;
836 by Brian Aker
Fixed session call from function to method.
1364
    session->my_ok();
1 by brian
clean slate
1365
    break;
1366
  case SQLCOM_RELEASE_SAVEPOINT:
1367
  {
1368
    SAVEPOINT *sv;
520.1.22 by Brian Aker
Second pass of thd cleanup
1369
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
1 by brian
clean slate
1370
    {
1371
      if (my_strnncoll(system_charset_info,
481 by Brian Aker
Remove all of uchar.
1372
                       (unsigned char *)lex->ident.str, lex->ident.length,
1373
                       (unsigned char *)sv->name, sv->length) == 0)
1 by brian
clean slate
1374
        break;
1375
    }
1376
    if (sv)
1377
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1378
      if (ha_release_savepoint(session, sv))
55 by brian
Update for using real bool types.
1379
        res= true; // cannot happen
1 by brian
clean slate
1380
      else
836 by Brian Aker
Fixed session call from function to method.
1381
        session->my_ok();
520.1.22 by Brian Aker
Second pass of thd cleanup
1382
      session->transaction.savepoints=sv->prev;
1 by brian
clean slate
1383
    }
1384
    else
1385
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
1386
    break;
1387
  }
1388
  case SQLCOM_ROLLBACK_TO_SAVEPOINT:
1389
  {
1390
    SAVEPOINT *sv;
520.1.22 by Brian Aker
Second pass of thd cleanup
1391
    for (sv=session->transaction.savepoints; sv; sv=sv->prev)
1 by brian
clean slate
1392
    {
1393
      if (my_strnncoll(system_charset_info,
481 by Brian Aker
Remove all of uchar.
1394
                       (unsigned char *)lex->ident.str, lex->ident.length,
1395
                       (unsigned char *)sv->name, sv->length) == 0)
1 by brian
clean slate
1396
        break;
1397
    }
1398
    if (sv)
1399
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1400
      if (ha_rollback_to_savepoint(session, sv))
55 by brian
Update for using real bool types.
1401
        res= true; // cannot happen
1 by brian
clean slate
1402
      else
1403
      {
808 by Brian Aker
Move number of threads to use for pool of threads to module. Removed slave
1404
        if ((session->options & OPTION_KEEP_LOG) || session->transaction.all.modified_non_trans_table)
520.1.22 by Brian Aker
Second pass of thd cleanup
1405
          push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
1406
                       ER_WARNING_NOT_COMPLETE_ROLLBACK,
1407
                       ER(ER_WARNING_NOT_COMPLETE_ROLLBACK));
836 by Brian Aker
Fixed session call from function to method.
1408
        session->my_ok();
1 by brian
clean slate
1409
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
1410
      session->transaction.savepoints=sv;
1 by brian
clean slate
1411
    }
1412
    else
1413
      my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "SAVEPOINT", lex->ident.str);
1414
    break;
1415
  }
1416
  case SQLCOM_SAVEPOINT:
953 by Brian Aker
Remove dead support for not having trans engine (just the remains of it)
1417
    if (!(session->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
836 by Brian Aker
Fixed session call from function to method.
1418
      session->my_ok();
1 by brian
clean slate
1419
    else
1420
    {
1421
      SAVEPOINT **sv, *newsv;
520.1.22 by Brian Aker
Second pass of thd cleanup
1422
      for (sv=&session->transaction.savepoints; *sv; sv=&(*sv)->prev)
1 by brian
clean slate
1423
      {
1424
        if (my_strnncoll(system_charset_info,
481 by Brian Aker
Remove all of uchar.
1425
                         (unsigned char *)lex->ident.str, lex->ident.length,
1426
                         (unsigned char *)(*sv)->name, (*sv)->length) == 0)
1 by brian
clean slate
1427
          break;
1428
      }
1429
      if (*sv) /* old savepoint of the same name exists */
1430
      {
1431
        newsv=*sv;
520.1.22 by Brian Aker
Second pass of thd cleanup
1432
        ha_release_savepoint(session, *sv); // it cannot fail
1 by brian
clean slate
1433
        *sv=(*sv)->prev;
1434
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
1435
      else if ((newsv=(SAVEPOINT *) alloc_root(&session->transaction.mem_root,
1 by brian
clean slate
1436
                                               savepoint_alloc_size)) == 0)
1437
      {
1438
        my_error(ER_OUT_OF_RESOURCES, MYF(0));
1439
        break;
1440
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
1441
      newsv->name=strmake_root(&session->transaction.mem_root,
1 by brian
clean slate
1442
                               lex->ident.str, lex->ident.length);
1443
      newsv->length=lex->ident.length;
1444
      /*
1445
        if we'll get an error here, don't add new savepoint to the list.
1446
        we'll lose a little bit of memory in transaction mem_root, but it'll
1447
        be free'd when transaction ends anyway
1448
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
1449
      if (ha_savepoint(session, newsv))
55 by brian
Update for using real bool types.
1450
        res= true;
1 by brian
clean slate
1451
      else
1452
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1453
        newsv->prev=session->transaction.savepoints;
1454
        session->transaction.savepoints=newsv;
836 by Brian Aker
Fixed session call from function to method.
1455
        session->my_ok();
1 by brian
clean slate
1456
      }
1457
    }
1458
    break;
1459
  default:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1460
    assert(0);                             /* Impossible */
836 by Brian Aker
Fixed session call from function to method.
1461
    session->my_ok();
1 by brian
clean slate
1462
    break;
1463
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1464
  session->set_proc_info("query end");
1 by brian
clean slate
1465
1466
  /*
1467
    The return value for ROW_COUNT() is "implementation dependent" if the
1468
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
1469
    wants. We also keep the last value in case of SQLCOM_CALL or
1470
    SQLCOM_EXECUTE.
1471
  */
602.2.2 by Yoshinori Sano
Fix bugs in params of sql_command_flags[...].test(). Previously, the value such as, 0, 1, 2, 4, 8 and 16 is given to std::bitset.test() in that place, which is misunderstanding of the spec of the test(). The test(size_t n) returns true if bit n is set.
1472
  if (!(sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
520.1.22 by Brian Aker
Second pass of thd cleanup
1473
    session->row_count_func= -1;
1 by brian
clean slate
1474
1475
  goto finish;
1476
1477
error:
55 by brian
Update for using real bool types.
1478
  res= true;
1 by brian
clean slate
1479
1480
finish:
1481
  if (need_start_waiting)
1482
  {
1483
    /*
1484
      Release the protection against the global read lock and wake
1485
      everyone, who might want to set a global read lock.
1486
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
1487
    start_waiting_global_read_lock(session);
1 by brian
clean slate
1488
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1489
  return(res || session->is_error());
1 by brian
clean slate
1490
}
1491
520.1.22 by Brian Aker
Second pass of thd cleanup
1492
bool execute_sqlcom_select(Session *session, TableList *all_tables)
1 by brian
clean slate
1493
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1494
  LEX	*lex= session->lex;
1 by brian
clean slate
1495
  select_result *result=lex->result;
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
1496
  bool res= false;
1 by brian
clean slate
1497
  /* assign global limit variable if limit is not given */
1498
  {
846 by Brian Aker
Removing on typedeffed class.
1499
    Select_Lex *param= lex->unit.global_parameters;
1 by brian
clean slate
1500
    if (!param->explicit_limit)
1501
      param->select_limit=
520.1.22 by Brian Aker
Second pass of thd cleanup
1502
        new Item_int((uint64_t) session->variables.select_limit);
1 by brian
clean slate
1503
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1504
  if (!(res= open_and_lock_tables(session, all_tables)))
1 by brian
clean slate
1505
  {
1506
    if (lex->describe)
1507
    {
1508
      /*
1509
        We always use select_send for EXPLAIN, even if it's an EXPLAIN
1510
        for SELECT ... INTO OUTFILE: a user application should be able
1511
        to prepend EXPLAIN to any query and receive output for it,
1512
        even if the query itself redirects the output.
1513
      */
1514
      if (!(result= new select_send()))
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
1515
        return true;                               /* purecov: inspected */
520.1.22 by Brian Aker
Second pass of thd cleanup
1516
      session->send_explain_fields(result);
1517
      res= mysql_explain_union(session, &session->lex->unit, result);
1 by brian
clean slate
1518
      if (lex->describe & DESCRIBE_EXTENDED)
1519
      {
1520
        char buff[1024];
205 by Brian Aker
uint32 -> uin32_t
1521
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
1 by brian
clean slate
1522
        str.length(0);
520.1.22 by Brian Aker
Second pass of thd cleanup
1523
        session->lex->unit.print(&str, QT_ORDINARY);
1 by brian
clean slate
1524
        str.append('\0');
520.1.22 by Brian Aker
Second pass of thd cleanup
1525
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
1526
                     ER_YES, str.ptr());
1527
      }
1528
      if (res)
1529
        result->abort();
1530
      else
1531
        result->send_eof();
1532
      delete result;
1533
    }
1534
    else
1535
    {
1536
      if (!result && !(result= new select_send()))
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
1537
        return true;                               /* purecov: inspected */
520.1.22 by Brian Aker
Second pass of thd cleanup
1538
      res= handle_select(session, lex, result, 0);
1 by brian
clean slate
1539
      if (result != lex->result)
1540
        delete result;
1541
    }
1542
  }
1543
  return res;
1544
}
1545
1546
1547
#define MY_YACC_INIT 1000			// Start with big alloc
1548
#define MY_YACC_MAX  32000			// Because of 'short'
1549
1550
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
1551
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1552
  LEX	*lex= current_session->lex;
1 by brian
clean slate
1553
  ulong old_info=0;
438.1.13 by Brian Aker
uint cleanup.
1554
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
1 by brian
clean slate
1555
    return 1;
1556
  if (!lex->yacc_yyvs)
1557
    old_info= *yystacksize;
1558
  *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
656.1.46 by Monty Taylor
More malloc return cleanups.
1559
  unsigned char *tmpptr= NULL;
1560
  if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
1561
                                         *yystacksize* sizeof(**yyvs))))
1562
      return 1;
1563
  lex->yacc_yyvs= tmpptr;
1564
  tmpptr= NULL;
1565
  if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
1566
                                        *yystacksize* sizeof(**yyss))))
1567
      return 1;
1568
  lex->yacc_yyss= tmpptr;
1 by brian
clean slate
1569
  if (old_info)
1570
  {						// Copy old info from stack
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
1571
    memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
1572
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
1 by brian
clean slate
1573
  }
1574
  *yyss=(short*) lex->yacc_yyss;
1575
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
1576
  return 0;
1577
}
1578
1579
1580
void
1581
mysql_init_select(LEX *lex)
1582
{
846 by Brian Aker
Removing on typedeffed class.
1583
  Select_Lex *select_lex= lex->current_select;
1 by brian
clean slate
1584
  select_lex->init_select();
1585
  lex->wild= 0;
1586
  if (select_lex == &lex->select_lex)
1587
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1588
    assert(lex->result == 0);
1 by brian
clean slate
1589
    lex->exchange= 0;
1590
  }
1591
}
1592
1593
1594
bool
1595
mysql_new_select(LEX *lex, bool move_down)
1596
{
846 by Brian Aker
Removing on typedeffed class.
1597
  Select_Lex *select_lex;
520.1.22 by Brian Aker
Second pass of thd cleanup
1598
  Session *session= lex->session;
1 by brian
clean slate
1599
846 by Brian Aker
Removing on typedeffed class.
1600
  if (!(select_lex= new (session->mem_root) Select_Lex()))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1601
    return(1);
520.1.22 by Brian Aker
Second pass of thd cleanup
1602
  select_lex->select_number= ++session->select_number;
1 by brian
clean slate
1603
  select_lex->parent_lex= lex; /* Used in init_query. */
1604
  select_lex->init_query();
1605
  select_lex->init_select();
1606
  lex->nest_level++;
1607
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
1608
  {
1609
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1610
    return(1);
1 by brian
clean slate
1611
  }
1612
  select_lex->nest_level= lex->nest_level;
1613
  if (move_down)
1614
  {
848 by Brian Aker
typdef class removal (just... use the name of the class).
1615
    Select_Lex_Unit *unit;
55 by brian
Update for using real bool types.
1616
    lex->subqueries= true;
1 by brian
clean slate
1617
    /* first select_lex of subselect or derived table */
848 by Brian Aker
typdef class removal (just... use the name of the class).
1618
    if (!(unit= new (session->mem_root) Select_Lex_Unit()))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1619
      return(1);
1 by brian
clean slate
1620
1621
    unit->init_query();
1622
    unit->init_select();
520.1.22 by Brian Aker
Second pass of thd cleanup
1623
    unit->session= session;
1 by brian
clean slate
1624
    unit->include_down(lex->current_select);
1625
    unit->link_next= 0;
1626
    unit->link_prev= 0;
1627
    unit->return_to= lex->current_select;
1628
    select_lex->include_down(unit);
1629
    /*
1630
      By default we assume that it is usual subselect and we have outer name
1631
      resolution context, if no we will assign it to 0 later
1632
    */
1633
    select_lex->context.outer_context= &select_lex->outer_select()->context;
1634
  }
1635
  else
1636
  {
1637
    if (lex->current_select->order_list.first && !lex->current_select->braces)
1638
    {
327.2.3 by Brian Aker
Refactoring of class Table
1639
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1640
      return(1);
1 by brian
clean slate
1641
    }
1642
    select_lex->include_neighbour(lex->current_select);
848 by Brian Aker
typdef class removal (just... use the name of the class).
1643
    Select_Lex_Unit *unit= select_lex->master_unit();
520.1.22 by Brian Aker
Second pass of thd cleanup
1644
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1645
      return(1);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1646
    select_lex->context.outer_context=
1 by brian
clean slate
1647
                unit->first_select()->context.outer_context;
1648
  }
1649
1650
  select_lex->master_unit()->global_parameters= select_lex;
847 by Brian Aker
More typdef class removal.
1651
  select_lex->include_global((Select_Lex_Node**)&lex->all_selects_list);
1 by brian
clean slate
1652
  lex->current_select= select_lex;
1653
  /*
1654
    in subquery is SELECT query and we allow resolution of names in SELECT
1655
    list
1656
  */
55 by brian
Update for using real bool types.
1657
  select_lex->context.resolve_in_select_list= true;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1658
  return(0);
1 by brian
clean slate
1659
}
1660
1661
/**
1662
  Create a select to return the same output as 'SELECT @@var_name'.
1663
1664
  Used for SHOW COUNT(*) [ WARNINGS | ERROR].
1665
1666
  This will crash with a core dump if the variable doesn't exists.
1667
1668
  @param var_name		Variable name
1669
*/
1670
1671
void create_select_for_variable(const char *var_name)
1672
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1673
  Session *session;
1 by brian
clean slate
1674
  LEX *lex;
1675
  LEX_STRING tmp, null_lex_string;
1676
  Item *var;
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
1677
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
1678
  char *end= buff;
1 by brian
clean slate
1679
520.1.22 by Brian Aker
Second pass of thd cleanup
1680
  session= current_session;
1681
  lex= session->lex;
1 by brian
clean slate
1682
  mysql_init_select(lex);
1683
  lex->sql_command= SQLCOM_SELECT;
1684
  tmp.str= (char*) var_name;
1685
  tmp.length=strlen(var_name);
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
1686
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
1 by brian
clean slate
1687
  /*
1688
    We set the name of Item to @@session.var_name because that then is used
1689
    as the column name in the output.
1690
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1691
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
1 by brian
clean slate
1692
  {
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
1693
    end+= sprintf(buff, "@@session.%s", var_name);
1 by brian
clean slate
1694
    var->set_name(buff, end-buff, system_charset_info);
838 by Brian Aker
More class adoption/method
1695
    session->add_item_to_list(var);
1 by brian
clean slate
1696
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1697
  return;
1 by brian
clean slate
1698
}
1699
1700
1701
void mysql_init_multi_delete(LEX *lex)
1702
{
1703
  lex->sql_command=  SQLCOM_DELETE_MULTI;
1704
  mysql_init_select(lex);
1705
  lex->select_lex.select_limit= 0;
1706
  lex->unit.select_limit_cnt= HA_POS_ERROR;
1707
  lex->select_lex.table_list.save_and_clear(&lex->auxiliary_table_list);
604 by Brian Aker
Remove lock condition needed (we do row based replication, so... lock is
1708
  lex->lock_option= TL_READ;
1 by brian
clean slate
1709
  lex->query_tables= 0;
1710
  lex->query_tables_last= &lex->query_tables;
1711
}
1712
1713
1714
/**
1715
  Parse a query.
1716
520.1.22 by Brian Aker
Second pass of thd cleanup
1717
  @param       session     Current thread
1 by brian
clean slate
1718
  @param       inBuf   Begining of the query text
1719
  @param       length  Length of the query text
1720
  @param[out]  found_semicolon For multi queries, position of the character of
1721
                               the next query in the query text.
1722
*/
1723
520.1.22 by Brian Aker
Second pass of thd cleanup
1724
void mysql_parse(Session *session, const char *inBuf, uint32_t length,
1 by brian
clean slate
1725
                 const char ** found_semicolon)
1726
{
1727
  /*
1728
    Warning.
1729
    The purpose of query_cache_send_result_to_client() is to lookup the
1730
    query in the query cache first, to avoid parsing and executing it.
1731
    So, the natural implementation would be to:
1732
    - first, call query_cache_send_result_to_client,
1733
    - second, if caching failed, initialise the lexical and syntactic parser.
1734
    The problem is that the query cache depends on a clean initialization
520.1.22 by Brian Aker
Second pass of thd cleanup
1735
    of (among others) lex->safe_to_cache_query and session->server_status,
1 by brian
clean slate
1736
    which are reset respectively in
1737
    - lex_start()
520.1.22 by Brian Aker
Second pass of thd cleanup
1738
    - mysql_reset_session_for_next_command()
1 by brian
clean slate
1739
    So, initializing the lexical analyser *before* using the query cache
1740
    is required for the cache to work properly.
1741
    FIXME: cleanup the dependencies in the code to simplify this.
1742
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
1743
  lex_start(session);
735 by Brian Aker
Refactor session.
1744
  session->reset_for_next_command();
1 by brian
clean slate
1745
1746
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
1747
    LEX *lex= session->lex;
1748
1749
    Lex_input_stream lip(session, inBuf, length);
1750
1751
    bool err= parse_sql(session, &lip);
1 by brian
clean slate
1752
    *found_semicolon= lip.found_semicolon;
1753
1754
    if (!err)
1755
    {
1756
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
1757
	if (! session->is_error())
1 by brian
clean slate
1758
	{
1759
          /*
520.1.22 by Brian Aker
Second pass of thd cleanup
1760
            Binlog logs a string starting from session->query and having length
1761
            session->query_length; so we set session->query_length correctly (to not
1 by brian
clean slate
1762
            log several statements in one event, when we executed only first).
1763
            We set it to not see the ';' (otherwise it would get into binlog
1764
            and Query_log_event::print() would give ';;' output).
1765
            This also helps display only the current query in SHOW
1766
            PROCESSLIST.
1767
            Note that we don't need LOCK_thread_count to modify query_length.
1768
          */
1769
          if (*found_semicolon &&
520.1.22 by Brian Aker
Second pass of thd cleanup
1770
              (session->query_length= (ulong)(*found_semicolon - session->query)))
1771
            session->query_length--;
1 by brian
clean slate
1772
          /* Actually execute the query */
520.1.22 by Brian Aker
Second pass of thd cleanup
1773
          mysql_execute_command(session);
1 by brian
clean slate
1774
	}
1775
      }
1776
    }
1777
    else
1778
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1779
      assert(session->is_error());
1 by brian
clean slate
1780
    }
1781
    lex->unit.cleanup();
520.1.22 by Brian Aker
Second pass of thd cleanup
1782
    session->set_proc_info("freeing items");
1783
    session->end_statement();
1784
    session->cleanup_after_query();
1 by brian
clean slate
1785
  }
1786
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1787
  return;
1 by brian
clean slate
1788
}
1789
1790
1791
1792
/**
1793
  Store field definition for create.
1794
1795
  @return
1796
    Return 0 if ok
1797
*/
1798
520.1.22 by Brian Aker
Second pass of thd cleanup
1799
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
1 by brian
clean slate
1800
		       char *length, char *decimals,
438.1.13 by Brian Aker
uint cleanup.
1801
		       uint32_t type_modifier,
1 by brian
clean slate
1802
                       enum column_format_type column_format,
1803
		       Item *default_value, Item *on_update_value,
1804
                       LEX_STRING *comment,
1805
		       char *change,
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1806
                       List<String> *interval_list, const CHARSET_INFO * const cs,
1807
                       virtual_column_info *vcol_info)
1 by brian
clean slate
1808
{
1809
  register Create_field *new_field;
520.1.22 by Brian Aker
Second pass of thd cleanup
1810
  LEX  *lex= session->lex;
1 by brian
clean slate
1811
1812
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1813
    return(1);				/* purecov: inspected */
1 by brian
clean slate
1814
1815
  if (type_modifier & PRI_KEY_FLAG)
1816
  {
1817
    Key *key;
1818
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
1819
    key= new Key(Key::PRIMARY, null_lex_str,
1820
                      &default_key_create_info,
1821
                      0, lex->col_list);
1822
    lex->alter_info.key_list.push_back(key);
1823
    lex->col_list.empty();
1824
  }
1825
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
1826
  {
1827
    Key *key;
1828
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
1829
    key= new Key(Key::UNIQUE, null_lex_str,
1830
                 &default_key_create_info, 0,
1831
                 lex->col_list);
1832
    lex->alter_info.key_list.push_back(key);
1833
    lex->col_list.empty();
1834
  }
1835
1836
  if (default_value)
1837
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1838
    /*
1 by brian
clean slate
1839
      Default value should be literal => basic constants =>
1840
      no need fix_fields()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1841
1842
      We allow only one function as part of default value -
1 by brian
clean slate
1843
      NOW() as default for TIMESTAMP type.
1844
    */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1845
    if (default_value->type() == Item::FUNC_ITEM &&
1 by brian
clean slate
1846
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1847
         type == DRIZZLE_TYPE_TIMESTAMP))
1 by brian
clean slate
1848
    {
1849
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1850
      return(1);
1 by brian
clean slate
1851
    }
1852
    else if (default_value->type() == Item::NULL_ITEM)
1853
    {
1854
      default_value= 0;
1855
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
1856
	  NOT_NULL_FLAG)
1857
      {
1858
	my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1859
	return(1);
1 by brian
clean slate
1860
      }
1861
    }
1862
    else if (type_modifier & AUTO_INCREMENT_FLAG)
1863
    {
1864
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1865
      return(1);
1 by brian
clean slate
1866
    }
1867
  }
1868
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1869
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
1870
  {
1871
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1872
    return(1);
1 by brian
clean slate
1873
  }
1874
1875
  if (!(new_field= new Create_field()) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
1876
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
1 by brian
clean slate
1877
                      default_value, on_update_value, comment, change,
383.7.1 by Andrey Zhakov
Initial submit of code and tests
1878
                      interval_list, cs, 0, column_format,
1879
                      vcol_info))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1880
    return(1);
1 by brian
clean slate
1881
1882
  lex->alter_info.create_list.push_back(new_field);
1883
  lex->last_field=new_field;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1884
  return(0);
1 by brian
clean slate
1885
}
1886
1887
1888
/** Store position for column in ALTER TABLE .. ADD column. */
1889
1890
void store_position_for_column(const char *name)
1891
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1892
  current_session->lex->last_field->after=const_cast<char*> (name);
1 by brian
clean slate
1893
}
1894
1895
/**
1896
  save order by and tables in own lists.
1897
*/
1898
520.1.22 by Brian Aker
Second pass of thd cleanup
1899
bool add_to_list(Session *session, SQL_LIST &list,Item *item,bool asc)
1 by brian
clean slate
1900
{
327.2.3 by Brian Aker
Refactoring of class Table
1901
  order_st *order;
520.1.22 by Brian Aker
Second pass of thd cleanup
1902
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1903
    return(1);
1 by brian
clean slate
1904
  order->item_ptr= item;
1905
  order->item= &order->item_ptr;
1906
  order->asc = asc;
1907
  order->free_me=0;
1908
  order->used=0;
1909
  order->counter_used= 0;
481 by Brian Aker
Remove all of uchar.
1910
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1911
  return(0);
1 by brian
clean slate
1912
}
1913
1914
1915
/**
1916
  Add a table to list of used tables.
1917
1918
  @param table		Table to add
1919
  @param alias		alias for table (or null if no alias)
1920
  @param table_options	A set of the following bits:
1921
                         - TL_OPTION_UPDATING : Table will be updated
1922
                         - TL_OPTION_FORCE_INDEX : Force usage of index
1923
                         - TL_OPTION_ALIAS : an alias in multi table DELETE
1924
  @param lock_type	How table should be locked
1925
  @param use_index	List of indexed used in USE INDEX
1926
  @param ignore_index	List of indexed used in IGNORE INDEX
1927
1928
  @retval
1929
      0		Error
1930
  @retval
327.2.4 by Brian Aker
Refactoring table.h
1931
    \#	Pointer to TableList element added to the total table list
1 by brian
clean slate
1932
*/
1933
846 by Brian Aker
Removing on typedeffed class.
1934
TableList *Select_Lex::add_table_to_list(Session *session,
1 by brian
clean slate
1935
					     Table_ident *table,
1936
					     LEX_STRING *alias,
202 by Brian Aker
Cleanup sql_lex to modern types.
1937
					     uint32_t table_options,
1 by brian
clean slate
1938
					     thr_lock_type lock_type,
1939
					     List<Index_hint> *index_hints_arg,
1940
                                             LEX_STRING *option)
1941
{
327.2.4 by Brian Aker
Refactoring table.h
1942
  register TableList *ptr;
1943
  TableList *previous_table_ref; /* The table preceding the current one. */
1 by brian
clean slate
1944
  char *alias_str;
520.1.22 by Brian Aker
Second pass of thd cleanup
1945
  LEX *lex= session->lex;
1 by brian
clean slate
1946
1947
  if (!table)
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1948
    return(0);				// End of memory
1 by brian
clean slate
1949
  alias_str= alias ? alias->str : table->table.str;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1950
  if (!test(table_options & TL_OPTION_ALIAS) &&
1 by brian
clean slate
1951
      check_table_name(table->table.str, table->table.length))
1952
  {
1953
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1954
    return(0);
1 by brian
clean slate
1955
  }
1956
55 by brian
Update for using real bool types.
1957
  if (table->is_derived_table() == false && table->db.str &&
1 by brian
clean slate
1958
      check_db_name(&table->db))
1959
  {
1960
    my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1961
    return(0);
1 by brian
clean slate
1962
  }
1963
1964
  if (!alias)					/* Alias is case sensitive */
1965
  {
1966
    if (table->sel)
1967
    {
1968
      my_message(ER_DERIVED_MUST_HAVE_ALIAS,
1969
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1970
      return(0);
1 by brian
clean slate
1971
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
1972
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1973
      return(0);
1 by brian
clean slate
1974
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1975
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1976
    return(0);				/* purecov: inspected */
1 by brian
clean slate
1977
  if (table->db.str)
1978
  {
55 by brian
Update for using real bool types.
1979
    ptr->is_fqtn= true;
1 by brian
clean slate
1980
    ptr->db= table->db.str;
1981
    ptr->db_length= table->db.length;
1982
  }
1983
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1984
    return(0);
1 by brian
clean slate
1985
  else
55 by brian
Update for using real bool types.
1986
    ptr->is_fqtn= false;
1 by brian
clean slate
1987
1988
  ptr->alias= alias_str;
55 by brian
Update for using real bool types.
1989
  ptr->is_alias= alias ? true : false;
1 by brian
clean slate
1990
  if (lower_case_table_names && table->table.length)
1991
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
1992
  ptr->table_name=table->table.str;
1993
  ptr->table_name_length=table->table.length;
1994
  ptr->lock_type=   lock_type;
1995
  ptr->lock_timeout= -1;      /* default timeout */
1996
  ptr->lock_transactional= 1; /* allow transactional locks */
1997
  ptr->updating=    test(table_options & TL_OPTION_UPDATING);
1998
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
1999
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
2000
  ptr->derived=	    table->sel;
2001
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
575.4.7 by Monty Taylor
More header cleanup.
2002
                                      INFORMATION_SCHEMA_NAME.c_str()))
1 by brian
clean slate
2003
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
2004
    ST_SCHEMA_TABLE *schema_table= find_schema_table(session, ptr->table_name);
1 by brian
clean slate
2005
    if (!schema_table ||
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2006
        (schema_table->hidden &&
602.2.2 by Yoshinori Sano
Fix bugs in params of sql_command_flags[...].test(). Previously, the value such as, 0, 1, 2, 4, 8 and 16 is given to std::bitset.test() in that place, which is misunderstanding of the spec of the test(). The test(size_t n) returns true if bit n is set.
2007
         ((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
1 by brian
clean slate
2008
          /*
2009
            this check is used for show columns|keys from I_S hidden table
2010
          */
2011
          lex->sql_command == SQLCOM_SHOW_FIELDS ||
2012
          lex->sql_command == SQLCOM_SHOW_KEYS)))
2013
    {
2014
      my_error(ER_UNKNOWN_TABLE, MYF(0),
575.4.7 by Monty Taylor
More header cleanup.
2015
               ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2016
      return(0);
1 by brian
clean slate
2017
    }
2018
    ptr->schema_table_name= ptr->table_name;
2019
    ptr->schema_table= schema_table;
2020
  }
2021
  ptr->select_lex=  lex->current_select;
2022
  ptr->cacheable_table= 1;
2023
  ptr->index_hints= index_hints_arg;
2024
  ptr->option= option ? option->str : 0;
2025
  /* check that used name is unique */
2026
  if (lock_type != TL_IGNORE)
2027
  {
327.2.4 by Brian Aker
Refactoring table.h
2028
    TableList *first_table= (TableList*) table_list.first;
2029
    for (TableList *tables= first_table ;
1 by brian
clean slate
2030
	 tables ;
2031
	 tables=tables->next_local)
2032
    {
2033
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
2034
	  !strcmp(ptr->db, tables->db))
2035
      {
2036
	my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str); /* purecov: tested */
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2037
	return(0);				/* purecov: tested */
1 by brian
clean slate
2038
      }
2039
    }
2040
  }
2041
  /* Store the table reference preceding the current one. */
2042
  if (table_list.elements > 0)
2043
  {
2044
    /*
327.2.4 by Brian Aker
Refactoring table.h
2045
      table_list.next points to the last inserted TableList->next_local'
1 by brian
clean slate
2046
      element
2047
      We don't use the offsetof() macro here to avoid warnings from gcc
2048
    */
327.2.4 by Brian Aker
Refactoring table.h
2049
    previous_table_ref= (TableList*) ((char*) table_list.next -
1 by brian
clean slate
2050
                                       ((char*) &(ptr->next_local) -
2051
                                        (char*) ptr));
2052
    /*
2053
      Set next_name_resolution_table of the previous table reference to point
2054
      to the current table reference. In effect the list
327.2.4 by Brian Aker
Refactoring table.h
2055
      TableList::next_name_resolution_table coincides with
2056
      TableList::next_local. Later this may be changed in
1 by brian
clean slate
2057
      store_top_level_join_columns() for NATURAL/USING joins.
2058
    */
2059
    previous_table_ref->next_name_resolution_table= ptr;
2060
  }
2061
2062
  /*
2063
    Link the current table reference in a local list (list for current select).
2064
    Notice that as a side effect here we set the next_local field of the
2065
    previous table reference to 'ptr'. Here we also add one element to the
2066
    list 'table_list'.
2067
  */
481 by Brian Aker
Remove all of uchar.
2068
  table_list.link_in_list((unsigned char*) ptr, (unsigned char**) &ptr->next_local);
1 by brian
clean slate
2069
  ptr->next_name_resolution_table= NULL;
2070
  /* Link table in global list (all used tables) */
2071
  lex->add_to_query_tables(ptr);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2072
  return(ptr);
1 by brian
clean slate
2073
}
2074
2075
2076
/**
2077
  Initialize a new table list for a nested join.
2078
327.2.4 by Brian Aker
Refactoring table.h
2079
    The function initializes a structure of the TableList type
1 by brian
clean slate
2080
    for a nested join. It sets up its nested join list as empty.
2081
    The created structure is added to the front of the current
846 by Brian Aker
Removing on typedeffed class.
2082
    join list in the Select_Lex object. Then the function
1 by brian
clean slate
2083
    changes the current nest level for joins to refer to the newly
2084
    created empty list after having saved the info on the old level
2085
    in the initialized structure.
2086
520.1.22 by Brian Aker
Second pass of thd cleanup
2087
  @param session         current thread
1 by brian
clean slate
2088
2089
  @retval
2090
    0   if success
2091
  @retval
2092
    1   otherwise
2093
*/
2094
846 by Brian Aker
Removing on typedeffed class.
2095
bool Select_Lex::init_nested_join(Session *session)
1 by brian
clean slate
2096
{
327.2.4 by Brian Aker
Refactoring table.h
2097
  TableList *ptr;
2098
  nested_join_st *nested_join;
1 by brian
clean slate
2099
520.1.22 by Brian Aker
Second pass of thd cleanup
2100
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
327.2.4 by Brian Aker
Refactoring table.h
2101
                                       sizeof(nested_join_st))))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2102
    return(1);
1 by brian
clean slate
2103
  nested_join= ptr->nested_join=
481 by Brian Aker
Remove all of uchar.
2104
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1 by brian
clean slate
2105
2106
  join_list->push_front(ptr);
2107
  ptr->embedding= embedding;
2108
  ptr->join_list= join_list;
2109
  ptr->alias= (char*) "(nested_join)";
2110
  embedding= ptr;
2111
  join_list= &nested_join->join_list;
2112
  join_list->empty();
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2113
  return(0);
1 by brian
clean slate
2114
}
2115
2116
2117
/**
2118
  End a nested join table list.
2119
2120
    The function returns to the previous join nest level.
2121
    If the current level contains only one member, the function
2122
    moves it one level up, eliminating the nest.
2123
520.1.22 by Brian Aker
Second pass of thd cleanup
2124
  @param session         current thread
1 by brian
clean slate
2125
2126
  @return
327.2.4 by Brian Aker
Refactoring table.h
2127
    - Pointer to TableList element added to the total table list, if success
1 by brian
clean slate
2128
    - 0, otherwise
2129
*/
2130
846 by Brian Aker
Removing on typedeffed class.
2131
TableList *Select_Lex::end_nested_join(Session *)
1 by brian
clean slate
2132
{
327.2.4 by Brian Aker
Refactoring table.h
2133
  TableList *ptr;
2134
  nested_join_st *nested_join;
1 by brian
clean slate
2135
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2136
  assert(embedding);
1 by brian
clean slate
2137
  ptr= embedding;
2138
  join_list= ptr->join_list;
2139
  embedding= ptr->embedding;
2140
  nested_join= ptr->nested_join;
2141
  if (nested_join->join_list.elements == 1)
2142
  {
327.2.4 by Brian Aker
Refactoring table.h
2143
    TableList *embedded= nested_join->join_list.head();
1 by brian
clean slate
2144
    join_list->pop();
2145
    embedded->join_list= join_list;
2146
    embedded->embedding= embedding;
2147
    join_list->push_front(embedded);
2148
    ptr= embedded;
2149
  }
2150
  else if (nested_join->join_list.elements == 0)
2151
  {
2152
    join_list->pop();
2153
    ptr= 0;                                     // return value
2154
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2155
  return(ptr);
1 by brian
clean slate
2156
}
2157
2158
2159
/**
2160
  Nest last join operation.
2161
2162
    The function nest last join operation as if it was enclosed in braces.
2163
520.1.22 by Brian Aker
Second pass of thd cleanup
2164
  @param session         current thread
1 by brian
clean slate
2165
2166
  @retval
2167
    0  Error
2168
  @retval
327.2.4 by Brian Aker
Refactoring table.h
2169
    \#  Pointer to TableList element created for the new nested join
1 by brian
clean slate
2170
*/
2171
846 by Brian Aker
Removing on typedeffed class.
2172
TableList *Select_Lex::nest_last_join(Session *session)
1 by brian
clean slate
2173
{
327.2.4 by Brian Aker
Refactoring table.h
2174
  TableList *ptr;
2175
  nested_join_st *nested_join;
2176
  List<TableList> *embedded_list;
1 by brian
clean slate
2177
520.1.22 by Brian Aker
Second pass of thd cleanup
2178
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
327.2.4 by Brian Aker
Refactoring table.h
2179
                                       sizeof(nested_join_st))))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2180
    return(0);
1 by brian
clean slate
2181
  nested_join= ptr->nested_join=
481 by Brian Aker
Remove all of uchar.
2182
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1 by brian
clean slate
2183
2184
  ptr->embedding= embedding;
2185
  ptr->join_list= join_list;
2186
  ptr->alias= (char*) "(nest_last_join)";
2187
  embedded_list= &nested_join->join_list;
2188
  embedded_list->empty();
2189
438.1.13 by Brian Aker
uint cleanup.
2190
  for (uint32_t i=0; i < 2; i++)
1 by brian
clean slate
2191
  {
327.2.4 by Brian Aker
Refactoring table.h
2192
    TableList *table= join_list->pop();
1 by brian
clean slate
2193
    table->join_list= embedded_list;
2194
    table->embedding= ptr;
2195
    embedded_list->push_back(table);
2196
    if (table->natural_join)
2197
    {
55 by brian
Update for using real bool types.
2198
      ptr->is_natural_join= true;
1 by brian
clean slate
2199
      /*
2200
        If this is a JOIN ... USING, move the list of joined fields to the
2201
        table reference that describes the join.
2202
      */
2203
      if (prev_join_using)
2204
        ptr->join_using_fields= prev_join_using;
2205
    }
2206
  }
2207
  join_list->push_front(ptr);
2208
  nested_join->used_tables= nested_join->not_null_tables= (table_map) 0;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2209
  return(ptr);
1 by brian
clean slate
2210
}
2211
2212
2213
/**
2214
  Add a table to the current join list.
2215
2216
    The function puts a table in front of the current join list
846 by Brian Aker
Removing on typedeffed class.
2217
    of Select_Lex object.
1 by brian
clean slate
2218
    Thus, joined tables are put into this list in the reverse order
2219
    (the most outer join operation follows first).
2220
2221
  @param table       the table to add
2222
2223
  @return
2224
    None
2225
*/
2226
846 by Brian Aker
Removing on typedeffed class.
2227
void Select_Lex::add_joined_table(TableList *table)
1 by brian
clean slate
2228
{
2229
  join_list->push_front(table);
2230
  table->join_list= join_list;
2231
  table->embedding= embedding;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2232
  return;
1 by brian
clean slate
2233
}
2234
2235
2236
/**
2237
  Convert a right join into equivalent left join.
2238
2239
    The function takes the current join list t[0],t[1] ... and
2240
    effectively converts it into the list t[1],t[0] ...
2241
    Although the outer_join flag for the new nested table contains
2242
    JOIN_TYPE_RIGHT, it will be handled as the inner table of a left join
2243
    operation.
2244
2245
  EXAMPLES
2246
  @verbatim
2247
    SELECT * FROM t1 RIGHT JOIN t2 ON on_expr =>
2248
      SELECT * FROM t2 LEFT JOIN t1 ON on_expr
2249
2250
    SELECT * FROM t1,t2 RIGHT JOIN t3 ON on_expr =>
2251
      SELECT * FROM t1,t3 LEFT JOIN t2 ON on_expr
2252
2253
    SELECT * FROM t1,t2 RIGHT JOIN (t3,t4) ON on_expr =>
2254
      SELECT * FROM t1,(t3,t4) LEFT JOIN t2 ON on_expr
2255
2256
    SELECT * FROM t1 LEFT JOIN t2 ON on_expr1 RIGHT JOIN t3  ON on_expr2 =>
2257
      SELECT * FROM t3 LEFT JOIN (t1 LEFT JOIN t2 ON on_expr2) ON on_expr1
2258
   @endverbatim
2259
520.1.22 by Brian Aker
Second pass of thd cleanup
2260
  @param session         current thread
1 by brian
clean slate
2261
2262
  @return
2263
    - Pointer to the table representing the inner table, if success
2264
    - 0, otherwise
2265
*/
2266
846 by Brian Aker
Removing on typedeffed class.
2267
TableList *Select_Lex::convert_right_join()
1 by brian
clean slate
2268
{
327.2.4 by Brian Aker
Refactoring table.h
2269
  TableList *tab2= join_list->pop();
2270
  TableList *tab1= join_list->pop();
1 by brian
clean slate
2271
2272
  join_list->push_front(tab2);
2273
  join_list->push_front(tab1);
2274
  tab1->outer_join|= JOIN_TYPE_RIGHT;
2275
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2276
  return(tab1);
1 by brian
clean slate
2277
}
2278
2279
/**
2280
  Set lock for all tables in current select level.
2281
2282
  @param lock_type			Lock to set for tables
2283
2284
  @note
2285
    If lock is a write lock, then tables->updating is set 1
2286
    This is to get tables_ok to know that the table is updated by the
2287
    query
2288
*/
2289
846 by Brian Aker
Removing on typedeffed class.
2290
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
1 by brian
clean slate
2291
{
2292
  bool for_update= lock_type >= TL_READ_NO_INSERT;
2293
327.2.4 by Brian Aker
Refactoring table.h
2294
  for (TableList *tables= (TableList*) table_list.first;
1 by brian
clean slate
2295
       tables;
2296
       tables= tables->next_local)
2297
  {
2298
    tables->lock_type= lock_type;
2299
    tables->updating=  for_update;
2300
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2301
  return;
1 by brian
clean slate
2302
}
2303
2304
2305
/**
846 by Brian Aker
Removing on typedeffed class.
2306
  Create a fake Select_Lex for a unit.
1 by brian
clean slate
2307
846 by Brian Aker
Removing on typedeffed class.
2308
    The method create a fake Select_Lex object for a unit.
1 by brian
clean slate
2309
    This object is created for any union construct containing a union
2310
    operation and also for any single select union construct of the form
2311
    @verbatim
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2312
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ...
1 by brian
clean slate
2313
    @endvarbatim
2314
    or of the form
2315
    @varbatim
327.2.3 by Brian Aker
Refactoring of class Table
2316
    (SELECT ... order_st BY LIMIT n) order_st BY ...
1 by brian
clean slate
2317
    @endvarbatim
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2318
520.1.22 by Brian Aker
Second pass of thd cleanup
2319
  @param session_arg		   thread handle
1 by brian
clean slate
2320
2321
  @note
2322
    The object is used to retrieve rows from the temporary table
2323
    where the result on the union is obtained.
2324
2325
  @retval
2326
    1     on failure to create the object
2327
  @retval
2328
    0     on success
2329
*/
2330
848 by Brian Aker
typdef class removal (just... use the name of the class).
2331
bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
1 by brian
clean slate
2332
{
846 by Brian Aker
Removing on typedeffed class.
2333
  Select_Lex *first_sl= first_select();
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2334
  assert(!fake_select_lex);
1 by brian
clean slate
2335
846 by Brian Aker
Removing on typedeffed class.
2336
  if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2337
      return(1);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2338
  fake_select_lex->include_standalone(this,
847 by Brian Aker
More typdef class removal.
2339
                                      (Select_Lex_Node**)&fake_select_lex);
1 by brian
clean slate
2340
  fake_select_lex->select_number= INT_MAX;
520.1.22 by Brian Aker
Second pass of thd cleanup
2341
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1 by brian
clean slate
2342
  fake_select_lex->make_empty_select();
2343
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
2344
  fake_select_lex->select_limit= 0;
2345
2346
  fake_select_lex->context.outer_context=first_sl->context.outer_context;
327.2.3 by Brian Aker
Refactoring of class Table
2347
  /* allow item list resolving in fake select for order_st BY */
55 by brian
Update for using real bool types.
2348
  fake_select_lex->context.resolve_in_select_list= true;
1 by brian
clean slate
2349
  fake_select_lex->context.select_lex= fake_select_lex;
2350
2351
  if (!is_union())
2352
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2353
    /*
2354
      This works only for
327.2.3 by Brian Aker
Refactoring of class Table
2355
      (SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
2356
      (SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
1 by brian
clean slate
2357
      just before the parser starts processing order_list
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2358
    */
1 by brian
clean slate
2359
    global_parameters= fake_select_lex;
2360
    fake_select_lex->no_table_names_allowed= 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
2361
    session_arg->lex->current_select= fake_select_lex;
1 by brian
clean slate
2362
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
2363
  session_arg->lex->pop_context();
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2364
  return(0);
1 by brian
clean slate
2365
}
2366
2367
2368
/**
2369
  Push a new name resolution context for a JOIN ... ON clause to the
2370
  context stack of a query block.
2371
2372
    Create a new name resolution context for a JOIN ... ON clause,
2373
    set the first and last leaves of the list of table references
2374
    to be used for name resolution, and push the newly created
2375
    context to the stack of contexts of the query.
2376
520.1.22 by Brian Aker
Second pass of thd cleanup
2377
  @param session       pointer to current thread
1 by brian
clean slate
2378
  @param left_op   left  operand of the JOIN
2379
  @param right_op  rigth operand of the JOIN
2380
2381
  @retval
55 by brian
Update for using real bool types.
2382
    false  if all is OK
1 by brian
clean slate
2383
  @retval
55 by brian
Update for using real bool types.
2384
    true   if a memory allocation error occured
1 by brian
clean slate
2385
*/
2386
2387
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
2388
push_new_name_resolution_context(Session *session,
327.2.4 by Brian Aker
Refactoring table.h
2389
                                 TableList *left_op, TableList *right_op)
1 by brian
clean slate
2390
{
2391
  Name_resolution_context *on_context;
520.1.22 by Brian Aker
Second pass of thd cleanup
2392
  if (!(on_context= new (session->mem_root) Name_resolution_context))
55 by brian
Update for using real bool types.
2393
    return true;
1 by brian
clean slate
2394
  on_context->init();
2395
  on_context->first_name_resolution_table=
2396
    left_op->first_leaf_for_name_resolution();
2397
  on_context->last_name_resolution_table=
2398
    right_op->last_leaf_for_name_resolution();
520.1.22 by Brian Aker
Second pass of thd cleanup
2399
  return session->lex->push_context(on_context);
1 by brian
clean slate
2400
}
2401
2402
2403
/**
2404
  Add an ON condition to the second operand of a JOIN ... ON.
2405
2406
    Add an ON condition to the right operand of a JOIN ... ON clause.
2407
2408
  @param b     the second operand of a JOIN ... ON
2409
  @param expr  the condition to be added to the ON clause
2410
2411
  @retval
55 by brian
Update for using real bool types.
2412
    false  if there was some error
1 by brian
clean slate
2413
  @retval
55 by brian
Update for using real bool types.
2414
    true   if all is OK
1 by brian
clean slate
2415
*/
2416
327.2.4 by Brian Aker
Refactoring table.h
2417
void add_join_on(TableList *b, Item *expr)
1 by brian
clean slate
2418
{
2419
  if (expr)
2420
  {
2421
    if (!b->on_expr)
2422
      b->on_expr= expr;
2423
    else
2424
    {
2425
      /*
2426
        If called from the parser, this happens if you have both a
2427
        right and left join. If called later, it happens if we add more
2428
        than one condition to the ON clause.
2429
      */
2430
      b->on_expr= new Item_cond_and(b->on_expr,expr);
2431
    }
2432
    b->on_expr->top_level_item();
2433
  }
2434
}
2435
2436
2437
/**
2438
  Mark that there is a NATURAL JOIN or JOIN ... USING between two
2439
  tables.
2440
2441
    This function marks that table b should be joined with a either via
2442
    a NATURAL JOIN or via JOIN ... USING. Both join types are special
2443
    cases of each other, so we treat them together. The function
2444
    setup_conds() creates a list of equal condition between all fields
2445
    of the same name for NATURAL JOIN or the fields in 'using_fields'
2446
    for JOIN ... USING. The list of equality conditions is stored
2447
    either in b->on_expr, or in JOIN::conds, depending on whether there
2448
    was an outer join.
2449
2450
  EXAMPLE
2451
  @verbatim
2452
    SELECT * FROM t1 NATURAL LEFT JOIN t2
2453
     <=>
2454
    SELECT * FROM t1 LEFT JOIN t2 ON (t1.i=t2.i and t1.j=t2.j ... )
2455
2456
    SELECT * FROM t1 NATURAL JOIN t2 WHERE <some_cond>
2457
     <=>
2458
    SELECT * FROM t1, t2 WHERE (t1.i=t2.i and t1.j=t2.j and <some_cond>)
2459
2460
    SELECT * FROM t1 JOIN t2 USING(j) WHERE <some_cond>
2461
     <=>
2462
    SELECT * FROM t1, t2 WHERE (t1.j=t2.j and <some_cond>)
2463
   @endverbatim
2464
2465
  @param a		  Left join argument
2466
  @param b		  Right join argument
2467
  @param using_fields    Field names from USING clause
2468
*/
2469
327.2.4 by Brian Aker
Refactoring table.h
2470
void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
846 by Brian Aker
Removing on typedeffed class.
2471
                      Select_Lex *lex)
1 by brian
clean slate
2472
{
2473
  b->natural_join= a;
2474
  lex->prev_join_using= using_fields;
2475
}
2476
2477
2478
/**
2479
  Reload/resets privileges and the different caches.
2480
520.1.22 by Brian Aker
Second pass of thd cleanup
2481
  @param session Thread handler (can be NULL!)
1 by brian
clean slate
2482
  @param options What should be reset/reloaded (tables, privileges, slave...)
2483
  @param tables Tables to flush (if any)
2484
  @param write_to_binlog True if we can write to the binlog.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2485
1 by brian
clean slate
2486
  @note Depending on 'options', it may be very bad to write the
2487
    query to the binlog (e.g. FLUSH SLAVE); this is a
2488
    pointer where reload_cache() will put 0 if
2489
    it thinks we really should not write to the binlog.
2490
    Otherwise it will put 1.
2491
2492
  @return Error status code
2493
    @retval 0 Ok
520.1.22 by Brian Aker
Second pass of thd cleanup
2494
    @retval !=0  Error; session->killed is set or session->is_error() is true
1 by brian
clean slate
2495
*/
2496
855 by Brian Aker
Refactor reset of status.
2497
bool reload_cache(Session *session, ulong options, TableList *tables, bool *write_to_binlog)
1 by brian
clean slate
2498
{
2499
  bool result=0;
2500
  select_errors=0;				/* Write if more errors */
2501
  bool tmp_write_to_binlog= 1;
2502
2503
  if (options & REFRESH_LOG)
2504
  {
2505
    /*
2506
      Flush the normal query log, the update log, the binary log,
2507
      the slow query log, the relay log (if it exists) and the log
2508
      tables.
2509
    */
2510
2511
    /*
2512
      Writing this command to the binlog may result in infinite loops
2513
      when doing mysqlbinlog|mysql, and anyway it does not really make
2514
      sense to log it automatically (would cause more trouble to users
2515
      than it would help them)
2516
    */
2517
    tmp_write_to_binlog= 0;
2518
2519
    if (ha_flush_logs(NULL))
2520
      result=1;
2521
  }
2522
  /*
2523
    Note that if REFRESH_READ_LOCK bit is set then REFRESH_TABLES is set too
2524
    (see sql_yacc.yy)
2525
  */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2526
  if (options & (REFRESH_TABLES | REFRESH_READ_LOCK))
1 by brian
clean slate
2527
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
2528
    if ((options & REFRESH_READ_LOCK) && session)
1 by brian
clean slate
2529
    {
2530
      /*
2531
        We must not try to aspire a global read lock if we have a write
2532
        locked table. This would lead to a deadlock when trying to
2533
        reopen (and re-lock) the table after the flush.
2534
      */
520.1.22 by Brian Aker
Second pass of thd cleanup
2535
      if (session->locked_tables)
1 by brian
clean slate
2536
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
2537
        THR_LOCK_DATA **lock_p= session->locked_tables->locks;
2538
        THR_LOCK_DATA **end_p= lock_p + session->locked_tables->lock_count;
1 by brian
clean slate
2539
2540
        for (; lock_p < end_p; lock_p++)
2541
        {
2542
          if ((*lock_p)->type >= TL_WRITE_ALLOW_WRITE)
2543
          {
2544
            my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
2545
            return 1;
2546
          }
2547
        }
2548
      }
2549
      /*
2550
	Writing to the binlog could cause deadlocks, as we don't log
2551
	UNLOCK TABLES
2552
      */
2553
      tmp_write_to_binlog= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
2554
      if (lock_global_read_lock(session))
1 by brian
clean slate
2555
	return 1;                               // Killed
520.1.22 by Brian Aker
Second pass of thd cleanup
2556
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
55 by brian
Update for using real bool types.
2557
                                  false : true, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
2558
      if (make_global_read_lock_block_commit(session)) // Killed
1 by brian
clean slate
2559
      {
2560
        /* Don't leave things in a half-locked state */
520.1.22 by Brian Aker
Second pass of thd cleanup
2561
        unlock_global_read_lock(session);
1 by brian
clean slate
2562
        return 1;
2563
      }
2564
    }
2565
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
2566
      result= close_cached_tables(session, tables, false, (options & REFRESH_FAST) ?
55 by brian
Update for using real bool types.
2567
                                  false : true, false);
1 by brian
clean slate
2568
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
2569
  if (session && (options & REFRESH_STATUS))
855 by Brian Aker
Refactor reset of status.
2570
    session->refresh_status();
1 by brian
clean slate
2571
 *write_to_binlog= tmp_write_to_binlog;
855 by Brian Aker
Refactor reset of status.
2572
1 by brian
clean slate
2573
 return result;
2574
}
2575
2576
2577
/**
2578
  kill on thread.
2579
520.1.22 by Brian Aker
Second pass of thd cleanup
2580
  @param session			Thread class
1 by brian
clean slate
2581
  @param id			Thread id
2582
  @param only_kill_query        Should it kill the query or the connection
2583
2584
  @note
2585
    This is written such that we have a short lock on LOCK_thread_count
2586
*/
2587
230.1.9 by Monty Taylor
Merged in remove-include-dir.
2588
static unsigned int
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
2589
kill_one_thread(Session *, ulong id, bool only_kill_query)
1 by brian
clean slate
2590
{
934.3.10 by Monty Taylor
Revert std::vector changes. dammit.
2591
  Session *tmp;
438.1.13 by Brian Aker
uint cleanup.
2592
  uint32_t error=ER_NO_SUCH_THREAD;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
2593
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
934.3.10 by Monty Taylor
Revert std::vector changes. dammit.
2594
  I_List_iterator<Session> it(session_list);
2595
  while ((tmp=it++))
1 by brian
clean slate
2596
  {
2597
    if (tmp->thread_id == id)
2598
    {
2599
      pthread_mutex_lock(&tmp->LOCK_delete);	// Lock from delete
2600
      break;
2601
    }
2602
  }
398.1.10 by Monty Taylor
Actually removed VOID() this time.
2603
  pthread_mutex_unlock(&LOCK_thread_count);
1 by brian
clean slate
2604
  if (tmp)
2605
  {
520.1.21 by Brian Aker
THD -> Session rename
2606
    tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
1 by brian
clean slate
2607
    error=0;
2608
    pthread_mutex_unlock(&tmp->LOCK_delete);
2609
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2610
  return(error);
1 by brian
clean slate
2611
}
2612
2613
2614
/*
2615
  kills a thread and sends response
2616
2617
  SYNOPSIS
2618
    sql_kill()
520.1.22 by Brian Aker
Second pass of thd cleanup
2619
    session			Thread class
1 by brian
clean slate
2620
    id			Thread id
2621
    only_kill_query     Should it kill the query or the connection
2622
*/
2623
520.1.22 by Brian Aker
Second pass of thd cleanup
2624
void sql_kill(Session *session, ulong id, bool only_kill_query)
1 by brian
clean slate
2625
{
438.1.13 by Brian Aker
uint cleanup.
2626
  uint32_t error;
520.1.22 by Brian Aker
Second pass of thd cleanup
2627
  if (!(error= kill_one_thread(session, id, only_kill_query)))
836 by Brian Aker
Fixed session call from function to method.
2628
    session->my_ok();
1 by brian
clean slate
2629
  else
2630
    my_error(error, MYF(0), id);
2631
}
2632
2633
2634
/** If pointer is not a null pointer, append filename to it. */
2635
520.1.22 by Brian Aker
Second pass of thd cleanup
2636
bool append_file_to_dir(Session *session, const char **filename_ptr,
1 by brian
clean slate
2637
                        const char *table_name)
2638
{
2639
  char buff[FN_REFLEN],*ptr, *end;
2640
  if (!*filename_ptr)
2641
    return 0;					// nothing to do
2642
2643
  /* Check that the filename is not too long and it's a hard path */
2644
  if (strlen(*filename_ptr)+strlen(table_name) >= FN_REFLEN-1 ||
2645
      !test_if_hard_path(*filename_ptr))
2646
  {
2647
    my_error(ER_WRONG_TABLE_NAME, MYF(0), *filename_ptr);
2648
    return 1;
2649
  }
2650
  /* Fix is using unix filename format on dos */
641.4.3 by Toru Maesaka
Final pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2651
  strcpy(buff,*filename_ptr);
461 by Monty Taylor
Removed NullS. bu-bye.
2652
  end=convert_dirname(buff, *filename_ptr, NULL);
520.1.22 by Brian Aker
Second pass of thd cleanup
2653
  if (!(ptr= (char*) session->alloc((size_t) (end-buff) + strlen(table_name)+1)))
1 by brian
clean slate
2654
    return 1;					// End of memory
2655
  *filename_ptr=ptr;
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
2656
  sprintf(ptr,"%s%s",buff,table_name);
1 by brian
clean slate
2657
  return 0;
2658
}
2659
2660
2661
/**
2662
  Check if the select is a simple select (not an union).
2663
2664
  @retval
2665
    0	ok
2666
  @retval
2667
    1	error	; In this case the error messege is sent to the client
2668
*/
2669
2670
bool check_simple_select()
2671
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2672
  Session *session= current_session;
2673
  LEX *lex= session->lex;
1 by brian
clean slate
2674
  if (lex->current_select != &lex->select_lex)
2675
  {
2676
    char command[80];
520.1.22 by Brian Aker
Second pass of thd cleanup
2677
    Lex_input_stream *lip= session->m_lip;
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
2678
    strncpy(command, lip->yylval->symbol.str,
722.2.33 by Monty Taylor
Fixed a wanton lack of null termination.
2679
            cmin(lip->yylval->symbol.length, sizeof(command)-1));
2680
    command[cmin(lip->yylval->symbol.length, sizeof(command)-1)]=0;
1 by brian
clean slate
2681
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
2682
    return 1;
2683
  }
2684
  return 0;
2685
}
2686
2687
2688
/**
2689
  Construct ALL/ANY/SOME subquery Item.
2690
2691
  @param left_expr   pointer to left expression
2692
  @param cmp         compare function creator
2693
  @param all         true if we create ALL subquery
2694
  @param select_lex  pointer on parsed subquery structure
2695
2696
  @return
2697
    constructed Item (or 0 if out of memory)
2698
*/
2699
Item * all_any_subquery_creator(Item *left_expr,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
2700
                                chooser_compare_func_creator cmp,
2701
                                bool all,
846 by Brian Aker
Removing on typedeffed class.
2702
                                Select_Lex *select_lex)
1 by brian
clean slate
2703
{
2704
  if ((cmp == &comp_eq_creator) && !all)       //  = ANY <=> IN
2705
    return new Item_in_subselect(left_expr, select_lex);
2706
2707
  if ((cmp == &comp_ne_creator) && all)        // <> ALL <=> NOT IN
2708
    return new Item_func_not(new Item_in_subselect(left_expr, select_lex));
2709
2710
  Item_allany_subselect *it=
2711
    new Item_allany_subselect(left_expr, cmp, select_lex, all);
2712
  if (all)
2713
    return it->upper_item= new Item_func_not_all(it);	/* ALL */
2714
2715
  return it->upper_item= new Item_func_nop_all(it);      /* ANY/SOME */
2716
}
2717
2718
2719
/**
826 by Brian Aker
Simplify update
2720
  Update query pre-check.
1 by brian
clean slate
2721
520.1.22 by Brian Aker
Second pass of thd cleanup
2722
  @param session		Thread handler
1 by brian
clean slate
2723
  @param tables	Global/local table list (have to be the same)
2724
2725
  @retval
55 by brian
Update for using real bool types.
2726
    false OK
1 by brian
clean slate
2727
  @retval
55 by brian
Update for using real bool types.
2728
    true  Error
1 by brian
clean slate
2729
*/
2730
826 by Brian Aker
Simplify update
2731
bool update_precheck(Session *session, TableList *)
1 by brian
clean slate
2732
{
2733
  const char *msg= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
2734
  LEX *lex= session->lex;
846 by Brian Aker
Removing on typedeffed class.
2735
  Select_Lex *select_lex= &lex->select_lex;
1 by brian
clean slate
2736
826 by Brian Aker
Simplify update
2737
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1 by brian
clean slate
2738
  {
2739
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2740
    return(true);
1 by brian
clean slate
2741
  }
2742
826 by Brian Aker
Simplify update
2743
  if (session->lex->select_lex.table_list.elements > 1)
1 by brian
clean slate
2744
  {
826 by Brian Aker
Simplify update
2745
    if (select_lex->order_list.elements)
2746
      msg= "ORDER BY";
2747
    else if (select_lex->select_limit)
2748
      msg= "LIMIT";
2749
    if (msg)
2750
    {
2751
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
2752
      return(true);
2753
    }
1 by brian
clean slate
2754
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2755
  return(false);
1 by brian
clean slate
2756
}
2757
2758
/**
2759
  Multi delete query pre-check.
2760
520.1.22 by Brian Aker
Second pass of thd cleanup
2761
  @param session			Thread handler
1 by brian
clean slate
2762
  @param tables		Global/local table list
2763
2764
  @retval
55 by brian
Update for using real bool types.
2765
    false OK
1 by brian
clean slate
2766
  @retval
55 by brian
Update for using real bool types.
2767
    true  error
1 by brian
clean slate
2768
*/
2769
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
2770
bool multi_delete_precheck(Session *session, TableList *)
1 by brian
clean slate
2771
{
846 by Brian Aker
Removing on typedeffed class.
2772
  Select_Lex *select_lex= &session->lex->select_lex;
520.1.22 by Brian Aker
Second pass of thd cleanup
2773
  TableList **save_query_tables_own_last= session->lex->query_tables_own_last;
2774
2775
  session->lex->query_tables_own_last= 0;
2776
  session->lex->query_tables_own_last= save_query_tables_own_last;
2777
2778
  if ((session->options & OPTION_SAFE_UPDATES) && !select_lex->where)
1 by brian
clean slate
2779
  {
2780
    my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
2781
               ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2782
    return(true);
1 by brian
clean slate
2783
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2784
  return(false);
1 by brian
clean slate
2785
}
2786
2787
2788
/*
2789
  Given a table in the source list, find a correspondent table in the
2790
  table references list.
2791
2792
  @param lex Pointer to LEX representing multi-delete.
2793
  @param src Source table to match.
2794
  @param ref Table references list.
2795
2796
  @remark The source table list (tables listed before the FROM clause
2797
  or tables listed in the FROM clause before the USING clause) may
2798
  contain table names or aliases that must match unambiguously one,
2799
  and only one, table in the target table list (table references list,
2800
  after FROM/USING clause).
2801
2802
  @return Matching table, NULL otherwise.
2803
*/
2804
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
2805
static TableList *multi_delete_table_match(LEX *, TableList *tbl,
2806
                                           TableList *tables)
1 by brian
clean slate
2807
{
327.2.4 by Brian Aker
Refactoring table.h
2808
  TableList *match= NULL;
1 by brian
clean slate
2809
327.2.4 by Brian Aker
Refactoring table.h
2810
  for (TableList *elem= tables; elem; elem= elem->next_local)
1 by brian
clean slate
2811
  {
2812
    int cmp;
2813
2814
    if (tbl->is_fqtn && elem->is_alias)
2815
      continue; /* no match */
2816
    if (tbl->is_fqtn && elem->is_fqtn)
2817
      cmp= my_strcasecmp(table_alias_charset, tbl->table_name, elem->table_name) ||
2818
           strcmp(tbl->db, elem->db);
2819
    else if (elem->is_alias)
2820
      cmp= my_strcasecmp(table_alias_charset, tbl->alias, elem->alias);
2821
    else
2822
      cmp= my_strcasecmp(table_alias_charset, tbl->table_name, elem->table_name) ||
2823
           strcmp(tbl->db, elem->db);
2824
2825
    if (cmp)
2826
      continue;
2827
2828
    if (match)
2829
    {
2830
      my_error(ER_NONUNIQ_TABLE, MYF(0), elem->alias);
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2831
      return(NULL);
1 by brian
clean slate
2832
    }
2833
2834
    match= elem;
2835
  }
2836
2837
  if (!match)
2838
    my_error(ER_UNKNOWN_TABLE, MYF(0), tbl->table_name, "MULTI DELETE");
2839
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2840
  return(match);
1 by brian
clean slate
2841
}
2842
2843
2844
/**
2845
  Link tables in auxilary table list of multi-delete with corresponding
2846
  elements in main table list, and set proper locks for them.
2847
2848
  @param lex   pointer to LEX representing multi-delete
2849
2850
  @retval
55 by brian
Update for using real bool types.
2851
    false   success
1 by brian
clean slate
2852
  @retval
55 by brian
Update for using real bool types.
2853
    true    error
1 by brian
clean slate
2854
*/
2855
2856
bool multi_delete_set_locks_and_link_aux_tables(LEX *lex)
2857
{
327.2.4 by Brian Aker
Refactoring table.h
2858
  TableList *tables= (TableList*)lex->select_lex.table_list.first;
2859
  TableList *target_tbl;
1 by brian
clean slate
2860
2861
  lex->table_count= 0;
2862
327.2.4 by Brian Aker
Refactoring table.h
2863
  for (target_tbl= (TableList *)lex->auxiliary_table_list.first;
1 by brian
clean slate
2864
       target_tbl; target_tbl= target_tbl->next_local)
2865
  {
2866
    lex->table_count++;
2867
    /* All tables in aux_tables must be found in FROM PART */
327.2.4 by Brian Aker
Refactoring table.h
2868
    TableList *walk= multi_delete_table_match(lex, target_tbl, tables);
1 by brian
clean slate
2869
    if (!walk)
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2870
      return(true);
1 by brian
clean slate
2871
    if (!walk->derived)
2872
    {
2873
      target_tbl->table_name= walk->table_name;
2874
      target_tbl->table_name_length= walk->table_name_length;
2875
    }
2876
    walk->updating= target_tbl->updating;
2877
    walk->lock_type= target_tbl->lock_type;
2878
    target_tbl->correspondent_table= walk;	// Remember corresponding table
2879
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2880
  return(false);
1 by brian
clean slate
2881
}
2882
2883
2884
/**
2885
  simple INSERT query pre-check.
2886
520.1.22 by Brian Aker
Second pass of thd cleanup
2887
  @param session		Thread handler
1 by brian
clean slate
2888
  @param tables	Global table list
2889
2890
  @retval
55 by brian
Update for using real bool types.
2891
    false  OK
1 by brian
clean slate
2892
  @retval
55 by brian
Update for using real bool types.
2893
    true   error
1 by brian
clean slate
2894
*/
2895
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
2896
bool insert_precheck(Session *session, TableList *)
1 by brian
clean slate
2897
{
520.1.22 by Brian Aker
Second pass of thd cleanup
2898
  LEX *lex= session->lex;
1 by brian
clean slate
2899
2900
  /*
2901
    Check that we have modify privileges for the first table and
2902
    select privileges for the rest
2903
  */
2904
  if (lex->update_list.elements != lex->value_list.elements)
2905
  {
2906
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2907
    return(true);
1 by brian
clean slate
2908
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2909
  return(false);
1 by brian
clean slate
2910
}
2911
2912
2913
/**
2914
  CREATE TABLE query pre-check.
2915
520.1.22 by Brian Aker
Second pass of thd cleanup
2916
  @param session			Thread handler
1 by brian
clean slate
2917
  @param tables		Global table list
2918
  @param create_table	        Table which will be created
2919
2920
  @retval
55 by brian
Update for using real bool types.
2921
    false   OK
1 by brian
clean slate
2922
  @retval
55 by brian
Update for using real bool types.
2923
    true   Error
1 by brian
clean slate
2924
*/
2925
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
2926
bool create_table_precheck(Session *, TableList *,
327.2.4 by Brian Aker
Refactoring table.h
2927
                           TableList *create_table)
1 by brian
clean slate
2928
{
55 by brian
Update for using real bool types.
2929
  bool error= true;                                 // Error message is given
1 by brian
clean slate
2930
2931
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
2932
  {
575.4.7 by Monty Taylor
More header cleanup.
2933
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", INFORMATION_SCHEMA_NAME.c_str());
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2934
    return(true);
1 by brian
clean slate
2935
  }
2936
55 by brian
Update for using real bool types.
2937
  error= false;
1 by brian
clean slate
2938
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
2939
  return(error);
1 by brian
clean slate
2940
}
2941
2942
2943
/**
2944
  negate given expression.
2945
520.1.22 by Brian Aker
Second pass of thd cleanup
2946
  @param session  thread handler
1 by brian
clean slate
2947
  @param expr expression for negation
2948
2949
  @return
2950
    negated expression
2951
*/
2952
520.1.22 by Brian Aker
Second pass of thd cleanup
2953
Item *negate_expression(Session *session, Item *expr)
1 by brian
clean slate
2954
{
2955
  Item *negated;
2956
  if (expr->type() == Item::FUNC_ITEM &&
2957
      ((Item_func *) expr)->functype() == Item_func::NOT_FUNC)
2958
  {
2959
    /* it is NOT(NOT( ... )) */
2960
    Item *arg= ((Item_func *) expr)->arguments()[0];
520.1.22 by Brian Aker
Second pass of thd cleanup
2961
    enum_parsing_place place= session->lex->current_select->parsing_place;
1 by brian
clean slate
2962
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
2963
      return arg;
2964
    /*
2965
      if it is not boolean function then we have to emulate value of
2966
      not(not(a)), it will be a != 0
2967
    */
2968
    return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
2969
  }
2970
520.1.22 by Brian Aker
Second pass of thd cleanup
2971
  if ((negated= expr->neg_transformer(session)) != 0)
1 by brian
clean slate
2972
    return negated;
2973
  return new Item_func_not(expr);
2974
}
2975
2976
2977
/*
2978
  Check that char length of a string does not exceed some limit.
2979
2980
  SYNOPSIS
2981
  check_string_char_length()
2982
      str              string to be checked
2983
      err_msg          error message to be displayed if the string is too long
2984
      max_char_length  max length in symbols
2985
      cs               string charset
2986
2987
  RETURN
55 by brian
Update for using real bool types.
2988
    false   the passed string is not longer than max_char_length
2989
    true    the passed string is longer than max_char_length
1 by brian
clean slate
2990
*/
2991
2992
2993
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
438.1.13 by Brian Aker
uint cleanup.
2994
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
1 by brian
clean slate
2995
                              bool no_error)
2996
{
2997
  int well_formed_error;
438.1.13 by Brian Aker
uint cleanup.
2998
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1 by brian
clean slate
2999
                                      max_char_length, &well_formed_error);
3000
3001
  if (!well_formed_error &&  str->length == res)
55 by brian
Update for using real bool types.
3002
    return false;
1 by brian
clean slate
3003
3004
  if (!no_error)
3005
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_char_length);
55 by brian
Update for using real bool types.
3006
  return true;
1 by brian
clean slate
3007
}
3008
3009
575.4.7 by Monty Taylor
More header cleanup.
3010
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
3011
                           uint32_t max_char_length,
3012
                           const char *param_for_err_msg)
1 by brian
clean slate
3013
{
3014
  /*
3015
    We don't support non-BMP characters in identifiers at the moment,
3016
    so they should be prohibited until such support is done.
3017
    This is why we use the 3-byte utf8 to check well-formedness here.
3018
  */
760 by Brian Aker
Cleanup around UTf8 code.
3019
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
3020
1 by brian
clean slate
3021
  int well_formed_error;
438.1.13 by Brian Aker
uint cleanup.
3022
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1 by brian
clean slate
3023
                                      max_char_length, &well_formed_error);
3024
3025
  if (well_formed_error)
3026
  {
3027
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
55 by brian
Update for using real bool types.
3028
    return true;
1 by brian
clean slate
3029
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3030
1 by brian
clean slate
3031
  if (str->length == res)
55 by brian
Update for using real bool types.
3032
    return false;
1 by brian
clean slate
3033
3034
  switch (err_code)
3035
  {
3036
  case 0:
3037
    break;
3038
  case ER_WRONG_STRING_LENGTH:
3039
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
3040
    break;
3041
  case ER_TOO_LONG_IDENT:
3042
    my_error(err_code, MYF(0), str->str);
3043
    break;
3044
  default:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
3045
    assert(0);
1 by brian
clean slate
3046
    break;
3047
  }
55 by brian
Update for using real bool types.
3048
  return true;
1 by brian
clean slate
3049
}
3050
3051
3052
/*
3053
  Check if path does not contain mysql data home directory
3054
  SYNOPSIS
3055
    test_if_data_home_dir()
3056
    dir                     directory
3057
    conv_home_dir           converted data home directory
3058
    home_dir_len            converted data home directory length
3059
3060
  RETURN VALUES
3061
    0	ok
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3062
    1	error
1 by brian
clean slate
3063
*/
3064
3065
bool test_if_data_home_dir(const char *dir)
3066
{
3067
  char path[FN_REFLEN], conv_path[FN_REFLEN];
575.4.1 by ysano
Rename mysql to drizzle.
3068
  uint32_t dir_len, home_dir_len= strlen(drizzle_unpacked_real_data_home);
1 by brian
clean slate
3069
3070
  if (!dir)
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
3071
    return(0);
1 by brian
clean slate
3072
3073
  (void) fn_format(path, dir, "", "",
3074
                   (MY_RETURN_REAL_PATH|MY_RESOLVE_SYMLINKS));
3075
  dir_len= unpack_dirname(conv_path, dir);
3076
3077
  if (home_dir_len < dir_len)
3078
  {
224.2.2 by Brian Aker
Second pass cleanup around filesystem type.
3079
    if (!my_strnncoll(character_set_filesystem,
481 by Brian Aker
Remove all of uchar.
3080
                      (const unsigned char*) conv_path, home_dir_len,
575.4.1 by ysano
Rename mysql to drizzle.
3081
                      (const unsigned char*) drizzle_unpacked_real_data_home,
224.2.2 by Brian Aker
Second pass cleanup around filesystem type.
3082
                      home_dir_len))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
3083
      return(1);
1 by brian
clean slate
3084
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
3085
  return(0);
1 by brian
clean slate
3086
}
3087
3088
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
3089
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
1 by brian
clean slate
3090
3091
3092
/**
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
3093
  This is a wrapper of DRIZZLEparse(). All the code should call parse_sql()
3094
  instead of DRIZZLEparse().
1 by brian
clean slate
3095
520.1.22 by Brian Aker
Second pass of thd cleanup
3096
  @param session Thread context.
1 by brian
clean slate
3097
  @param lip Lexer context.
3098
3099
  @return Error status.
55 by brian
Update for using real bool types.
3100
    @retval false on success.
3101
    @retval true on parsing error.
1 by brian
clean slate
3102
*/
3103
520.1.22 by Brian Aker
Second pass of thd cleanup
3104
bool parse_sql(Session *session, Lex_input_stream *lip)
1 by brian
clean slate
3105
{
520.1.22 by Brian Aker
Second pass of thd cleanup
3106
  assert(session->m_lip == NULL);
1 by brian
clean slate
3107
3108
  /* Set Lex_input_stream. */
3109
520.1.22 by Brian Aker
Second pass of thd cleanup
3110
  session->m_lip= lip;
1 by brian
clean slate
3111
3112
  /* Parse the query. */
3113
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
3114
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
3115
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
3116
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
520.1.22 by Brian Aker
Second pass of thd cleanup
3117
3118
  assert(!mysql_parse_status || session->is_error());
1 by brian
clean slate
3119
3120
  /* Reset Lex_input_stream. */
3121
520.1.22 by Brian Aker
Second pass of thd cleanup
3122
  session->m_lip= NULL;
1 by brian
clean slate
3123
3124
  /* That's it. */
3125
520.1.22 by Brian Aker
Second pass of thd cleanup
3126
  return mysql_parse_status || session->is_fatal_error;
1 by brian
clean slate
3127
}
3128
3129
/**
3130
  @} (end of group Runtime_Environment)
3131
*/