~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Mark Atwood
  • Date: 2009-01-05 04:37:22 UTC
  • mto: (758.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 759.
  • Revision ID: me@mark.atwood.name-20090105043722-03l4mzcxi4yjjjih
replace sql_print_error etc with errmsg_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#define DRIZZLE_LEX 1
20
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/item/num.h>
 
22
#include <drizzled/error.h>
 
23
#include <drizzled/session.h>
 
24
#include <drizzled/sql_base.h>
21
25
 
22
 
static int lex_one_token(void *arg, void *yythd);
 
26
static int lex_one_token(void *arg, void *yysession);
23
27
 
24
28
/*
25
29
  We are using pointer to this variable for distinguishing between assignment
33
37
*/
34
38
const LEX_STRING null_lex_str= {NULL, 0};
35
39
 
36
 
/* Longest standard keyword name */
37
 
 
38
 
#define TOCK_NAME_LENGTH 24
39
40
 
40
41
/*
41
42
  The following data is based on the latin1 character set, and is only
62
63
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
63
64
};
64
65
 
65
 
/* 
66
 
  Names of the index hints (for error messages). Keep in sync with 
67
 
  index_hint_type 
 
66
/*
 
67
  Names of the index hints (for error messages). Keep in sync with
 
68
  index_hint_type
68
69
*/
69
70
 
70
71
const char * index_hint_type_name[] =
71
72
{
72
 
  "IGNORE INDEX", 
73
 
  "USE INDEX", 
 
73
  "IGNORE INDEX",
 
74
  "USE INDEX",
74
75
  "FORCE INDEX"
75
76
};
76
77
 
77
 
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
 
78
int lex_casecmp(const char *s, const char *t, uint32_t len)
78
79
{
79
80
  while (len-- != 0 &&
80
81
         to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
81
82
  return (int) len+1;
82
83
}
83
84
 
84
 
#include <lex_hash.h>
 
85
/* EVIL EVIL - this is included here so that it will have to_upper_lex */
 
86
#include <drizzled/lex_hash.h>
85
87
 
86
88
 
87
89
void lex_init(void)
105
107
void
106
108
st_parsing_options::reset()
107
109
{
108
 
  allows_variable= true;
109
 
  allows_select_into= true;
110
110
  allows_select_procedure= true;
111
 
  allows_derived= true;
112
111
}
113
112
 
114
 
Lex_input_stream::Lex_input_stream(THD *thd,
 
113
Lex_input_stream::Lex_input_stream(Session *session,
115
114
                                   const char* buffer,
116
115
                                   unsigned int length)
117
 
: m_thd(thd),
 
116
: m_session(session),
118
117
  yylineno(1),
119
118
  yytoklen(0),
120
119
  yylval(NULL),
139
138
  in_comment(NO_COMMENT),
140
139
  m_underscore_cs(NULL)
141
140
{
142
 
  m_cpp_buf= (char*) thd->alloc(length + 1);
 
141
  m_cpp_buf= (char*) session->alloc(length + 1);
143
142
  m_cpp_ptr= m_cpp_buf;
144
143
}
145
144
 
153
152
     statement;
154
153
  2) Determine the beginning of the body.
155
154
 
156
 
  @param thd        Thread context.
 
155
  @param session        Thread context.
157
156
  @param begin_ptr  Pointer to the start of the body in the pre-processed
158
157
                    buffer.
159
158
*/
160
159
 
161
 
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
 
160
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
162
161
{
163
162
  assert(begin_ptr);
164
163
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
165
164
 
166
165
  uint32_t body_utf8_length=
167
 
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
 
166
    (m_buf_length / default_charset_info->mbminlen) *
168
167
    my_charset_utf8_bin.mbmaxlen;
169
168
 
170
 
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
 
169
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
171
170
  m_body_utf8_ptr= m_body_utf8;
172
171
  *m_body_utf8_ptr= 0;
173
172
 
233
232
  The operation converts the specified text literal to the utf8 and appends
234
233
  the result to the utf8-body.
235
234
 
236
 
  @param thd      Thread context.
 
235
  @param session      Thread context.
237
236
  @param txt      Text literal.
238
237
  @param txt_cs   Character set of the text literal.
239
238
  @param end_ptr  Pointer in the pre-processed buffer, to which
241
240
                  operation.
242
241
*/
243
242
 
244
 
void Lex_input_stream::body_utf8_append_literal(THD *thd,
 
243
void Lex_input_stream::body_utf8_append_literal(Session *session,
245
244
                                                const LEX_STRING *txt,
246
245
                                                const CHARSET_INFO * const txt_cs,
247
246
                                                const char *end_ptr)
253
252
 
254
253
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
255
254
  {
256
 
    thd->convert_string(&utf_txt,
 
255
    session->convert_string(&utf_txt,
257
256
                        &my_charset_utf8_general_ci,
258
257
                        txt->str, txt->length,
259
258
                        txt_cs);
280
279
  (We already do too much here)
281
280
*/
282
281
 
283
 
void lex_start(THD *thd)
 
282
void lex_start(Session *session)
284
283
{
285
 
  LEX *lex= thd->lex;
 
284
  LEX *lex= session->lex;
286
285
 
287
 
  lex->thd= lex->unit.thd= thd;
 
286
  lex->session= lex->unit.session= session;
288
287
 
289
288
  lex->context_stack.empty();
290
289
  lex->unit.init_query();
314
313
  lex->lock_option= TL_READ;
315
314
  lex->leaf_tables_insert= 0;
316
315
  lex->parsing_options.reset();
317
 
  lex->empty_field_list_on_rset= 0;
318
316
  lex->select_lex.select_number= 1;
319
317
  lex->length=0;
320
318
  lex->select_lex.in_sum_expr=0;
331
329
  lex->reset_query_tables_list(false);
332
330
  lex->expr_allows_subselect= true;
333
331
  lex->use_only_table_context= false;
 
332
  lex->parse_vcol_expr= false;
334
333
 
335
334
  lex->name.str= 0;
336
335
  lex->name.length= 0;
337
336
  lex->nest_level=0 ;
338
337
  lex->allow_sum_func= 0;
339
338
  lex->in_sum_func= NULL;
340
 
  /*
341
 
    ok, there must be a better solution for this, long-term
342
 
    I tried "memset" in the sql_yacc.yy code, but that for
343
 
    some reason made the values zero, even if they were set
344
 
  */
345
 
  lex->server_options.server_name= 0;
346
 
  lex->server_options.server_name_length= 0;
347
 
  lex->server_options.host= 0;
348
 
  lex->server_options.db= 0;
349
 
  lex->server_options.username= 0;
350
 
  lex->server_options.password= 0;
351
 
  lex->server_options.scheme= 0;
352
 
  lex->server_options.owner= 0;
353
 
  lex->server_options.port= -1;
354
339
 
355
340
  lex->is_lex_started= true;
356
 
  return;
357
341
}
358
342
 
359
343
void lex_end(LEX *lex)
366
350
    lex->yacc_yyvs= 0;
367
351
  }
368
352
 
369
 
  /* release used plugins */
370
 
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
371
 
                     lex->plugins.elements);
372
 
  reset_dynamic(&lex->plugins);
373
 
 
374
353
  delete lex->result;
375
354
  lex->result= 0;
376
 
 
377
 
  return;
378
355
}
379
356
 
380
357
 
391
368
 
392
369
    return symbol->tok;
393
370
  }
 
371
 
394
372
  return 0;
395
373
}
396
374
 
426
404
  LEX_STRING tmp;
427
405
  lip->yyUnget();                       // ptr points now after last token char
428
406
  tmp.length=lip->yytoklen=length;
429
 
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
 
407
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
430
408
 
431
409
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
432
410
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
434
412
  return tmp;
435
413
}
436
414
 
437
 
/* 
438
 
 todo: 
439
 
   There are no dangerous charsets in mysql for function 
440
 
   get_quoted_token yet. But it should be fixed in the 
 
415
/*
 
416
 todo:
 
417
   There are no dangerous charsets in mysql for function
 
418
   get_quoted_token yet. But it should be fixed in the
441
419
   future to operate multichar strings (like ucs2)
442
420
*/
443
421
 
450
428
  char *to;
451
429
  lip->yyUnget();                       // ptr points now after last token char
452
430
  tmp.length= lip->yytoklen=length;
453
 
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
 
431
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
454
432
  from= lip->get_tok_start() + skip;
455
433
  to= tmp.str;
456
434
  end= to+length;
480
458
{
481
459
  register unsigned char c,sep;
482
460
  uint32_t found_escape=0;
483
 
  const CHARSET_INFO * const cs= lip->m_thd->charset();
 
461
  const CHARSET_INFO * const cs= lip->m_session->charset();
484
462
 
485
463
  lip->tok_bitmap= 0;
486
464
  sep= lip->yyGetLast();                        // String should end with this
528
506
      end -= post_skip;
529
507
      assert(end >= str);
530
508
 
531
 
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
 
509
      if (!(start= (char*) lip->m_session->alloc((uint) (end-str)+1)))
532
510
        return (char*) "";              // Sql_alloc has set error flag
533
511
 
534
512
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
604
582
/*
605
583
** Calc type of integer; long integer, int64_t integer or real.
606
584
** Returns smallest type that match the string.
607
 
** When using unsigned long long values the result is converted to a real
 
585
** When using uint64_t values the result is converted to a real
608
586
** because else they will be unexpected sign changes because all calculation
609
587
** is done with int64_t or double.
610
588
*/
693
671
 
694
672
 
695
673
/*
696
 
  MYSQLlex remember the following states from the following MYSQLlex()
 
674
  DRIZZLElex remember the following states from the following DRIZZLElex()
697
675
 
698
676
  - MY_LEX_EOQ                  Found end of query
699
677
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
700
678
                                (which can't be followed by a signed number)
701
679
*/
702
680
 
703
 
int MYSQLlex(void *arg, void *yythd)
 
681
int DRIZZLElex(void *arg, void *yysession)
704
682
{
705
 
  THD *thd= (THD *)yythd;
706
 
  Lex_input_stream *lip= thd->m_lip;
 
683
  Session *session= (Session *)yysession;
 
684
  Lex_input_stream *lip= session->m_lip;
707
685
  YYSTYPE *yylval=(YYSTYPE*) arg;
708
686
  int token;
709
687
 
720
698
    return token;
721
699
  }
722
700
 
723
 
  token= lex_one_token(arg, yythd);
 
701
  token= lex_one_token(arg, yysession);
724
702
 
725
703
  switch(token) {
726
704
  case WITH:
731
709
      to transform the grammar into a LALR(1) grammar,
732
710
      which sql_yacc.yy can process.
733
711
    */
734
 
    token= lex_one_token(arg, yythd);
 
712
    token= lex_one_token(arg, yysession);
735
713
    if (token == ROLLUP_SYM)
736
714
    {
737
715
      return WITH_ROLLUP_SYM;
754
732
  return token;
755
733
}
756
734
 
757
 
int lex_one_token(void *arg, void *yythd)
 
735
int lex_one_token(void *arg, void *yysession)
758
736
{
759
737
  register unsigned char c= 0; /* Just set to shutup GCC */
760
738
  bool comment_closed;
761
739
  int   tokval, result_state;
762
740
  unsigned int length;
763
741
  enum my_lex_states state;
764
 
  THD *thd= (THD *)yythd;
765
 
  Lex_input_stream *lip= thd->m_lip;
766
 
  LEX *lex= thd->lex;
 
742
  Session *session= (Session *)yysession;
 
743
  Lex_input_stream *lip= session->m_lip;
 
744
  LEX *lex= session->lex;
767
745
  YYSTYPE *yylval=(YYSTYPE*) arg;
768
 
  const CHARSET_INFO * const cs= thd->charset();
 
746
  const CHARSET_INFO * const cs= session->charset();
769
747
  unsigned char *state_map= cs->state_map;
770
748
  unsigned char *ident_map= cs->ident_map;
771
749
 
926
904
 
927
905
      lip->body_utf8_append(lip->m_cpp_text_start);
928
906
 
929
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
907
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
930
908
                                    lip->m_cpp_text_end);
931
909
 
932
910
      return(result_state);                     // IDENT or IDENT_QUOTED
1029
1007
 
1030
1008
      lip->body_utf8_append(lip->m_cpp_text_start);
1031
1009
 
1032
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1010
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1033
1011
                                    lip->m_cpp_text_end);
1034
1012
 
1035
1013
      return(result_state);
1070
1048
 
1071
1049
      lip->body_utf8_append(lip->m_cpp_text_start);
1072
1050
 
1073
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1051
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1074
1052
                                    lip->m_cpp_text_end);
1075
1053
 
1076
1054
      return(IDENT_QUOTED);
1184
1162
 
1185
1163
      lip->body_utf8_append(lip->m_cpp_text_start);
1186
1164
 
1187
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
 
1165
      lip->body_utf8_append_literal(session, &yylval->lex_str,
1188
1166
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1189
1167
        lip->m_cpp_text_end);
1190
1168
 
1326
1304
    case MY_LEX_SEMICOLON:                      // optional line terminator
1327
1305
      if (lip->yyPeek())
1328
1306
      {
1329
 
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1307
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
1330
1308
        {
1331
1309
          lip->found_semicolon= lip->get_ptr();
1332
 
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1310
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
1333
1311
          lip->next_state= MY_LEX_END;
1334
1312
          lip->set_echo(true);
1335
1313
          return (END_OF_INPUT);
1357
1335
    case MY_LEX_END:
1358
1336
      lip->next_state=MY_LEX_END;
1359
1337
      return(0);                        // We found end of input last time
1360
 
      
 
1338
 
1361
1339
      /* Actually real shouldn't start with . but allow them anyhow */
1362
1340
    case MY_LEX_REAL_OR_POINT:
1363
1341
      if (my_isdigit(cs,lip->yyPeek()))
1424
1402
 
1425
1403
      lip->body_utf8_append(lip->m_cpp_text_start);
1426
1404
 
1427
 
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
 
1405
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1428
1406
                                    lip->m_cpp_text_end);
1429
1407
 
1430
1408
      return(result_state);
1442
1420
  statements and stored procedures and is compensated by always
1443
1421
  supplying a copy of Alter_info to these functions.
1444
1422
 
1445
 
  @return You need to use check the error in THD for out
 
1423
  @return You need to use check the error in Session for out
1446
1424
  of memory condition after calling this function.
1447
1425
*/
1448
1426
 
1561
1539
  parent_lex->push_context(&context);
1562
1540
  cond_count= between_count= with_wild= 0;
1563
1541
  max_equal_elems= 0;
1564
 
  conds_processed_with_permanent_arena= 0;
1565
1542
  ref_pointer_array= 0;
1566
1543
  select_n_where_fields= 0;
1567
1544
  select_n_having_items= 0;
1667
1644
  // Remove slave structure
1668
1645
  for (; slave; slave= slave->next)
1669
1646
    slave->fast_exclude();
1670
 
  
 
1647
 
1671
1648
}
1672
1649
 
1673
1650
/*
1681
1658
  //exclude from other structures
1682
1659
  if ((*prev= next))
1683
1660
    next->prev= prev;
1684
 
  /* 
1685
 
     We do not need following statements, because prev pointer of first 
 
1661
  /*
 
1662
     We do not need following statements, because prev pointer of first
1686
1663
     list element point to master->slave
1687
1664
     if (master->slave == this)
1688
1665
       master->slave= next;
1697
1674
    st_select_lex_unit::exclude_level()
1698
1675
 
1699
1676
  NOTE: units which belong to current will be brought up on level of
1700
 
  currernt unit 
 
1677
  currernt unit
1701
1678
*/
1702
1679
void st_select_lex_unit::exclude_level()
1703
1680
{
1768
1745
 
1769
1746
 
1770
1747
/*
1771
 
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1748
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from
1772
1749
  this to 'last' as dependent
1773
1750
 
1774
1751
  SYNOPSIS
1775
 
    last - pointer to last st_select_lex struct, before wich all 
 
1752
    last - pointer to last st_select_lex struct, before wich all
1776
1753
           st_select_lex have to be marked as dependent
1777
1754
 
1778
1755
  NOTE
1811
1788
  }
1812
1789
}
1813
1790
 
1814
 
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
 
1791
bool st_select_lex_node::set_braces(bool)
1815
1792
{ return 1; }
1816
1793
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
1817
1794
uint32_t st_select_lex_node::get_in_sum_expr()           { return 0; }
1818
1795
TableList* st_select_lex_node::get_table_list()     { return 0; }
1819
1796
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
1820
 
TableList *st_select_lex_node::add_table_to_list (THD *thd __attribute__((unused)),
1821
 
                                                   Table_ident *table __attribute__((unused)),
1822
 
                                                  LEX_STRING *alias __attribute__((unused)),
1823
 
                                                  uint32_t table_join_options __attribute__((unused)),
1824
 
                                                  thr_lock_type flags __attribute__((unused)),
1825
 
                                                  List<Index_hint> *hints __attribute__((unused)),
1826
 
                                                  LEX_STRING *option __attribute__((unused)))
 
1797
TableList *st_select_lex_node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1798
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1827
1799
{
1828
1800
  return 0;
1829
1801
}
1859
1831
}
1860
1832
 
1861
1833
 
1862
 
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
 
1834
bool st_select_lex::add_order_to_list(Session *session, Item *item, bool asc)
1863
1835
{
1864
 
  return add_to_list(thd, order_list, item, asc);
 
1836
  return add_to_list(session, order_list, item, asc);
1865
1837
}
1866
1838
 
1867
1839
 
1868
 
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
1869
 
                                     Item *item)
 
1840
bool st_select_lex::add_item_to_list(Session *, Item *item)
1870
1841
{
1871
1842
  return(item_list.push_back(item));
1872
1843
}
1873
1844
 
1874
1845
 
1875
 
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
 
1846
bool st_select_lex::add_group_to_list(Session *session, Item *item, bool asc)
1876
1847
{
1877
 
  return add_to_list(thd, group_list, item, asc);
 
1848
  return add_to_list(session, group_list, item, asc);
1878
1849
}
1879
1850
 
1880
1851
 
1893
1864
bool st_select_lex::set_braces(bool value)
1894
1865
{
1895
1866
  braces= value;
1896
 
  return 0; 
 
1867
  return 0;
1897
1868
}
1898
1869
 
1899
1870
 
1926
1897
}
1927
1898
 
1928
1899
 
1929
 
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
 
1900
bool st_select_lex::setup_ref_array(Session *session, uint32_t order_group_num)
1930
1901
{
1931
1902
  if (ref_pointer_array)
1932
1903
    return 0;
1933
1904
 
1934
1905
  return (ref_pointer_array=
1935
 
          (Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
 
1906
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1936
1907
                                                 item_list.elements +
1937
1908
                                                 select_n_having_items +
1938
1909
                                                 select_n_where_fields +
1955
1926
    }
1956
1927
    if (sl->braces)
1957
1928
      str->append('(');
1958
 
    sl->print(thd, str, query_type);
 
1929
    sl->print(session, str, query_type);
1959
1930
    if (sl->braces)
1960
1931
      str->append(')');
1961
1932
  }
1969
1940
        (order_st *) fake_select_lex->order_list.first,
1970
1941
        query_type);
1971
1942
    }
1972
 
    fake_select_lex->print_limit(thd, str, query_type);
 
1943
    fake_select_lex->print_limit(session, str, query_type);
1973
1944
  }
1974
1945
}
1975
1946
 
1994
1965
      str->append(',');
1995
1966
  }
1996
1967
}
1997
 
 
1998
 
 
1999
 
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
2000
 
                                String *str,
 
1968
 
 
1969
 
 
1970
void st_select_lex::print_limit(Session *, String *str,
2001
1971
                                enum_query_type query_type)
2002
1972
{
2003
1973
  SELECT_LEX_UNIT *unit= master_unit();
2037
2007
}
2038
2008
 
2039
2009
/**
2040
 
  @brief Restore the LEX and THD in case of a parse error.
 
2010
  @brief Restore the LEX and Session in case of a parse error.
2041
2011
 
2042
2012
  This is a clean up call that is invoked by the Bison generated
2043
 
  parser before returning an error from MYSQLparse. If your
 
2013
  parser before returning an error from DRIZZLEparse. If your
2044
2014
  semantic actions manipulate with the global thread state (which
2045
2015
  is a very bad practice and should not normally be employed) and
2046
2016
  need a clean-up in case of error, and you can not use %destructor
2048
2018
  to implement the clean up.
2049
2019
*/
2050
2020
 
2051
 
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
 
2021
void LEX::cleanup_lex_after_parse_error(Session *)
2052
2022
{
2053
2023
}
2054
2024
 
2085
2055
  query_tables= 0;
2086
2056
  query_tables_last= &query_tables;
2087
2057
  query_tables_own_last= 0;
2088
 
  if (init)
2089
 
  {
2090
 
    /*
2091
 
      We delay real initialization of hash (and therefore related
2092
 
      memory allocation) until first insertion into this hash.
2093
 
    */
2094
 
    hash_clear(&sroutines);
2095
 
  }
2096
 
  else if (sroutines.records)
2097
 
  {
2098
 
    /* Non-zero sroutines.records means that hash was initialized. */
2099
 
    my_hash_reset(&sroutines);
2100
 
  }
2101
 
  sroutines_list.empty();
2102
 
  sroutines_list_own_last= sroutines_list.next;
2103
 
  sroutines_list_own_elements= 0;
2104
 
  binlog_stmt_flags= 0;
2105
2058
}
2106
2059
 
2107
2060
 
2114
2067
 
2115
2068
void Query_tables_list::destroy_query_tables_list()
2116
2069
{
2117
 
  hash_free(&sroutines);
2118
2070
}
2119
2071
 
2120
2072
 
2122
2074
  Initialize LEX object.
2123
2075
 
2124
2076
  SYNOPSIS
2125
 
    st_lex::st_lex()
 
2077
    LEX::LEX()
2126
2078
 
2127
2079
  NOTE
2128
2080
    LEX object initialized with this constructor can be used as part of
2129
 
    THD object for which one can safely call open_tables(), lock_tables()
 
2081
    Session object for which one can safely call open_tables(), lock_tables()
2130
2082
    and close_thread_tables() functions. But it is not yet ready for
2131
2083
    statement parsing. On should use lex_start() function to prepare LEX
2132
2084
    for this.
2133
2085
*/
2134
2086
 
2135
 
st_lex::st_lex()
 
2087
LEX::LEX()
2136
2088
  :result(0), yacc_yyss(0), yacc_yyvs(0),
2137
2089
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2138
2090
{
2139
2091
 
2140
 
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2141
 
                         plugins_static_buffer,
2142
 
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
2143
 
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
2144
2092
  reset_query_tables_list(true);
2145
2093
}
2146
2094
 
2147
 
 
2148
 
/*
2149
 
  Check whether the merging algorithm can be used on this VIEW
2150
 
 
2151
 
  SYNOPSIS
2152
 
    st_lex::can_be_merged()
2153
 
 
2154
 
  DESCRIPTION
2155
 
    We can apply merge algorithm if it is single SELECT view  with
2156
 
    subqueries only in WHERE clause (we do not count SELECTs of underlying
2157
 
    views, and second level subqueries) and we have not grpouping, ordering,
2158
 
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2159
 
    several underlying tables.
2160
 
 
2161
 
  RETURN
2162
 
    false - only temporary table algorithm can be used
2163
 
    true  - merge algorithm can be used
2164
 
*/
2165
 
 
2166
 
bool st_lex::can_be_merged()
2167
 
{
2168
 
  // TODO: do not forget implement case when select_lex.table_list.elements==0
2169
 
 
2170
 
  /* find non VIEW subqueries/unions */
2171
 
  bool selects_allow_merge= select_lex.next_select() == 0;
2172
 
  if (selects_allow_merge)
2173
 
  {
2174
 
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2175
 
         tmp_unit;
2176
 
         tmp_unit= tmp_unit->next_unit())
2177
 
    {
2178
 
      if (tmp_unit->first_select()->parent_lex == this &&
2179
 
          (tmp_unit->item == 0 ||
2180
 
           (tmp_unit->item->place() != IN_WHERE &&
2181
 
            tmp_unit->item->place() != IN_ON)))
2182
 
      {
2183
 
        selects_allow_merge= 0;
2184
 
        break;
2185
 
      }
2186
 
    }
2187
 
  }
2188
 
 
2189
 
  return (selects_allow_merge &&
2190
 
          select_lex.group_list.elements == 0 &&
2191
 
          select_lex.having == 0 &&
2192
 
          select_lex.with_sum_func == 0 &&
2193
 
          select_lex.table_list.elements >= 1 &&
2194
 
          !(select_lex.options & SELECT_DISTINCT) &&
2195
 
          select_lex.select_limit == 0);
2196
 
}
2197
 
 
2198
 
 
2199
 
/*
2200
 
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
2201
 
 
2202
 
  SYNOPSIS
2203
 
    st_lex::can_use_merged()
2204
 
 
2205
 
  DESCRIPTION
2206
 
    Only listed here commands can use merge algorithm in top level
2207
 
    SELECT_LEX (for subqueries will be used merge algorithm if
2208
 
    st_lex::can_not_use_merged() is not true).
2209
 
 
2210
 
  RETURN
2211
 
    false - command can't use merged VIEWs
2212
 
    true  - VIEWs with MERGE algorithms can be used
2213
 
*/
2214
 
 
2215
 
bool st_lex::can_use_merged()
2216
 
{
2217
 
  switch (sql_command)
2218
 
  {
2219
 
  case SQLCOM_SELECT:
2220
 
  case SQLCOM_CREATE_TABLE:
2221
 
  case SQLCOM_UPDATE:
2222
 
  case SQLCOM_UPDATE_MULTI:
2223
 
  case SQLCOM_DELETE:
2224
 
  case SQLCOM_DELETE_MULTI:
2225
 
  case SQLCOM_INSERT:
2226
 
  case SQLCOM_INSERT_SELECT:
2227
 
  case SQLCOM_REPLACE:
2228
 
  case SQLCOM_REPLACE_SELECT:
2229
 
  case SQLCOM_LOAD:
2230
 
    return true;
2231
 
  default:
2232
 
    return false;
2233
 
  }
2234
 
}
2235
 
 
2236
 
/*
2237
 
  Check if command can't use merged views in any part of command
2238
 
 
2239
 
  SYNOPSIS
2240
 
    st_lex::can_not_use_merged()
2241
 
 
2242
 
  DESCRIPTION
2243
 
    Temporary table algorithm will be used on all SELECT levels for queries
2244
 
    listed here (see also st_lex::can_use_merged()).
2245
 
 
2246
 
  RETURN
2247
 
    false - command can't use merged VIEWs
2248
 
    true  - VIEWs with MERGE algorithms can be used
2249
 
*/
2250
 
 
2251
 
bool st_lex::can_not_use_merged()
2252
 
{
2253
 
  switch (sql_command)
2254
 
  {
2255
 
  /*
2256
 
    SQLCOM_SHOW_FIELDS is necessary to make 
2257
 
    information schema tables working correctly with views.
2258
 
    see get_schema_tables_result function
2259
 
  */
2260
 
  case SQLCOM_SHOW_FIELDS:
2261
 
    return true;
2262
 
  default:
2263
 
    return false;
2264
 
  }
2265
 
}
2266
 
 
2267
2095
/*
2268
2096
  Detect that we need only table structure of derived table/view
2269
2097
 
2275
2103
    false no, we need data
2276
2104
*/
2277
2105
 
2278
 
bool st_lex::only_view_structure()
 
2106
bool LEX::only_view_structure()
2279
2107
{
2280
2108
  switch (sql_command) {
2281
2109
  case SQLCOM_SHOW_CREATE:
2300
2128
*/
2301
2129
 
2302
2130
 
2303
 
bool st_lex::need_correct_ident()
 
2131
bool LEX::need_correct_ident()
2304
2132
{
2305
2133
  switch(sql_command)
2306
2134
  {
2319
2147
  and will initialize the destination with the default
2320
2148
  database of the stored routine, rather than the default
2321
2149
  database of the connection it is parsed in.
2322
 
  E.g. if one has no current database selected, or current database 
 
2150
  E.g. if one has no current database selected, or current database
2323
2151
  set to 'bar' and then issues:
2324
2152
 
2325
2153
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2333
2161
*/
2334
2162
 
2335
2163
bool
2336
 
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
 
2164
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
2337
2165
{
2338
 
  return thd->copy_db_to(p_db, p_db_length);
 
2166
  return session->copy_db_to(p_db, p_db_length);
2339
2167
}
2340
2168
 
2341
2169
/*
2353
2181
 
2354
2182
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2355
2183
  select_limit_val= (ha_rows)val;
2356
 
#ifndef BIG_TABLES
2357
 
  /* 
 
2184
  /*
2358
2185
    Check for overflow : ha_rows can be smaller then uint64_t if
2359
2186
    BIG_TABLES is off.
2360
2187
    */
2361
2188
  if (val != (uint64_t)select_limit_val)
2362
2189
    select_limit_val= HA_POS_ERROR;
2363
 
#endif
2364
2190
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2365
2191
                                                 0UL);
2366
2192
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2387
2213
      In this case link_to_local is set.
2388
2214
 
2389
2215
*/
2390
 
TableList *st_lex::unlink_first_table(bool *link_to_local)
 
2216
TableList *LEX::unlink_first_table(bool *link_to_local)
2391
2217
{
2392
2218
  TableList *first;
2393
2219
  if ((first= query_tables))
2406
2232
    */
2407
2233
    if ((*link_to_local= test(select_lex.table_list.first)))
2408
2234
    {
2409
 
      select_lex.context.table_list= 
 
2235
      select_lex.context.table_list=
2410
2236
        select_lex.context.first_name_resolution_table= first->next_local;
2411
2237
      select_lex.table_list.first= (unsigned char*) (first->next_local);
2412
2238
      select_lex.table_list.elements--; //safety
2427
2253
  table list
2428
2254
 
2429
2255
  SYNOPSYS
2430
 
     st_lex::first_lists_tables_same()
 
2256
     LEX::first_lists_tables_same()
2431
2257
 
2432
2258
  NOTES
2433
2259
    In many cases (for example, usual INSERT/DELETE/...) the first table of
2438
2264
    the global list first.
2439
2265
*/
2440
2266
 
2441
 
void st_lex::first_lists_tables_same()
 
2267
void LEX::first_lists_tables_same()
2442
2268
{
2443
2269
  TableList *first_table= (TableList*) select_lex.table_list.first;
2444
2270
  if (query_tables != first_table && first_table != 0)
2474
2300
    global list
2475
2301
*/
2476
2302
 
2477
 
void st_lex::link_first_table_back(TableList *first,
 
2303
void LEX::link_first_table_back(TableList *first,
2478
2304
                                   bool link_to_local)
2479
2305
{
2480
2306
  if (first)
2501
2327
  cleanup lex for case when we open table by table for processing
2502
2328
 
2503
2329
  SYNOPSIS
2504
 
    st_lex::cleanup_after_one_table_open()
 
2330
    LEX::cleanup_after_one_table_open()
2505
2331
 
2506
2332
  NOTE
2507
2333
    This method is mostly responsible for cleaning up of selects lists and
2509
2335
    to call Query_tables_list::reset_query_tables_list(false).
2510
2336
*/
2511
2337
 
2512
 
void st_lex::cleanup_after_one_table_open()
 
2338
void LEX::cleanup_after_one_table_open()
2513
2339
{
2514
2340
  /*
2515
 
    thd->lex->derived_tables & additional units may be set if we open
2516
 
    a view. It is necessary to clear thd->lex->derived_tables flag
 
2341
    session->lex->derived_tables & additional units may be set if we open
 
2342
    a view. It is necessary to clear session->lex->derived_tables flag
2517
2343
    to prevent processing of derived tables during next open_and_lock_tables
2518
2344
    if next table is a real table and cleanup & remove underlying units
2519
 
    NOTE: all units will be connected to thd->lex->select_lex, because we
 
2345
    NOTE: all units will be connected to session->lex->select_lex, because we
2520
2346
    have not UNION on most upper level.
2521
2347
    */
2522
2348
  if (all_selects_list != &select_lex)
2536
2362
 
2537
2363
 
2538
2364
/*
2539
 
  Save current state of Query_tables_list for this LEX, and prepare it
2540
 
  for processing of new statemnt.
2541
 
 
2542
 
  SYNOPSIS
2543
 
    reset_n_backup_query_tables_list()
2544
 
      backup  Pointer to Query_tables_list instance to be used for backup
2545
 
*/
2546
 
 
2547
 
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2548
 
{
2549
 
}
2550
 
 
2551
 
 
2552
 
/*
2553
 
  Restore state of Query_tables_list for this LEX from backup.
2554
 
 
2555
 
  SYNOPSIS
2556
 
    restore_backup_query_tables_list()
2557
 
      backup  Pointer to Query_tables_list instance used for backup
2558
 
*/
2559
 
 
2560
 
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2561
 
{
2562
 
}
2563
 
 
2564
 
 
2565
 
/*
2566
 
  Checks for usage of routines and/or tables in a parsed statement
2567
 
 
2568
 
  SYNOPSIS
2569
 
    st_lex:table_or_sp_used()
2570
 
 
2571
 
  RETURN
2572
 
    false  No routines and tables used
2573
 
    true   Either or both routines and tables are used.
2574
 
*/
2575
 
 
2576
 
bool st_lex::table_or_sp_used()
2577
 
{
2578
 
  if (sroutines.records || query_tables)
2579
 
    return(true);
2580
 
 
2581
 
  return(false);
2582
 
}
2583
 
 
2584
 
 
2585
 
/*
2586
2365
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2587
2366
 
2588
2367
  SYNOPSIS
2589
2368
    fix_prepare_info_in_table_list()
2590
 
      thd  Thread handle
 
2369
      session  Thread handle
2591
2370
      tbl  List of tables to process
2592
2371
 
2593
2372
  DESCRIPTION
2597
2376
 
2598
2377
*/
2599
2378
 
2600
 
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
 
2379
static void fix_prepare_info_in_table_list(Session *session, TableList *tbl)
2601
2380
{
2602
2381
  for (; tbl; tbl= tbl->next_local)
2603
2382
  {
2604
2383
    if (tbl->on_expr)
2605
2384
    {
2606
2385
      tbl->prep_on_expr= tbl->on_expr;
2607
 
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
 
2386
      tbl->on_expr= tbl->on_expr->copy_andor_structure(session);
2608
2387
    }
2609
 
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
 
2388
    fix_prepare_info_in_table_list(session, tbl->merge_underlying_list);
2610
2389
  }
2611
2390
}
2612
2391
 
2633
2412
 
2634
2413
  DESCRIPTION
2635
2414
    Used in filling up the tagged hints list.
2636
 
    This list is filled by first setting the kind of the hint as a 
 
2415
    This list is filled by first setting the kind of the hint as a
2637
2416
    context variable and then adding hints of the current kind.
2638
2417
    Then the context variable index_hint_type can be reset to the
2639
2418
    next hint type.
2640
2419
*/
2641
2420
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
2642
2421
                                        index_clause_map clause)
2643
 
 
2422
{
2644
2423
  current_index_hint_type= type_arg;
2645
2424
  current_index_hint_clause= clause;
2646
2425
}
2651
2430
 
2652
2431
  SYNOPSIS
2653
2432
    alloc_index_hints()
2654
 
      thd         current thread.
 
2433
      session         current thread.
2655
2434
*/
2656
2435
 
2657
 
void st_select_lex::alloc_index_hints (THD *thd)
2658
 
2659
 
  index_hints= new (thd->mem_root) List<Index_hint>(); 
 
2436
void st_select_lex::alloc_index_hints (Session *session)
 
2437
{
 
2438
  index_hints= new (session->mem_root) List<Index_hint>();
2660
2439
}
2661
2440
 
2662
2441
 
2663
2442
 
2664
2443
/*
2665
 
  adds an element to the array storing index usage hints 
 
2444
  adds an element to the array storing index usage hints
2666
2445
  (ADD/FORCE/IGNORE INDEX).
2667
2446
 
2668
2447
  SYNOPSIS
2669
2448
    add_index_hint()
2670
 
      thd         current thread.
 
2449
      session         current thread.
2671
2450
      str         name of the index.
2672
2451
      length      number of characters in str.
2673
2452
 
2674
2453
  RETURN VALUE
2675
2454
    0 on success, non-zero otherwise
2676
2455
*/
2677
 
bool st_select_lex::add_index_hint (THD *thd, char *str, uint32_t length)
 
2456
bool st_select_lex::add_index_hint (Session *session, char *str, uint32_t length)
2678
2457
{
2679
 
  return index_hints->push_front (new (thd->mem_root) 
 
2458
  return index_hints->push_front (new (session->mem_root)
2680
2459
                                 Index_hint(current_index_hint_type,
2681
2460
                                            current_index_hint_clause,
2682
2461
                                            str, length));