~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Marisa Plumb
  • Date: 2010-12-04 02:38:29 UTC
  • mto: This revision was merged to the branch mainline in revision 1984.
  • Revision ID: marisa.plumb@gmail.com-20101204023829-2khzxh30wxi256db
updates to a few sql docs 

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
 
#include <config.h>
20
 
 
 
19
#include "config.h"
21
20
#define DRIZZLE_LEX 1
22
 
 
23
 
#include <drizzled/sql_reserved_words.h>
24
 
 
25
 
#include <drizzled/configmake.h>
26
 
#include <drizzled/item/num.h>
27
 
#include <drizzled/error.h>
28
 
#include <drizzled/session.h>
29
 
#include <drizzled/sql_base.h>
30
 
#include <drizzled/lookup_symbol.h>
31
 
#include <drizzled/index_hint.h>
32
 
#include <drizzled/select_result.h>
 
21
#include "drizzled/configmake.h"
 
22
#include "drizzled/item/num.h"
 
23
#include "drizzled/error.h"
 
24
#include "drizzled/session.h"
 
25
#include "drizzled/sql_base.h"
 
26
#include "drizzled/lookup_symbol.h"
 
27
#include "drizzled/index_hint.h"
33
28
 
34
29
#include <cstdio>
35
30
#include <ctype.h>
36
31
 
37
 
union ParserType;
38
 
 
39
32
using namespace std;
40
33
 
41
34
/* Stay outside of the namespace because otherwise bison goes nuts */
42
 
int base_sql_lex(ParserType *arg, drizzled::Session *yysession);
 
35
int DRIZZLElex(void *arg, void *yysession);
43
36
 
44
37
namespace drizzled
45
38
{
46
39
 
47
 
static int lex_one_token(ParserType *arg, drizzled::Session *yysession);
 
40
static int lex_one_token(void *arg, void *yysession);
48
41
 
49
42
/**
50
43
  save order by and tables in own lists.
52
45
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
53
46
{
54
47
  Order *order;
55
 
 
56
 
  if (!(order = (Order *) session->getMemRoot()->allocate(sizeof(Order))))
57
 
    return true;
58
 
 
 
48
  if (!(order = (Order *) session->alloc(sizeof(Order))))
 
49
    return(1);
59
50
  order->item_ptr= item;
60
51
  order->item= &order->item_ptr;
61
52
  order->asc = asc;
62
53
  order->free_me=0;
63
54
  order->used=0;
64
55
  order->counter_used= 0;
65
 
  list.link_in_list((unsigned char*) order, (unsigned char**) &order->next);
66
 
 
67
 
  return false;
 
56
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
57
  return(0);
68
58
}
69
59
 
70
60
/**
74
64
 
75
65
Lex_input_stream::Lex_input_stream(Session *session,
76
66
                                   const char* buffer,
77
 
                                   unsigned int length) :
78
 
  m_session(session),
 
67
                                   unsigned int length)
 
68
: m_session(session),
79
69
  yylineno(1),
80
70
  yytoklen(0),
81
71
  yylval(NULL),
98
88
  ignore_space(1),
99
89
  in_comment(NO_COMMENT)
100
90
{
101
 
  m_cpp_buf= (char*) session->getMemRoot()->allocate(length + 1);
 
91
  m_cpp_buf= (char*) session->alloc(length + 1);
102
92
  m_cpp_ptr= m_cpp_buf;
103
93
}
104
94
 
125
115
    (m_buf_length / default_charset_info->mbminlen) *
126
116
    my_charset_utf8_bin.mbmaxlen;
127
117
 
128
 
  m_body_utf8= (char *) session->getMemRoot()->allocate(body_utf8_length + 1);
 
118
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
129
119
  m_body_utf8_ptr= m_body_utf8;
130
120
  *m_body_utf8_ptr= 0;
131
121
 
223
213
 
224
214
void lex_start(Session *session)
225
215
{
226
 
  LEX *lex= session->getLex();
 
216
  LEX *lex= session->lex;
227
217
 
228
218
  lex->session= lex->unit.session= session;
229
219
 
230
 
  lex->context_stack.clear();
 
220
  lex->context_stack.empty();
231
221
  lex->unit.init_query();
232
222
  lex->unit.init_select();
233
223
  /* 'parent_lex' is used in init_query() so it must be before it. */
234
224
  lex->select_lex.parent_lex= lex;
235
225
  lex->select_lex.init_query();
236
 
  lex->value_list.clear();
237
 
  lex->update_list.clear();
238
 
  lex->auxiliary_table_list.clear();
 
226
  lex->value_list.empty();
 
227
  lex->update_list.empty();
 
228
  lex->auxiliary_table_list.empty();
239
229
  lex->unit.next= lex->unit.master=
240
230
    lex->unit.link_next= lex->unit.return_to= 0;
241
231
  lex->unit.prev= lex->unit.link_prev= 0;
247
237
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
248
238
  lex->select_lex.options= 0;
249
239
  lex->select_lex.init_order();
250
 
  lex->select_lex.group_list.clear();
 
240
  lex->select_lex.group_list.empty();
251
241
  lex->describe= 0;
252
242
  lex->derived_tables= 0;
253
243
  lex->lock_option= TL_READ;
254
244
  lex->leaf_tables_insert= 0;
255
 
  lex->var_list.clear();
256
245
  lex->select_lex.select_number= 1;
257
246
  lex->length=0;
258
247
  lex->select_lex.in_sum_expr=0;
259
 
  lex->select_lex.group_list.clear();
260
 
  lex->select_lex.order_list.clear();
 
248
  lex->select_lex.group_list.empty();
 
249
  lex->select_lex.order_list.empty();
261
250
  lex->sql_command= SQLCOM_END;
262
251
  lex->duplicates= DUP_ERROR;
263
252
  lex->ignore= 0;
272
261
  lex->nest_level=0 ;
273
262
  lex->allow_sum_func= 0;
274
263
  lex->in_sum_func= NULL;
275
 
  lex->type= 0;
276
264
 
277
265
  lex->is_lex_started= true;
278
266
  lex->statement= NULL;
279
267
  
280
268
  lex->is_cross= false;
 
269
 
281
270
  lex->reset();
282
271
}
283
272
 
291
280
    yacc_yyvs= 0;
292
281
  }
293
282
 
294
 
  safe_delete(result);
295
 
  safe_delete(_create_table);
296
 
  _create_table= NULL;
297
 
  _create_field= NULL;
 
283
  delete result;
298
284
 
299
285
  result= 0;
300
286
  setCacheable(true);
301
287
 
302
 
  safe_delete(statement);
 
288
  delete statement;
 
289
  statement= NULL;
303
290
}
304
291
 
305
292
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
360
347
  char *to;
361
348
  lip->yyUnget();                       // ptr points now after last token char
362
349
  tmp.length= lip->yytoklen=length;
363
 
  tmp.str=(char*) lip->m_session->getMemRoot()->allocate(tmp.length+1);
 
350
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
364
351
  from= lip->get_tok_start() + skip;
365
352
  to= tmp.str;
366
353
  end= to+length;
387
374
*/
388
375
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
389
376
{
390
 
  unsigned char c,sep;
 
377
  register unsigned char c,sep;
391
378
  bool found_escape= false;
392
379
  const CHARSET_INFO * const cs= lip->m_session->charset();
393
380
 
436
423
      end-= post_skip;
437
424
      assert(end >= str);
438
425
 
439
 
      if (!(start= (char*) lip->m_session->getMemRoot()->allocate((uint32_t) (end-str)+1)))
 
426
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
440
427
        return (char*) "";              // memory::SqlAlloc has set error flag
441
428
 
442
429
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
602
589
 
603
590
} /* namespace drizzled */
604
591
/*
605
 
  base_sql_lex remember the following states from the following sql_baselex()
 
592
  DRIZZLElex remember the following states from the following DRIZZLElex()
606
593
 
607
594
  - MY_LEX_EOQ                  Found end of query
608
595
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
609
596
                                (which can't be followed by a signed number)
610
597
*/
611
 
int base_sql_lex(union ParserType *yylval, drizzled::Session *session)
 
598
int DRIZZLElex(void *arg, void *yysession)
612
599
{
 
600
  drizzled::Session *session= (drizzled::Session *)yysession;
613
601
  drizzled::Lex_input_stream *lip= session->m_lip;
 
602
  YYSTYPE *yylval=(YYSTYPE*) arg;
614
603
  int token;
615
604
 
616
605
  if (lip->lookahead_token != END_OF_INPUT)
626
615
    return token;
627
616
  }
628
617
 
629
 
  token= drizzled::lex_one_token(yylval, session);
 
618
  token= drizzled::lex_one_token(arg, yysession);
630
619
 
631
620
  switch(token) {
632
621
  case WITH:
637
626
      to transform the grammar into a LALR(1) grammar,
638
627
      which sql_yacc.yy can process.
639
628
    */
640
 
    token= drizzled::lex_one_token(yylval, session);
 
629
    token= drizzled::lex_one_token(arg, yysession);
641
630
    if (token == ROLLUP_SYM)
642
631
    {
643
632
      return WITH_ROLLUP_SYM;
662
651
namespace drizzled
663
652
{
664
653
 
665
 
int lex_one_token(ParserType *yylval, drizzled::Session *session)
 
654
int lex_one_token(void *arg, void *yysession)
666
655
{
667
 
  unsigned char c= 0; /* Just set to shutup GCC */
 
656
  register unsigned char c= 0; /* Just set to shutup GCC */
668
657
  bool comment_closed;
669
658
  int   tokval, result_state;
670
659
  unsigned int length;
671
660
  enum my_lex_states state;
 
661
  Session *session= (Session *)yysession;
672
662
  Lex_input_stream *lip= session->m_lip;
673
 
  LEX *lex= session->getLex();
 
663
  LEX *lex= session->lex;
 
664
  YYSTYPE *yylval=(YYSTYPE*) arg;
674
665
  const CHARSET_INFO * const cs= session->charset();
675
666
  unsigned char *state_map= cs->state_map;
676
667
  unsigned char *ident_map= cs->ident_map;
1334
1325
  table= 0;
1335
1326
  fake_select_lex= 0;
1336
1327
  cleaned= 0;
1337
 
  item_list.clear();
 
1328
  item_list.empty();
1338
1329
  describe= 0;
1339
1330
  found_rows_for_union= 0;
1340
1331
}
1342
1333
void Select_Lex::init_query()
1343
1334
{
1344
1335
  Select_Lex_Node::init_query();
1345
 
  table_list.clear();
1346
 
  top_join_list.clear();
 
1336
  table_list.empty();
 
1337
  top_join_list.empty();
1347
1338
  join_list= &top_join_list;
1348
1339
  embedding= leaf_tables= 0;
1349
 
  item_list.clear();
 
1340
  item_list.empty();
1350
1341
  join= 0;
1351
1342
  having= where= 0;
1352
1343
  olap= UNSPECIFIED_OLAP_TYPE;
1378
1369
 
1379
1370
void Select_Lex::init_select()
1380
1371
{
1381
 
  sj_nests.clear();
1382
 
  group_list.clear();
 
1372
  sj_nests.empty();
 
1373
  group_list.empty();
1383
1374
  db= 0;
1384
1375
  having= 0;
1385
1376
  in_sum_expr= with_wild= 0;
1386
1377
  options= 0;
1387
1378
  braces= 0;
1388
 
  interval_list.clear();
 
1379
  interval_list.empty();
1389
1380
  inner_sum_func_list= 0;
1390
1381
  linkage= UNSPECIFIED_TYPE;
1391
1382
  order_list.elements= 0;
1395
1386
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1396
1387
  offset_limit= 0;      /* denotes the default offset = 0 */
1397
1388
  with_sum_func= 0;
1398
 
  is_cross= false;
1399
1389
  is_correlated= 0;
1400
1390
  cur_pos_in_select_list= UNDEF_POS;
1401
 
  non_agg_fields.clear();
 
1391
  non_agg_fields.empty();
1402
1392
  cond_value= having_value= Item::COND_UNDEF;
1403
 
  inner_refs_list.clear();
 
1393
  inner_refs_list.empty();
1404
1394
  full_group_by_flag.reset();
1405
1395
}
1406
1396
 
1711
1701
    return false;
1712
1702
 
1713
1703
  return (ref_pointer_array=
1714
 
          (Item **)session->getMemRoot()->allocate(sizeof(Item*) * (n_child_sum_items +
 
1704
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1715
1705
                                                 item_list.elements +
1716
1706
                                                 select_n_having_items +
1717
1707
                                                 select_n_where_fields +
1811
1801
  }
1812
1802
}
1813
1803
 
1814
 
LEX::~LEX()
 
1804
/**
 
1805
  @brief Restore the LEX and Session in case of a parse error.
 
1806
 
 
1807
  This is a clean up call that is invoked by the Bison generated
 
1808
  parser before returning an error from DRIZZLEparse. If your
 
1809
  semantic actions manipulate with the global thread state (which
 
1810
  is a very bad practice and should not normally be employed) and
 
1811
  need a clean-up in case of error, and you can not use %destructor
 
1812
  rule in the grammar file itself, this function should be used
 
1813
  to implement the clean up.
 
1814
*/
 
1815
void LEX::cleanup_lex_after_parse_error(Session *)
1815
1816
{
1816
 
  delete _create_table;
1817
1817
}
1818
1818
 
1819
1819
/*
1863
1863
    statement parsing. On should use lex_start() function to prepare LEX
1864
1864
    for this.
1865
1865
*/
1866
 
LEX::LEX() :
 
1866
LEX::LEX()
 
1867
  :
1867
1868
    result(0), 
1868
1869
    yacc_yyss(0), 
1869
1870
    yacc_yyvs(0),
1870
 
    session(NULL),
1871
1871
    charset(NULL),
1872
 
    var_list(),
1873
1872
    sql_command(SQLCOM_END), 
1874
 
    statement(NULL),
1875
1873
    option_type(OPT_DEFAULT), 
1876
1874
    is_lex_started(0),
1877
1875
    cacheable(true),
1878
 
    sum_expr_used(false),
1879
 
    _create_table(NULL),
1880
 
    _create_field(NULL),
1881
 
    _exists(false)
 
1876
    sum_expr_used(false)
1882
1877
{
1883
1878
  reset_query_tables_list(true);
 
1879
  statement= NULL;
1884
1880
}
1885
1881
 
1886
1882
/**
2068
2064
void LEX::cleanup_after_one_table_open()
2069
2065
{
2070
2066
  /*
2071
 
    session->getLex()->derived_tables & additional units may be set if we open
2072
 
    a view. It is necessary to clear session->getLex()->derived_tables flag
 
2067
    session->lex->derived_tables & additional units may be set if we open
 
2068
    a view. It is necessary to clear session->lex->derived_tables flag
2073
2069
    to prevent processing of derived tables during next openTablesLock
2074
2070
    if next table is a real table and cleanup & remove underlying units
2075
 
    NOTE: all units will be connected to session->getLex()->select_lex, because we
 
2071
    NOTE: all units will be connected to session->lex->select_lex, because we
2076
2072
    have not UNION on most upper level.
2077
2073
    */
2078
2074
  if (all_selects_list != &select_lex)
2156
2152
                                            str, length));
2157
2153
}
2158
2154
 
2159
 
bool check_for_sql_keyword(drizzled::lex_string_t const& string)
2160
 
{
2161
 
  if (sql_reserved_words::in_word_set(string.str, string.length))
2162
 
      return true;
2163
 
 
2164
 
  return false;
2165
 
}
2166
 
 
2167
 
bool check_for_sql_keyword(drizzled::st_lex_symbol const& string)
2168
 
{
2169
 
  if (sql_reserved_words::in_word_set(string.str, string.length))
2170
 
      return true;
2171
 
 
2172
 
  return false;
2173
 
}
2174
 
 
2175
 
 
2176
2155
} /* namespace drizzled */