~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2010-12-26 03:15:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2038.
  • Revision ID: mordred@inaugust.com-20101226031544-1cf3raipu53fnmyj
Through page.

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