~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_func.cc

Merged in recent devel tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
 
138
138
void Item_func::signal_divide_by_null()
139
139
{
140
 
  THD *thd= current_thd;
141
 
  push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
140
  Session *session= current_session;
 
141
  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
142
142
  null_value= 1;
143
143
}
144
144
 
145
145
 
146
 
Item *Item_func::get_tmp_table_item(THD *thd)
 
146
Item *Item_func::get_tmp_table_item(Session *session)
147
147
{
148
148
  if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
149
149
    return new Item_field(result_field);
150
 
  return copy_or_same(thd);
 
150
  return copy_or_same(session);
151
151
}
152
152
 
153
153
// Shift-functions, same as << and >> in C/C++
512
512
  bool locked;
513
513
  pthread_cond_t cond;
514
514
  my_thread_id thread_id;
515
 
  void set_thread(THD *thd) { thread_id= thd->thread_id; }
 
515
  void set_thread(Session *session) { thread_id= session->thread_id; }
516
516
 
517
517
  User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id) 
518
518
    :key_length(length),count(1),locked(1), thread_id(id)
589
589
int64_t Item_master_pos_wait::val_int()
590
590
{
591
591
  assert(fixed == 1);
592
 
  THD* thd = current_thd;
 
592
  Session* session = current_session;
593
593
  String *log_name = args[0]->val_str(&value);
594
594
  int event_count= 0;
595
595
 
596
596
  null_value=0;
597
 
  if (thd->slave_thread || !log_name || !log_name->length())
 
597
  if (session->slave_thread || !log_name || !log_name->length())
598
598
  {
599
599
    null_value = 1;
600
600
    return 0;
601
601
  }
602
602
  int64_t pos = (ulong)args[1]->val_int();
603
603
  int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
604
 
  if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
 
604
  if ((event_count = active_mi->rli.wait_for_pos(session, log_name, pos, timeout)) == -2)
605
605
  {
606
606
    null_value = 1;
607
607
    event_count=0;
619
619
 
620
620
int64_t Item_func_last_insert_id::val_int()
621
621
{
622
 
  THD *thd= current_thd;
 
622
  Session *session= current_session;
623
623
  assert(fixed == 1);
624
624
  if (arg_count)
625
625
  {
632
632
      LAST_INSERT_ID(X) take precedence over an generated auto_increment
633
633
      value for this row.
634
634
    */
635
 
    thd->arg_of_last_insert_id_function= true;
636
 
    thd->first_successful_insert_id_in_prev_stmt= value;
 
635
    session->arg_of_last_insert_id_function= true;
 
636
    session->first_successful_insert_id_in_prev_stmt= value;
637
637
    return value;
638
638
  }
639
 
  return thd->read_first_successful_insert_id_in_prev_stmt();
 
639
  return session->read_first_successful_insert_id_in_prev_stmt();
640
640
}
641
641
 
642
642
 
643
 
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
 
643
bool Item_func_last_insert_id::fix_fields(Session *session, Item **ref)
644
644
{
645
 
  return Item_int_func::fix_fields(thd, ref);
 
645
  return Item_int_func::fix_fields(session, ref);
646
646
}
647
647
 
648
648
 
654
654
  char buff[MAX_FIELD_WIDTH];
655
655
  String tmp(buff,sizeof(buff), &my_charset_bin);
656
656
  my_decimal tmp_decimal;
657
 
  THD *thd=current_thd;
 
657
  Session *session=current_session;
658
658
  uint64_t loop_count;
659
659
 
660
660
  loop_count= (uint64_t) args[0]->val_int();
666
666
    {
667
667
      char buff[22];
668
668
      llstr(((int64_t) loop_count), buff);
669
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
669
      push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
670
670
                          ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
671
671
                          "count", buff, "benchmark");
672
672
    }
676
676
  }
677
677
 
678
678
  null_value=0;
679
 
  for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
 
679
  for (uint64_t loop=0 ; loop < loop_count && !session->killed; loop++)
680
680
  {
681
681
    switch (args[1]->result_type()) {
682
682
    case REAL_RESULT:
745
745
      the variable as "already logged" (line below) so that it won't be logged
746
746
      by Item_func_get_user_var (because that's not necessary).
747
747
    */
748
 
    entry->used_query_id=current_thd->query_id;
 
748
    entry->used_query_id=current_session->query_id;
749
749
    entry->type=STRING_RESULT;
750
750
    memcpy(entry->name.str, name.str, name.length+1);
751
751
    if (my_hash_insert(hash,(unsigned char*) entry))
762
762
  SELECT @a:= ).
763
763
*/
764
764
 
765
 
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
 
765
bool Item_func_set_user_var::fix_fields(Session *session, Item **ref)
766
766
{
767
767
  assert(fixed == 0);
768
768
  /* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
769
 
  if (Item_func::fix_fields(thd, ref) ||
770
 
      !(entry= get_variable(&thd->user_vars, name, 1)))
 
769
  if (Item_func::fix_fields(session, ref) ||
 
770
      !(entry= get_variable(&session->user_vars, name, 1)))
771
771
    return true;
772
772
  /* 
773
773
     Remember the last query which updated it, this way a query can later know
774
774
     if this variable is a constant item in the query (it is if update_query_id
775
775
     is different from query_id).
776
776
  */
777
 
  entry->update_query_id= thd->query_id;
 
777
  entry->update_query_id= session->query_id;
778
778
  /*
779
779
    As it is wrong and confusing to associate any 
780
780
    character set with NULL, @a should be latin2
1076
1076
 
1077
1077
  @note
1078
1078
    For now it always return OK. All problem with value evaluating
1079
 
    will be caught by thd->is_error() check in sql_set_variables().
 
1079
    will be caught by session->is_error() check in sql_set_variables().
1080
1080
 
1081
1081
  @retval
1082
1082
    false OK.
1447
1447
  use into the binary log.
1448
1448
 
1449
1449
  When a user variable is invoked from an update query (INSERT, UPDATE etc),
1450
 
  stores this variable and its value in thd->user_var_events, so that it can be
 
1450
  stores this variable and its value in session->user_var_events, so that it can be
1451
1451
  written to the binlog (will be written just before the query is written, see
1452
1452
  log.cc).
1453
1453
 
1454
 
  @param      thd        Current thread
 
1454
  @param      session        Current thread
1455
1455
  @param      name       Variable name
1456
1456
  @param[out] out_entry  variable structure or NULL. The pointer is set
1457
1457
                         regardless of whether function succeeded or not.
1463
1463
 
1464
1464
*/
1465
1465
 
1466
 
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
 
1466
int get_var_with_binlog(Session *session, enum_sql_command sql_command,
1467
1467
                        LEX_STRING &name, user_var_entry **out_entry)
1468
1468
{
1469
1469
  BINLOG_USER_VAR_EVENT *user_var_event;
1470
1470
  user_var_entry *var_entry;
1471
 
  var_entry= get_variable(&thd->user_vars, name, 0);
 
1471
  var_entry= get_variable(&session->user_vars, name, 0);
1472
1472
 
1473
1473
  /*
1474
1474
    Any reference to user-defined variable which is done from stored
1502
1502
    */
1503
1503
 
1504
1504
    List<set_var_base> tmp_var_list;
1505
 
    LEX *sav_lex= thd->lex, lex_tmp;
1506
 
    thd->lex= &lex_tmp;
1507
 
    lex_start(thd);
 
1505
    LEX *sav_lex= session->lex, lex_tmp;
 
1506
    session->lex= &lex_tmp;
 
1507
    lex_start(session);
1508
1508
    tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
1509
1509
                                                                       new Item_null())));
1510
1510
    /* Create the variable */
1511
 
    if (sql_set_variables(thd, &tmp_var_list))
 
1511
    if (sql_set_variables(session, &tmp_var_list))
1512
1512
    {
1513
 
      thd->lex= sav_lex;
 
1513
      session->lex= sav_lex;
1514
1514
      goto err;
1515
1515
    }
1516
 
    thd->lex= sav_lex;
1517
 
    if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
 
1516
    session->lex= sav_lex;
 
1517
    if (!(var_entry= get_variable(&session->user_vars, name, 0)))
1518
1518
      goto err;
1519
1519
  }
1520
 
  else if (var_entry->used_query_id == thd->query_id ||
1521
 
           mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
 
1520
  else if (var_entry->used_query_id == session->query_id ||
 
1521
           mysql_bin_log.is_query_in_union(session, var_entry->used_query_id))
1522
1522
  {
1523
1523
    /* 
1524
1524
       If this variable was already stored in user_var_events by this query
1544
1544
  */
1545
1545
  size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
1546
1546
  if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
1547
 
        alloc_root(thd->user_var_events_alloc, size)))
 
1547
        alloc_root(session->user_var_events_alloc, size)))
1548
1548
    goto err;
1549
1549
 
1550
1550
  user_var_event->value= (char*) user_var_event +
1565
1565
           var_entry->length);
1566
1566
  }
1567
1567
  /* Mark that this variable has been used by this query */
1568
 
  var_entry->used_query_id= thd->query_id;
1569
 
  if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
 
1568
  var_entry->used_query_id= session->query_id;
 
1569
  if (insert_dynamic(&session->user_var_events, (unsigned char*) &user_var_event))
1570
1570
    goto err;
1571
1571
 
1572
1572
  *out_entry= var_entry;
1579
1579
 
1580
1580
void Item_func_get_user_var::fix_length_and_dec()
1581
1581
{
1582
 
  THD *thd=current_thd;
 
1582
  Session *session=current_session;
1583
1583
  int error;
1584
1584
  maybe_null=1;
1585
1585
  decimals=NOT_FIXED_DEC;
1586
1586
  max_length=MAX_BLOB_WIDTH;
1587
1587
 
1588
 
  error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
 
1588
  error= get_var_with_binlog(session, session->lex->sql_command, name, &var_entry);
1589
1589
 
1590
1590
  /*
1591
1591
    If the variable didn't exist it has been created as a STRING-type.
1632
1632
 
1633
1633
bool Item_func_get_user_var::const_item() const
1634
1634
{
1635
 
  return (!var_entry || current_thd->query_id != var_entry->update_query_id);
 
1635
  return (!var_entry || current_session->query_id != var_entry->update_query_id);
1636
1636
}
1637
1637
 
1638
1638
 
1667
1667
}
1668
1668
 
1669
1669
 
1670
 
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
 
1670
bool Item_user_var_as_out_param::fix_fields(Session *session, Item **ref)
1671
1671
{
1672
1672
  assert(fixed == 0);
1673
 
  if (Item::fix_fields(thd, ref) ||
1674
 
      !(entry= get_variable(&thd->user_vars, name, 1)))
 
1673
  if (Item::fix_fields(session, ref) ||
 
1674
      !(entry= get_variable(&session->user_vars, name, 1)))
1675
1675
    return true;
1676
1676
  entry->type= STRING_RESULT;
1677
1677
  /*
1679
1679
    of fields in LOAD DATA INFILE.
1680
1680
    (Since Item_user_var_as_out_param is used only there).
1681
1681
  */
1682
 
  entry->collation.set(thd->variables.collation_database);
1683
 
  entry->update_query_id= thd->query_id;
 
1682
  entry->collation.set(session->variables.collation_database);
 
1683
  entry->update_query_id= session->query_id;
1684
1684
  return false;
1685
1685
}
1686
1686
 
1748
1748
 
1749
1749
 
1750
1750
bool
1751
 
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
 
1751
Item_func_get_system_var::fix_fields(Session *session, Item **ref)
1752
1752
{
1753
1753
  Item *item;
1754
1754
 
1757
1757
    instead of this item. If the variable can not be evaluated,
1758
1758
    the error is reported in sys_var::item().
1759
1759
  */
1760
 
  if (!(item= var->item(thd, var_type, &component)))
 
1760
  if (!(item= var->item(session, var_type, &component)))
1761
1761
    return(1);                             // Impossible
1762
1762
  item->set_name(name, 0, system_charset_info); // don't allocate a new name
1763
 
  thd->change_item_tree(ref, item);
 
1763
  session->change_item_tree(ref, item);
1764
1764
 
1765
1765
  return(0);
1766
1766
}
1789
1789
/**
1790
1790
  Return value of an system variable base[.name] as a constant item.
1791
1791
 
1792
 
  @param thd                    Thread handler
 
1792
  @param session                        Thread handler
1793
1793
  @param var_type               global / session
1794
1794
  @param name                   Name of base or system variable
1795
1795
  @param component              Component.
1803
1803
*/
1804
1804
 
1805
1805
 
1806
 
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
 
1806
Item *get_system_var(Session *session, enum_var_type var_type, LEX_STRING name,
1807
1807
                     LEX_STRING component)
1808
1808
{
1809
1809
  sys_var *var;
1820
1820
    component_name= &component;                 // Empty string
1821
1821
  }
1822
1822
 
1823
 
  if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
 
1823
  if (!(var= find_sys_var(session, base_name->str, base_name->length)))
1824
1824
    return 0;
1825
1825
  if (component.str)
1826
1826
  {
1896
1896
int64_t Item_func_row_count::val_int()
1897
1897
{
1898
1898
  assert(fixed == 1);
1899
 
  THD *thd= current_thd;
 
1899
  Session *session= current_session;
1900
1900
 
1901
 
  return thd->row_count_func;
 
1901
  return session->row_count_func;
1902
1902
}
1903
1903
 
1904
1904
int64_t Item_func_found_rows::val_int()
1905
1905
{
1906
1906
  assert(fixed == 1);
1907
 
  THD *thd= current_thd;
 
1907
  Session *session= current_session;
1908
1908
 
1909
 
  return thd->found_rows();
 
1909
  return session->found_rows();
1910
1910
}