~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-03-24 00:16:14 UTC
  • mto: This revision was merged to the branch mainline in revision 2251.
  • Revision ID: olafvdspek@gmail.com-20110324001614-wvmgc6eg52oq2321
Remove const_reference and reference from Session

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/my_hash.h>
 
20
#include <drizzled/item/num.h>
 
21
#include <drizzled/abort_exception.h>
21
22
#include <drizzled/error.h>
22
23
#include <drizzled/nested_join.h>
23
24
#include <drizzled/query_id.h>
24
 
#include "drizzled/transaction_services.h"
 
25
#include <drizzled/transaction_services.h>
25
26
#include <drizzled/sql_parse.h>
26
27
#include <drizzled/data_home.h>
27
28
#include <drizzled/sql_base.h>
28
29
#include <drizzled/show.h>
29
 
#include <drizzled/db.h>
30
30
#include <drizzled/function/time/unix_timestamp.h>
31
31
#include <drizzled/function/get_system_var.h>
32
32
#include <drizzled/item/cmpfunc.h>
33
33
#include <drizzled/item/null.h>
34
34
#include <drizzled/session.h>
 
35
#include <drizzled/session/cache.h>
35
36
#include <drizzled/sql_load.h>
36
37
#include <drizzled/lock.h>
37
38
#include <drizzled/select_send.h>
38
39
#include <drizzled/plugin/client.h>
39
40
#include <drizzled/statement.h>
40
41
#include <drizzled/statement/alter_table.h>
41
 
#include "drizzled/probes.h"
42
 
#include "drizzled/session/cache.h"
43
 
#include "drizzled/global_charset_info.h"
44
 
 
45
 
#include "drizzled/plugin/logging.h"
46
 
#include "drizzled/plugin/query_rewrite.h"
47
 
#include "drizzled/plugin/query_cache.h"
48
 
#include "drizzled/plugin/authorization.h"
49
 
#include "drizzled/optimizer/explain_plan.h"
50
 
#include "drizzled/pthread_globals.h"
51
 
#include "drizzled/plugin/event_observer.h"
 
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>
 
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/transactions.h>
52
61
 
53
62
#include <limits.h>
54
63
 
55
64
#include <bitset>
56
65
#include <algorithm>
57
66
#include <boost/date_time.hpp>
58
 
#include "drizzled/internal/my_sys.h"
 
67
#include <drizzled/internal/my_sys.h>
59
68
 
60
69
using namespace std;
61
70
 
62
 
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
 
71
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
63
72
 
64
 
namespace drizzled
65
 
{
 
73
namespace drizzled {
66
74
 
67
75
/* Prototypes */
68
 
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
 
76
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
69
77
static bool parse_sql(Session *session, Lex_input_stream *lip);
70
 
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
 
78
void parse(Session *session, const char *inBuf, uint32_t length);
71
79
 
72
80
/**
73
81
  @defgroup Runtime_Environment Runtime Environment
77
85
extern size_t my_thread_stack_size;
78
86
extern const CHARSET_INFO *character_set_filesystem;
79
87
 
80
 
const LEX_STRING command_name[COM_END+1]={
81
 
  { C_STRING_WITH_LEN("Sleep") },
82
 
  { C_STRING_WITH_LEN("Quit") },
83
 
  { C_STRING_WITH_LEN("Init DB") },
84
 
  { C_STRING_WITH_LEN("Query") },
85
 
  { C_STRING_WITH_LEN("Shutdown") },
86
 
  { C_STRING_WITH_LEN("Connect") },
87
 
  { C_STRING_WITH_LEN("Ping") },
88
 
  { C_STRING_WITH_LEN("Error") }  // Last command number
 
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",
 
99
  "Kill",
 
100
  "Error"  // Last command number
89
101
};
90
102
 
 
103
}
 
104
 
91
105
const char *xa_state_names[]={
92
106
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
93
107
};
106
120
*/
107
121
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
108
122
 
 
123
const std::string &getCommandName(const enum_server_command& command)
 
124
{
 
125
  return command_name[command];
 
126
}
 
127
 
109
128
void init_update_queries(void)
110
129
{
111
130
  uint32_t x;
154
173
                         can be zero.
155
174
 
156
175
  @todo
157
 
    set session->lex->sql_command to SQLCOM_END here.
 
176
    set session->lex().sql_command to SQLCOM_END here.
158
177
  @todo
159
178
    The following has to be changed to an 8 byte integer
160
179
 
164
183
    1   request of thread shutdown, i. e. if command is
165
184
        COM_QUIT/COM_SHUTDOWN
166
185
*/
167
 
bool dispatch_command(enum enum_server_command command, Session *session,
 
186
bool dispatch_command(enum_server_command command, Session *session,
168
187
                      char* packet, uint32_t packet_length)
169
188
{
170
189
  bool error= 0;
173
192
  DRIZZLE_COMMAND_START(session->thread_id, command);
174
193
 
175
194
  session->command= command;
176
 
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
195
  session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
177
196
  session->set_time();
178
197
  session->setQueryId(query_id.value());
179
198
 
187
206
    query_id.next();
188
207
  }
189
208
 
190
 
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
209
  /* @todo set session->lex().sql_command to SQLCOM_END here */
191
210
 
192
211
  plugin::Logging::preDo(session);
193
212
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
197
216
 
198
217
  session->server_status&=
199
218
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
 
219
 
200
220
  switch (command) {
201
221
  case COM_INIT_DB:
202
222
  {
208
228
 
209
229
    string tmp(packet, packet_length);
210
230
 
211
 
    SchemaIdentifier identifier(tmp);
 
231
    identifier::Schema identifier(tmp);
212
232
 
213
 
    if (not mysql_change_db(session, identifier))
 
233
    if (not schema::change(*session, identifier))
214
234
    {
215
235
      session->my_ok();
216
236
    }
224
244
                        session->thread_id,
225
245
                        const_cast<const char *>(session->schema()->c_str()));
226
246
 
227
 
    mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
247
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
228
248
 
229
249
    break;
230
250
  }
231
251
  case COM_QUIT:
232
252
    /* We don't calculate statistics for this command */
233
 
    session->main_da.disable_status();              // Don't send anything back
234
 
    error=true;                                 // End server
 
253
    session->main_da().disable_status();              // Don't send anything back
 
254
    error= true;                                        // End server
235
255
    break;
 
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));
 
267
 
 
268
        kill_id= ntohl(kill_id);
 
269
        (void)drizzled::kill(*session->user(), kill_id, true);
 
270
      }
 
271
      session->my_ok();
 
272
      break;
 
273
    }
236
274
  case COM_SHUTDOWN:
237
275
  {
238
276
    session->status_var.com_other++;
239
277
    session->my_eof();
240
278
    session->close_thread_tables();                     // Free before kill
241
279
    kill_drizzle();
242
 
    error=true;
 
280
    error= true;
243
281
    break;
244
282
  }
245
283
  case COM_PING:
255
293
  }
256
294
 
257
295
  /* If commit fails, we should be able to reset the OK status. */
258
 
  session->main_da.can_overwrite_status= true;
 
296
  session->main_da().can_overwrite_status= true;
259
297
  TransactionServices &transaction_services= TransactionServices::singleton();
260
 
  transaction_services.autocommitOrRollback(session, session->is_error());
261
 
  session->main_da.can_overwrite_status= false;
 
298
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
299
  session->main_da().can_overwrite_status= false;
262
300
 
263
301
  session->transaction.stmt.reset();
264
302
 
266
304
  /* report error issued during command execution */
267
305
  if (session->killed_errno())
268
306
  {
269
 
    if (! session->main_da.is_set())
 
307
    if (! session->main_da().is_set())
270
308
      session->send_kill_message();
271
309
  }
272
310
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
276
314
  }
277
315
 
278
316
  /* Can not be true, but do not take chances in production. */
279
 
  assert(! session->main_da.is_sent);
 
317
  assert(! session->main_da().is_sent);
280
318
 
281
 
  switch (session->main_da.status())
 
319
  switch (session->main_da().status())
282
320
  {
283
321
  case Diagnostics_area::DA_ERROR:
284
322
    /* The query failed, send error to log and abort bootstrap. */
285
 
    session->client->sendError(session->main_da.sql_errno(),
286
 
                               session->main_da.message());
 
323
    session->getClient()->sendError(session->main_da().sql_errno(),
 
324
                               session->main_da().message());
287
325
    break;
288
326
 
289
327
  case Diagnostics_area::DA_EOF:
290
 
    session->client->sendEOF();
 
328
    session->getClient()->sendEOF();
291
329
    break;
292
330
 
293
331
  case Diagnostics_area::DA_OK:
294
 
    session->client->sendOK();
 
332
    session->getClient()->sendOK();
295
333
    break;
296
334
 
297
335
  case Diagnostics_area::DA_DISABLED:
299
337
 
300
338
  case Diagnostics_area::DA_EMPTY:
301
339
  default:
302
 
    session->client->sendOK();
 
340
    session->getClient()->sendOK();
303
341
    break;
304
342
  }
305
343
 
306
 
  session->main_da.is_sent= true;
 
344
  session->main_da().is_sent= true;
307
345
 
308
346
  session->set_proc_info("closing tables");
309
347
  /* Free tables */
373
411
  session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
374
412
  session->make_lex_string(&table, schema_table_name, false);
375
413
 
376
 
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
377
 
                               NULL, table_options, TL_READ))
 
414
  if (not sel->add_table_to_list(session, new Table_ident(db, table), NULL, table_options, TL_READ))
378
415
  {
379
416
    return true;
380
417
  }
381
418
  return false;
382
419
}
383
420
 
384
 
int prepare_new_schema_table(Session *session, LEX *lex,
 
421
int prepare_new_schema_table(Session *session, LEX& lex,
385
422
                             const string& schema_table_name)
386
423
{
387
424
  Select_Lex *schema_select_lex= NULL;
388
425
 
389
 
  Select_Lex *select_lex= lex->current_select;
 
426
  Select_Lex *select_lex= lex.current_select;
390
427
  assert(select_lex);
391
428
  if (_schema_select(session, select_lex, schema_table_name))
392
429
  {
429
466
    true        Error
430
467
*/
431
468
 
432
 
static int mysql_execute_command(Session *session)
 
469
static int execute_command(Session *session)
433
470
{
434
471
  bool res= false;
435
 
  LEX  *lex= session->lex;
 
472
 
436
473
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
437
 
  Select_Lex *select_lex= &lex->select_lex;
 
474
  Select_Lex *select_lex= &session->lex().select_lex;
 
475
 
438
476
  /* list of all tables in query */
439
477
  TableList *all_tables;
440
478
 
453
491
    assert(first_table == all_tables);
454
492
    assert(first_table == all_tables && first_table != 0);
455
493
  */
456
 
  lex->first_lists_tables_same();
 
494
  session->lex().first_lists_tables_same();
 
495
 
457
496
  /* should be assigned after making first tables same */
458
 
  all_tables= lex->query_tables;
 
497
  all_tables= session->lex().query_tables;
 
498
 
459
499
  /* set context for commands which do not use setup_tables */
460
 
  select_lex->
461
 
    context.resolve_in_table_list_only((TableList*)select_lex->
462
 
                                       table_list.first);
 
500
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
463
501
 
464
502
  /*
465
503
    Reset warning count for each query that uses tables
468
506
    variables, but for now this is probably good enough.
469
507
    Don't reset warnings when executing a stored routine.
470
508
  */
471
 
  if (all_tables || ! lex->is_single_level_stmt())
 
509
  if (all_tables || ! session->lex().is_single_level_stmt())
472
510
  {
473
511
    drizzle_reset_errors(session, 0);
474
512
  }
475
513
 
476
514
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
477
515
 
 
516
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
 
517
      && ! session->inTransaction()
 
518
      && session->lex().statement->isTransactional())
 
519
  {
 
520
    if (session->startTransaction() == false)
 
521
    {
 
522
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
 
523
      return true;
 
524
    }
 
525
  }
 
526
 
478
527
  /* now we are ready to execute the statement */
479
 
  res= lex->statement->execute();
 
528
  res= session->lex().statement->execute();
480
529
  session->set_proc_info("query end");
481
530
  /*
482
531
    The return value for ROW_COUNT() is "implementation dependent" if the
484
533
    wants. We also keep the last value in case of SQLCOM_CALL or
485
534
    SQLCOM_EXECUTE.
486
535
  */
487
 
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
536
  if (! (sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT)))
488
537
  {
489
538
    session->row_count_func= -1;
490
539
  }
493
542
}
494
543
bool execute_sqlcom_select(Session *session, TableList *all_tables)
495
544
{
496
 
  LEX   *lex= session->lex;
 
545
  LEX   *lex= &session->lex();
497
546
  select_result *result=lex->result;
498
547
  bool res= false;
499
548
  /* assign global limit variable if limit is not given */
503
552
      param->select_limit=
504
553
        new Item_int((uint64_t) session->variables.select_limit);
505
554
  }
 
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
 
506
568
  if (not (res= session->openTablesLock(all_tables)))
507
569
  {
508
570
    if (lex->describe)
517
579
        return true;
518
580
      session->send_explain_fields(result);
519
581
      optimizer::ExplainPlan planner;
520
 
      res= planner.explainUnion(session, &session->lex->unit, result);
 
582
      res= planner.explainUnion(session, &session->lex().unit, result);
521
583
      if (lex->describe & DESCRIBE_EXTENDED)
522
584
      {
523
585
        char buff[1024];
524
586
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
525
587
        str.length(0);
526
 
        session->lex->unit.print(&str, QT_ORDINARY);
 
588
        session->lex().unit.print(&str);
527
589
        str.append('\0');
528
590
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
529
591
                     ER_YES, str.ptr());
532
594
        result->abort();
533
595
      else
534
596
        result->send_eof();
 
597
 
535
598
      delete result;
536
599
    }
537
600
    else
540
603
        return true;
541
604
 
542
605
      /* Init the Query Cache plugin */
543
 
      plugin::QueryCache::prepareResultset(session); 
 
606
      plugin::QueryCache::prepareResultset(session);
544
607
      res= handle_select(session, lex, result, 0);
545
608
      /* Send the Resultset to the cache */
546
 
      plugin::QueryCache::setResultset(session); 
 
609
      plugin::QueryCache::setResultset(session);
547
610
 
548
611
      if (result != lex->result)
549
612
        delete result;
556
619
#define MY_YACC_INIT 1000                       // Start with big alloc
557
620
#define MY_YACC_MAX  32000                      // Because of 'short'
558
621
 
559
 
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
 
622
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
560
623
{
561
 
  LEX   *lex= current_session->lex;
 
624
  LEX   *lex= &current_session->lex();
562
625
  ulong old_info=0;
563
626
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
564
627
    return 1;
581
644
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
582
645
  }
583
646
  *yyss=(short*) lex->yacc_yyss;
584
 
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
 
647
  *yyvs=(ParserType*) lex->yacc_yyvs;
585
648
  return 0;
586
649
}
587
650
 
588
651
 
589
652
void
590
 
mysql_init_select(LEX *lex)
 
653
init_select(LEX *lex)
591
654
{
592
655
  Select_Lex *select_lex= lex->current_select;
593
656
  select_lex->init_select();
601
664
 
602
665
 
603
666
bool
604
 
mysql_new_select(LEX *lex, bool move_down)
 
667
new_select(LEX *lex, bool move_down)
605
668
{
606
669
  Select_Lex *select_lex;
607
670
  Session *session= lex->session;
608
671
 
609
672
  if (!(select_lex= new (session->mem_root) Select_Lex()))
610
 
    return(1);
 
673
    return true;
 
674
 
611
675
  select_lex->select_number= ++session->select_number;
612
676
  select_lex->parent_lex= lex; /* Used in init_query. */
613
677
  select_lex->init_query();
614
678
  select_lex->init_select();
615
679
  lex->nest_level++;
 
680
 
616
681
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
617
682
  {
618
683
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
619
684
    return(1);
620
685
  }
 
686
 
621
687
  select_lex->nest_level= lex->nest_level;
622
688
  if (move_down)
623
689
  {
645
711
    if (lex->current_select->order_list.first && !lex->current_select->braces)
646
712
    {
647
713
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
648
 
      return(1);
 
714
      return true;
649
715
    }
 
716
 
650
717
    select_lex->include_neighbour(lex->current_select);
651
718
    Select_Lex_Unit *unit= select_lex->master_unit();
652
 
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
653
 
      return(1);
 
719
 
 
720
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
721
      return true;
 
722
 
654
723
    select_lex->context.outer_context=
655
724
                unit->first_select()->context.outer_context;
656
725
  }
663
732
    list
664
733
  */
665
734
  select_lex->context.resolve_in_select_list= true;
666
 
  return(0);
 
735
 
 
736
  return false;
667
737
}
668
738
 
669
739
/**
676
746
  @param var_name               Variable name
677
747
*/
678
748
 
679
 
void create_select_for_variable(const char *var_name)
 
749
void create_select_for_variable(Session *session, const char *var_name)
680
750
{
681
 
  Session *session;
682
751
  LEX *lex;
683
752
  LEX_STRING tmp, null_lex_string;
684
753
  Item *var;
685
754
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
686
755
  char *end= buff;
687
756
 
688
 
  session= current_session;
689
 
  lex= session->lex;
690
 
  mysql_init_select(lex);
 
757
  lex= &session->lex();
 
758
  init_select(lex);
691
759
  lex->sql_command= SQLCOM_SELECT;
692
760
  tmp.str= (char*) var_name;
693
761
  tmp.length=strlen(var_name);
702
770
    var->set_name(buff, end-buff, system_charset_info);
703
771
    session->add_item_to_list(var);
704
772
  }
705
 
  return;
706
773
}
707
774
 
708
775
 
714
781
  @param       length  Length of the query text
715
782
*/
716
783
 
717
 
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
 
784
void parse(Session *session, const char *inBuf, uint32_t length)
718
785
{
719
 
  boost::posix_time::ptime start_time=boost::posix_time::microsec_clock::local_time();
720
 
  session->lex->start(session);
 
786
  session->lex().start(session);
721
787
 
722
788
  session->reset_for_next_command();
723
789
  /* Check if the Query is Cached if and return true if yes
733
799
  {
734
800
    return;
735
801
  }
736
 
  LEX *lex= session->lex;
 
802
  LEX *lex= &session->lex();
737
803
  Lex_input_stream lip(session, inBuf, length);
738
804
  bool err= parse_sql(session, &lip);
739
805
  if (!err)
746
812
                                 const_cast<const char *>(session->schema()->c_str()));
747
813
        // Implement Views here --Brian
748
814
        /* Actually execute the query */
749
 
        try 
 
815
        try
750
816
        {
751
 
          mysql_execute_command(session);
 
817
          execute_command(session);
752
818
        }
753
819
        catch (...)
754
820
        {
755
821
          // Just try to catch any random failures that could have come
756
822
          // during execution.
757
 
          unireg_abort(1);
 
823
          DRIZZLE_ABORT;
758
824
        }
759
825
        DRIZZLE_QUERY_EXEC_DONE(0);
760
826
      }
768
834
  session->set_proc_info("freeing items");
769
835
  session->end_statement();
770
836
  session->cleanup_after_query();
771
 
  boost::posix_time::ptime end_time=boost::posix_time::microsec_clock::local_time();
772
 
  session->status_var.execution_time_nsec+=(end_time-start_time).total_microseconds();
 
837
  session->set_end_timer();
773
838
}
774
839
 
775
840
 
791
856
                       List<String> *interval_list, const CHARSET_INFO * const cs)
792
857
{
793
858
  register CreateField *new_field;
794
 
  LEX  *lex= session->lex;
 
859
  LEX  *lex= &session->lex();
795
860
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
796
861
 
797
862
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
805
870
                      &default_key_create_info,
806
871
                      0, lex->col_list);
807
872
    statement->alter_info.key_list.push_back(key);
808
 
    lex->col_list.empty();
 
873
    lex->col_list.clear();
809
874
  }
810
875
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
811
876
  {
815
880
                 &default_key_create_info, 0,
816
881
                 lex->col_list);
817
882
    statement->alter_info.key_list.push_back(key);
818
 
    lex->col_list.empty();
 
883
    lex->col_list.clear();
819
884
  }
820
885
 
821
886
  if (default_value)
829
894
    */
830
895
    if (default_value->type() == Item::FUNC_ITEM &&
831
896
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
832
 
         type == DRIZZLE_TYPE_TIMESTAMP))
 
897
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
833
898
    {
834
899
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
835
900
      return true;
837
902
    else if (default_value->type() == Item::NULL_ITEM)
838
903
    {
839
904
      default_value= 0;
840
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
841
 
          NOT_NULL_FLAG)
 
905
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
842
906
      {
843
907
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
844
908
        return true;
851
915
    }
852
916
  }
853
917
 
854
 
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
 
918
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
855
919
  {
856
920
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
857
921
    return true;
858
922
  }
859
923
 
860
 
  if (!(new_field= new CreateField()) ||
861
 
      new_field->init(session, field_name->str, type, length, decimals, type_modifier,
862
 
                      default_value, on_update_value, comment, change,
863
 
                      interval_list, cs, 0, column_format))
 
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))
864
929
    return true;
865
930
 
866
931
  statement->alter_info.create_list.push_back(new_field);
870
935
}
871
936
 
872
937
 
873
 
/** Store position for column in ALTER TABLE .. ADD column. */
874
 
 
875
 
void store_position_for_column(const char *name)
876
 
{
877
 
  current_session->lex->last_field->after=const_cast<char*> (name);
878
 
}
879
 
 
880
938
/**
881
939
  Add a table to list of used tables.
882
940
 
897
955
*/
898
956
 
899
957
TableList *Select_Lex::add_table_to_list(Session *session,
900
 
                                                             Table_ident *table,
901
 
                                                             LEX_STRING *alias,
902
 
                                                             const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
903
 
                                                             thr_lock_type lock_type,
904
 
                                                             List<Index_hint> *index_hints_arg,
 
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,
905
963
                                         LEX_STRING *option)
906
964
{
907
 
  TableList *ptr;
908
965
  TableList *previous_table_ref; /* The table preceding the current one. */
909
966
  char *alias_str;
910
 
  LEX *lex= session->lex;
 
967
  LEX *lex= &session->lex();
911
968
 
912
969
  if (!table)
913
970
    return NULL;                                // End of memory
923
980
  {
924
981
    my_casedn_str(files_charset_info, table->db.str);
925
982
 
926
 
    SchemaIdentifier schema_identifier(string(table->db.str));
927
 
    if (not check_db_name(session, schema_identifier))
 
983
    identifier::Schema schema_identifier(string(table->db.str));
 
984
    if (not schema::check(*session, schema_identifier))
928
985
    {
929
986
 
930
987
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
940
997
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
941
998
      return NULL;
942
999
    }
943
 
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
 
1000
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
944
1001
      return NULL;
945
1002
  }
946
 
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
947
 
    return NULL;
 
1003
  TableList *ptr = (TableList *) session->calloc(sizeof(TableList));
948
1004
 
949
1005
  if (table->db.str)
950
1006
  {
976
1032
         tables ;
977
1033
         tables=tables->next_local)
978
1034
    {
979
 
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
980
 
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
 
1035
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
1036
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
981
1037
      {
982
1038
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
983
1039
        return NULL;
985
1041
    }
986
1042
  }
987
1043
  /* Store the table reference preceding the current one. */
988
 
  if (table_list.elements > 0)
 
1044
  if (table_list.size() > 0)
989
1045
  {
990
1046
    /*
991
1047
      table_list.next points to the last inserted TableList->next_local'
1041
1097
bool Select_Lex::init_nested_join(Session *session)
1042
1098
{
1043
1099
  TableList *ptr;
1044
 
  nested_join_st *nested_join;
 
1100
  NestedJoin *nested_join;
1045
1101
 
1046
1102
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1047
 
                                       sizeof(nested_join_st))))
 
1103
                                       sizeof(NestedJoin))))
1048
1104
    return true;
1049
 
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1105
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1050
1106
  nested_join= ptr->getNestedJoin();
1051
1107
  join_list->push_front(ptr);
1052
1108
  ptr->setEmbedding(embedding);
1054
1110
  ptr->alias= (char*) "(nested_join)";
1055
1111
  embedding= ptr;
1056
1112
  join_list= &nested_join->join_list;
1057
 
  join_list->empty();
 
1113
  join_list->clear();
1058
1114
  return false;
1059
1115
}
1060
1116
 
1076
1132
TableList *Select_Lex::end_nested_join(Session *)
1077
1133
{
1078
1134
  TableList *ptr;
1079
 
  nested_join_st *nested_join;
 
1135
  NestedJoin *nested_join;
1080
1136
 
1081
1137
  assert(embedding);
1082
1138
  ptr= embedding;
1083
1139
  join_list= ptr->getJoinList();
1084
1140
  embedding= ptr->getEmbedding();
1085
1141
  nested_join= ptr->getNestedJoin();
1086
 
  if (nested_join->join_list.elements == 1)
 
1142
  if (nested_join->join_list.size() == 1)
1087
1143
  {
1088
 
    TableList *embedded= nested_join->join_list.head();
 
1144
    TableList *embedded= &nested_join->join_list.front();
1089
1145
    join_list->pop();
1090
1146
    embedded->setJoinList(join_list);
1091
1147
    embedded->setEmbedding(embedding);
1092
1148
    join_list->push_front(embedded);
1093
1149
    ptr= embedded;
1094
1150
  }
1095
 
  else if (nested_join->join_list.elements == 0)
 
1151
  else if (nested_join->join_list.size() == 0)
1096
1152
  {
1097
1153
    join_list->pop();
1098
1154
    ptr= NULL;                                     // return value
1117
1173
TableList *Select_Lex::nest_last_join(Session *session)
1118
1174
{
1119
1175
  TableList *ptr;
1120
 
  nested_join_st *nested_join;
 
1176
  NestedJoin *nested_join;
1121
1177
  List<TableList> *embedded_list;
1122
1178
 
1123
1179
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1124
 
                                          sizeof(nested_join_st))))
 
1180
                                          sizeof(NestedJoin))))
1125
1181
    return NULL;
1126
 
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1182
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1127
1183
  nested_join= ptr->getNestedJoin();
1128
1184
  ptr->setEmbedding(embedding);
1129
1185
  ptr->setJoinList(join_list);
1130
1186
  ptr->alias= (char*) "(nest_last_join)";
1131
1187
  embedded_list= &nested_join->join_list;
1132
 
  embedded_list->empty();
 
1188
  embedded_list->clear();
1133
1189
 
1134
1190
  for (uint32_t i=0; i < 2; i++)
1135
1191
  {
1277
1333
  fake_select_lex->include_standalone(this,
1278
1334
                                      (Select_Lex_Node**)&fake_select_lex);
1279
1335
  fake_select_lex->select_number= INT_MAX;
1280
 
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
 
1336
  fake_select_lex->parent_lex= &session_arg->lex(); /* Used in init_query. */
1281
1337
  fake_select_lex->make_empty_select();
1282
1338
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1283
1339
  fake_select_lex->select_limit= 0;
1297
1353
    */
1298
1354
    global_parameters= fake_select_lex;
1299
1355
    fake_select_lex->no_table_names_allowed= 1;
1300
 
    session_arg->lex->current_select= fake_select_lex;
 
1356
    session_arg->lex().current_select= fake_select_lex;
1301
1357
  }
1302
 
  session_arg->lex->pop_context();
 
1358
  session_arg->lex().pop_context();
1303
1359
  return(0);
1304
1360
}
1305
1361
 
1323
1379
    true   if a memory allocation error occured
1324
1380
*/
1325
1381
 
1326
 
bool
1327
 
push_new_name_resolution_context(Session *session,
1328
 
                                 TableList *left_op, TableList *right_op)
 
1382
void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
1329
1383
{
1330
 
  Name_resolution_context *on_context;
1331
 
  if (!(on_context= new (session->mem_root) Name_resolution_context))
1332
 
    return true;
 
1384
  Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
1333
1385
  on_context->init();
1334
 
  on_context->first_name_resolution_table=
1335
 
    left_op->first_leaf_for_name_resolution();
1336
 
  on_context->last_name_resolution_table=
1337
 
    right_op->last_leaf_for_name_resolution();
1338
 
  return session->lex->push_context(on_context);
 
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);
1339
1389
}
1340
1390
 
1341
1391
 
1423
1473
    1   error   ; In this case the error messege is sent to the client
1424
1474
*/
1425
1475
 
1426
 
bool check_simple_select()
 
1476
bool check_simple_select(Session* session)
1427
1477
{
1428
 
  Session *session= current_session;
1429
 
  LEX *lex= session->lex;
1430
 
  if (lex->current_select != &lex->select_lex)
 
1478
  if (session->lex().current_select != &session->lex().select_lex)
1431
1479
  {
1432
1480
    char command[80];
1433
1481
    Lex_input_stream *lip= session->m_lip;
1487
1535
bool update_precheck(Session *session, TableList *)
1488
1536
{
1489
1537
  const char *msg= 0;
1490
 
  LEX *lex= session->lex;
1491
 
  Select_Lex *select_lex= &lex->select_lex;
 
1538
  Select_Lex *select_lex= &session->lex().select_lex;
1492
1539
 
1493
 
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
 
1540
  if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
1494
1541
  {
1495
1542
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1496
1543
    return(true);
1497
1544
  }
1498
1545
 
1499
 
  if (session->lex->select_lex.table_list.elements > 1)
 
1546
  if (session->lex().select_lex.table_list.size() > 1)
1500
1547
  {
1501
 
    if (select_lex->order_list.elements)
 
1548
    if (select_lex->order_list.size())
1502
1549
      msg= "ORDER BY";
1503
1550
    else if (select_lex->select_limit)
1504
1551
      msg= "LIMIT";
1526
1573
 
1527
1574
bool insert_precheck(Session *session, TableList *)
1528
1575
{
1529
 
  LEX *lex= session->lex;
1530
 
 
1531
1576
  /*
1532
1577
    Check that we have modify privileges for the first table and
1533
1578
    select privileges for the rest
1534
1579
  */
1535
 
  if (lex->update_list.elements != lex->value_list.elements)
 
1580
  if (session->lex().update_list.size() != session->lex().value_list.size())
1536
1581
  {
1537
1582
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1538
1583
    return(true);
1559
1604
  {
1560
1605
    /* it is NOT(NOT( ... )) */
1561
1606
    Item *arg= ((Item_func *) expr)->arguments()[0];
1562
 
    enum_parsing_place place= session->lex->current_select->parsing_place;
 
1607
    enum_parsing_place place= session->lex().current_select->parsing_place;
1563
1608
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1564
1609
      return arg;
1565
1610
    /*
1608
1653
}
1609
1654
 
1610
1655
 
1611
 
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
 
1656
bool check_identifier_name(LEX_STRING *str, error_t err_code,
1612
1657
                           uint32_t max_char_length,
1613
1658
                           const char *param_for_err_msg)
1614
1659
{
1634
1679
 
1635
1680
  switch (err_code)
1636
1681
  {
1637
 
  case 0:
 
1682
  case EE_OK:
1638
1683
    break;
1639
1684
  case ER_WRONG_STRING_LENGTH:
1640
1685
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1646
1691
    assert(0);
1647
1692
    break;
1648
1693
  }
 
1694
 
1649
1695
  return true;
1650
1696
}
1651
1697
 
1674
1720
 
1675
1721
  /* Parse the query. */
1676
1722
 
1677
 
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
 
1723
  bool parse_status= base_sql_parse(session) != 0;
1678
1724
 
1679
1725
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1680
1726
 
1681
 
  assert(!mysql_parse_status || session->is_error());
 
1727
  assert(!parse_status || session->is_error());
1682
1728
 
1683
1729
  /* Reset Lex_input_stream. */
1684
1730
 
1685
1731
  session->m_lip= NULL;
1686
1732
 
1687
 
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
 
1733
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
1688
1734
 
1689
1735
  /* That's it. */
1690
1736
 
1691
 
  return mysql_parse_status || session->is_fatal_error;
 
1737
  return parse_status || session->is_fatal_error;
1692
1738
}
1693
1739
 
1694
1740
/**