~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2009-02-27 21:38:40 UTC
  • Revision ID: brian@tangent.org-20090227213840-r9hq3sfk8d8qrg72
Code cleanup in signal_handler.cc

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>
21
22
#include <drizzled/error.h>
 
23
#include <drizzled/session.h>
 
24
#include <drizzled/sql_base.h>
 
25
#include <drizzled/lookup_symbol.h>
 
26
 
 
27
 
22
28
 
23
29
static int lex_one_token(void *arg, void *yysession);
24
30
 
 
31
 
25
32
/*
26
33
  We are using pointer to this variable for distinguishing between assignment
27
34
  to NEW row field (when parsing trigger definition) and structured variable.
36
43
 
37
44
 
38
45
/*
39
 
  The following data is based on the latin1 character set, and is only
40
 
  used when comparing keywords
41
 
*/
42
 
 
43
 
static unsigned char to_upper_lex[]=
44
 
{
45
 
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
46
 
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
47
 
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48
 
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
49
 
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
50
 
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
51
 
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
52
 
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
53
 
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
54
 
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
55
 
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
56
 
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
57
 
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
58
 
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
59
 
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
60
 
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
61
 
};
62
 
 
63
 
/* 
64
 
  Names of the index hints (for error messages). Keep in sync with 
65
 
  index_hint_type 
 
46
  Names of the index hints (for error messages). Keep in sync with
 
47
  index_hint_type
66
48
*/
67
49
 
68
50
const char * index_hint_type_name[] =
69
51
{
70
 
  "IGNORE INDEX", 
71
 
  "USE INDEX", 
 
52
  "IGNORE INDEX",
 
53
  "USE INDEX",
72
54
  "FORCE INDEX"
73
55
};
74
56
 
75
 
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
76
 
{
77
 
  while (len-- != 0 &&
78
 
         to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
79
 
  return (int) len+1;
80
 
}
81
 
 
82
 
#include <lex_hash.h>
83
 
 
84
 
 
85
 
void lex_init(void)
86
 
{
87
 
  uint32_t i;
88
 
  for (i=0 ; i < array_elements(symbols) ; i++)
89
 
    symbols[i].length=(unsigned char) strlen(symbols[i].name);
90
 
  for (i=0 ; i < array_elements(sql_functions) ; i++)
91
 
    sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
92
 
 
93
 
  return;
94
 
}
95
 
 
96
 
 
97
 
void lex_free(void)
98
 
{                                       // Call this when daemon ends
99
 
  return;
100
 
}
101
57
 
102
58
 
103
59
void
104
60
st_parsing_options::reset()
105
61
{
106
 
  allows_variable= true;
107
 
  allows_select_into= true;
108
62
  allows_select_procedure= true;
109
 
  allows_derived= true;
110
63
}
111
64
 
112
65
Lex_input_stream::Lex_input_stream(Session *session,
162
115
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
163
116
 
164
117
  uint32_t body_utf8_length=
165
 
    (m_buf_length / session->variables.character_set_client->mbminlen) *
 
118
    (m_buf_length / default_charset_info->mbminlen) *
166
119
    my_charset_utf8_bin.mbmaxlen;
167
120
 
168
121
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
302
255
  lex->select_lex.master= &lex->unit;
303
256
  lex->select_lex.prev= &lex->unit.slave;
304
257
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
305
 
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
 
258
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
306
259
  lex->select_lex.options= 0;
307
260
  lex->select_lex.init_order();
308
261
  lex->select_lex.group_list.empty();
312
265
  lex->lock_option= TL_READ;
313
266
  lex->leaf_tables_insert= 0;
314
267
  lex->parsing_options.reset();
315
 
  lex->empty_field_list_on_rset= 0;
316
268
  lex->select_lex.select_number= 1;
317
269
  lex->length=0;
318
270
  lex->select_lex.in_sum_expr=0;
336
288
  lex->nest_level=0 ;
337
289
  lex->allow_sum_func= 0;
338
290
  lex->in_sum_func= NULL;
339
 
  /*
340
 
    ok, there must be a better solution for this, long-term
341
 
    I tried "memset" in the sql_yacc.yy code, but that for
342
 
    some reason made the values zero, even if they were set
343
 
  */
344
 
  lex->server_options.server_name= 0;
345
 
  lex->server_options.server_name_length= 0;
346
 
  lex->server_options.host= 0;
347
 
  lex->server_options.db= 0;
348
 
  lex->server_options.username= 0;
349
 
  lex->server_options.password= 0;
350
 
  lex->server_options.scheme= 0;
351
 
  lex->server_options.owner= 0;
352
 
  lex->server_options.port= -1;
353
291
 
354
292
  lex->is_lex_started= true;
355
 
  return;
356
293
}
357
294
 
358
295
void lex_end(LEX *lex)
365
302
    lex->yacc_yyvs= 0;
366
303
  }
367
304
 
368
 
  /* release used plugins */
369
 
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
370
 
                     lex->plugins.elements);
371
 
  reset_dynamic(&lex->plugins);
372
 
 
373
305
  delete lex->result;
374
306
  lex->result= 0;
375
 
 
376
 
  return;
377
307
}
378
308
 
379
309
 
380
310
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
381
311
{
 
312
  /* Plenty of memory for the largest lex symbol we have */
 
313
  char tok_upper[64];
382
314
  const char *tok= lip->get_tok_start();
 
315
  uint32_t tok_pos= 0;
 
316
  for (;tok_pos<len && tok_pos<63;tok_pos++)
 
317
    tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
 
318
  tok_upper[tok_pos]=0;
383
319
 
384
 
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
 
320
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
385
321
  if (symbol)
386
322
  {
387
323
    lip->yylval->symbol.symbol=symbol;
390
326
 
391
327
    return symbol->tok;
392
328
  }
 
329
 
393
330
  return 0;
394
331
}
395
332
 
396
 
/*
397
 
  Check if name is a keyword
398
 
 
399
 
  SYNOPSIS
400
 
    is_keyword()
401
 
    name      checked name (must not be empty)
402
 
    len       length of checked name
403
 
 
404
 
  RETURN VALUES
405
 
    0         name is a keyword
406
 
    1         name isn't a keyword
407
 
*/
408
 
 
409
 
bool is_keyword(const char *name, uint32_t len)
410
 
{
411
 
  assert(len != 0);
412
 
  return get_hash_symbol(name,len,0)!=0;
413
 
}
414
333
 
415
334
bool is_lex_native_function(const LEX_STRING *name)
416
335
{
417
336
  assert(name != NULL);
418
 
  return (get_hash_symbol(name->str, name->length, 1) != 0);
 
337
  return (lookup_symbol(name->str, name->length, 1) != 0);
419
338
}
420
339
 
421
340
/* make a copy of token before ptr and set yytoklen */
433
352
  return tmp;
434
353
}
435
354
 
436
 
/* 
437
 
 todo: 
438
 
   There are no dangerous charsets in mysql for function 
439
 
   get_quoted_token yet. But it should be fixed in the 
 
355
/*
 
356
 todo:
 
357
   There are no dangerous charsets in mysql for function
 
358
   get_quoted_token yet. But it should be fixed in the
440
359
   future to operate multichar strings (like ucs2)
441
360
*/
442
361
 
527
446
      end -= post_skip;
528
447
      assert(end >= str);
529
448
 
530
 
      if (!(start= (char*) lip->m_session->alloc((uint) (end-str)+1)))
 
449
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
531
450
        return (char*) "";              // Sql_alloc has set error flag
532
451
 
533
452
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
535
454
 
536
455
      if (!found_escape)
537
456
      {
538
 
        lip->yytoklen=(uint) (end-str);
 
457
        lip->yytoklen=(uint32_t) (end-str);
539
458
        memcpy(start,str,lip->yytoklen);
540
459
        start[lip->yytoklen]=0;
541
460
      }
591
510
            *to++ = *str;
592
511
        }
593
512
        *to=0;
594
 
        lip->yytoklen=(uint) (to-start);
 
513
        lip->yytoklen=(uint32_t) (to-start);
595
514
      }
596
515
      return start;
597
516
    }
745
664
      lip->lookahead_token= token;
746
665
      return WITH;
747
666
    }
748
 
    break;
749
667
  default:
750
668
    break;
751
669
  }
901
819
      }
902
820
      yylval->lex_str=get_token(lip, 0, length);
903
821
 
904
 
      /*
905
 
         Note: "SELECT _bla AS 'alias'"
906
 
         _bla should be considered as a IDENT if charset haven't been found.
907
 
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
908
 
         producing an error.
909
 
      */
910
 
 
911
 
      if (yylval->lex_str.str[0] == '_')
912
 
      {
913
 
        const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
914
 
                                                             MY_CS_PRIMARY, MYF(0));
915
 
        if (cs)
916
 
        {
917
 
          yylval->charset= cs;
918
 
          lip->m_underscore_cs= cs;
919
 
 
920
 
          lip->body_utf8_append(lip->m_cpp_text_start,
921
 
                                lip->get_cpp_tok_start() + length);
922
 
          return(UNDERSCORE_CHARSET);
923
 
        }
924
 
      }
925
 
 
926
822
      lip->body_utf8_append(lip->m_cpp_text_start);
927
823
 
928
824
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
1356
1252
    case MY_LEX_END:
1357
1253
      lip->next_state=MY_LEX_END;
1358
1254
      return(0);                        // We found end of input last time
1359
 
      
 
1255
 
1360
1256
      /* Actually real shouldn't start with . but allow them anyhow */
1361
1257
    case MY_LEX_REAL_OR_POINT:
1362
1258
      if (my_isdigit(cs,lip->yyPeek()))
1500
1396
 
1501
1397
 
1502
1398
/*
1503
 
  st_select_lex structures initialisations
 
1399
  Select_Lex structures initialisations
1504
1400
*/
1505
1401
 
1506
 
void st_select_lex_node::init_query()
 
1402
void Select_Lex_Node::init_query()
1507
1403
{
1508
1404
  options= 0;
1509
1405
  linkage= UNSPECIFIED_TYPE;
1511
1407
  uncacheable= 0;
1512
1408
}
1513
1409
 
1514
 
void st_select_lex_node::init_select()
 
1410
void Select_Lex_Node::init_select()
1515
1411
{
1516
1412
}
1517
1413
 
1518
 
void st_select_lex_unit::init_query()
 
1414
void Select_Lex_Unit::init_query()
1519
1415
{
1520
 
  st_select_lex_node::init_query();
 
1416
  Select_Lex_Node::init_query();
1521
1417
  linkage= GLOBAL_OPTIONS_TYPE;
1522
1418
  global_parameters= first_select();
1523
1419
  select_limit_cnt= HA_POS_ERROR;
1534
1430
  found_rows_for_union= 0;
1535
1431
}
1536
1432
 
1537
 
void st_select_lex::init_query()
 
1433
void Select_Lex::init_query()
1538
1434
{
1539
 
  st_select_lex_node::init_query();
 
1435
  Select_Lex_Node::init_query();
1540
1436
  table_list.empty();
1541
1437
  top_join_list.empty();
1542
1438
  join_list= &top_join_list;
1560
1456
  parent_lex->push_context(&context);
1561
1457
  cond_count= between_count= with_wild= 0;
1562
1458
  max_equal_elems= 0;
1563
 
  conds_processed_with_permanent_arena= 0;
1564
1459
  ref_pointer_array= 0;
1565
1460
  select_n_where_fields= 0;
1566
1461
  select_n_having_items= 0;
1572
1467
  link_next= 0;
1573
1468
}
1574
1469
 
1575
 
void st_select_lex::init_select()
 
1470
void Select_Lex::init_select()
1576
1471
{
1577
 
  st_select_lex_node::init_select();
 
1472
  Select_Lex_Node::init_select();
1578
1473
  sj_nests.empty();
1579
1474
  group_list.empty();
1580
1475
  type= db= 0;
1604
1499
}
1605
1500
 
1606
1501
/*
1607
 
  st_select_lex structures linking
 
1502
  Select_Lex structures linking
1608
1503
*/
1609
1504
 
1610
1505
/* include on level down */
1611
 
void st_select_lex_node::include_down(st_select_lex_node *upper)
 
1506
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1612
1507
{
1613
1508
  if ((next= upper->slave))
1614
1509
    next->prev= &next;
1622
1517
  include on level down (but do not link)
1623
1518
 
1624
1519
  SYNOPSYS
1625
 
    st_select_lex_node::include_standalone()
 
1520
    Select_Lex_Node::include_standalone()
1626
1521
    upper - reference on node underr which this node should be included
1627
1522
    ref - references on reference on this node
1628
1523
*/
1629
 
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
1630
 
                                            st_select_lex_node **ref)
 
1524
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
 
1525
                                            Select_Lex_Node **ref)
1631
1526
{
1632
1527
  next= 0;
1633
1528
  prev= ref;
1636
1531
}
1637
1532
 
1638
1533
/* include neighbour (on same level) */
1639
 
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
 
1534
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1640
1535
{
1641
1536
  if ((next= before->next))
1642
1537
    next->prev= &next;
1646
1541
  slave= 0;
1647
1542
}
1648
1543
 
1649
 
/* including in global SELECT_LEX list */
1650
 
void st_select_lex_node::include_global(st_select_lex_node **plink)
 
1544
/* including in global Select_Lex list */
 
1545
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1651
1546
{
1652
1547
  if ((link_next= *plink))
1653
1548
    link_next->link_prev= &link_next;
1656
1551
}
1657
1552
 
1658
1553
//excluding from global list (internal function)
1659
 
void st_select_lex_node::fast_exclude()
 
1554
void Select_Lex_Node::fast_exclude()
1660
1555
{
1661
1556
  if (link_prev)
1662
1557
  {
1666
1561
  // Remove slave structure
1667
1562
  for (; slave; slave= slave->next)
1668
1563
    slave->fast_exclude();
1669
 
  
 
1564
 
1670
1565
}
1671
1566
 
1672
1567
/*
1673
1568
  excluding select_lex structure (except first (first select can't be
1674
1569
  deleted, because it is most upper select))
1675
1570
*/
1676
 
void st_select_lex_node::exclude()
 
1571
void Select_Lex_Node::exclude()
1677
1572
{
1678
1573
  //exclude from global list
1679
1574
  fast_exclude();
1680
1575
  //exclude from other structures
1681
1576
  if ((*prev= next))
1682
1577
    next->prev= prev;
1683
 
  /* 
1684
 
     We do not need following statements, because prev pointer of first 
 
1578
  /*
 
1579
     We do not need following statements, because prev pointer of first
1685
1580
     list element point to master->slave
1686
1581
     if (master->slave == this)
1687
1582
       master->slave= next;
1693
1588
  Exclude level of current unit from tree of SELECTs
1694
1589
 
1695
1590
  SYNOPSYS
1696
 
    st_select_lex_unit::exclude_level()
 
1591
    Select_Lex_Unit::exclude_level()
1697
1592
 
1698
1593
  NOTE: units which belong to current will be brought up on level of
1699
 
  currernt unit 
 
1594
  currernt unit
1700
1595
*/
1701
 
void st_select_lex_unit::exclude_level()
 
1596
void Select_Lex_Unit::exclude_level()
1702
1597
{
1703
 
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
1704
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1598
  Select_Lex_Unit *units= 0, **units_last= &units;
 
1599
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1705
1600
  {
1706
1601
    // unlink current level from global SELECTs list
1707
1602
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1708
1603
      sl->link_next->link_prev= sl->link_prev;
1709
1604
 
1710
1605
    // bring up underlay levels
1711
 
    SELECT_LEX_UNIT **last= 0;
1712
 
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1606
    Select_Lex_Unit **last= 0;
 
1607
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1713
1608
    {
1714
1609
      u->master= master;
1715
 
      last= (SELECT_LEX_UNIT**)&(u->next);
 
1610
      last= (Select_Lex_Unit**)&(u->next);
1716
1611
    }
1717
1612
    if (last)
1718
1613
    {
1724
1619
  {
1725
1620
    // include brought up levels in place of current
1726
1621
    (*prev)= units;
1727
 
    (*units_last)= (SELECT_LEX_UNIT*)next;
 
1622
    (*units_last)= (Select_Lex_Unit*)next;
1728
1623
    if (next)
1729
 
      next->prev= (SELECT_LEX_NODE**)units_last;
 
1624
      next->prev= (Select_Lex_Node**)units_last;
1730
1625
    units->prev= prev;
1731
1626
  }
1732
1627
  else
1743
1638
  Exclude subtree of current unit from tree of SELECTs
1744
1639
 
1745
1640
  SYNOPSYS
1746
 
    st_select_lex_unit::exclude_tree()
 
1641
    Select_Lex_Unit::exclude_tree()
1747
1642
*/
1748
 
void st_select_lex_unit::exclude_tree()
 
1643
void Select_Lex_Unit::exclude_tree()
1749
1644
{
1750
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1645
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1751
1646
  {
1752
1647
    // unlink current level from global SELECTs list
1753
1648
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1754
1649
      sl->link_next->link_prev= sl->link_prev;
1755
1650
 
1756
1651
    // unlink underlay levels
1757
 
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1652
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1758
1653
    {
1759
1654
      u->exclude_level();
1760
1655
    }
1767
1662
 
1768
1663
 
1769
1664
/*
1770
 
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
 
1665
  Select_Lex_Node::mark_as_dependent mark all Select_Lex struct from
1771
1666
  this to 'last' as dependent
1772
1667
 
1773
1668
  SYNOPSIS
1774
 
    last - pointer to last st_select_lex struct, before wich all 
1775
 
           st_select_lex have to be marked as dependent
 
1669
    last - pointer to last Select_Lex struct, before wich all
 
1670
           Select_Lex have to be marked as dependent
1776
1671
 
1777
1672
  NOTE
1778
 
    'last' should be reachable from this st_select_lex_node
 
1673
    'last' should be reachable from this Select_Lex_Node
1779
1674
*/
1780
1675
 
1781
 
void st_select_lex::mark_as_dependent(st_select_lex *last)
 
1676
void Select_Lex::mark_as_dependent(Select_Lex *last)
1782
1677
{
1783
1678
  /*
1784
1679
    Mark all selects from resolved to 1 before select where was
1785
1680
    found table as depended (of select where was found table)
1786
1681
  */
1787
 
  for (SELECT_LEX *s= this;
 
1682
  for (Select_Lex *s= this;
1788
1683
       s && s != last;
1789
1684
       s= s->outer_select())
1790
1685
  {
1793
1688
      // Select is dependent of outer select
1794
1689
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1795
1690
                       UNCACHEABLE_DEPENDENT;
1796
 
      SELECT_LEX_UNIT *munit= s->master_unit();
 
1691
      Select_Lex_Unit *munit= s->master_unit();
1797
1692
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1798
1693
                       UNCACHEABLE_DEPENDENT;
1799
 
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1694
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1800
1695
      {
1801
1696
        if (sl != s &&
1802
1697
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1810
1705
  }
1811
1706
}
1812
1707
 
1813
 
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
 
1708
bool Select_Lex_Node::set_braces(bool)
1814
1709
{ return 1; }
1815
 
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
1816
 
uint32_t st_select_lex_node::get_in_sum_expr()           { return 0; }
1817
 
TableList* st_select_lex_node::get_table_list()     { return 0; }
1818
 
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
1819
 
TableList *st_select_lex_node::add_table_to_list (Session *session __attribute__((unused)),
1820
 
                                                   Table_ident *table __attribute__((unused)),
1821
 
                                                  LEX_STRING *alias __attribute__((unused)),
1822
 
                                                  uint32_t table_join_options __attribute__((unused)),
1823
 
                                                  thr_lock_type flags __attribute__((unused)),
1824
 
                                                  List<Index_hint> *hints __attribute__((unused)),
1825
 
                                                  LEX_STRING *option __attribute__((unused)))
 
1710
bool Select_Lex_Node::inc_in_sum_expr()           { return 1; }
 
1711
uint32_t Select_Lex_Node::get_in_sum_expr()           { return 0; }
 
1712
TableList* Select_Lex_Node::get_table_list()     { return 0; }
 
1713
List<Item>* Select_Lex_Node::get_item_list()      { return 0; }
 
1714
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
 
1715
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
1826
1716
{
1827
1717
  return 0;
1828
1718
}
1829
 
uint32_t st_select_lex_node::get_table_join_options()
 
1719
uint32_t Select_Lex_Node::get_table_join_options()
1830
1720
{
1831
1721
  return 0;
1832
1722
}
1834
1724
/*
1835
1725
  prohibit using LIMIT clause
1836
1726
*/
1837
 
bool st_select_lex::test_limit()
 
1727
bool Select_Lex::test_limit()
1838
1728
{
1839
1729
  if (select_limit != 0)
1840
1730
  {
1846
1736
}
1847
1737
 
1848
1738
 
1849
 
st_select_lex_unit* st_select_lex_unit::master_unit()
 
1739
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1850
1740
{
1851
1741
    return this;
1852
1742
}
1853
1743
 
1854
1744
 
1855
 
st_select_lex* st_select_lex_unit::outer_select()
 
1745
Select_Lex* Select_Lex_Unit::outer_select()
1856
1746
{
1857
 
  return (st_select_lex*) master;
 
1747
  return (Select_Lex*) master;
1858
1748
}
1859
1749
 
1860
1750
 
1861
 
bool st_select_lex::add_order_to_list(Session *session, Item *item, bool asc)
 
1751
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1862
1752
{
1863
1753
  return add_to_list(session, order_list, item, asc);
1864
1754
}
1865
1755
 
1866
1756
 
1867
 
bool st_select_lex::add_item_to_list(Session *session __attribute__((unused)),
1868
 
                                     Item *item)
 
1757
bool Select_Lex::add_item_to_list(Session *, Item *item)
1869
1758
{
1870
1759
  return(item_list.push_back(item));
1871
1760
}
1872
1761
 
1873
1762
 
1874
 
bool st_select_lex::add_group_to_list(Session *session, Item *item, bool asc)
 
1763
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1875
1764
{
1876
1765
  return add_to_list(session, group_list, item, asc);
1877
1766
}
1878
1767
 
1879
1768
 
1880
 
st_select_lex_unit* st_select_lex::master_unit()
1881
 
{
1882
 
  return (st_select_lex_unit*) master;
1883
 
}
1884
 
 
1885
 
 
1886
 
st_select_lex* st_select_lex::outer_select()
1887
 
{
1888
 
  return (st_select_lex*) master->get_master();
1889
 
}
1890
 
 
1891
 
 
1892
 
bool st_select_lex::set_braces(bool value)
 
1769
Select_Lex_Unit* Select_Lex::master_unit()
 
1770
{
 
1771
  return (Select_Lex_Unit*) master;
 
1772
}
 
1773
 
 
1774
 
 
1775
Select_Lex* Select_Lex::outer_select()
 
1776
{
 
1777
  return (Select_Lex*) master->get_master();
 
1778
}
 
1779
 
 
1780
 
 
1781
bool Select_Lex::set_braces(bool value)
1893
1782
{
1894
1783
  braces= value;
1895
 
  return 0; 
 
1784
  return 0;
1896
1785
}
1897
1786
 
1898
1787
 
1899
 
bool st_select_lex::inc_in_sum_expr()
 
1788
bool Select_Lex::inc_in_sum_expr()
1900
1789
{
1901
1790
  in_sum_expr++;
1902
1791
  return 0;
1903
1792
}
1904
1793
 
1905
1794
 
1906
 
uint32_t st_select_lex::get_in_sum_expr()
 
1795
uint32_t Select_Lex::get_in_sum_expr()
1907
1796
{
1908
1797
  return in_sum_expr;
1909
1798
}
1910
1799
 
1911
1800
 
1912
 
TableList* st_select_lex::get_table_list()
 
1801
TableList* Select_Lex::get_table_list()
1913
1802
{
1914
1803
  return (TableList*) table_list.first;
1915
1804
}
1916
1805
 
1917
 
List<Item>* st_select_lex::get_item_list()
 
1806
List<Item>* Select_Lex::get_item_list()
1918
1807
{
1919
1808
  return &item_list;
1920
1809
}
1921
1810
 
1922
 
uint32_t st_select_lex::get_table_join_options()
 
1811
uint32_t Select_Lex::get_table_join_options()
1923
1812
{
1924
1813
  return table_join_options;
1925
1814
}
1926
1815
 
1927
1816
 
1928
 
bool st_select_lex::setup_ref_array(Session *session, uint32_t order_group_num)
 
1817
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1929
1818
{
1930
1819
  if (ref_pointer_array)
1931
1820
    return 0;
1939
1828
}
1940
1829
 
1941
1830
 
1942
 
void st_select_lex_unit::print(String *str, enum_query_type query_type)
 
1831
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
1943
1832
{
1944
1833
  bool union_all= !union_distinct;
1945
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
1834
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1946
1835
  {
1947
1836
    if (sl != first_select())
1948
1837
    {
1973
1862
}
1974
1863
 
1975
1864
 
1976
 
void st_select_lex::print_order(String *str,
 
1865
void Select_Lex::print_order(String *str,
1977
1866
                                order_st *order,
1978
1867
                                enum_query_type query_type)
1979
1868
{
1993
1882
      str->append(',');
1994
1883
  }
1995
1884
}
1996
 
 
1997
 
 
1998
 
void st_select_lex::print_limit(Session *session __attribute__((unused)),
1999
 
                                String *str,
 
1885
 
 
1886
 
 
1887
void Select_Lex::print_limit(Session *, String *str,
2000
1888
                                enum_query_type query_type)
2001
1889
{
2002
 
  SELECT_LEX_UNIT *unit= master_unit();
 
1890
  Select_Lex_Unit *unit= master_unit();
2003
1891
  Item_subselect *item= unit->item;
2004
1892
 
2005
1893
  if (item && unit->global_parameters == this)
2047
1935
  to implement the clean up.
2048
1936
*/
2049
1937
 
2050
 
void st_lex::cleanup_lex_after_parse_error(Session *session __attribute__((unused)))
 
1938
void LEX::cleanup_lex_after_parse_error(Session *)
2051
1939
{
2052
1940
}
2053
1941
 
2084
1972
  query_tables= 0;
2085
1973
  query_tables_last= &query_tables;
2086
1974
  query_tables_own_last= 0;
2087
 
  if (init)
2088
 
  {
2089
 
    /*
2090
 
      We delay real initialization of hash (and therefore related
2091
 
      memory allocation) until first insertion into this hash.
2092
 
    */
2093
 
    hash_clear(&sroutines);
2094
 
  }
2095
 
  else if (sroutines.records)
2096
 
  {
2097
 
    /* Non-zero sroutines.records means that hash was initialized. */
2098
 
    my_hash_reset(&sroutines);
2099
 
  }
2100
 
  sroutines_list.empty();
2101
 
  sroutines_list_own_last= sroutines_list.next;
2102
 
  sroutines_list_own_elements= 0;
2103
 
  binlog_stmt_flags= 0;
2104
1975
}
2105
1976
 
2106
1977
 
2113
1984
 
2114
1985
void Query_tables_list::destroy_query_tables_list()
2115
1986
{
2116
 
  hash_free(&sroutines);
2117
1987
}
2118
1988
 
2119
1989
 
2121
1991
  Initialize LEX object.
2122
1992
 
2123
1993
  SYNOPSIS
2124
 
    st_lex::st_lex()
 
1994
    LEX::LEX()
2125
1995
 
2126
1996
  NOTE
2127
1997
    LEX object initialized with this constructor can be used as part of
2131
2001
    for this.
2132
2002
*/
2133
2003
 
2134
 
st_lex::st_lex()
 
2004
LEX::LEX()
2135
2005
  :result(0), yacc_yyss(0), yacc_yyvs(0),
2136
2006
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2137
2007
{
2138
2008
 
2139
 
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2140
 
                         plugins_static_buffer,
2141
 
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
2142
 
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
2143
2009
  reset_query_tables_list(true);
2144
2010
}
2145
2011
 
2146
 
 
2147
 
/*
2148
 
  Check whether the merging algorithm can be used on this VIEW
2149
 
 
2150
 
  SYNOPSIS
2151
 
    st_lex::can_be_merged()
2152
 
 
2153
 
  DESCRIPTION
2154
 
    We can apply merge algorithm if it is single SELECT view  with
2155
 
    subqueries only in WHERE clause (we do not count SELECTs of underlying
2156
 
    views, and second level subqueries) and we have not grpouping, ordering,
2157
 
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2158
 
    several underlying tables.
2159
 
 
2160
 
  RETURN
2161
 
    false - only temporary table algorithm can be used
2162
 
    true  - merge algorithm can be used
2163
 
*/
2164
 
 
2165
 
bool st_lex::can_be_merged()
2166
 
{
2167
 
  // TODO: do not forget implement case when select_lex.table_list.elements==0
2168
 
 
2169
 
  /* find non VIEW subqueries/unions */
2170
 
  bool selects_allow_merge= select_lex.next_select() == 0;
2171
 
  if (selects_allow_merge)
2172
 
  {
2173
 
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2174
 
         tmp_unit;
2175
 
         tmp_unit= tmp_unit->next_unit())
2176
 
    {
2177
 
      if (tmp_unit->first_select()->parent_lex == this &&
2178
 
          (tmp_unit->item == 0 ||
2179
 
           (tmp_unit->item->place() != IN_WHERE &&
2180
 
            tmp_unit->item->place() != IN_ON)))
2181
 
      {
2182
 
        selects_allow_merge= 0;
2183
 
        break;
2184
 
      }
2185
 
    }
2186
 
  }
2187
 
 
2188
 
  return (selects_allow_merge &&
2189
 
          select_lex.group_list.elements == 0 &&
2190
 
          select_lex.having == 0 &&
2191
 
          select_lex.with_sum_func == 0 &&
2192
 
          select_lex.table_list.elements >= 1 &&
2193
 
          !(select_lex.options & SELECT_DISTINCT) &&
2194
 
          select_lex.select_limit == 0);
2195
 
}
2196
 
 
2197
 
 
2198
 
/*
2199
 
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
2200
 
 
2201
 
  SYNOPSIS
2202
 
    st_lex::can_use_merged()
2203
 
 
2204
 
  DESCRIPTION
2205
 
    Only listed here commands can use merge algorithm in top level
2206
 
    SELECT_LEX (for subqueries will be used merge algorithm if
2207
 
    st_lex::can_not_use_merged() is not true).
2208
 
 
2209
 
  RETURN
2210
 
    false - command can't use merged VIEWs
2211
 
    true  - VIEWs with MERGE algorithms can be used
2212
 
*/
2213
 
 
2214
 
bool st_lex::can_use_merged()
2215
 
{
2216
 
  switch (sql_command)
2217
 
  {
2218
 
  case SQLCOM_SELECT:
2219
 
  case SQLCOM_CREATE_TABLE:
2220
 
  case SQLCOM_UPDATE:
2221
 
  case SQLCOM_UPDATE_MULTI:
2222
 
  case SQLCOM_DELETE:
2223
 
  case SQLCOM_DELETE_MULTI:
2224
 
  case SQLCOM_INSERT:
2225
 
  case SQLCOM_INSERT_SELECT:
2226
 
  case SQLCOM_REPLACE:
2227
 
  case SQLCOM_REPLACE_SELECT:
2228
 
  case SQLCOM_LOAD:
2229
 
    return true;
2230
 
  default:
2231
 
    return false;
2232
 
  }
2233
 
}
2234
 
 
2235
 
/*
2236
 
  Check if command can't use merged views in any part of command
2237
 
 
2238
 
  SYNOPSIS
2239
 
    st_lex::can_not_use_merged()
2240
 
 
2241
 
  DESCRIPTION
2242
 
    Temporary table algorithm will be used on all SELECT levels for queries
2243
 
    listed here (see also st_lex::can_use_merged()).
2244
 
 
2245
 
  RETURN
2246
 
    false - command can't use merged VIEWs
2247
 
    true  - VIEWs with MERGE algorithms can be used
2248
 
*/
2249
 
 
2250
 
bool st_lex::can_not_use_merged()
2251
 
{
2252
 
  switch (sql_command)
2253
 
  {
2254
 
  /*
2255
 
    SQLCOM_SHOW_FIELDS is necessary to make 
2256
 
    information schema tables working correctly with views.
2257
 
    see get_schema_tables_result function
2258
 
  */
2259
 
  case SQLCOM_SHOW_FIELDS:
2260
 
    return true;
2261
 
  default:
2262
 
    return false;
2263
 
  }
2264
 
}
2265
 
 
2266
2012
/*
2267
2013
  Detect that we need only table structure of derived table/view
2268
2014
 
2274
2020
    false no, we need data
2275
2021
*/
2276
2022
 
2277
 
bool st_lex::only_view_structure()
 
2023
bool LEX::only_view_structure()
2278
2024
{
2279
2025
  switch (sql_command) {
2280
2026
  case SQLCOM_SHOW_CREATE:
2299
2045
*/
2300
2046
 
2301
2047
 
2302
 
bool st_lex::need_correct_ident()
 
2048
bool LEX::need_correct_ident()
2303
2049
{
2304
2050
  switch(sql_command)
2305
2051
  {
2318
2064
  and will initialize the destination with the default
2319
2065
  database of the stored routine, rather than the default
2320
2066
  database of the connection it is parsed in.
2321
 
  E.g. if one has no current database selected, or current database 
 
2067
  E.g. if one has no current database selected, or current database
2322
2068
  set to 'bar' and then issues:
2323
2069
 
2324
2070
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2332
2078
*/
2333
2079
 
2334
2080
bool
2335
 
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
 
2081
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
2336
2082
{
2337
2083
  return session->copy_db_to(p_db, p_db_length);
2338
2084
}
2341
2087
  initialize limit counters
2342
2088
 
2343
2089
  SYNOPSIS
2344
 
    st_select_lex_unit::set_limit()
2345
 
    values      - SELECT_LEX with initial values for counters
 
2090
    Select_Lex_Unit::set_limit()
 
2091
    values      - Select_Lex with initial values for counters
2346
2092
*/
2347
2093
 
2348
 
void st_select_lex_unit::set_limit(st_select_lex *sl)
 
2094
void Select_Lex_Unit::set_limit(Select_Lex *sl)
2349
2095
{
2350
2096
  ha_rows select_limit_val;
2351
2097
  uint64_t val;
2352
2098
 
2353
2099
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2354
2100
  select_limit_val= (ha_rows)val;
2355
 
  /* 
 
2101
  /*
2356
2102
    Check for overflow : ha_rows can be smaller then uint64_t if
2357
2103
    BIG_TABLES is off.
2358
2104
    */
2384
2130
      In this case link_to_local is set.
2385
2131
 
2386
2132
*/
2387
 
TableList *st_lex::unlink_first_table(bool *link_to_local)
 
2133
TableList *LEX::unlink_first_table(bool *link_to_local)
2388
2134
{
2389
2135
  TableList *first;
2390
2136
  if ((first= query_tables))
2403
2149
    */
2404
2150
    if ((*link_to_local= test(select_lex.table_list.first)))
2405
2151
    {
2406
 
      select_lex.context.table_list= 
 
2152
      select_lex.context.table_list=
2407
2153
        select_lex.context.first_name_resolution_table= first->next_local;
2408
2154
      select_lex.table_list.first= (unsigned char*) (first->next_local);
2409
2155
      select_lex.table_list.elements--; //safety
2424
2170
  table list
2425
2171
 
2426
2172
  SYNOPSYS
2427
 
     st_lex::first_lists_tables_same()
 
2173
     LEX::first_lists_tables_same()
2428
2174
 
2429
2175
  NOTES
2430
2176
    In many cases (for example, usual INSERT/DELETE/...) the first table of
2431
 
    main SELECT_LEX have special meaning => check that it is the first table
 
2177
    main Select_Lex have special meaning => check that it is the first table
2432
2178
    in global list and re-link to be first in the global list if it is
2433
2179
    necessary.  We need such re-linking only for queries with sub-queries in
2434
2180
    the select list, as only in this case tables of sub-queries will go to
2435
2181
    the global list first.
2436
2182
*/
2437
2183
 
2438
 
void st_lex::first_lists_tables_same()
 
2184
void LEX::first_lists_tables_same()
2439
2185
{
2440
2186
  TableList *first_table= (TableList*) select_lex.table_list.first;
2441
2187
  if (query_tables != first_table && first_table != 0)
2471
2217
    global list
2472
2218
*/
2473
2219
 
2474
 
void st_lex::link_first_table_back(TableList *first,
 
2220
void LEX::link_first_table_back(TableList *first,
2475
2221
                                   bool link_to_local)
2476
2222
{
2477
2223
  if (first)
2498
2244
  cleanup lex for case when we open table by table for processing
2499
2245
 
2500
2246
  SYNOPSIS
2501
 
    st_lex::cleanup_after_one_table_open()
 
2247
    LEX::cleanup_after_one_table_open()
2502
2248
 
2503
2249
  NOTE
2504
2250
    This method is mostly responsible for cleaning up of selects lists and
2506
2252
    to call Query_tables_list::reset_query_tables_list(false).
2507
2253
*/
2508
2254
 
2509
 
void st_lex::cleanup_after_one_table_open()
 
2255
void LEX::cleanup_after_one_table_open()
2510
2256
{
2511
2257
  /*
2512
2258
    session->lex->derived_tables & additional units may be set if we open
2520
2266
  {
2521
2267
    derived_tables= 0;
2522
2268
    /* cleunup underlying units (units of VIEW) */
2523
 
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
 
2269
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
2524
2270
         un;
2525
2271
         un= un->next_unit())
2526
2272
      un->cleanup();
2533
2279
 
2534
2280
 
2535
2281
/*
2536
 
  Save current state of Query_tables_list for this LEX, and prepare it
2537
 
  for processing of new statemnt.
2538
 
 
2539
 
  SYNOPSIS
2540
 
    reset_n_backup_query_tables_list()
2541
 
      backup  Pointer to Query_tables_list instance to be used for backup
2542
 
*/
2543
 
 
2544
 
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2545
 
{
2546
 
}
2547
 
 
2548
 
 
2549
 
/*
2550
 
  Restore state of Query_tables_list for this LEX from backup.
2551
 
 
2552
 
  SYNOPSIS
2553
 
    restore_backup_query_tables_list()
2554
 
      backup  Pointer to Query_tables_list instance used for backup
2555
 
*/
2556
 
 
2557
 
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2558
 
{
2559
 
}
2560
 
 
2561
 
 
2562
 
/*
2563
 
  Checks for usage of routines and/or tables in a parsed statement
2564
 
 
2565
 
  SYNOPSIS
2566
 
    st_lex:table_or_sp_used()
2567
 
 
2568
 
  RETURN
2569
 
    false  No routines and tables used
2570
 
    true   Either or both routines and tables are used.
2571
 
*/
2572
 
 
2573
 
bool st_lex::table_or_sp_used()
2574
 
{
2575
 
  if (sroutines.records || query_tables)
2576
 
    return(true);
2577
 
 
2578
 
  return(false);
2579
 
}
2580
 
 
2581
 
 
2582
 
/*
2583
2282
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2584
2283
 
2585
2284
  SYNOPSIS
2609
2308
 
2610
2309
 
2611
2310
/*
2612
 
  There are st_select_lex::add_table_to_list &
2613
 
  st_select_lex::set_lock_for_tables are in sql_parse.cc
2614
 
 
2615
 
  st_select_lex::print is in sql_select.cc
2616
 
 
2617
 
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
2618
 
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
2619
 
  st_select_lex_unit::change_result
 
2311
  There are Select_Lex::add_table_to_list &
 
2312
  Select_Lex::set_lock_for_tables are in sql_parse.cc
 
2313
 
 
2314
  Select_Lex::print is in sql_select.cc
 
2315
 
 
2316
  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
 
2317
  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
 
2318
  Select_Lex_Unit::change_result
2620
2319
  are in sql_union.cc
2621
2320
*/
2622
2321
 
2630
2329
 
2631
2330
  DESCRIPTION
2632
2331
    Used in filling up the tagged hints list.
2633
 
    This list is filled by first setting the kind of the hint as a 
 
2332
    This list is filled by first setting the kind of the hint as a
2634
2333
    context variable and then adding hints of the current kind.
2635
2334
    Then the context variable index_hint_type can be reset to the
2636
2335
    next hint type.
2637
2336
*/
2638
 
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
 
2337
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg,
2639
2338
                                        index_clause_map clause)
2640
 
 
2339
{
2641
2340
  current_index_hint_type= type_arg;
2642
2341
  current_index_hint_clause= clause;
2643
2342
}
2651
2350
      session         current thread.
2652
2351
*/
2653
2352
 
2654
 
void st_select_lex::alloc_index_hints (Session *session)
2655
 
2656
 
  index_hints= new (session->mem_root) List<Index_hint>(); 
 
2353
void Select_Lex::alloc_index_hints (Session *session)
 
2354
{
 
2355
  index_hints= new (session->mem_root) List<Index_hint>();
2657
2356
}
2658
2357
 
2659
2358
 
2660
2359
 
2661
2360
/*
2662
 
  adds an element to the array storing index usage hints 
 
2361
  adds an element to the array storing index usage hints
2663
2362
  (ADD/FORCE/IGNORE INDEX).
2664
2363
 
2665
2364
  SYNOPSIS
2671
2370
  RETURN VALUE
2672
2371
    0 on success, non-zero otherwise
2673
2372
*/
2674
 
bool st_select_lex::add_index_hint (Session *session, char *str, uint32_t length)
 
2373
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
2675
2374
{
2676
 
  return index_hints->push_front (new (session->mem_root) 
 
2375
  return index_hints->push_front (new (session->mem_root)
2677
2376
                                 Index_hint(current_index_hint_type,
2678
2377
                                            current_index_hint_clause,
2679
2378
                                            str, length));