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