~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

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
#include "drizzled/item/num.h"
 
21
#include "drizzled/abort_exception.h"
 
22
#include <drizzled/my_hash.h>
22
23
#include <drizzled/error.h>
23
24
#include <drizzled/nested_join.h>
24
25
#include <drizzled/query_id.h>
25
 
#include <drizzled/transaction_services.h>
 
26
#include "drizzled/transaction_services.h"
26
27
#include <drizzled/sql_parse.h>
27
28
#include <drizzled/data_home.h>
28
29
#include <drizzled/sql_base.h>
29
30
#include <drizzled/show.h>
 
31
#include <drizzled/db.h>
30
32
#include <drizzled/function/time/unix_timestamp.h>
31
33
#include <drizzled/function/get_system_var.h>
32
34
#include <drizzled/item/cmpfunc.h>
39
41
#include <drizzled/plugin/client.h>
40
42
#include <drizzled/statement.h>
41
43
#include <drizzled/statement/alter_table.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>
 
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"
62
55
 
63
56
#include <limits.h>
64
57
 
65
58
#include <bitset>
66
59
#include <algorithm>
67
60
#include <boost/date_time.hpp>
68
 
#include <drizzled/internal/my_sys.h>
 
61
#include "drizzled/internal/my_sys.h"
69
62
 
70
63
using namespace std;
71
64
 
72
 
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
 
65
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
73
66
 
74
 
namespace drizzled {
 
67
namespace drizzled
 
68
{
75
69
 
76
70
/* Prototypes */
77
 
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
 
71
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
78
72
static bool parse_sql(Session *session, Lex_input_stream *lip);
79
 
void parse(Session&, const char *inBuf, uint32_t length);
 
73
void parse(Session *session, const char *inBuf, uint32_t length);
80
74
 
81
75
/**
82
76
  @defgroup Runtime_Environment Runtime Environment
84
78
*/
85
79
 
86
80
extern size_t my_thread_stack_size;
87
 
extern const charset_info_st *character_set_filesystem;
 
81
extern const CHARSET_INFO *character_set_filesystem;
88
82
 
89
83
namespace
90
84
{
97
91
  "Shutdown",
98
92
  "Connect",
99
93
  "Ping",
100
 
  "Kill",
101
94
  "Error"  // Last command number
102
95
};
103
96
 
174
167
                         can be zero.
175
168
 
176
169
  @todo
177
 
    set session->lex().sql_command to SQLCOM_END here.
 
170
    set session->lex->sql_command to SQLCOM_END here.
178
171
  @todo
179
172
    The following has to be changed to an 8 byte integer
180
173
 
184
177
    1   request of thread shutdown, i. e. if command is
185
178
        COM_QUIT/COM_SHUTDOWN
186
179
*/
187
 
bool dispatch_command(enum_server_command command, Session *session,
 
180
bool dispatch_command(enum enum_server_command command, Session *session,
188
181
                      char* packet, uint32_t packet_length)
189
182
{
190
183
  bool error= 0;
193
186
  DRIZZLE_COMMAND_START(session->thread_id, command);
194
187
 
195
188
  session->command= command;
196
 
  session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
197
 
  session->times.set_time();
 
189
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
190
  session->set_time();
198
191
  session->setQueryId(query_id.value());
199
192
 
200
193
  switch( command ) {
207
200
    query_id.next();
208
201
  }
209
202
 
210
 
  /* @todo set session->lex().sql_command to SQLCOM_END here */
 
203
  /* @todo set session->lex->sql_command to SQLCOM_END here */
211
204
 
212
205
  plugin::Logging::preDo(session);
213
206
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
217
210
 
218
211
  session->server_status&=
219
212
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
220
 
 
221
213
  switch (command) {
222
214
  case COM_INIT_DB:
223
215
  {
226
218
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
227
219
      break;
228
220
    }
229
 
    if (not schema::change(*session, identifier::Schema(string(packet, packet_length))))
 
221
 
 
222
    string tmp(packet, packet_length);
 
223
 
 
224
    identifier::Schema identifier(tmp);
 
225
 
 
226
    if (not change_db(session, identifier))
230
227
    {
231
228
      session->my_ok();
232
229
    }
234
231
  }
235
232
  case COM_QUERY:
236
233
  {
237
 
    session->readAndStoreQuery(packet, packet_length);
238
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(), session->thread_id, session->schema()->c_str());
239
 
    parse(*session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
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
 
240
242
    break;
241
243
  }
242
244
  case COM_QUIT:
243
245
    /* We don't calculate statistics for this command */
244
 
    session->main_da().disable_status();              // Don't send anything back
245
 
    error= true;                                        // End server
 
246
    session->main_da.disable_status();              // Don't send anything back
 
247
    error=true;                                 // End server
246
248
    break;
247
 
  case COM_KILL:
248
 
    {
249
 
      if (packet_length != 4)
250
 
      {
251
 
        my_error(ER_NO_SUCH_THREAD, MYF(0), 0);
252
 
        break;
253
 
      }
254
 
      else
255
 
      {
256
 
        uint32_t kill_id;
257
 
        memcpy(&kill_id, packet, sizeof(uint32_t));
258
 
 
259
 
        kill_id= ntohl(kill_id);
260
 
        (void)drizzled::kill(*session->user(), kill_id, true);
261
 
      }
262
 
      session->my_ok();
263
 
      break;
264
 
    }
265
249
  case COM_SHUTDOWN:
266
250
  {
267
251
    session->status_var.com_other++;
268
252
    session->my_eof();
269
253
    session->close_thread_tables();                     // Free before kill
270
254
    kill_drizzle();
271
 
    error= true;
 
255
    error=true;
272
256
    break;
273
257
  }
274
258
  case COM_PING:
284
268
  }
285
269
 
286
270
  /* If commit fails, we should be able to reset the OK status. */
287
 
  session->main_da().can_overwrite_status= true;
 
271
  session->main_da.can_overwrite_status= true;
288
272
  TransactionServices &transaction_services= TransactionServices::singleton();
289
273
  transaction_services.autocommitOrRollback(*session, session->is_error());
290
 
  session->main_da().can_overwrite_status= false;
 
274
  session->main_da.can_overwrite_status= false;
291
275
 
292
276
  session->transaction.stmt.reset();
293
277
 
295
279
  /* report error issued during command execution */
296
280
  if (session->killed_errno())
297
281
  {
298
 
    if (! session->main_da().is_set())
 
282
    if (! session->main_da.is_set())
299
283
      session->send_kill_message();
300
284
  }
301
285
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
305
289
  }
306
290
 
307
291
  /* Can not be true, but do not take chances in production. */
308
 
  assert(! session->main_da().is_sent);
 
292
  assert(! session->main_da.is_sent);
309
293
 
310
 
  switch (session->main_da().status())
 
294
  switch (session->main_da.status())
311
295
  {
312
296
  case Diagnostics_area::DA_ERROR:
313
297
    /* The query failed, send error to log and abort bootstrap. */
314
 
    session->getClient()->sendError(session->main_da().sql_errno(),
315
 
                               session->main_da().message());
 
298
    session->getClient()->sendError(session->main_da.sql_errno(),
 
299
                               session->main_da.message());
316
300
    break;
317
301
 
318
302
  case Diagnostics_area::DA_EOF:
332
316
    break;
333
317
  }
334
318
 
335
 
  session->main_da().is_sent= true;
 
319
  session->main_da.is_sent= true;
336
320
 
337
321
  session->set_proc_info("closing tables");
338
322
  /* Free tables */
402
386
  session->make_lex_string(&db, "data_dictionary", sizeof("data_dictionary"), false);
403
387
  session->make_lex_string(&table, schema_table_name, false);
404
388
 
405
 
  if (not sel->add_table_to_list(session, new Table_ident(db, table), NULL, table_options, TL_READ))
 
389
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
 
390
                               NULL, table_options, TL_READ))
406
391
  {
407
392
    return true;
408
393
  }
409
394
  return false;
410
395
}
411
396
 
412
 
int prepare_new_schema_table(Session *session, LEX& lex,
 
397
int prepare_new_schema_table(Session *session, LEX *lex,
413
398
                             const string& schema_table_name)
414
399
{
415
400
  Select_Lex *schema_select_lex= NULL;
416
401
 
417
 
  Select_Lex *select_lex= lex.current_select;
 
402
  Select_Lex *select_lex= lex->current_select;
418
403
  assert(select_lex);
419
404
  if (_schema_select(session, select_lex, schema_table_name))
420
405
  {
460
445
static int execute_command(Session *session)
461
446
{
462
447
  bool res= false;
463
 
 
 
448
  LEX  *lex= session->lex;
464
449
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
465
 
  Select_Lex *select_lex= &session->lex().select_lex;
466
 
 
 
450
  Select_Lex *select_lex= &lex->select_lex;
467
451
  /* list of all tables in query */
468
452
  TableList *all_tables;
469
453
 
482
466
    assert(first_table == all_tables);
483
467
    assert(first_table == all_tables && first_table != 0);
484
468
  */
485
 
  session->lex().first_lists_tables_same();
486
 
 
 
469
  lex->first_lists_tables_same();
487
470
  /* should be assigned after making first tables same */
488
 
  all_tables= session->lex().query_tables;
489
 
 
 
471
  all_tables= lex->query_tables;
490
472
  /* set context for commands which do not use setup_tables */
491
 
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
 
473
  select_lex->
 
474
    context.resolve_in_table_list_only((TableList*)select_lex->
 
475
                                       table_list.first);
492
476
 
493
477
  /*
494
478
    Reset warning count for each query that uses tables
497
481
    variables, but for now this is probably good enough.
498
482
    Don't reset warnings when executing a stored routine.
499
483
  */
500
 
  if (all_tables || ! session->lex().is_single_level_stmt())
 
484
  if (all_tables || ! lex->is_single_level_stmt())
501
485
  {
502
486
    drizzle_reset_errors(session, 0);
503
487
  }
506
490
 
507
491
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
508
492
      && ! session->inTransaction()
509
 
      && session->lex().statement->isTransactional())
 
493
      && lex->statement->isTransactional())
510
494
  {
511
495
    if (session->startTransaction() == false)
512
496
    {
516
500
  }
517
501
 
518
502
  /* now we are ready to execute the statement */
519
 
  res= session->lex().statement->execute();
 
503
  res= lex->statement->execute();
520
504
  session->set_proc_info("query end");
521
505
  /*
522
506
    The return value for ROW_COUNT() is "implementation dependent" if the
524
508
    wants. We also keep the last value in case of SQLCOM_CALL or
525
509
    SQLCOM_EXECUTE.
526
510
  */
527
 
  if (! (sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
511
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
528
512
  {
529
513
    session->row_count_func= -1;
530
514
  }
533
517
}
534
518
bool execute_sqlcom_select(Session *session, TableList *all_tables)
535
519
{
536
 
  LEX   *lex= &session->lex();
 
520
  LEX   *lex= session->lex;
537
521
  select_result *result=lex->result;
538
522
  bool res= false;
539
523
  /* assign global limit variable if limit is not given */
570
554
        return true;
571
555
      session->send_explain_fields(result);
572
556
      optimizer::ExplainPlan planner;
573
 
      res= planner.explainUnion(session, &session->lex().unit, result);
 
557
      res= planner.explainUnion(session, &session->lex->unit, result);
574
558
      if (lex->describe & DESCRIBE_EXTENDED)
575
559
      {
576
560
        char buff[1024];
577
561
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
578
562
        str.length(0);
579
 
        session->lex().unit.print(&str);
 
563
        session->lex->unit.print(&str, QT_ORDINARY);
580
564
        str.append('\0');
581
565
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
582
566
                     ER_YES, str.ptr());
585
569
        result->abort();
586
570
      else
587
571
        result->send_eof();
588
 
 
589
572
      delete result;
590
573
    }
591
574
    else
594
577
        return true;
595
578
 
596
579
      /* Init the Query Cache plugin */
597
 
      plugin::QueryCache::prepareResultset(session);
 
580
      plugin::QueryCache::prepareResultset(session); 
598
581
      res= handle_select(session, lex, result, 0);
599
582
      /* Send the Resultset to the cache */
600
 
      plugin::QueryCache::setResultset(session);
 
583
      plugin::QueryCache::setResultset(session); 
601
584
 
602
585
      if (result != lex->result)
603
586
        delete result;
610
593
#define MY_YACC_INIT 1000                       // Start with big alloc
611
594
#define MY_YACC_MAX  32000                      // Because of 'short'
612
595
 
613
 
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
 
596
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
614
597
{
615
 
  LEX   *lex= &current_session->lex();
 
598
  LEX   *lex= current_session->lex;
616
599
  ulong old_info=0;
617
600
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
618
601
    return 1;
635
618
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
636
619
  }
637
620
  *yyss=(short*) lex->yacc_yyss;
638
 
  *yyvs=(ParserType*) lex->yacc_yyvs;
 
621
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
639
622
  return 0;
640
623
}
641
624
 
739
722
 
740
723
void create_select_for_variable(Session *session, const char *var_name)
741
724
{
742
 
  LEX& lex= session->lex();
743
 
  init_select(&lex);
744
 
  lex.sql_command= SQLCOM_SELECT;
745
 
  LEX_STRING tmp;
 
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;
746
734
  tmp.str= (char*) var_name;
747
735
  tmp.length=strlen(var_name);
748
 
  LEX_STRING null_lex_string;
749
736
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
750
737
  /*
751
738
    We set the name of Item to @@session.var_name because that then is used
752
739
    as the column name in the output.
753
740
  */
754
 
  if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string))
 
741
  if ((var= get_system_var(session, OPT_SESSION, tmp, null_lex_string)))
755
742
  {
756
 
    char buff[MAX_SYS_VAR_LENGTH*2+4+8];
757
 
    char *end= buff;
758
743
    end+= snprintf(buff, sizeof(buff), "@@session.%s", var_name);
759
744
    var->set_name(buff, end-buff, system_charset_info);
760
745
    session->add_item_to_list(var);
770
755
  @param       length  Length of the query text
771
756
*/
772
757
 
773
 
void parse(Session& session, const char *inBuf, uint32_t length)
 
758
void parse(Session *session, const char *inBuf, uint32_t length)
774
759
{
775
 
  session.lex().start(&session);
776
 
  session.reset_for_next_command();
 
760
  session->lex->start(session);
 
761
 
 
762
  session->reset_for_next_command();
777
763
  /* Check if the Query is Cached if and return true if yes
778
764
   * TODO the plugin has to make sure that the query is cacheble
779
765
   * by setting the query_safe_cache param to TRUE
780
766
   */
781
 
  if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
782
 
      return;
783
 
  Lex_input_stream lip(&session, inBuf, length);
784
 
  if (parse_sql(&session, &lip))
785
 
    assert(session.is_error());
786
 
  else if (not session.is_error())
787
 
  {
788
 
    DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id,
789
 
                             const_cast<const char *>(session.schema()->c_str()));
790
 
    // Implement Views here --Brian
791
 
    /* Actually execute the query */
792
 
    try
793
 
    {
794
 
      execute_command(&session);
795
 
    }
796
 
    catch (...)
797
 
    {
798
 
      // Just try to catch any random failures that could have come
799
 
      // during execution.
800
 
      DRIZZLE_ABORT;
801
 
    }
802
 
    DRIZZLE_QUERY_EXEC_DONE(0);
803
 
  }
804
 
  session.lex().unit.cleanup();
805
 
  session.set_proc_info("freeing items");
806
 
  session.end_statement();
807
 
  session.cleanup_after_query();
808
 
  session.times.set_end_timer(session);
 
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();
809
812
}
810
813
 
811
814
 
 
815
 
812
816
/**
813
817
  Store field definition for create.
814
818
 
823
827
                       Item *default_value, Item *on_update_value,
824
828
                       LEX_STRING *comment,
825
829
                       char *change,
826
 
                       List<String> *interval_list, const charset_info_st * const cs)
 
830
                       List<String> *interval_list, const CHARSET_INFO * const cs)
827
831
{
828
832
  register CreateField *new_field;
829
 
  LEX  *lex= &session->lex();
 
833
  LEX  *lex= session->lex;
830
834
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
831
835
 
832
836
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
840
844
                      &default_key_create_info,
841
845
                      0, lex->col_list);
842
846
    statement->alter_info.key_list.push_back(key);
843
 
    lex->col_list.clear();
 
847
    lex->col_list.empty();
844
848
  }
845
849
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
846
850
  {
850
854
                 &default_key_create_info, 0,
851
855
                 lex->col_list);
852
856
    statement->alter_info.key_list.push_back(key);
853
 
    lex->col_list.clear();
 
857
    lex->col_list.empty();
854
858
  }
855
859
 
856
860
  if (default_value)
891
895
    return true;
892
896
  }
893
897
 
894
 
  if (!(new_field= new CreateField())
895
 
      || new_field->init(session, field_name->str, type, length, decimals,
896
 
                         type_modifier, comment, change, interval_list,
897
 
                         cs, 0, column_format)
898
 
      || new_field->setDefaultValue(default_value, on_update_value))
 
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))
899
902
    return true;
900
903
 
901
904
  statement->alter_info.create_list.push_back(new_field);
932
935
                                         List<Index_hint> *index_hints_arg,
933
936
                                         LEX_STRING *option)
934
937
{
 
938
  TableList *ptr;
935
939
  TableList *previous_table_ref; /* The table preceding the current one. */
936
940
  char *alias_str;
937
 
  LEX *lex= &session->lex();
 
941
  LEX *lex= session->lex;
938
942
 
939
943
  if (!table)
940
944
    return NULL;                                // End of memory
951
955
    my_casedn_str(files_charset_info, table->db.str);
952
956
 
953
957
    identifier::Schema schema_identifier(string(table->db.str));
954
 
    if (not schema::check(*session, schema_identifier))
 
958
    if (not check_db_name(session, schema_identifier))
955
959
    {
956
960
 
957
961
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
970
974
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
971
975
      return NULL;
972
976
  }
973
 
  TableList *ptr = (TableList *) session->calloc(sizeof(TableList));
 
977
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
 
978
    return NULL;
974
979
 
975
980
  if (table->db.str)
976
981
  {
1011
1016
    }
1012
1017
  }
1013
1018
  /* Store the table reference preceding the current one. */
1014
 
  if (table_list.size() > 0)
 
1019
  if (table_list.elements > 0)
1015
1020
  {
1016
1021
    /*
1017
1022
      table_list.next points to the last inserted TableList->next_local'
1080
1085
  ptr->alias= (char*) "(nested_join)";
1081
1086
  embedding= ptr;
1082
1087
  join_list= &nested_join->join_list;
1083
 
  join_list->clear();
 
1088
  join_list->empty();
1084
1089
  return false;
1085
1090
}
1086
1091
 
1109
1114
  join_list= ptr->getJoinList();
1110
1115
  embedding= ptr->getEmbedding();
1111
1116
  nested_join= ptr->getNestedJoin();
1112
 
  if (nested_join->join_list.size() == 1)
 
1117
  if (nested_join->join_list.elements == 1)
1113
1118
  {
1114
 
    TableList *embedded= &nested_join->join_list.front();
 
1119
    TableList *embedded= nested_join->join_list.head();
1115
1120
    join_list->pop();
1116
1121
    embedded->setJoinList(join_list);
1117
1122
    embedded->setEmbedding(embedding);
1118
1123
    join_list->push_front(embedded);
1119
1124
    ptr= embedded;
1120
1125
  }
1121
 
  else if (nested_join->join_list.size() == 0)
 
1126
  else if (nested_join->join_list.elements == 0)
1122
1127
  {
1123
1128
    join_list->pop();
1124
1129
    ptr= NULL;                                     // return value
1155
1160
  ptr->setJoinList(join_list);
1156
1161
  ptr->alias= (char*) "(nest_last_join)";
1157
1162
  embedded_list= &nested_join->join_list;
1158
 
  embedded_list->clear();
 
1163
  embedded_list->empty();
1159
1164
 
1160
1165
  for (uint32_t i=0; i < 2; i++)
1161
1166
  {
1303
1308
  fake_select_lex->include_standalone(this,
1304
1309
                                      (Select_Lex_Node**)&fake_select_lex);
1305
1310
  fake_select_lex->select_number= INT_MAX;
1306
 
  fake_select_lex->parent_lex= &session_arg->lex(); /* Used in init_query. */
 
1311
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1307
1312
  fake_select_lex->make_empty_select();
1308
1313
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1309
1314
  fake_select_lex->select_limit= 0;
1323
1328
    */
1324
1329
    global_parameters= fake_select_lex;
1325
1330
    fake_select_lex->no_table_names_allowed= 1;
1326
 
    session_arg->lex().current_select= fake_select_lex;
 
1331
    session_arg->lex->current_select= fake_select_lex;
1327
1332
  }
1328
 
  session_arg->lex().pop_context();
 
1333
  session_arg->lex->pop_context();
1329
1334
  return(0);
1330
1335
}
1331
1336
 
1349
1354
    true   if a memory allocation error occured
1350
1355
*/
1351
1356
 
1352
 
void push_new_name_resolution_context(Session& session, TableList& left_op, TableList& right_op)
 
1357
bool
 
1358
push_new_name_resolution_context(Session *session,
 
1359
                                 TableList *left_op, TableList *right_op)
1353
1360
{
1354
 
  Name_resolution_context *on_context= new (session.mem_root) Name_resolution_context;
 
1361
  Name_resolution_context *on_context;
 
1362
  if (!(on_context= new (session->mem_root) Name_resolution_context))
 
1363
    return true;
1355
1364
  on_context->init();
1356
 
  on_context->first_name_resolution_table= left_op.first_leaf_for_name_resolution();
1357
 
  on_context->last_name_resolution_table= right_op.last_leaf_for_name_resolution();
1358
 
  session.lex().push_context(on_context);
 
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);
1359
1370
}
1360
1371
 
1361
1372
 
1443
1454
    1   error   ; In this case the error messege is sent to the client
1444
1455
*/
1445
1456
 
1446
 
bool check_simple_select(Session* session)
 
1457
bool check_simple_select(Session::pointer session)
1447
1458
{
1448
 
  if (session->lex().current_select != &session->lex().select_lex)
 
1459
  LEX *lex= session->lex;
 
1460
  if (lex->current_select != &lex->select_lex)
1449
1461
  {
1450
1462
    char command[80];
1451
1463
    Lex_input_stream *lip= session->m_lip;
1505
1517
bool update_precheck(Session *session, TableList *)
1506
1518
{
1507
1519
  const char *msg= 0;
1508
 
  Select_Lex *select_lex= &session->lex().select_lex;
 
1520
  LEX *lex= session->lex;
 
1521
  Select_Lex *select_lex= &lex->select_lex;
1509
1522
 
1510
 
  if (session->lex().select_lex.item_list.size() != session->lex().value_list.size())
 
1523
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1511
1524
  {
1512
1525
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1513
1526
    return(true);
1514
1527
  }
1515
1528
 
1516
 
  if (session->lex().select_lex.table_list.size() > 1)
 
1529
  if (session->lex->select_lex.table_list.elements > 1)
1517
1530
  {
1518
 
    if (select_lex->order_list.size())
 
1531
    if (select_lex->order_list.elements)
1519
1532
      msg= "ORDER BY";
1520
1533
    else if (select_lex->select_limit)
1521
1534
      msg= "LIMIT";
1543
1556
 
1544
1557
bool insert_precheck(Session *session, TableList *)
1545
1558
{
 
1559
  LEX *lex= session->lex;
 
1560
 
1546
1561
  /*
1547
1562
    Check that we have modify privileges for the first table and
1548
1563
    select privileges for the rest
1549
1564
  */
1550
 
  if (session->lex().update_list.size() != session->lex().value_list.size())
 
1565
  if (lex->update_list.elements != lex->value_list.elements)
1551
1566
  {
1552
1567
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1553
1568
    return(true);
1574
1589
  {
1575
1590
    /* it is NOT(NOT( ... )) */
1576
1591
    Item *arg= ((Item_func *) expr)->arguments()[0];
1577
 
    enum_parsing_place place= session->lex().current_select->parsing_place;
 
1592
    enum_parsing_place place= session->lex->current_select->parsing_place;
1578
1593
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1579
1594
      return arg;
1580
1595
    /*
1607
1622
 
1608
1623
 
1609
1624
bool check_string_char_length(LEX_STRING *str, const char *err_msg,
1610
 
                              uint32_t max_char_length, const charset_info_st * const cs,
 
1625
                              uint32_t max_char_length, const CHARSET_INFO * const cs,
1611
1626
                              bool no_error)
1612
1627
{
1613
1628
  int well_formed_error;
1632
1647
    so they should be prohibited until such support is done.
1633
1648
    This is why we use the 3-byte utf8 to check well-formedness here.
1634
1649
  */
1635
 
  const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
 
1650
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
1636
1651
 
1637
1652
  int well_formed_error;
1638
1653
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1690
1705
 
1691
1706
  /* Parse the query. */
1692
1707
 
1693
 
  bool parse_status= base_sql_parse(session) != 0;
 
1708
  bool parse_status= DRIZZLEparse(session) != 0;
1694
1709
 
1695
1710
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1696
1711