~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2009-08-17 01:44:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1118.
  • Revision ID: brian@gaz-20090817014423-jxi2qonsumm8mndf
Remove SQL level reference for DELAY (just now done correctly by default in
engine).

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
 
 
21
19
#define DRIZZLE_LEX 1
22
 
 
23
 
#include "drizzled/sql_reserved_words.h"
24
 
 
25
 
#include "drizzled/configmake.h"
 
20
#include "drizzled/server_includes.h"
26
21
#include "drizzled/item/num.h"
27
22
#include "drizzled/error.h"
28
23
#include "drizzled/session.h"
30
25
#include "drizzled/lookup_symbol.h"
31
26
#include "drizzled/index_hint.h"
32
27
 
33
 
#include <cstdio>
34
28
#include <ctype.h>
35
29
 
36
30
using namespace std;
37
31
 
38
 
/* Stay outside of the namespace because otherwise bison goes nuts */
 
32
static int lex_one_token(void *arg, void *yysession);
39
33
int DRIZZLElex(void *arg, void *yysession);
40
34
 
41
 
namespace drizzled
42
 
{
43
 
 
44
 
static int lex_one_token(void *arg, void *yysession);
45
 
 
46
35
/**
47
36
  save order by and tables in own lists.
48
37
*/
49
38
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
50
39
{
51
 
  Order *order;
52
 
 
53
 
  if (!(order = (Order *) session->getMemRoot()->allocate(sizeof(Order))))
54
 
    return true;
55
 
 
 
40
  order_st *order;
 
41
  if (!(order = (order_st *) session->alloc(sizeof(order_st))))
 
42
    return(1);
56
43
  order->item_ptr= item;
57
44
  order->item= &order->item_ptr;
58
45
  order->asc = asc;
59
46
  order->free_me=0;
60
47
  order->used=0;
61
48
  order->counter_used= 0;
62
 
  list.link_in_list((unsigned char*) order, (unsigned char**) &order->next);
63
 
 
64
 
  return false;
 
49
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
 
50
  return(0);
65
51
}
66
52
 
 
53
 
 
54
 
 
55
/*
 
56
  We are using pointer to this variable for distinguishing between assignment
 
57
  to NEW row field (when parsing trigger definition) and structured variable.
 
58
*/
 
59
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
 
60
 
67
61
/**
68
62
  LEX_STRING constant for null-string to be used in parser and other places.
69
63
*/
71
65
 
72
66
Lex_input_stream::Lex_input_stream(Session *session,
73
67
                                   const char* buffer,
74
 
                                   unsigned int length) :
75
 
  m_session(session),
 
68
                                   unsigned int length)
 
69
: m_session(session),
76
70
  yylineno(1),
77
71
  yytoklen(0),
78
72
  yylval(NULL),
92
86
  m_body_utf8(NULL),
93
87
  m_cpp_utf8_processed_ptr(NULL),
94
88
  next_state(MY_LEX_START),
 
89
  found_semicolon(NULL),
95
90
  ignore_space(1),
96
91
  in_comment(NO_COMMENT)
97
92
{
98
 
  m_cpp_buf= (char*) session->getMemRoot()->allocate(length + 1);
 
93
  m_cpp_buf= (char*) session->alloc(length + 1);
99
94
  m_cpp_ptr= m_cpp_buf;
100
95
}
101
96
 
122
117
    (m_buf_length / default_charset_info->mbminlen) *
123
118
    my_charset_utf8_bin.mbmaxlen;
124
119
 
125
 
  m_body_utf8= (char *) session->getMemRoot()->allocate(body_utf8_length + 1);
 
120
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
126
121
  m_body_utf8_ptr= m_body_utf8;
127
122
  *m_body_utf8_ptr= 0;
128
123
 
213
208
  Because of this, it's critical to not do too much things here.
214
209
  (We already do too much here)
215
210
*/
216
 
void LEX::start(Session *arg)
217
 
{
218
 
  lex_start(arg);
219
 
}
220
 
 
221
211
void lex_start(Session *session)
222
212
{
223
213
  LEX *lex= session->lex;
232
222
  lex->select_lex.init_query();
233
223
  lex->value_list.empty();
234
224
  lex->update_list.empty();
 
225
  lex->param_list.empty();
235
226
  lex->auxiliary_table_list.empty();
236
227
  lex->unit.next= lex->unit.master=
237
228
    lex->unit.link_next= lex->unit.return_to= 0;
249
240
  lex->derived_tables= 0;
250
241
  lex->lock_option= TL_READ;
251
242
  lex->leaf_tables_insert= 0;
252
 
  lex->var_list.clear();
253
243
  lex->select_lex.select_number= 1;
254
244
  lex->length=0;
255
245
  lex->select_lex.in_sum_expr=0;
269
259
  lex->nest_level=0 ;
270
260
  lex->allow_sum_func= 0;
271
261
  lex->in_sum_func= NULL;
272
 
  lex->type= 0;
273
262
 
274
263
  lex->is_lex_started= true;
 
264
  lex->create_table_proto= NULL;
275
265
  lex->statement= NULL;
276
 
  
277
 
  lex->is_cross= false;
278
 
  lex->reset();
279
266
}
280
267
 
281
 
void LEX::end()
 
268
void lex_end(LEX *lex)
282
269
{
283
 
  if (yacc_yyss)
 
270
  if (lex->yacc_yyss)
284
271
  {
285
 
    free(yacc_yyss);
286
 
    free(yacc_yyvs);
287
 
    yacc_yyss= 0;
288
 
    yacc_yyvs= 0;
 
272
    free(lex->yacc_yyss);
 
273
    free(lex->yacc_yyvs);
 
274
    lex->yacc_yyss= 0;
 
275
    lex->yacc_yyvs= 0;
289
276
  }
290
277
 
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;
 
278
  delete lex->result;
 
279
 
 
280
  if(lex->create_table_proto)
 
281
    delete lex->create_table_proto;
 
282
  lex->result= 0;
 
283
 
 
284
  if (lex->statement) 
 
285
    delete lex->statement;
301
286
}
302
287
 
303
288
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
358
343
  char *to;
359
344
  lip->yyUnget();                       // ptr points now after last token char
360
345
  tmp.length= lip->yytoklen=length;
361
 
  tmp.str=(char*) lip->m_session->getMemRoot()->allocate(tmp.length+1);
 
346
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
362
347
  from= lip->get_tok_start() + skip;
363
348
  to= tmp.str;
364
349
  end= to+length;
434
419
      end-= post_skip;
435
420
      assert(end >= str);
436
421
 
437
 
      if (!(start= (char*) lip->m_session->getMemRoot()->allocate((uint32_t) (end-str)+1)))
438
 
        return (char*) "";              // memory::SqlAlloc has set error flag
 
422
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
 
423
        return (char*) "";              // Sql_alloc has set error flag
439
424
 
440
425
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
441
426
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
598
583
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
599
584
}
600
585
 
601
 
} /* namespace drizzled */
602
586
/*
603
587
  DRIZZLElex remember the following states from the following DRIZZLElex()
604
588
 
608
592
*/
609
593
int DRIZZLElex(void *arg, void *yysession)
610
594
{
611
 
  drizzled::Session *session= (drizzled::Session *)yysession;
612
 
  drizzled::Lex_input_stream *lip= session->m_lip;
 
595
  Session *session= (Session *)yysession;
 
596
  Lex_input_stream *lip= session->m_lip;
613
597
  YYSTYPE *yylval=(YYSTYPE*) arg;
614
598
  int token;
615
599
 
626
610
    return token;
627
611
  }
628
612
 
629
 
  token= drizzled::lex_one_token(arg, yysession);
 
613
  token= lex_one_token(arg, yysession);
630
614
 
631
615
  switch(token) {
632
616
  case WITH:
637
621
      to transform the grammar into a LALR(1) grammar,
638
622
      which sql_yacc.yy can process.
639
623
    */
640
 
    token= drizzled::lex_one_token(arg, yysession);
 
624
    token= lex_one_token(arg, yysession);
641
625
    if (token == ROLLUP_SYM)
642
626
    {
643
627
      return WITH_ROLLUP_SYM;
659
643
  return token;
660
644
}
661
645
 
662
 
namespace drizzled
663
 
{
664
 
 
665
646
int lex_one_token(void *arg, void *yysession)
666
647
{
667
648
  register unsigned char c= 0; /* Just set to shutup GCC */
1284
1265
  }
1285
1266
}
1286
1267
 
 
1268
/**
 
1269
  Construct a copy of this object to be used for mysql_alter_table
 
1270
  and mysql_create_table.
 
1271
 
 
1272
  Historically, these two functions modify their Alter_info
 
1273
  arguments. This behaviour breaks re-execution of prepared
 
1274
  statements and stored procedures and is compensated by always
 
1275
  supplying a copy of Alter_info to these functions.
 
1276
 
 
1277
  @return You need to use check the error in Session for out
 
1278
  of memory condition after calling this function.
 
1279
*/
 
1280
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
 
1281
  :drop_list(rhs.drop_list, mem_root),
 
1282
  alter_list(rhs.alter_list, mem_root),
 
1283
  key_list(rhs.key_list, mem_root),
 
1284
  create_list(rhs.create_list, mem_root),
 
1285
  flags(rhs.flags),
 
1286
  keys_onoff(rhs.keys_onoff),
 
1287
  tablespace_op(rhs.tablespace_op),
 
1288
  no_parts(rhs.no_parts),
 
1289
  build_method(rhs.build_method),
 
1290
  datetime_field(rhs.datetime_field),
 
1291
  error_if_not_empty(rhs.error_if_not_empty)
 
1292
{
 
1293
  /*
 
1294
    Make deep copies of used objects.
 
1295
    This is not a fully deep copy - clone() implementations
 
1296
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
 
1297
    do not copy string constants. At the same length the only
 
1298
    reason we make a copy currently is that ALTER/CREATE TABLE
 
1299
    code changes input Alter_info definitions, but string
 
1300
    constants never change.
 
1301
  */
 
1302
  list_copy_and_replace_each_value(drop_list, mem_root);
 
1303
  list_copy_and_replace_each_value(alter_list, mem_root);
 
1304
  list_copy_and_replace_each_value(key_list, mem_root);
 
1305
  list_copy_and_replace_each_value(create_list, mem_root);
 
1306
}
 
1307
 
1287
1308
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
1288
1309
{
1289
1310
  /*
1315
1336
  options= 0;
1316
1337
  linkage= UNSPECIFIED_TYPE;
1317
1338
  no_error= no_table_names_allowed= 0;
1318
 
  uncacheable.reset();
 
1339
  uncacheable= 0;
1319
1340
}
1320
1341
 
1321
1342
void Select_Lex_Node::init_select()
1382
1403
{
1383
1404
  sj_nests.empty();
1384
1405
  group_list.empty();
1385
 
  db= 0;
 
1406
  type= db= 0;
1386
1407
  having= 0;
 
1408
  table_join_options= 0;
1387
1409
  in_sum_expr= with_wild= 0;
1388
1410
  options= 0;
1389
1411
  braces= 0;
1397
1419
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1398
1420
  offset_limit= 0;      /* denotes the default offset = 0 */
1399
1421
  with_sum_func= 0;
1400
 
  is_cross= false;
1401
1422
  is_correlated= 0;
1402
1423
  cur_pos_in_select_list= UNDEF_POS;
1403
1424
  non_agg_fields.empty();
1581
1602
       s && s != last;
1582
1603
       s= s->outer_select())
1583
1604
  {
1584
 
    if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
 
1605
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1585
1606
    {
1586
1607
      // Select is dependent of outer select
1587
 
      s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1588
 
      s->uncacheable.set(UNCACHEABLE_UNITED);
 
1608
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1609
                       UNCACHEABLE_DEPENDENT;
1589
1610
      Select_Lex_Unit *munit= s->master_unit();
1590
 
      munit->uncacheable.set(UNCACHEABLE_UNITED);
1591
 
      munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
 
1611
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1612
                       UNCACHEABLE_DEPENDENT;
1592
1613
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1593
1614
      {
1594
1615
        if (sl != s &&
1595
 
            ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1596
 
          sl->uncacheable.set(UNCACHEABLE_UNITED);
 
1616
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1617
          sl->uncacheable|= UNCACHEABLE_UNITED;
1597
1618
      }
1598
1619
    }
1599
1620
    s->is_correlated= true;
1618
1639
List<Item>* Select_Lex_Node::get_item_list()
1619
1640
{ return NULL; }
1620
1641
 
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 *)
 
1642
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1643
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1628
1644
{
1629
1645
  return 0;
1630
1646
}
1631
1647
 
 
1648
uint32_t Select_Lex_Node::get_table_join_options()
 
1649
{
 
1650
  return 0;
 
1651
}
1632
1652
 
1633
1653
/*
1634
1654
  prohibit using LIMIT clause
1706
1726
  return &item_list;
1707
1727
}
1708
1728
 
 
1729
uint32_t Select_Lex::get_table_join_options()
 
1730
{
 
1731
  return table_join_options;
 
1732
}
1709
1733
 
1710
1734
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1711
1735
{
1713
1737
    return false;
1714
1738
 
1715
1739
  return (ref_pointer_array=
1716
 
          (Item **)session->getMemRoot()->allocate(sizeof(Item*) * (n_child_sum_items +
 
1740
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1717
1741
                                                 item_list.elements +
1718
1742
                                                 select_n_having_items +
1719
1743
                                                 select_n_where_fields +
1746
1770
      str->append(STRING_WITH_LEN(" order by "));
1747
1771
      fake_select_lex->print_order(
1748
1772
        str,
1749
 
        (Order *) fake_select_lex->order_list.first,
 
1773
        (order_st *) fake_select_lex->order_list.first,
1750
1774
        query_type);
1751
1775
    }
1752
1776
    fake_select_lex->print_limit(session, str, query_type);
1754
1778
}
1755
1779
 
1756
1780
void Select_Lex::print_order(String *str,
1757
 
                                Order *order,
 
1781
                                order_st *order,
1758
1782
                                enum_query_type query_type)
1759
1783
{
1760
1784
  for (; order; order= order->next)
1813
1837
  }
1814
1838
}
1815
1839
 
1816
 
LEX::~LEX()
 
1840
/**
 
1841
  @brief Restore the LEX and Session in case of a parse error.
 
1842
 
 
1843
  This is a clean up call that is invoked by the Bison generated
 
1844
  parser before returning an error from DRIZZLEparse. If your
 
1845
  semantic actions manipulate with the global thread state (which
 
1846
  is a very bad practice and should not normally be employed) and
 
1847
  need a clean-up in case of error, and you can not use %destructor
 
1848
  rule in the grammar file itself, this function should be used
 
1849
  to implement the clean up.
 
1850
*/
 
1851
void LEX::cleanup_lex_after_parse_error(Session *)
1817
1852
{
1818
 
  delete _create_table;
1819
1853
}
1820
1854
 
1821
1855
/*
1865
1899
    statement parsing. On should use lex_start() function to prepare LEX
1866
1900
    for this.
1867
1901
*/
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)
 
1902
LEX::LEX()
 
1903
  :result(0), yacc_yyss(0), yacc_yyvs(0),
 
1904
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1884
1905
{
1885
1906
  reset_query_tables_list(true);
 
1907
  create_table_proto= NULL;
 
1908
  statement= NULL;
 
1909
}
 
1910
 
 
1911
/*
 
1912
  Detect that we need only table structure of derived table/view
 
1913
 
 
1914
  SYNOPSIS
 
1915
    only_view_structure()
 
1916
 
 
1917
  RETURN
 
1918
    true yes, we need only structure
 
1919
    false no, we need data
 
1920
*/
 
1921
bool LEX::only_view_structure()
 
1922
{
 
1923
  switch (sql_command) {
 
1924
  case SQLCOM_SHOW_CREATE:
 
1925
  case SQLCOM_SHOW_TABLES:
 
1926
  case SQLCOM_SHOW_FIELDS:
 
1927
    return true;
 
1928
  default:
 
1929
    return false;
 
1930
  }
 
1931
}
 
1932
 
 
1933
/*
 
1934
  Should Items_ident be printed correctly
 
1935
 
 
1936
  SYNOPSIS
 
1937
    need_correct_ident()
 
1938
 
 
1939
  RETURN
 
1940
    true yes, we need only structure
 
1941
    false no, we need data
 
1942
*/
 
1943
bool LEX::need_correct_ident()
 
1944
{
 
1945
  switch(sql_command)
 
1946
  {
 
1947
  case SQLCOM_SHOW_CREATE:
 
1948
  case SQLCOM_SHOW_TABLES:
 
1949
    return true;
 
1950
  default:
 
1951
    return false;
 
1952
  }
1886
1953
}
1887
1954
 
1888
1955
/**
2157
2224
                                            current_index_hint_clause,
2158
2225
                                            str, length));
2159
2226
}
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 */