|
| 1 | +#ifndef SQL_ITERATORS_UPDATE_ROWS_ITERATOR_H_ |
| 2 | +#define SQL_ITERATORS_UPDATE_ROWS_ITERATOR_H_ |
| 3 | + |
| 4 | +/* Copyright (c) 2022, Oracle and/or its affiliates. |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License, version 2.0, |
| 8 | + as published by the Free Software Foundation. |
| 9 | +
|
| 10 | + This program is also distributed with certain software (including |
| 11 | + but not limited to OpenSSL) that is licensed under separate terms, |
| 12 | + as designated in a particular file or component or in included license |
| 13 | + documentation. The authors of MySQL hereby grant you an additional |
| 14 | + permission to link the program and your derivative works with the |
| 15 | + separately licensed software that they have included with MySQL. |
| 16 | +
|
| 17 | + This program is distributed in the hope that it will be useful, |
| 18 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + GNU General Public License, version 2.0, for more details. |
| 21 | +
|
| 22 | + You should have received a copy of the GNU General Public License |
| 23 | + along with this program; if not, write to the Free Software |
| 24 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 25 | + |
| 26 | +#include <cassert> |
| 27 | +#include <memory> |
| 28 | + |
| 29 | +#include "my_alloc.h" |
| 30 | +#include "my_base.h" |
| 31 | +#include "sql/iterators/row_iterator.h" |
| 32 | +#include "sql/sql_list.h" |
| 33 | + |
| 34 | +class COPY_INFO; |
| 35 | +class Copy_field; |
| 36 | +class Item; |
| 37 | +class THD; |
| 38 | +struct TABLE; |
| 39 | +struct TABLE_LIST; |
| 40 | +template <class T> |
| 41 | +class mem_root_deque; |
| 42 | + |
| 43 | +/// An iterator that performs updates to rows returned by its child iterator. |
| 44 | +class UpdateRowsIterator final : public RowIterator { |
| 45 | + public: |
| 46 | + UpdateRowsIterator(THD *thd, unique_ptr_destroy_only<RowIterator> source, |
| 47 | + TABLE *outermost_table, TABLE *immediate_table, |
| 48 | + TABLE_LIST *update_tables, TABLE **tmp_tables, |
| 49 | + Copy_field *copy_fields, |
| 50 | + List<TABLE> unupdated_check_opt_tables, |
| 51 | + COPY_INFO **update_operations, |
| 52 | + mem_root_deque<Item *> **fields_for_table, |
| 53 | + mem_root_deque<Item *> **values_for_table); |
| 54 | + ~UpdateRowsIterator() override = default; |
| 55 | + bool Init() override; |
| 56 | + int Read() override; |
| 57 | + void StartPSIBatchMode() override { m_source->StartPSIBatchMode(); } |
| 58 | + void EndPSIBatchModeIfStarted() override { |
| 59 | + m_source->EndPSIBatchModeIfStarted(); |
| 60 | + } |
| 61 | + void SetNullRowFlag([[maybe_unused]] bool is_null_row) override { |
| 62 | + assert(false); |
| 63 | + } |
| 64 | + void UnlockRow() override { assert(false); } |
| 65 | + ha_rows found_rows() const { return m_found_rows; } |
| 66 | + ha_rows updated_rows() const { return m_updated_rows; } |
| 67 | + |
| 68 | + private: |
| 69 | + /// The iterator producing the rows to update. |
| 70 | + unique_ptr_destroy_only<RowIterator> m_source; |
| 71 | + /// The outermost table of the join. It may or may not be one of the tables |
| 72 | + /// being updated. |
| 73 | + TABLE *m_outermost_table; |
| 74 | + /// The table to perform immediate update on, or nullptr if immediate update |
| 75 | + /// is not possible. |
| 76 | + TABLE *m_immediate_table; |
| 77 | + /// Pointer to list of updated tables, linked via 'next_local'. |
| 78 | + TABLE_LIST *m_update_tables; |
| 79 | + /// Temporary tables used to store cached updates. |
| 80 | + TABLE **m_tmp_tables; |
| 81 | + /// Objects that copy the updated values from a temporary table to the update |
| 82 | + /// target table, and perform conversions if the types differ. |
| 83 | + Copy_field *m_copy_fields; |
| 84 | + /// Tables referenced in the CHECK OPTION condition of the updated view |
| 85 | + /// excluding the updated table. |
| 86 | + List<TABLE> m_unupdated_check_opt_tables; |
| 87 | + /// The update operations of each table in m_update_tables (indexed in the |
| 88 | + /// same order as m_update_tables). |
| 89 | + COPY_INFO **m_update_operations; |
| 90 | + /// The fields list decomposed into separate lists per table. |
| 91 | + mem_root_deque<Item *> **m_fields_for_table; |
| 92 | + /// The values list decomposed into separate lists per table. |
| 93 | + mem_root_deque<Item *> **m_values_for_table; |
| 94 | + /// The number of rows matching the WHERE and join conditions. |
| 95 | + ha_rows m_found_rows{0}; |
| 96 | + /// The number of rows actually updated. |
| 97 | + ha_rows m_updated_rows{0}; |
| 98 | + |
| 99 | + /// Perform all the immediate updates for the current row returned by the |
| 100 | + /// join, and buffer row IDs for the non-immediate tables. |
| 101 | + /// |
| 102 | + /// @param[out] trans_safe Gets set to false if a non-transactional table |
| 103 | + /// is updated. |
| 104 | + /// @param[out] transactional_tables Gets set to true if a transactional |
| 105 | + /// table is updated. |
| 106 | + /// @return True on error. |
| 107 | + bool DoImmediateUpdatesAndBufferRowIds(bool *trans_safe, |
| 108 | + bool *transactional_tables); |
| 109 | + |
| 110 | + /// Perform all the delayed updates. |
| 111 | + /// |
| 112 | + /// @param[in,out] trans_safe Gets set to false if a non-transactional table |
| 113 | + /// is updated. |
| 114 | + /// @param[out] transactional_tables Gets set to true if a transactional |
| 115 | + /// table is updated. |
| 116 | + /// @return True on error. |
| 117 | + bool DoDelayedUpdates(bool *trans_safe, bool *transactional_tables); |
| 118 | +}; |
| 119 | + |
| 120 | +#endif // SQL_ITERATORS_UPDATE_ROWS_ITERATOR_H_ |
0 commit comments