~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Marisa Plumb
  • Date: 2010-12-04 02:38:29 UTC
  • mto: This revision was merged to the branch mainline in revision 1984.
  • Revision ID: marisa.plumb@gmail.com-20101204023829-2khzxh30wxi256db
updates to a few sql docs 

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
#include <config.h>
 
16
#include "config.h"
17
17
 
18
18
#define DRIZZLE_LEX 1
19
19
 
20
 
#include <drizzled/item/num.h>
21
 
#include <drizzled/abort_exception.h>
22
20
#include <drizzled/my_hash.h>
23
21
#include <drizzled/error.h>
24
22
#include <drizzled/nested_join.h>
25
23
#include <drizzled/query_id.h>
26
 
#include <drizzled/transaction_services.h>
 
24
#include "drizzled/transaction_services.h"
27
25
#include <drizzled/sql_parse.h>
28
26
#include <drizzled/data_home.h>
29
27
#include <drizzled/sql_base.h>
30
28
#include <drizzled/show.h>
 
29
#include <drizzled/db.h>
31
30
#include <drizzled/function/time/unix_timestamp.h>
32
31
#include <drizzled/function/get_system_var.h>
33
32
#include <drizzled/item/cmpfunc.h>
34
33
#include <drizzled/item/null.h>
35
34
#include <drizzled/session.h>
36
 
#include <drizzled/session/cache.h>
37
35
#include <drizzled/sql_load.h>
38
36
#include <drizzled/lock.h>
39
37
#include <drizzled/select_send.h>
40
38
#include <drizzled/plugin/client.h>
41
39
#include <drizzled/statement.h>
42
40
#include <drizzled/statement/alter_table.h>
43
 
#include <drizzled/probes.h>
44
 
#include <drizzled/global_charset_info.h>
45
 
 
46
 
#include <drizzled/plugin/logging.h>
47
 
#include <drizzled/plugin/query_rewrite.h>
48
 
#include <drizzled/plugin/query_cache.h>
49
 
#include <drizzled/plugin/authorization.h>
50
 
#include <drizzled/optimizer/explain_plan.h>
51
 
#include <drizzled/pthread_globals.h>
52
 
#include <drizzled/plugin/event_observer.h>
53
 
#include <drizzled/visibility.h>
54
 
 
55
 
#include <drizzled/schema.h>
 
41
#include "drizzled/probes.h"
 
42
#include "drizzled/session_list.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"
56
52
 
57
53
#include <limits.h>
58
54
 
59
55
#include <bitset>
60
56
#include <algorithm>
61
57
#include <boost/date_time.hpp>
62
 
#include <drizzled/internal/my_sys.h>
 
58
#include "drizzled/internal/my_sys.h"
63
59
 
64
60
using namespace std;
65
61
 
66
 
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
 
62
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
67
63
 
68
64
namespace drizzled
69
65
{
70
66
 
71
67
/* Prototypes */
72
 
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
 
68
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
73
69
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
 
void parse(Session *session, const char *inBuf, uint32_t length);
 
70
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
75
71
 
76
72
/**
77
73
  @defgroup Runtime_Environment Runtime Environment
81
77
extern size_t my_thread_stack_size;
82
78
extern const CHARSET_INFO *character_set_filesystem;
83
79
 
84
 
namespace
85
 
{
86
 
 
87
 
static const std::string command_name[COM_END+1]={
88
 
  "Sleep",
89
 
  "Quit",
90
 
  "Init DB",
91
 
  "Query",
92
 
  "Shutdown",
93
 
  "Connect",
94
 
  "Ping",
95
 
  "Error"  // Last command number
 
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
96
89
};
97
90
 
98
 
}
99
 
 
100
91
const char *xa_state_names[]={
101
92
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
102
93
};
115
106
*/
116
107
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
117
108
 
118
 
const std::string &getCommandName(const enum_server_command& command)
119
 
{
120
 
  return command_name[command];
121
 
}
122
 
 
123
109
void init_update_queries(void)
124
110
{
125
111
  uint32_t x;
168
154
                         can be zero.
169
155
 
170
156
  @todo
171
 
    set session->getLex()->sql_command to SQLCOM_END here.
 
157
    set session->lex->sql_command to SQLCOM_END here.
172
158
  @todo
173
159
    The following has to be changed to an 8 byte integer
174
160
 
187
173
  DRIZZLE_COMMAND_START(session->thread_id, command);
188
174
 
189
175
  session->command= command;
190
 
  session->getLex()->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
176
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
191
177
  session->set_time();
192
178
  session->setQueryId(query_id.value());
193
179
 
201
187
    query_id.next();
202
188
  }
203
189
 
204
 
  /* @todo set session->getLex()->sql_command to SQLCOM_END here */
 
190
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
205
191
 
206
192
  plugin::Logging::preDo(session);
207
193
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
222
208
 
223
209
    string tmp(packet, packet_length);
224
210
 
225
 
    identifier::Schema identifier(tmp);
 
211
    SchemaIdentifier identifier(tmp);
226
212
 
227
 
    if (not schema::change(*session, identifier))
 
213
    if (not mysql_change_db(session, identifier))
228
214
    {
229
215
      session->my_ok();
230
216
    }
236
222
      break;                                    // fatal error is set
237
223
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
238
224
                        session->thread_id,
239
 
                        const_cast<const char *>(session->schema()->c_str()));
 
225
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
240
226
 
241
 
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
227
    plugin::QueryRewriter::rewriteQuery(session->getSchema(), session->getQueryString());
 
228
    mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
242
229
 
243
230
    break;
244
231
  }
271
258
  /* If commit fails, we should be able to reset the OK status. */
272
259
  session->main_da.can_overwrite_status= true;
273
260
  TransactionServices &transaction_services= TransactionServices::singleton();
274
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
261
  transaction_services.autocommitOrRollback(session, session->is_error());
275
262
  session->main_da.can_overwrite_status= false;
276
263
 
277
264
  session->transaction.stmt.reset();
296
283
  {
297
284
  case Diagnostics_area::DA_ERROR:
298
285
    /* The query failed, send error to log and abort bootstrap. */
299
 
    session->getClient()->sendError(session->main_da.sql_errno(),
 
286
    session->client->sendError(session->main_da.sql_errno(),
300
287
                               session->main_da.message());
301
288
    break;
302
289
 
303
290
  case Diagnostics_area::DA_EOF:
304
 
    session->getClient()->sendEOF();
 
291
    session->client->sendEOF();
305
292
    break;
306
293
 
307
294
  case Diagnostics_area::DA_OK:
308
 
    session->getClient()->sendOK();
 
295
    session->client->sendOK();
309
296
    break;
310
297
 
311
298
  case Diagnostics_area::DA_DISABLED:
313
300
 
314
301
  case Diagnostics_area::DA_EMPTY:
315
302
  default:
316
 
    session->getClient()->sendOK();
 
303
    session->client->sendOK();
317
304
    break;
318
305
  }
319
306
 
443
430
    true        Error
444
431
*/
445
432
 
446
 
static int execute_command(Session *session)
 
433
static int mysql_execute_command(Session *session)
447
434
{
448
435
  bool res= false;
449
 
 
 
436
  LEX  *lex= session->lex;
450
437
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
451
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
452
 
 
 
438
  Select_Lex *select_lex= &lex->select_lex;
453
439
  /* list of all tables in query */
454
440
  TableList *all_tables;
455
441
 
468
454
    assert(first_table == all_tables);
469
455
    assert(first_table == all_tables && first_table != 0);
470
456
  */
471
 
  session->getLex()->first_lists_tables_same();
472
 
 
 
457
  lex->first_lists_tables_same();
473
458
  /* should be assigned after making first tables same */
474
 
  all_tables= session->getLex()->query_tables;
475
 
 
 
459
  all_tables= lex->query_tables;
476
460
  /* set context for commands which do not use setup_tables */
477
 
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
 
461
  select_lex->
 
462
    context.resolve_in_table_list_only((TableList*)select_lex->
 
463
                                       table_list.first);
478
464
 
479
465
  /*
480
466
    Reset warning count for each query that uses tables
483
469
    variables, but for now this is probably good enough.
484
470
    Don't reset warnings when executing a stored routine.
485
471
  */
486
 
  if (all_tables || ! session->getLex()->is_single_level_stmt())
 
472
  if (all_tables || ! lex->is_single_level_stmt())
487
473
  {
488
474
    drizzle_reset_errors(session, 0);
489
475
  }
490
476
 
491
477
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
492
478
 
493
 
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
494
 
      && ! session->inTransaction()
495
 
      && session->getLex()->statement->isTransactional())
496
 
  {
497
 
    if (session->startTransaction() == false)
498
 
    {
499
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
500
 
      return true;
501
 
    }
502
 
  }
503
 
 
504
479
  /* now we are ready to execute the statement */
505
 
  res= session->getLex()->statement->execute();
 
480
  res= lex->statement->execute();
506
481
  session->set_proc_info("query end");
507
482
  /*
508
483
    The return value for ROW_COUNT() is "implementation dependent" if the
510
485
    wants. We also keep the last value in case of SQLCOM_CALL or
511
486
    SQLCOM_EXECUTE.
512
487
  */
513
 
  if (! (sql_command_flags[session->getLex()->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
488
  if (! (sql_command_flags[lex->sql_command].test(CF_BIT_HAS_ROW_COUNT)))
514
489
  {
515
490
    session->row_count_func= -1;
516
491
  }
519
494
}
520
495
bool execute_sqlcom_select(Session *session, TableList *all_tables)
521
496
{
522
 
  LEX   *lex= session->getLex();
 
497
  LEX   *lex= session->lex;
523
498
  select_result *result=lex->result;
524
499
  bool res= false;
525
500
  /* assign global limit variable if limit is not given */
529
504
      param->select_limit=
530
505
        new Item_int((uint64_t) session->variables.select_limit);
531
506
  }
532
 
 
533
 
  if (all_tables
534
 
      && ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
535
 
      && ! session->inTransaction()
536
 
      && ! lex->statement->isShow())
537
 
  {
538
 
    if (session->startTransaction() == false)
539
 
    {
540
 
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
541
 
      return true;
542
 
    }
543
 
  }
544
 
 
545
507
  if (not (res= session->openTablesLock(all_tables)))
546
508
  {
547
509
    if (lex->describe)
556
518
        return true;
557
519
      session->send_explain_fields(result);
558
520
      optimizer::ExplainPlan planner;
559
 
      res= planner.explainUnion(session, &session->getLex()->unit, result);
 
521
      res= planner.explainUnion(session, &session->lex->unit, result);
560
522
      if (lex->describe & DESCRIBE_EXTENDED)
561
523
      {
562
524
        char buff[1024];
563
525
        String str(buff,(uint32_t) sizeof(buff), system_charset_info);
564
526
        str.length(0);
565
 
        session->getLex()->unit.print(&str, QT_ORDINARY);
 
527
        session->lex->unit.print(&str, QT_ORDINARY);
566
528
        str.append('\0');
567
529
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
568
530
                     ER_YES, str.ptr());
571
533
        result->abort();
572
534
      else
573
535
        result->send_eof();
574
 
 
575
536
      delete result;
576
537
    }
577
538
    else
596
557
#define MY_YACC_INIT 1000                       // Start with big alloc
597
558
#define MY_YACC_MAX  32000                      // Because of 'short'
598
559
 
599
 
bool my_yyoverflow(short **yyss, ParserType **yyvs, ulong *yystacksize)
 
560
bool my_yyoverflow(short **yyss, YYSTYPE **yyvs, ulong *yystacksize)
600
561
{
601
 
  LEX   *lex= current_session->getLex();
 
562
  LEX   *lex= current_session->lex;
602
563
  ulong old_info=0;
603
564
  if ((uint32_t) *yystacksize >= MY_YACC_MAX)
604
565
    return 1;
621
582
    memcpy(lex->yacc_yyvs, *yyvs, old_info*sizeof(**yyvs));
622
583
  }
623
584
  *yyss=(short*) lex->yacc_yyss;
624
 
  *yyvs=(ParserType*) lex->yacc_yyvs;
 
585
  *yyvs=(YYSTYPE*) lex->yacc_yyvs;
625
586
  return 0;
626
587
}
627
588
 
628
589
 
629
590
void
630
 
init_select(LEX *lex)
 
591
mysql_init_select(LEX *lex)
631
592
{
632
593
  Select_Lex *select_lex= lex->current_select;
633
594
  select_lex->init_select();
641
602
 
642
603
 
643
604
bool
644
 
new_select(LEX *lex, bool move_down)
 
605
mysql_new_select(LEX *lex, bool move_down)
645
606
{
646
607
  Select_Lex *select_lex;
647
608
  Session *session= lex->session;
648
609
 
649
610
  if (!(select_lex= new (session->mem_root) Select_Lex()))
650
 
    return true;
651
 
 
 
611
    return(1);
652
612
  select_lex->select_number= ++session->select_number;
653
613
  select_lex->parent_lex= lex; /* Used in init_query. */
654
614
  select_lex->init_query();
655
615
  select_lex->init_select();
656
616
  lex->nest_level++;
657
 
 
658
617
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
659
618
  {
660
619
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
661
620
    return(1);
662
621
  }
663
 
 
664
622
  select_lex->nest_level= lex->nest_level;
665
623
  if (move_down)
666
624
  {
688
646
    if (lex->current_select->order_list.first && !lex->current_select->braces)
689
647
    {
690
648
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
691
 
      return true;
 
649
      return(1);
692
650
    }
693
 
 
694
651
    select_lex->include_neighbour(lex->current_select);
695
652
    Select_Lex_Unit *unit= select_lex->master_unit();
696
 
 
697
 
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
698
 
      return true;
699
 
 
 
653
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
654
      return(1);
700
655
    select_lex->context.outer_context=
701
656
                unit->first_select()->context.outer_context;
702
657
  }
709
664
    list
710
665
  */
711
666
  select_lex->context.resolve_in_select_list= true;
712
 
 
713
 
  return false;
 
667
  return(0);
714
668
}
715
669
 
716
670
/**
723
677
  @param var_name               Variable name
724
678
*/
725
679
 
726
 
void create_select_for_variable(Session *session, const char *var_name)
 
680
void create_select_for_variable(const char *var_name)
727
681
{
 
682
  Session *session;
728
683
  LEX *lex;
729
684
  LEX_STRING tmp, null_lex_string;
730
685
  Item *var;
731
686
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
732
687
  char *end= buff;
733
688
 
734
 
  lex= session->getLex();
735
 
  init_select(lex);
 
689
  session= current_session;
 
690
  lex= session->lex;
 
691
  mysql_init_select(lex);
736
692
  lex->sql_command= SQLCOM_SELECT;
737
693
  tmp.str= (char*) var_name;
738
694
  tmp.length=strlen(var_name);
747
703
    var->set_name(buff, end-buff, system_charset_info);
748
704
    session->add_item_to_list(var);
749
705
  }
 
706
  return;
750
707
}
751
708
 
752
709
 
758
715
  @param       length  Length of the query text
759
716
*/
760
717
 
761
 
void parse(Session *session, const char *inBuf, uint32_t length)
 
718
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
762
719
{
763
 
  session->getLex()->start(session);
 
720
  boost::posix_time::ptime start_time=boost::posix_time::microsec_clock::local_time();
 
721
  session->lex->start(session);
764
722
 
765
723
  session->reset_for_next_command();
766
724
  /* Check if the Query is Cached if and return true if yes
776
734
  {
777
735
    return;
778
736
  }
779
 
  LEX *lex= session->getLex();
 
737
  LEX *lex= session->lex;
780
738
  Lex_input_stream lip(session, inBuf, length);
781
739
  bool err= parse_sql(session, &lip);
782
740
  if (!err)
786
744
      {
787
745
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
788
746
                                 session->thread_id,
789
 
                                 const_cast<const char *>(session->schema()->c_str()));
 
747
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
790
748
        // Implement Views here --Brian
791
749
        /* Actually execute the query */
792
750
        try 
793
751
        {
794
 
          execute_command(session);
 
752
          mysql_execute_command(session);
795
753
        }
796
754
        catch (...)
797
755
        {
798
756
          // Just try to catch any random failures that could have come
799
757
          // during execution.
800
 
          DRIZZLE_ABORT;
801
758
        }
802
759
        DRIZZLE_QUERY_EXEC_DONE(0);
803
760
      }
811
768
  session->set_proc_info("freeing items");
812
769
  session->end_statement();
813
770
  session->cleanup_after_query();
814
 
  session->set_end_timer();
 
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();
815
773
}
816
774
 
817
775
 
833
791
                       List<String> *interval_list, const CHARSET_INFO * const cs)
834
792
{
835
793
  register CreateField *new_field;
836
 
  LEX  *lex= session->getLex();
 
794
  LEX  *lex= session->lex;
837
795
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
838
796
 
839
797
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
847
805
                      &default_key_create_info,
848
806
                      0, lex->col_list);
849
807
    statement->alter_info.key_list.push_back(key);
850
 
    lex->col_list.clear();
 
808
    lex->col_list.empty();
851
809
  }
852
810
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
853
811
  {
857
815
                 &default_key_create_info, 0,
858
816
                 lex->col_list);
859
817
    statement->alter_info.key_list.push_back(key);
860
 
    lex->col_list.clear();
 
818
    lex->col_list.empty();
861
819
  }
862
820
 
863
821
  if (default_value)
871
829
    */
872
830
    if (default_value->type() == Item::FUNC_ITEM &&
873
831
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
874
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
832
         type == DRIZZLE_TYPE_TIMESTAMP))
875
833
    {
876
834
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
877
835
      return true;
879
837
    else if (default_value->type() == Item::NULL_ITEM)
880
838
    {
881
839
      default_value= 0;
882
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
840
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
841
          NOT_NULL_FLAG)
883
842
      {
884
843
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
885
844
        return true;
892
851
    }
893
852
  }
894
853
 
895
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
854
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
896
855
  {
897
856
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
898
857
    return true;
911
870
}
912
871
 
913
872
 
 
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
 
914
880
/**
915
881
  Add a table to list of used tables.
916
882
 
931
897
*/
932
898
 
933
899
TableList *Select_Lex::add_table_to_list(Session *session,
934
 
                                         Table_ident *table,
935
 
                                         LEX_STRING *alias,
936
 
                                         const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
937
 
                                         thr_lock_type lock_type,
938
 
                                         List<Index_hint> *index_hints_arg,
 
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,
939
905
                                         LEX_STRING *option)
940
906
{
941
907
  TableList *ptr;
942
908
  TableList *previous_table_ref; /* The table preceding the current one. */
943
909
  char *alias_str;
944
 
  LEX *lex= session->getLex();
 
910
  LEX *lex= session->lex;
945
911
 
946
912
  if (!table)
947
913
    return NULL;                                // End of memory
957
923
  {
958
924
    my_casedn_str(files_charset_info, table->db.str);
959
925
 
960
 
    identifier::Schema schema_identifier(string(table->db.str));
961
 
    if (not schema::check(*session, schema_identifier))
 
926
    SchemaIdentifier schema_identifier(string(table->db.str));
 
927
    if (not check_db_name(session, schema_identifier))
962
928
    {
963
929
 
964
930
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
974
940
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
975
941
      return NULL;
976
942
    }
977
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
943
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
978
944
      return NULL;
979
945
  }
980
946
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
1010
976
         tables ;
1011
977
         tables=tables->next_local)
1012
978
    {
1013
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1014
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
979
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
980
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
1015
981
      {
1016
982
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1017
983
        return NULL;
1075
1041
bool Select_Lex::init_nested_join(Session *session)
1076
1042
{
1077
1043
  TableList *ptr;
1078
 
  NestedJoin *nested_join;
 
1044
  nested_join_st *nested_join;
1079
1045
 
1080
1046
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
 
                                       sizeof(NestedJoin))))
 
1047
                                       sizeof(nested_join_st))))
1082
1048
    return true;
1083
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1049
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
1050
  nested_join= ptr->getNestedJoin();
1085
1051
  join_list->push_front(ptr);
1086
1052
  ptr->setEmbedding(embedding);
1088
1054
  ptr->alias= (char*) "(nested_join)";
1089
1055
  embedding= ptr;
1090
1056
  join_list= &nested_join->join_list;
1091
 
  join_list->clear();
 
1057
  join_list->empty();
1092
1058
  return false;
1093
1059
}
1094
1060
 
1110
1076
TableList *Select_Lex::end_nested_join(Session *)
1111
1077
{
1112
1078
  TableList *ptr;
1113
 
  NestedJoin *nested_join;
 
1079
  nested_join_st *nested_join;
1114
1080
 
1115
1081
  assert(embedding);
1116
1082
  ptr= embedding;
1151
1117
TableList *Select_Lex::nest_last_join(Session *session)
1152
1118
{
1153
1119
  TableList *ptr;
1154
 
  NestedJoin *nested_join;
 
1120
  nested_join_st *nested_join;
1155
1121
  List<TableList> *embedded_list;
1156
1122
 
1157
1123
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
 
                                          sizeof(NestedJoin))))
 
1124
                                          sizeof(nested_join_st))))
1159
1125
    return NULL;
1160
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1126
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
1127
  nested_join= ptr->getNestedJoin();
1162
1128
  ptr->setEmbedding(embedding);
1163
1129
  ptr->setJoinList(join_list);
1164
1130
  ptr->alias= (char*) "(nest_last_join)";
1165
1131
  embedded_list= &nested_join->join_list;
1166
 
  embedded_list->clear();
 
1132
  embedded_list->empty();
1167
1133
 
1168
1134
  for (uint32_t i=0; i < 2; i++)
1169
1135
  {
1311
1277
  fake_select_lex->include_standalone(this,
1312
1278
                                      (Select_Lex_Node**)&fake_select_lex);
1313
1279
  fake_select_lex->select_number= INT_MAX;
1314
 
  fake_select_lex->parent_lex= session_arg->getLex(); /* Used in init_query. */
 
1280
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1315
1281
  fake_select_lex->make_empty_select();
1316
1282
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1317
1283
  fake_select_lex->select_limit= 0;
1331
1297
    */
1332
1298
    global_parameters= fake_select_lex;
1333
1299
    fake_select_lex->no_table_names_allowed= 1;
1334
 
    session_arg->getLex()->current_select= fake_select_lex;
 
1300
    session_arg->lex->current_select= fake_select_lex;
1335
1301
  }
1336
 
  session_arg->getLex()->pop_context();
 
1302
  session_arg->lex->pop_context();
1337
1303
  return(0);
1338
1304
}
1339
1305
 
1369
1335
    left_op->first_leaf_for_name_resolution();
1370
1336
  on_context->last_name_resolution_table=
1371
1337
    right_op->last_leaf_for_name_resolution();
1372
 
  return session->getLex()->push_context(on_context);
 
1338
  return session->lex->push_context(on_context);
1373
1339
}
1374
1340
 
1375
1341
 
1449
1415
 
1450
1416
 
1451
1417
/**
 
1418
  kill on thread.
 
1419
 
 
1420
  @param session                        Thread class
 
1421
  @param id                     Thread id
 
1422
  @param only_kill_query        Should it kill the query or the connection
 
1423
 
 
1424
  @note
 
1425
    This is written such that we have a short lock on LOCK_thread_count
 
1426
*/
 
1427
 
 
1428
static unsigned int kill_one_thread(session_id_t id, bool only_kill_query)
 
1429
{
 
1430
  uint32_t error= ER_NO_SUCH_THREAD;
 
1431
  
 
1432
  Session::shared_ptr session= session::Cache::singleton().find(id);
 
1433
 
 
1434
  if (not session)
 
1435
    return error;
 
1436
 
 
1437
  if (session->isViewable())
 
1438
  {
 
1439
    session->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1440
    error= 0;
 
1441
  }
 
1442
 
 
1443
  return error;
 
1444
}
 
1445
 
 
1446
 
 
1447
/*
 
1448
  kills a thread and sends response
 
1449
 
 
1450
  SYNOPSIS
 
1451
    sql_kill()
 
1452
    session                     Thread class
 
1453
    id                  Thread id
 
1454
    only_kill_query     Should it kill the query or the connection
 
1455
*/
 
1456
 
 
1457
void sql_kill(Session *session, int64_t id, bool only_kill_query)
 
1458
{
 
1459
  uint32_t error;
 
1460
 
 
1461
  if (not (error= kill_one_thread(id, only_kill_query)))
 
1462
    session->my_ok();
 
1463
  else
 
1464
    my_error(error, MYF(0), id);
 
1465
}
 
1466
 
 
1467
 
 
1468
/**
1452
1469
  Check if the select is a simple select (not an union).
1453
1470
 
1454
1471
  @retval
1457
1474
    1   error   ; In this case the error messege is sent to the client
1458
1475
*/
1459
1476
 
1460
 
bool check_simple_select(Session::pointer session)
 
1477
bool check_simple_select()
1461
1478
{
1462
 
  if (session->getLex()->current_select != &session->getLex()->select_lex)
 
1479
  Session *session= current_session;
 
1480
  LEX *lex= session->lex;
 
1481
  if (lex->current_select != &lex->select_lex)
1463
1482
  {
1464
1483
    char command[80];
1465
1484
    Lex_input_stream *lip= session->m_lip;
1519
1538
bool update_precheck(Session *session, TableList *)
1520
1539
{
1521
1540
  const char *msg= 0;
1522
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
1541
  LEX *lex= session->lex;
 
1542
  Select_Lex *select_lex= &lex->select_lex;
1523
1543
 
1524
 
  if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
 
1544
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1525
1545
  {
1526
1546
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1527
1547
    return(true);
1528
1548
  }
1529
1549
 
1530
 
  if (session->getLex()->select_lex.table_list.elements > 1)
 
1550
  if (session->lex->select_lex.table_list.elements > 1)
1531
1551
  {
1532
1552
    if (select_lex->order_list.elements)
1533
1553
      msg= "ORDER BY";
1557
1577
 
1558
1578
bool insert_precheck(Session *session, TableList *)
1559
1579
{
 
1580
  LEX *lex= session->lex;
 
1581
 
1560
1582
  /*
1561
1583
    Check that we have modify privileges for the first table and
1562
1584
    select privileges for the rest
1563
1585
  */
1564
 
  if (session->getLex()->update_list.elements != session->getLex()->value_list.elements)
 
1586
  if (lex->update_list.elements != lex->value_list.elements)
1565
1587
  {
1566
1588
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1567
1589
    return(true);
1571
1593
 
1572
1594
 
1573
1595
/**
 
1596
  CREATE TABLE query pre-check.
 
1597
 
 
1598
  @param session                        Thread handler
 
1599
  @param tables         Global table list
 
1600
  @param create_table           Table which will be created
 
1601
 
 
1602
  @retval
 
1603
    false   OK
 
1604
  @retval
 
1605
    true   Error
 
1606
*/
 
1607
 
 
1608
bool create_table_precheck(TableIdentifier &identifier)
 
1609
{
 
1610
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1611
  {
 
1612
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1613
    return true;
 
1614
  }
 
1615
 
 
1616
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1617
  {
 
1618
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1619
    return true;
 
1620
  }
 
1621
 
 
1622
  return false;
 
1623
}
 
1624
 
 
1625
 
 
1626
/**
1574
1627
  negate given expression.
1575
1628
 
1576
1629
  @param session  thread handler
1588
1641
  {
1589
1642
    /* it is NOT(NOT( ... )) */
1590
1643
    Item *arg= ((Item_func *) expr)->arguments()[0];
1591
 
    enum_parsing_place place= session->getLex()->current_select->parsing_place;
 
1644
    enum_parsing_place place= session->lex->current_select->parsing_place;
1592
1645
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1593
1646
      return arg;
1594
1647
    /*
1637
1690
}
1638
1691
 
1639
1692
 
1640
 
bool check_identifier_name(LEX_STRING *str, error_t err_code,
 
1693
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
1641
1694
                           uint32_t max_char_length,
1642
1695
                           const char *param_for_err_msg)
1643
1696
{
1663
1716
 
1664
1717
  switch (err_code)
1665
1718
  {
1666
 
  case EE_OK:
 
1719
  case 0:
1667
1720
    break;
1668
1721
  case ER_WRONG_STRING_LENGTH:
1669
1722
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1675
1728
    assert(0);
1676
1729
    break;
1677
1730
  }
1678
 
 
1679
1731
  return true;
1680
1732
}
1681
1733
 
1704
1756
 
1705
1757
  /* Parse the query. */
1706
1758
 
1707
 
  bool parse_status= base_sql_parse(session) != 0;
 
1759
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
1708
1760
 
1709
1761
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1710
1762
 
1711
 
  assert(!parse_status || session->is_error());
 
1763
  assert(!mysql_parse_status || session->is_error());
1712
1764
 
1713
1765
  /* Reset Lex_input_stream. */
1714
1766
 
1715
1767
  session->m_lip= NULL;
1716
1768
 
1717
 
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
 
1769
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
1718
1770
 
1719
1771
  /* That's it. */
1720
1772
 
1721
 
  return parse_status || session->is_fatal_error;
 
1773
  return mysql_parse_status || session->is_fatal_error;
1722
1774
}
1723
1775
 
1724
1776
/**