~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Monty Taylor
  • Date: 2009-12-25 08:50:15 UTC
  • mto: This revision was merged to the branch mainline in revision 1255.
  • Revision ID: mordred@inaugust.com-20091225085015-83sux5qsvy312gew
MEM_ROOT == memory::Root

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
 
19
#define DRIZZLE_LEX 1
19
20
#include "config.h"
20
 
 
21
 
#define DRIZZLE_LEX 1
22
 
 
23
 
#include "drizzled/sql_reserved_words.h"
24
 
 
25
21
#include "drizzled/configmake.h"
26
22
#include "drizzled/item/num.h"
27
23
#include "drizzled/error.h"
30
26
#include "drizzled/lookup_symbol.h"
31
27
#include "drizzled/index_hint.h"
32
28
 
33
 
#include <cstdio>
34
29
#include <ctype.h>
35
30
 
36
31
using namespace std;
37
32
 
38
 
/* Stay outside of the namespace because otherwise bison goes nuts */
 
33
static int lex_one_token(void *arg, void *yysession);
39
34
int DRIZZLElex(void *arg, void *yysession);
40
35
 
41
 
namespace drizzled
42
 
{
43
 
 
44
 
static int lex_one_token(void *arg, void *yysession);
45
 
 
46
36
/**
47
37
  save order by and tables in own lists.
48
38
*/
49
39
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
50
40
{
51
 
  Order *order;
52
 
 
53
 
  if (!(order = (Order *) session->getMemRoot()->allocate(sizeof(Order))))
54
 
    return true;
55
 
 
 
41
  order_st *order;
 
42
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
 
43
    return(1);
56
44
  order->item_ptr= item;
57
45
  order->item= &order->item_ptr;
58
46
  order->asc = asc;
59
47
  order->free_me=0;
60
48
  order->used=0;
61
49
  order->counter_used= 0;
62
 
  list.link_in_list((unsigned char*) order, (unsigned char**) &order->next);
63
 
 
64
 
  return false;
 
50
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
51
  return(0);
65
52
}
66
53
 
67
54
/**
71
58
 
72
59
Lex_input_stream::Lex_input_stream(Session *session,
73
60
                                   const char* buffer,
74
 
                                   unsigned int length) :
75
 
  m_session(session),
 
61
                                   unsigned int length)
 
62
: m_session(session),
76
63
  yylineno(1),
77
64
  yytoklen(0),
78
65
  yylval(NULL),
92
79
  m_body_utf8(NULL),
93
80
  m_cpp_utf8_processed_ptr(NULL),
94
81
  next_state(MY_LEX_START),
 
82
  found_semicolon(NULL),
95
83
  ignore_space(1),
96
84
  in_comment(NO_COMMENT)
97
85
{
98
 
  m_cpp_buf= (char*) session->getMemRoot()->allocate(length + 1);
 
86
  m_cpp_buf= (char*) session->alloc(length + 1);
99
87
  m_cpp_ptr= m_cpp_buf;
100
88
}
101
89
 
122
110
    (m_buf_length / default_charset_info->mbminlen) *
123
111
    my_charset_utf8_bin.mbmaxlen;
124
112
 
125
 
  m_body_utf8= (char *) session->getMemRoot()->allocate(body_utf8_length + 1);
 
113
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
126
114
  m_body_utf8_ptr= m_body_utf8;
127
115
  *m_body_utf8_ptr= 0;
128
116
 
213
201
  Because of this, it's critical to not do too much things here.
214
202
  (We already do too much here)
215
203
*/
216
 
void LEX::start(Session *arg)
217
 
{
218
 
  lex_start(arg);
219
 
}
220
 
 
221
204
void lex_start(Session *session)
222
205
{
223
206
  LEX *lex= session->lex;
249
232
  lex->derived_tables= 0;
250
233
  lex->lock_option= TL_READ;
251
234
  lex->leaf_tables_insert= 0;
252
 
  lex->var_list.clear();
253
235
  lex->select_lex.select_number= 1;
254
236
  lex->length=0;
255
237
  lex->select_lex.in_sum_expr=0;
269
251
  lex->nest_level=0 ;
270
252
  lex->allow_sum_func= 0;
271
253
  lex->in_sum_func= NULL;
272
 
  lex->type= 0;
273
254
 
274
255
  lex->is_lex_started= true;
275
256
  lex->statement= NULL;
276
 
  
277
 
  lex->is_cross= false;
278
 
  lex->reset();
279
257
}
280
258
 
281
 
void LEX::end()
 
259
void lex_end(LEX *lex)
282
260
{
283
 
  if (yacc_yyss)
 
261
  if (lex->yacc_yyss)
284
262
  {
285
 
    free(yacc_yyss);
286
 
    free(yacc_yyvs);
287
 
    yacc_yyss= 0;
288
 
    yacc_yyvs= 0;
 
263
    free(lex->yacc_yyss);
 
264
    free(lex->yacc_yyvs);
 
265
    lex->yacc_yyss= 0;
 
266
    lex->yacc_yyvs= 0;
289
267
  }
290
268
 
291
 
  delete result;
292
 
  delete _create_table;
293
 
  _create_table= NULL;
294
 
  _create_field= NULL;
295
 
 
296
 
  result= 0;
297
 
  setCacheable(true);
298
 
 
299
 
  delete statement;
300
 
  statement= NULL;
 
269
  delete lex->result;
 
270
 
 
271
  lex->result= 0;
 
272
 
 
273
  if (lex->statement) 
 
274
    delete lex->statement;
301
275
}
302
276
 
303
277
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
358
332
  char *to;
359
333
  lip->yyUnget();                       // ptr points now after last token char
360
334
  tmp.length= lip->yytoklen=length;
361
 
  tmp.str=(char*) lip->m_session->getMemRoot()->allocate(tmp.length+1);
 
335
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
362
336
  from= lip->get_tok_start() + skip;
363
337
  to= tmp.str;
364
338
  end= to+length;
434
408
      end-= post_skip;
435
409
      assert(end >= str);
436
410
 
437
 
      if (!(start= (char*) lip->m_session->getMemRoot()->allocate((uint32_t) (end-str)+1)))
438
 
        return (char*) "";              // memory::SqlAlloc has set error flag
 
411
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
 
412
        return (char*) "";              // Sql_alloc has set error flag
439
413
 
440
414
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
441
415
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
598
572
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
599
573
}
600
574
 
601
 
} /* namespace drizzled */
602
575
/*
603
576
  DRIZZLElex remember the following states from the following DRIZZLElex()
604
577
 
608
581
*/
609
582
int DRIZZLElex(void *arg, void *yysession)
610
583
{
611
 
  drizzled::Session *session= (drizzled::Session *)yysession;
612
 
  drizzled::Lex_input_stream *lip= session->m_lip;
 
584
  Session *session= (Session *)yysession;
 
585
  Lex_input_stream *lip= session->m_lip;
613
586
  YYSTYPE *yylval=(YYSTYPE*) arg;
614
587
  int token;
615
588
 
626
599
    return token;
627
600
  }
628
601
 
629
 
  token= drizzled::lex_one_token(arg, yysession);
 
602
  token= lex_one_token(arg, yysession);
630
603
 
631
604
  switch(token) {
632
605
  case WITH:
637
610
      to transform the grammar into a LALR(1) grammar,
638
611
      which sql_yacc.yy can process.
639
612
    */
640
 
    token= drizzled::lex_one_token(arg, yysession);
 
613
    token= lex_one_token(arg, yysession);
641
614
    if (token == ROLLUP_SYM)
642
615
    {
643
616
      return WITH_ROLLUP_SYM;
659
632
  return token;
660
633
}
661
634
 
662
 
namespace drizzled
663
 
{
664
 
 
665
635
int lex_one_token(void *arg, void *yysession)
666
636
{
667
637
  register unsigned char c= 0; /* Just set to shutup GCC */
1315
1285
  options= 0;
1316
1286
  linkage= UNSPECIFIED_TYPE;
1317
1287
  no_error= no_table_names_allowed= 0;
1318
 
  uncacheable.reset();
 
1288
  uncacheable= 0;
1319
1289
}
1320
1290
 
1321
1291
void Select_Lex_Node::init_select()
1384
1354
  group_list.empty();
1385
1355
  db= 0;
1386
1356
  having= 0;
 
1357
  table_join_options= 0;
1387
1358
  in_sum_expr= with_wild= 0;
1388
1359
  options= 0;
1389
1360
  braces= 0;
1397
1368
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1398
1369
  offset_limit= 0;      /* denotes the default offset = 0 */
1399
1370
  with_sum_func= 0;
1400
 
  is_cross= false;
1401
1371
  is_correlated= 0;
1402
1372
  cur_pos_in_select_list= UNDEF_POS;
1403
1373
  non_agg_fields.empty();
1581
1551
       s && s != last;
1582
1552
       s= s->outer_select())
1583
1553
  {
1584
 
    if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
 
1554
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1585
1555
    {
1586
1556
      // Select is dependent of outer select
1587
 
      s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1588
 
      s->uncacheable.set(UNCACHEABLE_UNITED);
 
1557
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1558
                       UNCACHEABLE_DEPENDENT;
1589
1559
      Select_Lex_Unit *munit= s->master_unit();
1590
 
      munit->uncacheable.set(UNCACHEABLE_UNITED);
1591
 
      munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
 
1560
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1561
                       UNCACHEABLE_DEPENDENT;
1592
1562
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1593
1563
      {
1594
1564
        if (sl != s &&
1595
 
            ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1596
 
          sl->uncacheable.set(UNCACHEABLE_UNITED);
 
1565
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1566
          sl->uncacheable|= UNCACHEABLE_UNITED;
1597
1567
      }
1598
1568
    }
1599
1569
    s->is_correlated= true;
1618
1588
List<Item>* Select_Lex_Node::get_item_list()
1619
1589
{ return NULL; }
1620
1590
 
1621
 
TableList *Select_Lex_Node::add_table_to_list(Session *, 
1622
 
                                              Table_ident *, 
1623
 
                                              LEX_STRING *, 
1624
 
                                              const bitset<NUM_OF_TABLE_OPTIONS>&,
1625
 
                                              thr_lock_type, 
1626
 
                                              List<Index_hint> *, 
1627
 
                                              LEX_STRING *)
 
1591
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1592
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1628
1593
{
1629
1594
  return 0;
1630
1595
}
1631
1596
 
 
1597
uint32_t Select_Lex_Node::get_table_join_options()
 
1598
{
 
1599
  return 0;
 
1600
}
1632
1601
 
1633
1602
/*
1634
1603
  prohibit using LIMIT clause
1706
1675
  return &item_list;
1707
1676
}
1708
1677
 
 
1678
uint32_t Select_Lex::get_table_join_options()
 
1679
{
 
1680
  return table_join_options;
 
1681
}
1709
1682
 
1710
1683
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1711
1684
{
1713
1686
    return false;
1714
1687
 
1715
1688
  return (ref_pointer_array=
1716
 
          (Item **)session->getMemRoot()->allocate(sizeof(Item*) * (n_child_sum_items +
 
1689
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1717
1690
                                                 item_list.elements +
1718
1691
                                                 select_n_having_items +
1719
1692
                                                 select_n_where_fields +
1746
1719
      str->append(STRING_WITH_LEN(" order by "));
1747
1720
      fake_select_lex->print_order(
1748
1721
        str,
1749
 
        (Order *) fake_select_lex->order_list.first,
 
1722
        (order_st *) fake_select_lex->order_list.first,
1750
1723
        query_type);
1751
1724
    }
1752
1725
    fake_select_lex->print_limit(session, str, query_type);
1754
1727
}
1755
1728
 
1756
1729
void Select_Lex::print_order(String *str,
1757
 
                                Order *order,
 
1730
                                order_st *order,
1758
1731
                                enum_query_type query_type)
1759
1732
{
1760
1733
  for (; order; order= order->next)
1813
1786
  }
1814
1787
}
1815
1788
 
1816
 
LEX::~LEX()
 
1789
/**
 
1790
  @brief Restore the LEX and Session in case of a parse error.
 
1791
 
 
1792
  This is a clean up call that is invoked by the Bison generated
 
1793
  parser before returning an error from DRIZZLEparse. If your
 
1794
  semantic actions manipulate with the global thread state (which
 
1795
  is a very bad practice and should not normally be employed) and
 
1796
  need a clean-up in case of error, and you can not use %destructor
 
1797
  rule in the grammar file itself, this function should be used
 
1798
  to implement the clean up.
 
1799
*/
 
1800
void LEX::cleanup_lex_after_parse_error(Session *)
1817
1801
{
1818
 
  delete _create_table;
1819
1802
}
1820
1803
 
1821
1804
/*
1865
1848
    statement parsing. On should use lex_start() function to prepare LEX
1866
1849
    for this.
1867
1850
*/
1868
 
LEX::LEX() :
1869
 
    result(0), 
1870
 
    yacc_yyss(0), 
1871
 
    yacc_yyvs(0),
1872
 
    session(NULL),
1873
 
    charset(NULL),
1874
 
    var_list(),
1875
 
    sql_command(SQLCOM_END), 
1876
 
    statement(NULL),
1877
 
    option_type(OPT_DEFAULT), 
1878
 
    is_lex_started(0),
1879
 
    cacheable(true),
1880
 
    sum_expr_used(false),
1881
 
    _create_table(NULL),
1882
 
    _create_field(NULL),
1883
 
    _exists(false)
 
1851
LEX::LEX()
 
1852
  :result(0), yacc_yyss(0), yacc_yyvs(0),
 
1853
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1884
1854
{
1885
1855
  reset_query_tables_list(true);
 
1856
  statement= NULL;
 
1857
}
 
1858
 
 
1859
/*
 
1860
  Detect that we need only table structure of derived table/view
 
1861
 
 
1862
  SYNOPSIS
 
1863
    only_view_structure()
 
1864
 
 
1865
  RETURN
 
1866
    true yes, we need only structure
 
1867
    false no, we need data
 
1868
*/
 
1869
bool LEX::only_view_structure()
 
1870
{
 
1871
  switch (sql_command) {
 
1872
  case SQLCOM_SHOW_CREATE:
 
1873
  case SQLCOM_SHOW_TABLES:
 
1874
  case SQLCOM_SHOW_FIELDS:
 
1875
    return true;
 
1876
  default:
 
1877
    return false;
 
1878
  }
 
1879
}
 
1880
 
 
1881
/*
 
1882
  Should Items_ident be printed correctly
 
1883
 
 
1884
  SYNOPSIS
 
1885
    need_correct_ident()
 
1886
 
 
1887
  RETURN
 
1888
    true yes, we need only structure
 
1889
    false no, we need data
 
1890
*/
 
1891
bool LEX::need_correct_ident()
 
1892
{
 
1893
  switch(sql_command)
 
1894
  {
 
1895
  case SQLCOM_SHOW_CREATE:
 
1896
  case SQLCOM_SHOW_TABLES:
 
1897
    return true;
 
1898
  default:
 
1899
    return false;
 
1900
  }
1886
1901
}
1887
1902
 
1888
1903
/**
2157
2172
                                            current_index_hint_clause,
2158
2173
                                            str, length));
2159
2174
}
2160
 
 
2161
 
bool check_for_sql_keyword(drizzled::lex_string_t const& string)
2162
 
{
2163
 
  if (sql_reserved_words::in_word_set(string.str, string.length))
2164
 
      return true;
2165
 
 
2166
 
  return false;
2167
 
}
2168
 
 
2169
 
bool check_for_sql_keyword(drizzled::st_lex_symbol const& string)
2170
 
{
2171
 
  if (sql_reserved_words::in_word_set(string.str, string.length))
2172
 
      return true;
2173
 
 
2174
 
  return false;
2175
 
}
2176
 
 
2177
 
 
2178
 
} /* namespace drizzled */