~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Brian Aker
  • Date: 2011-01-24 23:10:42 UTC
  • mto: This revision was merged to the branch mainline in revision 2113.
  • Revision ID: brian@tangent.org-20110124231042-hg2zx5cq1angsgjy
Minor cleanup, drop one of two needs for current session during parsing.

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>
 
20
#include "drizzled/abort_exception.h"
22
21
#include <drizzled/my_hash.h>
23
22
#include <drizzled/error.h>
24
23
#include <drizzled/nested_join.h>
25
24
#include <drizzled/query_id.h>
26
 
#include <drizzled/transaction_services.h>
 
25
#include "drizzled/transaction_services.h"
27
26
#include <drizzled/sql_parse.h>
28
27
#include <drizzled/data_home.h>
29
28
#include <drizzled/sql_base.h>
30
29
#include <drizzled/show.h>
 
30
#include <drizzled/db.h>
31
31
#include <drizzled/function/time/unix_timestamp.h>
32
32
#include <drizzled/function/get_system_var.h>
33
33
#include <drizzled/item/cmpfunc.h>
34
34
#include <drizzled/item/null.h>
35
35
#include <drizzled/session.h>
36
 
#include <drizzled/session/cache.h>
37
36
#include <drizzled/sql_load.h>
38
37
#include <drizzled/lock.h>
39
38
#include <drizzled/select_send.h>
40
39
#include <drizzled/plugin/client.h>
41
40
#include <drizzled/statement.h>
42
41
#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>
 
42
#include "drizzled/probes.h"
 
43
#include "drizzled/session/cache.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"
56
53
 
57
54
#include <limits.h>
58
55
 
59
56
#include <bitset>
60
57
#include <algorithm>
61
58
#include <boost/date_time.hpp>
62
 
#include <drizzled/internal/my_sys.h>
 
59
#include "drizzled/internal/my_sys.h"
63
60
 
64
61
using namespace std;
65
62
 
66
 
extern int base_sql_parse(drizzled::Session *session); // from sql_yacc.cc
 
63
extern int DRIZZLEparse(void *session); // from sql_yacc.cc
67
64
 
68
65
namespace drizzled
69
66
{
70
67
 
71
68
/* Prototypes */
72
 
bool my_yyoverflow(short **a, ParserType **b, ulong *yystacksize);
 
69
bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
73
70
static bool parse_sql(Session *session, Lex_input_stream *lip);
74
71
void parse(Session *session, const char *inBuf, uint32_t length);
75
72
 
81
78
extern size_t my_thread_stack_size;
82
79
extern const CHARSET_INFO *character_set_filesystem;
83
80
 
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
 
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
96
90
};
97
91
 
98
 
}
99
 
 
100
92
const char *xa_state_names[]={
101
93
  "NON-EXISTING", "ACTIVE", "IDLE", "PREPARED"
102
94
};
115
107
*/
116
108
bitset<CF_BIT_SIZE> sql_command_flags[SQLCOM_END+1];
117
109
 
118
 
const std::string &getCommandName(const enum_server_command& command)
119
 
{
120
 
  return command_name[command];
121
 
}
122
 
 
123
110
void init_update_queries(void)
124
111
{
125
112
  uint32_t x;
168
155
                         can be zero.
169
156
 
170
157
  @todo
171
 
    set session->getLex()->sql_command to SQLCOM_END here.
 
158
    set session->lex->sql_command to SQLCOM_END here.
172
159
  @todo
173
160
    The following has to be changed to an 8 byte integer
174
161
 
187
174
  DRIZZLE_COMMAND_START(session->thread_id, command);
188
175
 
189
176
  session->command= command;
190
 
  session->getLex()->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
 
177
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
191
178
  session->set_time();
192
179
  session->setQueryId(query_id.value());
193
180
 
201
188
    query_id.next();
202
189
  }
203
190
 
204
 
  /* @todo set session->getLex()->sql_command to SQLCOM_END here */
 
191
  /* @todo set session->lex->sql_command to SQLCOM_END here */
205
192
 
206
193
  plugin::Logging::preDo(session);
207
194
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
224
211
 
225
212
    identifier::Schema identifier(tmp);
226
213
 
227
 
    if (not schema::change(*session, identifier))
 
214
    if (not change_db(session, identifier))
228
215
    {
229
216
      session->my_ok();
230
217
    }
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();
446
433
static int 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
 
723
684
  @param var_name               Variable name
724
685
*/
725
686
 
726
 
void create_select_for_variable(Session *session, const char *var_name)
 
687
void create_select_for_variable(const char *var_name)
727
688
{
 
689
  Session *session;
728
690
  LEX *lex;
729
691
  LEX_STRING tmp, null_lex_string;
730
692
  Item *var;
731
693
  char buff[MAX_SYS_VAR_LENGTH*2+4+8];
732
694
  char *end= buff;
733
695
 
734
 
  lex= session->getLex();
 
696
  session= current_session;
 
697
  lex= session->lex;
735
698
  init_select(lex);
736
699
  lex->sql_command= SQLCOM_SELECT;
737
700
  tmp.str= (char*) var_name;
747
710
    var->set_name(buff, end-buff, system_charset_info);
748
711
    session->add_item_to_list(var);
749
712
  }
 
713
  return;
750
714
}
751
715
 
752
716
 
760
724
 
761
725
void parse(Session *session, const char *inBuf, uint32_t length)
762
726
{
763
 
  session->getLex()->start(session);
 
727
  session->lex->start(session);
764
728
 
765
729
  session->reset_for_next_command();
766
730
  /* Check if the Query is Cached if and return true if yes
776
740
  {
777
741
    return;
778
742
  }
779
 
  LEX *lex= session->getLex();
 
743
  LEX *lex= session->lex;
780
744
  Lex_input_stream lip(session, inBuf, length);
781
745
  bool err= parse_sql(session, &lip);
782
746
  if (!err)
833
797
                       List<String> *interval_list, const CHARSET_INFO * const cs)
834
798
{
835
799
  register CreateField *new_field;
836
 
  LEX  *lex= session->getLex();
 
800
  LEX  *lex= session->lex;
837
801
  statement::AlterTable *statement= (statement::AlterTable *)lex->statement;
838
802
 
839
803
  if (check_identifier_name(field_name, ER_TOO_LONG_IDENT))
847
811
                      &default_key_create_info,
848
812
                      0, lex->col_list);
849
813
    statement->alter_info.key_list.push_back(key);
850
 
    lex->col_list.clear();
 
814
    lex->col_list.empty();
851
815
  }
852
816
  if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
853
817
  {
857
821
                 &default_key_create_info, 0,
858
822
                 lex->col_list);
859
823
    statement->alter_info.key_list.push_back(key);
860
 
    lex->col_list.clear();
 
824
    lex->col_list.empty();
861
825
  }
862
826
 
863
827
  if (default_value)
911
875
}
912
876
 
913
877
 
 
878
/** Store position for column in ALTER TABLE .. ADD column. */
 
879
 
 
880
void store_position_for_column(const char *name)
 
881
{
 
882
  current_session->lex->last_field->after=const_cast<char*> (name);
 
883
}
 
884
 
914
885
/**
915
886
  Add a table to list of used tables.
916
887
 
941
912
  TableList *ptr;
942
913
  TableList *previous_table_ref; /* The table preceding the current one. */
943
914
  char *alias_str;
944
 
  LEX *lex= session->getLex();
 
915
  LEX *lex= session->lex;
945
916
 
946
917
  if (!table)
947
918
    return NULL;                                // End of memory
958
929
    my_casedn_str(files_charset_info, table->db.str);
959
930
 
960
931
    identifier::Schema schema_identifier(string(table->db.str));
961
 
    if (not schema::check(*session, schema_identifier))
 
932
    if (not check_db_name(session, schema_identifier))
962
933
    {
963
934
 
964
935
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
974
945
                 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
975
946
      return NULL;
976
947
    }
977
 
    if (!(alias_str= (char*) session->getMemRoot()->duplicate(alias_str,table->table.length+1)))
 
948
    if (!(alias_str= (char*) session->memdup(alias_str,table->table.length+1)))
978
949
      return NULL;
979
950
  }
980
951
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
1075
1046
bool Select_Lex::init_nested_join(Session *session)
1076
1047
{
1077
1048
  TableList *ptr;
1078
 
  NestedJoin *nested_join;
 
1049
  nested_join_st *nested_join;
1079
1050
 
1080
1051
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1081
 
                                       sizeof(NestedJoin))))
 
1052
                                       sizeof(nested_join_st))))
1082
1053
    return true;
1083
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1054
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1084
1055
  nested_join= ptr->getNestedJoin();
1085
1056
  join_list->push_front(ptr);
1086
1057
  ptr->setEmbedding(embedding);
1088
1059
  ptr->alias= (char*) "(nested_join)";
1089
1060
  embedding= ptr;
1090
1061
  join_list= &nested_join->join_list;
1091
 
  join_list->clear();
 
1062
  join_list->empty();
1092
1063
  return false;
1093
1064
}
1094
1065
 
1110
1081
TableList *Select_Lex::end_nested_join(Session *)
1111
1082
{
1112
1083
  TableList *ptr;
1113
 
  NestedJoin *nested_join;
 
1084
  nested_join_st *nested_join;
1114
1085
 
1115
1086
  assert(embedding);
1116
1087
  ptr= embedding;
1151
1122
TableList *Select_Lex::nest_last_join(Session *session)
1152
1123
{
1153
1124
  TableList *ptr;
1154
 
  NestedJoin *nested_join;
 
1125
  nested_join_st *nested_join;
1155
1126
  List<TableList> *embedded_list;
1156
1127
 
1157
1128
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1158
 
                                          sizeof(NestedJoin))))
 
1129
                                          sizeof(nested_join_st))))
1159
1130
    return NULL;
1160
 
  ptr->setNestedJoin(((NestedJoin*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
 
1131
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1161
1132
  nested_join= ptr->getNestedJoin();
1162
1133
  ptr->setEmbedding(embedding);
1163
1134
  ptr->setJoinList(join_list);
1164
1135
  ptr->alias= (char*) "(nest_last_join)";
1165
1136
  embedded_list= &nested_join->join_list;
1166
 
  embedded_list->clear();
 
1137
  embedded_list->empty();
1167
1138
 
1168
1139
  for (uint32_t i=0; i < 2; i++)
1169
1140
  {
1311
1282
  fake_select_lex->include_standalone(this,
1312
1283
                                      (Select_Lex_Node**)&fake_select_lex);
1313
1284
  fake_select_lex->select_number= INT_MAX;
1314
 
  fake_select_lex->parent_lex= session_arg->getLex(); /* Used in init_query. */
 
1285
  fake_select_lex->parent_lex= session_arg->lex; /* Used in init_query. */
1315
1286
  fake_select_lex->make_empty_select();
1316
1287
  fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
1317
1288
  fake_select_lex->select_limit= 0;
1331
1302
    */
1332
1303
    global_parameters= fake_select_lex;
1333
1304
    fake_select_lex->no_table_names_allowed= 1;
1334
 
    session_arg->getLex()->current_select= fake_select_lex;
 
1305
    session_arg->lex->current_select= fake_select_lex;
1335
1306
  }
1336
 
  session_arg->getLex()->pop_context();
 
1307
  session_arg->lex->pop_context();
1337
1308
  return(0);
1338
1309
}
1339
1310
 
1369
1340
    left_op->first_leaf_for_name_resolution();
1370
1341
  on_context->last_name_resolution_table=
1371
1342
    right_op->last_leaf_for_name_resolution();
1372
 
  return session->getLex()->push_context(on_context);
 
1343
  return session->lex->push_context(on_context);
1373
1344
}
1374
1345
 
1375
1346
 
1459
1430
 
1460
1431
bool check_simple_select(Session::pointer session)
1461
1432
{
1462
 
  if (session->getLex()->current_select != &session->getLex()->select_lex)
 
1433
  LEX *lex= session->lex;
 
1434
  if (lex->current_select != &lex->select_lex)
1463
1435
  {
1464
1436
    char command[80];
1465
1437
    Lex_input_stream *lip= session->m_lip;
1519
1491
bool update_precheck(Session *session, TableList *)
1520
1492
{
1521
1493
  const char *msg= 0;
1522
 
  Select_Lex *select_lex= &session->getLex()->select_lex;
 
1494
  LEX *lex= session->lex;
 
1495
  Select_Lex *select_lex= &lex->select_lex;
1523
1496
 
1524
 
  if (session->getLex()->select_lex.item_list.elements != session->getLex()->value_list.elements)
 
1497
  if (session->lex->select_lex.item_list.elements != session->lex->value_list.elements)
1525
1498
  {
1526
1499
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1527
1500
    return(true);
1528
1501
  }
1529
1502
 
1530
 
  if (session->getLex()->select_lex.table_list.elements > 1)
 
1503
  if (session->lex->select_lex.table_list.elements > 1)
1531
1504
  {
1532
1505
    if (select_lex->order_list.elements)
1533
1506
      msg= "ORDER BY";
1557
1530
 
1558
1531
bool insert_precheck(Session *session, TableList *)
1559
1532
{
 
1533
  LEX *lex= session->lex;
 
1534
 
1560
1535
  /*
1561
1536
    Check that we have modify privileges for the first table and
1562
1537
    select privileges for the rest
1563
1538
  */
1564
 
  if (session->getLex()->update_list.elements != session->getLex()->value_list.elements)
 
1539
  if (lex->update_list.elements != lex->value_list.elements)
1565
1540
  {
1566
1541
    my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
1567
1542
    return(true);
1588
1563
  {
1589
1564
    /* it is NOT(NOT( ... )) */
1590
1565
    Item *arg= ((Item_func *) expr)->arguments()[0];
1591
 
    enum_parsing_place place= session->getLex()->current_select->parsing_place;
 
1566
    enum_parsing_place place= session->lex->current_select->parsing_place;
1592
1567
    if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
1593
1568
      return arg;
1594
1569
    /*
1704
1679
 
1705
1680
  /* Parse the query. */
1706
1681
 
1707
 
  bool parse_status= base_sql_parse(session) != 0;
 
1682
  bool parse_status= DRIZZLEparse(session) != 0;
1708
1683
 
1709
1684
  /* Check that if DRIZZLEparse() failed, session->is_error() is set. */
1710
1685