You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.7 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  1. /* Copyright (C) 2000-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Buffers to save and compare item values */
  13. #include "mysql_priv.h"
  14. /*
  15. ** Create right type of Cached_item for an item
  16. */
  17. Cached_item *new_Cached_item(THD *thd, Item *item)
  18. {
  19. if (item->real_item()->type() == Item::FIELD_ITEM &&
  20. !(((Item_field *) (item->real_item()))->field->flags & BLOB_FLAG))
  21. return new Cached_item_field((Item_field *) (item->real_item()));
  22. switch (item->result_type()) {
  23. case STRING_RESULT:
  24. return new Cached_item_str(thd, (Item_field *) item);
  25. case INT_RESULT:
  26. return new Cached_item_int((Item_field *) item);
  27. case REAL_RESULT:
  28. return new Cached_item_real(item);
  29. case DECIMAL_RESULT:
  30. return new Cached_item_decimal(item);
  31. case ROW_RESULT:
  32. default:
  33. DBUG_ASSERT(0);
  34. return 0;
  35. }
  36. }
  37. Cached_item::~Cached_item() {}
  38. /*
  39. ** Compare with old value and replace value with new value
  40. ** Return true if values have changed
  41. */
  42. Cached_item_str::Cached_item_str(THD *thd, Item *arg)
  43. :item(arg), value(min(arg->max_length, thd->variables.max_sort_length))
  44. {}
  45. bool Cached_item_str::cmp(void)
  46. {
  47. String *res;
  48. bool tmp;
  49. if ((res=item->val_str(&tmp_value)))
  50. res->length(min(res->length(), value.alloced_length()));
  51. if (null_value != item->null_value)
  52. {
  53. if ((null_value= item->null_value))
  54. return TRUE; // New value was null
  55. tmp=TRUE;
  56. }
  57. else if (null_value)
  58. return 0; // new and old value was null
  59. else
  60. tmp= sortcmp(&value,res,item->collation.collation) != 0;
  61. if (tmp)
  62. value.copy(*res); // Remember for next cmp
  63. return tmp;
  64. }
  65. Cached_item_str::~Cached_item_str()
  66. {
  67. item=0; // Safety
  68. }
  69. bool Cached_item_real::cmp(void)
  70. {
  71. double nr= item->val_real();
  72. if (null_value != item->null_value || nr != value)
  73. {
  74. null_value= item->null_value;
  75. value=nr;
  76. return TRUE;
  77. }
  78. return FALSE;
  79. }
  80. bool Cached_item_int::cmp(void)
  81. {
  82. longlong nr=item->val_int();
  83. if (null_value != item->null_value || nr != value)
  84. {
  85. null_value= item->null_value;
  86. value=nr;
  87. return TRUE;
  88. }
  89. return FALSE;
  90. }
  91. bool Cached_item_field::cmp(void)
  92. {
  93. bool tmp= field->cmp(buff) != 0; // This is not a blob!
  94. if (tmp)
  95. field->get_image(buff,length,field->charset());
  96. if (null_value != field->is_null())
  97. {
  98. null_value= !null_value;
  99. tmp=TRUE;
  100. }
  101. return tmp;
  102. }
  103. Cached_item_decimal::Cached_item_decimal(Item *it)
  104. :item(it)
  105. {
  106. my_decimal_set_zero(&value);
  107. }
  108. bool Cached_item_decimal::cmp()
  109. {
  110. my_decimal tmp;
  111. my_decimal *ptmp= item->val_decimal(&tmp);
  112. if (null_value != item->null_value ||
  113. (!item->null_value && my_decimal_cmp(&value, ptmp)))
  114. {
  115. null_value= item->null_value;
  116. /* Save only not null values */
  117. if (!null_value)
  118. {
  119. my_decimal2decimal(ptmp, &value);
  120. return TRUE;
  121. }
  122. return FALSE;
  123. }
  124. return FALSE;
  125. }
  126. /*****************************************************************************
  127. ** Instansiate templates
  128. *****************************************************************************/
  129. #ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
  130. template class List<Cached_item>;
  131. template class List_iterator<Cached_item>;
  132. #endif