~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2011-01-06 05:17:09 UTC
  • Revision ID: brian@tangent.org-20110106051709-oa0se8ur02uc6i9o
Added native functions into the function table.

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
20
#include "drizzled/abort_exception.h"
22
21
#include <drizzled/my_hash.h>
23
22
#include <drizzled/error.h>
34
33
#include <drizzled/item/cmpfunc.h>
35
34
#include <drizzled/item/null.h>
36
35
#include <drizzled/session.h>
37
 
#include <drizzled/session/cache.h>
38
36
#include <drizzled/sql_load.h>
39
37
#include <drizzled/lock.h>
40
38
#include <drizzled/select_send.h>
42
40
#include <drizzled/statement.h>
43
41
#include <drizzled/statement/alter_table.h>
44
42
#include "drizzled/probes.h"
 
43
#include "drizzled/session/cache.h"
45
44
#include "drizzled/global_charset_info.h"
46
45
 
47
46
#include "drizzled/plugin/logging.h"
51
50
#include "drizzled/optimizer/explain_plan.h"
52
51
#include "drizzled/pthread_globals.h"
53
52
#include "drizzled/plugin/event_observer.h"
54
 
#include "drizzled/visibility.h"
55
53
 
56
54
#include <limits.h>
57
55
 
80
78
extern size_t my_thread_stack_size;
81
79
extern const CHARSET_INFO *character_set_filesystem;
82
80
 
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
 
81
const LEX_STRING command_name[COM_END+1]={
 
82
  { C_STRING_WITH_LEN("Sleep") },
 
83
  { C_STRING_WITH_LEN("Quit") },
 
84
  { C_STRING_WITH_LEN("Init DB") },
 
85
  { C_STRING_WITH_LEN("Query") },
 
86
  { C_STRING_WITH_LEN("Shutdown") },
 
87
  { C_STRING_WITH_LEN("Connect") },
 
88
  { C_STRING_WITH_LEN("Ping") },
 
89
  { C_STRING_WITH_LEN("Error") }  // Last command number
95
90
};
96
91
 
97
 
}
98
 
 
99
92
const char *xa_state_names[]={
100
93
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
101
94
};
114
107
*/
115
108
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
116
109
 
117
 
const std::string &getCommandName(const enum_server_command& command)
118
 
{
119
 
  return command_name[command];
120
 
}
121
 
 
122
110
void init_update_queries(void)
123
111
{
124
112
  uint32_t x;
200
188
    query_id.next();
201
189
  }
202
190
 
203
 
  /* @todo set session->lex->sql_command to SQLCOM_END here */
 
191
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
204
192
 
205
193
  plugin::Logging::preDo(session);
206
194
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
221
209
 
222
210
    string tmp(packet, packet_length);
223
211
 
224
 
    identifier::Schema identifier(tmp);
 
212
    SchemaIdentifier identifier(tmp);
225
213
 
226
214
    if (not change_db(session, identifier))
227
215
    {
270
258
  /* If commit fails, we should be able to reset the OK status. */
271
259
  session->main_da.can_overwrite_status= true;
272
260
  TransactionServices &transaction_services= TransactionServices::singleton();
273
 
  transaction_services.autocommitOrRollback(*session, session->is_error());
 
261
  transaction_services.autocommitOrRollback(session, session->is_error());
274
262
  session->main_da.can_overwrite_status= false;
275
263
 
276
264
  session->transaction.stmt.reset();
488
476
 
489
477
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
490
478
 
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
479
  /* now we are ready to execute the statement */
503
480
  res= lex->statement->execute();
504
481
  session->set_proc_info("query end");
527
504
      param->select_limit=
528
505
        new Item_int((uint64_t) session->variables.select_limit);
529
506
  }
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
507
  if (not (res= session->openTablesLock(all_tables)))
544
508
  {
545
509
    if (lex->describe)
644
608
  Session *session= lex->session;
645
609
 
646
610
  if (!(select_lex= new (session->mem_root) Select_Lex()))
647
 
    return true;
648
 
 
 
611
    return(1);
649
612
  select_lex->select_number= ++session->select_number;
650
613
  select_lex->parent_lex= lex; /* Used in init_query. */
651
614
  select_lex->init_query();
652
615
  select_lex->init_select();
653
616
  lex->nest_level++;
654
 
 
655
617
  if (lex->nest_level > (int) MAX_SELECT_NESTING)
656
618
  {
657
619
    my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT,MYF(0),MAX_SELECT_NESTING);
658
620
    return(1);
659
621
  }
660
 
 
661
622
  select_lex->nest_level= lex->nest_level;
662
623
  if (move_down)
663
624
  {
685
646
    if (lex->current_select->order_list.first && !lex->current_select->braces)
686
647
    {
687
648
      my_error(ER_WRONG_USAGE, MYF(0), "UNION", "order_st BY");
688
 
      return true;
 
649
      return(1);
689
650
    }
690
 
 
691
651
    select_lex->include_neighbour(lex->current_select);
692
652
    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
 
 
 
653
    if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
 
654
      return(1);
697
655
    select_lex->context.outer_context=
698
656
                unit->first_select()->context.outer_context;
699
657
  }
706
664
    list
707
665
  */
708
666
  select_lex->context.resolve_in_select_list= true;
709
 
 
710
 
  return false;
 
667
  return(0);
711
668
}
712
669
 
713
670
/**
720
677
  @param var_name               Variable name
721
678
*/
722
679
 
723
 
void create_select_for_variable(Session *session, const char *var_name)
 
680
void create_select_for_variable(const char *var_name)
724
681
{
 
682
  Session *session;
725
683
  LEX *lex;
726
684
  LEX_STRING tmp, null_lex_string;
727
685
  Item *var;
728
686
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
729
687
  char *end= buff;
730
688
 
 
689
  session= current_session;
731
690
  lex= session->lex;
732
691
  init_select(lex);
733
692
  lex->sql_command= SQLCOM_SELECT;
744
703
    var->set_name(buff, end-buff, system_charset_info);
745
704
    session->add_item_to_list(var);
746
705
  }
 
706
  return;
747
707
}
748
708
 
749
709
 
868
828
    */
869
829
    if (default_value->type() == Item::FUNC_ITEM &&
870
830
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
871
 
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
 
831
         type == DRIZZLE_TYPE_TIMESTAMP))
872
832
    {
873
833
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
874
834
      return true;
876
836
    else if (default_value->type() == Item::NULL_ITEM)
877
837
    {
878
838
      default_value= 0;
879
 
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
 
839
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
 
840
          NOT_NULL_FLAG)
880
841
      {
881
842
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
882
843
        return true;
889
850
    }
890
851
  }
891
852
 
892
 
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
 
853
  if (on_update_value && type != DRIZZLE_TYPE_TIMESTAMP)
893
854
  {
894
855
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
895
856
    return true;
908
869
}
909
870
 
910
871
 
 
872
/** Store position for column in ALTER TABLE .. ADD column. */
 
873
 
 
874
void store_position_for_column(const char *name)
 
875
{
 
876
  current_session->lex->last_field->after=const_cast<char*> (name);
 
877
}
 
878
 
911
879
/**
912
880
  Add a table to list of used tables.
913
881
 
928
896
*/
929
897
 
930
898
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,
 
899
                                                             Table_ident *table,
 
900
                                                             LEX_STRING *alias,
 
901
                                                             const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
 
902
                                                             thr_lock_type lock_type,
 
903
                                                             List<Index_hint> *index_hints_arg,
936
904
                                         LEX_STRING *option)
937
905
{
938
906
  TableList *ptr;
954
922
  {
955
923
    my_casedn_str(files_charset_info, table->db.str);
956
924
 
957
 
    identifier::Schema schema_identifier(string(table->db.str));
 
925
    SchemaIdentifier schema_identifier(string(table->db.str));
958
926
    if (not check_db_name(session, schema_identifier))
959
927
    {
960
928
 
971
939
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
972
940
      return NULL;
973
941
    }
974
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
942
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
975
943
      return NULL;
976
944
  }
977
945
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
1007
975
         tables ;
1008
976
         tables=tables->next_local)
1009
977
    {
1010
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
1011
 
          not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
978
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
 
979
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
1012
980
      {
1013
981
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
1014
982
        return NULL;
1072
1040
bool Select_Lex::init_nested_join(Session *session)
1073
1041
{
1074
1042
  TableList *ptr;
1075
 
  NestedJoin *nested_join;
 
1043
  nested_join_st *nested_join;
1076
1044
 
1077
1045
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1078
 
                                       sizeof(NestedJoin))))
 
1046
                                       sizeof(nested_join_st))))
1079
1047
    return true;
1080
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1048
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1081
1049
  nested_join= ptr->getNestedJoin();
1082
1050
  join_list->push_front(ptr);
1083
1051
  ptr->setEmbedding(embedding);
1107
1075
TableList *Select_Lex::end_nested_join(Session *)
1108
1076
{
1109
1077
  TableList *ptr;
1110
 
  NestedJoin *nested_join;
 
1078
  nested_join_st *nested_join;
1111
1079
 
1112
1080
  assert(embedding);
1113
1081
  ptr= embedding;
1148
1116
TableList *Select_Lex::nest_last_join(Session *session)
1149
1117
{
1150
1118
  TableList *ptr;
1151
 
  NestedJoin *nested_join;
 
1119
  nested_join_st *nested_join;
1152
1120
  List<TableList> *embedded_list;
1153
1121
 
1154
1122
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1155
 
                                          sizeof(NestedJoin))))
 
1123
                                          sizeof(nested_join_st))))
1156
1124
    return NULL;
1157
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1125
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1158
1126
  nested_join= ptr->getNestedJoin();
1159
1127
  ptr->setEmbedding(embedding);
1160
1128
  ptr->setJoinList(join_list);