~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Olaf van der Spek
  • Date: 2011-07-04 19:11:47 UTC
  • mto: This revision was merged to the branch mainline in revision 2367.
  • Revision ID: olafvdspek@gmail.com-20110704191147-s99ojek811zi1fzj
RemoveĀ unusedĀ Name_resolution_context::error_reporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
#include "config.h"
 
16
#include <config.h>
17
17
 
18
18
#define DRIZZLE_LEX 1
19
19
 
20
 
#include "drizzled/item/num.h"
21
 
#include "drizzled/abort_exception.h"
22
 
#include <drizzled/my_hash.h>
 
20
#include <drizzled/item/num.h>
 
21
#include <drizzled/abort_exception.h>
23
22
#include <drizzled/error.h>
24
23
#include <drizzled/nested_join.h>
25
24
#include <drizzled/query_id.h>
26
 
#include "drizzled/transaction_services.h"
 
25
#include <drizzled/transaction_services.h>
27
26
#include <drizzled/sql_parse.h>
28
27
#include <drizzled/data_home.h>
29
28
#include <drizzled/sql_base.h>
30
29
#include <drizzled/show.h>
31
 
#include <drizzled/db.h>
32
30
#include <drizzled/function/time/unix_timestamp.h>
33
31
#include <drizzled/function/get_system_var.h>
34
32
#include <drizzled/item/cmpfunc.h>
41
39
#include <drizzled/plugin/client.h>
42
40
#include <drizzled/statement.h>
43
41
#include <drizzled/statement/alter_table.h>
44
 
#include "drizzled/probes.h"
45
 
#include "drizzled/global_charset_info.h"
46
 
 
47
 
#include "drizzled/plugin/logging.h"
48
 
#include "drizzled/plugin/query_rewrite.h"
49
 
#include "drizzled/plugin/query_cache.h"
50
 
#include "drizzled/plugin/authorization.h"
51
 
#include "drizzled/optimizer/explain_plan.h"
52
 
#include "drizzled/pthread_globals.h"
53
 
#include "drizzled/plugin/event_observer.h"
54
 
#include "drizzled/visibility.h"
 
42
#include <drizzled/probes.h>
 
43
#include <drizzled/charset.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>
 
51
#include <drizzled/display.h>
 
52
#include <drizzled/visibility.h>
 
53
#include <drizzled/kill.h>
 
54
#include <drizzled/schema.h>
 
55
#include <drizzled/item/subselect.h>
 
56
#include <drizzled/diagnostics_area.h>
 
57
#include <drizzled/table_ident.h>
 
58
#include <drizzled/statistics_variables.h>
 
59
#include <drizzled/system_variables.h>
 
60
#include <drizzled/session/times.h>
 
61
#include <drizzled/session/transactions.h>
55
62
 
56
63
#include <limits.h>
57
64
 
58
65
#include <bitset>
59
66
#include <algorithm>
60
67
#include <boost/date_time.hpp>
61
 
#include "drizzled/internal/my_sys.h"
 
68
#include <drizzled/internal/my_sys.h>
62
69
 
63
70
using namespace std;
64
71
 
65
 
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
 
72
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
66
73
 
67
 
namespace drizzled
68
 
{
 
74
namespace drizzled {
69
75
 
70
76
/* Prototypes */
71
 
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
 
77
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
72
78
static bool parse_sql(Session *session, Lex_input_stream *lip);
73
 
void parse(Session *session, const char *inBuf, uint32_t length);
 
79
void parse(Session&, const char *inBuf, uint32_t length);
74
80
 
75
81
/**
76
82
  @defgroup Runtime_Environment Runtime Environment
78
84
*/
79
85
 
80
86
extern size_t my_thread_stack_size;
81
 
extern const CHARSET_INFO *character_set_filesystem;
 
87
extern const charset_info_st *character_set_filesystem;
82
88
 
83
89
namespace
84
90
{
91
97
  "Shutdown",
92
98
  "Connect",
93
99
  "Ping",
 
100
  "Kill",
94
101
  "Error"  // Last command number
95
102
};
96
103
 
121
128
 
122
129
void init_update_queries(void)
123
130
{
124
 
  uint32_t x;
125
 
 
126
 
  for (x= 0; x <= SQLCOM_END; x++)
 
131
  for (uint32_t x= uint32_t(SQLCOM_SELECT); 
 
132
       x <= uint32_t(SQLCOM_END); x++)
 
133
  {
127
134
    sql_command_flags[x].reset();
 
135
  }
128
136
 
129
137
  sql_command_flags[SQLCOM_CREATE_TABLE]=   CF_CHANGES_DATA;
130
138
  sql_command_flags[SQLCOM_CREATE_INDEX]=   CF_CHANGES_DATA;
167
175
                         can be zero.
168
176
 
169
177
  @todo
170
 
    set session->lex->sql_command to SQLCOM_END here.
 
178
    set session->lex().sql_command to SQLCOM_END here.
171
179
  @todo
172
180
    The following has to be changed to an 8 byte integer
173
181
 
177
185
    1   request of thread shutdown, i. e. if command is
178
186
        COM_QUIT/COM_SHUTDOWN
179
187
*/
180
 
bool dispatch_command(enum enum_server_command command, Session *session,
181
 
                      char* packet, uint32_t packet_length)
 
188
bool dispatch_command(enum_server_command command, Session *session,
 
189
                      const char* packet, uint32_t packet_length)
182
190
{
183
 
  bool error= 0;
 
191
  bool error= false;
184
192
  Query_id &query_id= Query_id::get_query_id();
185
193
 
186
194
  DRIZZLE_COMMAND_START(session->thread_id, command);
187
195
 
188
196
  session->command= command;
189
 
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
190
 
  session->set_time();
 
197
  session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
198
  session->times.set_time();
191
199
  session->setQueryId(query_id.value());
192
200
 
193
 
  switch( command ) {
 
201
  switch (command)
 
202
  {
194
203
  /* Ignore these statements. */
195
204
  case COM_PING:
196
205
    break;
 
206
 
197
207
  /* Increase id and count all other statements. */
198
208
  default:
199
209
    session->status_var.questions++;
200
210
    query_id.next();
201
211
  }
202
212
 
203
 
  /* @todo set session->lex->sql_command to SQLCOM_END here */
 
213
  /* @todo set session->lex().sql_command to SQLCOM_END here */
204
214
 
205
215
  plugin::Logging::preDo(session);
206
216
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
210
220
 
211
221
  session->server_status&=
212
222
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
213
 
  switch (command) {
214
 
  case COM_INIT_DB:
 
223
 
 
224
  switch (command)
215
225
  {
216
 
    if (packet_length == 0)
 
226
  case COM_USE_SCHEMA:
217
227
    {
218
 
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
 
228
      if (packet_length == 0)
 
229
      {
 
230
        my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
 
231
        break;
 
232
      }
 
233
      if (not schema::change(*session, identifier::Schema(string(packet, packet_length))))
 
234
      {
 
235
        session->my_ok();
 
236
      }
219
237
      break;
220
238
    }
221
239
 
222
 
    string tmp(packet, packet_length);
223
 
 
224
 
    identifier::Schema identifier(tmp);
225
 
 
226
 
    if (not change_db(session, identifier))
227
 
    {
228
 
      session->my_ok();
229
 
    }
230
 
    break;
231
 
  }
232
240
  case COM_QUERY:
233
 
  {
234
 
    if (not session->readAndStoreQuery(packet, packet_length))
235
 
      break;                                    // fatal error is set
236
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
237
 
                        session->thread_id,
238
 
                        const_cast<const char *>(session->schema()->c_str()));
239
 
 
240
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
241
 
 
242
 
    break;
243
 
  }
 
241
    {
 
242
      session->readAndStoreQuery(packet, packet_length);
 
243
      DRIZZLE_QUERY_START(session->getQueryString()->c_str(), session->thread_id, session->schema()->c_str());
 
244
      parse(*session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
245
      break;
 
246
    }
 
247
 
244
248
  case COM_QUIT:
245
249
    /* We don't calculate statistics for this command */
246
 
    session->main_da.disable_status();              // Don't send anything back
247
 
    error=true;                                 // End server
 
250
    session->main_da().disable_status();              // Don't send anything back
 
251
    error= true;                                        // End server
248
252
    break;
 
253
 
 
254
  case COM_KILL:
 
255
    {
 
256
      if (packet_length != 4)
 
257
      {
 
258
        my_error(ER_NO_SUCH_THREAD, MYF(0), 0);
 
259
        break;
 
260
      }
 
261
      else
 
262
      {
 
263
        uint32_t kill_id;
 
264
        memcpy(&kill_id, packet, sizeof(uint32_t));
 
265
 
 
266
        kill_id= ntohl(kill_id);
 
267
        (void)drizzled::kill(*session->user(), kill_id, true);
 
268
      }
 
269
      session->my_ok();
 
270
      break;
 
271
    }
 
272
 
249
273
  case COM_SHUTDOWN:
250
 
  {
251
 
    session->status_var.com_other++;
252
 
    session->my_eof();
253
 
    session->close_thread_tables();                     // Free before kill
254
 
    kill_drizzle();
255
 
    error=true;
256
 
    break;
257
 
  }
 
274
    {
 
275
      session->status_var.com_other++;
 
276
      session->my_eof();
 
277
      session->close_thread_tables();                   // Free before kill
 
278
      kill_drizzle();
 
279
      error= true;
 
280
      break;
 
281
    }
 
282
 
258
283
  case COM_PING:
259
284
    session->status_var.com_other++;
260
285
    session->my_ok();                           // Tell client we are alive
261
286
    break;
 
287
 
262
288
  case COM_SLEEP:
263
289
  case COM_CONNECT:                             // Impossible here
264
290
  case COM_END:
268
294
  }
269
295
 
270
296
  /* If commit fails, we should be able to reset the OK status. */
271
 
  session->main_da.can_overwrite_status= true;
272
 
  TransactionServices &transaction_services= TransactionServices::singleton();
273
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
274
 
  session->main_da.can_overwrite_status= false;
 
297
  session->main_da().can_overwrite_status= true;
 
298
  TransactionServices::autocommitOrRollback(*session, session->is_error());
 
299
  session->main_da().can_overwrite_status= false;
275
300
 
276
301
  session->transaction.stmt.reset();
277
302
 
279
304
  /* report error issued during command execution */
280
305
  if (session->killed_errno())
281
306
  {
282
 
    if (! session->main_da.is_set())
 
307
    if (not session->main_da().is_set())
283
308
      session->send_kill_message();
284
309
  }
285
310
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
289
314
  }
290
315
 
291
316
  /* Can not be true, but do not take chances in production. */
292
 
  assert(! session->main_da.is_sent);
 
317
  assert(! session->main_da().is_sent);
293
318
 
294
 
  switch (session->main_da.status())
 
319
  switch (session->main_da().status())
295
320
  {
296
321
  case Diagnostics_area::DA_ERROR:
297
322
    /* The query failed, send error to log and abort bootstrap. */
298
 
    session->getClient()->sendError(session->main_da.sql_errno(),
299
 
                               session->main_da.message());
 
323
    session->getClient()->sendError(session->main_da().sql_errno(),
 
324
                               session->main_da().message());
300
325
    break;
301
326
 
302
327
  case Diagnostics_area::DA_EOF:
316
341
    break;
317
342
  }
318
343
 
319
 
  session->main_da.is_sent= true;
 
344
  session->main_da().is_sent= true;
320
345
 
321
346
  session->set_proc_info("closing tables");
322
347
  /* Free tables */
374
399
    1                 out of memory or SHOW commands are not allowed
375
400
                      in this version of the server.
376
401
*/
377
 
static bool _schema_select(Session *session, Select_Lex *sel,
378
 
                           const string& schema_table_name)
 
402
static bool _schema_select(Session& session, Select_Lex& sel, const string& schema_table_name)
379
403
{
380
404
  LEX_STRING db, table;
381
405
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
383
407
     We have to make non const db_name & table_name
384
408
     because of lower_case_table_names
385
409
  */
386
 
  session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
387
 
  session->make_lex_string(&table, schema_table_name, false);
388
 
 
389
 
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
390
 
                               NULL, table_options, TL_READ))
391
 
  {
392
 
    return true;
393
 
  }
394
 
  return false;
 
410
  session.make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
 
411
  session.make_lex_string(&table, schema_table_name, false);
 
412
  return not sel.add_table_to_list(&session, new Table_ident(db, table), NULL, table_options, TL_READ);
395
413
}
396
414
 
397
 
int prepare_new_schema_table(Session *session, LEX *lex,
398
 
                             const string& schema_table_name)
 
415
int prepare_new_schema_table(Session *session, LEX& lex0, const string& schema_table_name)
399
416
{
400
 
  Select_Lex *schema_select_lex= NULL;
401
 
 
402
 
  Select_Lex *select_lex= lex->current_select;
403
 
  assert(select_lex);
404
 
  if (_schema_select(session, select_lex, schema_table_name))
405
 
  {
406
 
    return(1);
407
 
  }
408
 
  TableList *table_list= (TableList*) select_lex->table_list.first;
409
 
  assert(table_list);
410
 
  table_list->schema_select_lex= schema_select_lex;
411
 
 
 
417
  Select_Lex& lex= *lex0.current_select;
 
418
  if (_schema_select(*session, lex, schema_table_name))
 
419
    return 1;
 
420
  TableList *table_list= (TableList*)lex.table_list.first;
 
421
  table_list->schema_select_lex= NULL;
412
422
  return 0;
413
423
}
414
424
 
445
455
static int execute_command(Session *session)
446
456
{
447
457
  bool res= false;
448
 
  LEX  *lex= session->lex;
 
458
 
449
459
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
450
 
  Select_Lex *select_lex= &lex->select_lex;
 
460
  Select_Lex *select_lex= &session->lex().select_lex;
 
461
 
451
462
  /* list of all tables in query */
452
463
  TableList *all_tables;
453
464
 
466
477
    assert(first_table == all_tables);
467
478
    assert(first_table == all_tables && first_table != 0);
468
479
  */
469
 
  lex->first_lists_tables_same();
 
480
  session->lex().first_lists_tables_same();
 
481
 
470
482
  /* should be assigned after making first tables same */
471
 
  all_tables= lex->query_tables;
 
483
  all_tables= session->lex().query_tables;
 
484
 
472
485
  /* set context for commands which do not use setup_tables */
473
 
  select_lex->
474
 
    context.resolve_in_table_list_only((TableList*)select_lex->
475
 
                                       table_list.first);
 
486
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
476
487
 
477
488
  /*
478
489
    Reset warning count for each query that uses tables
481
492
    variables, but for now this is probably good enough.
482
493
    Don't reset warnings when executing a stored routine.
483
494
  */
484
 
  if (all_tables || ! lex->is_single_level_stmt())
 
495
  if (all_tables || ! session->lex().is_single_level_stmt())
485
496
  {
486
 
    drizzle_reset_errors(session, 0);
 
497
    drizzle_reset_errors(*session, 0);
487
498
  }
488
499
 
489
 
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
 
500
  assert(not session->transaction.stmt.hasModifiedNonTransData());
490
501
 
491
502
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
492
503
      && ! session->inTransaction()
493
 
      && lex->statement->isTransactional())
 
504
      && session->lex().statement->isTransactional())
494
505
  {
495
 
    if (session->startTransaction() == false)
 
506
    if (not session->startTransaction())
496
507
    {
497
508
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
498
509
      return true;
500
511
  }
501
512
 
502
513
  /* now we are ready to execute the statement */
503
 
  res= lex->statement->execute();
 
514
  res= session->lex().statement->execute();
504
515
  session->set_proc_info("query end");
505
516
  /*
506
517
    The return value for ROW_COUNT() is "implementation dependent" if the
508
519
    wants. We also keep the last value in case of SQLCOM_CALL or
509
520
    SQLCOM_EXECUTE.
510
521
  */
511
 
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
522
  if (! (sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT)))
512
523
  {
513
524
    session->row_count_func= -1;
514
525
  }
517
528
}
518
529
bool execute_sqlcom_select(Session *session, TableList *all_tables)
519
530
{
520
 
  LEX   *lex= session->lex;
 
531
  LEX   *lex= &session->lex();
521
532
  select_result *result=lex->result;
522
533
  bool res= false;
523
534
  /* assign global limit variable if limit is not given */
533
544
      && ! session->inTransaction()
534
545
      && ! lex->statement->isShow())
535
546
  {
536
 
    if (session->startTransaction() == false)
 
547
    if (not session->startTransaction())
537
548
    {
538
549
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
539
550
      return true;
550
561
        to prepend EXPLAIN to any query and receive output for it,
551
562
        even if the query itself redirects the output.
552
563
      */
553
 
      if (!(result= new select_send()))
554
 
        return true;
 
564
      result= new select_send();
555
565
      session->send_explain_fields(result);
556
566
      optimizer::ExplainPlan planner;
557
 
      res= planner.explainUnion(session, &session->lex->unit, result);
 
567
      res= planner.explainUnion(session, &session->lex().unit, result);
558
568
      if (lex->describe & DESCRIBE_EXTENDED)
559
569
      {
560
570
        char buff[1024];
561
571
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
562
572
        str.length(0);
563
 
        session->lex->unit.print(&str, QT_ORDINARY);
 
573
        session->lex().unit.print(&str);
564
574
        str.append('\0');
565
575
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
566
576
                     ER_YES, str.ptr());
569
579
        result->abort();
570
580
      else
571
581
        result->send_eof();
 
582
 
572
583
      delete result;
573
584
    }
574
585
    else
575
586
    {
576
 
      if (!result && !(result= new select_send()))
577
 
        return true;
 
587
      if (not result)
 
588
        result= new select_send();
578
589
 
579
590
      /* Init the Query Cache plugin */
580
 
      plugin::QueryCache::prepareResultset(session); 
 
591
      plugin::QueryCache::prepareResultset(session);
581
592
      res= handle_select(session, lex, result, 0);
582
593
      /* Send the Resultset to the cache */
583
 
      plugin::QueryCache::setResultset(session); 
 
594
      plugin::QueryCache::setResultset(session);
584
595
 
585
596
      if (result != lex->result)
586
597
        delete result;
593
604
#define MY_YACC_INIT 1000                       // Start with big alloc
594
605
#define MY_YACC_MAX  32000                      // Because of 'short'
595
606
 
596
 
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
 
607
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
597
608
{
598
 
  LEX   *lex= current_session->lex;
 
609
  LEX   *lex= &current_session->lex();
599
610
  ulong old_info=0;
600
611
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
601
612
    return 1;
618
629
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
619
630
  }
620
631
  *yyss=(short*) lex->yacc_yyss;
621
 
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
 
632
  *yyvs=(ParserType*) lex->yacc_yyvs;
622
633
  return 0;
623
634
}
624
635
 
637
648
}
638
649
 
639
650
 
640
 
bool
641
 
new_select(LEX *lex, bool move_down)
 
651
bool new_select(LEX *lex, bool move_down)
642
652
{
643
 
  Select_Lex *select_lex;
644
 
  Session *session= lex->session;
645
 
 
646
 
  if (!(select_lex= new (session->mem_root) Select_Lex()))
647
 
    return true;
 
653
  Session* session= lex->session;
 
654
  Select_Lex* select_lex= new (session->mem_root) Select_Lex;
648
655
 
649
656
  select_lex->select_number= ++session->select_number;
650
657
  select_lex->parent_lex= lex; /* Used in init_query. */
655
662
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
656
663
  {
657
664
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
658
 
    return(1);
 
665
    return 1;
659
666
  }
660
667
 
661
668
  select_lex->nest_level= lex->nest_level;
662
669
  if (move_down)
663
670
  {
664
 
    Select_Lex_Unit *unit;
665
671
    /* first select_lex of subselect or derived table */
666
 
    if (!(unit= new (session->mem_root) Select_Lex_Unit()))
667
 
      return(1);
 
672
    Select_Lex_Unit* unit= new (session->mem_root) Select_Lex_Unit();
668
673
 
669
674
    unit->init_query();
670
675
    unit->init_select();
722
727
 
723
728
void create_select_for_variable(Session *session, const char *var_name)
724
729
{
725
 
  LEX *lex;
726
 
  LEX_STRING tmp, null_lex_string;
727
 
  Item *var;
728
 
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
729
 
  char *end= buff;
730
 
 
731
 
  lex= session->lex;
732
 
  init_select(lex);
733
 
  lex->sql_command= SQLCOM_SELECT;
 
730
  LEX& lex= session->lex();
 
731
  init_select(&lex);
 
732
  lex.sql_command= SQLCOM_SELECT;
 
733
  LEX_STRING tmp;
734
734
  tmp.str= (char*) var_name;
735
735
  tmp.length=strlen(var_name);
 
736
  LEX_STRING null_lex_string;
736
737
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
737
738
  /*
738
739
    We set the name of Item to @@session.var_name because that then is used
739
740
    as the column name in the output.
740
741
  */
741
 
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
 
742
  if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string))
742
743
  {
 
744
    char buff[MAX_SYS_VAR_LENGTH*2+4+8];
 
745
    char *end= buff;
743
746
    end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
744
747
    var->set_name(buff, end-buff, system_charset_info);
745
748
    session->add_item_to_list(var);
755
758
  @param       length  Length of the query text
756
759
*/
757
760
 
758
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
761
void parse(Session& session, const char *inBuf, uint32_t length)
759
762
{
760
 
  session->lex->start(session);
761
 
 
762
 
  session->reset_for_next_command();
 
763
  session.lex().start(&session);
 
764
  session.reset_for_next_command();
763
765
  /* Check if the Query is Cached if and return true if yes
764
766
   * TODO the plugin has to make sure that the query is cacheble
765
767
   * by setting the query_safe_cache param to TRUE
766
768
   */
767
 
  bool res= true;
768
 
  if (plugin::QueryCache::isCached(session))
769
 
  {
770
 
    res= plugin::QueryCache::sendCachedResultset(session);
771
 
  }
772
 
  if (not res)
773
 
  {
774
 
    return;
775
 
  }
776
 
  LEX *lex= session->lex;
777
 
  Lex_input_stream lip(session, inBuf, length);
778
 
  bool err= parse_sql(session, &lip);
779
 
  if (!err)
780
 
  {
781
 
    {
782
 
      if (not session->is_error())
783
 
      {
784
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
785
 
                                 session->thread_id,
786
 
                                 const_cast<const char *>(session->schema()->c_str()));
787
 
        // Implement Views here --Brian
788
 
        /* Actually execute the query */
789
 
        try 
790
 
        {
791
 
          execute_command(session);
792
 
        }
793
 
        catch (...)
794
 
        {
795
 
          // Just try to catch any random failures that could have come
796
 
          // during execution.
797
 
          DRIZZLE_ABORT;
798
 
        }
799
 
        DRIZZLE_QUERY_EXEC_DONE(0);
800
 
      }
801
 
    }
802
 
  }
803
 
  else
804
 
  {
805
 
    assert(session->is_error());
806
 
  }
807
 
  lex->unit.cleanup();
808
 
  session->set_proc_info("freeing items");
809
 
  session->end_statement();
810
 
  session->cleanup_after_query();
811
 
  session->set_end_timer();
 
769
  if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
 
770
      return;
 
771
  Lex_input_stream lip(&session, inBuf, length);
 
772
  if (parse_sql(&session, &lip))
 
773
    assert(session.is_error());
 
774
  else if (not session.is_error())
 
775
  {
 
776
    DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id,
 
777
                             const_cast<const char *>(session.schema()->c_str()));
 
778
    // Implement Views here --Brian
 
779
    /* Actually execute the query */
 
780
    try
 
781
    {
 
782
      execute_command(&session);
 
783
    }
 
784
    catch (...)
 
785
    {
 
786
      // Just try to catch any random failures that could have come
 
787
      // during execution.
 
788
      DRIZZLE_ABORT;
 
789
    }
 
790
    DRIZZLE_QUERY_EXEC_DONE(0);
 
791
  }
 
792
  session.lex().unit.cleanup();
 
793
  session.set_proc_info("freeing items");
 
794
  session.end_statement();
 
795
  session.cleanup_after_query();
 
796
  session.times.set_end_timer(session);
812
797
}
813
798
 
814
799
 
815
 
 
816
800
/**
817
801
  Store field definition for create.
818
802
 
827
811
                       Item *default_value, Item *on_update_value,
828
812
                       LEX_STRING *comment,
829
813
                       char *change,
830
 
                       List<String> *interval_list, const CHARSET_INFO * const cs)
 
814
                       List<String> *interval_list, const charset_info_st * const cs)
831
815
{
832
816
  register CreateField *new_field;
833
 
  LEX  *lex= session->lex;
 
817
  LEX  *lex= &session->lex();
834
818
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
835
819
 
836
820
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
844
828
                      &default_key_create_info,
845
829
                      0, lex->col_list);
846
830
    statement->alter_info.key_list.push_back(key);
847
 
    lex->col_list.empty();
 
831
    lex->col_list.clear();
848
832
  }
849
833
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
850
834
  {
854
838
                 &default_key_create_info, 0,
855
839
                 lex->col_list);
856
840
    statement->alter_info.key_list.push_back(key);
857
 
    lex->col_list.empty();
 
841
    lex->col_list.clear();
858
842
  }
859
843
 
860
844
  if (default_value)
895
879
    return true;
896
880
  }
897
881
 
898
 
  if (!(new_field= new CreateField()) ||
899
 
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
900
 
                      default_value, on_update_value, comment, change,
901
 
                      interval_list, cs, 0, column_format))
 
882
  new_field= new CreateField;
 
883
  if (new_field->init(session, field_name->str, type, length, decimals,
 
884
                         type_modifier, comment, change, interval_list, cs, 0, column_format)
 
885
      || new_field->setDefaultValue(default_value, on_update_value))
902
886
    return true;
903
887
 
904
888
  statement->alter_info.create_list.push_back(new_field);
935
919
                                         List<Index_hint> *index_hints_arg,
936
920
                                         LEX_STRING *option)
937
921
{
938
 
  TableList *ptr;
939
922
  TableList *previous_table_ref; /* The table preceding the current one. */
940
923
  char *alias_str;
941
 
  LEX *lex= session->lex;
 
924
  LEX *lex= &session->lex();
942
925
 
943
926
  if (!table)
944
927
    return NULL;                                // End of memory
950
933
    return NULL;
951
934
  }
952
935
 
953
 
  if (table->is_derived_table() == false && table->db.str)
 
936
  if (not table->is_derived_table() && table->db.str)
954
937
  {
955
938
    my_casedn_str(files_charset_info, table->db.str);
956
939
 
957
940
    identifier::Schema schema_identifier(string(table->db.str));
958
 
    if (not check_db_name(session, schema_identifier))
 
941
    if (not schema::check(*session, schema_identifier))
959
942
    {
960
943
 
961
944
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
971
954
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
972
955
      return NULL;
973
956
    }
974
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
975
 
      return NULL;
 
957
    alias_str= (char*) session->mem.memdup(alias_str,table->table.length+1);
976
958
  }
977
 
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
978
 
    return NULL;
 
959
  TableList *ptr = (TableList *) session->mem.calloc(sizeof(TableList));
979
960
 
980
961
  if (table->db.str)
981
962
  {
1016
997
    }
1017
998
  }
1018
999
  /* Store the table reference preceding the current one. */
1019
 
  if (table_list.elements > 0)
 
1000
  if (table_list.size() > 0)
1020
1001
  {
1021
1002
    /*
1022
1003
      table_list.next points to the last inserted TableList->next_local'
1069
1050
    1   otherwise
1070
1051
*/
1071
1052
 
1072
 
bool Select_Lex::init_nested_join(Session *session)
 
1053
void Select_Lex::init_nested_join(Session& session)
1073
1054
{
1074
 
  TableList *ptr;
1075
 
  NestedJoin *nested_join;
1076
 
 
1077
 
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1078
 
                                       sizeof(NestedJoin))))
1079
 
    return true;
 
1055
  TableList* ptr= (TableList*) session.mem.calloc(ALIGN_SIZE(sizeof(TableList)) + sizeof(NestedJoin));
1080
1056
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1081
 
  nested_join= ptr->getNestedJoin();
 
1057
  NestedJoin* nested_join= ptr->getNestedJoin();
1082
1058
  join_list->push_front(ptr);
1083
1059
  ptr->setEmbedding(embedding);
1084
1060
  ptr->setJoinList(join_list);
1085
1061
  ptr->alias= (char*) "(nested_join)";
1086
1062
  embedding= ptr;
1087
1063
  join_list= &nested_join->join_list;
1088
 
  join_list->empty();
1089
 
  return false;
 
1064
  join_list->clear();
1090
1065
}
1091
1066
 
1092
1067
 
1114
1089
  join_list= ptr->getJoinList();
1115
1090
  embedding= ptr->getEmbedding();
1116
1091
  nested_join= ptr->getNestedJoin();
1117
 
  if (nested_join->join_list.elements == 1)
 
1092
  if (nested_join->join_list.size() == 1)
1118
1093
  {
1119
 
    TableList *embedded= nested_join->join_list.head();
 
1094
    TableList *embedded= &nested_join->join_list.front();
1120
1095
    join_list->pop();
1121
1096
    embedded->setJoinList(join_list);
1122
1097
    embedded->setEmbedding(embedding);
1123
1098
    join_list->push_front(embedded);
1124
1099
    ptr= embedded;
1125
1100
  }
1126
 
  else if (nested_join->join_list.elements == 0)
 
1101
  else if (nested_join->join_list.size() == 0)
1127
1102
  {
1128
1103
    join_list->pop();
1129
1104
    ptr= NULL;                                     // return value
1147
1122
 
1148
1123
TableList *Select_Lex::nest_last_join(Session *session)
1149
1124
{
1150
 
  TableList *ptr;
1151
 
  NestedJoin *nested_join;
1152
 
  List<TableList> *embedded_list;
1153
 
 
1154
 
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1155
 
                                          sizeof(NestedJoin))))
1156
 
    return NULL;
 
1125
  TableList* ptr= (TableList*) session->mem.calloc(ALIGN_SIZE(sizeof(TableList)) + sizeof(NestedJoin));
1157
1126
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1158
 
  nested_join= ptr->getNestedJoin();
 
1127
  NestedJoin* nested_join= ptr->getNestedJoin();
1159
1128
  ptr->setEmbedding(embedding);
1160
1129
  ptr->setJoinList(join_list);
1161
1130
  ptr->alias= (char*) "(nest_last_join)";
1162
 
  embedded_list= &nested_join->join_list;
1163
 
  embedded_list->empty();
 
1131
  List<TableList>* embedded_list= &nested_join->join_list;
 
1132
  embedded_list->clear();
1164
1133
 
1165
1134
  for (uint32_t i=0; i < 2; i++)
1166
1135
  {
1303
1272
  Select_Lex *first_sl= first_select();
1304
1273
  assert(!fake_select_lex);
1305
1274
 
1306
 
  if (!(fake_select_lex= new (session_arg->mem_root) Select_Lex()))
1307
 
      return(1);
1308
 
  fake_select_lex->include_standalone(this,
1309
 
                                      (Select_Lex_Node**)&fake_select_lex);
 
1275
  fake_select_lex= new (session_arg->mem_root) Select_Lex();
 
1276
  fake_select_lex->include_standalone(this, (Select_Lex_Node**)&fake_select_lex);
1310
1277
  fake_select_lex->select_number= INT_MAX;
1311
 
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
 
1278
  fake_select_lex->parent_lex= &session_arg->lex(); /* Used in init_query. */
1312
1279
  fake_select_lex->make_empty_select();
1313
1280
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1314
1281
  fake_select_lex->select_limit= 0;
1328
1295
    */
1329
1296
    global_parameters= fake_select_lex;
1330
1297
    fake_select_lex->no_table_names_allowed= 1;
1331
 
    session_arg->lex->current_select= fake_select_lex;
 
1298
    session_arg->lex().current_select= fake_select_lex;
1332
1299
  }
1333
 
  session_arg->lex->pop_context();
1334
 
  return(0);
 
1300
  session_arg->lex().pop_context();
 
1301
  return 0;
1335
1302
}
1336
1303
 
1337
1304
 
1354
1321
    true   if a memory allocation error occured
1355
1322
*/
1356
1323
 
1357
 
bool
1358
 
push_new_name_resolution_context(Session *session,
1359
 
                                 TableList *left_op, TableList *right_op)
 
1324
void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
1360
1325
{
1361
 
  Name_resolution_context *on_context;
1362
 
  if (!(on_context= new (session->mem_root) Name_resolution_context))
1363
 
    return true;
 
1326
  Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
1364
1327
  on_context->init();
1365
 
  on_context->first_name_resolution_table=
1366
 
    left_op->first_leaf_for_name_resolution();
1367
 
  on_context->last_name_resolution_table=
1368
 
    right_op->last_leaf_for_name_resolution();
1369
 
  return session->lex->push_context(on_context);
 
1328
  on_context->first_name_resolution_table= left_op.first_leaf_for_name_resolution();
 
1329
  on_context->last_name_resolution_table= right_op.last_leaf_for_name_resolution();
 
1330
  session.lex().push_context(on_context);
1370
1331
}
1371
1332
 
1372
1333
 
1454
1415
    1   error   ; In this case the error messege is sent to the client
1455
1416
*/
1456
1417
 
1457
 
bool check_simple_select(Session::pointer session)
 
1418
bool check_simple_select(Session* session)
1458
1419
{
1459
 
  LEX *lex= session->lex;
1460
 
  if (lex->current_select != &lex->select_lex)
 
1420
  if (session->lex().current_select != &session->lex().select_lex)
1461
1421
  {
1462
1422
    char command[80];
1463
1423
    Lex_input_stream *lip= session->m_lip;
1517
1477
bool update_precheck(Session *session, TableList *)
1518
1478
{
1519
1479
  const char *msg= 0;
1520
 
  LEX *lex= session->lex;
1521
 
  Select_Lex *select_lex= &lex->select_lex;
 
1480
  Select_Lex *select_lex= &session->lex().select_lex;
1522
1481
 
1523
 
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
 
1482
  if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
1524
1483
  {
1525
1484
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1526
 
    return(true);
 
1485
    return true;
1527
1486
  }
1528
1487
 
1529
 
  if (session->lex->select_lex.table_list.elements > 1)
 
1488
  if (session->lex().select_lex.table_list.size() > 1)
1530
1489
  {
1531
 
    if (select_lex->order_list.elements)
 
1490
    if (select_lex->order_list.size())
1532
1491
      msg= "ORDER BY";
1533
1492
    else if (select_lex->select_limit)
1534
1493
      msg= "LIMIT";
1535
1494
    if (msg)
1536
1495
    {
1537
1496
      my_error(ER_WRONG_USAGE, MYF(0), "UPDATE", msg);
1538
 
      return(true);
 
1497
      return true;
1539
1498
    }
1540
1499
  }
1541
 
  return(false);
 
1500
  return false;
1542
1501
}
1543
1502
 
1544
1503
 
1556
1515
 
1557
1516
bool insert_precheck(Session *session, TableList *)
1558
1517
{
1559
 
  LEX *lex= session->lex;
1560
 
 
1561
1518
  /*
1562
1519
    Check that we have modify privileges for the first table and
1563
1520
    select privileges for the rest
1564
1521
  */
1565
 
  if (lex->update_list.elements != lex->value_list.elements)
 
1522
  if (session->lex().update_list.size() != session->lex().value_list.size())
1566
1523
  {
1567
1524
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1568
 
    return(true);
 
1525
    return true;
1569
1526
  }
1570
 
  return(false);
 
1527
  return false;
1571
1528
}
1572
1529
 
1573
1530
 
1589
1546
  {
1590
1547
    /* it is NOT(NOT( ... )) */
1591
1548
    Item *arg= ((Item_func *) expr)->arguments()[0];
1592
 
    enum_parsing_place place= session->lex->current_select->parsing_place;
 
1549
    enum_parsing_place place= session->lex().current_select->parsing_place;
1593
1550
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1594
1551
      return arg;
1595
1552
    /*
1622
1579
 
1623
1580
 
1624
1581
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
1625
 
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
 
1582
                              uint32_t max_char_length, const charset_info_st * const cs,
1626
1583
                              bool no_error)
1627
1584
{
1628
1585
  int well_formed_error;
1647
1604
    so they should be prohibited until such support is done.
1648
1605
    This is why we use the 3-byte utf8 to check well-formedness here.
1649
1606
  */
1650
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
1607
  const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
1651
1608
 
1652
1609
  int well_formed_error;
1653
1610
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1705
1662
 
1706
1663
  /* Parse the query. */
1707
1664
 
1708
 
  bool parse_status= DRIZZLEparse(session) != 0;
 
1665
  bool parse_status= base_sql_parse(session) != 0;
1709
1666
 
1710
1667
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1711
1668