| 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 FAKE_TABLE_H |
| 25 |
#define FAKE_TABLE_H |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <string.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <ostream> |
| 31 |
#include <string> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include "gtest/gtest.h" |
| 35 |
#include "lex_string.h" |
| 36 |
#include "my_alloc.h" |
| 37 |
#include "my_bitmap.h" |
| 38 |
|
| 39 |
#include "my_inttypes.h" |
| 40 |
#include "mysql_com.h" |
| 41 |
#include "sql/current_thd.h" |
| 42 |
#include "sql/field.h" |
| 43 |
#include "sql/item.h" |
| 44 |
#include "sql/key.h" |
| 45 |
#include "sql/sql_bitmap.h" |
| 46 |
#include "sql/sql_const.h" |
| 47 |
#include "sql/sql_lex.h" |
| 48 |
#include "sql/sql_list.h" |
| 49 |
#include "sql/table.h" |
| 50 |
#include "sql/thr_malloc.h" |
| 51 |
#include "unittest/gunit/handler-t.h" |
| 52 |
#include "unittest/gunit/mock_field_long.h" |
| 53 |
|
| 54 |
class handler; |
| 55 |
struct handlerton; |
| 56 |
|
| 57 |
using std::string; |
| 58 |
using std::vector; |
| 59 |
using ::testing::NiceMock; |
| 60 |
|
| 61 |
static const uint MAX_TABLE_COLUMNS = sizeof(int) * 8; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class Fake_Table_ref : public Table_ref { |
| 68 |
public: |
| 69 |
Fake_Table_ref() = default; |
| 70 |
~Fake_Table_ref() = default; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
class Fake_TABLE_SHARE : public TABLE_SHARE { |
| 77 |
uint32 all_set_buf; |
| 78 |
|
| 79 |
public: |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
Fake_TABLE_SHARE(uint number_of_columns) { |
| 86 |
static const char *fakepath = "fakepath"; |
| 87 |
fields = number_of_columns; |
| 88 |
db_create_options = 0; |
| 89 |
primary_key = 0; |
| 90 |
column_bitmap_size = sizeof(int); |
| 91 |
tmp_table = NO_TMP_TABLE; |
| 92 |
db_low_byte_first = true; |
| 93 |
path.str = const_cast<char *>(fakepath); |
| 94 |
path.length = strlen(path.str); |
| 95 |
|
| 96 |
EXPECT_EQ(0, bitmap_init(&all_set, &all_set_buf, fields)); |
| 97 |
bitmap_set_above(&all_set, 0, true); |
| 98 |
} |
| 99 |
~Fake_TABLE_SHARE() = default; |
| 100 |
void set_secondary_engine(bool enable) { m_secondary_engine = enable; } |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
class Fake_TABLE : public TABLE { |
| 108 |
|
| 109 |
static const int max_keys = 8; |
| 110 |
KEY m_keys[max_keys]; |
| 111 |
|
| 112 |
KEY_PART_INFO m_key_part_infos[max_keys][8]; |
| 113 |
|
| 114 |
uchar m_record[MAX_FIELD_WIDTH * MAX_TABLE_COLUMNS]; |
| 115 |
uchar m_null_flags[MAX_TABLE_COLUMNS + 7 / 8]; |
| 116 |
|
| 117 |
Fake_TABLE_SHARE table_share; |
| 118 |
|
| 119 |
Fake_handlerton fake_handlerton; |
| 120 |
MY_BITMAP write_set_struct; |
| 121 |
uint32 write_set_buf; |
| 122 |
MY_BITMAP read_set_struct; |
| 123 |
uint32 read_set_buf; |
| 124 |
uint32 tmp_set_buf; |
| 125 |
Field *m_field_array[MAX_TABLE_COLUMNS]{}; |
| 126 |
Mock_field_long *m_mock_field_array[MAX_TABLE_COLUMNS]; |
| 127 |
|
| 128 |
|
| 129 |
int highest_index_id; |
| 130 |
|
| 131 |
|
| 132 |
static int highest_table_id; |
| 133 |
|
| 134 |
private: |
| 135 |
void initialize() { |
| 136 |
TABLE *as_table = static_cast<TABLE *>(this); |
| 137 |
new (as_table) TABLE(); |
| 138 |
s = &table_share; |
| 139 |
in_use = current_thd; |
| 140 |
null_row = false; |
| 141 |
read_set = &read_set_struct; |
| 142 |
write_set = &write_set_struct; |
| 143 |
next_number_field = nullptr; |
| 144 |
pos_in_table_list = new (*THR_MALLOC) Fake_Table_ref(); |
| 145 |
pos_in_table_list->table = this; |
| 146 |
pos_in_table_list->query_block = |
| 147 |
new (&mem_root) Query_block(&mem_root, nullptr, nullptr); |
| 148 |
EXPECT_EQ(0, bitmap_init(write_set, &write_set_buf, s->fields)); |
| 149 |
EXPECT_EQ(0, bitmap_init(read_set, &read_set_buf, s->fields)); |
| 150 |
EXPECT_EQ(0, bitmap_init(&tmp_set, &tmp_set_buf, s->fields)); |
| 151 |
read_set_internal = *read_set; |
| 152 |
|
| 153 |
const_table = false; |
| 154 |
pos_in_table_list->set_tableno(highest_table_id); |
| 155 |
highest_table_id = (highest_table_id + 1) % MAX_TABLES; |
| 156 |
key_info = &m_keys[0]; |
| 157 |
record[0] = &m_record[0]; |
| 158 |
memset(record[0], 0, sizeof(m_record)); |
| 159 |
null_flags = m_null_flags; |
| 160 |
memset(null_flags, 0, sizeof(m_null_flags)); |
| 161 |
s->null_bytes = sizeof(m_null_flags); |
| 162 |
for (int i = 0; i < max_keys; i++) |
| 163 |
key_info[i].key_part = m_key_part_infos[i]; |
| 164 |
highest_index_id = 0; |
| 165 |
|
| 166 |
set_handler(&mock_handler); |
| 167 |
mock_handler.change_table_ptr(this, &table_share); |
| 168 |
field = m_field_array; |
| 169 |
} |
| 170 |
|
| 171 |
public: |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static const int DEFAULT_PACK_LENGTH = Mock_field_long::PACK_LENGTH; |
| 177 |
NiceMock<Mock_HANDLER> mock_handler{&fake_handlerton, &table_share}; |
| 178 |
|
| 179 |
explicit Fake_TABLE(List<Field> fields) : table_share(fields.elements) { |
| 180 |
initialize(); |
| 181 |
List_iterator<Field> it(fields); |
| 182 |
int nbr_fields = 0; |
| 183 |
for (Field *cur_field = it++; cur_field; cur_field = it++) |
| 184 |
add(cur_field, nbr_fields++); |
| 185 |
} |
| 186 |
|
| 187 |
explicit Fake_TABLE(Field *column) : table_share(1) { |
| 188 |
initialize(); |
| 189 |
add(column, 0); |
| 190 |
} |
| 191 |
|
| 192 |
Fake_TABLE(Field *column1, Field *column2) : table_share(2) { |
| 193 |
initialize(); |
| 194 |
add(column1, 0); |
| 195 |
add(column2, 1); |
| 196 |
} |
| 197 |
|
| 198 |
Fake_TABLE(Field *column1, Field *column2, Field *column3) : table_share(3) { |
| 199 |
initialize(); |
| 200 |
add(column1, 0); |
| 201 |
add(column2, 1); |
| 202 |
add(column3, 2); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
Fake_TABLE(int column_count, bool cols_nullable) |
| 213 |
: table_share(column_count), |
| 214 |
mock_handler(&fake_handlerton, &table_share) { |
| 215 |
assert(static_cast<size_t>(column_count) <= sizeof(int) * 8); |
| 216 |
initialize(); |
| 217 |
for (int i = 0; i < column_count; ++i) { |
| 218 |
std::stringstream str; |
| 219 |
str << "field_" << (i + 1); |
| 220 |
add(new (*THR_MALLOC) |
| 221 |
Mock_field_long(str.str().c_str(), cols_nullable, false), |
| 222 |
i); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
Fake_TABLE(std::initializer_list<int> column_values, bool are_nullable = true) |
| 233 |
: table_share(column_values.size()), |
| 234 |
mock_handler(static_cast<handlerton *>(nullptr), &table_share) { |
| 235 |
field = m_field_array; |
| 236 |
initialize(); |
| 237 |
for (size_t i = 0; i < column_values.size(); ++i) { |
| 238 |
std::stringstream s; |
| 239 |
s << "field_" << i + 1; |
| 240 |
field[i] = |
| 241 |
new (*THR_MALLOC) Mock_field_long(s.str(), are_nullable, false); |
| 242 |
field[i]->table = this; |
| 243 |
const ptrdiff_t field_offset = i * MAX_FIELD_WIDTH; |
| 244 |
field[i]->set_field_ptr(record[0] + field_offset + 1); |
| 245 |
if (are_nullable) field[i]->set_null_ptr(record[0] + field_offset, 1); |
| 246 |
} |
| 247 |
int i = 0; |
| 248 |
for (auto column_value : column_values) { |
| 249 |
auto item = new Item_int(column_value); |
| 250 |
item->save_in_field_no_warnings(field[i++], true); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
~Fake_TABLE() { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
int create_index(Field *column1, Field *column2, bool unique = false) { |
| 263 |
return create_index(column1, column2, unique ? ulong{HA_NOSAME} : 0); |
| 264 |
} |
| 265 |
|
| 266 |
int create_index(Field *column1, Field *column2, ulong key_flags) { |
| 267 |
column1->set_flag(PART_KEY_FLAG); |
| 268 |
int index_id = highest_index_id++; |
| 269 |
column1->key_start.set_bit(index_id); |
| 270 |
keys_in_use_for_query.set_bit(index_id); |
| 271 |
column1->part_of_key.set_bit(index_id); |
| 272 |
|
| 273 |
if (column2 != nullptr) { |
| 274 |
column2->set_flag(PART_KEY_FLAG); |
| 275 |
column2->part_of_key.set_bit(index_id); |
| 276 |
} |
| 277 |
|
| 278 |
KEY *key = &m_keys[index_id]; |
| 279 |
key->table = this; |
| 280 |
key->flags = key->actual_flags = key_flags; |
| 281 |
key->actual_key_parts = key->user_defined_key_parts = 1; |
| 282 |
key->key_part[0].init_from_field(column1); |
| 283 |
++s->key_parts; |
| 284 |
if (column2 != nullptr) { |
| 285 |
key->actual_key_parts = key->user_defined_key_parts = 2; |
| 286 |
key->key_part[1].init_from_field(column2); |
| 287 |
++s->key_parts; |
| 288 |
} |
| 289 |
key->name = "unittest_index"; |
| 290 |
|
| 291 |
++s->keys; |
| 292 |
s->visible_indexes.set_bit(index_id); |
| 293 |
s->keys_in_use.set_bit(index_id); |
| 294 |
return index_id; |
| 295 |
} |
| 296 |
|
| 297 |
void set_handler(handler *h) { file = h; } |
| 298 |
TABLE_SHARE *get_share() { return &table_share; } |
| 299 |
|
| 300 |
static void reset_highest_table_id() { highest_table_id = 0; } |
| 301 |
|
| 302 |
private: |
| 303 |
void add(Field *new_field, int pos) { |
| 304 |
field[pos] = new_field; |
| 305 |
new_field->table = this; |
| 306 |
static const char *table_name = "Fake"; |
| 307 |
new_field->table_name = &table_name; |
| 308 |
new_field->set_field_index(pos); |
| 309 |
bitmap_set_bit(read_set, pos); |
| 310 |
const ptrdiff_t field_offset = pos * MAX_FIELD_WIDTH; |
| 311 |
new_field->set_field_ptr(record[0] + field_offset); |
| 312 |
if (new_field->get_null_ptr() != nullptr) |
| 313 |
new_field->set_null_ptr(null_flags + pos / 8, 1 << (pos % 8)); |
| 314 |
} |
| 315 |
}; |
| 316 |
|
| 317 |
#endif |