~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2009-05-23 17:13:03 UTC
  • mfrom: (1034.1.8 merge)
  • Revision ID: brian@gaz-20090523171303-d28xhutqic0xe2b4
Merge Brian

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
#define DRIZZLE_LEX 1
21
 
#include "drizzled/configmake.h"
 
20
#include "drizzled/server_includes.h"
22
21
#include "drizzled/item/num.h"
23
22
#include "drizzled/error.h"
24
23
#include "drizzled/session.h"
26
25
#include "drizzled/lookup_symbol.h"
27
26
#include "drizzled/index_hint.h"
28
27
 
29
 
#include <cstdio>
30
28
#include <ctype.h>
31
29
 
32
30
using namespace std;
33
31
 
34
 
/* Stay outside of the namespace because otherwise bison goes nuts */
35
 
int DRIZZLElex(void *arg, void *yysession);
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
40
32
static int lex_one_token(void *arg, void *yysession);
41
33
 
42
 
/**
43
 
  save order by and tables in own lists.
 
34
/*
 
35
  We are using pointer to this variable for distinguishing between assignment
 
36
  to NEW row field (when parsing trigger definition) and structured variable.
44
37
*/
45
 
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
46
 
{
47
 
  Order *order;
48
 
  if (!(order = (Order *) session->alloc(sizeof(Order))))
49
 
    return(1);
50
 
  order->item_ptr= item;
51
 
  order->item= &order->item_ptr;
52
 
  order->asc = asc;
53
 
  order->free_me=0;
54
 
  order->used=0;
55
 
  order->counter_used= 0;
56
 
  list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
57
 
  return(0);
58
 
}
 
38
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
59
39
 
60
40
/**
61
41
  LEX_STRING constant for null-string to be used in parser and other places.
85
65
  m_body_utf8(NULL),
86
66
  m_cpp_utf8_processed_ptr(NULL),
87
67
  next_state(MY_LEX_START),
 
68
  found_semicolon(NULL),
88
69
  ignore_space(1),
89
 
  in_comment(NO_COMMENT)
 
70
  in_comment(NO_COMMENT),
 
71
  m_underscore_cs(NULL)
90
72
{
91
73
  m_cpp_buf= (char*) session->alloc(length + 1);
92
74
  m_cpp_ptr= m_cpp_buf;
186
168
                  m_cpp_utf8_processed_ptr will be set in the end of the
187
169
                  operation.
188
170
*/
189
 
void Lex_input_stream::body_utf8_append_literal(const LEX_STRING *txt,
 
171
void Lex_input_stream::body_utf8_append_literal(Session *session,
 
172
                                                const LEX_STRING *txt,
 
173
                                                const CHARSET_INFO * const txt_cs,
190
174
                                                const char *end_ptr)
191
175
{
192
176
  if (!m_cpp_utf8_processed_ptr)
193
177
    return;
194
178
 
 
179
  LEX_STRING utf_txt;
 
180
 
 
181
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
 
182
  {
 
183
    session->convert_string(&utf_txt,
 
184
                        &my_charset_utf8_general_ci,
 
185
                        txt->str, txt->length,
 
186
                        txt_cs);
 
187
  }
 
188
  else
 
189
  {
 
190
    utf_txt.str= txt->str;
 
191
    utf_txt.length= txt->length;
 
192
  }
 
193
 
195
194
  /* NOTE: utf_txt.length is in bytes, not in symbols. */
196
195
 
197
 
  memcpy(m_body_utf8_ptr, txt->str, txt->length);
198
 
  m_body_utf8_ptr += txt->length;
 
196
  memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
 
197
  m_body_utf8_ptr += utf_txt.length;
199
198
  *m_body_utf8_ptr= 0;
200
199
 
201
200
  m_cpp_utf8_processed_ptr= end_ptr;
206
205
  Because of this, it's critical to not do too much things here.
207
206
  (We already do too much here)
208
207
*/
209
 
void LEX::start(Session *arg)
210
 
{
211
 
  lex_start(arg);
212
 
}
213
 
 
214
208
void lex_start(Session *session)
215
209
{
216
210
  LEX *lex= session->lex;
225
219
  lex->select_lex.init_query();
226
220
  lex->value_list.empty();
227
221
  lex->update_list.empty();
 
222
  lex->param_list.empty();
228
223
  lex->auxiliary_table_list.empty();
229
224
  lex->unit.next= lex->unit.master=
230
225
    lex->unit.link_next= lex->unit.return_to= 0;
263
258
  lex->in_sum_func= NULL;
264
259
 
265
260
  lex->is_lex_started= true;
266
 
  lex->statement= NULL;
267
 
  
268
 
  lex->is_cross= false;
269
 
 
270
 
  lex->reset();
 
261
  lex->create_table_proto= NULL;
271
262
}
272
263
 
273
 
void LEX::end()
 
264
void lex_end(LEX *lex)
274
265
{
275
 
  if (yacc_yyss)
 
266
  if (lex->yacc_yyss)
276
267
  {
277
 
    free(yacc_yyss);
278
 
    free(yacc_yyvs);
279
 
    yacc_yyss= 0;
280
 
    yacc_yyvs= 0;
 
268
    free(lex->yacc_yyss);
 
269
    free(lex->yacc_yyvs);
 
270
    lex->yacc_yyss= 0;
 
271
    lex->yacc_yyvs= 0;
281
272
  }
282
273
 
283
 
  delete result;
284
 
 
285
 
  result= 0;
286
 
  setCacheable(true);
287
 
 
288
 
  delete statement;
289
 
  statement= NULL;
 
274
  delete lex->result;
 
275
 
 
276
  if(lex->create_table_proto)
 
277
    delete lex->create_table_proto;
 
278
  lex->result= 0;
290
279
}
291
280
 
292
281
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
384
373
  {
385
374
    c= lip->yyGet();
386
375
    lip->tok_bitmap|= c;
 
376
#ifdef USE_MB
387
377
    {
388
378
      if (use_mb(cs))
389
379
      {
395
385
        }
396
386
      }
397
387
    }
 
388
#endif
398
389
    if (c == '\\')
399
390
    {                                   // Escaped character
400
391
      found_escape= true;
424
415
      assert(end >= str);
425
416
 
426
417
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
427
 
        return (char*) "";              // memory::SqlAlloc has set error flag
 
418
        return (char*) "";              // Sql_alloc has set error flag
428
419
 
429
420
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
430
421
      lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
441
432
 
442
433
        for (to= start; str != end; str++)
443
434
        {
 
435
#ifdef USE_MB
444
436
          if (use_mb(cs))
445
437
          {
446
438
            int l= my_ismbchar(cs, str, end);
452
444
              continue;
453
445
            }
454
446
          }
 
447
#endif
455
448
          if (*str == '\\' && (str + 1) != end)
456
449
          {
457
450
            switch (*++str) {
587
580
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
588
581
}
589
582
 
590
 
} /* namespace drizzled */
591
583
/*
592
584
  DRIZZLElex remember the following states from the following DRIZZLElex()
593
585
 
597
589
*/
598
590
int DRIZZLElex(void *arg, void *yysession)
599
591
{
600
 
  drizzled::Session *session= (drizzled::Session *)yysession;
601
 
  drizzled::Lex_input_stream *lip= session->m_lip;
 
592
  Session *session= (Session *)yysession;
 
593
  Lex_input_stream *lip= session->m_lip;
602
594
  YYSTYPE *yylval=(YYSTYPE*) arg;
603
595
  int token;
604
596
 
615
607
    return token;
616
608
  }
617
609
 
618
 
  token= drizzled::lex_one_token(arg, yysession);
 
610
  token= lex_one_token(arg, yysession);
619
611
 
620
612
  switch(token) {
621
613
  case WITH:
626
618
      to transform the grammar into a LALR(1) grammar,
627
619
      which sql_yacc.yy can process.
628
620
    */
629
 
    token= drizzled::lex_one_token(arg, yysession);
 
621
    token= lex_one_token(arg, yysession);
630
622
    if (token == ROLLUP_SYM)
631
623
    {
632
624
      return WITH_ROLLUP_SYM;
648
640
  return token;
649
641
}
650
642
 
651
 
namespace drizzled
652
 
{
653
 
 
654
643
int lex_one_token(void *arg, void *yysession)
655
644
{
656
645
  register unsigned char c= 0; /* Just set to shutup GCC */
740
729
      }
741
730
    case MY_LEX_IDENT:
742
731
      const char *start;
 
732
#if defined(USE_MB) && defined(USE_MB_IDENT)
743
733
      if (use_mb(cs))
744
734
      {
745
735
        result_state= IDENT_QUOTED;
766
756
        }
767
757
      }
768
758
      else
 
759
#endif
769
760
      {
770
761
        for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
771
762
        /* If there were non-ASCII characters, mark that we must convert */
797
788
 
798
789
      lip->body_utf8_append(lip->m_cpp_text_start);
799
790
 
800
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
791
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
792
                                    lip->m_cpp_text_end);
801
793
 
802
794
      return(result_state);                     // IDENT or IDENT_QUOTED
803
795
 
868
860
      // fall through
869
861
    case MY_LEX_IDENT_START:                    // We come here after '.'
870
862
      result_state= IDENT;
 
863
#if defined(USE_MB) && defined(USE_MB_IDENT)
871
864
      if (use_mb(cs))
872
865
      {
873
866
        result_state= IDENT_QUOTED;
883
876
        }
884
877
      }
885
878
      else
 
879
#endif
886
880
      {
887
881
        for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
888
882
        /* If there were non-ASCII characters, mark that we must convert */
895
889
 
896
890
      lip->body_utf8_append(lip->m_cpp_text_start);
897
891
 
898
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
892
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
893
                                    lip->m_cpp_text_end);
899
894
 
900
895
      return(result_state);
901
896
 
917
912
            continue;
918
913
          }
919
914
        }
 
915
#ifdef USE_MB
920
916
        else if (var_length < 1)
921
917
          break;                                // Error
922
918
        lip->skip_binary(var_length-1);
 
919
#endif
923
920
      }
924
921
      if (double_quotes)
925
922
              yylval->lex_str=get_quoted_token(lip, 1, lip->yyLength() - double_quotes -1, quote_char);
929
926
        lip->yySkip();                  // Skip end `
930
927
      lip->next_state= MY_LEX_START;
931
928
      lip->body_utf8_append(lip->m_cpp_text_start);
932
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
929
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs, lip->m_cpp_text_end);
933
930
      return(IDENT_QUOTED);
934
931
    }
935
932
    case MY_LEX_INT_OR_REAL:            // Complete int or incomplete real
1041
1038
 
1042
1039
      lip->body_utf8_append(lip->m_cpp_text_start);
1043
1040
 
1044
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1041
      lip->body_utf8_append_literal(session, &yylval->lex_str,
 
1042
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
 
1043
        lip->m_cpp_text_end);
 
1044
 
 
1045
      lip->m_underscore_cs= NULL;
1045
1046
 
1046
1047
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1047
1048
      return(TEXT_STRING);
1176
1177
    case MY_LEX_SEMICOLON:                      // optional line terminator
1177
1178
      if (lip->yyPeek())
1178
1179
      {
 
1180
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1181
        {
 
1182
          lip->found_semicolon= lip->get_ptr();
 
1183
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1184
          lip->next_state= MY_LEX_END;
 
1185
          lip->set_echo(true);
 
1186
          return (END_OF_INPUT);
 
1187
        }
1179
1188
        state= MY_LEX_CHAR;             // Return ';'
1180
1189
        break;
1181
1190
      }
1266
1275
 
1267
1276
      lip->body_utf8_append(lip->m_cpp_text_start);
1268
1277
 
1269
 
      lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
 
1278
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1279
                                    lip->m_cpp_text_end);
1270
1280
 
1271
1281
      return(result_state);
1272
1282
    }
1273
1283
  }
1274
1284
}
1275
1285
 
 
1286
/**
 
1287
  Construct a copy of this object to be used for mysql_alter_table
 
1288
  and mysql_create_table.
 
1289
 
 
1290
  Historically, these two functions modify their Alter_info
 
1291
  arguments. This behaviour breaks re-execution of prepared
 
1292
  statements and stored procedures and is compensated by always
 
1293
  supplying a copy of Alter_info to these functions.
 
1294
 
 
1295
  @return You need to use check the error in Session for out
 
1296
  of memory condition after calling this function.
 
1297
*/
 
1298
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
 
1299
  :drop_list(rhs.drop_list, mem_root),
 
1300
  alter_list(rhs.alter_list, mem_root),
 
1301
  key_list(rhs.key_list, mem_root),
 
1302
  create_list(rhs.create_list, mem_root),
 
1303
  flags(rhs.flags),
 
1304
  keys_onoff(rhs.keys_onoff),
 
1305
  tablespace_op(rhs.tablespace_op),
 
1306
  no_parts(rhs.no_parts),
 
1307
  build_method(rhs.build_method),
 
1308
  datetime_field(rhs.datetime_field),
 
1309
  error_if_not_empty(rhs.error_if_not_empty)
 
1310
{
 
1311
  /*
 
1312
    Make deep copies of used objects.
 
1313
    This is not a fully deep copy - clone() implementations
 
1314
    of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
 
1315
    do not copy string constants. At the same length the only
 
1316
    reason we make a copy currently is that ALTER/CREATE TABLE
 
1317
    code changes input Alter_info definitions, but string
 
1318
    constants never change.
 
1319
  */
 
1320
  list_copy_and_replace_each_value(drop_list, mem_root);
 
1321
  list_copy_and_replace_each_value(alter_list, mem_root);
 
1322
  list_copy_and_replace_each_value(key_list, mem_root);
 
1323
  list_copy_and_replace_each_value(create_list, mem_root);
 
1324
}
 
1325
 
1276
1326
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
1277
1327
{
1278
1328
  /*
1304
1354
  options= 0;
1305
1355
  linkage= UNSPECIFIED_TYPE;
1306
1356
  no_error= no_table_names_allowed= 0;
1307
 
  uncacheable.reset();
 
1357
  uncacheable= 0;
1308
1358
}
1309
1359
 
1310
1360
void Select_Lex_Node::init_select()
1371
1421
{
1372
1422
  sj_nests.empty();
1373
1423
  group_list.empty();
1374
 
  db= 0;
 
1424
  type= db= 0;
1375
1425
  having= 0;
 
1426
  table_join_options= 0;
1376
1427
  in_sum_expr= with_wild= 0;
1377
1428
  options= 0;
1378
1429
  braces= 0;
1391
1442
  non_agg_fields.empty();
1392
1443
  cond_value= having_value= Item::COND_UNDEF;
1393
1444
  inner_refs_list.empty();
1394
 
  full_group_by_flag.reset();
 
1445
  full_group_by_flag= 0;
1395
1446
}
1396
1447
 
1397
1448
/*
1569
1620
       s && s != last;
1570
1621
       s= s->outer_select())
1571
1622
  {
1572
 
    if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
 
1623
    if (!(s->uncacheable & UNCACHEABLE_DEPENDENT))
1573
1624
    {
1574
1625
      // Select is dependent of outer select
1575
 
      s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1576
 
      s->uncacheable.set(UNCACHEABLE_UNITED);
 
1626
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
 
1627
                       UNCACHEABLE_DEPENDENT;
1577
1628
      Select_Lex_Unit *munit= s->master_unit();
1578
 
      munit->uncacheable.set(UNCACHEABLE_UNITED);
1579
 
      munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
 
1629
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
 
1630
                       UNCACHEABLE_DEPENDENT;
1580
1631
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1581
1632
      {
1582
1633
        if (sl != s &&
1583
 
            ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1584
 
          sl->uncacheable.set(UNCACHEABLE_UNITED);
 
1634
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
 
1635
          sl->uncacheable|= UNCACHEABLE_UNITED;
1585
1636
      }
1586
1637
    }
1587
1638
    s->is_correlated= true;
1606
1657
List<Item>* Select_Lex_Node::get_item_list()
1607
1658
{ return NULL; }
1608
1659
 
1609
 
TableList *Select_Lex_Node::add_table_to_list(Session *, 
1610
 
                                              Table_ident *, 
1611
 
                                              LEX_STRING *, 
1612
 
                                              const bitset<NUM_OF_TABLE_OPTIONS>&,
1613
 
                                              thr_lock_type, 
1614
 
                                              List<Index_hint> *, 
1615
 
                                              LEX_STRING *)
 
1660
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1661
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1616
1662
{
1617
1663
  return 0;
1618
1664
}
1619
1665
 
 
1666
uint32_t Select_Lex_Node::get_table_join_options()
 
1667
{
 
1668
  return 0;
 
1669
}
1620
1670
 
1621
1671
/*
1622
1672
  prohibit using LIMIT clause
1694
1744
  return &item_list;
1695
1745
}
1696
1746
 
 
1747
uint32_t Select_Lex::get_table_join_options()
 
1748
{
 
1749
  return table_join_options;
 
1750
}
1697
1751
 
1698
1752
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1699
1753
{
1734
1788
      str->append(STRING_WITH_LEN(" order by "));
1735
1789
      fake_select_lex->print_order(
1736
1790
        str,
1737
 
        (Order *) fake_select_lex->order_list.first,
 
1791
        (order_st *) fake_select_lex->order_list.first,
1738
1792
        query_type);
1739
1793
    }
1740
1794
    fake_select_lex->print_limit(session, str, query_type);
1742
1796
}
1743
1797
 
1744
1798
void Select_Lex::print_order(String *str,
1745
 
                                Order *order,
 
1799
                                order_st *order,
1746
1800
                                enum_query_type query_type)
1747
1801
{
1748
1802
  for (; order; order= order->next)
1864
1918
    for this.
1865
1919
*/
1866
1920
LEX::LEX()
1867
 
  :
1868
 
    result(0), 
1869
 
    yacc_yyss(0), 
1870
 
    yacc_yyvs(0),
1871
 
    charset(NULL),
1872
 
    sql_command(SQLCOM_END), 
1873
 
    option_type(OPT_DEFAULT), 
1874
 
    is_lex_started(0),
1875
 
    cacheable(true),
1876
 
    sum_expr_used(false)
 
1921
  :result(0), yacc_yyss(0), yacc_yyvs(0),
 
1922
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1877
1923
{
1878
1924
  reset_query_tables_list(true);
1879
 
  statement= NULL;
 
1925
  create_table_proto= NULL;
 
1926
}
 
1927
 
 
1928
/*
 
1929
  Detect that we need only table structure of derived table/view
 
1930
 
 
1931
  SYNOPSIS
 
1932
    only_view_structure()
 
1933
 
 
1934
  RETURN
 
1935
    true yes, we need only structure
 
1936
    false no, we need data
 
1937
*/
 
1938
bool LEX::only_view_structure()
 
1939
{
 
1940
  switch (sql_command) {
 
1941
  case SQLCOM_SHOW_CREATE:
 
1942
  case SQLCOM_SHOW_TABLES:
 
1943
  case SQLCOM_SHOW_FIELDS:
 
1944
    return true;
 
1945
  default:
 
1946
    return false;
 
1947
  }
 
1948
}
 
1949
 
 
1950
/*
 
1951
  Should Items_ident be printed correctly
 
1952
 
 
1953
  SYNOPSIS
 
1954
    need_correct_ident()
 
1955
 
 
1956
  RETURN
 
1957
    true yes, we need only structure
 
1958
    false no, we need data
 
1959
*/
 
1960
bool LEX::need_correct_ident()
 
1961
{
 
1962
  switch(sql_command)
 
1963
  {
 
1964
  case SQLCOM_SHOW_CREATE:
 
1965
  case SQLCOM_SHOW_TABLES:
 
1966
    return true;
 
1967
  default:
 
1968
    return false;
 
1969
  }
1880
1970
}
1881
1971
 
1882
1972
/**
2066
2156
  /*
2067
2157
    session->lex->derived_tables & additional units may be set if we open
2068
2158
    a view. It is necessary to clear session->lex->derived_tables flag
2069
 
    to prevent processing of derived tables during next openTablesLock
 
2159
    to prevent processing of derived tables during next open_and_lock_tables
2070
2160
    if next table is a real table and cleanup & remove underlying units
2071
2161
    NOTE: all units will be connected to session->lex->select_lex, because we
2072
2162
    have not UNION on most upper level.
2151
2241
                                            current_index_hint_clause,
2152
2242
                                            str, length));
2153
2243
}
2154
 
 
2155
 
} /* namespace drizzled */