| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 70 |
|
| 71 |
class Mock_dd_HANDLER : public Base_mock_HANDLER { |
| 72 |
public: |
| 73 |
|
| 74 |
MOCK_METHOD4(index_read_map, int(::uchar *, const ::uchar *, key_part_map, |
| 75 |
enum ha_rkey_function)); |
| 76 |
|
| 77 |
|
| 78 |
MOCK_METHOD1(write_row, int(::uchar *)); |
| 79 |
|
| 80 |
|
| 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 |
|
| 91 |
|
| 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 |
|
| 99 |
using Field_longlong::store; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 131 |
class Mock_dd_field_varstring : public Base_mock_field_varstring { |
| 132 |
const char *m_fake_val; |
| 133 |
|
| 134 |
public: |
| 135 |
|
| 136 |
using Field_varstring::store; |
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 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 |
|
| 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); |
| 174 |
|
| 175 |
|
| 176 |
m_field_list.push_back(new (thd->mem_root) Mock_dd_field_longlong()); |
| 177 |
m_field_list.push_back(new (thd->mem_root) |
| 178 |
Mock_dd_field_longlong()); |
| 179 |
m_field_list.push_back( |
| 180 |
new (thd->mem_root) Mock_dd_field_varstring(64, &dummy_share)); |
| 181 |
m_field_list.push_back(new (thd->mem_root) |
| 182 |
Mock_dd_field_longlong()); |
| 183 |
m_field_list.push_back(new (thd->mem_root) |
| 184 |
Mock_dd_field_longlong()); |
| 185 |
m_field_list.push_back(new (thd->mem_root) |
| 186 |
Mock_dd_field_longlong()); |
| 187 |
m_field_list.push_back(new (*THR_MALLOC) |
| 188 |
Mock_dd_field_varstring(128, &dummy_share)); |
| 189 |
m_field_list.push_back(new (*THR_MALLOC) |
| 190 |
Mock_dd_field_longlong()); |
| 191 |
|
| 192 |
|
| 193 |
table = new Fake_TABLE(m_field_list); |
| 194 |
|
| 195 |
|
| 196 |
StrictMock<Mock_dd_HANDLER> *ha = |
| 197 |
new (thd->mem_root) StrictMock<Mock_dd_HANDLER>(hton, table->s); |
| 198 |
|
| 199 |
|
| 200 |
ha->change_table_ptr(table, table->s); |
| 201 |
|
| 202 |
|
| 203 |
table->set_handler(ha); |
| 204 |
table->in_use = thd; |
| 205 |
|
| 206 |
|
| 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 |
|
| 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 |
|
| 225 |
table->record[1] = new uchar[table->s->reclength]; |
| 226 |
|
| 227 |
return table; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 285 |
dd::Index_element *ie = idx_obj->add_element(col_obj1); |
| 286 |
ie->set_hidden(true); |
| 287 |
|
| 288 |
|
| 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 |
|
| 295 |
for (const dd::Index_element *e : idx_obj->elements()) |
| 296 |
idx2_obj->add_element(const_cast<dd::Column *>(&e->column())); |
| 297 |
|
| 298 |
|
| 299 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 425 |
|
| 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 |
} |
| 439 |
|
| 440 |
#endif |