1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2011-2017 Kouhei Sutou <kou@clear-code.com>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#ifndef MRN_MYSQL_COMPAT_H_
21#define MRN_MYSQL_COMPAT_H_
22
23#include "mrn_mysql.h"
24
25#if MYSQL_VERSION_ID >= 50604
26# define MRN_HAVE_MYSQL_TYPE_TIMESTAMP2
27# define MRN_HAVE_MYSQL_TYPE_DATETIME2
28# define MRN_HAVE_MYSQL_TYPE_TIME2
29#endif
30
31#if MYSQL_VERSION_ID >= 50709 && !defined(MRN_MARIADB_P)
32# define MRN_HAVE_MYSQL_TYPE_JSON
33#endif
34
35#if MYSQL_VERSION_ID < 50603
36 typedef MYSQL_ERROR Sql_condition;
37#endif
38
39#if defined(MRN_MARIADB_P)
40# if MYSQL_VERSION_ID < 100000
41 typedef COST_VECT Cost_estimate;
42# endif
43#endif
44
45#ifndef MRN_MARIADB_P
46 typedef char *range_id_t;
47#endif
48
49#if defined(MRN_MARIADB_P) || MYSQL_VERSION_ID < 80002
50 typedef st_select_lex SELECT_LEX;
51#endif
52
53#if MYSQL_VERSION_ID >= 50609
54# define MRN_KEY_HAS_USER_DEFINED_KEYPARTS
55#endif
56
57#ifdef MRN_KEY_HAS_USER_DEFINED_KEYPARTS
58# define KEY_N_KEY_PARTS(key) (key)->user_defined_key_parts
59#else
60# define KEY_N_KEY_PARTS(key) (key)->key_parts
61#endif
62
63#if defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100213
64# define mrn_init_alloc_root(PTR, SZ1, SZ2, FLAG) \
65 init_alloc_root(PTR, "mroonga", SZ1, SZ2, FLAG)
66#elif defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100000
67# define mrn_init_alloc_root(PTR, SZ1, SZ2, FLAG) \
68 init_alloc_root(PTR, SZ1, SZ2, FLAG)
69#elif MYSQL_VERSION_ID >= 50706
70# define mrn_init_alloc_root(PTR, SZ1, SZ2, FLAG) \
71 init_alloc_root(mrn_memory_key, PTR, SZ1, SZ2)
72#else
73# define mrn_init_alloc_root(PTR, SZ1, SZ2, FLAG) \
74 init_alloc_root(PTR, SZ1, SZ2)
75#endif
76
77#if MYSQL_VERSION_ID < 100002 || !defined(MRN_MARIADB_P)
78# define GTS_TABLE 0
79#endif
80
81#if MYSQL_VERSION_ID >= 50607
82# if MYSQL_VERSION_ID >= 100007 && defined(MRN_MARIADB_P)
83# define MRN_GET_ERROR_MESSAGE thd_get_error_message(current_thd)
84# define MRN_GET_ERROR_NUMBER thd_get_error_number(current_thd)
85# define MRN_GET_CURRENT_ROW_FOR_WARNING(thd) thd_get_error_row(thd)
86# else
87# define MRN_GET_ERROR_MESSAGE current_thd->get_stmt_da()->message()
88# define MRN_GET_ERROR_NUMBER current_thd->get_stmt_da()->sql_errno()
89# if MYSQL_VERSION_ID >= 50706
90# define MRN_GET_CURRENT_ROW_FOR_WARNING(thd) \
91 thd->get_stmt_da()->current_row_for_condition()
92# else
93# define MRN_GET_CURRENT_ROW_FOR_WARNING(thd) \
94 thd->get_stmt_da()->current_row_for_warning()
95# endif
96# endif
97#else
98# define MRN_GET_ERROR_MESSAGE current_thd->stmt_da->message()
99# define MRN_GET_ERROR_NUMBER current_thd->stmt_da->sql_errno()
100# define MRN_GET_CURRENT_ROW_FOR_WARNING(thd) thd->warning_info->current_row_for_warning()
101#endif
102
103#if MYSQL_VERSION_ID >= 50607 && !defined(MRN_MARIADB_P)
104# define MRN_ITEM_HAVE_ITEM_NAME
105#endif
106
107#if MYSQL_VERSION_ID < 100000
108# define MRN_HAVE_TABLE_DEF_CACHE
109#endif
110
111#if defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100009
112# define MRN_HAVE_TDC_ACQUIRE_SHARE
113# if MYSQL_VERSION_ID < 100200
114# define MRN_TDC_ACQUIRE_SHARE_REQUIRE_KEY
115# endif
116#endif
117
118#if MYSQL_VERSION_ID >= 50613
119# define MRN_HAVE_ALTER_INFO
120#endif
121
122#if MYSQL_VERSION_ID >= 50603
123# define MRN_HAVE_GET_TABLE_DEF_KEY
124#endif
125
126#if defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100004
127# define MRN_TABLE_SHARE_HAVE_LOCK_SHARE
128#endif
129
130#ifndef TIME_FUZZY_DATE
131/* For MariaDB 10. */
132# ifdef TIME_FUZZY_DATES
133# define TIME_FUZZY_DATE TIME_FUZZY_DATES
134# endif
135#endif
136
137#if MYSQL_VERSION_ID >= 100007 && defined(MRN_MARIADB_P)
138# define MRN_USE_MYSQL_DATA_HOME
139#endif
140
141#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
142# define MRN_SEVERITY_WARNING Sql_condition::SL_WARNING
143#else
144# define MRN_SEVERITY_WARNING Sql_condition::WARN_LEVEL_WARN
145#endif
146
147#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
148# define MRN_HAVE_PSI_MEMORY_KEY
149#endif
150
151#ifdef MRN_HAVE_PSI_MEMORY_KEY
152# define mrn_my_malloc(size, flags) \
153 my_malloc(mrn_memory_key, size, flags)
154# define mrn_my_strdup(string, flags) \
155 my_strdup(mrn_memory_key, string, flags)
156# define mrn_my_strndup(string, size, flags) \
157 my_strndup(mrn_memory_key, string, size, flags)
158# define mrn_my_multi_malloc(flags, ...) \
159 my_multi_malloc(mrn_memory_key, flags, __VA_ARGS__)
160#else
161# define mrn_my_malloc(size, flags) my_malloc(size, flags)
162# define mrn_my_strdup(string, flags) my_strdup(string, flags)
163# define mrn_my_strndup(string, size, flags) \
164 my_strndup(string, size, flags)
165# define mrn_my_multi_malloc(flags, ...) \
166 my_multi_malloc(flags, __VA_ARGS__)
167#endif
168
169#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
170# define MRN_STRING_FREE(string) string.mem_free();
171#else
172# define MRN_STRING_FREE(string) string.free();
173#endif
174
175#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
176# define MRN_THD_DB_PATH(thd) ((thd)->db().str)
177#else
178# define MRN_THD_DB_PATH(thd) ((thd)->db.str)
179#endif
180
181#ifndef INT_MAX64
182# define INT_MAX64 LONGLONG_MAX
183#endif
184
185#ifdef UINT_MAX
186# define UINT_MAX64 UINT_MAX
187#else
188# define UINT_MAX64 LONGLONG_MAX
189#endif
190
191#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
192# define mrn_my_stpmov(dst, src) my_stpmov(dst, src)
193#else
194# define mrn_my_stpmov(dst, src) strmov(dst, src)
195#endif
196
197#if MYSQL_VERSION_ID >= 50607
198# if !defined(MRN_MARIADB_P)
199# define MRN_HAVE_SQL_OPTIMIZER_H
200# endif
201#endif
202
203#if MYSQL_VERSION_ID >= 50600 && !defined(MRN_MARIADB_P)
204# define MRN_HAVE_BINLOG_H
205#endif
206
207#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
208# define MRN_HAVE_SPATIAL
209#elif defined(HAVE_SPATIAL)
210# define MRN_HAVE_SPATIAL
211#endif
212
213#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
214# define MRN_FORMAT_STRING_LENGTH "zu"
215#else
216# define MRN_FORMAT_STRING_LENGTH "u"
217#endif
218
219#ifdef MRN_MARIADB_P
220# define MRN_SUPPORT_CUSTOM_OPTIONS
221#endif
222
223#ifdef MRN_MARIADB_P
224# define MRN_HAVE_ITEM_EQUAL_FIELDS_ITERATOR
225#endif
226
227#if MYSQL_VERSION_ID >= 50706 && !defined(MRN_MARIADB_P)
228# define MRN_SELECT_LEX_GET_WHERE_COND(select_lex) \
229 ((select_lex)->where_cond())
230# define MRN_SELECT_LEX_GET_HAVING_COND(select_lex) \
231 ((select_lex)->having_cond())
232# define MRN_SELECT_LEX_GET_ACTIVE_OPTIONS(select_lex) \
233 ((select_lex)->active_options())
234#else
235# define MRN_SELECT_LEX_GET_WHERE_COND(select_lex) \
236 ((select_lex)->where)
237# define MRN_SELECT_LEX_GET_HAVING_COND(select_lex) \
238 ((select_lex)->having)
239# define MRN_SELECT_LEX_GET_ACTIVE_OPTIONS(select_lex) \
240 ((select_lex)->options)
241#endif
242
243#if defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100000
244# if MYSQL_VERSION_ID >= 100213
245# define mrn_init_sql_alloc(thd, mem_root) \
246 init_sql_alloc(mem_root, "Mroonga", \
247 TABLE_ALLOC_BLOCK_SIZE, \
248 0, \
249 MYF(thd->slave_thread ? 0 : MY_THREAD_SPECIFIC))
250#elif MYSQL_VERSION_ID >= 100104
251# define mrn_init_sql_alloc(thd, mem_root) \
252 init_sql_alloc(mem_root, \
253 TABLE_ALLOC_BLOCK_SIZE, \
254 0, \
255 MYF(thd->slave_thread ? 0 : MY_THREAD_SPECIFIC))
256# else
257# define mrn_init_sql_alloc(thd, mem_root) \
258 init_sql_alloc(mem_root, \
259 TABLE_ALLOC_BLOCK_SIZE, \
260 0, \
261 MYF(0))
262# endif
263#else
264# if MYSQL_VERSION_ID >= 50709
265# define mrn_init_sql_alloc(thd, mem_root) \
266 init_sql_alloc(mrn_memory_key, \
267 mem_root, \
268 TABLE_ALLOC_BLOCK_SIZE, \
269 0)
270# else
271# define mrn_init_sql_alloc(thd, mem_root) \
272 init_sql_alloc(mem_root, \
273 TABLE_ALLOC_BLOCK_SIZE, \
274 0)
275# endif
276#endif
277
278#ifdef MRN_MARIADB_P
279# define MRN_ABORT_ON_WARNING(thd) thd->abort_on_warning
280#else
281# if MYSQL_VERSION_ID >= 50706
282# define MRN_ABORT_ON_WARNING(thd) thd->is_strict_mode()
283# else
284# define MRN_ABORT_ON_WARNING(thd) thd->abort_on_warning
285# endif
286#endif
287
288#define MRN_ERROR_CODE_DATA_TRUNCATE(thd) \
289 (MRN_ABORT_ON_WARNING(thd) ? ER_WARN_DATA_OUT_OF_RANGE : WARN_DATA_TRUNCATED)
290
291#if MYSQL_VERSION_ID >= 50709 && !defined(MRN_MARIADB_P)
292# define mrn_my_hash_init(hash, \
293 charset, \
294 default_array_elements, \
295 key_offset, \
296 key_length, \
297 get_key, \
298 free_element, \
299 flags) \
300 my_hash_init(hash, \
301 charset, \
302 default_array_elements, \
303 key_offset, \
304 key_length, \
305 get_key, \
306 free_element, \
307 flags, \
308 mrn_memory_key)
309#else
310# define mrn_my_hash_init(hash, \
311 charset, \
312 default_array_elements, \
313 key_offset, \
314 key_length, \
315 get_key, \
316 free_element, \
317 flags) \
318 my_hash_init(hash, \
319 charset, \
320 default_array_elements, \
321 key_offset, \
322 key_length, \
323 get_key, \
324 free_element, \
325 flags)
326#endif
327
328#if defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100000
329# define mrn_strconvert(from_cs, \
330 from, \
331 from_length, \
332 to_cs, \
333 to, \
334 to_length, \
335 errors) \
336 strconvert((from_cs), \
337 (from), \
338 (from_length), \
339 (to_cs), \
340 (to), \
341 (to_length), \
342 (errors))
343#else
344# define mrn_strconvert(from_cs, \
345 from, \
346 from_length, \
347 to_cs, \
348 to, \
349 to_length, \
350 errors) \
351 strconvert((from_cs), \
352 (from), \
353 (to_cs), \
354 (to), \
355 (to_length), \
356 (errors))
357#endif
358
359#if MYSQL_VERSION_ID >= 50717 && !defined(MRN_MARIADB_P)
360# define mrn_is_directory_separator(c) \
361 is_directory_separator((c))
362#else
363# define mrn_is_directory_separator(c) \
364 (c == FN_LIBCHAR || c == FN_LIBCHAR2)
365#endif
366
367#if ((MYSQL_VERSION_ID < 50636) || \
368 (MYSQL_VERSION_ID >= 50700 && MYSQL_VERSION_ID < 50718)) && !defined(MRN_MARIADB_P)
369# define MRN_HAVE_MYSQL_FIELD_PART_OF_KEY_NOT_CLUSTERED
370#endif
371
372#if defined(MRN_MARIADB_P) && \
373 ((MYSQL_VERSION_ID >= 100207) || \
374 ((MYSQL_VERSION_ID >= 100126) && (MYSQL_VERSION_ID < 100200)) || \
375 ((MYSQL_VERSION_ID >= 100032) && (MYSQL_VERSION_ID < 100100)) || \
376 ((MYSQL_VERSION_ID >= 50557) && (MYSQL_VERSION_ID < 100000)))
377# define mrn_create_partition_name(out, \
378 out_length, \
379 in1, \
380 in2, \
381 name_variant, \
382 translate) \
383 create_partition_name(out, out_length, in1, in2, name_variant, translate)
384# define mrn_create_subpartition_name(out, \
385 out_length, \
386 in1, \
387 in2, \
388 in3, \
389 name_variant) \
390 create_subpartition_name(out, out_length, in1, in2, in3, name_variant)
391#else
392# define mrn_create_partition_name(out, \
393 out_length, \
394 in1, \
395 in2, \
396 name_variant, \
397 translate) \
398 (create_partition_name(out, in1, in2, name_variant, translate), 0)
399# define mrn_create_subpartition_name(out, \
400 out_length, \
401 in1, \
402 in2, \
403 in3, \
404 name_variant) \
405 (create_subpartition_name(out, in1, in2, in3, name_variant), 0)
406#endif
407
408#if (!defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 80002)
409# define ITEM_SUM_GET_NEST_LEVEL(sum_item) (sum_item)->base_select->nest_level
410# define ITEM_SUM_GET_AGGR_LEVEL(sum_item) (sum_item)->aggr_select->nest_level
411# define ITEM_SUM_GET_MAX_AGGR_LEVEL(sum_item) (sum_item)->max_aggr_level
412#else
413# define ITEM_SUM_GET_NEST_LEVEL(sum_item) (sum_item)->nest_level
414# define ITEM_SUM_GET_AGGR_LEVEL(sum_item) (sum_item)->aggr_level
415# define ITEM_SUM_GET_MAX_AGGR_LEVEL(sum_item) (sum_item)->max_arg_level
416#endif
417
418#if (!defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 80002)
419 typedef bool mrn_bool;
420#else
421 typedef my_bool mrn_bool;
422#endif
423
424#define MRN_ALLOCATE_VARIABLE_LENGTH_ARRAYS(type, variable_name, variable_size) \
425 type *variable_name = \
426 (type *)mrn_my_malloc(sizeof(type) * (variable_size), MYF(MY_WME))
427#define MRN_FREE_VARIABLE_LENGTH_ARRAYS(variable_name) \
428 my_free(variable_name)
429
430#if ((defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 100203)) || \
431 (!defined(MRN_MARIADB_P) && MYSQL_VERSION_ID >= 50711)
432# define MRN_ALTER_INPLACE_INFO_ADD_VIRTUAL_COLUMN \
433 ALTER_ADD_VIRTUAL_COLUMN
434# define MRN_ALTER_INPLACE_INFO_ADD_STORED_BASE_COLUMN \
435 ALTER_ADD_STORED_BASE_COLUMN
436# define MRN_ALTER_INPLACE_INFO_ADD_STORED_GENERATED_COLUMN \
437 ALTER_ADD_STORED_GENERATED_COLUMN
438#else
439# define MRN_ALTER_INPLACE_INFO_ADD_VIRTUAL_COLUMN 0
440# define MRN_ALTER_INPLACE_INFO_ADD_STORED_BASE_COLUMN \
441 Alter_inplace_info::ADD_COLUMN
442# define MRN_ALTER_INPLACE_INFO_ADD_STORED_GENERATED_COLUMN 0
443#endif
444
445#if (defined(HA_CAN_VIRTUAL_COLUMNS) || defined(HA_GENERATED_COLUMNS))
446# define MRN_SUPPORT_GENERATED_COLUMNS
447#endif
448
449#ifdef MRN_MARIADB_P
450# if (MYSQL_VERSION_ID >= 100200)
451# define MRN_GENERATED_COLUMNS_FIELD_IS_VIRTUAL(field) \
452 (!field->stored_in_db())
453# define MRN_GENERATED_COLUMNS_FIELD_IS_STORED(field) \
454 (field->vcol_info && field->vcol_info->is_stored())
455# elif (MYSQL_VERSION_ID >= 50500)
456# define MRN_GENERATED_COLUMNS_FIELD_IS_VIRTUAL(field) \
457 (!field->stored_in_db)
458# define MRN_GENERATED_COLUMNS_FIELD_IS_STORED(field) \
459 (field->vcol_info && field->vcol_info->is_stored())
460# else
461# define MRN_GENERATED_COLUMNS_FIELD_IS_VIRTUAL(field) false
462# define MRN_GENERATED_COLUMNS_FIELD_IS_STORED(field) false
463# endif
464#else
465# if (MYSQL_VERSION_ID >= 50708)
466# define MRN_GENERATED_COLUMNS_FIELD_IS_VIRTUAL(field) \
467 (field->is_virtual_gcol())
468# define MRN_GENERATED_COLUMNS_FIELD_IS_STORED(field) \
469 (field->is_gcol() && !field->is_virtual_gcol())
470# elif (MYSQL_VERSION_ID >= 50706)
471# define MRN_GENERATED_COLUMNS_FIELD_IS_VIRTUAL(field) \
472 (!field->stored_in_db)
473# define MRN_GENERATED_COLUMNS_FIELD_IS_STORED(field) \
474 (field->gcol_info && field->gcol_info->get_field_stored())
475# else
476# define MRN_GENERATED_COLUMNS_FIELD_IS_VIRTUAL(field) false
477# define MRN_GENERATED_COLUMNS_FIELD_IS_STORED(field) false
478# endif
479#endif
480
481#ifdef MRN_MARIADB_P
482# if (MYSQL_VERSION_ID >= 100203)
483# define MRN_GENERATED_COLUMNS_UPDATE_VIRTUAL_FIELD(table, field) \
484 (table->update_virtual_field(field))
485# else
486# define MRN_GENERATED_COLUMNS_UPDATE_VIRTUAL_FIELD(table, field) \
487 (field->vcol_info->expr_item->save_in_field(field, 0))
488# endif
489#endif
490
491#endif /* MRN_MYSQL_COMPAT_H_ */
492