Debian Codegraph @

unittest/gunit/fake_string_iterator.h

raw symbols SCIP
1 #ifndef UNITTEST_GUNIT_FAKE_STRING_ITERATOR_H_
2 #define UNITTEST_GUNIT_FAKE_STRING_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 "m_ctype.h"
33 #include "sql/field.h"
34 #include "sql/iterators/row_iterator.h"
35 #include "sql/sql_class.h"
36
37 // An implementation of a RowIterator that contains a user defined set of
38 // string values, without having to set up the entire SQL machinery. It works
39 // just like any other iterator; you call Init() before doing any reads, and you
40 // call Read() until it returns -1 (no more data).
41 //
42 // The iterator is expected to only work on a single-column table, and the
43 // column must be given to the iterator constructor.
44 class FakeStringIterator final : public TableRowIterator {
45 public:
46 FakeStringIterator(THD *thd, TABLE *table, Field_varstring *field,
47 std::vector<std::optional<std::string>> dataset)
48 : TableRowIterator(thd, table),
49 m_field(field),
50 m_dataset(std::move(dataset)) {}
51
52 bool Init() override {
53 m_current_index = 0;
54 return false;
55 }
56
57 int Read() override {
58 if (m_current_index == m_dataset.size()) {
59 return -1;
60 }
61
62 const std::optional<std::string> &value = m_dataset[m_current_index++];
63 if (value.has_value()) {
64 m_field->store(value->c_str(), value->size(),
65 &my_charset_utf8mb4_0900_ai_ci);
66 m_field->set_notnull();
67 } else {
68 m_field->set_null();
69 }
70
71 return 0;
72 }
73
74 private:
75 size_t m_current_index{0};
76 Field_varstring *m_field;
77 std::vector<std::optional<std::string>> m_dataset;
78 };
79
80 #endif // UNITTEST_GUNIT_FAKE_STRING_ITERATOR_H_