~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Mark Atwood
  • Date: 2011-12-20 02:32:53 UTC
  • mfrom: (2469.1.1 drizzle-build)
  • Revision ID: me@mark.atwood.name-20111220023253-bvu0kr14kwsdvz7g
mergeĀ lp:~brianaker/drizzle/deprecate-pbms

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <drizzled/abort_exception.h>
22
22
#include <drizzled/error.h>
23
23
#include <drizzled/nested_join.h>
24
 
#include <drizzled/query_id.h>
25
24
#include <drizzled/transaction_services.h>
26
25
#include <drizzled/sql_parse.h>
27
26
#include <drizzled/data_home.h>
88
87
extern size_t my_thread_stack_size;
89
88
extern const charset_info_st *character_set_filesystem;
90
89
 
 
90
static atomic<uint64_t> g_query_id;
 
91
 
91
92
namespace
92
93
{
93
94
  static const std::string command_name[]=
192
193
                      const char* packet, uint32_t packet_length)
193
194
{
194
195
  bool error= false;
195
 
  Query_id& query_id= Query_id::get_query_id();
196
196
 
197
197
  DRIZZLE_COMMAND_START(session->thread_id, command);
198
198
 
199
199
  session->command= command;
200
200
  session->lex().sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
201
201
  session->times.set_time();
202
 
  session->setQueryId(query_id.value());
 
202
  session->setQueryId(g_query_id.increment());
203
203
 
204
204
  if (command != COM_PING)
205
205
  {
206
206
    // Increase id and count all other statements
207
207
    session->status_var.questions++;
208
 
    query_id.next();
209
208
  }
210
209
 
211
210
  /* @todo set session->lex().sql_command to SQLCOM_END here */
451
450
 
452
451
static int execute_command(Session *session)
453
452
{
454
 
  bool res= false;
455
 
 
456
453
  /* first Select_Lex (have special meaning for many of non-SELECTcommands) */
457
454
  Select_Lex *select_lex= &session->lex().select_lex;
458
455
 
459
 
  /* list of all tables in query */
460
 
  TableList *all_tables;
461
 
 
462
456
  /*
463
457
    In many cases first table of main Select_Lex have special meaning =>
464
458
    check that it is first table in global list and relink it first in
476
470
  */
477
471
  session->lex().first_lists_tables_same();
478
472
 
 
473
  /* list of all tables in query */
479
474
  /* should be assigned after making first tables same */
480
 
  all_tables= session->lex().query_tables;
 
475
  TableList* all_tables= session->lex().query_tables;
481
476
 
482
477
  /* set context for commands which do not use setup_tables */
483
478
  select_lex->context.resolve_in_table_list_only((TableList*)select_lex->table_list.first);
496
491
 
497
492
  assert(not session->transaction.stmt.hasModifiedNonTransData());
498
493
 
499
 
  if (! (session->server_status & SERVER_STATUS_AUTOCOMMIT)
500
 
      && ! session->inTransaction()
 
494
  if (not (session->server_status & SERVER_STATUS_AUTOCOMMIT)
 
495
      && not session->inTransaction()
501
496
      && session->lex().statement->isTransactional())
502
497
  {
503
498
    if (not session->startTransaction())
508
503
  }
509
504
 
510
505
  /* now we are ready to execute the statement */
511
 
  res= session->lex().statement->execute();
 
506
  bool res= session->lex().statement->execute();
512
507
  session->set_proc_info("query end");
513
508
  /*
514
509
    The return value for ROW_COUNT() is "implementation dependent" if the
516
511
    wants. We also keep the last value in case of SQLCOM_CALL or
517
512
    SQLCOM_EXECUTE.
518
513
  */
519
 
  if (! (sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT)))
 
514
  if (not sql_command_flags[session->lex().sql_command].test(CF_BIT_HAS_ROW_COUNT))
520
515
  {
521
516
    session->row_count_func= -1;
522
517
  }
523
518
 
524
 
  return (res || session->is_error());
 
519
  return res || session->is_error();
525
520
}
 
521
 
526
522
bool execute_sqlcom_select(Session *session, TableList *all_tables)
527
523
{
528
524
  LEX   *lex= &session->lex();
696
692
    if (not unit->fake_select_lex && unit->add_fake_select_lex(lex->session))
697
693
      return true;
698
694
 
699
 
    select_lex->context.outer_context=
700
 
                unit->first_select()->context.outer_context;
 
695
    select_lex->context.outer_context= unit->first_select()->context.outer_context;
701
696
  }
702
697
 
703
698
  select_lex->master_unit()->global_parameters= select_lex;
728
723
  init_select(&lex);
729
724
  lex.sql_command= SQLCOM_SELECT;
730
725
  lex_string_t tmp;
731
 
  tmp.str= (char*) var_name;
732
 
  tmp.length=strlen(var_name);
733
 
  lex_string_t null_lex_string;
734
 
  memset(&null_lex_string.str, 0, sizeof(null_lex_string));
 
726
  tmp.assign(var_name, strlen(var_name));
735
727
  /*
736
728
    We set the name of Item to @@session.var_name because that then is used
737
729
    as the column name in the output.
738
730
  */
739
 
  if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string))
 
731
  if (Item* var= get_system_var(session, OPT_SESSION, tmp, null_lex_string()))
740
732
  {
741
733
    char buff[MAX_SYS_VAR_LENGTH*2+4+8];
742
734
    char *end= buff;
764
756
   * by setting the query_safe_cache param to TRUE
765
757
   */
766
758
  if (plugin::QueryCache::isCached(&session) && not plugin::QueryCache::sendCachedResultset(&session))
767
 
      return;
 
759
    return;
768
760
  Lex_input_stream lip(&session, inBuf, length);
769
761
  if (parse_sql(&session, &lip))
770
762
    assert(session.is_error());
771
763
  else if (not session.is_error())
772
764
  {
773
 
    DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id,
774
 
                             const_cast<const char *>(session.schema()->c_str()));
 
765
    DRIZZLE_QUERY_EXEC_START(session.getQueryString()->c_str(), session.thread_id, session.schema()->c_str());
775
766
    // Implement Views here --Brian
776
767
    /* Actually execute the query */
777
768
    try
801
792
    Return 0 if ok
802
793
*/
803
794
 
804
 
bool add_field_to_list(Session *session, lex_string_t *field_name, enum_field_types type,
 
795
bool add_field_to_list(Session *session, str_ref field_name, enum_field_types type,
805
796
                       const char *length, const char *decimals,
806
 
                       uint32_t type_modifier,
807
 
                       enum column_format_type column_format,
808
 
                       Item *default_value, Item *on_update_value,
809
 
                       lex_string_t *comment,
810
 
                       const char *change,
811
 
                       List<String> *interval_list, const charset_info_st * const cs)
 
797
                       uint32_t type_modifier, column_format_type column_format,
 
798
                       Item *default_value, Item *on_update_value, str_ref comment,
 
799
                       const char *change, List<String> *interval_list, const charset_info_st* cs)
812
800
{
813
 
  register CreateField *new_field;
814
801
  LEX  *lex= &session->lex();
815
802
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
816
803
 
819
806
 
820
807
  if (type_modifier & PRI_KEY_FLAG)
821
808
  {
822
 
    Key *key;
823
 
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
824
 
    key= new Key(Key::PRIMARY, null_lex_string(),
825
 
                      &default_key_create_info,
826
 
                      0, lex->col_list);
827
 
    statement->alter_info.key_list.push_back(key);
 
809
    lex->col_list.push_back(new Key_part_spec(field_name, 0));
 
810
    statement->alter_info.key_list.push_back(new Key(Key::PRIMARY, null_lex_string(), &default_key_create_info, 0, lex->col_list));
828
811
    lex->col_list.clear();
829
812
  }
830
813
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
831
814
  {
832
 
    Key *key;
833
 
    lex->col_list.push_back(new Key_part_spec(*field_name, 0));
834
 
    key= new Key(Key::UNIQUE, null_lex_string(),
835
 
                 &default_key_create_info, 0,
836
 
                 lex->col_list);
837
 
    statement->alter_info.key_list.push_back(key);
 
815
    lex->col_list.push_back(new Key_part_spec(field_name, 0));
 
816
    statement->alter_info.key_list.push_back(new Key(Key::UNIQUE, null_lex_string(), &default_key_create_info, 0, lex->col_list));
838
817
    lex->col_list.clear();
839
818
  }
840
819
 
851
830
        !(((Item_func*)default_value)->functype() == Item_func::NOW_FUNC &&
852
831
         (type == DRIZZLE_TYPE_TIMESTAMP or type == DRIZZLE_TYPE_MICROTIME)))
853
832
    {
854
 
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
 
833
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
855
834
      return true;
856
835
    }
857
836
    else if (default_value->type() == Item::NULL_ITEM)
859
838
      default_value= 0;
860
839
      if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) == NOT_NULL_FLAG)
861
840
      {
862
 
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
863
 
        return true;
 
841
        my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
 
842
        return true;
864
843
      }
865
844
    }
866
845
    else if (type_modifier & AUTO_INCREMENT_FLAG)
867
846
    {
868
 
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
 
847
      my_error(ER_INVALID_DEFAULT, MYF(0), field_name.data());
869
848
      return true;
870
849
    }
871
850
  }
872
851
 
873
852
  if (on_update_value && (type != DRIZZLE_TYPE_TIMESTAMP and type != DRIZZLE_TYPE_MICROTIME))
874
853
  {
875
 
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
 
854
    my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name.data());
876
855
    return true;
877
856
  }
878
857
 
879
 
  new_field= new CreateField;
880
 
  if (new_field->init(session, field_name->str, type, length, decimals,
881
 
                         type_modifier, comment, change, interval_list, cs, 0, column_format)
 
858
  CreateField* new_field= new CreateField;
 
859
  if (new_field->init(session, field_name.data(), type, length, decimals, type_modifier, comment, change, interval_list, cs, 0, column_format)
882
860
      || new_field->setDefaultValue(default_value, on_update_value))
883
861
    return true;
884
862
 
921
899
 
922
900
  if (!table)
923
901
    return NULL;                                // End of memory
924
 
  const char* alias_str= alias ? alias->str : table->table.str;
925
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
926
 
      check_table_name(table->table.str, table->table.length))
 
902
  const char* alias_str= alias ? alias->data() : table->table.data();
 
903
  if (not table_options.test(TL_OPTION_ALIAS) && check_table_name(table->table))
927
904
  {
928
 
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
 
905
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.data());
929
906
    return NULL;
930
907
  }
931
908
 
932
 
  if (not table->is_derived_table() && table->db.str)
 
909
  if (not table->is_derived_table() && table->db.data())
933
910
  {
934
 
    my_casedn_str(files_charset_info, table->db.str);
935
 
 
936
 
    identifier::Schema schema_identifier(string(table->db.str));
937
 
    if (not schema::check(*session, schema_identifier))
 
911
    files_charset_info->casedn_str(table->db.str_);
 
912
    if (not schema::check(*session, identifier::Schema(table->db)))
938
913
    {
939
 
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
 
914
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.data());
940
915
      return NULL;
941
916
    }
942
917
  }
948
923
      my_message(ER_DERIVED_MUST_HAVE_ALIAS, ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
949
924
      return NULL;
950
925
    }
951
 
    alias_str= (char*) session->mem.memdup(alias_str,table->table.length+1);
 
926
    alias_str= (char*) session->mem.memdup(alias_str, table->table.size() + 1);
952
927
  }
953
928
  TableList *ptr = (TableList *) session->mem.calloc(sizeof(TableList));
954
929
 
955
 
  char* name;
956
 
  size_t name_size;
957
 
  if (table->db.str)
 
930
  if (table->db.data())
958
931
  {
959
932
    ptr->setIsFqtn(true);
960
 
    ptr->setSchemaName(table->db.str);
 
933
    ptr->setSchemaName(table->db.data());
961
934
  }
962
 
  else if (lex->session->copy_db_to(name, name_size))
963
 
    return NULL;
964
 
  else
 
935
  else 
965
936
  {
 
937
    str_ref schema = lex->session->copy_db_to();
 
938
    if (schema.empty())
 
939
      return NULL;
966
940
    ptr->setIsFqtn(false);
967
 
    ptr->setSchemaName(name);
 
941
    ptr->setSchemaName(schema.data());
968
942
  }
969
943
 
970
944
  ptr->alias= alias_str;
971
945
  ptr->setIsAlias(alias ? true : false);
972
 
  ptr->setTableName(table->table.str);
 
946
  ptr->setTableName(table->table.data());
973
947
  ptr->lock_type=   lock_type;
974
948
  ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
975
949
  ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
976
950
  ptr->derived=     table->sel;
977
951
  ptr->select_lex=  lex->current_select;
978
952
  ptr->index_hints= index_hints_arg;
979
 
  ptr->option= option ? option->str : 0;
 
953
  ptr->option= option ? option->data() : NULL;
980
954
  /* check that used name is unique */
981
955
  if (lock_type != TL_IGNORE)
982
956
  {
983
957
    TableList *first_table= (TableList*) table_list.first;
984
958
    for (TableList *tables= first_table; tables; tables= tables->next_local)
985
959
    {
986
 
      if (not my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
987
 
        not my_strcasecmp(system_charset_info, ptr->getSchemaName(), tables->getSchemaName()))
 
960
      if (not table_alias_charset->strcasecmp(alias_str, tables->alias) &&
 
961
        not system_charset_info->strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
988
962
      {
989
963
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
990
964
        return NULL;
999
973
      element
1000
974
      We don't use the offsetof() macro here to avoid warnings from gcc
1001
975
    */
1002
 
    previous_table_ref= (TableList*) ((char*) table_list.next -
1003
 
                                       ((char*) &(ptr->next_local) -
1004
 
                                        (char*) ptr));
 
976
    previous_table_ref= (TableList*) ((char*) table_list.next - ((char*) &(ptr->next_local) - (char*) ptr));
1005
977
    /*
1006
978
      Set next_name_resolution_table of the previous table reference to point
1007
979
      to the current table reference. In effect the list
1074
1046
    - 0, otherwise
1075
1047
*/
1076
1048
 
1077
 
TableList *Select_Lex::end_nested_join(Session *)
 
1049
TableList *Select_Lex::end_nested_join()
1078
1050
{
1079
 
  TableList *ptr;
1080
 
  NestedJoin *nested_join;
1081
 
 
1082
1051
  assert(embedding);
1083
 
  ptr= embedding;
 
1052
  TableList* ptr= embedding;
1084
1053
  join_list= ptr->getJoinList();
1085
1054
  embedding= ptr->getEmbedding();
1086
 
  nested_join= ptr->getNestedJoin();
 
1055
  NestedJoin* nested_join= ptr->getNestedJoin();
1087
1056
  if (nested_join->join_list.size() == 1)
1088
1057
  {
1089
1058
    TableList *embedded= &nested_join->join_list.front();
1091
1060
    embedded->setJoinList(join_list);
1092
1061
    embedded->setEmbedding(embedding);
1093
1062
    join_list->push_front(embedded);
1094
 
    ptr= embedded;
 
1063
    return embedded;
1095
1064
  }
1096
 
  else if (nested_join->join_list.size() == 0)
 
1065
  else if (not nested_join->join_list.size())
1097
1066
  {
1098
1067
    join_list->pop();
1099
 
    ptr= NULL;                                     // return value
 
1068
    return NULL;
1100
1069
  }
1101
1070
  return ptr;
1102
1071
}
1227
1196
 
1228
1197
void Select_Lex::set_lock_for_tables(thr_lock_type lock_type)
1229
1198
{
1230
 
  for (TableList *tables= (TableList*) table_list.first;
1231
 
       tables;
1232
 
       tables= tables->next_local)
 
1199
  for (TableList *tables= (TableList*) table_list.first; tables; tables= tables->next_local)
1233
1200
  {
1234
1201
    tables->lock_type= lock_type;
1235
1202
  }
1573
1540
*/
1574
1541
 
1575
1542
 
1576
 
bool check_string_char_length(lex_string_t *str, const char *err_msg,
 
1543
bool check_string_char_length(str_ref str, const char *err_msg,
1577
1544
                              uint32_t max_char_length, const charset_info_st * const cs,
1578
1545
                              bool no_error)
1579
1546
{
1580
1547
  int well_formed_error;
1581
 
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1582
 
                                      max_char_length, &well_formed_error);
 
1548
  uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
1583
1549
 
1584
 
  if (!well_formed_error &&  str->length == res)
 
1550
  if (!well_formed_error && str.size() == res)
1585
1551
    return false;
1586
1552
 
1587
 
  if (!no_error)
1588
 
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_char_length);
 
1553
  if (not no_error)
 
1554
    my_error(ER_WRONG_STRING_LENGTH, MYF(0), str.data(), err_msg, max_char_length);
1589
1555
  return true;
1590
1556
}
1591
1557
 
1592
1558
 
1593
 
bool check_identifier_name(lex_string_t *str, error_t err_code,
1594
 
                           uint32_t max_char_length,
1595
 
                           const char *param_for_err_msg)
 
1559
bool check_identifier_name(str_ref str, error_t err_code)
1596
1560
{
 
1561
  uint32_t max_char_length= NAME_CHAR_LEN;
1597
1562
  /*
1598
1563
    We don't support non-BMP characters in identifiers at the moment,
1599
1564
    so they should be prohibited until such support is done.
1602
1567
  const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
1603
1568
 
1604
1569
  int well_formed_error;
1605
 
  uint32_t res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
1606
 
                                      max_char_length, &well_formed_error);
 
1570
  uint32_t res= cs->cset->well_formed_len(*cs, str, max_char_length, &well_formed_error);
1607
1571
 
1608
1572
  if (well_formed_error)
1609
1573
  {
1610
 
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str->str);
 
1574
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", str.data());
1611
1575
    return true;
1612
1576
  }
1613
1577
 
1614
 
  if (str->length == res)
 
1578
  if (str.size() == res)
1615
1579
    return false;
1616
1580
 
1617
1581
  switch (err_code)
1619
1583
  case EE_OK:
1620
1584
    break;
1621
1585
  case ER_WRONG_STRING_LENGTH:
1622
 
    my_error(err_code, MYF(0), str->str, param_for_err_msg, max_char_length);
 
1586
    my_error(err_code, MYF(0), str.data(), "", max_char_length);
1623
1587
    break;
1624
1588
  case ER_TOO_LONG_IDENT:
1625
 
    my_error(err_code, MYF(0), str->str);
 
1589
    my_error(err_code, MYF(0), str.data());
1626
1590
    break;
1627
1591
  default:
1628
 
    assert(0);
1629
 
    break;
 
1592
    assert(false);
1630
1593
  }
1631
1594
 
1632
1595
  return true;