~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

Modernize our call to the parser, no more casting required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
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>
39
41
#include <drizzled/statement.h>
40
42
#include <drizzled/statement/alter_table.h>
41
43
#include "drizzled/probes.h"
42
 
#include "drizzled/session/cache.h"
43
44
#include "drizzled/global_charset_info.h"
44
45
 
45
46
#include "drizzled/plugin/logging.h"
49
50
#include "drizzled/optimizer/explain_plan.h"
50
51
#include "drizzled/pthread_globals.h"
51
52
#include "drizzled/plugin/event_observer.h"
 
53
#include "drizzled/visibility.h"
 
54
 
 
55
#include <drizzled/schema.h>
52
56
 
53
57
#include <limits.h>
54
58
 
59
63
 
60
64
using namespace std;
61
65
 
62
 
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
 
66
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
63
67
 
64
68
namespace drizzled
65
69
{
67
71
/* Prototypes */
68
72
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
69
73
static bool parse_sql(Session *session, Lex_input_stream *lip);
70
 
void mysql_parse(Session *session, const char *inBuf, uint32_t length);
 
74
void parse(Session *session, const char *inBuf, uint32_t length);
71
75
 
72
76
/**
73
77
  @defgroup Runtime_Environment Runtime Environment
77
81
extern size_t my_thread_stack_size;
78
82
extern const CHARSET_INFO *character_set_filesystem;
79
83
 
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
 
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
89
96
};
90
97
 
 
98
}
 
99
 
91
100
const char *xa_state_names[]={
92
101
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
93
102
};
106
115
*/
107
116
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
108
117
 
 
118
const std::string &getCommandName(const enum_server_command& command)
 
119
{
 
120
  return command_name[command];
 
121
}
 
122
 
109
123
void init_update_queries(void)
110
124
{
111
125
  uint32_t x;
187
201
    query_id.next();
188
202
  }
189
203
 
190
 
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
 
204
  /* @todo set session->lex->sql_command to SQLCOM_END here */
191
205
 
192
206
  plugin::Logging::preDo(session);
193
207
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
208
222
 
209
223
    string tmp(packet, packet_length);
210
224
 
211
 
    SchemaIdentifier identifier(tmp);
 
225
    identifier::Schema identifier(tmp);
212
226
 
213
 
    if (not mysql_change_db(session, identifier))
 
227
    if (not schema::change(*session, identifier))
214
228
    {
215
229
      session->my_ok();
216
230
    }
224
238
                        session->thread_id,
225
239
                        const_cast<const char *>(session->schema()->c_str()));
226
240
 
227
 
    mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
241
    parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
228
242
 
229
243
    break;
230
244
  }
257
271
  /* If commit fails, we should be able to reset the OK status. */
258
272
  session->main_da.can_overwrite_status= true;
259
273
  TransactionServices &transaction_services= TransactionServices::singleton();
260
 
  transaction_services.autocommitOrRollback(session, session->is_error());
 
274
  transaction_services.autocommitOrRollback(*session, session->is_error());
261
275
  session->main_da.can_overwrite_status= false;
262
276
 
263
277
  session->transaction.stmt.reset();
282
296
  {
283
297
  case Diagnostics_area::DA_ERROR:
284
298
    /* The query failed, send error to log and abort bootstrap. */
285
 
    session->client->sendError(session->main_da.sql_errno(),
 
299
    session->getClient()->sendError(session->main_da.sql_errno(),
286
300
                               session->main_da.message());
287
301
    break;
288
302
 
289
303
  case Diagnostics_area::DA_EOF:
290
 
    session->client->sendEOF();
 
304
    session->getClient()->sendEOF();
291
305
    break;
292
306
 
293
307
  case Diagnostics_area::DA_OK:
294
 
    session->client->sendOK();
 
308
    session->getClient()->sendOK();
295
309
    break;
296
310
 
297
311
  case Diagnostics_area::DA_DISABLED:
299
313
 
300
314
  case Diagnostics_area::DA_EMPTY:
301
315
  default:
302
 
    session->client->sendOK();
 
316
    session->getClient()->sendOK();
303
317
    break;
304
318
  }
305
319
 
429
443
    true        Error
430
444
*/
431
445
 
432
 
static int mysql_execute_command(Session *session)
 
446
static int execute_command(Session *session)
433
447
{
434
448
  bool res= false;
435
449
  LEX  *lex= session->lex;
475
489
 
476
490
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
477
491
 
 
492
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
 
493
      && ! session->inTransaction()
 
494
      && lex->statement->isTransactional())
 
495
  {
 
496
    if (session->startTransaction() == false)
 
497
    {
 
498
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
 
499
      return true;
 
500
    }
 
501
  }
 
502
 
478
503
  /* now we are ready to execute the statement */
479
504
  res= lex->statement->execute();
480
505
  session->set_proc_info("query end");
503
528
      param->select_limit=
504
529
        new Item_int((uint64_t) session->variables.select_limit);
505
530
  }
 
531
 
 
532
  if (all_tables
 
533
      && ! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
 
534
      && ! session->inTransaction()
 
535
      && ! lex->statement->isShow())
 
536
  {
 
537
    if (session->startTransaction() == false)
 
538
    {
 
539
      my_error(drizzled::ER_UNKNOWN_ERROR, MYF(0));
 
540
      return true;
 
541
    }
 
542
  }
 
543
 
506
544
  if (not (res= session->openTablesLock(all_tables)))
507
545
  {
508
546
    if (lex->describe)
587
625
 
588
626
 
589
627
void
590
 
mysql_init_select(LEX *lex)
 
628
init_select(LEX *lex)
591
629
{
592
630
  Select_Lex *select_lex= lex->current_select;
593
631
  select_lex->init_select();
601
639
 
602
640
 
603
641
bool
604
 
mysql_new_select(LEX *lex, bool move_down)
 
642
new_select(LEX *lex, bool move_down)
605
643
{
606
644
  Select_Lex *select_lex;
607
645
  Session *session= lex->session;
608
646
 
609
647
  if (!(select_lex= new (session->mem_root) Select_Lex()))
610
 
    return(1);
 
648
    return true;
 
649
 
611
650
  select_lex->select_number= ++session->select_number;
612
651
  select_lex->parent_lex= lex; /* Used in init_query. */
613
652
  select_lex->init_query();
614
653
  select_lex->init_select();
615
654
  lex->nest_level++;
 
655
 
616
656
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
617
657
  {
618
658
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
619
659
    return(1);
620
660
  }
 
661
 
621
662
  select_lex->nest_level= lex->nest_level;
622
663
  if (move_down)
623
664
  {
645
686
    if (lex->current_select->order_list.first && !lex->current_select->braces)
646
687
    {
647
688
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
648
 
      return(1);
 
689
      return true;
649
690
    }
 
691
 
650
692
    select_lex->include_neighbour(lex->current_select);
651
693
    Select_Lex_Unit *unit= select_lex->master_unit();
652
 
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
653
 
      return(1);
 
694
 
 
695
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
696
      return true;
 
697
 
654
698
    select_lex->context.outer_context=
655
699
                unit->first_select()->context.outer_context;
656
700
  }
663
707
    list
664
708
  */
665
709
  select_lex->context.resolve_in_select_list= true;
666
 
  return(0);
 
710
 
 
711
  return false;
667
712
}
668
713
 
669
714
/**
676
721
  @param var_name               Variable name
677
722
*/
678
723
 
679
 
void create_select_for_variable(const char *var_name)
 
724
void create_select_for_variable(Session *session, const char *var_name)
680
725
{
681
 
  Session *session;
682
726
  LEX *lex;
683
727
  LEX_STRING tmp, null_lex_string;
684
728
  Item *var;
685
729
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
686
730
  char *end= buff;
687
731
 
688
 
  session= current_session;
689
732
  lex= session->lex;
690
 
  mysql_init_select(lex);
 
733
  init_select(lex);
691
734
  lex->sql_command= SQLCOM_SELECT;
692
735
  tmp.str= (char*) var_name;
693
736
  tmp.length=strlen(var_name);
702
745
    var->set_name(buff, end-buff, system_charset_info);
703
746
    session->add_item_to_list(var);
704
747
  }
705
 
  return;
706
748
}
707
749
 
708
750
 
714
756
  @param       length  Length of the query text
715
757
*/
716
758
 
717
 
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
 
759
void parse(Session *session, const char *inBuf, uint32_t length)
718
760
{
719
 
  boost::posix_time::ptime start_time=boost::posix_time::microsec_clock::local_time();
720
761
  session->lex->start(session);
721
762
 
722
763
  session->reset_for_next_command();
748
789
        /* Actually execute the query */
749
790
        try 
750
791
        {
751
 
          mysql_execute_command(session);
 
792
          execute_command(session);
752
793
        }
753
794
        catch (...)
754
795
        {
755
796
          // Just try to catch any random failures that could have come
756
797
          // during execution.
757
 
          unireg_abort(1);
 
798
          DRIZZLE_ABORT;
758
799
        }
759
800
        DRIZZLE_QUERY_EXEC_DONE(0);
760
801
      }
768
809
  session->set_proc_info("freeing items");
769
810
  session->end_statement();
770
811
  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();
 
812
  session->set_end_timer();
773
813
}
774
814
 
775
815
 
829
869
    */
830
870
    if (default_value->type() == Item::FUNC_ITEM &&
831
871
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
832
 
         type == DRIZZLE_TYPE_TIMESTAMP))
 
872
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
833
873
    {
834
874
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
835
875
      return true;
837
877
    else if (default_value->type() == Item::NULL_ITEM)
838
878
    {
839
879
      default_value= 0;
840
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
841
 
          NOT_NULL_FLAG)
 
880
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
842
881
      {
843
882
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
844
883
        return true;
851
890
    }
852
891
  }
853
892
 
854
 
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
 
893
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
855
894
  {
856
895
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
857
896
    return true;
870
909
}
871
910
 
872
911
 
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
912
/**
881
913
  Add a table to list of used tables.
882
914
 
897
929
*/
898
930
 
899
931
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,
 
932
                                         Table_ident *table,
 
933
                                         LEX_STRING *alias,
 
934
                                         const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
 
935
                                         thr_lock_type lock_type,
 
936
                                         List<Index_hint> *index_hints_arg,
905
937
                                         LEX_STRING *option)
906
938
{
907
939
  TableList *ptr;
923
955
  {
924
956
    my_casedn_str(files_charset_info, table->db.str);
925
957
 
926
 
    SchemaIdentifier schema_identifier(string(table->db.str));
927
 
    if (not check_db_name(session, schema_identifier))
 
958
    identifier::Schema schema_identifier(string(table->db.str));
 
959
    if (not schema::check(*session, schema_identifier))
928
960
    {
929
961
 
930
962
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
940
972
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
941
973
      return NULL;
942
974
    }
943
 
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
 
975
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
944
976
      return NULL;
945
977
  }
946
978
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
976
1008
         tables ;
977
1009
         tables=tables->next_local)
978
1010
    {
979
 
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
980
 
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
 
1011
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
1012
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
981
1013
      {
982
1014
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
983
1015
        return NULL;
1041
1073
bool Select_Lex::init_nested_join(Session *session)
1042
1074
{
1043
1075
  TableList *ptr;
1044
 
  nested_join_st *nested_join;
 
1076
  NestedJoin *nested_join;
1045
1077
 
1046
1078
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1047
 
                                       sizeof(nested_join_st))))
 
1079
                                       sizeof(NestedJoin))))
1048
1080
    return true;
1049
 
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1081
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1050
1082
  nested_join= ptr->getNestedJoin();
1051
1083
  join_list->push_front(ptr);
1052
1084
  ptr->setEmbedding(embedding);
1076
1108
TableList *Select_Lex::end_nested_join(Session *)
1077
1109
{
1078
1110
  TableList *ptr;
1079
 
  nested_join_st *nested_join;
 
1111
  NestedJoin *nested_join;
1080
1112
 
1081
1113
  assert(embedding);
1082
1114
  ptr= embedding;
1117
1149
TableList *Select_Lex::nest_last_join(Session *session)
1118
1150
{
1119
1151
  TableList *ptr;
1120
 
  nested_join_st *nested_join;
 
1152
  NestedJoin *nested_join;
1121
1153
  List<TableList> *embedded_list;
1122
1154
 
1123
1155
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1124
 
                                          sizeof(nested_join_st))))
 
1156
                                          sizeof(NestedJoin))))
1125
1157
    return NULL;
1126
 
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1158
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1127
1159
  nested_join= ptr->getNestedJoin();
1128
1160
  ptr->setEmbedding(embedding);
1129
1161
  ptr->setJoinList(join_list);
1423
1455
    1   error   ; In this case the error messege is sent to the client
1424
1456
*/
1425
1457
 
1426
 
bool check_simple_select()
 
1458
bool check_simple_select(Session::pointer session)
1427
1459
{
1428
 
  Session *session= current_session;
1429
1460
  LEX *lex= session->lex;
1430
1461
  if (lex->current_select != &lex->select_lex)
1431
1462
  {
1608
1639
}
1609
1640
 
1610
1641
 
1611
 
bool check_identifier_name(LEX_STRING *str, uint32_t err_code,
 
1642
bool check_identifier_name(LEX_STRING *str, error_t err_code,
1612
1643
                           uint32_t max_char_length,
1613
1644
                           const char *param_for_err_msg)
1614
1645
{
1634
1665
 
1635
1666
  switch (err_code)
1636
1667
  {
1637
 
  case 0:
 
1668
  case EE_OK:
1638
1669
    break;
1639
1670
  case ER_WRONG_STRING_LENGTH:
1640
1671
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
1646
1677
    assert(0);
1647
1678
    break;
1648
1679
  }
 
1680
 
1649
1681
  return true;
1650
1682
}
1651
1683
 
1674
1706
 
1675
1707
  /* Parse the query. */
1676
1708
 
1677
 
  bool mysql_parse_status= DRIZZLEparse(session) != 0;
 
1709
  bool parse_status= base_sql_parse(session) != 0;
1678
1710
 
1679
1711
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1680
1712
 
1681
 
  assert(!mysql_parse_status || session->is_error());
 
1713
  assert(!parse_status || session->is_error());
1682
1714
 
1683
1715
  /* Reset Lex_input_stream. */
1684
1716
 
1685
1717
  session->m_lip= NULL;
1686
1718
 
1687
 
  DRIZZLE_QUERY_PARSE_DONE(mysql_parse_status || session->is_fatal_error);
 
1719
  DRIZZLE_QUERY_PARSE_DONE(parse_status || session->is_fatal_error);
1688
1720
 
1689
1721
  /* That's it. */
1690
1722
 
1691
 
  return mysql_parse_status || session->is_fatal_error;
 
1723
  return parse_status || session->is_fatal_error;
1692
1724
}
1693
1725
 
1694
1726
/**