~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2010-06-22 01:08:50 UTC
  • mto: This revision was merged to the branch mainline in revision 1635.
  • Revision ID: brian@gaz-20100622010850-eh2gi2i0e1kp0jye
Put a copy of the Session in the root of function to make use of.

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
 
#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.
51
44
*/
52
45
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
53
46
{
54
 
  Order *order;
55
 
 
56
 
  if (!(order = (Order *) session->getMemRoot()->allocate(sizeof(Order))))
57
 
    return true;
58
 
 
 
47
  order_st *order;
 
48
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
 
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
 
216
206
  Because of this, it's critical to not do too much things here.
217
207
  (We already do too much here)
218
208
*/
219
 
void LEX::start(Session *arg)
220
 
{
221
 
  lex_start(arg);
222
 
}
223
 
 
224
209
void lex_start(Session *session)
225
210
{
226
 
  LEX *lex= session->getLex();
 
211
  LEX *lex= session->lex;
227
212
 
228
213
  lex->session= lex->unit.session= session;
229
214
 
230
 
  lex->context_stack.clear();
 
215
  lex->context_stack.empty();
231
216
  lex->unit.init_query();
232
217
  lex->unit.init_select();
233
218
  /* 'parent_lex' is used in init_query() so it must be before it. */
234
219
  lex->select_lex.parent_lex= lex;
235
220
  lex->select_lex.init_query();
236
 
  lex->value_list.clear();
237
 
  lex->update_list.clear();
238
 
  lex->auxiliary_table_list.clear();
 
221
  lex->value_list.empty();
 
222
  lex->update_list.empty();
 
223
  lex->auxiliary_table_list.empty();
239
224
  lex->unit.next= lex->unit.master=
240
225
    lex->unit.link_next= lex->unit.return_to= 0;
241
226
  lex->unit.prev= lex->unit.link_prev= 0;
247
232
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
248
233
  lex->select_lex.options= 0;
249
234
  lex->select_lex.init_order();
250
 
  lex->select_lex.group_list.clear();
 
235
  lex->select_lex.group_list.empty();
251
236
  lex->describe= 0;
252
237
  lex->derived_tables= 0;
253
238
  lex->lock_option= TL_READ;
254
239
  lex->leaf_tables_insert= 0;
255
 
  lex->var_list.clear();
256
240
  lex->select_lex.select_number= 1;
257
241
  lex->length=0;
258
242
  lex->select_lex.in_sum_expr=0;
259
 
  lex->select_lex.group_list.clear();
260
 
  lex->select_lex.order_list.clear();
 
243
  lex->select_lex.group_list.empty();
 
244
  lex->select_lex.order_list.empty();
261
245
  lex->sql_command= SQLCOM_END;
262
246
  lex->duplicates= DUP_ERROR;
263
247
  lex->ignore= 0;
272
256
  lex->nest_level=0 ;
273
257
  lex->allow_sum_func= 0;
274
258
  lex->in_sum_func= NULL;
275
 
  lex->type= 0;
276
259
 
277
260
  lex->is_lex_started= true;
278
261
  lex->statement= NULL;
279
 
  
280
 
  lex->is_cross= false;
281
 
  lex->reset();
282
262
}
283
263
 
284
 
void LEX::end()
 
264
void lex_end(LEX *lex)
285
265
{
286
 
  if (yacc_yyss)
 
266
  if (lex->yacc_yyss)
287
267
  {
288
 
    free(yacc_yyss);
289
 
    free(yacc_yyvs);
290
 
    yacc_yyss= 0;
291
 
    yacc_yyvs= 0;
 
268
    free(lex->yacc_yyss);
 
269
    free(lex->yacc_yyvs);
 
270
    lex->yacc_yyss= 0;
 
271
    lex->yacc_yyvs= 0;
292
272
  }
293
273
 
294
 
  safe_delete(result);
295
 
  safe_delete(_create_table);
296
 
  _create_table= NULL;
297
 
  _create_field= NULL;
298
 
 
299
 
  result= 0;
300
 
  setCacheable(true);
301
 
 
302
 
  safe_delete(statement);
 
274
  delete lex->result;
 
275
 
 
276
  lex->result= 0;
 
277
 
 
278
  if (lex->statement) 
 
279
    delete lex->statement;
303
280
}
304
281
 
305
282
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
360
337
  char *to;
361
338
  lip->yyUnget();                       // ptr points now after last token char
362
339
  tmp.length= lip->yytoklen=length;
363
 
  tmp.str=(char*) lip->m_session->getMemRoot()->allocate(tmp.length+1);
 
340
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
364
341
  from= lip->get_tok_start() + skip;
365
342
  to= tmp.str;
366
343
  end= to+length;
387
364
*/
388
365
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
389
366
{
390
 
  unsigned char c,sep;
 
367
  register unsigned char c,sep;
391
368
  bool found_escape= false;
392
369
  const CHARSET_INFO * const cs= lip->m_session->charset();
393
370
 
436
413
      end-= post_skip;
437
414
      assert(end >= str);
438
415
 
439
 
      if (!(start= (char*) lip->m_session->getMemRoot()->allocate((uint32_t) (end-str)+1)))
 
416
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
440
417
        return (char*) "";              // memory::SqlAlloc has set error flag
441
418
 
442
419
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
602
579
 
603
580
} /* namespace drizzled */
604
581
/*
605
 
  base_sql_lex remember the following states from the following sql_baselex()
 
582
  DRIZZLElex remember the following states from the following DRIZZLElex()
606
583
 
607
584
  - MY_LEX_EOQ                  Found end of query
608
585
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
609
586
                                (which can't be followed by a signed number)
610
587
*/
611
 
int base_sql_lex(union ParserType *yylval, drizzled::Session *session)
 
588
int DRIZZLElex(void *arg, void *yysession)
612
589
{
 
590
  drizzled::Session *session= (drizzled::Session *)yysession;
613
591
  drizzled::Lex_input_stream *lip= session->m_lip;
 
592
  YYSTYPE *yylval=(YYSTYPE*) arg;
614
593
  int token;
615
594
 
616
595
  if (lip->lookahead_token != END_OF_INPUT)
626
605
    return token;
627
606
  }
628
607
 
629
 
  token= drizzled::lex_one_token(yylval, session);
 
608
  token= drizzled::lex_one_token(arg, yysession);
630
609
 
631
610
  switch(token) {
632
611
  case WITH:
637
616
      to transform the grammar into a LALR(1) grammar,
638
617
      which sql_yacc.yy can process.
639
618
    */
640
 
    token= drizzled::lex_one_token(yylval, session);
 
619
    token= drizzled::lex_one_token(arg, yysession);
641
620
    if (token == ROLLUP_SYM)
642
621
    {
643
622
      return WITH_ROLLUP_SYM;
662
641
namespace drizzled
663
642
{
664
643
 
665
 
int lex_one_token(ParserType *yylval, drizzled::Session *session)
 
644
int lex_one_token(void *arg, void *yysession)
666
645
{
667
 
  unsigned char c= 0; /* Just set to shutup GCC */
 
646
  register unsigned char c= 0; /* Just set to shutup GCC */
668
647
  bool comment_closed;
669
648
  int   tokval, result_state;
670
649
  unsigned int length;
671
650
  enum my_lex_states state;
 
651
  Session *session= (Session *)yysession;
672
652
  Lex_input_stream *lip= session->m_lip;
673
 
  LEX *lex= session->getLex();
 
653
  LEX *lex= session->lex;
 
654
  YYSTYPE *yylval=(YYSTYPE*) arg;
674
655
  const CHARSET_INFO * const cs= session->charset();
675
656
  unsigned char *state_map= cs->state_map;
676
657
  unsigned char *ident_map= cs->ident_map;
1313
1294
  options= 0;
1314
1295
  linkage= UNSPECIFIED_TYPE;
1315
1296
  no_error= no_table_names_allowed= 0;
1316
 
  uncacheable.reset();
 
1297
  uncacheable= 0;
1317
1298
}
1318
1299
 
1319
1300
void Select_Lex_Node::init_select()
1334
1315
  table= 0;
1335
1316
  fake_select_lex= 0;
1336
1317
  cleaned= 0;
1337
 
  item_list.clear();
 
1318
  item_list.empty();
1338
1319
  describe= 0;
1339
1320
  found_rows_for_union= 0;
1340
1321
}
1342
1323
void Select_Lex::init_query()
1343
1324
{
1344
1325
  Select_Lex_Node::init_query();
1345
 
  table_list.clear();
1346
 
  top_join_list.clear();
 
1326
  table_list.empty();
 
1327
  top_join_list.empty();
1347
1328
  join_list= &top_join_list;
1348
1329
  embedding= leaf_tables= 0;
1349
 
  item_list.clear();
 
1330
  item_list.empty();
1350
1331
  join= 0;
1351
1332
  having= where= 0;
1352
1333
  olap= UNSPECIFIED_OLAP_TYPE;
1378
1359
 
1379
1360
void Select_Lex::init_select()
1380
1361
{
1381
 
  sj_nests.clear();
1382
 
  group_list.clear();
 
1362
  sj_nests.empty();
 
1363
  group_list.empty();
1383
1364
  db= 0;
1384
1365
  having= 0;
 
1366
  table_join_options= 0;
1385
1367
  in_sum_expr= with_wild= 0;
1386
1368
  options= 0;
1387
1369
  braces= 0;
1388
 
  interval_list.clear();
 
1370
  interval_list.empty();
1389
1371
  inner_sum_func_list= 0;
1390
1372
  linkage= UNSPECIFIED_TYPE;
1391
1373
  order_list.elements= 0;
1395
1377
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1396
1378
  offset_limit= 0;      /* denotes the default offset = 0 */
1397
1379
  with_sum_func= 0;
1398
 
  is_cross= false;
1399
1380
  is_correlated= 0;
1400
1381
  cur_pos_in_select_list= UNDEF_POS;
1401
 
  non_agg_fields.clear();
 
1382
  non_agg_fields.empty();
1402
1383
  cond_value= having_value= Item::COND_UNDEF;
1403
 
  inner_refs_list.clear();
 
1384
  inner_refs_list.empty();
1404
1385
  full_group_by_flag.reset();
1405
1386
}
1406
1387
 
1579
1560
       s && s != last;
1580
1561
       s= s->outer_select())
1581
1562
  {
1582
 
    if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
 
1563
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1583
1564
    {
1584
1565
      // Select is dependent of outer select
1585
 
      s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1586
 
      s->uncacheable.set(UNCACHEABLE_UNITED);
 
1566
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1567
                       UNCACHEABLE_DEPENDENT;
1587
1568
      Select_Lex_Unit *munit= s->master_unit();
1588
 
      munit->uncacheable.set(UNCACHEABLE_UNITED);
1589
 
      munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
 
1569
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1570
                       UNCACHEABLE_DEPENDENT;
1590
1571
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1591
1572
      {
1592
1573
        if (sl != s &&
1593
 
            ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1594
 
          sl->uncacheable.set(UNCACHEABLE_UNITED);
 
1574
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1575
          sl->uncacheable|= UNCACHEABLE_UNITED;
1595
1576
      }
1596
1577
    }
1597
1578
    s->is_correlated= true;
1616
1597
List<Item>* Select_Lex_Node::get_item_list()
1617
1598
{ return NULL; }
1618
1599
 
1619
 
TableList *Select_Lex_Node::add_table_to_list(Session *, 
1620
 
                                              Table_ident *, 
1621
 
                                              LEX_STRING *, 
1622
 
                                              const bitset<NUM_OF_TABLE_OPTIONS>&,
1623
 
                                              thr_lock_type, 
1624
 
                                              List<Index_hint> *, 
1625
 
                                              LEX_STRING *)
 
1600
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1601
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1626
1602
{
1627
1603
  return 0;
1628
1604
}
1629
1605
 
 
1606
uint32_t Select_Lex_Node::get_table_join_options()
 
1607
{
 
1608
  return 0;
 
1609
}
1630
1610
 
1631
1611
/*
1632
1612
  prohibit using LIMIT clause
1704
1684
  return &item_list;
1705
1685
}
1706
1686
 
 
1687
uint32_t Select_Lex::get_table_join_options()
 
1688
{
 
1689
  return table_join_options;
 
1690
}
1707
1691
 
1708
1692
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1709
1693
{
1711
1695
    return false;
1712
1696
 
1713
1697
  return (ref_pointer_array=
1714
 
          (Item **)session->getMemRoot()->allocate(sizeof(Item*) * (n_child_sum_items +
 
1698
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1715
1699
                                                 item_list.elements +
1716
1700
                                                 select_n_having_items +
1717
1701
                                                 select_n_where_fields +
1744
1728
      str->append(STRING_WITH_LEN(" order by "));
1745
1729
      fake_select_lex->print_order(
1746
1730
        str,
1747
 
        (Order *) fake_select_lex->order_list.first,
 
1731
        (order_st *) fake_select_lex->order_list.first,
1748
1732
        query_type);
1749
1733
    }
1750
1734
    fake_select_lex->print_limit(session, str, query_type);
1752
1736
}
1753
1737
 
1754
1738
void Select_Lex::print_order(String *str,
1755
 
                                Order *order,
 
1739
                                order_st *order,
1756
1740
                                enum_query_type query_type)
1757
1741
{
1758
1742
  for (; order; order= order->next)
1811
1795
  }
1812
1796
}
1813
1797
 
1814
 
LEX::~LEX()
 
1798
/**
 
1799
  @brief Restore the LEX and Session in case of a parse error.
 
1800
 
 
1801
  This is a clean up call that is invoked by the Bison generated
 
1802
  parser before returning an error from DRIZZLEparse. If your
 
1803
  semantic actions manipulate with the global thread state (which
 
1804
  is a very bad practice and should not normally be employed) and
 
1805
  need a clean-up in case of error, and you can not use %destructor
 
1806
  rule in the grammar file itself, this function should be used
 
1807
  to implement the clean up.
 
1808
*/
 
1809
void LEX::cleanup_lex_after_parse_error(Session *)
1815
1810
{
1816
 
  delete _create_table;
1817
1811
}
1818
1812
 
1819
1813
/*
1863
1857
    statement parsing. On should use lex_start() function to prepare LEX
1864
1858
    for this.
1865
1859
*/
1866
 
LEX::LEX() :
1867
 
    result(0), 
1868
 
    yacc_yyss(0), 
1869
 
    yacc_yyvs(0),
1870
 
    session(NULL),
1871
 
    charset(NULL),
1872
 
    var_list(),
1873
 
    sql_command(SQLCOM_END), 
1874
 
    statement(NULL),
1875
 
    option_type(OPT_DEFAULT), 
1876
 
    is_lex_started(0),
1877
 
    cacheable(true),
1878
 
    sum_expr_used(false),
1879
 
    _create_table(NULL),
1880
 
    _create_field(NULL),
1881
 
    _exists(false)
 
1860
LEX::LEX()
 
1861
  :result(0), yacc_yyss(0), yacc_yyvs(0),
 
1862
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1882
1863
{
1883
1864
  reset_query_tables_list(true);
 
1865
  statement= NULL;
 
1866
}
 
1867
 
 
1868
/*
 
1869
  Detect that we need only table structure of derived table/view
 
1870
 
 
1871
  SYNOPSIS
 
1872
    only_view_structure()
 
1873
 
 
1874
  RETURN
 
1875
    true yes, we need only structure
 
1876
    false no, we need data
 
1877
*/
 
1878
bool LEX::only_view_structure()
 
1879
{
 
1880
  if (sql_command == SQLCOM_SHOW_CREATE)
 
1881
    return true;
 
1882
 
 
1883
  return false;
 
1884
}
 
1885
 
 
1886
/*
 
1887
  Should Items_ident be printed correctly
 
1888
 
 
1889
  SYNOPSIS
 
1890
    need_correct_ident()
 
1891
 
 
1892
  RETURN
 
1893
    true yes, we need only structure
 
1894
    false no, we need data
 
1895
*/
 
1896
bool LEX::need_correct_ident()
 
1897
{
 
1898
  if (sql_command== SQLCOM_SHOW_CREATE)
 
1899
    return true;
 
1900
 
 
1901
  return false;
1884
1902
}
1885
1903
 
1886
1904
/**
2068
2086
void LEX::cleanup_after_one_table_open()
2069
2087
{
2070
2088
  /*
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
 
2089
    session->lex->derived_tables & additional units may be set if we open
 
2090
    a view. It is necessary to clear session->lex->derived_tables flag
2073
2091
    to prevent processing of derived tables during next openTablesLock
2074
2092
    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
 
2093
    NOTE: all units will be connected to session->lex->select_lex, because we
2076
2094
    have not UNION on most upper level.
2077
2095
    */
2078
2096
  if (all_selects_list != &select_lex)
2156
2174
                                            str, length));
2157
2175
}
2158
2176
 
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
2177
} /* namespace drizzled */