Debian Codegraph @

unittest/gunit/dd.h

raw symbols SCIP
1 /* Copyright (c) 2014, 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 DD_INCLUDED
25 #define DD_INCLUDED
26
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29
30 #include "sql/dd/types/charset.h"
31 #include "sql/dd/types/collation.h"
32 #include "sql/dd/types/column.h"
33 #include "sql/dd/types/column_statistics.h"
34 #include "sql/dd/types/column_type_element.h"
35 #include "sql/dd/types/event.h"
36 #include "sql/dd/types/foreign_key.h"
37 #include "sql/dd/types/foreign_key_element.h"
38 #include "sql/dd/types/index.h"
39 #include "sql/dd/types/index_element.h"
40 #include "sql/dd/types/parameter.h"
41 #include "sql/dd/types/partition.h"
42 #include "sql/dd/types/partition_index.h"
43 #include "sql/dd/types/partition_value.h"
44 #include "sql/dd/types/procedure.h"
45 #include "sql/dd/types/schema.h"
46 #include "sql/dd/types/table.h"
47 #include "sql/dd/types/tablespace.h"
48 #include "sql/dd/types/tablespace_file.h"
49 #include "sql/dd/types/trigger.h"
50 #include "sql/dd/types/view.h"
51 #include "sql/dd/types/view_table.h"
52 #include "sql/histograms/histogram.h"
53 #include "sql/histograms/value_map.h"
54 #include "sql/sql_class.h"
55 #include "unittest/gunit/base_mock_field.h"
56 #include "unittest/gunit/base_mock_handler.h"
57 #include "unittest/gunit/fake_table.h"
58
59 class Json_wrapper;
60
61 namespace dd_unittest {
62
63 using ::testing::_;
64 using ::testing::NiceMock;
65 using ::testing::Return;
66 using ::testing::StrictMock;
67
68 /**
69 Mock handler for dictionary operations.
70 */
71 class Mock_dd_HANDLER : public Base_mock_HANDLER {
72 public:
73 // Mock method used indirectly by find_record
74 MOCK_METHOD4(index_read_map, int(::uchar *, const ::uchar *, key_part_map,
75 enum ha_rkey_function));
76
77 // Handler method used for inserts
78 MOCK_METHOD1(write_row, int(::uchar *));
79
80 // Handler method used for updates
81 MOCK_METHOD2(update_row, int(const ::uchar *, ::uchar *));
82
83 Mock_dd_HANDLER(handlerton *hton, TABLE_SHARE *share)
84 : Base_mock_HANDLER(hton, share) {}
85
86 virtual ~Mock_dd_HANDLER() = default;
87 };
88
89 /**
90 Mock class for Field_longlong. Mock store and val_int, provide fake
91 storage to set contents to be returned or to capture values stored.
92 */
93 class Mock_dd_field_longlong : public Base_mock_field_longlong {
94 longlong m_fake_val;
95 bool m_val_is_unsigned;
96
97 public:
98 // Avoid warning about hiding other overloaded versions of store().
99 using Field_longlong::store;
100
101 /*
102 Mock the store and val_int methods.
103 Note: Sun Studio needs a little help in resolving longlong.
104 */
105 MOCK_METHOD2(store, type_conversion_status(::longlong, bool));
106 MOCK_CONST_METHOD0(val_int, ::longlong(void));
107 MOCK_METHOD0(val_uint, ::ulonglong(void));
108
109 /*
110 Add fake methods to set and get expected contents.
111 */
112 type_conversion_status fake_store(longlong val, bool unsigned_val) {
113 m_val_is_unsigned = unsigned_val;
114 m_fake_val = val;
115 return TYPE_OK;
116 }
117
118 longlong fake_val_int() { return m_fake_val; }
119
120 ulonglong fake_val_uint() { return static_cast<ulonglong>(m_fake_val); }
121
122 Mock_dd_field_longlong() : Base_mock_field_longlong() {}
123
124 virtual ~Mock_dd_field_longlong() = default;
125 };
126
127 /**
128 Mock class for Field_varstring. Mock store and val_str, provide fake
129 storage to set contents to be returned or to capture values stored.
130 */
131 class Mock_dd_field_varstring : public Base_mock_field_varstring {
132 const char *m_fake_val;
133
134 public:
135 // Avoid warning about hiding other overloaded versions of store().
136 using Field_varstring::store;
137
138 /*
139 Mock the store and var_str methods.
140 */
141 MOCK_METHOD3(store, type_conversion_status(const char *, size_t length,
142 const CHARSET_INFO *));
143 MOCK_CONST_METHOD2(val_str, String *(String *, String *));
144
145 /*
146 Add fake methods to set and get expected contents.
147 */
148 type_conversion_status fake_store(const char *str, size_t,
149 const CHARSET_INFO *) {
150 m_fake_val = str;
151 return TYPE_OK;
152 }
153
154 String *fake_val_str(String *, String *str) {
155 str->set((const char *)m_fake_val, strlen(m_fake_val), &my_charset_latin1);
156 return str;
157 }
158
159 const char *fake_val_c_str() { return m_fake_val; }
160
161 Mock_dd_field_varstring(uint32 length, TABLE_SHARE *share)
162 : Base_mock_field_varstring(length, share) {}
163
164 virtual ~Mock_dd_field_varstring() = default;
165 };
166
167 /**
168 Create a Fake_TABLE object representing the schemata table.
169 */
170 inline Fake_TABLE *get_schema_table(THD *thd, handlerton *hton) {
171 List<Field> m_field_list;
172 Fake_TABLE *table = nullptr;
173 Fake_TABLE_SHARE dummy_share(1); // Keep Field_varstring constructor happy.
174
175 // Add fields
176 m_field_list.push_back(new (thd->mem_root) Mock_dd_field_longlong()); // id
177 m_field_list.push_back(new (thd->mem_root)
178 Mock_dd_field_longlong()); // catalog_id
179 m_field_list.push_back(
180 new (thd->mem_root) Mock_dd_field_varstring(64, &dummy_share)); // name
181 m_field_list.push_back(new (thd->mem_root)
182 Mock_dd_field_longlong()); // collation_id
183 m_field_list.push_back(new (thd->mem_root)
184 Mock_dd_field_longlong()); // created
185 m_field_list.push_back(new (thd->mem_root)
186 Mock_dd_field_longlong()); // last_altered
187 m_field_list.push_back(new (*THR_MALLOC) // options
188 Mock_dd_field_varstring(128, &dummy_share));
189 m_field_list.push_back(new (*THR_MALLOC)
190 Mock_dd_field_longlong()); // default_encryption
191
192 // Create table object (and table share implicitly).
193 table = new Fake_TABLE(m_field_list);
194
195 // Create a strict mock handler for the share.
196 StrictMock<Mock_dd_HANDLER> *ha =
197 new (thd->mem_root) StrictMock<Mock_dd_HANDLER>(hton, table->s);
198
199 // Set current open table.
200 ha->change_table_ptr(table, table->s);
201
202 // Assign handler and thd.
203 table->set_handler(ha);
204 table->in_use = thd;
205
206 // Setup fake key.
207 table->key_info[0].key_length = 1;
208 table->key_info[0].key_part[0].field = table->field[0];
209 table->key_info[0].key_part[0].length = 1;
210 table->key_info[0].key_part[0].null_offset = 0;
211
212 table->key_info[1].key_length = 64;
213 table->key_info[1].key_part[0].field = table->field[1];
214 table->key_info[1].key_part[0].length = 64;
215 table->key_info[1].key_part[0].null_offset = 0;
216
217 // Setup the fake share to avoid asserts failing in the handler API.
218 table->s->varchar_fields = 1;
219 table->s->cached_row_logging_check = 0;
220 table->s->reclength = 512;
221 table->s->default_values = new uchar[table->s->reclength];
222 table->s->tmp_table = NON_TRANSACTIONAL_TMP_TABLE;
223
224 // Allocate dummy record[1] to avoid failures in the handler functions.
225 table->record[1] = new uchar[table->s->reclength];
226
227 return table;
228 }
229
230 // Overloaded functions for populating DD objects
231
232 inline void set_attributes(dd::Schema *obj, const dd::String_type &name) {
233 obj->set_name(name);
234 obj->set_default_collation_id(1);
235 }
236
237 inline void set_attributes(dd::Tablespace *obj, const dd::String_type &name) {
238 //
239 // Create a new tablespacefile
240 //
241 dd::Tablespace_file *tsf_obj = obj->add_file();
242 tsf_obj->set_filename(name + "file1");
243 obj->set_engine("innodb");
244 obj->set_name(name);
245 }
246
247 inline void set_attributes(dd::Table *obj, const dd::String_type &name,
248 const dd::Schema &schema) {
249 obj->set_name(name);
250
251 obj->set_schema_id(schema.id());
252 obj->set_collation_id(1);
253 obj->set_tablespace_id(1);
254 obj->set_engine("innodb");
255
256 //
257 // Create a new column
258 //
259 dd::Column *col_obj1 = obj->add_column();
260 col_obj1->set_name(name + "col2");
261 col_obj1->set_default_value_null(true);
262 col_obj1->set_collation_id(1);
263
264 // New column of type enum/set
265 dd::Column *col_obj2 = obj->add_column();
266 col_obj2->set_name(name + "col3");
267 col_obj2->set_default_value_null(true);
268 col_obj2->set_type(dd::enum_column_types::ENUM);
269 col_obj2->set_collation_id(1);
270
271 dd::Column_type_element *elem_obj = col_obj2->add_element();
272 elem_obj->set_name("enum elem1");
273
274 //
275 // Create a new indexes
276 //
277 dd::Index *idx_obj = obj->add_index();
278 idx_obj->set_name(name + "idx2");
279 idx_obj->set_comment("Index2 comment");
280 idx_obj->set_engine("innodb");
281 idx_obj->set_tablespace_id(1);
282 idx_obj->add_element(col_obj2);
283
284 // Add hidden index element
285 dd::Index_element *ie = idx_obj->add_element(col_obj1);
286 ie->set_hidden(true);
287
288 // Create one more index on table.
289 dd::Index *idx2_obj = obj->add_index();
290 idx2_obj->set_name(name + "idx3");
291 idx2_obj->set_engine("innodb");
292 idx2_obj->set_comment("Index3 comment");
293
294 // Copy Index elements from first index.
295 for (const dd::Index_element *e : idx_obj->elements())
296 idx2_obj->add_element(const_cast<dd::Column *>(&e->column()));
297
298 //
299 // Store table partition information
300 //
301
302 dd::Partition *part_obj = obj->add_partition();
303 part_obj->set_name("table_part1");
304 part_obj->set_number(2);
305 part_obj->set_comment("Partition comment");
306 part_obj->set_tablespace_id(1);
307
308 dd::Partition_value *part_value_obj = part_obj->add_value();
309 part_value_obj->set_list_num(1);
310 part_value_obj->set_column_num(2);
311 part_value_obj->set_value_utf8("part value");
312
313 dd::Partition_index *part_index_obj = part_obj->add_index(idx_obj);
314 part_index_obj->set_tablespace_id(1);
315
316 //
317 // Store table trigger information
318 //
319
320 dd::Trigger *trig_obj =
321 obj->add_trigger(dd::Trigger::enum_action_timing::AT_BEFORE,
322 dd::Trigger::enum_event_type::ET_INSERT);
323 trig_obj->set_name("newtrigger0");
324 trig_obj->set_definer("definer_username", "definer_hostname");
325 trig_obj->set_client_collation_id(1);
326 trig_obj->set_connection_collation_id(1);
327 trig_obj->set_schema_collation_id(1);
328 }
329
330 inline void set_attributes(dd::View *obj, const dd::String_type &name,
331 const dd::Schema &schema) {
332 obj->set_name(name);
333 obj->set_definer("definer_username", "definer_hostname");
334 obj->set_schema_id(schema.id());
335 obj->set_client_collation_id(1);
336 obj->set_connection_collation_id(1);
337
338 //
339 // Create a new column of type ENUM
340 //
341
342 dd::Column *col_obj = obj->add_column();
343 col_obj->set_name(name + "viewcol2");
344 col_obj->set_collation_id(1);
345 col_obj->set_default_value_null(true);
346
347 //
348 // Add tables using which view is defined.
349 //
350
351 dd::View_table *vt_obj = obj->add_table();
352 vt_obj->set_table_catalog("def");
353 vt_obj->set_table_schema("test");
354 vt_obj->set_table_name("t1");
355 }
356
357 inline void set_attributes(dd::Event *obj, const dd::String_type &name,
358 const dd::Schema &schema) {
359 obj->set_name(name);
360 obj->set_definer("definer_username", "definer_hostname");
361 obj->set_schema_id(schema.id());
362 obj->set_client_collation_id(1);
363 obj->set_connection_collation_id(1);
364 obj->set_schema_collation_id(1);
365 }
366
367 inline void set_attributes(dd::Procedure *obj, const dd::String_type &name,
368 const dd::Schema &schema) {
369 obj->set_name(name);
370 obj->set_definer("definer_username", "definer_hostname");
371 obj->set_schema_id(schema.id());
372 obj->set_client_collation_id(1);
373 obj->set_connection_collation_id(1);
374 obj->set_schema_collation_id(1);
375
376 //
377 // Create a new parameter
378 //
379
380 dd::Parameter *param_obj = obj->add_parameter();
381 param_obj->set_name(name + "param1");
382 param_obj->set_collation_id(1);
383 }
384
385 inline void set_attributes(dd::Foreign_key *obj, const dd::String_type &name) {
386 obj->set_name(name);
387 obj->set_match_option(dd::Foreign_key::OPTION_FULL);
388 obj->set_update_rule(dd::Foreign_key::RULE_SET_DEFAULT);
389 obj->set_delete_rule(dd::Foreign_key::RULE_CASCADE);
390 obj->set_referenced_table_schema_name("mysql");
391 obj->set_referenced_table_name("dual");
392
393 // Create Foreign key column
394 dd::Foreign_key_element *fke = obj->add_element();
395 fke->referenced_column_name("EMPLOYEE");
396 }
397
398 inline void set_attributes(dd::Charset *obj, const dd::String_type &name) {
399 obj->set_name(name);
400 obj->set_default_collation_id(42);
401 }
402
403 inline void set_attributes(dd::Collation *obj, const dd::String_type &name) {
404 obj->set_name(name);
405 obj->set_charset_id(42);
406 }
407
408 inline void set_attributes(dd::Column_statistics *obj,
409 const dd::String_type &name) {
410 obj->set_name(name);
411 obj->set_schema_name("schema");
412 obj->set_table_name("table");
413 obj->set_column_name("column");
414
415 histograms::Value_map<longlong> value_map(&my_charset_numeric,
416 histograms::Value_map_type::INT);
417 value_map.add_values(100, 10);
418 value_map.add_values(-1, 10);
419 value_map.add_values(1, 10);
420
421 MEM_ROOT mem_root(PSI_NOT_INSTRUMENTED, 256);
422
423 /*
424 The Column_statistics object will take over the histogram data and free the
425 MEM_ROOT contents.
426 */
427 histograms::Histogram *histogram = histograms::build_histogram(
428 &mem_root, value_map, 10, "schema", "table", "column");
429
430 obj->set_histogram(histogram);
431 }
432
433 template <typename T>
434 T *nullp() {
435 return nullptr;
436 }
437
438 } // namespace dd_unittest
439
440 #endif // DD_INCLUDED