~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-06 15:49:49 UTC
  • mto: (2226.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2227.
  • Revision ID: olafvdspek@gmail.com-20110306154949-zzso0l15mbwi60xb
Provide drizzle/drizzle.h

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