~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>
575.4.6 by Monty Taylor
Removed my_getwd.
19
#include <drizzled/db.h>
549 by Monty Taylor
Took gettext.h out of header files.
20
#include <drizzled/error.h>
553.1.3 by Monty Taylor
Split out nested_join.h.
21
#include <drizzled/nested_join.h>
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
22
#include <drizzled/query_id.h>
520.8.2 by Monty Taylor
Moved sql_parse.h and sql_error.h out of common_includes.
23
#include <drizzled/sql_parse.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
24
#include <drizzled/data_home.h>
575.4.7 by Monty Taylor
More header cleanup.
25
#include <drizzled/sql_base.h>
26
#include <drizzled/show.h>
1130.1.7 by Monty Taylor
Renamed plugin::InfoSchema to plugin::InfoSchemaTable as per Jay.
27
#include <drizzled/plugin/info_schema_table.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
28
#include <drizzled/function/time/unix_timestamp.h>
29
#include <drizzled/function/get_system_var.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
30
#include <drizzled/item/cmpfunc.h>
642.1.20 by Lee
header file clean up
31
#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.
32
#include <drizzled/session.h>
33
#include <drizzled/sql_load.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
34
#include <drizzled/lock.h>
837 by Brian Aker
Reworked some classes out of session.h
35
#include <drizzled/select_send.h>
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
36
#include <drizzled/plugin/client.h>
1100.3.30 by Padraig O'Sullivan
Renamed the Command class to be Statement. Renamed the command directory to
37
#include <drizzled/statement.h>
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
38
#include <drizzled/statement/alter_table.h>
1126.10.18 by Padraig O'Sullivan
Various small build fixes for when dtrace is enabled.
39
#include "drizzled/probes.h"
1100.3.1 by Padraig O'Sullivan
Beginnings of reworking the mysql_execute_command() method.
40
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
41
#include "drizzled/plugin/logging.h"
42
#include "drizzled/plugin/info_schema_table.h"
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
43
590.2.18 by Monty Taylor
Changed the bitset<32> to a bitset<5>. Made the int constants into bitset constants.
44
#include <bitset>
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
45
#include <algorithm>
590.2.18 by Monty Taylor
Changed the bitset<32> to a bitset<5>. Made the int constants into bitset constants.
46
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
47
using namespace drizzled;
590.2.18 by Monty Taylor
Changed the bitset<32> to a bitset<5>. Made the int constants into bitset constants.
48
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.
49
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
50
/* Prototypes */
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
51
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
1165.1.48 by Stewart Smith
make parse_sql static to sql_parse.cc
52
static bool parse_sql(Session *session, Lex_input_stream *lip);
1165.1.49 by Stewart Smith
make mysql_parse static to sql_parse.cc
53
static void mysql_parse(Session *session, const char *inBuf, uint32_t length,
54
                 const char ** found_semicolon);
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
55
1 by brian
clean slate
56
/**
57
  @defgroup Runtime_Environment Runtime Environment
58
  @{
59
*/
60
629.2.7 by Monty Taylor
Fixed a couple of memory buffer size issues.
61
extern size_t my_thread_stack_size;
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
62
extern const CHARSET_INFO *character_set_filesystem;
1 by brian
clean slate
63
const char *any_db="*any*";	// Special symbol for check_access
64
230.1.8 by Monty Taylor
Set length to catch errors in adding or removing COM_* constants.
65
const LEX_STRING command_name[COM_END+1]={
1 by brian
clean slate
66
  { C_STRING_WITH_LEN("Sleep") },
67
  { C_STRING_WITH_LEN("Quit") },
68
  { C_STRING_WITH_LEN("Init DB") },
69
  { C_STRING_WITH_LEN("Query") },
70
  { C_STRING_WITH_LEN("Shutdown") },
71
  { C_STRING_WITH_LEN("Connect") },
72
  { C_STRING_WITH_LEN("Ping") },
73
  { C_STRING_WITH_LEN("Error") }  // Last command number
74
};
75
76
const char *xa_state_names[]={
77
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
78
};
79
80
/**
81
  Mark all commands that somehow changes a table.
82
83
  This is used to check number of updates / hour.
84
85
  sql_command is actually set to SQLCOM_END sometimes
86
  so we need the +1 to include it in the array.
87
88
  See COMMAND_FLAG_xxx for different type of commands
89
     2  - query that returns meaningful ROW_COUNT() -
90
          a number of modified rows
91
*/
590.2.18 by Monty Taylor
Changed the bitset<32> to a bitset<5>. Made the int constants into bitset constants.
92
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
1 by brian
clean slate
93
94
void init_update_queries(void)
95
{
610 by Brian Aker
Merge of Monty's (plus fix for increment style/useless +1)
96
  uint32_t x;
97
98
  for (x= 0; x <= SQLCOM_END; x++)
99
    sql_command_flags[x].reset();
1 by brian
clean slate
100
101
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
102
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
103
  sql_command_flags[SQLCOM_ALTER_TABLE]=    CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND;
104
  sql_command_flags[SQLCOM_TRUNCATE]=       CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND;
105
  sql_command_flags[SQLCOM_DROP_TABLE]=     CF_CHANGES_DATA;
106
  sql_command_flags[SQLCOM_LOAD]=           CF_CHANGES_DATA;
107
  sql_command_flags[SQLCOM_CREATE_DB]=      CF_CHANGES_DATA;
108
  sql_command_flags[SQLCOM_DROP_DB]=        CF_CHANGES_DATA;
109
  sql_command_flags[SQLCOM_RENAME_TABLE]=   CF_CHANGES_DATA;
110
  sql_command_flags[SQLCOM_DROP_INDEX]=     CF_CHANGES_DATA;
111
112
  sql_command_flags[SQLCOM_UPDATE]=	    CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
113
  sql_command_flags[SQLCOM_INSERT]=	    CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
114
  sql_command_flags[SQLCOM_INSERT_SELECT]=  CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
115
  sql_command_flags[SQLCOM_DELETE]=         CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
116
  sql_command_flags[SQLCOM_REPLACE]=        CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
117
  sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_HAS_ROW_COUNT;
118
119
  sql_command_flags[SQLCOM_SHOW_STATUS]=      CF_STATUS_COMMAND;
120
  sql_command_flags[SQLCOM_SHOW_DATABASES]=   CF_STATUS_COMMAND;
121
  sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND;
122
  sql_command_flags[SQLCOM_SHOW_FIELDS]=      CF_STATUS_COMMAND;
123
  sql_command_flags[SQLCOM_SHOW_KEYS]=        CF_STATUS_COMMAND;
124
  sql_command_flags[SQLCOM_SHOW_VARIABLES]=   CF_STATUS_COMMAND;
125
  sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND;
126
  sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND;
127
  sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
128
  sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
129
  sql_command_flags[SQLCOM_SHOW_CREATE_DB]=  CF_STATUS_COMMAND;
130
  sql_command_flags[SQLCOM_SHOW_CREATE]=  CF_STATUS_COMMAND;
131
132
   sql_command_flags[SQLCOM_SHOW_TABLES]=       (CF_STATUS_COMMAND |
133
                                               CF_SHOW_TABLE_COMMAND);
134
  sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
135
                                                CF_SHOW_TABLE_COMMAND);
136
  /*
137
    The following admin table operations are allowed
138
    on log tables.
139
  */
140
  sql_command_flags[SQLCOM_OPTIMIZE]=         CF_WRITE_LOGS_COMMAND;
141
  sql_command_flags[SQLCOM_ANALYZE]=          CF_WRITE_LOGS_COMMAND;
142
}
143
144
/**
145
  Perform one connection-level (COM_XXXX) command.
146
147
  @param command         type of command to perform
520.1.22 by Brian Aker
Second pass of thd cleanup
148
  @param session             connection handle
1 by brian
clean slate
149
  @param packet          data for the command, packet is always null-terminated
150
  @param packet_length   length of packet + 1 (to show that data is
151
                         null-terminated) except for COM_SLEEP, where it
152
                         can be zero.
153
154
  @todo
520.1.22 by Brian Aker
Second pass of thd cleanup
155
    set session->lex->sql_command to SQLCOM_END here.
1 by brian
clean slate
156
  @todo
157
    The following has to be changed to an 8 byte integer
158
159
  @retval
160
    0   ok
161
  @retval
162
    1   request of thread shutdown, i. e. if command is
163
        COM_QUIT/COM_SHUTDOWN
164
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
165
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.
166
                      char* packet, uint32_t packet_length)
1 by brian
clean slate
167
{
168
  bool error= 0;
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
169
  Query_id &query_id= Query_id::get_query_id();
1 by brian
clean slate
170
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
171
  DRIZZLE_COMMAND_START(session->thread_id,
172
                        command);
173
174
  session->command= command;
520.1.22 by Brian Aker
Second pass of thd cleanup
175
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
176
  session->set_time();
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
177
  session->query_id= query_id.value();
1 by brian
clean slate
178
179
  switch( command ) {
180
  /* Ignore these statements. */
181
  case COM_PING:
182
    break;
183
  /* Increase id and count all other statements. */
184
  default:
520.1.22 by Brian Aker
Second pass of thd cleanup
185
    statistic_increment(session->status_var.questions, &LOCK_status);
561.1.3 by Monty Taylor
Split some more things out of common_includes.h.
186
    query_id.next();
1 by brian
clean slate
187
  }
188
520.1.22 by Brian Aker
Second pass of thd cleanup
189
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
1 by brian
clean slate
190
1130.1.16 by Monty Taylor
Fixed naming issue.
191
  plugin::Logging::preDo(session);
383.6.4 by Mark Atwood
more make plug logging work
192
520.1.22 by Brian Aker
Second pass of thd cleanup
193
  session->server_status&=
1 by brian
clean slate
194
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
195
  switch (command) {
196
  case COM_INIT_DB:
197
  {
198
    LEX_STRING tmp;
520.1.22 by Brian Aker
Second pass of thd cleanup
199
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
1054.2.11 by Monty Taylor
Removed copy_and_convert.
200
    tmp.str= packet;
201
    tmp.length= packet_length;
520.1.22 by Brian Aker
Second pass of thd cleanup
202
    if (!mysql_change_db(session, &tmp, false))
1 by brian
clean slate
203
    {
836 by Brian Aker
Fixed session call from function to method.
204
      session->my_ok();
1 by brian
clean slate
205
    }
206
    break;
207
  }
208
  case COM_QUERY:
209
  {
934.2.9 by Jay Pipes
Pulls alloc_query() C function out of sql_parse.cc and adds readAndStoreQuery() member method of Session class.
210
    if (! session->readAndStoreQuery(packet, packet_length))
1 by brian
clean slate
211
      break;					// fatal error is set
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
212
    DRIZZLE_QUERY_START(session->query,
213
                        session->thread_id,
1126.10.25 by Padraig O'Sullivan
Updated calls to some dtrace probes to cast the parameter to const char *
214
                        const_cast<const char *>(session->db ? session->db : ""));
1 by brian
clean slate
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 */
520.1.22 by Brian Aker
Second pass of thd cleanup
223
    session->main_da.disable_status();              // Don't send anything back
55 by brian
Update for using real bool types.
224
    error=true;					// End server
1 by brian
clean slate
225
    break;
226
  case COM_SHUTDOWN:
227
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
228
    status_var_increment(session->status_var.com_other);
836 by Brian Aker
Fixed session call from function to method.
229
    session->my_eof();
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
230
    session->close_thread_tables();			// Free before kill
575.4.4 by Yoshinori Sano
Rename mysql to drizzle.
231
    kill_drizzle();
55 by brian
Update for using real bool types.
232
    error=true;
1 by brian
clean slate
233
    break;
234
  }
235
  case COM_PING:
520.1.22 by Brian Aker
Second pass of thd cleanup
236
    status_var_increment(session->status_var.com_other);
836 by Brian Aker
Fixed session call from function to method.
237
    session->my_ok();				// Tell client we are alive
1 by brian
clean slate
238
    break;
239
  case COM_SLEEP:
240
  case COM_CONNECT:				// Impossible here
241
  case COM_END:
242
  default:
243
    my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
244
    break;
245
  }
246
247
  /* If commit fails, we should be able to reset the OK status. */
520.1.22 by Brian Aker
Second pass of thd cleanup
248
  session->main_da.can_overwrite_status= true;
249
  ha_autocommit_or_rollback(session, session->is_error());
250
  session->main_da.can_overwrite_status= false;
1 by brian
clean slate
251
520.1.22 by Brian Aker
Second pass of thd cleanup
252
  session->transaction.stmt.reset();
1 by brian
clean slate
253
254
255
  /* report error issued during command execution */
520.1.22 by Brian Aker
Second pass of thd cleanup
256
  if (session->killed_errno())
257
  {
258
    if (! session->main_da.is_set())
259
      session->send_kill_message();
260
  }
261
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
262
  {
263
    session->killed= Session::NOT_KILLED;
264
    session->mysys_var->abort= 0;
265
  }
266
971.3.12 by Eric Day
Started abstracting Protocol, removed init_connect, init_file.
267
  /* Can not be true, but do not take chances in production. */
268
  assert(! session->main_da.is_sent);
269
270
  switch (session->main_da.status())
271
  {
272
  case Diagnostics_area::DA_ERROR:
273
    /* The query failed, send error to log and abort bootstrap. */
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
274
    session->client->sendError(session->main_da.sql_errno(),
275
                               session->main_da.message());
971.3.12 by Eric Day
Started abstracting Protocol, removed init_connect, init_file.
276
    break;
277
278
  case Diagnostics_area::DA_EOF:
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
279
    session->client->sendEOF();
971.3.12 by Eric Day
Started abstracting Protocol, removed init_connect, init_file.
280
    break;
281
282
  case Diagnostics_area::DA_OK:
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
283
    session->client->sendOK();
971.3.12 by Eric Day
Started abstracting Protocol, removed init_connect, init_file.
284
    break;
285
286
  case Diagnostics_area::DA_DISABLED:
287
    break;
288
289
  case Diagnostics_area::DA_EMPTY:
290
  default:
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
291
    session->client->sendOK();
971.3.12 by Eric Day
Started abstracting Protocol, removed init_connect, init_file.
292
    break;
293
  }
294
295
  session->main_da.is_sent= true;
520.1.22 by Brian Aker
Second pass of thd cleanup
296
297
  session->set_proc_info("closing tables");
1 by brian
clean slate
298
  /* Free tables */
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
299
  session->close_thread_tables();
520.1.22 by Brian Aker
Second pass of thd cleanup
300
1130.1.16 by Monty Taylor
Fixed naming issue.
301
  plugin::Logging::postDo(session);
520.1.22 by Brian Aker
Second pass of thd cleanup
302
961.1.4 by Brian Aker
Remove another lock for processlist.
303
  /* Store temp state for processlist */
520.1.22 by Brian Aker
Second pass of thd cleanup
304
  session->set_proc_info("cleaning up");
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
305
  session->command= COM_SLEEP;
1003.1.13 by Brian Aker
Memset should be used for init (I seconded guessed myself in a patch about
306
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
307
  session->query= 0;
308
  session->query_length= 0;
961.1.4 by Brian Aker
Remove another lock for processlist.
309
310
  session->set_proc_info(NULL);
520.1.22 by Brian Aker
Second pass of thd cleanup
311
  free_root(session->mem_root,MYF(MY_KEEP_PREALLOC));
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
312
313
  if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
314
  {
315
    if (command == COM_QUERY)
316
    {
1126.10.21 by Padraig O'Sullivan
Whoops, simple fix for a build warning that happens if the build is not
317
      DRIZZLE_QUERY_DONE(session->is_error());
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
318
    }
1126.10.21 by Padraig O'Sullivan
Whoops, simple fix for a build warning that happens if the build is not
319
    DRIZZLE_COMMAND_DONE(session->is_error());
1126.10.2 by Padraig O'Sullivan
Added hooks into the command start and query start dtrace probes.
320
  }
321
322
  return error;
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
1079.2.9 by Padraig O'Sullivan
Removed the dependency on knowing the position of an I_S table in the
334
  @param session           thread handle
335
  @param lex               current lex
336
  @param table_ident       table alias if it's used
337
  @param schema_table_name the name of the INFORMATION_SCHEMA table to be
338
                           created
1 by brian
clean slate
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,
1079.2.9 by Padraig O'Sullivan
Removed the dependency on knowing the position of an I_S table in the
353
                         const string& schema_table_name)
1 by brian
clean slate
354
{
846 by Brian Aker
Removing on typedeffed class.
355
  Select_Lex *schema_select_lex= NULL;
1 by brian
clean slate
356
1079.2.9 by Padraig O'Sullivan
Removed the dependency on knowing the position of an I_S table in the
357
358
  if (schema_table_name.compare("TABLES") == 0 ||
359
      schema_table_name.compare("TABLE_NAMES") == 0)
360
  {
361
    LEX_STRING db;
362
    size_t dummy;
363
    if (lex->select_lex.db == NULL &&
364
        lex->copy_db_to(&lex->select_lex.db, &dummy))
365
    {
366
      return (1);
367
    }
368
    schema_select_lex= new Select_Lex();
369
    db.str= schema_select_lex->db= lex->select_lex.db;
370
    schema_select_lex->table_list.first= NULL;
371
    db.length= strlen(db.str);
372
373
    if (check_db_name(&db))
374
    {
375
      my_error(ER_WRONG_DB_NAME, MYF(0), db.str);
376
      return (1);
377
    }
378
  }
379
  else if (schema_table_name.compare("COLUMNS") == 0 ||
380
           schema_table_name.compare("STATISTICS") == 0)
1 by brian
clean slate
381
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
382
    assert(table_ident);
327.2.4 by Brian Aker
Refactoring table.h
383
    TableList **query_tables_last= lex->query_tables_last;
846 by Brian Aker
Removing on typedeffed class.
384
    schema_select_lex= new Select_Lex();
1 by brian
clean slate
385
    /* 'parent_lex' is used in init_query() so it must be before it. */
386
    schema_select_lex->parent_lex= lex;
387
    schema_select_lex->init_query();
1079.2.9 by Padraig O'Sullivan
Removed the dependency on knowing the position of an I_S table in the
388
    if (! schema_select_lex->add_table_to_list(session, table_ident, 0, 0, TL_READ))
389
    {
390
      return (1);
391
    }
1 by brian
clean slate
392
    lex->query_tables_last= query_tables_last;
393
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
394
846 by Brian Aker
Removing on typedeffed class.
395
  Select_Lex *select_lex= lex->current_select;
1 by brian
clean slate
396
  assert(select_lex);
1079.2.9 by Padraig O'Sullivan
Removed the dependency on knowing the position of an I_S table in the
397
  if (make_schema_select(session, select_lex, schema_table_name))
1 by brian
clean slate
398
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
399
    return(1);
1 by brian
clean slate
400
  }
327.2.4 by Brian Aker
Refactoring table.h
401
  TableList *table_list= (TableList*) select_lex->table_list.first;
1 by brian
clean slate
402
  assert(table_list);
403
  table_list->schema_select_lex= schema_select_lex;
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
404
405
  return 0;
1 by brian
clean slate
406
}
407
408
/**
520.1.22 by Brian Aker
Second pass of thd cleanup
409
  Execute command saved in session and lex->sql_command.
1 by brian
clean slate
410
411
    Before every operation that can request a write lock for a table
412
    wait if a global read lock exists. However do not wait if this
413
    thread has locked tables already. No new locks can be requested
414
    until the other locks are released. The thread that requests the
415
    global read lock waits for write locked tables to become unlocked.
416
417
    Note that wait_if_global_read_lock() sets a protection against a new
418
    global read lock when it succeeds. This needs to be released by
419
    start_waiting_global_read_lock() after the operation.
420
520.1.22 by Brian Aker
Second pass of thd cleanup
421
  @param session                       Thread handle
1 by brian
clean slate
422
423
  @todo
424
    - Invalidate the table in the query cache if something changed
425
    after unlocking when changes become visible.
426
    TODO: this is workaround. right way will be move invalidating in
427
    the unlock procedure.
428
    - TODO: use check_change_password()
429
    - JOIN is not supported yet. TODO
430
    - SUSPEND and FOR MIGRATE are not supported yet. TODO
431
432
  @retval
55 by brian
Update for using real bool types.
433
    false       OK
1 by brian
clean slate
434
  @retval
55 by brian
Update for using real bool types.
435
    true        Error
1 by brian
clean slate
436
*/
437
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
438
static int
520.1.22 by Brian Aker
Second pass of thd cleanup
439
mysql_execute_command(Session *session)
1 by brian
clean slate
440
{
1100.3.68 by Padraig O'Sullivan
Changed the type of res in mysql_execute_command() to bool instead of int
441
  bool res= false;
520.1.22 by Brian Aker
Second pass of thd cleanup
442
  LEX  *lex= session->lex;
846 by Brian Aker
Removing on typedeffed class.
443
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
444
  Select_Lex *select_lex= &lex->select_lex;
1 by brian
clean slate
445
  /* list of all tables in query */
327.2.4 by Brian Aker
Refactoring table.h
446
  TableList *all_tables;
998 by Brian Aker
Patch on show processlist from davi@apache.org
447
  /* A peek into the query string */
448
  size_t proc_info_len= session->query_length > PROCESS_LIST_WIDTH ?
449
                        PROCESS_LIST_WIDTH : session->query_length;
450
451
  memcpy(session->process_list_info, session->query, proc_info_len);
452
  session->process_list_info[proc_info_len]= '\0';
1 by brian
clean slate
453
454
  /*
846 by Brian Aker
Removing on typedeffed class.
455
    In many cases first table of main Select_Lex have special meaning =>
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
456
    check that it is first table in global list and relink it first in
1 by brian
clean slate
457
    queries_tables list if it is necessary (we need such relinking only
458
    for queries with subqueries in select list, in this case tables of
459
    subqueries will go to global list first)
460
846 by Brian Aker
Removing on typedeffed class.
461
    all_tables will differ from first_table only if most upper Select_Lex
1 by brian
clean slate
462
    do not contain tables.
463
464
    Because of above in place where should be at least one table in most
846 by Brian Aker
Removing on typedeffed class.
465
    outer Select_Lex we have following check:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
466
    assert(first_table == all_tables);
467
    assert(first_table == all_tables && first_table != 0);
1 by brian
clean slate
468
  */
469
  lex->first_lists_tables_same();
470
  /* should be assigned after making first tables same */
471
  all_tables= lex->query_tables;
472
  /* set context for commands which do not use setup_tables */
473
  select_lex->
327.2.4 by Brian Aker
Refactoring table.h
474
    context.resolve_in_table_list_only((TableList*)select_lex->
1 by brian
clean slate
475
                                       table_list.first);
476
477
  /*
478
    Reset warning count for each query that uses tables
479
    A better approach would be to reset this for any commands
480
    that is not a SHOW command or a select that only access local
481
    variables, but for now this is probably good enough.
482
    Don't reset warnings when executing a stored routine.
483
  */
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
484
  if (all_tables || ! lex->is_single_level_stmt())
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
485
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
486
    drizzle_reset_errors(session, 0);
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
487
  }
1 by brian
clean slate
488
520.1.22 by Brian Aker
Second pass of thd cleanup
489
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
1 by brian
clean slate
490
520.1.22 by Brian Aker
Second pass of thd cleanup
491
  assert(session->transaction.stmt.modified_non_trans_table == false);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
492
1100.3.66 by Padraig O'Sullivan
Extracted the last commands....Finally removed that gigantic switch
493
  /* now we are ready to execute the statement */
494
  res= lex->statement->execute();
1100.3.1 by Padraig O'Sullivan
Beginnings of reworking the mysql_execute_command() method.
495
520.1.22 by Brian Aker
Second pass of thd cleanup
496
  session->set_proc_info("query end");
1 by brian
clean slate
497
498
  /*
499
    The return value for ROW_COUNT() is "implementation dependent" if the
500
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
501
    wants. We also keep the last value in case of SQLCOM_CALL or
502
    SQLCOM_EXECUTE.
503
  */
1100.3.49 by Padraig O'Sullivan
Extracted the KILL command into its own class and implementation files.
504
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
505
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
506
    session->row_count_func= -1;
1100.3.49 by Padraig O'Sullivan
Extracted the KILL command into its own class and implementation files.
507
  }
1 by brian
clean slate
508
1100.3.63 by Padraig O'Sullivan
Extracted the ALTER TABLE command into its own class and implementation
509
  return (res || session->is_error());
1 by brian
clean slate
510
}
511
520.1.22 by Brian Aker
Second pass of thd cleanup
512
bool execute_sqlcom_select(Session *session, TableList *all_tables)
1 by brian
clean slate
513
{
520.1.22 by Brian Aker
Second pass of thd cleanup
514
  LEX	*lex= session->lex;
1 by brian
clean slate
515
  select_result *result=lex->result;
873.1.9 by Jay Pipes
This patch fixes the following functions to properly error out
516
  bool res= false;
1 by brian
clean slate
517
  /* assign global limit variable if limit is not given */
518
  {
846 by Brian Aker
Removing on typedeffed class.
519
    Select_Lex *param= lex->unit.global_parameters;
1 by brian
clean slate
520
    if (!param->explicit_limit)
521
      param->select_limit=
520.1.22 by Brian Aker
Second pass of thd cleanup
522
        new Item_int((uint64_t) session->variables.select_limit);
1 by brian
clean slate
523
  }
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
524
  if (!(res= session->openTablesLock(all_tables)))
1 by brian
clean slate
525
  {
526
    if (lex->describe)
527
    {
528
      /*
529
        We always use select_send for EXPLAIN, even if it's an EXPLAIN
530
        for SELECT ... INTO OUTFILE: a user application should be able
531
        to prepend EXPLAIN to any query and receive output for it,
532
        even if the query itself redirects the output.
533
      */
534
      if (!(result= new select_send()))
971.6.11 by Eric Day
Removed purecov messages.
535
        return true;
520.1.22 by Brian Aker
Second pass of thd cleanup
536
      session->send_explain_fields(result);
537
      res= mysql_explain_union(session, &session->lex->unit, result);
1 by brian
clean slate
538
      if (lex->describe & DESCRIBE_EXTENDED)
539
      {
540
        char buff[1024];
205 by Brian Aker
uint32 -> uin32_t
541
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
1 by brian
clean slate
542
        str.length(0);
520.1.22 by Brian Aker
Second pass of thd cleanup
543
        session->lex->unit.print(&str, QT_ORDINARY);
1 by brian
clean slate
544
        str.append('\0');
520.1.22 by Brian Aker
Second pass of thd cleanup
545
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
546
                     ER_YES, str.ptr());
547
      }
548
      if (res)
549
        result->abort();
550
      else
551
        result->send_eof();
552
      delete result;
553
    }
554
    else
555
    {
556
      if (!result && !(result= new select_send()))
971.6.11 by Eric Day
Removed purecov messages.
557
        return true;
520.1.22 by Brian Aker
Second pass of thd cleanup
558
      res= handle_select(session, lex, result, 0);
1 by brian
clean slate
559
      if (result != lex->result)
560
        delete result;
561
    }
562
  }
563
  return res;
564
}
565
566
567
#define MY_YACC_INIT 1000			// Start with big alloc
568
#define MY_YACC_MAX  32000			// Because of 'short'
569
570
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
571
{
520.1.22 by Brian Aker
Second pass of thd cleanup
572
  LEX	*lex= current_session->lex;
1 by brian
clean slate
573
  ulong old_info=0;
438.1.13 by Brian Aker
uint cleanup.
574
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
1 by brian
clean slate
575
    return 1;
576
  if (!lex->yacc_yyvs)
577
    old_info= *yystacksize;
578
  *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
656.1.46 by Monty Taylor
More malloc return cleanups.
579
  unsigned char *tmpptr= NULL;
580
  if (!(tmpptr= (unsigned char *)realloc(lex->yacc_yyvs,
581
                                         *yystacksize* sizeof(**yyvs))))
582
      return 1;
583
  lex->yacc_yyvs= tmpptr;
584
  tmpptr= NULL;
585
  if (!(tmpptr= (unsigned char*)realloc(lex->yacc_yyss,
586
                                        *yystacksize* sizeof(**yyss))))
587
      return 1;
588
  lex->yacc_yyss= tmpptr;
1 by brian
clean slate
589
  if (old_info)
590
  {						// Copy old info from stack
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
591
    memcpy(lex->yacc_yyss, *yyss, old_info*sizeof(**yyss));
592
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
1 by brian
clean slate
593
  }
594
  *yyss=(short*) lex->yacc_yyss;
595
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
596
  return 0;
597
}
598
599
600
void
601
mysql_init_select(LEX *lex)
602
{
846 by Brian Aker
Removing on typedeffed class.
603
  Select_Lex *select_lex= lex->current_select;
1 by brian
clean slate
604
  select_lex->init_select();
605
  lex->wild= 0;
606
  if (select_lex == &lex->select_lex)
607
  {
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
608
    assert(lex->result == 0);
1 by brian
clean slate
609
    lex->exchange= 0;
610
  }
611
}
612
613
614
bool
615
mysql_new_select(LEX *lex, bool move_down)
616
{
846 by Brian Aker
Removing on typedeffed class.
617
  Select_Lex *select_lex;
520.1.22 by Brian Aker
Second pass of thd cleanup
618
  Session *session= lex->session;
1 by brian
clean slate
619
846 by Brian Aker
Removing on typedeffed class.
620
  if (!(select_lex= new (session->mem_root) Select_Lex()))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
621
    return(1);
520.1.22 by Brian Aker
Second pass of thd cleanup
622
  select_lex->select_number= ++session->select_number;
1 by brian
clean slate
623
  select_lex->parent_lex= lex; /* Used in init_query. */
624
  select_lex->init_query();
625
  select_lex->init_select();
626
  lex->nest_level++;
627
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
628
  {
629
    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
630
    return(1);
1 by brian
clean slate
631
  }
632
  select_lex->nest_level= lex->nest_level;
633
  if (move_down)
634
  {
848 by Brian Aker
typdef class removal (just... use the name of the class).
635
    Select_Lex_Unit *unit;
1 by brian
clean slate
636
    /* first select_lex of subselect or derived table */
848 by Brian Aker
typdef class removal (just... use the name of the class).
637
    if (!(unit= new (session->mem_root) Select_Lex_Unit()))
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
638
      return(1);
1 by brian
clean slate
639
640
    unit->init_query();
641
    unit->init_select();
520.1.22 by Brian Aker
Second pass of thd cleanup
642
    unit->session= session;
1 by brian
clean slate
643
    unit->include_down(lex->current_select);
644
    unit->link_next= 0;
645
    unit->link_prev= 0;
646
    unit->return_to= lex->current_select;
647
    select_lex->include_down(unit);
648
    /*
649
      By default we assume that it is usual subselect and we have outer name
650
      resolution context, if no we will assign it to 0 later
651
    */
652
    select_lex->context.outer_context= &select_lex->outer_select()->context;
653
  }
654
  else
655
  {
656
    if (lex->current_select->order_list.first && !lex->current_select->braces)
657
    {
327.2.3 by Brian Aker
Refactoring of class Table
658
      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
659
      return(1);
1 by brian
clean slate
660
    }
661
    select_lex->include_neighbour(lex->current_select);
848 by Brian Aker
typdef class removal (just... use the name of the class).
662
    Select_Lex_Unit *unit= select_lex->master_unit();
520.1.22 by Brian Aker
Second pass of thd cleanup
663
    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
664
      return(1);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
665
    select_lex->context.outer_context=
1 by brian
clean slate
666
                unit->first_select()->context.outer_context;
667
  }
668
669
  select_lex->master_unit()->global_parameters= select_lex;
847 by Brian Aker
More typdef class removal.
670
  select_lex->include_global((Select_Lex_Node**)&lex->all_selects_list);
1 by brian
clean slate
671
  lex->current_select= select_lex;
672
  /*
673
    in subquery is SELECT query and we allow resolution of names in SELECT
674
    list
675
  */
55 by brian
Update for using real bool types.
676
  select_lex->context.resolve_in_select_list= true;
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
677
  return(0);
1 by brian
clean slate
678
}
679
680
/**
681
  Create a select to return the same output as 'SELECT @@var_name'.
682
683
  Used for SHOW COUNT(*) [ WARNINGS | ERROR].
684
685
  This will crash with a core dump if the variable doesn't exists.
686
687
  @param var_name		Variable name
688
*/
689
690
void create_select_for_variable(const char *var_name)
691
{
520.1.22 by Brian Aker
Second pass of thd cleanup
692
  Session *session;
1 by brian
clean slate
693
  LEX *lex;
694
  LEX_STRING tmp, null_lex_string;
695
  Item *var;
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
696
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
697
  char *end= buff;
1 by brian
clean slate
698
520.1.22 by Brian Aker
Second pass of thd cleanup
699
  session= current_session;
700
  lex= session->lex;
1 by brian
clean slate
701
  mysql_init_select(lex);
702
  lex->sql_command= SQLCOM_SELECT;
703
  tmp.str= (char*) var_name;
704
  tmp.length=strlen(var_name);
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
705
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
1 by brian
clean slate
706
  /*
707
    We set the name of Item to @@session.var_name because that then is used
708
    as the column name in the output.
709
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
710
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
1 by brian
clean slate
711
  {
673.2.1 by Toru Maesaka
First pass of replacing MySQL's strxmov with libc alternatives
712
    end+= sprintf(buff, "@@session.%s", var_name);
1 by brian
clean slate
713
    var->set_name(buff, end-buff, system_charset_info);
838 by Brian Aker
More class adoption/method
714
    session->add_item_to_list(var);
1 by brian
clean slate
715
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
716
  return;
1 by brian
clean slate
717
}
718
719
720
/**
721
  Parse a query.
722
520.1.22 by Brian Aker
Second pass of thd cleanup
723
  @param       session     Current thread
1 by brian
clean slate
724
  @param       inBuf   Begining of the query text
725
  @param       length  Length of the query text
726
  @param[out]  found_semicolon For multi queries, position of the character of
727
                               the next query in the query text.
728
*/
729
1165.1.49 by Stewart Smith
make mysql_parse static to sql_parse.cc
730
static void mysql_parse(Session *session, const char *inBuf, uint32_t length,
1 by brian
clean slate
731
                 const char ** found_semicolon)
732
{
733
  /*
734
    Warning.
735
    The purpose of query_cache_send_result_to_client() is to lookup the
736
    query in the query cache first, to avoid parsing and executing it.
737
    So, the natural implementation would be to:
738
    - first, call query_cache_send_result_to_client,
739
    - second, if caching failed, initialise the lexical and syntactic parser.
740
    The problem is that the query cache depends on a clean initialization
520.1.22 by Brian Aker
Second pass of thd cleanup
741
    of (among others) lex->safe_to_cache_query and session->server_status,
1 by brian
clean slate
742
    which are reset respectively in
743
    - lex_start()
520.1.22 by Brian Aker
Second pass of thd cleanup
744
    - mysql_reset_session_for_next_command()
1 by brian
clean slate
745
    So, initializing the lexical analyser *before* using the query cache
746
    is required for the cache to work properly.
747
    FIXME: cleanup the dependencies in the code to simplify this.
748
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
749
  lex_start(session);
735 by Brian Aker
Refactor session.
750
  session->reset_for_next_command();
1 by brian
clean slate
751
752
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
753
    LEX *lex= session->lex;
754
755
    Lex_input_stream lip(session, inBuf, length);
756
757
    bool err= parse_sql(session, &lip);
1 by brian
clean slate
758
    *found_semicolon= lip.found_semicolon;
759
760
    if (!err)
761
    {
762
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
763
	if (! session->is_error())
1 by brian
clean slate
764
	{
765
          /*
520.1.22 by Brian Aker
Second pass of thd cleanup
766
            Binlog logs a string starting from session->query and having length
767
            session->query_length; so we set session->query_length correctly (to not
1 by brian
clean slate
768
            log several statements in one event, when we executed only first).
769
            We set it to not see the ';' (otherwise it would get into binlog
770
            and Query_log_event::print() would give ';;' output).
771
            This also helps display only the current query in SHOW
772
            PROCESSLIST.
773
            Note that we don't need LOCK_thread_count to modify query_length.
774
          */
775
          if (*found_semicolon &&
520.1.22 by Brian Aker
Second pass of thd cleanup
776
              (session->query_length= (ulong)(*found_semicolon - session->query)))
777
            session->query_length--;
1126.10.14 by Padraig O'Sullivan
Added calls to the query execution start/end dtrace probes.
778
          DRIZZLE_QUERY_EXEC_START(session->query,
779
                                   session->thread_id,
1126.10.25 by Padraig O'Sullivan
Updated calls to some dtrace probes to cast the parameter to const char *
780
                                   const_cast<const char *>(session->db ? session->db : ""));
1 by brian
clean slate
781
          /* Actually execute the query */
520.1.22 by Brian Aker
Second pass of thd cleanup
782
          mysql_execute_command(session);
1126.10.14 by Padraig O'Sullivan
Added calls to the query execution start/end dtrace probes.
783
          DRIZZLE_QUERY_EXEC_DONE(0);
1 by brian
clean slate
784
	}
785
      }
786
    }
787
    else
788
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
789
      assert(session->is_error());
1 by brian
clean slate
790
    }
791
    lex->unit.cleanup();
520.1.22 by Brian Aker
Second pass of thd cleanup
792
    session->set_proc_info("freeing items");
793
    session->end_statement();
794
    session->cleanup_after_query();
1 by brian
clean slate
795
  }
796
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
797
  return;
1 by brian
clean slate
798
}
799
800
801
802
/**
803
  Store field definition for create.
804
805
  @return
806
    Return 0 if ok
807
*/
808
520.1.22 by Brian Aker
Second pass of thd cleanup
809
bool add_field_to_list(Session *session, LEX_STRING *field_name, enum_field_types type,
1 by brian
clean slate
810
		       char *length, char *decimals,
438.1.13 by Brian Aker
uint cleanup.
811
		       uint32_t type_modifier,
1 by brian
clean slate
812
                       enum column_format_type column_format,
813
		       Item *default_value, Item *on_update_value,
814
                       LEX_STRING *comment,
815
		       char *change,
998.1.2 by Brian Aker
First pass on removing virt columns
816
                       List<String> *interval_list, const CHARSET_INFO * const cs)
1 by brian
clean slate
817
{
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
818
  register CreateField *new_field;
520.1.22 by Brian Aker
Second pass of thd cleanup
819
  LEX  *lex= session->lex;
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
820
  drizzled::statement::AlterTable *statement= (drizzled::statement::AlterTable *)lex->statement;
1 by brian
clean slate
821
822
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
1135 by Brian Aker
Merge of Lex -> Statement refactoring
823
    return true;
1 by brian
clean slate
824
825
  if (type_modifier & PRI_KEY_FLAG)
826
  {
827
    Key *key;
828
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
829
    key= new Key(Key::PRIMARY, null_lex_str,
830
                      &default_key_create_info,
831
                      0, lex->col_list);
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
832
    statement->alter_info.key_list.push_back(key);
1 by brian
clean slate
833
    lex->col_list.empty();
834
  }
835
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
836
  {
837
    Key *key;
838
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
839
    key= new Key(Key::UNIQUE, null_lex_str,
840
                 &default_key_create_info, 0,
841
                 lex->col_list);
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
842
    statement->alter_info.key_list.push_back(key);
1 by brian
clean slate
843
    lex->col_list.empty();
844
  }
845
846
  if (default_value)
847
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
848
    /*
1 by brian
clean slate
849
      Default value should be literal => basic constants =>
850
      no need fix_fields()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
851
852
      We allow only one function as part of default value -
1 by brian
clean slate
853
      NOW() as default for TIMESTAMP type.
854
    */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
855
    if (default_value->type() == Item::FUNC_ITEM &&
1 by brian
clean slate
856
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
857
         type == DRIZZLE_TYPE_TIMESTAMP))
1 by brian
clean slate
858
    {
859
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
860
      return true;
1 by brian
clean slate
861
    }
862
    else if (default_value->type() == Item::NULL_ITEM)
863
    {
864
      default_value= 0;
865
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
866
	  NOT_NULL_FLAG)
867
      {
868
	my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
869
	return true;
1 by brian
clean slate
870
      }
871
    }
872
    else if (type_modifier & AUTO_INCREMENT_FLAG)
873
    {
874
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
875
      return true;
1 by brian
clean slate
876
    }
877
  }
878
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
879
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
1 by brian
clean slate
880
  {
881
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
882
    return true;
1 by brian
clean slate
883
  }
884
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
885
  if (!(new_field= new CreateField()) ||
520.1.22 by Brian Aker
Second pass of thd cleanup
886
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
1 by brian
clean slate
887
                      default_value, on_update_value, comment, change,
998.1.2 by Brian Aker
First pass on removing virt columns
888
                      interval_list, cs, 0, column_format))
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
889
    return true;
1 by brian
clean slate
890
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
891
  statement->alter_info.create_list.push_back(new_field);
1 by brian
clean slate
892
  lex->last_field=new_field;
1128.2.4 by Brian Aker
AlterInfo refactor back to class.
893
894
  return false;
1 by brian
clean slate
895
}
896
897
898
/** Store position for column in ALTER TABLE .. ADD column. */
899
900
void store_position_for_column(const char *name)
901
{
520.1.22 by Brian Aker
Second pass of thd cleanup
902
  current_session->lex->last_field->after=const_cast<char*> (name);
1 by brian
clean slate
903
}
904
905
/**
906
  Add a table to list of used tables.
907
908
  @param table		Table to add
909
  @param alias		alias for table (or null if no alias)
910
  @param table_options	A set of the following bits:
911
                         - TL_OPTION_UPDATING : Table will be updated
912
                         - TL_OPTION_FORCE_INDEX : Force usage of index
913
                         - TL_OPTION_ALIAS : an alias in multi table DELETE
914
  @param lock_type	How table should be locked
915
  @param use_index	List of indexed used in USE INDEX
916
  @param ignore_index	List of indexed used in IGNORE INDEX
917
918
  @retval
919
      0		Error
920
  @retval
327.2.4 by Brian Aker
Refactoring table.h
921
    \#	Pointer to TableList element added to the total table list
1 by brian
clean slate
922
*/
923
846 by Brian Aker
Removing on typedeffed class.
924
TableList *Select_Lex::add_table_to_list(Session *session,
1 by brian
clean slate
925
					     Table_ident *table,
926
					     LEX_STRING *alias,
202 by Brian Aker
Cleanup sql_lex to modern types.
927
					     uint32_t table_options,
1 by brian
clean slate
928
					     thr_lock_type lock_type,
929
					     List<Index_hint> *index_hints_arg,
930
                                             LEX_STRING *option)
931
{
327.2.4 by Brian Aker
Refactoring table.h
932
  register TableList *ptr;
933
  TableList *previous_table_ref; /* The table preceding the current one. */
1 by brian
clean slate
934
  char *alias_str;
520.1.22 by Brian Aker
Second pass of thd cleanup
935
  LEX *lex= session->lex;
1 by brian
clean slate
936
937
  if (!table)
1054.1.5 by Brian Aker
Formatting/remove dead variables.
938
    return NULL;				// End of memory
1 by brian
clean slate
939
  alias_str= alias ? alias->str : table->table.str;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
940
  if (!test(table_options & TL_OPTION_ALIAS) &&
1 by brian
clean slate
941
      check_table_name(table->table.str, table->table.length))
942
  {
943
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
1054.1.5 by Brian Aker
Formatting/remove dead variables.
944
    return NULL;
1 by brian
clean slate
945
  }
946
55 by brian
Update for using real bool types.
947
  if (table->is_derived_table() == false && table->db.str &&
1 by brian
clean slate
948
      check_db_name(&table->db))
949
  {
950
    my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
1054.1.5 by Brian Aker
Formatting/remove dead variables.
951
    return NULL;
1 by brian
clean slate
952
  }
953
954
  if (!alias)					/* Alias is case sensitive */
955
  {
956
    if (table->sel)
957
    {
958
      my_message(ER_DERIVED_MUST_HAVE_ALIAS,
959
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
1054.1.5 by Brian Aker
Formatting/remove dead variables.
960
      return NULL;
1 by brian
clean slate
961
    }
520.1.22 by Brian Aker
Second pass of thd cleanup
962
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
1054.1.5 by Brian Aker
Formatting/remove dead variables.
963
      return NULL;
1 by brian
clean slate
964
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
965
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
971.6.11 by Eric Day
Removed purecov messages.
966
    return NULL;
1 by brian
clean slate
967
  if (table->db.str)
968
  {
55 by brian
Update for using real bool types.
969
    ptr->is_fqtn= true;
1 by brian
clean slate
970
    ptr->db= table->db.str;
971
    ptr->db_length= table->db.length;
972
  }
973
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
1054.1.5 by Brian Aker
Formatting/remove dead variables.
974
    return NULL;
1 by brian
clean slate
975
  else
55 by brian
Update for using real bool types.
976
    ptr->is_fqtn= false;
1 by brian
clean slate
977
978
  ptr->alias= alias_str;
55 by brian
Update for using real bool types.
979
  ptr->is_alias= alias ? true : false;
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
980
  if (table->table.length)
1 by brian
clean slate
981
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
982
  ptr->table_name=table->table.str;
983
  ptr->table_name_length=table->table.length;
984
  ptr->lock_type=   lock_type;
985
  ptr->updating=    test(table_options & TL_OPTION_UPDATING);
986
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
987
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
988
  ptr->derived=	    table->sel;
989
  if (!ptr->derived && !my_strcasecmp(system_charset_info, ptr->db,
575.4.7 by Monty Taylor
More header cleanup.
990
                                      INFORMATION_SCHEMA_NAME.c_str()))
1 by brian
clean slate
991
  {
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
992
    plugin::InfoSchemaTable *schema_table= plugin::InfoSchemaTable::getTable(ptr->table_name);
1 by brian
clean slate
993
    if (!schema_table ||
1063.4.1 by Padraig O'Sullivan
Re-factored the InfoSchemaTable structure to now be a class. Made all data
994
        (schema_table->isHidden() &&
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.
995
         ((sql_command_flags[lex->sql_command].test(CF_BIT_STATUS_COMMAND)) == 0 ||
1 by brian
clean slate
996
          /*
997
            this check is used for show columns|keys from I_S hidden table
998
          */
999
          lex->sql_command == SQLCOM_SHOW_FIELDS ||
1000
          lex->sql_command == SQLCOM_SHOW_KEYS)))
1001
    {
1002
      my_error(ER_UNKNOWN_TABLE, MYF(0),
575.4.7 by Monty Taylor
More header cleanup.
1003
               ptr->table_name, INFORMATION_SCHEMA_NAME.c_str());
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1004
      return NULL;
1 by brian
clean slate
1005
    }
1006
    ptr->schema_table_name= ptr->table_name;
1007
    ptr->schema_table= schema_table;
1008
  }
1009
  ptr->select_lex=  lex->current_select;
1010
  ptr->index_hints= index_hints_arg;
1011
  ptr->option= option ? option->str : 0;
1012
  /* check that used name is unique */
1013
  if (lock_type != TL_IGNORE)
1014
  {
327.2.4 by Brian Aker
Refactoring table.h
1015
    TableList *first_table= (TableList*) table_list.first;
1016
    for (TableList *tables= first_table ;
1 by brian
clean slate
1017
	 tables ;
1018
	 tables=tables->next_local)
1019
    {
1020
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1021
	  !strcmp(ptr->db, tables->db))
1022
      {
971.6.11 by Eric Day
Removed purecov messages.
1023
	my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1024
	return NULL;
1 by brian
clean slate
1025
      }
1026
    }
1027
  }
1028
  /* Store the table reference preceding the current one. */
1029
  if (table_list.elements > 0)
1030
  {
1031
    /*
327.2.4 by Brian Aker
Refactoring table.h
1032
      table_list.next points to the last inserted TableList->next_local'
1 by brian
clean slate
1033
      element
1034
      We don't use the offsetof() macro here to avoid warnings from gcc
1035
    */
327.2.4 by Brian Aker
Refactoring table.h
1036
    previous_table_ref= (TableList*) ((char*) table_list.next -
1 by brian
clean slate
1037
                                       ((char*) &(ptr->next_local) -
1038
                                        (char*) ptr));
1039
    /*
1040
      Set next_name_resolution_table of the previous table reference to point
1041
      to the current table reference. In effect the list
327.2.4 by Brian Aker
Refactoring table.h
1042
      TableList::next_name_resolution_table coincides with
1043
      TableList::next_local. Later this may be changed in
1 by brian
clean slate
1044
      store_top_level_join_columns() for NATURAL/USING joins.
1045
    */
1046
    previous_table_ref->next_name_resolution_table= ptr;
1047
  }
1048
1049
  /*
1050
    Link the current table reference in a local list (list for current select).
1051
    Notice that as a side effect here we set the next_local field of the
1052
    previous table reference to 'ptr'. Here we also add one element to the
1053
    list 'table_list'.
1054
  */
481 by Brian Aker
Remove all of uchar.
1055
  table_list.link_in_list((unsigned char*) ptr, (unsigned char**) &ptr->next_local);
1 by brian
clean slate
1056
  ptr->next_name_resolution_table= NULL;
1057
  /* Link table in global list (all used tables) */
1058
  lex->add_to_query_tables(ptr);
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1059
  return ptr;
1 by brian
clean slate
1060
}
1061
1062
1063
/**
1064
  Initialize a new table list for a nested join.
1065
327.2.4 by Brian Aker
Refactoring table.h
1066
    The function initializes a structure of the TableList type
1 by brian
clean slate
1067
    for a nested join. It sets up its nested join list as empty.
1068
    The created structure is added to the front of the current
846 by Brian Aker
Removing on typedeffed class.
1069
    join list in the Select_Lex object. Then the function
1 by brian
clean slate
1070
    changes the current nest level for joins to refer to the newly
1071
    created empty list after having saved the info on the old level
1072
    in the initialized structure.
1073
520.1.22 by Brian Aker
Second pass of thd cleanup
1074
  @param session         current thread
1 by brian
clean slate
1075
1076
  @retval
1077
    0   if success
1078
  @retval
1079
    1   otherwise
1080
*/
1081
846 by Brian Aker
Removing on typedeffed class.
1082
bool Select_Lex::init_nested_join(Session *session)
1 by brian
clean slate
1083
{
327.2.4 by Brian Aker
Refactoring table.h
1084
  TableList *ptr;
1085
  nested_join_st *nested_join;
1 by brian
clean slate
1086
520.1.22 by Brian Aker
Second pass of thd cleanup
1087
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
327.2.4 by Brian Aker
Refactoring table.h
1088
                                       sizeof(nested_join_st))))
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1089
    return true;
1 by brian
clean slate
1090
  nested_join= ptr->nested_join=
481 by Brian Aker
Remove all of uchar.
1091
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1 by brian
clean slate
1092
1093
  join_list->push_front(ptr);
1094
  ptr->embedding= embedding;
1095
  ptr->join_list= join_list;
1096
  ptr->alias= (char*) "(nested_join)";
1097
  embedding= ptr;
1098
  join_list= &nested_join->join_list;
1099
  join_list->empty();
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1100
  return false;
1 by brian
clean slate
1101
}
1102
1103
1104
/**
1105
  End a nested join table list.
1106
1107
    The function returns to the previous join nest level.
1108
    If the current level contains only one member, the function
1109
    moves it one level up, eliminating the nest.
1110
520.1.22 by Brian Aker
Second pass of thd cleanup
1111
  @param session         current thread
1 by brian
clean slate
1112
1113
  @return
327.2.4 by Brian Aker
Refactoring table.h
1114
    - Pointer to TableList element added to the total table list, if success
1 by brian
clean slate
1115
    - 0, otherwise
1116
*/
1117
846 by Brian Aker
Removing on typedeffed class.
1118
TableList *Select_Lex::end_nested_join(Session *)
1 by brian
clean slate
1119
{
327.2.4 by Brian Aker
Refactoring table.h
1120
  TableList *ptr;
1121
  nested_join_st *nested_join;
1 by brian
clean slate
1122
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1123
  assert(embedding);
1 by brian
clean slate
1124
  ptr= embedding;
1125
  join_list= ptr->join_list;
1126
  embedding= ptr->embedding;
1127
  nested_join= ptr->nested_join;
1128
  if (nested_join->join_list.elements == 1)
1129
  {
327.2.4 by Brian Aker
Refactoring table.h
1130
    TableList *embedded= nested_join->join_list.head();
1 by brian
clean slate
1131
    join_list->pop();
1132
    embedded->join_list= join_list;
1133
    embedded->embedding= embedding;
1134
    join_list->push_front(embedded);
1135
    ptr= embedded;
1136
  }
1137
  else if (nested_join->join_list.elements == 0)
1138
  {
1139
    join_list->pop();
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1140
    ptr= NULL;                                     // return value
1 by brian
clean slate
1141
  }
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1142
  return ptr;
1 by brian
clean slate
1143
}
1144
1145
1146
/**
1147
  Nest last join operation.
1148
1149
    The function nest last join operation as if it was enclosed in braces.
1150
520.1.22 by Brian Aker
Second pass of thd cleanup
1151
  @param session         current thread
1 by brian
clean slate
1152
1153
  @retval
1154
    0  Error
1155
  @retval
327.2.4 by Brian Aker
Refactoring table.h
1156
    \#  Pointer to TableList element created for the new nested join
1 by brian
clean slate
1157
*/
1158
846 by Brian Aker
Removing on typedeffed class.
1159
TableList *Select_Lex::nest_last_join(Session *session)
1 by brian
clean slate
1160
{
327.2.4 by Brian Aker
Refactoring table.h
1161
  TableList *ptr;
1162
  nested_join_st *nested_join;
1163
  List<TableList> *embedded_list;
1 by brian
clean slate
1164
520.1.22 by Brian Aker
Second pass of thd cleanup
1165
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
327.2.4 by Brian Aker
Refactoring table.h
1166
                                       sizeof(nested_join_st))))
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1167
    return NULL;
1 by brian
clean slate
1168
  nested_join= ptr->nested_join=
481 by Brian Aker
Remove all of uchar.
1169
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
1 by brian
clean slate
1170
1171
  ptr->embedding= embedding;
1172
  ptr->join_list= join_list;
1173
  ptr->alias= (char*) "(nest_last_join)";
1174
  embedded_list= &nested_join->join_list;
1175
  embedded_list->empty();
1176
438.1.13 by Brian Aker
uint cleanup.
1177
  for (uint32_t i=0; i < 2; i++)
1 by brian
clean slate
1178
  {
327.2.4 by Brian Aker
Refactoring table.h
1179
    TableList *table= join_list->pop();
1 by brian
clean slate
1180
    table->join_list= embedded_list;
1181
    table->embedding= ptr;
1182
    embedded_list->push_back(table);
1183
    if (table->natural_join)
1184
    {
55 by brian
Update for using real bool types.
1185
      ptr->is_natural_join= true;
1 by brian
clean slate
1186
      /*
1187
        If this is a JOIN ... USING, move the list of joined fields to the
1188
        table reference that describes the join.
1189
      */
1190
      if (prev_join_using)
1191
        ptr->join_using_fields= prev_join_using;
1192
    }
1193
  }
1194
  join_list->push_front(ptr);
1195
  nested_join->used_tables= nested_join->not_null_tables= (table_map) 0;
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1196
  return ptr;
1 by brian
clean slate
1197
}
1198
1199
1200
/**
1201
  Add a table to the current join list.
1202
1203
    The function puts a table in front of the current join list
846 by Brian Aker
Removing on typedeffed class.
1204
    of Select_Lex object.
1 by brian
clean slate
1205
    Thus, joined tables are put into this list in the reverse order
1206
    (the most outer join operation follows first).
1207
1208
  @param table       the table to add
1209
1210
  @return
1211
    None
1212
*/
1213
846 by Brian Aker
Removing on typedeffed class.
1214
void Select_Lex::add_joined_table(TableList *table)
1 by brian
clean slate
1215
{
1216
  join_list->push_front(table);
1217
  table->join_list= join_list;
1218
  table->embedding= embedding;
1219
}
1220
1221
1222
/**
1223
  Convert a right join into equivalent left join.
1224
1225
    The function takes the current join list t[0],t[1] ... and
1226
    effectively converts it into the list t[1],t[0] ...
1227
    Although the outer_join flag for the new nested table contains
1228
    JOIN_TYPE_RIGHT, it will be handled as the inner table of a left join
1229
    operation.
1230
1231
  EXAMPLES
1232
  @verbatim
1233
    SELECT * FROM t1 RIGHT JOIN t2 ON on_expr =>
1234
      SELECT * FROM t2 LEFT JOIN t1 ON on_expr
1235
1236
    SELECT * FROM t1,t2 RIGHT JOIN t3 ON on_expr =>
1237
      SELECT * FROM t1,t3 LEFT JOIN t2 ON on_expr
1238
1239
    SELECT * FROM t1,t2 RIGHT JOIN (t3,t4) ON on_expr =>
1240
      SELECT * FROM t1,(t3,t4) LEFT JOIN t2 ON on_expr
1241
1242
    SELECT * FROM t1 LEFT JOIN t2 ON on_expr1 RIGHT JOIN t3  ON on_expr2 =>
1243
      SELECT * FROM t3 LEFT JOIN (t1 LEFT JOIN t2 ON on_expr2) ON on_expr1
1244
   @endverbatim
1245
520.1.22 by Brian Aker
Second pass of thd cleanup
1246
  @param session         current thread
1 by brian
clean slate
1247
1248
  @return
1249
    - Pointer to the table representing the inner table, if success
1250
    - 0, otherwise
1251
*/
1252
846 by Brian Aker
Removing on typedeffed class.
1253
TableList *Select_Lex::convert_right_join()
1 by brian
clean slate
1254
{
327.2.4 by Brian Aker
Refactoring table.h
1255
  TableList *tab2= join_list->pop();
1256
  TableList *tab1= join_list->pop();
1 by brian
clean slate
1257
1258
  join_list->push_front(tab2);
1259
  join_list->push_front(tab1);
1260
  tab1->outer_join|= JOIN_TYPE_RIGHT;
1261
1054.1.5 by Brian Aker
Formatting/remove dead variables.
1262
  return tab1;
1 by brian
clean slate
1263
}
1264
1265
/**
1266
  Set lock for all tables in current select level.
1267
1268
  @param lock_type			Lock to set for tables
1269
1270
  @note
1271
    If lock is a write lock, then tables->updating is set 1
1272
    This is to get tables_ok to know that the table is updated by the
1273
    query
1274
*/
1275
846 by Brian Aker
Removing on typedeffed class.
1276
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
1 by brian
clean slate
1277
{
1278
  bool for_update= lock_type >= TL_READ_NO_INSERT;
1279
327.2.4 by Brian Aker
Refactoring table.h
1280
  for (TableList *tables= (TableList*) table_list.first;
1 by brian
clean slate
1281
       tables;
1282
       tables= tables->next_local)
1283
  {
1284
    tables->lock_type= lock_type;
1285
    tables->updating=  for_update;
1286
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1287
  return;
1 by brian
clean slate
1288
}
1289
1290
1291
/**
846 by Brian Aker
Removing on typedeffed class.
1292
  Create a fake Select_Lex for a unit.
1 by brian
clean slate
1293
846 by Brian Aker
Removing on typedeffed class.
1294
    The method create a fake Select_Lex object for a unit.
1 by brian
clean slate
1295
    This object is created for any union construct containing a union
1296
    operation and also for any single select union construct of the form
1297
    @verbatim
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1298
    (SELECT ... order_st BY order_list [LIMIT n]) order_st BY ...
1 by brian
clean slate
1299
    @endvarbatim
1300
    or of the form
1301
    @varbatim
327.2.3 by Brian Aker
Refactoring of class Table
1302
    (SELECT ... order_st BY LIMIT n) order_st BY ...
1 by brian
clean slate
1303
    @endvarbatim
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1304
520.1.22 by Brian Aker
Second pass of thd cleanup
1305
  @param session_arg		   thread handle
1 by brian
clean slate
1306
1307
  @note
1308
    The object is used to retrieve rows from the temporary table
1309
    where the result on the union is obtained.
1310
1311
  @retval
1312
    1     on failure to create the object
1313
  @retval
1314
    0     on success
1315
*/
1316
848 by Brian Aker
typdef class removal (just... use the name of the class).
1317
bool Select_Lex_Unit::add_fake_select_lex(Session *session_arg)
1 by brian
clean slate
1318
{
846 by Brian Aker
Removing on typedeffed class.
1319
  Select_Lex *first_sl= first_select();
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1320
  assert(!fake_select_lex);
1 by brian
clean slate
1321
846 by Brian Aker
Removing on typedeffed class.
1322
  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
1323
      return(1);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1324
  fake_select_lex->include_standalone(this,
847 by Brian Aker
More typdef class removal.
1325
                                      (Select_Lex_Node**)&fake_select_lex);
1 by brian
clean slate
1326
  fake_select_lex->select_number= INT_MAX;
520.1.22 by Brian Aker
Second pass of thd cleanup
1327
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1 by brian
clean slate
1328
  fake_select_lex->make_empty_select();
1329
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1330
  fake_select_lex->select_limit= 0;
1331
1332
  fake_select_lex->context.outer_context=first_sl->context.outer_context;
327.2.3 by Brian Aker
Refactoring of class Table
1333
  /* allow item list resolving in fake select for order_st BY */
55 by brian
Update for using real bool types.
1334
  fake_select_lex->context.resolve_in_select_list= true;
1 by brian
clean slate
1335
  fake_select_lex->context.select_lex= fake_select_lex;
1336
1337
  if (!is_union())
1338
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1339
    /*
1340
      This works only for
327.2.3 by Brian Aker
Refactoring of class Table
1341
      (SELECT ... order_st BY list [LIMIT n]) order_st BY order_list [LIMIT m],
1342
      (SELECT ... LIMIT n) order_st BY order_list [LIMIT m]
1 by brian
clean slate
1343
      just before the parser starts processing order_list
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1344
    */
1 by brian
clean slate
1345
    global_parameters= fake_select_lex;
1346
    fake_select_lex->no_table_names_allowed= 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
1347
    session_arg->lex->current_select= fake_select_lex;
1 by brian
clean slate
1348
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1349
  session_arg->lex->pop_context();
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1350
  return(0);
1 by brian
clean slate
1351
}
1352
1353
1354
/**
1355
  Push a new name resolution context for a JOIN ... ON clause to the
1356
  context stack of a query block.
1357
1358
    Create a new name resolution context for a JOIN ... ON clause,
1359
    set the first and last leaves of the list of table references
1360
    to be used for name resolution, and push the newly created
1361
    context to the stack of contexts of the query.
1362
520.1.22 by Brian Aker
Second pass of thd cleanup
1363
  @param session       pointer to current thread
1 by brian
clean slate
1364
  @param left_op   left  operand of the JOIN
1365
  @param right_op  rigth operand of the JOIN
1366
1367
  @retval
55 by brian
Update for using real bool types.
1368
    false  if all is OK
1 by brian
clean slate
1369
  @retval
55 by brian
Update for using real bool types.
1370
    true   if a memory allocation error occured
1 by brian
clean slate
1371
*/
1372
1373
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
1374
push_new_name_resolution_context(Session *session,
327.2.4 by Brian Aker
Refactoring table.h
1375
                                 TableList *left_op, TableList *right_op)
1 by brian
clean slate
1376
{
1377
  Name_resolution_context *on_context;
520.1.22 by Brian Aker
Second pass of thd cleanup
1378
  if (!(on_context= new (session->mem_root) Name_resolution_context))
55 by brian
Update for using real bool types.
1379
    return true;
1 by brian
clean slate
1380
  on_context->init();
1381
  on_context->first_name_resolution_table=
1382
    left_op->first_leaf_for_name_resolution();
1383
  on_context->last_name_resolution_table=
1384
    right_op->last_leaf_for_name_resolution();
520.1.22 by Brian Aker
Second pass of thd cleanup
1385
  return session->lex->push_context(on_context);
1 by brian
clean slate
1386
}
1387
1388
1389
/**
1390
  Add an ON condition to the second operand of a JOIN ... ON.
1391
1392
    Add an ON condition to the right operand of a JOIN ... ON clause.
1393
1394
  @param b     the second operand of a JOIN ... ON
1395
  @param expr  the condition to be added to the ON clause
1396
1397
  @retval
55 by brian
Update for using real bool types.
1398
    false  if there was some error
1 by brian
clean slate
1399
  @retval
55 by brian
Update for using real bool types.
1400
    true   if all is OK
1 by brian
clean slate
1401
*/
1402
327.2.4 by Brian Aker
Refactoring table.h
1403
void add_join_on(TableList *b, Item *expr)
1 by brian
clean slate
1404
{
1405
  if (expr)
1406
  {
1407
    if (!b->on_expr)
1408
      b->on_expr= expr;
1409
    else
1410
    {
1411
      /*
1412
        If called from the parser, this happens if you have both a
1413
        right and left join. If called later, it happens if we add more
1414
        than one condition to the ON clause.
1415
      */
1416
      b->on_expr= new Item_cond_and(b->on_expr,expr);
1417
    }
1418
    b->on_expr->top_level_item();
1419
  }
1420
}
1421
1422
1423
/**
1424
  Mark that there is a NATURAL JOIN or JOIN ... USING between two
1425
  tables.
1426
1427
    This function marks that table b should be joined with a either via
1428
    a NATURAL JOIN or via JOIN ... USING. Both join types are special
1429
    cases of each other, so we treat them together. The function
1430
    setup_conds() creates a list of equal condition between all fields
1431
    of the same name for NATURAL JOIN or the fields in 'using_fields'
1432
    for JOIN ... USING. The list of equality conditions is stored
1433
    either in b->on_expr, or in JOIN::conds, depending on whether there
1434
    was an outer join.
1435
1436
  EXAMPLE
1437
  @verbatim
1438
    SELECT * FROM t1 NATURAL LEFT JOIN t2
1439
     <=>
1440
    SELECT * FROM t1 LEFT JOIN t2 ON (t1.i=t2.i and t1.j=t2.j ... )
1441
1442
    SELECT * FROM t1 NATURAL JOIN t2 WHERE <some_cond>
1443
     <=>
1444
    SELECT * FROM t1, t2 WHERE (t1.i=t2.i and t1.j=t2.j and <some_cond>)
1445
1446
    SELECT * FROM t1 JOIN t2 USING(j) WHERE <some_cond>
1447
     <=>
1448
    SELECT * FROM t1, t2 WHERE (t1.j=t2.j and <some_cond>)
1449
   @endverbatim
1450
1451
  @param a		  Left join argument
1452
  @param b		  Right join argument
1453
  @param using_fields    Field names from USING clause
1454
*/
1455
327.2.4 by Brian Aker
Refactoring table.h
1456
void add_join_natural(TableList *a, TableList *b, List<String> *using_fields,
846 by Brian Aker
Removing on typedeffed class.
1457
                      Select_Lex *lex)
1 by brian
clean slate
1458
{
1459
  b->natural_join= a;
1460
  lex->prev_join_using= using_fields;
1461
}
1462
1463
1464
/**
1465
  kill on thread.
1466
520.1.22 by Brian Aker
Second pass of thd cleanup
1467
  @param session			Thread class
1 by brian
clean slate
1468
  @param id			Thread id
1469
  @param only_kill_query        Should it kill the query or the connection
1470
1471
  @note
1472
    This is written such that we have a short lock on LOCK_thread_count
1473
*/
1474
230.1.9 by Monty Taylor
Merged in remove-include-dir.
1475
static unsigned int
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1476
kill_one_thread(Session *, ulong id, bool only_kill_query)
1 by brian
clean slate
1477
{
1014.7.3 by Nathan Williams
Reformatted refactored bits per monty's suggestion.
1478
  Session *tmp= NULL;
438.1.13 by Brian Aker
uint cleanup.
1479
  uint32_t error=ER_NO_SUCH_THREAD;
398.1.10 by Monty Taylor
Actually removed VOID() this time.
1480
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
1014.7.1 by Nathan Williams
Replaced global I_List<Session> with std::vector<Session*>
1481
  
1014.7.3 by Nathan Williams
Reformatted refactored bits per monty's suggestion.
1482
  for( vector<Session*>::iterator it= session_list.begin(); it != session_list.end(); ++it )
1 by brian
clean slate
1483
  {
1014.7.3 by Nathan Williams
Reformatted refactored bits per monty's suggestion.
1484
    if ((*it)->thread_id == id)
1 by brian
clean slate
1485
    {
1014.7.3 by Nathan Williams
Reformatted refactored bits per monty's suggestion.
1486
      tmp= *it;
1 by brian
clean slate
1487
      pthread_mutex_lock(&tmp->LOCK_delete);	// Lock from delete
1488
      break;
1489
    }
1490
  }
398.1.10 by Monty Taylor
Actually removed VOID() this time.
1491
  pthread_mutex_unlock(&LOCK_thread_count);
1 by brian
clean slate
1492
  if (tmp)
1493
  {
520.1.21 by Brian Aker
THD -> Session rename
1494
    tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
1 by brian
clean slate
1495
    error=0;
1496
    pthread_mutex_unlock(&tmp->LOCK_delete);
1497
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1498
  return(error);
1 by brian
clean slate
1499
}
1500
1501
1502
/*
1503
  kills a thread and sends response
1504
1505
  SYNOPSIS
1506
    sql_kill()
520.1.22 by Brian Aker
Second pass of thd cleanup
1507
    session			Thread class
1 by brian
clean slate
1508
    id			Thread id
1509
    only_kill_query     Should it kill the query or the connection
1510
*/
1511
520.1.22 by Brian Aker
Second pass of thd cleanup
1512
void sql_kill(Session *session, ulong id, bool only_kill_query)
1 by brian
clean slate
1513
{
438.1.13 by Brian Aker
uint cleanup.
1514
  uint32_t error;
520.1.22 by Brian Aker
Second pass of thd cleanup
1515
  if (!(error= kill_one_thread(session, id, only_kill_query)))
836 by Brian Aker
Fixed session call from function to method.
1516
    session->my_ok();
1 by brian
clean slate
1517
  else
1518
    my_error(error, MYF(0), id);
1519
}
1520
1521
1522
/**
1523
  Check if the select is a simple select (not an union).
1524
1525
  @retval
1526
    0	ok
1527
  @retval
1528
    1	error	; In this case the error messege is sent to the client
1529
*/
1530
1531
bool check_simple_select()
1532
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1533
  Session *session= current_session;
1534
  LEX *lex= session->lex;
1 by brian
clean slate
1535
  if (lex->current_select != &lex->select_lex)
1536
  {
1537
    char command[80];
520.1.22 by Brian Aker
Second pass of thd cleanup
1538
    Lex_input_stream *lip= session->m_lip;
629.5.2 by Toru Maesaka
Second pass of replacing MySQL's strmake() with libc calls
1539
    strncpy(command, lip->yylval->symbol.str,
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
1540
            min(lip->yylval->symbol.length, (uint32_t)(sizeof(command)-1)));
1541
    command[min(lip->yylval->symbol.length, (uint32_t)(sizeof(command)-1))]=0;
1 by brian
clean slate
1542
    my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
1543
    return 1;
1544
  }
1545
  return 0;
1546
}
1547
1548
1549
/**
1550
  Construct ALL/ANY/SOME subquery Item.
1551
1552
  @param left_expr   pointer to left expression
1553
  @param cmp         compare function creator
1554
  @param all         true if we create ALL subquery
1555
  @param select_lex  pointer on parsed subquery structure
1556
1557
  @return
1558
    constructed Item (or 0 if out of memory)
1559
*/
1560
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.
1561
                                chooser_compare_func_creator cmp,
1562
                                bool all,
846 by Brian Aker
Removing on typedeffed class.
1563
                                Select_Lex *select_lex)
1 by brian
clean slate
1564
{
1565
  if ((cmp == &comp_eq_creator) && !all)       //  = ANY <=> IN
1566
    return new Item_in_subselect(left_expr, select_lex);
1567
1568
  if ((cmp == &comp_ne_creator) && all)        // <> ALL <=> NOT IN
1569
    return new Item_func_not(new Item_in_subselect(left_expr, select_lex));
1570
1571
  Item_allany_subselect *it=
1572
    new Item_allany_subselect(left_expr, cmp, select_lex, all);
1573
  if (all)
1574
    return it->upper_item= new Item_func_not_all(it);	/* ALL */
1575
1576
  return it->upper_item= new Item_func_nop_all(it);      /* ANY/SOME */
1577
}
1578
1579
1580
/**
826 by Brian Aker
Simplify update
1581
  Update query pre-check.
1 by brian
clean slate
1582
520.1.22 by Brian Aker
Second pass of thd cleanup
1583
  @param session		Thread handler
1 by brian
clean slate
1584
  @param tables	Global/local table list (have to be the same)
1585
1586
  @retval
55 by brian
Update for using real bool types.
1587
    false OK
1 by brian
clean slate
1588
  @retval
55 by brian
Update for using real bool types.
1589
    true  Error
1 by brian
clean slate
1590
*/
1591
826 by Brian Aker
Simplify update
1592
bool update_precheck(Session *session, TableList *)
1 by brian
clean slate
1593
{
1594
  const char *msg= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
1595
  LEX *lex= session->lex;
846 by Brian Aker
Removing on typedeffed class.
1596
  Select_Lex *select_lex= &lex->select_lex;
1 by brian
clean slate
1597
826 by Brian Aker
Simplify update
1598
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1 by brian
clean slate
1599
  {
1600
    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
1601
    return(true);
1 by brian
clean slate
1602
  }
1603
826 by Brian Aker
Simplify update
1604
  if (session->lex->select_lex.table_list.elements > 1)
1 by brian
clean slate
1605
  {
826 by Brian Aker
Simplify update
1606
    if (select_lex->order_list.elements)
1607
      msg= "ORDER BY";
1608
    else if (select_lex->select_limit)
1609
      msg= "LIMIT";
1610
    if (msg)
1611
    {
1612
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
1613
      return(true);
1614
    }
1 by brian
clean slate
1615
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1616
  return(false);
1 by brian
clean slate
1617
}
1618
1619
1620
/**
1621
  simple INSERT query pre-check.
1622
520.1.22 by Brian Aker
Second pass of thd cleanup
1623
  @param session		Thread handler
1 by brian
clean slate
1624
  @param tables	Global table list
1625
1626
  @retval
55 by brian
Update for using real bool types.
1627
    false  OK
1 by brian
clean slate
1628
  @retval
55 by brian
Update for using real bool types.
1629
    true   error
1 by brian
clean slate
1630
*/
1631
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1632
bool insert_precheck(Session *session, TableList *)
1 by brian
clean slate
1633
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1634
  LEX *lex= session->lex;
1 by brian
clean slate
1635
1636
  /*
1637
    Check that we have modify privileges for the first table and
1638
    select privileges for the rest
1639
  */
1640
  if (lex->update_list.elements != lex->value_list.elements)
1641
  {
1642
    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
1643
    return(true);
1 by brian
clean slate
1644
  }
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1645
  return(false);
1 by brian
clean slate
1646
}
1647
1648
1649
/**
1650
  CREATE TABLE query pre-check.
1651
520.1.22 by Brian Aker
Second pass of thd cleanup
1652
  @param session			Thread handler
1 by brian
clean slate
1653
  @param tables		Global table list
1654
  @param create_table	        Table which will be created
1655
1656
  @retval
55 by brian
Update for using real bool types.
1657
    false   OK
1 by brian
clean slate
1658
  @retval
55 by brian
Update for using real bool types.
1659
    true   Error
1 by brian
clean slate
1660
*/
1661
575.1.2 by Monty Taylor
Changed a bunch of __attribute__((unused)) to removing the parameter name instead.
1662
bool create_table_precheck(Session *, TableList *,
327.2.4 by Brian Aker
Refactoring table.h
1663
                           TableList *create_table)
1 by brian
clean slate
1664
{
55 by brian
Update for using real bool types.
1665
  bool error= true;                                 // Error message is given
1 by brian
clean slate
1666
1667
  if (create_table && (strcmp(create_table->db, "information_schema") == 0))
1668
  {
575.4.7 by Monty Taylor
More header cleanup.
1669
    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
1670
    return(true);
1 by brian
clean slate
1671
  }
1672
55 by brian
Update for using real bool types.
1673
  error= false;
1 by brian
clean slate
1674
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1675
  return(error);
1 by brian
clean slate
1676
}
1677
1678
1679
/**
1680
  negate given expression.
1681
520.1.22 by Brian Aker
Second pass of thd cleanup
1682
  @param session  thread handler
1 by brian
clean slate
1683
  @param expr expression for negation
1684
1685
  @return
1686
    negated expression
1687
*/
1688
520.1.22 by Brian Aker
Second pass of thd cleanup
1689
Item *negate_expression(Session *session, Item *expr)
1 by brian
clean slate
1690
{
1691
  Item *negated;
1692
  if (expr->type() == Item::FUNC_ITEM &&
1693
      ((Item_func *) expr)->functype() == Item_func::NOT_FUNC)
1694
  {
1695
    /* it is NOT(NOT( ... )) */
1696
    Item *arg= ((Item_func *) expr)->arguments()[0];
520.1.22 by Brian Aker
Second pass of thd cleanup
1697
    enum_parsing_place place= session->lex->current_select->parsing_place;
1 by brian
clean slate
1698
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1699
      return arg;
1700
    /*
1701
      if it is not boolean function then we have to emulate value of
1702
      not(not(a)), it will be a != 0
1703
    */
1704
    return new Item_func_ne(arg, new Item_int((char*) "0", 0, 1));
1705
  }
1706
520.1.22 by Brian Aker
Second pass of thd cleanup
1707
  if ((negated= expr->neg_transformer(session)) != 0)
1 by brian
clean slate
1708
    return negated;
1709
  return new Item_func_not(expr);
1710
}
1711
1712
1713
/*
1714
  Check that char length of a string does not exceed some limit.
1715
1716
  SYNOPSIS
1717
  check_string_char_length()
1718
      str              string to be checked
1719
      err_msg          error message to be displayed if the string is too long
1720
      max_char_length  max length in symbols
1721
      cs               string charset
1722
1723
  RETURN
55 by brian
Update for using real bool types.
1724
    false   the passed string is not longer than max_char_length
1725
    true    the passed string is longer than max_char_length
1 by brian
clean slate
1726
*/
1727
1728
1729
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
438.1.13 by Brian Aker
uint cleanup.
1730
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
1 by brian
clean slate
1731
                              bool no_error)
1732
{
1733
  int well_formed_error;
438.1.13 by Brian Aker
uint cleanup.
1734
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1 by brian
clean slate
1735
                                      max_char_length, &well_formed_error);
1736
1737
  if (!well_formed_error &&  str->length == res)
55 by brian
Update for using real bool types.
1738
    return false;
1 by brian
clean slate
1739
1740
  if (!no_error)
1741
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_char_length);
55 by brian
Update for using real bool types.
1742
  return true;
1 by brian
clean slate
1743
}
1744
1745
575.4.7 by Monty Taylor
More header cleanup.
1746
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1747
                           uint32_t max_char_length,
1748
                           const char *param_for_err_msg)
1 by brian
clean slate
1749
{
1750
  /*
1751
    We don't support non-BMP characters in identifiers at the moment,
1752
    so they should be prohibited until such support is done.
1753
    This is why we use the 3-byte utf8 to check well-formedness here.
1754
  */
760 by Brian Aker
Cleanup around UTf8 code.
1755
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
1756
1 by brian
clean slate
1757
  int well_formed_error;
438.1.13 by Brian Aker
uint cleanup.
1758
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1 by brian
clean slate
1759
                                      max_char_length, &well_formed_error);
1760
1761
  if (well_formed_error)
1762
  {
1763
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
55 by brian
Update for using real bool types.
1764
    return true;
1 by brian
clean slate
1765
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1766
1 by brian
clean slate
1767
  if (str->length == res)
55 by brian
Update for using real bool types.
1768
    return false;
1 by brian
clean slate
1769
1770
  switch (err_code)
1771
  {
1772
  case 0:
1773
    break;
1774
  case ER_WRONG_STRING_LENGTH:
1775
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1776
    break;
1777
  case ER_TOO_LONG_IDENT:
1778
    my_error(err_code, MYF(0), str->str);
1779
    break;
1780
  default:
51.1.61 by Jay Pipes
Removed/replaced BUG symbols and standardized TRUE/FALSE
1781
    assert(0);
1 by brian
clean slate
1782
    break;
1783
  }
55 by brian
Update for using real bool types.
1784
  return true;
1 by brian
clean slate
1785
}
1786
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
1787
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
1 by brian
clean slate
1788
1789
1790
/**
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
1791
  This is a wrapper of DRIZZLEparse(). All the code should call parse_sql()
1792
  instead of DRIZZLEparse().
1 by brian
clean slate
1793
520.1.22 by Brian Aker
Second pass of thd cleanup
1794
  @param session Thread context.
1 by brian
clean slate
1795
  @param lip Lexer context.
1796
1797
  @return Error status.
55 by brian
Update for using real bool types.
1798
    @retval false on success.
1799
    @retval true on parsing error.
1 by brian
clean slate
1800
*/
1801
1165.1.48 by Stewart Smith
make parse_sql static to sql_parse.cc
1802
static bool parse_sql(Session *session, Lex_input_stream *lip)
1 by brian
clean slate
1803
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1804
  assert(session->m_lip == NULL);
1 by brian
clean slate
1805
1126.10.15 by Padraig O'Sullivan
Added calls to the parsing related dtrace probes.
1806
  DRIZZLE_QUERY_PARSE_START(session->query);
1807
1 by brian
clean slate
1808
  /* Set Lex_input_stream. */
1809
520.1.22 by Brian Aker
Second pass of thd cleanup
1810
  session->m_lip= lip;
1 by brian
clean slate
1811
1812
  /* Parse the query. */
1813
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
1814
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
1815
520.4.50 by Monty Taylor
Finish changing the bison namespace argument from MYSQL to DRIZZLE
1816
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
520.1.22 by Brian Aker
Second pass of thd cleanup
1817
1818
  assert(!mysql_parse_status || session->is_error());
1 by brian
clean slate
1819
1820
  /* Reset Lex_input_stream. */
1821
520.1.22 by Brian Aker
Second pass of thd cleanup
1822
  session->m_lip= NULL;
1 by brian
clean slate
1823
1126.10.15 by Padraig O'Sullivan
Added calls to the parsing related dtrace probes.
1824
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1825
1 by brian
clean slate
1826
  /* That's it. */
1827
520.1.22 by Brian Aker
Second pass of thd cleanup
1828
  return mysql_parse_status || session->is_fatal_error;
1 by brian
clean slate
1829
}
1830
1831
/**
1832
  @} (end of group Runtime_Environment)
1833
*/