| 1 | /* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #ifndef MOCK_FIELD_VARSTRING_INCLUDED |
| 25 | #define MOCK_FIELD_VARSTRING_INCLUDED |
| 26 | |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "sql/field.h" |
| 30 | |
| 31 | /** Helper to simplify creating fields. */ |
| 32 | class Mock_field_varstring : public Field_varstring { |
| 33 | public: |
| 34 | /** |
| 35 | Creates a column. |
| 36 | @param share TABLE_SHARE this field belongs to. |
| 37 | @param name The column name. |
| 38 | @param char_len Length in chars |
| 39 | @param is_nullable Whether it's nullable. |
| 40 | */ |
| 41 | Mock_field_varstring(TABLE_SHARE *share, const char *name, uint char_len, |
| 42 | bool is_nullable) |
| 43 | : Field_varstring(nullptr, // ptr_arg |
| 44 | calc_len_arg(char_len), // len_arg |
| 45 | calc_length_bytes_arg(char_len), // length_bytes_arg |
| 46 | is_nullable ? buffer : nullptr, // null_ptr_arg |
| 47 | is_nullable ? 1 : 0, // null_bit_arg |
| 48 | Field::NONE, // auto_flags_arg |
| 49 | name, // field_name_arg |
| 50 | share, // share |
| 51 | &my_charset_utf8mb4_general_ci) // cs |
| 52 | { |
| 53 | ptr = buffer + 1; |
| 54 | std::memset(buffer, 0, MAX_FIELD_VARCHARLENGTH + 1); |
| 55 | |
| 56 | static const char *table_name_buf = "table_name"; |
| 57 | table_name = &table_name_buf; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | uchar buffer[MAX_FIELD_VARCHARLENGTH + 1]; |
| 62 | |
| 63 | static uint calc_byte_len(uint char_len) { |
| 64 | return char_len * my_charset_utf8mb4_general_ci.mbmaxlen; |
| 65 | } |
| 66 | |
| 67 | static uint32 calc_len_arg(uint char_len) { |
| 68 | return calc_byte_len(char_len) + calc_length_bytes_arg(char_len); |
| 69 | } |
| 70 | |
| 71 | static uint calc_length_bytes_arg(uint char_len) { |
| 72 | return (calc_byte_len(char_len) < 256) ? 1 : 2; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | #endif // MOCK_FIELD_VARSTRING_INCLUDED |