| 1 | #ifndef UNITTEST_GUNIT_FAKE_INTEGER_ITERATOR_H_ |
| 2 | #define UNITTEST_GUNIT_FAKE_INTEGER_ITERATOR_H_ |
| 3 | |
| 4 | /* Copyright (c) 2019, 2026, 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 designed to work 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 either included with |
| 16 | the program or referenced in the documentation. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License, version 2.0, for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 26 | |
| 27 | #include <optional> |
| 28 | #include <random> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "sql/field.h" |
| 33 | #include "sql/iterators/row_iterator.h" |
| 34 | #include "sql/sql_class.h" |
| 35 | |
| 36 | // An implementation of a RowIterator that contains a user-defined set of |
| 37 | // integer values, without having to set up the entire SQL machinery. It works |
| 38 | // just like any other iterator; you call Init() before doing any reads, and you |
| 39 | // call Read() until it returns -1 (no more data). |
| 40 | // |
| 41 | // The iterator is expected to only work on a single-column table, and the |
| 42 | // column must be given to the iterator constructor. |
| 43 | class FakeIntegerIterator final : public TableRowIterator { |
| 44 | public: |
| 45 | FakeIntegerIterator(THD *thd, TABLE *table, Field_long *field, |
| 46 | std::vector<std::optional<int>> dataset) |
| 47 | : TableRowIterator(thd, table), |
| 48 | m_field(field), |
| 49 | m_dataset(std::move(dataset)) {} |
| 50 | |
| 51 | bool Init() override { |
| 52 | m_current_index = 0; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | int Read() override { |
| 57 | ++m_num_read_calls; |
| 58 | if (m_current_index == m_dataset.size()) { |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | const std::optional<int> value = m_dataset[m_current_index++]; |
| 63 | if (value.has_value()) { |
| 64 | m_field->store(*value, /*unsigned_val=*/false); |
| 65 | m_field->set_notnull(); |
| 66 | } else { |
| 67 | m_field->set_null(); |
| 68 | } |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int num_read_calls() const { return m_num_read_calls; } |
| 74 | |
| 75 | private: |
| 76 | size_t m_current_index{0}; |
| 77 | int m_num_read_calls{0}; |
| 78 | Field_long *m_field; |
| 79 | std::vector<std::optional<int>> m_dataset; |
| 80 | }; |
| 81 | |
| 82 | #endif // UNITTEST_GUNIT_FAKE_INTEGER_ITERATOR_H_ |