1#ifndef HANDLER_INCLUDED
2#define HANDLER_INCLUDED
3/*
4 Copyright (c) 2000, 2016, Oracle and/or its affiliates.
5 Copyright (c) 2009, 2017, MariaDB Corporation.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of
10 the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22/* Definitions for parameters to do with handler-routines */
23
24#ifdef USE_PRAGMA_INTERFACE
25#pragma interface /* gcc class implementation */
26#endif
27
28#include "sql_const.h"
29#include "sql_basic_types.h"
30#include "mysqld.h" /* server_id */
31#include "sql_plugin.h" /* plugin_ref, st_plugin_int, plugin */
32#include "thr_lock.h" /* thr_lock_type, THR_LOCK_DATA */
33#include "sql_cache.h"
34#include "structs.h" /* SHOW_COMP_OPTION */
35#include "sql_array.h" /* Dynamic_array<> */
36#include "mdl.h"
37#include "vers_string.h"
38
39#include "sql_analyze_stmt.h" // for Exec_time_tracker
40
41#include <my_compare.h>
42#include <ft_global.h>
43#include <keycache.h>
44#include <mysql/psi/mysql_table.h>
45#include "sql_sequence.h"
46
47class Alter_info;
48class Virtual_column_info;
49class sequence_definition;
50
51// the following is for checking tables
52
53#define HA_ADMIN_ALREADY_DONE 1
54#define HA_ADMIN_OK 0
55#define HA_ADMIN_NOT_IMPLEMENTED -1
56#define HA_ADMIN_FAILED -2
57#define HA_ADMIN_CORRUPT -3
58#define HA_ADMIN_INTERNAL_ERROR -4
59#define HA_ADMIN_INVALID -5
60#define HA_ADMIN_REJECT -6
61#define HA_ADMIN_TRY_ALTER -7
62#define HA_ADMIN_WRONG_CHECKSUM -8
63#define HA_ADMIN_NOT_BASE_TABLE -9
64#define HA_ADMIN_NEEDS_UPGRADE -10
65#define HA_ADMIN_NEEDS_ALTER -11
66#define HA_ADMIN_NEEDS_CHECK -12
67
68/**
69 Return values for check_if_supported_inplace_alter().
70
71 @see check_if_supported_inplace_alter() for description of
72 the individual values.
73*/
74enum enum_alter_inplace_result {
75 HA_ALTER_ERROR,
76 HA_ALTER_INPLACE_COPY_NO_LOCK,
77 HA_ALTER_INPLACE_COPY_LOCK,
78 HA_ALTER_INPLACE_NOCOPY_LOCK,
79 HA_ALTER_INPLACE_NOCOPY_NO_LOCK,
80 HA_ALTER_INPLACE_INSTANT,
81 HA_ALTER_INPLACE_NOT_SUPPORTED,
82 HA_ALTER_INPLACE_EXCLUSIVE_LOCK,
83 HA_ALTER_INPLACE_SHARED_LOCK,
84 HA_ALTER_INPLACE_NO_LOCK
85};
86
87/* Bits in table_flags() to show what database can do */
88
89#define HA_NO_TRANSACTIONS (1ULL << 0) /* Doesn't support transactions */
90#define HA_PARTIAL_COLUMN_READ (1ULL << 1) /* read may not return all columns */
91#define HA_TABLE_SCAN_ON_INDEX (1ULL << 2) /* No separate data/index file */
92/*
93 The following should be set if the following is not true when scanning
94 a table with rnd_next()
95 - We will see all rows (including deleted ones)
96 - Row positions are 'table->s->db_record_offset' apart
97 If this flag is not set, filesort will do a position() call for each matched
98 row to be able to find the row later.
99*/
100#define HA_REC_NOT_IN_SEQ (1ULL << 3)
101#define HA_CAN_GEOMETRY (1ULL << 4)
102/*
103 Reading keys in random order is as fast as reading keys in sort order
104 (Used in records.cc to decide if we should use a record cache and by
105 filesort to decide if we should sort key + data or key + pointer-to-row
106*/
107#define HA_FAST_KEY_READ (1ULL << 5)
108/*
109 Set the following flag if we on delete should force all key to be read
110 and on update read all keys that changes
111*/
112#define HA_REQUIRES_KEY_COLUMNS_FOR_DELETE (1ULL << 6)
113#define HA_NULL_IN_KEY (1ULL << 7) /* One can have keys with NULL */
114#define HA_DUPLICATE_POS (1ULL << 8) /* ha_position() gives dup row */
115#define HA_NO_BLOBS (1ULL << 9) /* Doesn't support blobs */
116#define HA_CAN_INDEX_BLOBS (1ULL << 10)
117#define HA_AUTO_PART_KEY (1ULL << 11) /* auto-increment in multi-part key */
118#define HA_REQUIRE_PRIMARY_KEY (1ULL << 12) /* .. and can't create a hidden one */
119#define HA_STATS_RECORDS_IS_EXACT (1ULL << 13) /* stats.records is exact */
120/*
121 INSERT_DELAYED only works with handlers that uses MySQL internal table
122 level locks
123*/
124#define HA_CAN_INSERT_DELAYED (1ULL << 14)
125/*
126 If we get the primary key columns for free when we do an index read
127 (usually, it also implies that HA_PRIMARY_KEY_REQUIRED_FOR_POSITION
128 flag is set).
129*/
130#define HA_PRIMARY_KEY_IN_READ_INDEX (1ULL << 15)
131/*
132 If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, it means that to position()
133 uses a primary key given by the record argument.
134 Without primary key, we can't call position().
135 If not set, the position is returned as the current rows position
136 regardless of what argument is given.
137*/
138#define HA_PRIMARY_KEY_REQUIRED_FOR_POSITION (1ULL << 16)
139#define HA_CAN_RTREEKEYS (1ULL << 17)
140#define HA_NOT_DELETE_WITH_CACHE (1ULL << 18) /* unused */
141/*
142 The following is we need to a primary key to delete (and update) a row.
143 If there is no primary key, all columns needs to be read on update and delete
144*/
145#define HA_PRIMARY_KEY_REQUIRED_FOR_DELETE (1ULL << 19)
146#define HA_NO_PREFIX_CHAR_KEYS (1ULL << 20)
147#define HA_CAN_FULLTEXT (1ULL << 21)
148#define HA_CAN_SQL_HANDLER (1ULL << 22)
149#define HA_NO_AUTO_INCREMENT (1ULL << 23)
150/* Has automatic checksums and uses the old checksum format */
151#define HA_HAS_OLD_CHECKSUM (1ULL << 24)
152/* Table data are stored in separate files (for lower_case_table_names) */
153#define HA_FILE_BASED (1ULL << 26)
154#define HA_NO_VARCHAR (1ULL << 27) /* unused */
155#define HA_CAN_BIT_FIELD (1ULL << 28) /* supports bit fields */
156#define HA_NEED_READ_RANGE_BUFFER (1ULL << 29) /* for read_multi_range */
157#define HA_ANY_INDEX_MAY_BE_UNIQUE (1ULL << 30)
158#define HA_NO_COPY_ON_ALTER (1ULL << 31)
159#define HA_HAS_RECORDS (1ULL << 32) /* records() gives exact count*/
160/* Has it's own method of binlog logging */
161#define HA_HAS_OWN_BINLOGGING (1ULL << 33)
162/*
163 Engine is capable of row-format and statement-format logging,
164 respectively
165*/
166#define HA_BINLOG_ROW_CAPABLE (1ULL << 34)
167#define HA_BINLOG_STMT_CAPABLE (1ULL << 35)
168
169/*
170 When a multiple key conflict happens in a REPLACE command mysql
171 expects the conflicts to be reported in the ascending order of
172 key names.
173
174 For e.g.
175
176 CREATE TABLE t1 (a INT, UNIQUE (a), b INT NOT NULL, UNIQUE (b), c INT NOT
177 NULL, INDEX(c));
178
179 REPLACE INTO t1 VALUES (1,1,1),(2,2,2),(2,1,3);
180
181 MySQL expects the conflict with 'a' to be reported before the conflict with
182 'b'.
183
184 If the underlying storage engine does not report the conflicting keys in
185 ascending order, it causes unexpected errors when the REPLACE command is
186 executed.
187
188 This flag helps the underlying SE to inform the server that the keys are not
189 ordered.
190*/
191#define HA_DUPLICATE_KEY_NOT_IN_ORDER (1ULL << 36)
192
193/*
194 Engine supports REPAIR TABLE. Used by CHECK TABLE FOR UPGRADE if an
195 incompatible table is detected. If this flag is set, CHECK TABLE FOR UPGRADE
196 will report ER_TABLE_NEEDS_UPGRADE, otherwise ER_TABLE_NEED_REBUILD.
197*/
198#define HA_CAN_REPAIR (1ULL << 37)
199
200/* Has automatic checksums and uses the new checksum format */
201#define HA_HAS_NEW_CHECKSUM (1ULL << 38)
202#define HA_CAN_VIRTUAL_COLUMNS (1ULL << 39)
203#define HA_MRR_CANT_SORT (1ULL << 40)
204#define HA_RECORD_MUST_BE_CLEAN_ON_WRITE (1ULL << 41) /* unused */
205
206/*
207 This storage engine supports condition pushdown
208*/
209#define HA_CAN_TABLE_CONDITION_PUSHDOWN (1ULL << 42)
210/* old name for the same flag */
211#define HA_MUST_USE_TABLE_CONDITION_PUSHDOWN HA_CAN_TABLE_CONDITION_PUSHDOWN
212
213/**
214 The handler supports read before write removal optimization
215
216 Read before write removal may be used for storage engines which support
217 write without previous read of the row to be updated. Handler returning
218 this flag must implement start_read_removal() and end_read_removal().
219 The handler may return "fake" rows constructed from the key of the row
220 asked for. This is used to optimize UPDATE and DELETE by reducing the
221 numer of roundtrips between handler and storage engine.
222
223 Example:
224 UPDATE a=1 WHERE pk IN (<keys>)
225
226 mysql_update()
227 {
228 if (<conditions for starting read removal>)
229 start_read_removal()
230 -> handler returns true if read removal supported for this table/query
231
232 while(read_record("pk=<key>"))
233 -> handler returns fake row with column "pk" set to <key>
234
235 ha_update_row()
236 -> handler sends write "a=1" for row with "pk=<key>"
237
238 end_read_removal()
239 -> handler returns the number of rows actually written
240 }
241
242 @note This optimization in combination with batching may be used to
243 remove even more roundtrips.
244*/
245#define HA_READ_BEFORE_WRITE_REMOVAL (1ULL << 43)
246
247/*
248 Engine supports extended fulltext API
249 */
250#define HA_CAN_FULLTEXT_EXT (1ULL << 44)
251
252/*
253 Storage engine supports table export using the
254 FLUSH TABLE <table_list> FOR EXPORT statement
255 (meaning, after this statement one can copy table files out of the
256 datadir and later "import" (somehow) in another MariaDB instance)
257 */
258#define HA_CAN_EXPORT (1ULL << 45)
259
260/*
261 Storage engine does not require an exclusive metadata lock
262 on the table during optimize. (TODO and repair?).
263 It can allow other connections to open the table.
264 (it does not necessarily mean that other connections can
265 read or modify the table - this is defined by THR locks and the
266 ::store_lock() method).
267*/
268#define HA_CONCURRENT_OPTIMIZE (1ULL << 46)
269
270/*
271 If the storage engine support tables that will not roll back on commit
272 In addition the table should not lock rows and support READ and WRITE
273 UNCOMMITTED.
274 This is useful for implementing things like SEQUENCE but can also in
275 the future be useful to do logging that should never roll back.
276*/
277#define HA_CAN_TABLES_WITHOUT_ROLLBACK (1ULL << 47)
278
279/*
280 Mainly for usage by SEQUENCE engine. Setting this flag means
281 that the table will never roll back and that all operations
282 for this table should stored in the non transactional log
283 space that will always be written, even on rollback.
284*/
285
286#define HA_PERSISTENT_TABLE (1ULL << 48)
287
288/*
289 Set of all binlog flags. Currently only contain the capabilities
290 flags.
291 */
292#define HA_BINLOG_FLAGS (HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE)
293
294/* The following are used by Spider */
295#define HA_CAN_FORCE_BULK_UPDATE (1ULL << 50)
296#define HA_CAN_FORCE_BULK_DELETE (1ULL << 51)
297#define HA_CAN_DIRECT_UPDATE_AND_DELETE (1ULL << 52)
298
299/* The following is for partition handler */
300#define HA_CAN_MULTISTEP_MERGE (1LL << 53)
301
302/* calling cmp_ref() on the engine is expensive */
303#define HA_CMP_REF_IS_EXPENSIVE (1ULL << 54)
304
305/* bits in index_flags(index_number) for what you can do with index */
306#define HA_READ_NEXT 1 /* TODO really use this flag */
307#define HA_READ_PREV 2 /* supports ::index_prev */
308#define HA_READ_ORDER 4 /* index_next/prev follow sort order */
309#define HA_READ_RANGE 8 /* can find all records in a range */
310#define HA_ONLY_WHOLE_INDEX 16 /* Can't use part key searches */
311#define HA_KEYREAD_ONLY 64 /* Support HA_EXTRA_KEYREAD */
312
313/*
314 Index scan will not return records in rowid order. Not guaranteed to be
315 set for unordered (e.g. HASH) indexes.
316*/
317#define HA_KEY_SCAN_NOT_ROR 128
318#define HA_DO_INDEX_COND_PUSHDOWN 256 /* Supports Index Condition Pushdown */
319/*
320 Data is clustered on this key. This means that when you read the key
321 you also get the row data without any additional disk reads.
322*/
323#define HA_CLUSTERED_INDEX 512
324
325/*
326 bits in alter_table_flags:
327*/
328/*
329 These bits are set if different kinds of indexes can be created or dropped
330 in-place without re-creating the table using a temporary table.
331 NO_READ_WRITE indicates that the handler needs concurrent reads and writes
332 of table data to be blocked.
333 Partitioning needs both ADD and DROP to be supported by its underlying
334 handlers, due to error handling, see bug#57778.
335*/
336#define HA_INPLACE_ADD_INDEX_NO_READ_WRITE (1UL << 0)
337#define HA_INPLACE_DROP_INDEX_NO_READ_WRITE (1UL << 1)
338#define HA_INPLACE_ADD_UNIQUE_INDEX_NO_READ_WRITE (1UL << 2)
339#define HA_INPLACE_DROP_UNIQUE_INDEX_NO_READ_WRITE (1UL << 3)
340#define HA_INPLACE_ADD_PK_INDEX_NO_READ_WRITE (1UL << 4)
341#define HA_INPLACE_DROP_PK_INDEX_NO_READ_WRITE (1UL << 5)
342/*
343 These are set if different kinds of indexes can be created or dropped
344 in-place while still allowing concurrent reads (but not writes) of table
345 data. If a handler is capable of one or more of these, it should also set
346 the corresponding *_NO_READ_WRITE bit(s).
347*/
348#define HA_INPLACE_ADD_INDEX_NO_WRITE (1UL << 6)
349#define HA_INPLACE_DROP_INDEX_NO_WRITE (1UL << 7)
350#define HA_INPLACE_ADD_UNIQUE_INDEX_NO_WRITE (1UL << 8)
351#define HA_INPLACE_DROP_UNIQUE_INDEX_NO_WRITE (1UL << 9)
352#define HA_INPLACE_ADD_PK_INDEX_NO_WRITE (1UL << 10)
353#define HA_INPLACE_DROP_PK_INDEX_NO_WRITE (1UL << 11)
354/*
355 HA_PARTITION_FUNCTION_SUPPORTED indicates that the function is
356 supported at all.
357 HA_FAST_CHANGE_PARTITION means that optimised variants of the changes
358 exists but they are not necessarily done online.
359
360 HA_ONLINE_DOUBLE_WRITE means that the handler supports writing to both
361 the new partition and to the old partitions when updating through the
362 old partitioning schema while performing a change of the partitioning.
363 This means that we can support updating of the table while performing
364 the copy phase of the change. For no lock at all also a double write
365 from new to old must exist and this is not required when this flag is
366 set.
367 This is actually removed even before it was introduced the first time.
368 The new idea is that handlers will handle the lock level already in
369 store_lock for ALTER TABLE partitions.
370
371 HA_PARTITION_ONE_PHASE is a flag that can be set by handlers that take
372 care of changing the partitions online and in one phase. Thus all phases
373 needed to handle the change are implemented inside the storage engine.
374 The storage engine must also support auto-discovery since the frm file
375 is changed as part of the change and this change must be controlled by
376 the storage engine. A typical engine to support this is NDB (through
377 WL #2498).
378*/
379#define HA_PARTITION_FUNCTION_SUPPORTED (1UL << 12)
380#define HA_FAST_CHANGE_PARTITION (1UL << 13)
381#define HA_PARTITION_ONE_PHASE (1UL << 14)
382
383/* operations for disable/enable indexes */
384#define HA_KEY_SWITCH_NONUNIQ 0
385#define HA_KEY_SWITCH_ALL 1
386#define HA_KEY_SWITCH_NONUNIQ_SAVE 2
387#define HA_KEY_SWITCH_ALL_SAVE 3
388
389/*
390 Note: the following includes binlog and closing 0.
391 TODO remove the limit, use dynarrays
392*/
393#define MAX_HA 64
394
395/*
396 Use this instead of 0 as the initial value for the slot number of
397 handlerton, so that we can distinguish uninitialized slot number
398 from slot 0.
399*/
400#define HA_SLOT_UNDEF ((uint)-1)
401
402/*
403 Parameters for open() (in register form->filestat)
404 HA_GET_INFO does an implicit HA_ABORT_IF_LOCKED
405*/
406
407#define HA_OPEN_KEYFILE 1U
408#define HA_READ_ONLY 16U /* File opened as readonly */
409/* Try readonly if can't open with read and write */
410#define HA_TRY_READ_ONLY 32U
411
412 /* Some key definitions */
413#define HA_KEY_NULL_LENGTH 1
414#define HA_KEY_BLOB_LENGTH 2
415
416#define HA_LEX_CREATE_TMP_TABLE 1U
417#define HA_CREATE_TMP_ALTER 8U
418#define HA_LEX_CREATE_SEQUENCE 16U
419#define HA_VERSIONED_TABLE 32U
420
421#define HA_MAX_REC_LENGTH 65535
422
423/* Table caching type */
424#define HA_CACHE_TBL_NONTRANSACT 0
425#define HA_CACHE_TBL_NOCACHE 1U
426#define HA_CACHE_TBL_ASKTRANSACT 2U
427#define HA_CACHE_TBL_TRANSACT 4U
428
429/**
430 Options for the START TRANSACTION statement.
431
432 Note that READ ONLY and READ WRITE are logically mutually exclusive.
433 This is enforced by the parser and depended upon by trans_begin().
434
435 We need two flags instead of one in order to differentiate between
436 situation when no READ WRITE/ONLY clause were given and thus transaction
437 is implicitly READ WRITE and the case when READ WRITE clause was used
438 explicitly.
439*/
440
441// WITH CONSISTENT SNAPSHOT option
442static const uint MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT = 1;
443// READ ONLY option
444static const uint MYSQL_START_TRANS_OPT_READ_ONLY = 2;
445// READ WRITE option
446static const uint MYSQL_START_TRANS_OPT_READ_WRITE = 4;
447
448/* Flags for method is_fatal_error */
449#define HA_CHECK_DUP_KEY 1U
450#define HA_CHECK_DUP_UNIQUE 2U
451#define HA_CHECK_FK_ERROR 4U
452#define HA_CHECK_DUP (HA_CHECK_DUP_KEY + HA_CHECK_DUP_UNIQUE)
453#define HA_CHECK_ALL (~0U)
454
455/* Options for info_push() */
456#define INFO_KIND_UPDATE_FIELDS 101
457#define INFO_KIND_UPDATE_VALUES 102
458#define INFO_KIND_FORCE_LIMIT_BEGIN 103
459#define INFO_KIND_FORCE_LIMIT_END 104
460
461enum legacy_db_type
462{
463 /* note these numerical values are fixed and can *not* be changed */
464 DB_TYPE_UNKNOWN=0,
465 DB_TYPE_HEAP=6,
466 DB_TYPE_MYISAM=9,
467 DB_TYPE_MRG_MYISAM=10,
468 DB_TYPE_INNODB=12,
469 DB_TYPE_EXAMPLE_DB=15,
470 DB_TYPE_ARCHIVE_DB=16,
471 DB_TYPE_CSV_DB=17,
472 DB_TYPE_FEDERATED_DB=18,
473 DB_TYPE_BLACKHOLE_DB=19,
474 DB_TYPE_PARTITION_DB=20,
475 DB_TYPE_BINLOG=21,
476 DB_TYPE_PBXT=23,
477 DB_TYPE_PERFORMANCE_SCHEMA=28,
478 DB_TYPE_ARIA=42,
479 DB_TYPE_TOKUDB=43,
480 DB_TYPE_SEQUENCE=44,
481 DB_TYPE_FIRST_DYNAMIC=45,
482 DB_TYPE_DEFAULT=127 // Must be last
483};
484/*
485 Better name for DB_TYPE_UNKNOWN. Should be used for engines that do not have
486 a hard-coded type value here.
487 */
488#define DB_TYPE_AUTOASSIGN DB_TYPE_UNKNOWN
489
490enum row_type { ROW_TYPE_NOT_USED=-1, ROW_TYPE_DEFAULT, ROW_TYPE_FIXED,
491 ROW_TYPE_DYNAMIC, ROW_TYPE_COMPRESSED,
492 ROW_TYPE_REDUNDANT, ROW_TYPE_COMPACT, ROW_TYPE_PAGE };
493
494/* not part of the enum, so that it shouldn't be in switch(row_type) */
495#define ROW_TYPE_MAX ((uint)ROW_TYPE_PAGE + 1)
496
497/* Specifies data storage format for individual columns */
498enum column_format_type {
499 COLUMN_FORMAT_TYPE_DEFAULT= 0, /* Not specified (use engine default) */
500 COLUMN_FORMAT_TYPE_FIXED= 1, /* FIXED format */
501 COLUMN_FORMAT_TYPE_DYNAMIC= 2 /* DYNAMIC format */
502};
503
504enum enum_binlog_func {
505 BFN_RESET_LOGS= 1,
506 BFN_RESET_SLAVE= 2,
507 BFN_BINLOG_WAIT= 3,
508 BFN_BINLOG_END= 4,
509 BFN_BINLOG_PURGE_FILE= 5
510};
511
512enum enum_binlog_command {
513 LOGCOM_CREATE_TABLE,
514 LOGCOM_ALTER_TABLE,
515 LOGCOM_RENAME_TABLE,
516 LOGCOM_DROP_TABLE,
517 LOGCOM_CREATE_DB,
518 LOGCOM_ALTER_DB,
519 LOGCOM_DROP_DB
520};
521
522/* struct to hold information about the table that should be created */
523
524/* Bits in used_fields */
525#define HA_CREATE_USED_AUTO (1UL << 0)
526#define HA_CREATE_USED_RAID (1UL << 1) //RAID is no longer availble
527#define HA_CREATE_USED_UNION (1UL << 2)
528#define HA_CREATE_USED_INSERT_METHOD (1UL << 3)
529#define HA_CREATE_USED_MIN_ROWS (1UL << 4)
530#define HA_CREATE_USED_MAX_ROWS (1UL << 5)
531#define HA_CREATE_USED_AVG_ROW_LENGTH (1UL << 6)
532#define HA_CREATE_USED_PACK_KEYS (1UL << 7)
533#define HA_CREATE_USED_CHARSET (1UL << 8)
534#define HA_CREATE_USED_DEFAULT_CHARSET (1UL << 9)
535#define HA_CREATE_USED_DATADIR (1UL << 10)
536#define HA_CREATE_USED_INDEXDIR (1UL << 11)
537#define HA_CREATE_USED_ENGINE (1UL << 12)
538#define HA_CREATE_USED_CHECKSUM (1UL << 13)
539#define HA_CREATE_USED_DELAY_KEY_WRITE (1UL << 14)
540#define HA_CREATE_USED_ROW_FORMAT (1UL << 15)
541#define HA_CREATE_USED_COMMENT (1UL << 16)
542#define HA_CREATE_USED_PASSWORD (1UL << 17)
543#define HA_CREATE_USED_CONNECTION (1UL << 18)
544#define HA_CREATE_USED_KEY_BLOCK_SIZE (1UL << 19)
545/* The following two are used by Maria engine: */
546#define HA_CREATE_USED_TRANSACTIONAL (1UL << 20)
547#define HA_CREATE_USED_PAGE_CHECKSUM (1UL << 21)
548/** This is set whenever STATS_PERSISTENT=0|1|default has been
549specified in CREATE/ALTER TABLE. See also HA_OPTION_STATS_PERSISTENT in
550include/my_base.h. It is possible to distinguish whether
551STATS_PERSISTENT=default has been specified or no STATS_PERSISTENT= is
552given at all. */
553#define HA_CREATE_USED_STATS_PERSISTENT (1UL << 22)
554/**
555 This is set whenever STATS_AUTO_RECALC=0|1|default has been
556 specified in CREATE/ALTER TABLE. See enum_stats_auto_recalc.
557 It is possible to distinguish whether STATS_AUTO_RECALC=default
558 has been specified or no STATS_AUTO_RECALC= is given at all.
559*/
560#define HA_CREATE_USED_STATS_AUTO_RECALC (1UL << 23)
561/**
562 This is set whenever STATS_SAMPLE_PAGES=N|default has been
563 specified in CREATE/ALTER TABLE. It is possible to distinguish whether
564 STATS_SAMPLE_PAGES=default has been specified or no STATS_SAMPLE_PAGES= is
565 given at all.
566*/
567#define HA_CREATE_USED_STATS_SAMPLE_PAGES (1UL << 24)
568
569/* Create a sequence */
570#define HA_CREATE_USED_SEQUENCE (1UL << 25)
571
572typedef ulonglong alter_table_operations;
573
574/*
575 These flags are set by the parser and describes the type of
576 operation(s) specified by the ALTER TABLE statement.
577*/
578
579// Set by parser for ADD [COLUMN]
580#define ALTER_PARSER_ADD_COLUMN (1ULL << 0)
581// Set by parser for DROP [COLUMN]
582#define ALTER_PARSER_DROP_COLUMN (1ULL << 1)
583// Set for CHANGE [COLUMN] | MODIFY [CHANGE] & mysql_recreate_table
584#define ALTER_CHANGE_COLUMN (1ULL << 2)
585// Set for ADD INDEX | ADD KEY | ADD PRIMARY KEY | ADD UNIQUE KEY |
586// ADD UNIQUE INDEX | ALTER ADD [COLUMN]
587#define ALTER_ADD_INDEX (1ULL << 3)
588// Set for DROP PRIMARY KEY | DROP FOREIGN KEY | DROP KEY | DROP INDEX
589#define ALTER_DROP_INDEX (1ULL << 4)
590// Set for RENAME [TO]
591#define ALTER_RENAME (1ULL << 5)
592// Set for ORDER BY
593#define ALTER_ORDER (1ULL << 6)
594// Set for table_options, like table comment
595#define ALTER_OPTIONS (1ULL << 7)
596// Set for ALTER [COLUMN] ... SET DEFAULT ... | DROP DEFAULT
597#define ALTER_CHANGE_COLUMN_DEFAULT (1ULL << 8)
598// Set for DISABLE KEYS | ENABLE KEYS
599#define ALTER_KEYS_ONOFF (1ULL << 9)
600// Set for FORCE, ENGINE(same engine), by mysql_recreate_table()
601#define ALTER_RECREATE (1ULL << 10)
602// Set for ADD FOREIGN KEY
603#define ALTER_ADD_FOREIGN_KEY (1ULL << 21)
604// Set for DROP FOREIGN KEY
605#define ALTER_DROP_FOREIGN_KEY (1ULL << 22)
606// Set for ADD [COLUMN] FIRST | AFTER
607#define ALTER_COLUMN_ORDER (1ULL << 25)
608#define ALTER_ADD_CHECK_CONSTRAINT (1ULL << 27)
609#define ALTER_DROP_CHECK_CONSTRAINT (1ULL << 28)
610#define ALTER_RENAME_COLUMN (1ULL << 29)
611#define ALTER_COLUMN_UNVERSIONED (1ULL << 30)
612#define ALTER_ADD_SYSTEM_VERSIONING (1ULL << 31)
613#define ALTER_DROP_SYSTEM_VERSIONING (1ULL << 32)
614#define ALTER_ADD_PERIOD (1ULL << 33)
615#define ALTER_DROP_PERIOD (1ULL << 34)
616
617/*
618 Following defines are used by ALTER_INPLACE_TABLE
619
620 They do describe in more detail the type operation(s) to be executed
621 by the storage engine. For example, which type of type of index to be
622 added/dropped. These are set by fill_alter_inplace_info().
623*/
624
625#define ALTER_RECREATE_TABLE ALTER_RECREATE
626#define ALTER_CHANGE_CREATE_OPTION ALTER_OPTIONS
627#define ALTER_ADD_COLUMN (ALTER_ADD_VIRTUAL_COLUMN | \
628 ALTER_ADD_STORED_BASE_COLUMN | \
629 ALTER_ADD_STORED_GENERATED_COLUMN)
630#define ALTER_DROP_COLUMN (ALTER_DROP_VIRTUAL_COLUMN | \
631 ALTER_DROP_STORED_COLUMN)
632#define ALTER_COLUMN_DEFAULT ALTER_CHANGE_COLUMN_DEFAULT
633
634#define ALTER_DROP_HISTORICAL (1ULL << 35)
635
636// Add non-unique, non-primary index
637#define ALTER_ADD_NON_UNIQUE_NON_PRIM_INDEX (1ULL << 36)
638
639// Drop non-unique, non-primary index
640#define ALTER_DROP_NON_UNIQUE_NON_PRIM_INDEX (1ULL << 37)
641
642// Add unique, non-primary index
643#define ALTER_ADD_UNIQUE_INDEX (1ULL << 38)
644
645// Drop unique, non-primary index
646#define ALTER_DROP_UNIQUE_INDEX (1ULL << 39)
647
648// Add primary index
649#define ALTER_ADD_PK_INDEX (1ULL << 40)
650
651// Drop primary index
652#define ALTER_DROP_PK_INDEX (1ULL << 41)
653
654// Virtual generated column
655#define ALTER_ADD_VIRTUAL_COLUMN (1ULL << 42)
656// Stored base (non-generated) column
657#define ALTER_ADD_STORED_BASE_COLUMN (1ULL << 43)
658// Stored generated column
659#define ALTER_ADD_STORED_GENERATED_COLUMN (1ULL << 44)
660
661// Drop column
662#define ALTER_DROP_VIRTUAL_COLUMN (1ULL << 45)
663#define ALTER_DROP_STORED_COLUMN (1ULL << 46)
664
665// Rename column (verified; ALTER_RENAME_COLUMN may use original name)
666#define ALTER_COLUMN_NAME (1ULL << 47)
667
668// Change column datatype
669#define ALTER_VIRTUAL_COLUMN_TYPE (1ULL << 48)
670#define ALTER_STORED_COLUMN_TYPE (1ULL << 49)
671
672/**
673 Change column datatype in such way that new type has compatible
674 packed representation with old type, so it is theoretically
675 possible to perform change by only updating data dictionary
676 without changing table rows.
677*/
678#define ALTER_COLUMN_EQUAL_PACK_LENGTH (1ULL << 50)
679
680// Reorder column
681#define ALTER_STORED_COLUMN_ORDER (1ULL << 51)
682
683// Reorder column
684#define ALTER_VIRTUAL_COLUMN_ORDER (1ULL << 52)
685
686// Change column from NOT NULL to NULL
687#define ALTER_COLUMN_NULLABLE (1ULL << 53)
688
689// Change column from NULL to NOT NULL
690#define ALTER_COLUMN_NOT_NULLABLE (1ULL << 54)
691
692// Change column generation expression
693#define ALTER_VIRTUAL_GCOL_EXPR (1ULL << 55)
694#define ALTER_STORED_GCOL_EXPR (1ULL << 56)
695
696// column's engine options changed, something in field->option_struct
697#define ALTER_COLUMN_OPTION (1ULL << 57)
698
699// MySQL alias for the same thing:
700#define ALTER_COLUMN_STORAGE_TYPE (1ULL << 58)
701
702// Change the column format of column
703#define ALTER_COLUMN_COLUMN_FORMAT (1ULL << 59)
704
705/**
706 Changes in generated columns that affect storage,
707 for example, when a vcol type or expression changes
708 and this vcol is indexed or used in a partitioning expression
709*/
710#define ALTER_COLUMN_VCOL (1ULL << 60)
711
712/**
713 ALTER TABLE for a partitioned table. The engine needs to commit
714 online alter of all partitions atomically (using group_commit_ctx)
715*/
716#define ALTER_PARTITIONED (1ULL << 61)
717
718/*
719 Flags set in partition_flags when altering partitions
720*/
721
722// Set for ADD PARTITION
723#define ALTER_PARTITION_ADD (1ULL << 1)
724// Set for DROP PARTITION
725#define ALTER_PARTITION_DROP (1ULL << 2)
726// Set for COALESCE PARTITION
727#define ALTER_PARTITION_COALESCE (1ULL << 3)
728// Set for REORGANIZE PARTITION ... INTO
729#define ALTER_PARTITION_REORGANIZE (1ULL << 4)
730// Set for partition_options
731#define ALTER_PARTITION_INFO (1ULL << 5)
732// Set for LOAD INDEX INTO CACHE ... PARTITION
733// Set for CACHE INDEX ... PARTITION
734#define ALTER_PARTITION_ADMIN (1ULL << 6)
735// Set for REBUILD PARTITION
736#define ALTER_PARTITION_REBUILD (1ULL << 7)
737// Set for partitioning operations specifying ALL keyword
738#define ALTER_PARTITION_ALL (1ULL << 8)
739// Set for REMOVE PARTITIONING
740#define ALTER_PARTITION_REMOVE (1ULL << 9)
741// Set for EXCHANGE PARITION
742#define ALTER_PARTITION_EXCHANGE (1ULL << 10)
743// Set by Sql_cmd_alter_table_truncate_partition::execute()
744#define ALTER_PARTITION_TRUNCATE (1ULL << 11)
745// Set for REORGANIZE PARTITION
746#define ALTER_PARTITION_TABLE_REORG (1ULL << 12)
747
748/*
749 This is master database for most of system tables. However there
750 can be other databases which can hold system tables. Respective
751 storage engines define their own system database names.
752*/
753extern const char *mysqld_system_database;
754
755/*
756 Structure to hold list of system_database.system_table.
757 This is used at both mysqld and storage engine layer.
758*/
759struct st_system_tablename
760{
761 const char *db;
762 const char *tablename;
763};
764
765
766typedef ulonglong my_xid; // this line is the same as in log_event.h
767#define MYSQL_XID_PREFIX "MySQLXid"
768#define MYSQL_XID_PREFIX_LEN 8 // must be a multiple of 8
769#define MYSQL_XID_OFFSET (MYSQL_XID_PREFIX_LEN+sizeof(server_id))
770#define MYSQL_XID_GTRID_LEN (MYSQL_XID_OFFSET+sizeof(my_xid))
771
772#define XIDDATASIZE MYSQL_XIDDATASIZE
773#define MAXGTRIDSIZE 64
774#define MAXBQUALSIZE 64
775
776#define COMPATIBLE_DATA_YES 0
777#define COMPATIBLE_DATA_NO 1
778
779/**
780 struct xid_t is binary compatible with the XID structure as
781 in the X/Open CAE Specification, Distributed Transaction Processing:
782 The XA Specification, X/Open Company Ltd., 1991.
783 http://www.opengroup.org/bookstore/catalog/c193.htm
784
785 @see MYSQL_XID in mysql/plugin.h
786*/
787struct xid_t {
788 long formatID;
789 long gtrid_length;
790 long bqual_length;
791 char data[XIDDATASIZE]; // not \0-terminated !
792
793 xid_t() {} /* Remove gcc warning */
794 bool eq(struct xid_t *xid)
795 { return !xid->is_null() && eq(xid->gtrid_length, xid->bqual_length, xid->data); }
796 bool eq(long g, long b, const char *d)
797 { return !is_null() && g == gtrid_length && b == bqual_length && !memcmp(d, data, g+b); }
798 void set(struct xid_t *xid)
799 { memcpy(this, xid, xid->length()); }
800 void set(long f, const char *g, long gl, const char *b, long bl)
801 {
802 formatID= f;
803 memcpy(data, g, gtrid_length= gl);
804 memcpy(data+gl, b, bqual_length= bl);
805 }
806 void set(ulonglong xid)
807 {
808 my_xid tmp;
809 formatID= 1;
810 set(MYSQL_XID_PREFIX_LEN, 0, MYSQL_XID_PREFIX);
811 memcpy(data+MYSQL_XID_PREFIX_LEN, &server_id, sizeof(server_id));
812 tmp= xid;
813 memcpy(data+MYSQL_XID_OFFSET, &tmp, sizeof(tmp));
814 gtrid_length=MYSQL_XID_GTRID_LEN;
815 }
816 void set(long g, long b, const char *d)
817 {
818 formatID= 1;
819 gtrid_length= g;
820 bqual_length= b;
821 memcpy(data, d, g+b);
822 }
823 bool is_null() { return formatID == -1; }
824 void null() { formatID= -1; }
825 my_xid quick_get_my_xid()
826 {
827 my_xid tmp;
828 memcpy(&tmp, data+MYSQL_XID_OFFSET, sizeof(tmp));
829 return tmp;
830 }
831 my_xid get_my_xid()
832 {
833 return gtrid_length == MYSQL_XID_GTRID_LEN && bqual_length == 0 &&
834 !memcmp(data, MYSQL_XID_PREFIX, MYSQL_XID_PREFIX_LEN) ?
835 quick_get_my_xid() : 0;
836 }
837 uint length()
838 {
839 return static_cast<uint>(sizeof(formatID)) + key_length();
840 }
841 uchar *key() const
842 {
843 return (uchar *)&gtrid_length;
844 }
845 uint key_length() const
846 {
847 return static_cast<uint>(sizeof(gtrid_length)+sizeof(bqual_length)+
848 gtrid_length+bqual_length);
849 }
850};
851typedef struct xid_t XID;
852
853/*
854 The size of XID string representation in the form
855 'gtrid', 'bqual', formatID
856 see xid_t::get_sql_string() for details.
857*/
858#define SQL_XIDSIZE (XIDDATASIZE * 2 + 8 + MY_INT64_NUM_DECIMAL_DIGITS)
859/* The 'buf' has to have space for at least SQL_XIDSIZE bytes. */
860uint get_sql_xid(XID *xid, char *buf);
861
862/* for recover() handlerton call */
863#define MIN_XID_LIST_SIZE 128
864#define MAX_XID_LIST_SIZE (1024*128)
865
866/*
867 These structures are used to pass information from a set of SQL commands
868 on add/drop/change tablespace definitions to the proper hton.
869*/
870#define UNDEF_NODEGROUP 65535
871enum ts_command_type
872{
873 TS_CMD_NOT_DEFINED = -1,
874 CREATE_TABLESPACE = 0,
875 ALTER_TABLESPACE = 1,
876 CREATE_LOGFILE_GROUP = 2,
877 ALTER_LOGFILE_GROUP = 3,
878 DROP_TABLESPACE = 4,
879 DROP_LOGFILE_GROUP = 5,
880 CHANGE_FILE_TABLESPACE = 6,
881 ALTER_ACCESS_MODE_TABLESPACE = 7
882};
883
884enum ts_alter_tablespace_type
885{
886 TS_ALTER_TABLESPACE_TYPE_NOT_DEFINED = -1,
887 ALTER_TABLESPACE_ADD_FILE = 1,
888 ALTER_TABLESPACE_DROP_FILE = 2
889};
890
891enum tablespace_access_mode
892{
893 TS_NOT_DEFINED= -1,
894 TS_READ_ONLY = 0,
895 TS_READ_WRITE = 1,
896 TS_NOT_ACCESSIBLE = 2
897};
898
899struct handlerton;
900class st_alter_tablespace : public Sql_alloc
901{
902 public:
903 const char *tablespace_name;
904 const char *logfile_group_name;
905 enum ts_command_type ts_cmd_type;
906 enum ts_alter_tablespace_type ts_alter_tablespace_type;
907 const char *data_file_name;
908 const char *undo_file_name;
909 const char *redo_file_name;
910 ulonglong extent_size;
911 ulonglong undo_buffer_size;
912 ulonglong redo_buffer_size;
913 ulonglong initial_size;
914 ulonglong autoextend_size;
915 ulonglong max_size;
916 uint nodegroup_id;
917 handlerton *storage_engine;
918 bool wait_until_completed;
919 const char *ts_comment;
920 enum tablespace_access_mode ts_access_mode;
921 st_alter_tablespace()
922 {
923 tablespace_name= NULL;
924 logfile_group_name= "DEFAULT_LG"; //Default log file group
925 ts_cmd_type= TS_CMD_NOT_DEFINED;
926 data_file_name= NULL;
927 undo_file_name= NULL;
928 redo_file_name= NULL;
929 extent_size= 1024*1024; //Default 1 MByte
930 undo_buffer_size= 8*1024*1024; //Default 8 MByte
931 redo_buffer_size= 8*1024*1024; //Default 8 MByte
932 initial_size= 128*1024*1024; //Default 128 MByte
933 autoextend_size= 0; //No autoextension as default
934 max_size= 0; //Max size == initial size => no extension
935 storage_engine= NULL;
936 nodegroup_id= UNDEF_NODEGROUP;
937 wait_until_completed= TRUE;
938 ts_comment= NULL;
939 ts_access_mode= TS_NOT_DEFINED;
940 }
941};
942
943/* The handler for a table type. Will be included in the TABLE structure */
944
945struct TABLE;
946
947/*
948 Make sure that the order of schema_tables and enum_schema_tables are the same.
949*/
950enum enum_schema_tables
951{
952 SCH_ALL_PLUGINS,
953 SCH_APPLICABLE_ROLES,
954 SCH_CHARSETS,
955 SCH_COLLATIONS,
956 SCH_COLLATION_CHARACTER_SET_APPLICABILITY,
957 SCH_COLUMNS,
958 SCH_COLUMN_PRIVILEGES,
959 SCH_ENABLED_ROLES,
960 SCH_ENGINES,
961 SCH_EVENTS,
962 SCH_EXPLAIN,
963 SCH_FILES,
964 SCH_GLOBAL_STATUS,
965 SCH_GLOBAL_VARIABLES,
966 SCH_KEY_CACHES,
967 SCH_KEY_COLUMN_USAGE,
968 SCH_OPEN_TABLES,
969 SCH_PARAMETERS,
970 SCH_PARTITIONS,
971 SCH_PLUGINS,
972 SCH_PROCESSLIST,
973 SCH_PROFILES,
974 SCH_REFERENTIAL_CONSTRAINTS,
975 SCH_PROCEDURES,
976 SCH_SCHEMATA,
977 SCH_SCHEMA_PRIVILEGES,
978 SCH_SESSION_STATUS,
979 SCH_SESSION_VARIABLES,
980 SCH_STATISTICS,
981 SCH_SYSTEM_VARIABLES,
982 SCH_TABLES,
983 SCH_TABLESPACES,
984 SCH_TABLE_CONSTRAINTS,
985 SCH_TABLE_NAMES,
986 SCH_TABLE_PRIVILEGES,
987 SCH_TRIGGERS,
988 SCH_USER_PRIVILEGES,
989 SCH_VIEWS,
990#ifdef HAVE_SPATIAL
991 SCH_GEOMETRY_COLUMNS,
992 SCH_SPATIAL_REF_SYS,
993#endif /*HAVE_SPATIAL*/
994};
995
996struct TABLE_SHARE;
997struct HA_CREATE_INFO;
998struct st_foreign_key_info;
999typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
1000typedef bool (stat_print_fn)(THD *thd, const char *type, size_t type_len,
1001 const char *file, size_t file_len,
1002 const char *status, size_t status_len);
1003enum ha_stat_type { HA_ENGINE_STATUS, HA_ENGINE_LOGS, HA_ENGINE_MUTEX };
1004extern st_plugin_int *hton2plugin[MAX_HA];
1005
1006/* Transaction log maintains type definitions */
1007enum log_status
1008{
1009 HA_LOG_STATUS_FREE= 0, /* log is free and can be deleted */
1010 HA_LOG_STATUS_INUSE= 1, /* log can't be deleted because it is in use */
1011 HA_LOG_STATUS_NOSUCHLOG= 2 /* no such log (can't be returned by
1012 the log iterator status) */
1013};
1014/*
1015 Function for signaling that the log file changed its state from
1016 LOG_STATUS_INUSE to LOG_STATUS_FREE
1017
1018 Now it do nothing, will be implemented as part of new transaction
1019 log management for engines.
1020 TODO: implement the function.
1021*/
1022void signal_log_not_needed(struct handlerton, char *log_file);
1023/*
1024 Data of transaction log iterator.
1025*/
1026struct handler_log_file_data {
1027 LEX_STRING filename;
1028 enum log_status status;
1029};
1030
1031/*
1032 Definitions for engine-specific table/field/index options in the CREATE TABLE.
1033
1034 Options are declared with HA_*OPTION_* macros (HA_TOPTION_NUMBER,
1035 HA_FOPTION_ENUM, HA_IOPTION_STRING, etc).
1036
1037 Every macros takes the option name, and the name of the underlying field of
1038 the appropriate C structure. The "appropriate C structure" is
1039 ha_table_option_struct for table level options,
1040 ha_field_option_struct for field level options,
1041 ha_index_option_struct for key level options. The engine either
1042 defines a structure of this name, or uses #define's to map
1043 these "appropriate" names to the actual structure type name.
1044
1045 ULL options use a ulonglong as the backing store.
1046 HA_*OPTION_NUMBER() takes the option name, the structure field name,
1047 the default value for the option, min, max, and blk_siz values.
1048
1049 STRING options use a char* as a backing store.
1050 HA_*OPTION_STRING takes the option name and the structure field name.
1051 The default value will be 0.
1052
1053 ENUM options use a uint as a backing store (not enum!!!).
1054 HA_*OPTION_ENUM takes the option name, the structure field name,
1055 the default value for the option as a number, and a string with the
1056 permitted values for this enum - one string with comma separated values,
1057 for example: "gzip,bzip2,lzma"
1058
1059 BOOL options use a bool as a backing store.
1060 HA_*OPTION_BOOL takes the option name, the structure field name,
1061 and the default value for the option.
1062 From the SQL, BOOL options accept YES/NO, ON/OFF, and 1/0.
1063
1064 The name of the option is limited to 255 bytes,
1065 the value (for string options) - to the 32767 bytes.
1066
1067 See ha_example.cc for an example.
1068*/
1069
1070struct ha_table_option_struct;
1071struct ha_field_option_struct;
1072struct ha_index_option_struct;
1073
1074enum ha_option_type { HA_OPTION_TYPE_ULL, /* unsigned long long */
1075 HA_OPTION_TYPE_STRING, /* char * */
1076 HA_OPTION_TYPE_ENUM, /* uint */
1077 HA_OPTION_TYPE_BOOL, /* bool */
1078 HA_OPTION_TYPE_SYSVAR};/* type of the sysval */
1079
1080#define HA_xOPTION_NUMBER(name, struc, field, def, min, max, blk_siz) \
1081 { HA_OPTION_TYPE_ULL, name, sizeof(name)-1, \
1082 offsetof(struc, field), def, min, max, blk_siz, 0, 0 }
1083#define HA_xOPTION_STRING(name, struc, field) \
1084 { HA_OPTION_TYPE_STRING, name, sizeof(name)-1, \
1085 offsetof(struc, field), 0, 0, 0, 0, 0, 0}
1086#define HA_xOPTION_ENUM(name, struc, field, values, def) \
1087 { HA_OPTION_TYPE_ENUM, name, sizeof(name)-1, \
1088 offsetof(struc, field), def, 0, \
1089 sizeof(values)-1, 0, values, 0 }
1090#define HA_xOPTION_BOOL(name, struc, field, def) \
1091 { HA_OPTION_TYPE_BOOL, name, sizeof(name)-1, \
1092 offsetof(struc, field), def, 0, 1, 0, 0, 0 }
1093#define HA_xOPTION_SYSVAR(name, struc, field, sysvar) \
1094 { HA_OPTION_TYPE_SYSVAR, name, sizeof(name)-1, \
1095 offsetof(struc, field), 0, 0, 0, 0, 0, MYSQL_SYSVAR(sysvar) }
1096#define HA_xOPTION_END { HA_OPTION_TYPE_ULL, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
1097
1098#define HA_TOPTION_NUMBER(name, field, def, min, max, blk_siz) \
1099 HA_xOPTION_NUMBER(name, ha_table_option_struct, field, def, min, max, blk_siz)
1100#define HA_TOPTION_STRING(name, field) \
1101 HA_xOPTION_STRING(name, ha_table_option_struct, field)
1102#define HA_TOPTION_ENUM(name, field, values, def) \
1103 HA_xOPTION_ENUM(name, ha_table_option_struct, field, values, def)
1104#define HA_TOPTION_BOOL(name, field, def) \
1105 HA_xOPTION_BOOL(name, ha_table_option_struct, field, def)
1106#define HA_TOPTION_SYSVAR(name, field, sysvar) \
1107 HA_xOPTION_SYSVAR(name, ha_table_option_struct, field, sysvar)
1108#define HA_TOPTION_END HA_xOPTION_END
1109
1110#define HA_FOPTION_NUMBER(name, field, def, min, max, blk_siz) \
1111 HA_xOPTION_NUMBER(name, ha_field_option_struct, field, def, min, max, blk_siz)
1112#define HA_FOPTION_STRING(name, field) \
1113 HA_xOPTION_STRING(name, ha_field_option_struct, field)
1114#define HA_FOPTION_ENUM(name, field, values, def) \
1115 HA_xOPTION_ENUM(name, ha_field_option_struct, field, values, def)
1116#define HA_FOPTION_BOOL(name, field, def) \
1117 HA_xOPTION_BOOL(name, ha_field_option_struct, field, def)
1118#define HA_FOPTION_SYSVAR(name, field, sysvar) \
1119 HA_xOPTION_SYSVAR(name, ha_field_option_struct, field, sysvar)
1120#define HA_FOPTION_END HA_xOPTION_END
1121
1122#define HA_IOPTION_NUMBER(name, field, def, min, max, blk_siz) \
1123 HA_xOPTION_NUMBER(name, ha_index_option_struct, field, def, min, max, blk_siz)
1124#define HA_IOPTION_STRING(name, field) \
1125 HA_xOPTION_STRING(name, ha_index_option_struct, field)
1126#define HA_IOPTION_ENUM(name, field, values, def) \
1127 HA_xOPTION_ENUM(name, ha_index_option_struct, field, values, def)
1128#define HA_IOPTION_BOOL(name, field, def) \
1129 HA_xOPTION_BOOL(name, ha_index_option_struct, field, def)
1130#define HA_IOPTION_SYSVAR(name, field, sysvar) \
1131 HA_xOPTION_SYSVAR(name, ha_index_option_struct, field, sysvar)
1132#define HA_IOPTION_END HA_xOPTION_END
1133
1134typedef struct st_ha_create_table_option {
1135 enum ha_option_type type;
1136 const char *name;
1137 size_t name_length;
1138 ptrdiff_t offset;
1139 ulonglong def_value;
1140 ulonglong min_value, max_value, block_size;
1141 const char *values;
1142 struct st_mysql_sys_var *var;
1143} ha_create_table_option;
1144
1145enum handler_iterator_type
1146{
1147 /* request of transaction log iterator */
1148 HA_TRANSACTLOG_ITERATOR= 1
1149};
1150enum handler_create_iterator_result
1151{
1152 HA_ITERATOR_OK, /* iterator created */
1153 HA_ITERATOR_UNSUPPORTED, /* such type of iterator is not supported */
1154 HA_ITERATOR_ERROR /* error during iterator creation */
1155};
1156
1157/*
1158 Iterator structure. Can be used by handler/handlerton for different purposes.
1159
1160 Iterator should be created in the way to point "before" the first object
1161 it iterate, so next() call move it to the first object or return !=0 if
1162 there is nothing to iterate through.
1163*/
1164struct handler_iterator {
1165 /*
1166 Moves iterator to next record and return 0 or return !=0
1167 if there is no records.
1168 iterator_object will be filled by this function if next() returns 0.
1169 Content of the iterator_object depend on iterator type.
1170 */
1171 int (*next)(struct handler_iterator *, void *iterator_object);
1172 /*
1173 Free resources allocated by iterator, after this call iterator
1174 is not usable.
1175 */
1176 void (*destroy)(struct handler_iterator *);
1177 /*
1178 Pointer to buffer for the iterator to use.
1179 Should be allocated by function which created the iterator and
1180 destroied by freed by above "destroy" call
1181 */
1182 void *buffer;
1183};
1184
1185class handler;
1186class group_by_handler;
1187struct Query;
1188typedef class st_select_lex SELECT_LEX;
1189typedef struct st_order ORDER;
1190
1191/*
1192 handlerton is a singleton structure - one instance per storage engine -
1193 to provide access to storage engine functionality that works on the
1194 "global" level (unlike handler class that works on a per-table basis)
1195
1196 usually handlerton instance is defined statically in ha_xxx.cc as
1197
1198 static handlerton { ... } xxx_hton;
1199
1200 savepoint_*, prepare, recover, and *_by_xid pointers can be 0.
1201*/
1202struct handlerton
1203{
1204 /*
1205 Historical marker for if the engine is available of not
1206 */
1207 SHOW_COMP_OPTION state;
1208
1209 /*
1210 Historical number used for frm file to determine the correct
1211 storage engine. This is going away and new engines will just use
1212 "name" for this.
1213 */
1214 enum legacy_db_type db_type;
1215 /*
1216 each storage engine has it's own memory area (actually a pointer)
1217 in the thd, for storing per-connection information.
1218 It is accessed as
1219
1220 thd->ha_data[xxx_hton.slot]
1221
1222 slot number is initialized by MySQL after xxx_init() is called.
1223 */
1224 uint slot;
1225 /*
1226 to store per-savepoint data storage engine is provided with an area
1227 of a requested size (0 is ok here).
1228 savepoint_offset must be initialized statically to the size of
1229 the needed memory to store per-savepoint information.
1230 After xxx_init it is changed to be an offset to savepoint storage
1231 area and need not be used by storage engine.
1232 see binlog_hton and binlog_savepoint_set/rollback for an example.
1233 */
1234 uint savepoint_offset;
1235 /*
1236 handlerton methods:
1237
1238 close_connection is only called if
1239 thd->ha_data[xxx_hton.slot] is non-zero, so even if you don't need
1240 this storage area - set it to something, so that MySQL would know
1241 this storage engine was accessed in this connection
1242 */
1243 int (*close_connection)(handlerton *hton, THD *thd);
1244 /*
1245 Tell handler that query has been killed.
1246 */
1247 void (*kill_query)(handlerton *hton, THD *thd, enum thd_kill_levels level);
1248 /*
1249 sv points to an uninitialized storage area of requested size
1250 (see savepoint_offset description)
1251 */
1252 int (*savepoint_set)(handlerton *hton, THD *thd, void *sv);
1253 /*
1254 sv points to a storage area, that was earlier passed
1255 to the savepoint_set call
1256 */
1257 int (*savepoint_rollback)(handlerton *hton, THD *thd, void *sv);
1258 /**
1259 Check if storage engine allows to release metadata locks which were
1260 acquired after the savepoint if rollback to savepoint is done.
1261 @return true - If it is safe to release MDL locks.
1262 false - If it is not.
1263 */
1264 bool (*savepoint_rollback_can_release_mdl)(handlerton *hton, THD *thd);
1265 int (*savepoint_release)(handlerton *hton, THD *thd, void *sv);
1266 /*
1267 'all' is true if it's a real commit, that makes persistent changes
1268 'all' is false if it's not in fact a commit but an end of the
1269 statement that is part of the transaction.
1270 NOTE 'all' is also false in auto-commit mode where 'end of statement'
1271 and 'real commit' mean the same event.
1272 */
1273 int (*commit)(handlerton *hton, THD *thd, bool all);
1274 /*
1275 The commit_ordered() method is called prior to the commit() method, after
1276 the transaction manager has decided to commit (not rollback) the
1277 transaction. Unlike commit(), commit_ordered() is called only when the
1278 full transaction is committed, not for each commit of statement
1279 transaction in a multi-statement transaction.
1280
1281 Not that like prepare(), commit_ordered() is only called when 2-phase
1282 commit takes place. Ie. when no binary log and only a single engine
1283 participates in a transaction, one commit() is called, no
1284 commit_ordered(). So engines must be prepared for this.
1285
1286 The calls to commit_ordered() in multiple parallel transactions is
1287 guaranteed to happen in the same order in every participating
1288 handler. This can be used to ensure the same commit order among multiple
1289 handlers (eg. in table handler and binlog). So if transaction T1 calls
1290 into commit_ordered() of handler A before T2, then T1 will also call
1291 commit_ordered() of handler B before T2.
1292
1293 Engines that implement this method should during this call make the
1294 transaction visible to other transactions, thereby making the order of
1295 transaction commits be defined by the order of commit_ordered() calls.
1296
1297 The intention is that commit_ordered() should do the minimal amount of
1298 work that needs to happen in consistent commit order among handlers. To
1299 preserve ordering, calls need to be serialised on a global mutex, so
1300 doing any time-consuming or blocking operations in commit_ordered() will
1301 limit scalability.
1302
1303 Handlers can rely on commit_ordered() calls to be serialised (no two
1304 calls can run in parallel, so no extra locking on the handler part is
1305 required to ensure this).
1306
1307 Note that commit_ordered() can be called from a different thread than the
1308 one handling the transaction! So it can not do anything that depends on
1309 thread local storage, in particular it can not call my_error() and
1310 friends (instead it can store the error code and delay the call of
1311 my_error() to the commit() method).
1312
1313 Similarly, since commit_ordered() returns void, any return error code
1314 must be saved and returned from the commit() method instead.
1315
1316 The commit_ordered method is optional, and can be left unset if not
1317 needed in a particular handler (then there will be no ordering guarantees
1318 wrt. other engines and binary log).
1319 */
1320 void (*commit_ordered)(handlerton *hton, THD *thd, bool all);
1321 int (*rollback)(handlerton *hton, THD *thd, bool all);
1322 int (*prepare)(handlerton *hton, THD *thd, bool all);
1323 /*
1324 The prepare_ordered method is optional. If set, it will be called after
1325 successful prepare() in all handlers participating in 2-phase
1326 commit. Like commit_ordered(), it is called only when the full
1327 transaction is committed, not for each commit of statement transaction.
1328
1329 The calls to prepare_ordered() among multiple parallel transactions are
1330 ordered consistently with calls to commit_ordered(). This means that
1331 calls to prepare_ordered() effectively define the commit order, and that
1332 each handler will see the same sequence of transactions calling into
1333 prepare_ordered() and commit_ordered().
1334
1335 Thus, prepare_ordered() can be used to define commit order for handlers
1336 that need to do this in the prepare step (like binlog). It can also be
1337 used to release transaction's locks early in an order consistent with the
1338 order transactions will be eventually committed.
1339
1340 Like commit_ordered(), prepare_ordered() calls are serialised to maintain
1341 ordering, so the intention is that they should execute fast, with only
1342 the minimal amount of work needed to define commit order. Handlers can
1343 rely on this serialisation, and do not need to do any extra locking to
1344 avoid two prepare_ordered() calls running in parallel.
1345
1346 Like commit_ordered(), prepare_ordered() is not guaranteed to be called
1347 in the context of the thread handling the rest of the transaction. So it
1348 cannot invoke code that relies on thread local storage, in particular it
1349 cannot call my_error().
1350
1351 prepare_ordered() cannot cause a rollback by returning an error, all
1352 possible errors must be handled in prepare() (the prepare_ordered()
1353 method returns void). In case of some fatal error, a record of the error
1354 must be made internally by the engine and returned from commit() later.
1355
1356 Note that for user-level XA SQL commands, no consistent ordering among
1357 prepare_ordered() and commit_ordered() is guaranteed (as that would
1358 require blocking all other commits for an indefinite time).
1359
1360 When 2-phase commit is not used (eg. only one engine (and no binlog) in
1361 transaction), neither prepare() nor prepare_ordered() is called.
1362 */
1363 void (*prepare_ordered)(handlerton *hton, THD *thd, bool all);
1364 int (*recover)(handlerton *hton, XID *xid_list, uint len);
1365 int (*commit_by_xid)(handlerton *hton, XID *xid);
1366 int (*rollback_by_xid)(handlerton *hton, XID *xid);
1367 /*
1368 The commit_checkpoint_request() handlerton method is used to checkpoint
1369 the XA recovery process for storage engines that support two-phase
1370 commit.
1371
1372 The method is optional - an engine that does not implemented is expected
1373 to work the traditional way, where every commit() durably flushes the
1374 transaction to disk in the engine before completion, so XA recovery will
1375 no longer be needed for that transaction.
1376
1377 An engine that does implement commit_checkpoint_request() is also
1378 expected to implement commit_ordered(), so that ordering of commits is
1379 consistent between 2pc participants. Such engine is no longer required to
1380 durably flush to disk transactions in commit(), provided that the
1381 transaction has been successfully prepare()d and commit_ordered(); thus
1382 potentionally saving one fsync() call. (Engine must still durably flush
1383 to disk in commit() when no prepare()/commit_ordered() steps took place,
1384 at least if durable commits are wanted; this happens eg. if binlog is
1385 disabled).
1386
1387 The TC will periodically (eg. once per binlog rotation) call
1388 commit_checkpoint_request(). When this happens, the engine must arrange
1389 for all transaction that have completed commit_ordered() to be durably
1390 flushed to disk (this does not include transactions that might be in the
1391 middle of executing commit_ordered()). When such flush has completed, the
1392 engine must call commit_checkpoint_notify_ha(), passing back the opaque
1393 "cookie".
1394
1395 The flush and call of commit_checkpoint_notify_ha() need not happen
1396 immediately - it can be scheduled and performed asynchroneously (ie. as
1397 part of next prepare(), or sync every second, or whatever), but should
1398 not be postponed indefinitely. It is however also permissible to do it
1399 immediately, before returning from commit_checkpoint_request().
1400
1401 When commit_checkpoint_notify_ha() is called, the TC will know that the
1402 transactions are durably committed, and thus no longer require XA
1403 recovery. It uses that to reduce the work needed for any subsequent XA
1404 recovery process.
1405 */
1406 void (*commit_checkpoint_request)(handlerton *hton, void *cookie);
1407 /*
1408 "Disable or enable checkpointing internal to the storage engine. This is
1409 used for FLUSH TABLES WITH READ LOCK AND DISABLE CHECKPOINT to ensure that
1410 the engine will never start any recovery from a time between
1411 FLUSH TABLES ... ; UNLOCK TABLES.
1412
1413 While checkpointing is disabled, the engine should pause any background
1414 write activity (such as tablespace checkpointing) that require consistency
1415 between different files (such as transaction log and tablespace files) for
1416 crash recovery to succeed. The idea is to use this to make safe
1417 multi-volume LVM snapshot backups.
1418 */
1419 int (*checkpoint_state)(handlerton *hton, bool disabled);
1420 void *(*create_cursor_read_view)(handlerton *hton, THD *thd);
1421 void (*set_cursor_read_view)(handlerton *hton, THD *thd, void *read_view);
1422 void (*close_cursor_read_view)(handlerton *hton, THD *thd, void *read_view);
1423 handler *(*create)(handlerton *hton, TABLE_SHARE *table, MEM_ROOT *mem_root);
1424 void (*drop_database)(handlerton *hton, char* path);
1425 int (*panic)(handlerton *hton, enum ha_panic_function flag);
1426 int (*start_consistent_snapshot)(handlerton *hton, THD *thd);
1427 bool (*flush_logs)(handlerton *hton);
1428 bool (*show_status)(handlerton *hton, THD *thd, stat_print_fn *print, enum ha_stat_type stat);
1429 uint (*partition_flags)();
1430 alter_table_operations (*alter_table_flags)(alter_table_operations flags);
1431 int (*alter_tablespace)(handlerton *hton, THD *thd, st_alter_tablespace *ts_info);
1432 int (*fill_is_table)(handlerton *hton, THD *thd, TABLE_LIST *tables,
1433 class Item *cond,
1434 enum enum_schema_tables);
1435 uint32 flags; /* global handler flags */
1436 /*
1437 Those handlerton functions below are properly initialized at handler
1438 init.
1439 */
1440 int (*binlog_func)(handlerton *hton, THD *thd, enum_binlog_func fn, void *arg);
1441 void (*binlog_log_query)(handlerton *hton, THD *thd,
1442 enum_binlog_command binlog_command,
1443 const char *query, uint query_length,
1444 const char *db, const char *table_name);
1445
1446 /*
1447 Get log status.
1448 If log_status is null then the handler do not support transaction
1449 log information (i.e. log iterator can't be created).
1450 (see example of implementation in handler.cc, TRANS_LOG_MGM_EXAMPLE_CODE)
1451
1452 */
1453 enum log_status (*get_log_status)(handlerton *hton, char *log);
1454
1455 /*
1456 Iterators creator.
1457 Presence of the pointer should be checked before using
1458 */
1459 enum handler_create_iterator_result
1460 (*create_iterator)(handlerton *hton, enum handler_iterator_type type,
1461 struct handler_iterator *fill_this_in);
1462 int (*abort_transaction)(handlerton *hton, THD *bf_thd,
1463 THD *victim_thd, my_bool signal);
1464 int (*set_checkpoint)(handlerton *hton, const XID* xid);
1465 int (*get_checkpoint)(handlerton *hton, XID* xid);
1466 void (*fake_trx_id)(handlerton *hton, THD *thd);
1467 /*
1468 Optional clauses in the CREATE/ALTER TABLE
1469 */
1470 ha_create_table_option *table_options; // table level options
1471 ha_create_table_option *field_options; // these are specified per field
1472 ha_create_table_option *index_options; // these are specified per index
1473
1474 /**
1475 The list of extensions of files created for a single table in the
1476 database directory (datadir/db_name/).
1477
1478 Used by open_table_error(), by the default rename_table and delete_table
1479 handler methods, and by the default discovery implementation.
1480
1481 For engines that have more than one file name extentions (separate
1482 metadata, index, and/or data files), the order of elements is relevant.
1483 First element of engine file name extentions array should be metadata
1484 file extention. This is implied by the open_table_error()
1485 and the default discovery implementation.
1486
1487 Second element - data file extention. This is implied
1488 assumed by REPAIR TABLE ... USE_FRM implementation.
1489 */
1490 const char **tablefile_extensions; // by default - empty list
1491
1492 /**********************************************************************
1493 Functions to intercept queries
1494 **********************************************************************/
1495
1496 /*
1497 Create and return a group_by_handler, if the storage engine can execute
1498 the summary / group by query.
1499 If the storage engine can't do that, return NULL.
1500
1501 The server guaranteeds that all tables in the list belong to this
1502 storage engine.
1503 */
1504 group_by_handler *(*create_group_by)(THD *thd, Query *query);
1505
1506 /*********************************************************************
1507 Table discovery API.
1508 It allows the server to "discover" tables that exist in the storage
1509 engine, without user issuing an explicit CREATE TABLE statement.
1510 **********************************************************************/
1511
1512 /*
1513 This method is required for any engine that supports automatic table
1514 discovery, there is no default implementation.
1515
1516 Given a TABLE_SHARE discover_table() fills it in with a correct table
1517 structure using one of the TABLE_SHARE::init_from_* methods.
1518
1519 Returns HA_ERR_NO_SUCH_TABLE if the table did not exist in the engine,
1520 zero if the table was discovered successfully, or any other
1521 HA_ERR_* error code as appropriate if the table existed, but the
1522 discovery failed.
1523 */
1524 int (*discover_table)(handlerton *hton, THD* thd, TABLE_SHARE *share);
1525
1526 /*
1527 The discover_table_names method tells the server
1528 about all tables in the specified database that the engine
1529 knows about. Tables (or file names of tables) are added to
1530 the provided discovered_list collector object using
1531 add_table() or add_file() methods.
1532 */
1533 class discovered_list
1534 {
1535 public:
1536 virtual bool add_table(const char *tname, size_t tlen) = 0;
1537 virtual bool add_file(const char *fname) = 0;
1538 protected: virtual ~discovered_list() {}
1539 };
1540
1541 /*
1542 By default (if not implemented by the engine, but the discover_table() is
1543 implemented) it will perform a file-based discovery:
1544
1545 - if tablefile_extensions[0] is not null, this will discovers all tables
1546 with the tablefile_extensions[0] extension.
1547
1548 Returns 0 on success and 1 on error.
1549 */
1550 int (*discover_table_names)(handlerton *hton, LEX_CSTRING *db, MY_DIR *dir,
1551 discovered_list *result);
1552
1553 /*
1554 This is a method that allows to server to check if a table exists without
1555 an overhead of the complete discovery.
1556
1557 By default (if not implemented by the engine, but the discovery_table() is
1558 implemented) it will try to perform a file-based discovery:
1559
1560 - if tablefile_extensions[0] is not null this will look for a file name
1561 with the tablefile_extensions[0] extension.
1562
1563 - if tablefile_extensions[0] is null, this will resort to discover_table().
1564
1565 Note that resorting to discover_table() is slow and the engine
1566 should probably implement its own discover_table_existence() method,
1567 if its tablefile_extensions[0] is null.
1568
1569 Returns 1 if the table exists and 0 if it does not.
1570 */
1571 int (*discover_table_existence)(handlerton *hton, const char *db,
1572 const char *table_name);
1573
1574 /*
1575 This is the assisted table discovery method. Unlike the fully
1576 automatic discovery as above, here a user is expected to issue an
1577 explicit CREATE TABLE with the appropriate table attributes to
1578 "assist" the discovery of a table. But this "discovering" CREATE TABLE
1579 statement will not specify the table structure - the engine discovers
1580 it using this method. For example, FederatedX uses it in
1581
1582 CREATE TABLE t1 ENGINE=FEDERATED CONNECTION="mysql://foo/bar/t1";
1583
1584 Given a TABLE_SHARE discover_table_structure() fills it in with a correct
1585 table structure using one of the TABLE_SHARE::init_from_* methods.
1586
1587 Assisted discovery works independently from the automatic discover.
1588 An engine is allowed to support only assisted discovery and not
1589 support automatic one. Or vice versa.
1590 */
1591 int (*discover_table_structure)(handlerton *hton, THD* thd,
1592 TABLE_SHARE *share, HA_CREATE_INFO *info);
1593
1594 /*
1595 System Versioning
1596 */
1597 /** Determine if system-versioned data was modified by the transaction.
1598 @param[in,out] thd current session
1599 @param[out] trx_id transaction start ID
1600 @return transaction commit ID
1601 @retval 0 if no system-versioned data was affected by the transaction */
1602 ulonglong (*prepare_commit_versioned)(THD *thd, ulonglong *trx_id);
1603};
1604
1605
1606static inline LEX_CSTRING *hton_name(const handlerton *hton)
1607{
1608 return &(hton2plugin[hton->slot]->name);
1609}
1610
1611static inline handlerton *plugin_hton(plugin_ref plugin)
1612{
1613 return plugin_data(plugin, handlerton *);
1614}
1615
1616static inline sys_var *find_hton_sysvar(handlerton *hton, st_mysql_sys_var *var)
1617{
1618 return find_plugin_sysvar(hton2plugin[hton->slot], var);
1619}
1620
1621handlerton *ha_default_handlerton(THD *thd);
1622handlerton *ha_default_tmp_handlerton(THD *thd);
1623
1624/* Possible flags of a handlerton (there can be 32 of them) */
1625#define HTON_NO_FLAGS 0
1626#define HTON_CLOSE_CURSORS_AT_COMMIT (1 << 0)
1627#define HTON_ALTER_NOT_SUPPORTED (1 << 1) //Engine does not support alter
1628#define HTON_CAN_RECREATE (1 << 2) //Delete all is used for truncate
1629#define HTON_HIDDEN (1 << 3) //Engine does not appear in lists
1630#define HTON_NOT_USER_SELECTABLE (1 << 5)
1631#define HTON_TEMPORARY_NOT_SUPPORTED (1 << 6) //Having temporary tables not supported
1632#define HTON_SUPPORT_LOG_TABLES (1 << 7) //Engine supports log tables
1633#define HTON_NO_PARTITION (1 << 8) //Not partition of these tables
1634
1635/*
1636 This flag should be set when deciding that the engine does not allow
1637 row based binary logging (RBL) optimizations.
1638
1639 Currently, setting this flag, means that table's read/write_set will
1640 be left untouched when logging changes to tables in this engine. In
1641 practice this means that the server will not mess around with
1642 table->write_set and/or table->read_set when using RBL and deciding
1643 whether to log full or minimal rows.
1644
1645 It's valuable for instance for virtual tables, eg: Performance
1646 Schema which have no meaning for replication.
1647*/
1648#define HTON_NO_BINLOG_ROW_OPT (1 << 9)
1649#define HTON_SUPPORTS_EXTENDED_KEYS (1 <<10) //supports extended keys
1650#define HTON_NATIVE_SYS_VERSIONING (1 << 11) //Engine supports System Versioning
1651
1652// MySQL compatibility. Unused.
1653#define HTON_SUPPORTS_FOREIGN_KEYS (1 << 0) //Foreign key constraint supported.
1654
1655#define HTON_CAN_MERGE (1 <<11) //Merge type table
1656// Engine needs to access the main connect string in partitions
1657#define HTON_CAN_READ_CONNECT_STRING_IN_PARTITION (1 <<12)
1658
1659class Ha_trx_info;
1660
1661struct THD_TRANS
1662{
1663 /* true is not all entries in the ht[] support 2pc */
1664 bool no_2pc;
1665 /* storage engines that registered in this transaction */
1666 Ha_trx_info *ha_list;
1667 /*
1668 The purpose of this flag is to keep track of non-transactional
1669 tables that were modified in scope of:
1670 - transaction, when the variable is a member of
1671 THD::transaction.all
1672 - top-level statement or sub-statement, when the variable is a
1673 member of THD::transaction.stmt
1674 This member has the following life cycle:
1675 * stmt.modified_non_trans_table is used to keep track of
1676 modified non-transactional tables of top-level statements. At
1677 the end of the previous statement and at the beginning of the session,
1678 it is reset to FALSE. If such functions
1679 as mysql_insert, mysql_update, mysql_delete etc modify a
1680 non-transactional table, they set this flag to TRUE. At the
1681 end of the statement, the value of stmt.modified_non_trans_table
1682 is merged with all.modified_non_trans_table and gets reset.
1683 * all.modified_non_trans_table is reset at the end of transaction
1684
1685 * Since we do not have a dedicated context for execution of a
1686 sub-statement, to keep track of non-transactional changes in a
1687 sub-statement, we re-use stmt.modified_non_trans_table.
1688 At entrance into a sub-statement, a copy of the value of
1689 stmt.modified_non_trans_table (containing the changes of the
1690 outer statement) is saved on stack. Then
1691 stmt.modified_non_trans_table is reset to FALSE and the
1692 substatement is executed. Then the new value is merged with the
1693 saved value.
1694 */
1695 bool modified_non_trans_table;
1696
1697 void reset() {
1698 no_2pc= FALSE;
1699 modified_non_trans_table= FALSE;
1700 m_unsafe_rollback_flags= 0;
1701 }
1702 bool is_empty() const { return ha_list == NULL; }
1703 THD_TRANS() {} /* Remove gcc warning */
1704
1705 unsigned int m_unsafe_rollback_flags;
1706 /*
1707 Define the type of statements which cannot be rolled back safely.
1708 Each type occupies one bit in m_unsafe_rollback_flags.
1709 */
1710 enum unsafe_statement_types
1711 {
1712 CREATED_TEMP_TABLE= 2,
1713 DROPPED_TEMP_TABLE= 4,
1714 DID_WAIT= 8,
1715 DID_DDL= 0x10
1716 };
1717
1718 void mark_created_temp_table()
1719 {
1720 DBUG_PRINT("debug", ("mark_created_temp_table"));
1721 m_unsafe_rollback_flags|= CREATED_TEMP_TABLE;
1722 }
1723 void mark_dropped_temp_table()
1724 {
1725 DBUG_PRINT("debug", ("mark_dropped_temp_table"));
1726 m_unsafe_rollback_flags|= DROPPED_TEMP_TABLE;
1727 }
1728 bool has_created_dropped_temp_table() const {
1729 return
1730 (m_unsafe_rollback_flags & (CREATED_TEMP_TABLE|DROPPED_TEMP_TABLE)) != 0;
1731 }
1732 void mark_trans_did_wait() { m_unsafe_rollback_flags|= DID_WAIT; }
1733 bool trans_did_wait() const {
1734 return (m_unsafe_rollback_flags & DID_WAIT) != 0;
1735 }
1736 bool is_trx_read_write() const;
1737 void mark_trans_did_ddl() { m_unsafe_rollback_flags|= DID_DDL; }
1738 bool trans_did_ddl() const {
1739 return (m_unsafe_rollback_flags & DID_DDL) != 0;
1740 }
1741
1742};
1743
1744
1745/**
1746 Either statement transaction or normal transaction - related
1747 thread-specific storage engine data.
1748
1749 If a storage engine participates in a statement/transaction,
1750 an instance of this class is present in
1751 thd->transaction.{stmt|all}.ha_list. The addition to
1752 {stmt|all}.ha_list is made by trans_register_ha().
1753
1754 When it's time to commit or rollback, each element of ha_list
1755 is used to access storage engine's prepare()/commit()/rollback()
1756 methods, and also to evaluate if a full two phase commit is
1757 necessary.
1758
1759 @sa General description of transaction handling in handler.cc.
1760*/
1761
1762class Ha_trx_info
1763{
1764public:
1765 /** Register this storage engine in the given transaction context. */
1766 void register_ha(THD_TRANS *trans, handlerton *ht_arg)
1767 {
1768 DBUG_ASSERT(m_flags == 0);
1769 DBUG_ASSERT(m_ht == NULL);
1770 DBUG_ASSERT(m_next == NULL);
1771
1772 m_ht= ht_arg;
1773 m_flags= (int) TRX_READ_ONLY; /* Assume read-only at start. */
1774
1775 m_next= trans->ha_list;
1776 trans->ha_list= this;
1777 }
1778
1779 /** Clear, prepare for reuse. */
1780 void reset()
1781 {
1782 m_next= NULL;
1783 m_ht= NULL;
1784 m_flags= 0;
1785 }
1786
1787 Ha_trx_info() { reset(); }
1788
1789 void set_trx_read_write()
1790 {
1791 DBUG_ASSERT(is_started());
1792 m_flags|= (int) TRX_READ_WRITE;
1793 }
1794 bool is_trx_read_write() const
1795 {
1796 DBUG_ASSERT(is_started());
1797 return m_flags & (int) TRX_READ_WRITE;
1798 }
1799 bool is_started() const { return m_ht != NULL; }
1800 /** Mark this transaction read-write if the argument is read-write. */
1801 void coalesce_trx_with(const Ha_trx_info *stmt_trx)
1802 {
1803 /*
1804 Must be called only after the transaction has been started.
1805 Can be called many times, e.g. when we have many
1806 read-write statements in a transaction.
1807 */
1808 DBUG_ASSERT(is_started());
1809 if (stmt_trx->is_trx_read_write())
1810 set_trx_read_write();
1811 }
1812 Ha_trx_info *next() const
1813 {
1814 DBUG_ASSERT(is_started());
1815 return m_next;
1816 }
1817 handlerton *ht() const
1818 {
1819 DBUG_ASSERT(is_started());
1820 return m_ht;
1821 }
1822private:
1823 enum { TRX_READ_ONLY= 0, TRX_READ_WRITE= 1 };
1824 /** Auxiliary, used for ha_list management */
1825 Ha_trx_info *m_next;
1826 /**
1827 Although a given Ha_trx_info instance is currently always used
1828 for the same storage engine, 'ht' is not-NULL only when the
1829 corresponding storage is a part of a transaction.
1830 */
1831 handlerton *m_ht;
1832 /**
1833 Transaction flags related to this engine.
1834 Not-null only if this instance is a part of transaction.
1835 May assume a combination of enum values above.
1836 */
1837 uchar m_flags;
1838};
1839
1840
1841inline bool THD_TRANS::is_trx_read_write() const
1842{
1843 Ha_trx_info *ha_info;
1844 for (ha_info= ha_list; ha_info; ha_info= ha_info->next())
1845 if (ha_info->is_trx_read_write())
1846 return TRUE;
1847 return FALSE;
1848}
1849
1850
1851enum enum_tx_isolation { ISO_READ_UNCOMMITTED, ISO_READ_COMMITTED,
1852 ISO_REPEATABLE_READ, ISO_SERIALIZABLE};
1853
1854
1855typedef struct {
1856 ulonglong data_file_length;
1857 ulonglong max_data_file_length;
1858 ulonglong index_file_length;
1859 ulonglong max_index_file_length;
1860 ulonglong delete_length;
1861 ha_rows records;
1862 ulong mean_rec_length;
1863 time_t create_time;
1864 time_t check_time;
1865 time_t update_time;
1866 ulonglong check_sum;
1867} PARTITION_STATS;
1868
1869#define UNDEF_NODEGROUP 65535
1870class Item;
1871struct st_table_log_memory_entry;
1872
1873class partition_info;
1874
1875struct st_partition_iter;
1876
1877enum ha_choice { HA_CHOICE_UNDEF, HA_CHOICE_NO, HA_CHOICE_YES, HA_CHOICE_MAX };
1878
1879enum enum_stats_auto_recalc { HA_STATS_AUTO_RECALC_DEFAULT= 0,
1880 HA_STATS_AUTO_RECALC_ON,
1881 HA_STATS_AUTO_RECALC_OFF };
1882
1883/**
1884 A helper struct for schema DDL statements:
1885 CREATE SCHEMA [IF NOT EXISTS] name [ schema_specification... ]
1886 ALTER SCHEMA name [ schema_specification... ]
1887
1888 It stores the "schema_specification" part of the CREATE/ALTER statements and
1889 is passed to mysql_create_db() and mysql_alter_db().
1890 Currently consists only of the schema default character set and collation.
1891*/
1892struct Schema_specification_st
1893{
1894 CHARSET_INFO *default_table_charset;
1895 void init()
1896 {
1897 bzero(this, sizeof(*this));
1898 }
1899};
1900
1901class Create_field;
1902
1903enum vers_sys_type_t
1904{
1905 VERS_UNDEFINED= 0,
1906 VERS_TIMESTAMP,
1907 VERS_TRX_ID
1908};
1909
1910struct Vers_parse_info
1911{
1912 Vers_parse_info() :
1913 check_unit(VERS_UNDEFINED),
1914 versioned_fields(false),
1915 unversioned_fields(false)
1916 {}
1917
1918 struct start_end_t
1919 {
1920 start_end_t()
1921 {}
1922 start_end_t(LEX_CSTRING _start, LEX_CSTRING _end) :
1923 start(_start),
1924 end(_end) {}
1925 Lex_ident start;
1926 Lex_ident end;
1927 };
1928
1929 start_end_t system_time;
1930 start_end_t as_row;
1931 vers_sys_type_t check_unit;
1932
1933 void set_system_time(Lex_ident start, Lex_ident end)
1934 {
1935 system_time.start= start;
1936 system_time.end= end;
1937 }
1938
1939protected:
1940 friend struct Table_scope_and_contents_source_st;
1941 void set_start(const LEX_CSTRING field_name)
1942 {
1943 as_row.start= field_name;
1944 system_time.start= field_name;
1945 }
1946 void set_end(const LEX_CSTRING field_name)
1947 {
1948 as_row.end= field_name;
1949 system_time.end= field_name;
1950 }
1951 bool is_start(const char *name) const;
1952 bool is_end(const char *name) const;
1953 bool is_start(const Create_field &f) const;
1954 bool is_end(const Create_field &f) const;
1955 bool fix_implicit(THD *thd, Alter_info *alter_info);
1956 operator bool() const
1957 {
1958 return as_row.start || as_row.end || system_time.start || system_time.end;
1959 }
1960 bool need_check(const Alter_info *alter_info) const;
1961 bool check_conditions(const Lex_table_name &table_name,
1962 const Lex_table_name &db) const;
1963public:
1964 static const Lex_ident default_start;
1965 static const Lex_ident default_end;
1966
1967 bool fix_alter_info(THD *thd, Alter_info *alter_info,
1968 HA_CREATE_INFO *create_info, TABLE *table);
1969 bool fix_create_like(Alter_info &alter_info, HA_CREATE_INFO &create_info,
1970 TABLE_LIST &src_table, TABLE_LIST &table);
1971 bool check_sys_fields(const Lex_table_name &table_name,
1972 const Lex_table_name &db,
1973 Alter_info *alter_info, bool native);
1974
1975 /**
1976 At least one field was specified 'WITH/WITHOUT SYSTEM VERSIONING'.
1977 Useful for error handling.
1978 */
1979 bool versioned_fields : 1;
1980 bool unversioned_fields : 1;
1981};
1982
1983/**
1984 A helper struct for table DDL statements, e.g.:
1985 CREATE [OR REPLACE] [TEMPORARY]
1986 TABLE [IF NOT EXISTS] tbl_name table_contents_source;
1987
1988 Represents a combinations of:
1989 1. The scope, i.e. TEMPORARY or not TEMPORARY
1990 2. The "table_contents_source" part of the table DDL statements,
1991 which can be initialized from either of these:
1992 - table_element_list ... // Explicit definition (column and key list)
1993 - LIKE another_table_name ... // Copy structure from another table
1994 - [AS] SELECT ... // Copy structure from a subquery
1995*/
1996
1997struct Table_scope_and_contents_source_st
1998{
1999 CHARSET_INFO *table_charset;
2000 LEX_CUSTRING tabledef_version;
2001 LEX_CSTRING connect_string;
2002 LEX_CSTRING comment;
2003 LEX_CSTRING alias;
2004 const char *password, *tablespace;
2005 const char *data_file_name, *index_file_name;
2006 ulonglong max_rows,min_rows;
2007 ulonglong auto_increment_value;
2008 ulong table_options; ///< HA_OPTION_ values
2009 ulong avg_row_length;
2010 ulong used_fields;
2011 ulong key_block_size;
2012 ulong expression_length;
2013 ulong field_check_constraints;
2014 /*
2015 number of pages to sample during
2016 stats estimation, if used, otherwise 0.
2017 */
2018 uint stats_sample_pages;
2019 uint null_bits; /* NULL bits at start of record */
2020 uint options; /* OR of HA_CREATE_ options */
2021 uint merge_insert_method;
2022 uint extra_size; /* length of extra data segment */
2023 SQL_I_List<TABLE_LIST> merge_list;
2024 handlerton *db_type;
2025 /**
2026 Row type of the table definition.
2027
2028 Defaults to ROW_TYPE_DEFAULT for all non-ALTER statements.
2029 For ALTER TABLE defaults to ROW_TYPE_NOT_USED (means "keep the current").
2030
2031 Can be changed either explicitly by the parser.
2032 If nothing specified inherits the value of the original table (if present).
2033 */
2034 enum row_type row_type;
2035 enum ha_choice transactional;
2036 enum ha_storage_media storage_media; ///< DEFAULT, DISK or MEMORY
2037 enum ha_choice page_checksum; ///< If we have page_checksums
2038 engine_option_value *option_list; ///< list of table create options
2039 enum_stats_auto_recalc stats_auto_recalc;
2040 bool varchar; ///< 1 if table has a VARCHAR
2041 bool sequence; // If SEQUENCE=1 was used
2042
2043 List<Virtual_column_info> *check_constraint_list;
2044
2045 /* the following three are only for ALTER TABLE, check_if_incompatible_data() */
2046 ha_table_option_struct *option_struct; ///< structure with parsed table options
2047 ha_field_option_struct **fields_option_struct; ///< array of field option structures
2048 ha_index_option_struct **indexes_option_struct; ///< array of index option structures
2049
2050 /* The following is used to remember the old state for CREATE OR REPLACE */
2051 TABLE *table;
2052 TABLE_LIST *pos_in_locked_tables;
2053 MDL_ticket *mdl_ticket;
2054 bool table_was_deleted;
2055 sequence_definition *seq_create_info;
2056
2057 Vers_parse_info vers_info;
2058
2059 bool vers_fix_system_fields(THD *thd, Alter_info *alter_info,
2060 const TABLE_LIST &create_table,
2061 bool create_select= false);
2062
2063 bool vers_check_system_fields(THD *thd, Alter_info *alter_info,
2064 const TABLE_LIST &create_table);
2065
2066 bool vers_native(THD *thd) const;
2067
2068 void init()
2069 {
2070 bzero(this, sizeof(*this));
2071 }
2072 bool tmp_table() const { return options & HA_LEX_CREATE_TMP_TABLE; }
2073 void use_default_db_type(THD *thd)
2074 {
2075 db_type= tmp_table() ? ha_default_tmp_handlerton(thd)
2076 : ha_default_handlerton(thd);
2077 }
2078
2079 bool versioned() const
2080 {
2081 return options & HA_VERSIONED_TABLE;
2082 }
2083};
2084
2085
2086/**
2087 This struct is passed to handler table routines, e.g. ha_create().
2088 It does not include the "OR REPLACE" and "IF NOT EXISTS" parts, as these
2089 parts are handled on the SQL level and are not needed on the handler level.
2090*/
2091struct HA_CREATE_INFO: public Table_scope_and_contents_source_st,
2092 public Schema_specification_st
2093{
2094 void init()
2095 {
2096 Table_scope_and_contents_source_st::init();
2097 Schema_specification_st::init();
2098 }
2099 bool check_conflicting_charset_declarations(CHARSET_INFO *cs);
2100 bool add_table_option_default_charset(CHARSET_INFO *cs)
2101 {
2102 // cs can be NULL, e.g.: CREATE TABLE t1 (..) CHARACTER SET DEFAULT;
2103 if (check_conflicting_charset_declarations(cs))
2104 return true;
2105 default_table_charset= cs;
2106 used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
2107 return false;
2108 }
2109 bool add_alter_list_item_convert_to_charset(CHARSET_INFO *cs)
2110 {
2111 /*
2112 cs cannot be NULL, as sql_yacc.yy translates
2113 CONVERT TO CHARACTER SET DEFAULT
2114 to
2115 CONVERT TO CHARACTER SET <character-set-of-the-current-database>
2116 TODO: Should't we postpone resolution of DEFAULT until the
2117 character set of the table owner database is loaded from its db.opt?
2118 */
2119 DBUG_ASSERT(cs);
2120 if (check_conflicting_charset_declarations(cs))
2121 return true;
2122 table_charset= default_table_charset= cs;
2123 used_fields|= (HA_CREATE_USED_CHARSET | HA_CREATE_USED_DEFAULT_CHARSET);
2124 return false;
2125 }
2126 ulong table_options_with_row_type()
2127 {
2128 if (row_type == ROW_TYPE_DYNAMIC || row_type == ROW_TYPE_PAGE)
2129 return table_options | HA_OPTION_PACK_RECORD;
2130 else
2131 return table_options;
2132 }
2133};
2134
2135
2136/**
2137 This struct is passed to mysql_create_table() and similar creation functions,
2138 as well as to show_create_table().
2139*/
2140struct Table_specification_st: public HA_CREATE_INFO,
2141 public DDL_options_st
2142{
2143 // Deep initialization
2144 void init()
2145 {
2146 HA_CREATE_INFO::init();
2147 DDL_options_st::init();
2148 }
2149 void init(DDL_options_st::Options options_arg)
2150 {
2151 HA_CREATE_INFO::init();
2152 DDL_options_st::init(options_arg);
2153 }
2154 /*
2155 Quick initialization, for parser.
2156 Most of the HA_CREATE_INFO is left uninitialized.
2157 It gets fully initialized in sql_yacc.yy, only when the parser
2158 scans a related keyword (e.g. CREATE, ALTER).
2159 */
2160 void lex_start()
2161 {
2162 HA_CREATE_INFO::options= 0;
2163 DDL_options_st::init();
2164 }
2165};
2166
2167
2168/**
2169 In-place alter handler context.
2170
2171 This is a superclass intended to be subclassed by individual handlers
2172 in order to store handler unique context between in-place alter API calls.
2173
2174 The handler is responsible for creating the object. This can be done
2175 as early as during check_if_supported_inplace_alter().
2176
2177 The SQL layer is responsible for destroying the object.
2178 The class extends Sql_alloc so the memory will be mem root allocated.
2179
2180 @see Alter_inplace_info
2181*/
2182
2183class inplace_alter_handler_ctx : public Sql_alloc
2184{
2185public:
2186 inplace_alter_handler_ctx() {}
2187
2188 virtual ~inplace_alter_handler_ctx() {}
2189};
2190
2191
2192/**
2193 Class describing changes to be done by ALTER TABLE.
2194 Instance of this class is passed to storage engine in order
2195 to determine if this ALTER TABLE can be done using in-place
2196 algorithm. It is also used for executing the ALTER TABLE
2197 using in-place algorithm.
2198*/
2199
2200class Alter_inplace_info
2201{
2202public:
2203
2204 /**
2205 Create options (like MAX_ROWS) for the new version of table.
2206
2207 @note The referenced instance of HA_CREATE_INFO object was already
2208 used to create new .FRM file for table being altered. So it
2209 has been processed by mysql_prepare_create_table() already.
2210 For example, this means that it has HA_OPTION_PACK_RECORD
2211 flag in HA_CREATE_INFO::table_options member correctly set.
2212 */
2213 HA_CREATE_INFO *create_info;
2214
2215 /**
2216 Alter options, fields and keys for the new version of table.
2217
2218 @note The referenced instance of Alter_info object was already
2219 used to create new .FRM file for table being altered. So it
2220 has been processed by mysql_prepare_create_table() already.
2221 In particular, this means that in Create_field objects for
2222 fields which were present in some form in the old version
2223 of table, Create_field::field member points to corresponding
2224 Field instance for old version of table.
2225 */
2226 Alter_info *alter_info;
2227
2228 /**
2229 Array of KEYs for new version of table - including KEYs to be added.
2230
2231 @note Currently this array is produced as result of
2232 mysql_prepare_create_table() call.
2233 This means that it follows different convention for
2234 KEY_PART_INFO::fieldnr values than objects in TABLE::key_info
2235 array.
2236
2237 @todo This is mainly due to the fact that we need to keep compatibility
2238 with removed handler::add_index() call. We plan to switch to
2239 TABLE::key_info numbering later.
2240
2241 KEYs are sorted - see sort_keys().
2242 */
2243 KEY *key_info_buffer;
2244
2245 /** Size of key_info_buffer array. */
2246 uint key_count;
2247
2248 /** Size of index_drop_buffer array. */
2249 uint index_drop_count;
2250
2251 /**
2252 Array of pointers to KEYs to be dropped belonging to the TABLE instance
2253 for the old version of the table.
2254 */
2255 KEY **index_drop_buffer;
2256
2257 /** Size of index_add_buffer array. */
2258 uint index_add_count;
2259
2260 /**
2261 Array of indexes into key_info_buffer for KEYs to be added,
2262 sorted in increasing order.
2263 */
2264 uint *index_add_buffer;
2265
2266 /**
2267 Context information to allow handlers to keep context between in-place
2268 alter API calls.
2269
2270 @see inplace_alter_handler_ctx for information about object lifecycle.
2271 */
2272 inplace_alter_handler_ctx *handler_ctx;
2273
2274 /**
2275 If the table uses several handlers, like ha_partition uses one handler
2276 per partition, this contains a Null terminated array of ctx pointers
2277 that should all be committed together.
2278 Or NULL if only handler_ctx should be committed.
2279 Set to NULL if the low level handler::commit_inplace_alter_table uses it,
2280 to signal to the main handler that everything was committed as atomically.
2281
2282 @see inplace_alter_handler_ctx for information about object lifecycle.
2283 */
2284 inplace_alter_handler_ctx **group_commit_ctx;
2285
2286 /**
2287 Flags describing in detail which operations the storage engine is to
2288 execute. Flags are defined in sql_alter.h
2289 */
2290 alter_table_operations handler_flags;
2291
2292 /* Alter operations involving parititons are strored here */
2293 ulong partition_flags;
2294
2295 /**
2296 Partition_info taking into account the partition changes to be performed.
2297 Contains all partitions which are present in the old version of the table
2298 with partitions to be dropped or changed marked as such + all partitions
2299 to be added in the new version of table marked as such.
2300 */
2301 partition_info *modified_part_info;
2302
2303 /** true for ALTER IGNORE TABLE ... */
2304 const bool ignore;
2305
2306 /** true for online operation (LOCK=NONE) */
2307 bool online;
2308
2309 /**
2310 Can be set by handler to describe why a given operation cannot be done
2311 in-place (HA_ALTER_INPLACE_NOT_SUPPORTED) or why it cannot be done
2312 online (HA_ALTER_INPLACE_NO_LOCK or HA_ALTER_INPLACE_COPY_NO_LOCK)
2313 If set, it will be used with ER_ALTER_OPERATION_NOT_SUPPORTED_REASON if
2314 results from handler::check_if_supported_inplace_alter() doesn't match
2315 requirements set by user. If not set, the more generic
2316 ER_ALTER_OPERATION_NOT_SUPPORTED will be used.
2317
2318 Please set to a properly localized string, for example using
2319 my_get_err_msg(), so that the error message as a whole is localized.
2320 */
2321 const char *unsupported_reason;
2322
2323 Alter_inplace_info(HA_CREATE_INFO *create_info_arg,
2324 Alter_info *alter_info_arg,
2325 KEY *key_info_arg, uint key_count_arg,
2326 partition_info *modified_part_info_arg,
2327 bool ignore_arg)
2328 : create_info(create_info_arg),
2329 alter_info(alter_info_arg),
2330 key_info_buffer(key_info_arg),
2331 key_count(key_count_arg),
2332 index_drop_count(0),
2333 index_drop_buffer(NULL),
2334 index_add_count(0),
2335 index_add_buffer(NULL),
2336 handler_ctx(NULL),
2337 group_commit_ctx(NULL),
2338 handler_flags(0),
2339 modified_part_info(modified_part_info_arg),
2340 ignore(ignore_arg),
2341 online(false),
2342 unsupported_reason(NULL)
2343 {}
2344
2345 ~Alter_inplace_info()
2346 {
2347 delete handler_ctx;
2348 }
2349
2350 /**
2351 Used after check_if_supported_inplace_alter() to report
2352 error if the result does not match the LOCK/ALGORITHM
2353 requirements set by the user.
2354
2355 @param not_supported Part of statement that was not supported.
2356 @param try_instead Suggestion as to what the user should
2357 replace not_supported with.
2358 */
2359 void report_unsupported_error(const char *not_supported,
2360 const char *try_instead) const;
2361};
2362
2363
2364typedef struct st_key_create_information
2365{
2366 enum ha_key_alg algorithm;
2367 ulong block_size;
2368 uint flags; /* HA_USE.. flags */
2369 LEX_CSTRING parser_name;
2370 LEX_CSTRING comment;
2371 /**
2372 A flag to determine if we will check for duplicate indexes.
2373 This typically means that the key information was specified
2374 directly by the user (set by the parser).
2375 */
2376 bool check_for_duplicate_indexes;
2377} KEY_CREATE_INFO;
2378
2379
2380/*
2381 Class for maintaining hooks used inside operations on tables such
2382 as: create table functions, delete table functions, and alter table
2383 functions.
2384
2385 Class is using the Template Method pattern to separate the public
2386 usage interface from the private inheritance interface. This
2387 imposes no overhead, since the public non-virtual function is small
2388 enough to be inlined.
2389
2390 The hooks are usually used for functions that does several things,
2391 e.g., create_table_from_items(), which both create a table and lock
2392 it.
2393 */
2394class TABLEOP_HOOKS
2395{
2396public:
2397 TABLEOP_HOOKS() {}
2398 virtual ~TABLEOP_HOOKS() {}
2399
2400 inline void prelock(TABLE **tables, uint count)
2401 {
2402 do_prelock(tables, count);
2403 }
2404
2405 inline int postlock(TABLE **tables, uint count)
2406 {
2407 return do_postlock(tables, count);
2408 }
2409private:
2410 /* Function primitive that is called prior to locking tables */
2411 virtual void do_prelock(TABLE **tables, uint count)
2412 {
2413 /* Default is to do nothing */
2414 }
2415
2416 /**
2417 Primitive called after tables are locked.
2418
2419 If an error is returned, the tables will be unlocked and error
2420 handling start.
2421
2422 @return Error code or zero.
2423 */
2424 virtual int do_postlock(TABLE **tables, uint count)
2425 {
2426 return 0; /* Default is to do nothing */
2427 }
2428};
2429
2430typedef struct st_savepoint SAVEPOINT;
2431extern ulong savepoint_alloc_size;
2432extern KEY_CREATE_INFO default_key_create_info;
2433
2434/* Forward declaration for condition pushdown to storage engine */
2435typedef class Item COND;
2436
2437typedef struct st_ha_check_opt
2438{
2439 st_ha_check_opt() {} /* Remove gcc warning */
2440 uint flags; /* isam layer flags (e.g. for myisamchk) */
2441 uint sql_flags; /* sql layer flags - for something myisamchk cannot do */
2442 time_t start_time; /* When check/repair starts */
2443 KEY_CACHE *key_cache; /* new key cache when changing key cache */
2444 void init();
2445} HA_CHECK_OPT;
2446
2447
2448/********************************************************************************
2449 * MRR
2450 ********************************************************************************/
2451
2452typedef void *range_seq_t;
2453
2454typedef struct st_range_seq_if
2455{
2456 /*
2457 Get key information
2458
2459 SYNOPSIS
2460 get_key_info()
2461 init_params The seq_init_param parameter
2462 length OUT length of the keys in this range sequence
2463 map OUT key_part_map of the keys in this range sequence
2464
2465 DESCRIPTION
2466 This function is set only when using HA_MRR_FIXED_KEY mode. In that mode,
2467 all ranges are single-point equality ranges that use the same set of key
2468 parts. This function allows the MRR implementation to get the length of
2469 a key, and which keyparts it uses.
2470 */
2471 void (*get_key_info)(void *init_params, uint *length, key_part_map *map);
2472
2473 /*
2474 Initialize the traversal of range sequence
2475
2476 SYNOPSIS
2477 init()
2478 init_params The seq_init_param parameter
2479 n_ranges The number of ranges obtained
2480 flags A combination of HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
2481
2482 RETURN
2483 An opaque value to be used as RANGE_SEQ_IF::next() parameter
2484 */
2485 range_seq_t (*init)(void *init_params, uint n_ranges, uint flags);
2486
2487
2488 /*
2489 Get the next range in the range sequence
2490
2491 SYNOPSIS
2492 next()
2493 seq The value returned by RANGE_SEQ_IF::init()
2494 range OUT Information about the next range
2495
2496 RETURN
2497 FALSE - Ok, the range structure filled with info about the next range
2498 TRUE - No more ranges
2499 */
2500 bool (*next) (range_seq_t seq, KEY_MULTI_RANGE *range);
2501
2502 /*
2503 Check whether range_info orders to skip the next record
2504
2505 SYNOPSIS
2506 skip_record()
2507 seq The value returned by RANGE_SEQ_IF::init()
2508 range_info Information about the next range
2509 (Ignored if MRR_NO_ASSOCIATION is set)
2510 rowid Rowid of the record to be checked (ignored if set to 0)
2511
2512 RETURN
2513 1 - Record with this range_info and/or this rowid shall be filtered
2514 out from the stream of records returned by multi_range_read_next()
2515 0 - The record shall be left in the stream
2516 */
2517 bool (*skip_record) (range_seq_t seq, range_id_t range_info, uchar *rowid);
2518
2519 /*
2520 Check if the record combination matches the index condition
2521 SYNOPSIS
2522 skip_index_tuple()
2523 seq The value returned by RANGE_SEQ_IF::init()
2524 range_info Information about the next range
2525
2526 RETURN
2527 0 - The record combination satisfies the index condition
2528 1 - Otherwise
2529 */
2530 bool (*skip_index_tuple) (range_seq_t seq, range_id_t range_info);
2531} RANGE_SEQ_IF;
2532
2533typedef bool (*SKIP_INDEX_TUPLE_FUNC) (range_seq_t seq, range_id_t range_info);
2534
2535class Cost_estimate
2536{
2537public:
2538 double io_count; /* number of I/O */
2539 double avg_io_cost; /* cost of an average I/O oper. */
2540 double cpu_cost; /* cost of operations in CPU */
2541 double import_cost; /* cost of remote operations */
2542 double mem_cost; /* cost of used memory */
2543
2544 enum { IO_COEFF=1 };
2545 enum { CPU_COEFF=1 };
2546 enum { MEM_COEFF=1 };
2547 enum { IMPORT_COEFF=1 };
2548
2549 Cost_estimate()
2550 {
2551 reset();
2552 }
2553
2554 double total_cost()
2555 {
2556 return IO_COEFF*io_count*avg_io_cost + CPU_COEFF * cpu_cost +
2557 MEM_COEFF*mem_cost + IMPORT_COEFF*import_cost;
2558 }
2559
2560 /**
2561 Whether or not all costs in the object are zero
2562
2563 @return true if all costs are zero, false otherwise
2564 */
2565 bool is_zero() const
2566 {
2567 return io_count == 0.0 && cpu_cost == 0.0 &&
2568 import_cost == 0.0 && mem_cost == 0.0;
2569 }
2570
2571 void reset()
2572 {
2573 avg_io_cost= 1.0;
2574 io_count= cpu_cost= mem_cost= import_cost= 0.0;
2575 }
2576
2577 void multiply(double m)
2578 {
2579 io_count *= m;
2580 cpu_cost *= m;
2581 import_cost *= m;
2582 /* Don't multiply mem_cost */
2583 }
2584
2585 void add(const Cost_estimate* cost)
2586 {
2587 double io_count_sum= io_count + cost->io_count;
2588 add_io(cost->io_count, cost->avg_io_cost);
2589 io_count= io_count_sum;
2590 cpu_cost += cost->cpu_cost;
2591 }
2592
2593 void add_io(double add_io_cnt, double add_avg_cost)
2594 {
2595 /* In edge cases add_io_cnt may be zero */
2596 if (add_io_cnt > 0)
2597 {
2598 double io_count_sum= io_count + add_io_cnt;
2599 avg_io_cost= (io_count * avg_io_cost +
2600 add_io_cnt * add_avg_cost) / io_count_sum;
2601 io_count= io_count_sum;
2602 }
2603 }
2604
2605 /// Add to CPU cost
2606 void add_cpu(double add_cpu_cost) { cpu_cost+= add_cpu_cost; }
2607
2608 /// Add to import cost
2609 void add_import(double add_import_cost) { import_cost+= add_import_cost; }
2610
2611 /// Add to memory cost
2612 void add_mem(double add_mem_cost) { mem_cost+= add_mem_cost; }
2613
2614 /*
2615 To be used when we go from old single value-based cost calculations to
2616 the new Cost_estimate-based.
2617 */
2618 void convert_from_cost(double cost)
2619 {
2620 reset();
2621 io_count= cost;
2622 }
2623};
2624
2625void get_sweep_read_cost(TABLE *table, ha_rows nrows, bool interrupted,
2626 Cost_estimate *cost);
2627
2628/*
2629 Indicates that all scanned ranges will be singlepoint (aka equality) ranges.
2630 The ranges may not use the full key but all of them will use the same number
2631 of key parts.
2632*/
2633#define HA_MRR_SINGLE_POINT 1U
2634#define HA_MRR_FIXED_KEY 2U
2635
2636/*
2637 Indicates that RANGE_SEQ_IF::next(&range) doesn't need to fill in the
2638 'range' parameter.
2639*/
2640#define HA_MRR_NO_ASSOCIATION 4U
2641
2642/*
2643 The MRR user will provide ranges in key order, and MRR implementation
2644 must return rows in key order.
2645*/
2646#define HA_MRR_SORTED 8U
2647
2648/* MRR implementation doesn't have to retrieve full records */
2649#define HA_MRR_INDEX_ONLY 16U
2650
2651/*
2652 The passed memory buffer is of maximum possible size, the caller can't
2653 assume larger buffer.
2654*/
2655#define HA_MRR_LIMITS 32U
2656
2657
2658/*
2659 Flag set <=> default MRR implementation is used
2660 (The choice is made by **_info[_const]() function which may set this
2661 flag. SQL layer remembers the flag value and then passes it to
2662 multi_read_range_init().
2663*/
2664#define HA_MRR_USE_DEFAULT_IMPL 64U
2665
2666/*
2667 Used only as parameter to multi_range_read_info():
2668 Flag set <=> the caller guarantees that the bounds of the scanned ranges
2669 will not have NULL values.
2670*/
2671#define HA_MRR_NO_NULL_ENDPOINTS 128U
2672
2673/*
2674 The MRR user has materialized range keys somewhere in the user's buffer.
2675 This can be used for optimization of the procedure that sorts these keys
2676 since in this case key values don't have to be copied into the MRR buffer.
2677
2678 In other words, it is guaranteed that after RANGE_SEQ_IF::next() call the
2679 pointer in range->start_key.key will point to a key value that will remain
2680 there until the end of the MRR scan.
2681*/
2682#define HA_MRR_MATERIALIZED_KEYS 256U
2683
2684/*
2685 The following bits are reserved for use by MRR implementation. The intended
2686 use scenario:
2687
2688 * sql layer calls handler->multi_range_read_info[_const]()
2689 - MRR implementation figures out what kind of scan it will perform, saves
2690 the result in *mrr_mode parameter.
2691 * sql layer remembers what was returned in *mrr_mode
2692
2693 * the optimizer picks the query plan (which may or may not include the MRR
2694 scan that was estimated by the multi_range_read_info[_const] call)
2695
2696 * if the query is an EXPLAIN statement, sql layer will call
2697 handler->multi_range_read_explain_info(mrr_mode) to get a text description
2698 of the picked MRR scan; the description will be a part of EXPLAIN output.
2699*/
2700#define HA_MRR_IMPLEMENTATION_FLAG1 512U
2701#define HA_MRR_IMPLEMENTATION_FLAG2 1024U
2702#define HA_MRR_IMPLEMENTATION_FLAG3 2048U
2703#define HA_MRR_IMPLEMENTATION_FLAG4 4096U
2704#define HA_MRR_IMPLEMENTATION_FLAG5 8192U
2705#define HA_MRR_IMPLEMENTATION_FLAG6 16384U
2706
2707#define HA_MRR_IMPLEMENTATION_FLAGS \
2708 (512U | 1024U | 2048U | 4096U | 8192U | 16384U)
2709
2710/*
2711 This is a buffer area that the handler can use to store rows.
2712 'end_of_used_area' should be kept updated after calls to
2713 read-functions so that other parts of the code can use the
2714 remaining area (until next read calls is issued).
2715*/
2716
2717typedef struct st_handler_buffer
2718{
2719 /* const? */uchar *buffer; /* Buffer one can start using */
2720 /* const? */uchar *buffer_end; /* End of buffer */
2721 uchar *end_of_used_area; /* End of area that was used by handler */
2722} HANDLER_BUFFER;
2723
2724typedef struct system_status_var SSV;
2725
2726class ha_statistics
2727{
2728public:
2729 ulonglong data_file_length; /* Length off data file */
2730 ulonglong max_data_file_length; /* Length off data file */
2731 ulonglong index_file_length;
2732 ulonglong max_index_file_length;
2733 ulonglong delete_length; /* Free bytes */
2734 ulonglong auto_increment_value;
2735 /*
2736 The number of records in the table.
2737 0 - means the table has exactly 0 rows
2738 other - if (table_flags() & HA_STATS_RECORDS_IS_EXACT)
2739 the value is the exact number of records in the table
2740 else
2741 it is an estimate
2742 */
2743 ha_rows records;
2744 ha_rows deleted; /* Deleted records */
2745 ulong mean_rec_length; /* physical reclength */
2746 time_t create_time; /* When table was created */
2747 time_t check_time;
2748 time_t update_time;
2749 uint block_size; /* index block size */
2750
2751 /*
2752 number of buffer bytes that native mrr implementation needs,
2753 */
2754 uint mrr_length_per_rec;
2755
2756 ha_statistics():
2757 data_file_length(0), max_data_file_length(0),
2758 index_file_length(0), max_index_file_length(0), delete_length(0),
2759 auto_increment_value(0), records(0), deleted(0), mean_rec_length(0),
2760 create_time(0), check_time(0), update_time(0), block_size(0),
2761 mrr_length_per_rec(0)
2762 {}
2763};
2764
2765extern "C" enum icp_result handler_index_cond_check(void* h_arg);
2766
2767uint calculate_key_len(TABLE *, uint, const uchar *, key_part_map);
2768/*
2769 bitmap with first N+1 bits set
2770 (keypart_map for a key prefix of [0..N] keyparts)
2771*/
2772#define make_keypart_map(N) (((key_part_map)2 << (N)) - 1)
2773/*
2774 bitmap with first N bits set
2775 (keypart_map for a key prefix of [0..N-1] keyparts)
2776*/
2777#define make_prev_keypart_map(N) (((key_part_map)1 << (N)) - 1)
2778
2779
2780/** Base class to be used by handlers different shares */
2781class Handler_share
2782{
2783public:
2784 Handler_share() {}
2785 virtual ~Handler_share() {}
2786};
2787
2788
2789/**
2790 The handler class is the interface for dynamically loadable
2791 storage engines. Do not add ifdefs and take care when adding or
2792 changing virtual functions to avoid vtable confusion
2793
2794 Functions in this class accept and return table columns data. Two data
2795 representation formats are used:
2796 1. TableRecordFormat - Used to pass [partial] table records to/from
2797 storage engine
2798
2799 2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
2800 storage engine. See opt_range.cc for description of this format.
2801
2802 TableRecordFormat
2803 =================
2804 [Warning: this description is work in progress and may be incomplete]
2805 The table record is stored in a fixed-size buffer:
2806
2807 record: null_bytes, column1_data, column2_data, ...
2808
2809 The offsets of the parts of the buffer are also fixed: every column has
2810 an offset to its column{i}_data, and if it is nullable it also has its own
2811 bit in null_bytes.
2812
2813 The record buffer only includes data about columns that are marked in the
2814 relevant column set (table->read_set and/or table->write_set, depending on
2815 the situation).
2816 <not-sure>It could be that it is required that null bits of non-present
2817 columns are set to 1</not-sure>
2818
2819 VARIOUS EXCEPTIONS AND SPECIAL CASES
2820
2821 If the table has no nullable columns, then null_bytes is still
2822 present, its length is one byte <not-sure> which must be set to 0xFF
2823 at all times. </not-sure>
2824
2825 If the table has columns of type BIT, then certain bits from those columns
2826 may be stored in null_bytes as well. Grep around for Field_bit for
2827 details.
2828
2829 For blob columns (see Field_blob), the record buffer stores length of the
2830 data, following by memory pointer to the blob data. The pointer is owned
2831 by the storage engine and is valid until the next operation.
2832
2833 If a blob column has NULL value, then its length and blob data pointer
2834 must be set to 0.
2835*/
2836
2837class handler :public Sql_alloc
2838{
2839public:
2840 typedef ulonglong Table_flags;
2841protected:
2842 TABLE_SHARE *table_share; /* The table definition */
2843 TABLE *table; /* The current open table */
2844 Table_flags cached_table_flags; /* Set on init() and open() */
2845
2846 ha_rows estimation_rows_to_insert;
2847public:
2848 handlerton *ht; /* storage engine of this handler */
2849 uchar *ref; /* Pointer to current row */
2850 uchar *dup_ref; /* Pointer to duplicate row */
2851
2852 ha_statistics stats;
2853
2854 /** MultiRangeRead-related members: */
2855 range_seq_t mrr_iter; /* Interator to traverse the range sequence */
2856 RANGE_SEQ_IF mrr_funcs; /* Range sequence traversal functions */
2857 HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
2858 uint ranges_in_seq; /* Total number of ranges in the traversed sequence */
2859 /** Current range (the one we're now returning rows from) */
2860 KEY_MULTI_RANGE mrr_cur_range;
2861
2862 /** The following are for read_range() */
2863 key_range save_end_range, *end_range;
2864 KEY_PART_INFO *range_key_part;
2865 int key_compare_result_on_equal;
2866
2867 /* TRUE <=> source MRR ranges and the output are ordered */
2868 bool mrr_is_output_sorted;
2869 /** TRUE <=> we're currently traversing a range in mrr_cur_range. */
2870 bool mrr_have_range;
2871 bool eq_range;
2872 bool internal_tmp_table; /* If internal tmp table */
2873 bool implicit_emptied; /* Can be !=0 only if HEAP */
2874 bool mark_trx_read_write_done; /* mark_trx_read_write was called */
2875 bool check_table_binlog_row_based_done; /* check_table_binlog.. was called */
2876 bool check_table_binlog_row_based_result; /* cached check_table_binlog... */
2877 /* Set to 1 if handler logged last insert/update/delete operation */
2878 bool row_already_logged;
2879 /*
2880 TRUE <=> the engine guarantees that returned records are within the range
2881 being scanned.
2882 */
2883 bool in_range_check_pushed_down;
2884
2885 uint errkey; /* Last dup key */
2886 uint key_used_on_scan;
2887 uint active_index, keyread;
2888
2889 /** Length of ref (1-8 or the clustered key length) */
2890 uint ref_length;
2891 FT_INFO *ft_handler;
2892 enum init_stat { NONE=0, INDEX, RND };
2893 init_stat inited, pre_inited;
2894
2895 const COND *pushed_cond;
2896 /**
2897 next_insert_id is the next value which should be inserted into the
2898 auto_increment column: in a inserting-multi-row statement (like INSERT
2899 SELECT), for the first row where the autoinc value is not specified by the
2900 statement, get_auto_increment() called and asked to generate a value,
2901 next_insert_id is set to the next value, then for all other rows
2902 next_insert_id is used (and increased each time) without calling
2903 get_auto_increment().
2904 */
2905 ulonglong next_insert_id;
2906 /**
2907 insert id for the current row (*autogenerated*; if not
2908 autogenerated, it's 0).
2909 At first successful insertion, this variable is stored into
2910 THD::first_successful_insert_id_in_cur_stmt.
2911 */
2912 ulonglong insert_id_for_cur_row;
2913 /**
2914 Interval returned by get_auto_increment() and being consumed by the
2915 inserter.
2916 */
2917 /* Statistics variables */
2918 ulonglong rows_read;
2919 ulonglong rows_tmp_read;
2920 ulonglong rows_changed;
2921 /* One bigger than needed to avoid to test if key == MAX_KEY */
2922 ulonglong index_rows_read[MAX_KEY+1];
2923
2924private:
2925 /* ANALYZE time tracker, if present */
2926 Exec_time_tracker *tracker;
2927public:
2928 void set_time_tracker(Exec_time_tracker *tracker_arg) { tracker=tracker_arg;}
2929
2930 Item *pushed_idx_cond;
2931 uint pushed_idx_cond_keyno; /* The index which the above condition is for */
2932
2933 Discrete_interval auto_inc_interval_for_cur_row;
2934 /**
2935 Number of reserved auto-increment intervals. Serves as a heuristic
2936 when we have no estimation of how many records the statement will insert:
2937 the more intervals we have reserved, the bigger the next one. Reset in
2938 handler::ha_release_auto_increment().
2939 */
2940 uint auto_inc_intervals_count;
2941
2942 /**
2943 Instrumented table associated with this handler.
2944 This member should be set to NULL when no instrumentation is in place,
2945 so that linking an instrumented/non instrumented server/plugin works.
2946 For example:
2947 - the server is compiled with the instrumentation.
2948 The server expects either NULL or valid pointers in m_psi.
2949 - an engine plugin is compiled without instrumentation.
2950 The plugin can not leave this pointer uninitialized,
2951 or can not leave a trash value on purpose in this pointer,
2952 as this would crash the server.
2953 */
2954 PSI_table *m_psi;
2955
2956 virtual void unbind_psi();
2957 virtual void rebind_psi();
2958
2959 bool set_top_table_fields;
2960 struct TABLE *top_table;
2961 Field **top_table_field;
2962 uint top_table_fields;
2963
2964private:
2965 /**
2966 The lock type set by when calling::ha_external_lock(). This is
2967 propagated down to the storage engine. The reason for also storing
2968 it here, is that when doing MRR we need to create/clone a second handler
2969 object. This cloned handler object needs to know about the lock_type used.
2970 */
2971 int m_lock_type;
2972 /**
2973 Pointer where to store/retrieve the Handler_share pointer.
2974 For non partitioned handlers this is &TABLE_SHARE::ha_share.
2975 */
2976 Handler_share **ha_share;
2977
2978public:
2979 handler(handlerton *ht_arg, TABLE_SHARE *share_arg)
2980 :table_share(share_arg), table(0),
2981 estimation_rows_to_insert(0), ht(ht_arg),
2982 ref(0), end_range(NULL),
2983 implicit_emptied(0),
2984 mark_trx_read_write_done(0),
2985 check_table_binlog_row_based_done(0),
2986 check_table_binlog_row_based_result(0),
2987 row_already_logged(0),
2988 in_range_check_pushed_down(FALSE),
2989 key_used_on_scan(MAX_KEY),
2990 active_index(MAX_KEY), keyread(MAX_KEY),
2991 ref_length(sizeof(my_off_t)),
2992 ft_handler(0), inited(NONE), pre_inited(NONE),
2993 pushed_cond(0), next_insert_id(0), insert_id_for_cur_row(0),
2994 tracker(NULL),
2995 pushed_idx_cond(NULL),
2996 pushed_idx_cond_keyno(MAX_KEY),
2997 auto_inc_intervals_count(0),
2998 m_psi(NULL), set_top_table_fields(FALSE), top_table(0),
2999 top_table_field(0), top_table_fields(0),
3000 m_lock_type(F_UNLCK), ha_share(NULL)
3001 {
3002 DBUG_PRINT("info",
3003 ("handler created F_UNLCK %d F_RDLCK %d F_WRLCK %d",
3004 F_UNLCK, F_RDLCK, F_WRLCK));
3005 reset_statistics();
3006 }
3007 virtual ~handler(void)
3008 {
3009 DBUG_ASSERT(m_lock_type == F_UNLCK);
3010 DBUG_ASSERT(inited == NONE);
3011 }
3012 virtual handler *clone(const char *name, MEM_ROOT *mem_root);
3013 /** This is called after create to allow us to set up cached variables */
3014 void init()
3015 {
3016 cached_table_flags= table_flags();
3017 }
3018 /* ha_ methods: pubilc wrappers for private virtual API */
3019
3020 int ha_open(TABLE *table, const char *name, int mode, uint test_if_locked,
3021 MEM_ROOT *mem_root= 0, List<String> *partitions_to_open=NULL);
3022 int ha_index_init(uint idx, bool sorted)
3023 {
3024 DBUG_EXECUTE_IF("ha_index_init_fail", return HA_ERR_TABLE_DEF_CHANGED;);
3025 int result;
3026 DBUG_ENTER("ha_index_init");
3027 DBUG_ASSERT(inited==NONE);
3028 if (!(result= index_init(idx, sorted)))
3029 {
3030 inited= INDEX;
3031 active_index= idx;
3032 end_range= NULL;
3033 }
3034 DBUG_RETURN(result);
3035 }
3036 int ha_index_end()
3037 {
3038 DBUG_ENTER("ha_index_end");
3039 DBUG_ASSERT(inited==INDEX);
3040 inited= NONE;
3041 active_index= MAX_KEY;
3042 end_range= NULL;
3043 DBUG_RETURN(index_end());
3044 }
3045 /* This is called after index_init() if we need to do a index scan */
3046 virtual int prepare_index_scan() { return 0; }
3047 virtual int prepare_index_key_scan_map(const uchar * key, key_part_map keypart_map)
3048 {
3049 uint key_len= calculate_key_len(table, active_index, key, keypart_map);
3050 return prepare_index_key_scan(key, key_len);
3051 }
3052 virtual int prepare_index_key_scan( const uchar * key, uint key_len )
3053 { return 0; }
3054 virtual int prepare_range_scan(const key_range *start_key, const key_range *end_key)
3055 { return 0; }
3056
3057 int ha_rnd_init(bool scan) __attribute__ ((warn_unused_result))
3058 {
3059 DBUG_EXECUTE_IF("ha_rnd_init_fail", return HA_ERR_TABLE_DEF_CHANGED;);
3060 int result;
3061 DBUG_ENTER("ha_rnd_init");
3062 DBUG_ASSERT(inited==NONE || (inited==RND && scan));
3063 inited= (result= rnd_init(scan)) ? NONE: RND;
3064 end_range= NULL;
3065 DBUG_RETURN(result);
3066 }
3067 int ha_rnd_end()
3068 {
3069 DBUG_ENTER("ha_rnd_end");
3070 DBUG_ASSERT(inited==RND);
3071 inited=NONE;
3072 end_range= NULL;
3073 DBUG_RETURN(rnd_end());
3074 }
3075 int ha_rnd_init_with_error(bool scan) __attribute__ ((warn_unused_result));
3076 int ha_reset();
3077 /* this is necessary in many places, e.g. in HANDLER command */
3078 int ha_index_or_rnd_end()
3079 {
3080 return inited == INDEX ? ha_index_end() : inited == RND ? ha_rnd_end() : 0;
3081 }
3082 /**
3083 The cached_table_flags is set at ha_open and ha_external_lock
3084 */
3085 Table_flags ha_table_flags() const { return cached_table_flags; }
3086 /**
3087 These functions represent the public interface to *users* of the
3088 handler class, hence they are *not* virtual. For the inheritance
3089 interface, see the (private) functions write_row(), update_row(),
3090 and delete_row() below.
3091 */
3092 int ha_external_lock(THD *thd, int lock_type);
3093 int ha_write_row(uchar * buf);
3094 int ha_update_row(const uchar * old_data, const uchar * new_data);
3095 int ha_delete_row(const uchar * buf);
3096 void ha_release_auto_increment();
3097
3098 bool keyread_enabled() { return keyread < MAX_KEY; }
3099 int ha_start_keyread(uint idx)
3100 {
3101 int res= keyread_enabled() ? 0 : extra(HA_EXTRA_KEYREAD);
3102 keyread= idx;
3103 return res;
3104 }
3105 int ha_end_keyread()
3106 {
3107 if (!keyread_enabled())
3108 return 0;
3109 keyread= MAX_KEY;
3110 return extra(HA_EXTRA_NO_KEYREAD);
3111 }
3112
3113 int check_collation_compatibility();
3114 int ha_check_for_upgrade(HA_CHECK_OPT *check_opt);
3115 /** to be actually called to get 'check()' functionality*/
3116 int ha_check(THD *thd, HA_CHECK_OPT *check_opt);
3117 int ha_repair(THD* thd, HA_CHECK_OPT* check_opt);
3118 void ha_start_bulk_insert(ha_rows rows, uint flags= 0)
3119 {
3120 DBUG_ENTER("handler::ha_start_bulk_insert");
3121 estimation_rows_to_insert= rows;
3122 start_bulk_insert(rows, flags);
3123 DBUG_VOID_RETURN;
3124 }
3125 int ha_end_bulk_insert()
3126 {
3127 DBUG_ENTER("handler::ha_end_bulk_insert");
3128 estimation_rows_to_insert= 0;
3129 int ret= end_bulk_insert();
3130 DBUG_RETURN(ret);
3131 }
3132 int ha_bulk_update_row(const uchar *old_data, const uchar *new_data,
3133 ha_rows *dup_key_found);
3134 int ha_delete_all_rows();
3135 int ha_truncate();
3136 int ha_reset_auto_increment(ulonglong value);
3137 int ha_optimize(THD* thd, HA_CHECK_OPT* check_opt);
3138 int ha_analyze(THD* thd, HA_CHECK_OPT* check_opt);
3139 bool ha_check_and_repair(THD *thd);
3140 int ha_disable_indexes(uint mode);
3141 int ha_enable_indexes(uint mode);
3142 int ha_discard_or_import_tablespace(my_bool discard);
3143 int ha_rename_table(const char *from, const char *to);
3144 int ha_delete_table(const char *name);
3145 void ha_drop_table(const char *name);
3146
3147 int ha_create(const char *name, TABLE *form, HA_CREATE_INFO *info);
3148
3149 int ha_create_partitioning_metadata(const char *name, const char *old_name,
3150 int action_flag);
3151
3152 int ha_change_partitions(HA_CREATE_INFO *create_info,
3153 const char *path,
3154 ulonglong * const copied,
3155 ulonglong * const deleted,
3156 const uchar *pack_frm_data,
3157 size_t pack_frm_len);
3158 int ha_drop_partitions(const char *path);
3159 int ha_rename_partitions(const char *path);
3160
3161 void adjust_next_insert_id_after_explicit_value(ulonglong nr);
3162 int update_auto_increment();
3163 virtual void print_error(int error, myf errflag);
3164 virtual bool get_error_message(int error, String *buf);
3165 uint get_dup_key(int error);
3166 /**
3167 Retrieves the names of the table and the key for which there was a
3168 duplicate entry in the case of HA_ERR_FOREIGN_DUPLICATE_KEY.
3169
3170 If any of the table or key name is not available this method will return
3171 false and will not change any of child_table_name or child_key_name.
3172
3173 @param child_table_name[out] Table name
3174 @param child_table_name_len[in] Table name buffer size
3175 @param child_key_name[out] Key name
3176 @param child_key_name_len[in] Key name buffer size
3177
3178 @retval true table and key names were available
3179 and were written into the corresponding
3180 out parameters.
3181 @retval false table and key names were not available,
3182 the out parameters were not touched.
3183 */
3184 virtual bool get_foreign_dup_key(char *child_table_name,
3185 uint child_table_name_len,
3186 char *child_key_name,
3187 uint child_key_name_len)
3188 { DBUG_ASSERT(false); return(false); }
3189 void reset_statistics()
3190 {
3191 rows_read= rows_changed= rows_tmp_read= 0;
3192 bzero(index_rows_read, sizeof(index_rows_read));
3193 }
3194 virtual void change_table_ptr(TABLE *table_arg, TABLE_SHARE *share)
3195 {
3196 table= table_arg;
3197 table_share= share;
3198 reset_statistics();
3199 }
3200 virtual double scan_time()
3201 { return ulonglong2double(stats.data_file_length) / IO_SIZE + 2; }
3202
3203 /**
3204 The cost of reading a set of ranges from the table using an index
3205 to access it.
3206
3207 @param index The index number.
3208 @param ranges The number of ranges to be read.
3209 @param rows Total number of rows to be read.
3210
3211 This method can be used to calculate the total cost of scanning a table
3212 using an index by calling it using read_time(index, 1, table_size).
3213 */
3214 virtual double read_time(uint index, uint ranges, ha_rows rows)
3215 { return rows2double(ranges+rows); }
3216
3217 /**
3218 Calculate cost of 'keyread' scan for given index and number of records.
3219
3220 @param index index to read
3221 @param ranges #of ranges to read
3222 @param rows #of records to read
3223 */
3224 virtual double keyread_time(uint index, uint ranges, ha_rows rows);
3225
3226 virtual const key_map *keys_to_use_for_scanning() { return &key_map_empty; }
3227
3228 /*
3229 True if changes to the table is persistent (no rollback)
3230 This is manly used to decide how to log changes to the table in
3231 the binary log.
3232 */
3233 bool has_transactions()
3234 {
3235 return ((ha_table_flags() & (HA_NO_TRANSACTIONS | HA_PERSISTENT_TABLE))
3236 == 0);
3237 }
3238 /*
3239 True if the underlaying table doesn't support transactions
3240 */
3241 bool has_transaction_manager()
3242 {
3243 return ((ha_table_flags() & HA_NO_TRANSACTIONS) == 0);
3244 }
3245
3246 /**
3247 This method is used to analyse the error to see whether the error
3248 is ignorable or not, certain handlers can have more error that are
3249 ignorable than others. E.g. the partition handler can get inserts
3250 into a range where there is no partition and this is an ignorable
3251 error.
3252 HA_ERR_FOUND_DUP_UNIQUE is a special case in MyISAM that means the
3253 same thing as HA_ERR_FOUND_DUP_KEY but can in some cases lead to
3254 a slightly different error message.
3255 */
3256 virtual bool is_fatal_error(int error, uint flags)
3257 {
3258 if (!error ||
3259 ((flags & HA_CHECK_DUP_KEY) &&
3260 (error == HA_ERR_FOUND_DUPP_KEY ||
3261 error == HA_ERR_FOUND_DUPP_UNIQUE)) ||
3262 error == HA_ERR_AUTOINC_ERANGE ||
3263 ((flags & HA_CHECK_FK_ERROR) &&
3264 (error == HA_ERR_ROW_IS_REFERENCED ||
3265 error == HA_ERR_NO_REFERENCED_ROW)))
3266 return FALSE;
3267 return TRUE;
3268 }
3269
3270 /**
3271 Number of rows in table. It will only be called if
3272 (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
3273 */
3274 virtual int pre_records() { return 0; }
3275 virtual ha_rows records() { return stats.records; }
3276 /**
3277 Return upper bound of current number of records in the table
3278 (max. of how many records one will retrieve when doing a full table scan)
3279 If upper bound is not known, HA_POS_ERROR should be returned as a max
3280 possible upper bound.
3281 */
3282 virtual ha_rows estimate_rows_upper_bound()
3283 { return stats.records+EXTRA_RECORDS; }
3284
3285 /**
3286 Get the row type from the storage engine. If this method returns
3287 ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
3288 */
3289 virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
3290
3291 virtual const char *index_type(uint key_number) { DBUG_ASSERT(0); return "";}
3292
3293
3294 /**
3295 Signal that the table->read_set and table->write_set table maps changed
3296 The handler is allowed to set additional bits in the above map in this
3297 call. Normally the handler should ignore all calls until we have done
3298 a ha_rnd_init() or ha_index_init(), write_row(), update_row or delete_row()
3299 as there may be several calls to this routine.
3300 */
3301 virtual void column_bitmaps_signal();
3302 /*
3303 We have to check for inited as some engines, like innodb, sets
3304 active_index during table scan.
3305 */
3306 uint get_index(void) const
3307 { return inited == INDEX ? active_index : MAX_KEY; }
3308 int ha_close(void);
3309
3310 /**
3311 @retval 0 Bulk update used by handler
3312 @retval 1 Bulk update not used, normal operation used
3313 */
3314 virtual bool start_bulk_update() { return 1; }
3315 /**
3316 @retval 0 Bulk delete used by handler
3317 @retval 1 Bulk delete not used, normal operation used
3318 */
3319 virtual bool start_bulk_delete() { return 1; }
3320 /**
3321 After this call all outstanding updates must be performed. The number
3322 of duplicate key errors are reported in the duplicate key parameter.
3323 It is allowed to continue to the batched update after this call, the
3324 handler has to wait until end_bulk_update with changing state.
3325
3326 @param dup_key_found Number of duplicate keys found
3327
3328 @retval 0 Success
3329 @retval >0 Error code
3330 */
3331 virtual int exec_bulk_update(ha_rows *dup_key_found)
3332 {
3333 DBUG_ASSERT(FALSE);
3334 return HA_ERR_WRONG_COMMAND;
3335 }
3336 /**
3337 Perform any needed clean-up, no outstanding updates are there at the
3338 moment.
3339 */
3340 virtual int end_bulk_update() { return 0; }
3341 /**
3342 Execute all outstanding deletes and close down the bulk delete.
3343
3344 @retval 0 Success
3345 @retval >0 Error code
3346 */
3347 virtual int end_bulk_delete()
3348 {
3349 DBUG_ASSERT(FALSE);
3350 return HA_ERR_WRONG_COMMAND;
3351 }
3352 virtual int pre_index_read_map(const uchar *key,
3353 key_part_map keypart_map,
3354 enum ha_rkey_function find_flag,
3355 bool use_parallel)
3356 { return 0; }
3357 virtual int pre_index_first(bool use_parallel)
3358 { return 0; }
3359 virtual int pre_index_last(bool use_parallel)
3360 { return 0; }
3361 virtual int pre_index_read_last_map(const uchar *key,
3362 key_part_map keypart_map,
3363 bool use_parallel)
3364 { return 0; }
3365/*
3366 virtual int pre_read_multi_range_first(KEY_MULTI_RANGE **found_range_p,
3367 KEY_MULTI_RANGE *ranges,
3368 uint range_count,
3369 bool sorted, HANDLER_BUFFER *buffer,
3370 bool use_parallel);
3371*/
3372 virtual int pre_multi_range_read_next(bool use_parallel)
3373 { return 0; }
3374 virtual int pre_read_range_first(const key_range *start_key,
3375 const key_range *end_key,
3376 bool eq_range, bool sorted,
3377 bool use_parallel)
3378 { return 0; }
3379 virtual int pre_ft_read(bool use_parallel)
3380 { return 0; }
3381 virtual int pre_rnd_next(bool use_parallel)
3382 { return 0; }
3383 int ha_pre_rnd_init(bool scan)
3384 {
3385 int result;
3386 DBUG_ENTER("ha_pre_rnd_init");
3387 DBUG_ASSERT(pre_inited==NONE || (pre_inited==RND && scan));
3388 pre_inited= (result= pre_rnd_init(scan)) ? NONE: RND;
3389 DBUG_RETURN(result);
3390 }
3391 int ha_pre_rnd_end()
3392 {
3393 DBUG_ENTER("ha_pre_rnd_end");
3394 DBUG_ASSERT(pre_inited==RND);
3395 pre_inited=NONE;
3396 DBUG_RETURN(pre_rnd_end());
3397 }
3398 virtual int pre_rnd_init(bool scan) { return 0; }
3399 virtual int pre_rnd_end() { return 0; }
3400 virtual int pre_index_init(uint idx, bool sorted) { return 0; }
3401 virtual int pre_index_end() { return 0; }
3402 int ha_pre_index_init(uint idx, bool sorted)
3403 {
3404 int result;
3405 DBUG_ENTER("ha_pre_index_init");
3406 DBUG_ASSERT(pre_inited==NONE);
3407 if (!(result= pre_index_init(idx, sorted)))
3408 pre_inited=INDEX;
3409 DBUG_RETURN(result);
3410 }
3411 int ha_pre_index_end()
3412 {
3413 DBUG_ENTER("ha_pre_index_end");
3414 DBUG_ASSERT(pre_inited==INDEX);
3415 pre_inited=NONE;
3416 DBUG_RETURN(pre_index_end());
3417 }
3418 int ha_pre_index_or_rnd_end()
3419 {
3420 return (pre_inited == INDEX ?
3421 ha_pre_index_end() :
3422 pre_inited == RND ? ha_pre_rnd_end() : 0 );
3423 }
3424
3425 /**
3426 @brief
3427 Positions an index cursor to the index specified in the
3428 handle. Fetches the row if available. If the key value is null,
3429 begin at the first key of the index.
3430 */
3431protected:
3432 virtual int index_read_map(uchar * buf, const uchar * key,
3433 key_part_map keypart_map,
3434 enum ha_rkey_function find_flag)
3435 {
3436 uint key_len= calculate_key_len(table, active_index, key, keypart_map);
3437 return index_read(buf, key, key_len, find_flag);
3438 }
3439 /**
3440 @brief
3441 Positions an index cursor to the index specified in the
3442 handle. Fetches the row if available. If the key value is null,
3443 begin at the first key of the index.
3444 */
3445 virtual int index_read_idx_map(uchar * buf, uint index, const uchar * key,
3446 key_part_map keypart_map,
3447 enum ha_rkey_function find_flag);
3448 virtual int index_next(uchar * buf)
3449 { return HA_ERR_WRONG_COMMAND; }
3450 virtual int index_prev(uchar * buf)
3451 { return HA_ERR_WRONG_COMMAND; }
3452 virtual int index_first(uchar * buf)
3453 { return HA_ERR_WRONG_COMMAND; }
3454 virtual int index_last(uchar * buf)
3455 { return HA_ERR_WRONG_COMMAND; }
3456 virtual int index_next_same(uchar *buf, const uchar *key, uint keylen);
3457 /**
3458 @brief
3459 The following functions works like index_read, but it find the last
3460 row with the current key value or prefix.
3461 @returns @see index_read_map().
3462 */
3463 virtual int index_read_last_map(uchar * buf, const uchar * key,
3464 key_part_map keypart_map)
3465 {
3466 uint key_len= calculate_key_len(table, active_index, key, keypart_map);
3467 return index_read_last(buf, key, key_len);
3468 }
3469 virtual int close(void)=0;
3470 inline void update_rows_read()
3471 {
3472 if (likely(!internal_tmp_table))
3473 rows_read++;
3474 else
3475 rows_tmp_read++;
3476 }
3477 inline void update_index_statistics()
3478 {
3479 index_rows_read[active_index]++;
3480 update_rows_read();
3481 }
3482public:
3483
3484 int ha_index_read_map(uchar * buf, const uchar * key,
3485 key_part_map keypart_map,
3486 enum ha_rkey_function find_flag);
3487 int ha_index_read_idx_map(uchar * buf, uint index, const uchar * key,
3488 key_part_map keypart_map,
3489 enum ha_rkey_function find_flag);
3490 int ha_index_next(uchar * buf);
3491 int ha_index_prev(uchar * buf);
3492 int ha_index_first(uchar * buf);
3493 int ha_index_last(uchar * buf);
3494 int ha_index_next_same(uchar *buf, const uchar *key, uint keylen);
3495 /*
3496 TODO: should we make for those functions non-virtual ha_func_name wrappers,
3497 too?
3498 */
3499 virtual ha_rows multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
3500 void *seq_init_param,
3501 uint n_ranges, uint *bufsz,
3502 uint *mrr_mode,
3503 Cost_estimate *cost);
3504 virtual ha_rows multi_range_read_info(uint keyno, uint n_ranges, uint keys,
3505 uint key_parts, uint *bufsz,
3506 uint *mrr_mode, Cost_estimate *cost);
3507 virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
3508 uint n_ranges, uint mrr_mode,
3509 HANDLER_BUFFER *buf);
3510 virtual int multi_range_read_next(range_id_t *range_info);
3511 /*
3512 Return string representation of the MRR plan.
3513
3514 This is intended to be used for EXPLAIN, via the following scenario:
3515 1. SQL layer calls handler->multi_range_read_info().
3516 1.1. Storage engine figures out whether it will use some non-default
3517 MRR strategy, sets appropritate bits in *mrr_mode, and returns
3518 control to SQL layer
3519 2. SQL layer remembers the returned mrr_mode
3520 3. SQL layer compares various options and choses the final query plan. As
3521 a part of that, it makes a choice of whether to use the MRR strategy
3522 picked in 1.1
3523 4. EXPLAIN code converts the query plan to its text representation. If MRR
3524 strategy is part of the plan, it calls
3525 multi_range_read_explain_info(mrr_mode) to get a text representation of
3526 the picked MRR strategy.
3527
3528 @param mrr_mode Mode which was returned by multi_range_read_info[_const]
3529 @param str INOUT string to be printed for EXPLAIN
3530 @param str_end End of the string buffer. The function is free to put the
3531 string into [str..str_end] memory range.
3532 */
3533 virtual int multi_range_read_explain_info(uint mrr_mode, char *str,
3534 size_t size)
3535 { return 0; }
3536
3537 virtual int read_range_first(const key_range *start_key,
3538 const key_range *end_key,
3539 bool eq_range, bool sorted);
3540 virtual int read_range_next();
3541 void set_end_range(const key_range *end_key);
3542 int compare_key(key_range *range);
3543 int compare_key2(key_range *range) const;
3544 virtual int ft_init() { return HA_ERR_WRONG_COMMAND; }
3545 virtual int pre_ft_init() { return HA_ERR_WRONG_COMMAND; }
3546 virtual void ft_end() {}
3547 virtual int pre_ft_end() { return 0; }
3548 virtual FT_INFO *ft_init_ext(uint flags, uint inx,String *key)
3549 { return NULL; }
3550public:
3551 virtual int ft_read(uchar *buf) { return HA_ERR_WRONG_COMMAND; }
3552 virtual int rnd_next(uchar *buf)=0;
3553 virtual int rnd_pos(uchar * buf, uchar *pos)=0;
3554 /**
3555 This function only works for handlers having
3556 HA_PRIMARY_KEY_REQUIRED_FOR_POSITION set.
3557 It will return the row with the PK given in the record argument.
3558 */
3559 virtual int rnd_pos_by_record(uchar *record)
3560 {
3561 DBUG_ASSERT(table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_POSITION);
3562 position(record);
3563 return rnd_pos(record, ref);
3564 }
3565 virtual int read_first_row(uchar *buf, uint primary_key);
3566public:
3567
3568 /* Same as above, but with statistics */
3569 inline int ha_ft_read(uchar *buf);
3570 inline void ha_ft_end() { ft_end(); ft_handler=NULL; }
3571 int ha_rnd_next(uchar *buf);
3572 int ha_rnd_pos(uchar *buf, uchar *pos);
3573 inline int ha_rnd_pos_by_record(uchar *buf);
3574 inline int ha_read_first_row(uchar *buf, uint primary_key);
3575
3576 /**
3577 The following 3 function is only needed for tables that may be
3578 internal temporary tables during joins.
3579 */
3580 virtual int remember_rnd_pos()
3581 { return HA_ERR_WRONG_COMMAND; }
3582 virtual int restart_rnd_next(uchar *buf)
3583 { return HA_ERR_WRONG_COMMAND; }
3584 virtual int rnd_same(uchar *buf, uint inx)
3585 { return HA_ERR_WRONG_COMMAND; }
3586
3587 virtual ha_rows records_in_range(uint inx, key_range *min_key,
3588 key_range *max_key)
3589 { return (ha_rows) 10; }
3590 /*
3591 If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, then it sets ref
3592 (reference to the row, aka position, with the primary key given in
3593 the record).
3594 Otherwise it set ref to the current row.
3595 */
3596 virtual void position(const uchar *record)=0;
3597 virtual int info(uint)=0; // see my_base.h for full description
3598 virtual void get_dynamic_partition_info(PARTITION_STATS *stat_info,
3599 uint part_id);
3600 virtual void set_partitions_to_open(List<String> *partition_names) {}
3601 virtual int change_partitions_to_open(List<String> *partition_names)
3602 { return 0; }
3603 virtual int extra(enum ha_extra_function operation)
3604 { return 0; }
3605 virtual int extra_opt(enum ha_extra_function operation, ulong cache_size)
3606 { return extra(operation); }
3607
3608 /**
3609 In an UPDATE or DELETE, if the row under the cursor was locked by another
3610 transaction, and the engine used an optimistic read of the last
3611 committed row value under the cursor, then the engine returns 1 from this
3612 function. MySQL must NOT try to update this optimistic value. If the
3613 optimistic value does not match the WHERE condition, MySQL can decide to
3614 skip over this row. Currently only works for InnoDB. This can be used to
3615 avoid unnecessary lock waits.
3616
3617 If this method returns nonzero, it will also signal the storage
3618 engine that the next read will be a locking re-read of the row.
3619 */
3620 bool ha_was_semi_consistent_read();
3621 virtual bool was_semi_consistent_read() { return 0; }
3622 /**
3623 Tell the engine whether it should avoid unnecessary lock waits.
3624 If yes, in an UPDATE or DELETE, if the row under the cursor was locked
3625 by another transaction, the engine may try an optimistic read of
3626 the last committed row value under the cursor.
3627 */
3628 virtual void try_semi_consistent_read(bool) {}
3629 virtual void unlock_row() {}
3630 virtual int start_stmt(THD *thd, thr_lock_type lock_type) {return 0;}
3631 virtual bool need_info_for_auto_inc() { return 0; }
3632 virtual bool can_use_for_auto_inc_init() { return 1; }
3633 virtual void get_auto_increment(ulonglong offset, ulonglong increment,
3634 ulonglong nb_desired_values,
3635 ulonglong *first_value,
3636 ulonglong *nb_reserved_values);
3637 void set_next_insert_id(ulonglong id)
3638 {
3639 DBUG_PRINT("info",("auto_increment: next value %lu", (ulong)id));
3640 next_insert_id= id;
3641 }
3642 void restore_auto_increment(ulonglong prev_insert_id)
3643 {
3644 /*
3645 Insertion of a row failed, re-use the lastly generated auto_increment
3646 id, for the next row. This is achieved by resetting next_insert_id to
3647 what it was before the failed insertion (that old value is provided by
3648 the caller). If that value was 0, it was the first row of the INSERT;
3649 then if insert_id_for_cur_row contains 0 it means no id was generated
3650 for this first row, so no id was generated since the INSERT started, so
3651 we should set next_insert_id to 0; if insert_id_for_cur_row is not 0, it
3652 is the generated id of the first and failed row, so we use it.
3653 */
3654 next_insert_id= (prev_insert_id > 0) ? prev_insert_id :
3655 insert_id_for_cur_row;
3656 }
3657
3658 virtual void update_create_info(HA_CREATE_INFO *create_info) {}
3659 int check_old_types();
3660 virtual int assign_to_keycache(THD* thd, HA_CHECK_OPT* check_opt)
3661 { return HA_ADMIN_NOT_IMPLEMENTED; }
3662 virtual int preload_keys(THD* thd, HA_CHECK_OPT* check_opt)
3663 { return HA_ADMIN_NOT_IMPLEMENTED; }
3664 /* end of the list of admin commands */
3665
3666 virtual int indexes_are_disabled(void) {return 0;}
3667 virtual char *update_table_comment(const char * comment)
3668 { return (char*) comment;}
3669 virtual void append_create_info(String *packet) {}
3670 /**
3671 If index == MAX_KEY then a check for table is made and if index <
3672 MAX_KEY then a check is made if the table has foreign keys and if
3673 a foreign key uses this index (and thus the index cannot be dropped).
3674
3675 @param index Index to check if foreign key uses it
3676
3677 @retval TRUE Foreign key defined on table or index
3678 @retval FALSE No foreign key defined
3679 */
3680 virtual bool is_fk_defined_on_table_or_index(uint index)
3681 { return FALSE; }
3682 virtual char* get_foreign_key_create_info()
3683 { return(NULL);} /* gets foreign key create string from InnoDB */
3684 /**
3685 Used in ALTER TABLE to check if changing storage engine is allowed.
3686
3687 @note Called without holding thr_lock.c lock.
3688
3689 @retval true Changing storage engine is allowed.
3690 @retval false Changing storage engine not allowed.
3691 */
3692 virtual bool can_switch_engines() { return true; }
3693 virtual int can_continue_handler_scan() { return 0; }
3694 /**
3695 Get the list of foreign keys in this table.
3696
3697 @remark Returns the set of foreign keys where this table is the
3698 dependent or child table.
3699
3700 @param thd The thread handle.
3701 @param f_key_list[out] The list of foreign keys.
3702
3703 @return The handler error code or zero for success.
3704 */
3705 virtual int
3706 get_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list)
3707 { return 0; }
3708 /**
3709 Get the list of foreign keys referencing this table.
3710
3711 @remark Returns the set of foreign keys where this table is the
3712 referenced or parent table.
3713
3714 @param thd The thread handle.
3715 @param f_key_list[out] The list of foreign keys.
3716
3717 @return The handler error code or zero for success.
3718 */
3719 virtual int
3720 get_parent_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list)
3721 { return 0; }
3722 virtual uint referenced_by_foreign_key() { return 0;}
3723 virtual void init_table_handle_for_HANDLER()
3724 { return; } /* prepare InnoDB for HANDLER */
3725 virtual void free_foreign_key_create_info(char* str) {}
3726 /** The following can be called without an open handler */
3727 const char *table_type() const { return hton_name(ht)->str; }
3728 const char **bas_ext() const { return ht->tablefile_extensions; }
3729
3730 virtual int get_default_no_partitions(HA_CREATE_INFO *create_info)
3731 { return 1;}
3732 virtual void set_auto_partitions(partition_info *part_info) { return; }
3733 virtual bool get_no_parts(const char *name,
3734 uint *no_parts)
3735 {
3736 *no_parts= 0;
3737 return 0;
3738 }
3739 virtual void set_part_info(partition_info *part_info) {return;}
3740 virtual void return_record_by_parent() { return; }
3741
3742 virtual ulong index_flags(uint idx, uint part, bool all_parts) const =0;
3743
3744 uint max_record_length() const
3745 { return MY_MIN(HA_MAX_REC_LENGTH, max_supported_record_length()); }
3746 uint max_keys() const
3747 { return MY_MIN(MAX_KEY, max_supported_keys()); }
3748 uint max_key_parts() const
3749 { return MY_MIN(MAX_REF_PARTS, max_supported_key_parts()); }
3750 uint max_key_length() const
3751 { return MY_MIN(MAX_KEY_LENGTH, max_supported_key_length()); }
3752 uint max_key_part_length() const
3753 { return MY_MIN(MAX_KEY_LENGTH, max_supported_key_part_length()); }
3754
3755 virtual uint max_supported_record_length() const { return HA_MAX_REC_LENGTH; }
3756 virtual uint max_supported_keys() const { return 0; }
3757 virtual uint max_supported_key_parts() const { return MAX_REF_PARTS; }
3758 virtual uint max_supported_key_length() const { return MAX_KEY_LENGTH; }
3759 virtual uint max_supported_key_part_length() const { return 255; }
3760 virtual uint min_record_length(uint options) const { return 1; }
3761
3762 virtual uint checksum() const { return 0; }
3763 virtual bool is_crashed() const { return 0; }
3764 virtual bool auto_repair(int error) const { return 0; }
3765
3766 void update_global_table_stats();
3767 void update_global_index_stats();
3768
3769#define CHF_CREATE_FLAG 0
3770#define CHF_DELETE_FLAG 1
3771#define CHF_RENAME_FLAG 2
3772#define CHF_INDEX_FLAG 3
3773
3774 /**
3775 @note lock_count() can return > 1 if the table is MERGE or partitioned.
3776 */
3777 virtual uint lock_count(void) const { return 1; }
3778 /**
3779 Is not invoked for non-transactional temporary tables.
3780
3781 @note store_lock() can return more than one lock if the table is MERGE
3782 or partitioned.
3783
3784 @note that one can NOT rely on table->in_use in store_lock(). It may
3785 refer to a different thread if called from mysql_lock_abort_for_thread().
3786
3787 @note If the table is MERGE, store_lock() can return less locks
3788 than lock_count() claimed. This can happen when the MERGE children
3789 are not attached when this is called from another thread.
3790 */
3791 virtual THR_LOCK_DATA **store_lock(THD *thd,
3792 THR_LOCK_DATA **to,
3793 enum thr_lock_type lock_type)=0;
3794
3795 /** Type of table for caching query */
3796 virtual uint8 table_cache_type() { return HA_CACHE_TBL_NONTRANSACT; }
3797
3798
3799 /**
3800 @brief Register a named table with a call back function to the query cache.
3801
3802 @param thd The thread handle
3803 @param table_key A pointer to the table name in the table cache
3804 @param key_length The length of the table name
3805 @param[out] engine_callback The pointer to the storage engine call back
3806 function
3807 @param[out] engine_data Storage engine specific data which could be
3808 anything
3809
3810 This method offers the storage engine, the possibility to store a reference
3811 to a table name which is going to be used with query cache.
3812 The method is called each time a statement is written to the cache and can
3813 be used to verify if a specific statement is cachable. It also offers
3814 the possibility to register a generic (but static) call back function which
3815 is called each time a statement is matched against the query cache.
3816
3817 @note If engine_data supplied with this function is different from
3818 engine_data supplied with the callback function, and the callback returns
3819 FALSE, a table invalidation on the current table will occur.
3820
3821 @return Upon success the engine_callback will point to the storage engine
3822 call back function, if any, and engine_data will point to any storage
3823 engine data used in the specific implementation.
3824 @retval TRUE Success
3825 @retval FALSE The specified table or current statement should not be
3826 cached
3827 */
3828
3829 virtual my_bool register_query_cache_table(THD *thd, const char *table_key,
3830 uint key_length,
3831 qc_engine_callback
3832 *engine_callback,
3833 ulonglong *engine_data)
3834 {
3835 *engine_callback= 0;
3836 return TRUE;
3837 }
3838
3839 /*
3840 Count tables invisible from all tables list on which current one built
3841 (like myisammrg and partitioned tables)
3842
3843 tables_type mask for the tables should be added herdde
3844
3845 returns number of such tables
3846 */
3847
3848 virtual uint count_query_cache_dependant_tables(uint8 *tables_type
3849 __attribute__((unused)))
3850 {
3851 return 0;
3852 }
3853
3854 /*
3855 register tables invisible from all tables list on which current one built
3856 (like myisammrg and partitioned tables).
3857
3858 @note they should be counted by method above
3859
3860 cache Query cache pointer
3861 block Query cache block to write the table
3862 n Number of the table
3863
3864 @retval FALSE - OK
3865 @retval TRUE - Error
3866 */
3867
3868 virtual my_bool
3869 register_query_cache_dependant_tables(THD *thd
3870 __attribute__((unused)),
3871 Query_cache *cache
3872 __attribute__((unused)),
3873 Query_cache_block_table **block
3874 __attribute__((unused)),
3875 uint *n __attribute__((unused)))
3876 {
3877 return FALSE;
3878 }
3879
3880 /*
3881 Check if the primary key (if there is one) is a clustered and a
3882 reference key. This means:
3883
3884 - Data is stored together with the primary key (no secondary lookup
3885 needed to find the row data). The optimizer uses this to find out
3886 the cost of fetching data.
3887 - The primary key is part of each secondary key and is used
3888 to find the row data in the primary index when reading trough
3889 secondary indexes.
3890 - When doing a HA_KEYREAD_ONLY we get also all the primary key parts
3891 into the row. This is critical property used by index_merge.
3892
3893 All the above is usually true for engines that store the row
3894 data in the primary key index (e.g. in a b-tree), and use the primary
3895 key value as a position(). InnoDB is an example of such an engine.
3896
3897 For such a clustered primary key, the following should also hold:
3898 index_flags() should contain HA_CLUSTERED_INDEX
3899 table_flags() should contain HA_TABLE_SCAN_ON_INDEX
3900
3901 @retval TRUE yes
3902 @retval FALSE No.
3903 */
3904 virtual bool primary_key_is_clustered() { return FALSE; }
3905 virtual int cmp_ref(const uchar *ref1, const uchar *ref2)
3906 {
3907 return memcmp(ref1, ref2, ref_length);
3908 }
3909
3910 /*
3911 Condition pushdown to storage engines
3912 */
3913
3914 /**
3915 Push condition down to the table handler.
3916
3917 @param cond Condition to be pushed. The condition tree must not be
3918 modified by the by the caller.
3919
3920 @return
3921 The 'remainder' condition that caller must use to filter out records.
3922 NULL means the handler will not return rows that do not match the
3923 passed condition.
3924
3925 @note
3926 The pushed conditions form a stack (from which one can remove the
3927 last pushed condition using cond_pop).
3928 The table handler filters out rows using (pushed_cond1 AND pushed_cond2
3929 AND ... AND pushed_condN)
3930 or less restrictive condition, depending on handler's capabilities.
3931
3932 handler->ha_reset() call empties the condition stack.
3933 Calls to rnd_init/rnd_end, index_init/index_end etc do not affect the
3934 condition stack.
3935 */
3936 virtual const COND *cond_push(const COND *cond) { return cond; };
3937 /**
3938 Pop the top condition from the condition stack of the handler instance.
3939
3940 Pops the top if condition stack, if stack is not empty.
3941 */
3942 virtual void cond_pop() { return; };
3943
3944 /**
3945 Push metadata for the current operation down to the table handler.
3946 */
3947 virtual int info_push(uint info_type, void *info) { return 0; };
3948
3949 /**
3950 This function is used to get correlating of a parent (table/column)
3951 and children (table/column). When conditions are pushed down to child
3952 table (like child of myisam_merge), child table needs to know about
3953 which table/column is my parent for understanding conditions.
3954 */
3955 virtual int set_top_table_and_fields(TABLE *top_table,
3956 Field **top_table_field,
3957 uint top_table_fields)
3958 {
3959 if (!set_top_table_fields)
3960 {
3961 set_top_table_fields= TRUE;
3962 this->top_table= top_table;
3963 this->top_table_field= top_table_field;
3964 this->top_table_fields= top_table_fields;
3965 }
3966 return 0;
3967 }
3968 virtual void clear_top_table_fields()
3969 {
3970 if (set_top_table_fields)
3971 {
3972 set_top_table_fields= FALSE;
3973 top_table= NULL;
3974 top_table_field= NULL;
3975 top_table_fields= 0;
3976 }
3977 }
3978
3979 /**
3980 Push down an index condition to the handler.
3981
3982 The server will use this method to push down a condition it wants
3983 the handler to evaluate when retrieving records using a specified
3984 index. The pushed index condition will only refer to fields from
3985 this handler that is contained in the index (but it may also refer
3986 to fields in other handlers). Before the handler evaluates the
3987 condition it must read the content of the index entry into the
3988 record buffer.
3989
3990 The handler is free to decide if and how much of the condition it
3991 will take responsibility for evaluating. Based on this evaluation
3992 it should return the part of the condition it will not evaluate.
3993 If it decides to evaluate the entire condition it should return
3994 NULL. If it decides not to evaluate any part of the condition it
3995 should return a pointer to the same condition as given as argument.
3996
3997 @param keyno the index number to evaluate the condition on
3998 @param idx_cond the condition to be evaluated by the handler
3999
4000 @return The part of the pushed condition that the handler decides
4001 not to evaluate
4002 */
4003 virtual Item *idx_cond_push(uint keyno, Item* idx_cond) { return idx_cond; }
4004
4005 /** Reset information about pushed index conditions */
4006 virtual void cancel_pushed_idx_cond()
4007 {
4008 pushed_idx_cond= NULL;
4009 pushed_idx_cond_keyno= MAX_KEY;
4010 in_range_check_pushed_down= false;
4011 }
4012
4013 /* Needed for partition / spider */
4014 virtual TABLE_LIST *get_next_global_for_child() { return NULL; }
4015
4016 /**
4017 Part of old, deprecated in-place ALTER API.
4018 */
4019 virtual bool check_if_incompatible_data(HA_CREATE_INFO *create_info,
4020 uint table_changes)
4021 { return COMPATIBLE_DATA_NO; }
4022
4023 /* On-line/in-place ALTER TABLE interface. */
4024
4025 /*
4026 Here is an outline of on-line/in-place ALTER TABLE execution through
4027 this interface.
4028
4029 Phase 1 : Initialization
4030 ========================
4031 During this phase we determine which algorithm should be used
4032 for execution of ALTER TABLE and what level concurrency it will
4033 require.
4034
4035 *) This phase starts by opening the table and preparing description
4036 of the new version of the table.
4037 *) Then we check if it is impossible even in theory to carry out
4038 this ALTER TABLE using the in-place algorithm. For example, because
4039 we need to change storage engine or the user has explicitly requested
4040 usage of the "copy" algorithm.
4041 *) If in-place ALTER TABLE is theoretically possible, we continue
4042 by compiling differences between old and new versions of the table
4043 in the form of HA_ALTER_FLAGS bitmap. We also build a few
4044 auxiliary structures describing requested changes and store
4045 all these data in the Alter_inplace_info object.
4046 *) Then the handler::check_if_supported_inplace_alter() method is called
4047 in order to find if the storage engine can carry out changes requested
4048 by this ALTER TABLE using the in-place algorithm. To determine this,
4049 the engine can rely on data in HA_ALTER_FLAGS/Alter_inplace_info
4050 passed to it as well as on its own checks. If the in-place algorithm
4051 can be used for this ALTER TABLE, the level of required concurrency for
4052 its execution is also returned.
4053 If any errors occur during the handler call, ALTER TABLE is aborted
4054 and no further handler functions are called.
4055 *) Locking requirements of the in-place algorithm are compared to any
4056 concurrency requirements specified by user. If there is a conflict
4057 between them, we either switch to the copy algorithm or emit an error.
4058
4059 Phase 2 : Execution
4060 ===================
4061
4062 In this phase the operations are executed.
4063
4064 *) As the first step, we acquire a lock corresponding to the concurrency
4065 level which was returned by handler::check_if_supported_inplace_alter()
4066 and requested by the user. This lock is held for most of the
4067 duration of in-place ALTER (if HA_ALTER_INPLACE_COPY_LOCK
4068 or HA_ALTER_INPLACE_COPY_NO_LOCK were returned we acquire an
4069 exclusive lock for duration of the next step only).
4070 *) After that we call handler::ha_prepare_inplace_alter_table() to give the
4071 storage engine a chance to update its internal structures with a higher
4072 lock level than the one that will be used for the main step of algorithm.
4073 After that we downgrade the lock if it is necessary.
4074 *) After that, the main step of this phase and algorithm is executed.
4075 We call the handler::ha_inplace_alter_table() method, which carries out the
4076 changes requested by ALTER TABLE but does not makes them visible to other
4077 connections yet.
4078 *) We ensure that no other connection uses the table by upgrading our
4079 lock on it to exclusive.
4080 *) a) If the previous step succeeds, handler::ha_commit_inplace_alter_table() is
4081 called to allow the storage engine to do any final updates to its structures,
4082 to make all earlier changes durable and visible to other connections.
4083 b) If we have failed to upgrade lock or any errors have occurred during the
4084 handler functions calls (including commit), we call
4085 handler::ha_commit_inplace_alter_table()
4086 to rollback all changes which were done during previous steps.
4087
4088 Phase 3 : Final
4089 ===============
4090
4091 In this phase we:
4092
4093 *) Update SQL-layer data-dictionary by installing .FRM file for the new version
4094 of the table.
4095 *) Inform the storage engine about this change by calling the
4096 handler::ha_notify_table_changed() method.
4097 *) Destroy the Alter_inplace_info and handler_ctx objects.
4098
4099 */
4100
4101 /**
4102 Check if a storage engine supports a particular alter table in-place
4103
4104 @param altered_table TABLE object for new version of table.
4105 @param ha_alter_info Structure describing changes to be done
4106 by ALTER TABLE and holding data used
4107 during in-place alter.
4108
4109 @retval HA_ALTER_ERROR Unexpected error.
4110 @retval HA_ALTER_INPLACE_NOT_SUPPORTED Not supported, must use copy.
4111 @retval HA_ALTER_INPLACE_EXCLUSIVE_LOCK Supported, but requires X lock.
4112 @retval HA_ALTER_INPLACE_COPY_LOCK
4113 Supported, but requires SNW lock
4114 during main phase. Prepare phase
4115 requires X lock.
4116 @retval HA_ALTER_INPLACE_SHARED_LOCK Supported, but requires SNW lock.
4117 @retval HA_ALTER_INPLACE_COPY_NO_LOCK
4118 Supported, concurrent reads/writes
4119 allowed. However, prepare phase
4120 requires X lock.
4121 @retval HA_ALTER_INPLACE_NO_LOCK Supported, concurrent
4122 reads/writes allowed.
4123
4124 @note The default implementation uses the old in-place ALTER API
4125 to determine if the storage engine supports in-place ALTER or not.
4126
4127 @note Called without holding thr_lock.c lock.
4128 */
4129 virtual enum_alter_inplace_result
4130 check_if_supported_inplace_alter(TABLE *altered_table,
4131 Alter_inplace_info *ha_alter_info);
4132
4133
4134 /**
4135 Public functions wrapping the actual handler call.
4136 @see prepare_inplace_alter_table()
4137 */
4138 bool ha_prepare_inplace_alter_table(TABLE *altered_table,
4139 Alter_inplace_info *ha_alter_info);
4140
4141
4142 /**
4143 Public function wrapping the actual handler call.
4144 @see inplace_alter_table()
4145 */
4146 bool ha_inplace_alter_table(TABLE *altered_table,
4147 Alter_inplace_info *ha_alter_info)
4148 {
4149 return inplace_alter_table(altered_table, ha_alter_info);
4150 }
4151
4152
4153 /**
4154 Public function wrapping the actual handler call.
4155 Allows us to enforce asserts regardless of handler implementation.
4156 @see commit_inplace_alter_table()
4157 */
4158 bool ha_commit_inplace_alter_table(TABLE *altered_table,
4159 Alter_inplace_info *ha_alter_info,
4160 bool commit);
4161
4162
4163 /**
4164 Public function wrapping the actual handler call.
4165 @see notify_table_changed()
4166 */
4167 void ha_notify_table_changed()
4168 {
4169 notify_table_changed();
4170 }
4171
4172
4173protected:
4174 /**
4175 Allows the storage engine to update internal structures with concurrent
4176 writes blocked. If check_if_supported_inplace_alter() returns
4177 HA_ALTER_INPLACE_COPY_NO_LOCK or HA_ALTER_INPLACE_COPY_LOCK,
4178 this function is called with exclusive lock otherwise the same level
4179 of locking as for inplace_alter_table() will be used.
4180
4181 @note Storage engines are responsible for reporting any errors by
4182 calling my_error()/print_error()
4183
4184 @note If this function reports error, commit_inplace_alter_table()
4185 will be called with commit= false.
4186
4187 @note For partitioning, failing to prepare one partition, means that
4188 commit_inplace_alter_table() will be called to roll back changes for
4189 all partitions. This means that commit_inplace_alter_table() might be
4190 called without prepare_inplace_alter_table() having been called first
4191 for a given partition.
4192
4193 @param altered_table TABLE object for new version of table.
4194 @param ha_alter_info Structure describing changes to be done
4195 by ALTER TABLE and holding data used
4196 during in-place alter.
4197
4198 @retval true Error
4199 @retval false Success
4200 */
4201 virtual bool prepare_inplace_alter_table(TABLE *altered_table,
4202 Alter_inplace_info *ha_alter_info)
4203 { return false; }
4204
4205
4206 /**
4207 Alter the table structure in-place with operations specified using HA_ALTER_FLAGS
4208 and Alter_inplace_info. The level of concurrency allowed during this
4209 operation depends on the return value from check_if_supported_inplace_alter().
4210
4211 @note Storage engines are responsible for reporting any errors by
4212 calling my_error()/print_error()
4213
4214 @note If this function reports error, commit_inplace_alter_table()
4215 will be called with commit= false.
4216
4217 @param altered_table TABLE object for new version of table.
4218 @param ha_alter_info Structure describing changes to be done
4219 by ALTER TABLE and holding data used
4220 during in-place alter.
4221
4222 @retval true Error
4223 @retval false Success
4224 */
4225 virtual bool inplace_alter_table(TABLE *altered_table,
4226 Alter_inplace_info *ha_alter_info)
4227 { return false; }
4228
4229
4230 /**
4231 Commit or rollback the changes made during prepare_inplace_alter_table()
4232 and inplace_alter_table() inside the storage engine.
4233 Note that in case of rollback the allowed level of concurrency during
4234 this operation will be the same as for inplace_alter_table() and thus
4235 might be higher than during prepare_inplace_alter_table(). (For example,
4236 concurrent writes were blocked during prepare, but might not be during
4237 rollback).
4238
4239 @note Storage engines are responsible for reporting any errors by
4240 calling my_error()/print_error()
4241
4242 @note If this function with commit= true reports error, it will be called
4243 again with commit= false.
4244
4245 @note In case of partitioning, this function might be called for rollback
4246 without prepare_inplace_alter_table() having been called first.
4247 Also partitioned tables sets ha_alter_info->group_commit_ctx to a NULL
4248 terminated array of the partitions handlers and if all of them are
4249 committed as one, then group_commit_ctx should be set to NULL to indicate
4250 to the partitioning handler that all partitions handlers are committed.
4251 @see prepare_inplace_alter_table().
4252
4253 @param altered_table TABLE object for new version of table.
4254 @param ha_alter_info Structure describing changes to be done
4255 by ALTER TABLE and holding data used
4256 during in-place alter.
4257 @param commit True => Commit, False => Rollback.
4258
4259 @retval true Error
4260 @retval false Success
4261 */
4262 virtual bool commit_inplace_alter_table(TABLE *altered_table,
4263 Alter_inplace_info *ha_alter_info,
4264 bool commit)
4265{
4266 /* Nothing to commit/rollback, mark all handlers committed! */
4267 ha_alter_info->group_commit_ctx= NULL;
4268 return false;
4269}
4270
4271
4272 /**
4273 Notify the storage engine that the table structure (.FRM) has been updated.
4274
4275 @note No errors are allowed during notify_table_changed().
4276 */
4277 virtual void notify_table_changed() { }
4278
4279public:
4280 /* End of On-line/in-place ALTER TABLE interface. */
4281
4282
4283 /**
4284 use_hidden_primary_key() is called in case of an update/delete when
4285 (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
4286 but we don't have a primary key
4287 */
4288 virtual void use_hidden_primary_key();
4289 virtual alter_table_operations alter_table_flags(alter_table_operations flags)
4290 {
4291 if (ht->alter_table_flags)
4292 return ht->alter_table_flags(flags);
4293 return 0;
4294 }
4295
4296 virtual LEX_CSTRING *engine_name();
4297
4298 TABLE* get_table() { return table; }
4299 TABLE_SHARE* get_table_share() { return table_share; }
4300protected:
4301 /* Service methods for use by storage engines. */
4302 void **ha_data(THD *) const;
4303 THD *ha_thd(void) const;
4304
4305 /**
4306 Acquire the instrumented table information from a table share.
4307 @return an instrumented table share, or NULL.
4308 */
4309 PSI_table_share *ha_table_share_psi() const;
4310
4311 /**
4312 Default rename_table() and delete_table() rename/delete files with a
4313 given name and extensions from bas_ext().
4314
4315 These methods can be overridden, but their default implementation
4316 provide useful functionality.
4317 */
4318 virtual int rename_table(const char *from, const char *to);
4319 /**
4320 Delete a table in the engine. Called for base as well as temporary
4321 tables.
4322 */
4323 virtual int delete_table(const char *name);
4324
4325public:
4326 bool check_table_binlog_row_based(bool binlog_row);
4327
4328 /* Cache result to avoid extra calls */
4329 inline void mark_trx_read_write()
4330 {
4331 if (unlikely(!mark_trx_read_write_done))
4332 {
4333 mark_trx_read_write_done= 1;
4334 mark_trx_read_write_internal();
4335 }
4336 }
4337
4338private:
4339 void mark_trx_read_write_internal();
4340 bool check_table_binlog_row_based_internal(bool binlog_row);
4341
4342protected:
4343 /*
4344 These are intended to be used only by handler::ha_xxxx() functions
4345 However, engines that implement read_range_XXX() (like MariaRocks)
4346 or embed other engines (like ha_partition) may need to call these also
4347 */
4348 inline void increment_statistics(ulong SSV::*offset) const;
4349 inline void decrement_statistics(ulong SSV::*offset) const;
4350
4351private:
4352 /*
4353 Low-level primitives for storage engines. These should be
4354 overridden by the storage engine class. To call these methods, use
4355 the corresponding 'ha_*' method above.
4356 */
4357
4358 virtual int open(const char *name, int mode, uint test_if_locked)=0;
4359 /* Note: ha_index_read_idx_map() may bypass index_init() */
4360 virtual int index_init(uint idx, bool sorted) { return 0; }
4361 virtual int index_end() { return 0; }
4362 /**
4363 rnd_init() can be called two times without rnd_end() in between
4364 (it only makes sense if scan=1).
4365 then the second call should prepare for the new table scan (e.g
4366 if rnd_init allocates the cursor, second call should position it
4367 to the start of the table, no need to deallocate and allocate it again
4368 */
4369 virtual int rnd_init(bool scan)= 0;
4370 virtual int rnd_end() { return 0; }
4371 virtual int write_row(uchar *buf __attribute__((unused)))
4372 {
4373 return HA_ERR_WRONG_COMMAND;
4374 }
4375
4376 /**
4377 Update a single row.
4378
4379 Note: If HA_ERR_FOUND_DUPP_KEY is returned, the handler must read
4380 all columns of the row so MySQL can create an error message. If
4381 the columns required for the error message are not read, the error
4382 message will contain garbage.
4383 */
4384 virtual int update_row(const uchar *old_data __attribute__((unused)),
4385 const uchar *new_data __attribute__((unused)))
4386 {
4387 return HA_ERR_WRONG_COMMAND;
4388 }
4389
4390 /*
4391 Optimized function for updating the first row. Only used by sequence
4392 tables
4393 */
4394 virtual int update_first_row(uchar *new_data);
4395
4396 virtual int delete_row(const uchar *buf __attribute__((unused)))
4397 {
4398 return HA_ERR_WRONG_COMMAND;
4399 }
4400
4401 /* Perform initialization for a direct update request */
4402public:
4403 int ha_direct_update_rows(ha_rows *update_rows);
4404 virtual int direct_update_rows_init()
4405 {
4406 return HA_ERR_WRONG_COMMAND;
4407 }
4408private:
4409 virtual int pre_direct_update_rows_init()
4410 {
4411 return HA_ERR_WRONG_COMMAND;
4412 }
4413 virtual int direct_update_rows(ha_rows *update_rows __attribute__((unused)))
4414 {
4415 return HA_ERR_WRONG_COMMAND;
4416 }
4417 virtual int pre_direct_update_rows()
4418 {
4419 return HA_ERR_WRONG_COMMAND;
4420 }
4421
4422 /* Perform initialization for a direct delete request */
4423public:
4424 int ha_direct_delete_rows(ha_rows *delete_rows);
4425 virtual int direct_delete_rows_init()
4426 {
4427 return HA_ERR_WRONG_COMMAND;
4428 }
4429private:
4430 virtual int pre_direct_delete_rows_init()
4431 {
4432 return HA_ERR_WRONG_COMMAND;
4433 }
4434 virtual int direct_delete_rows(ha_rows *delete_rows __attribute__((unused)))
4435 {
4436 return HA_ERR_WRONG_COMMAND;
4437 }
4438 virtual int pre_direct_delete_rows()
4439 {
4440 return HA_ERR_WRONG_COMMAND;
4441 }
4442
4443 /**
4444 Reset state of file to after 'open'.
4445 This function is called after every statement for all tables used
4446 by that statement.
4447 */
4448 virtual int reset() { return 0; }
4449 virtual Table_flags table_flags(void) const= 0;
4450 /**
4451 Is not invoked for non-transactional temporary tables.
4452
4453 Tells the storage engine that we intend to read or write data
4454 from the table. This call is prefixed with a call to handler::store_lock()
4455 and is invoked only for those handler instances that stored the lock.
4456
4457 Calls to rnd_init/index_init are prefixed with this call. When table
4458 IO is complete, we call external_lock(F_UNLCK).
4459 A storage engine writer should expect that each call to
4460 ::external_lock(F_[RD|WR]LOCK is followed by a call to
4461 ::external_lock(F_UNLCK). If it is not, it is a bug in MySQL.
4462
4463 The name and signature originate from the first implementation
4464 in MyISAM, which would call fcntl to set/clear an advisory
4465 lock on the data file in this method.
4466
4467 @param lock_type F_RDLCK, F_WRLCK, F_UNLCK
4468
4469 @return non-0 in case of failure, 0 in case of success.
4470 When lock_type is F_UNLCK, the return value is ignored.
4471 */
4472 virtual int external_lock(THD *thd __attribute__((unused)),
4473 int lock_type __attribute__((unused)))
4474 {
4475 return 0;
4476 }
4477 virtual void release_auto_increment() { return; };
4478 /** admin commands - called from mysql_admin_table */
4479 virtual int check_for_upgrade(HA_CHECK_OPT *check_opt)
4480 { return 0; }
4481 virtual int check(THD* thd, HA_CHECK_OPT* check_opt)
4482 { return HA_ADMIN_NOT_IMPLEMENTED; }
4483
4484 /**
4485 In this method check_opt can be modified
4486 to specify CHECK option to use to call check()
4487 upon the table.
4488 */
4489 virtual int repair(THD* thd, HA_CHECK_OPT* check_opt)
4490 {
4491 DBUG_ASSERT(!(ha_table_flags() & HA_CAN_REPAIR));
4492 return HA_ADMIN_NOT_IMPLEMENTED;
4493 }
4494 virtual void start_bulk_insert(ha_rows rows, uint flags) {}
4495 virtual int end_bulk_insert() { return 0; }
4496protected:
4497 virtual int index_read(uchar * buf, const uchar * key, uint key_len,
4498 enum ha_rkey_function find_flag)
4499 { return HA_ERR_WRONG_COMMAND; }
4500 virtual int index_read_last(uchar * buf, const uchar * key, uint key_len)
4501 {
4502 my_errno= HA_ERR_WRONG_COMMAND;
4503 return HA_ERR_WRONG_COMMAND;
4504 }
4505 friend class ha_partition;
4506 friend class ha_sequence;
4507public:
4508 /**
4509 This method is similar to update_row, however the handler doesn't need
4510 to execute the updates at this point in time. The handler can be certain
4511 that another call to bulk_update_row will occur OR a call to
4512 exec_bulk_update before the set of updates in this query is concluded.
4513
4514 @param old_data Old record
4515 @param new_data New record
4516 @param dup_key_found Number of duplicate keys found
4517
4518 @retval 0 Bulk delete used by handler
4519 @retval 1 Bulk delete not used, normal operation used
4520 */
4521 virtual int bulk_update_row(const uchar *old_data, const uchar *new_data,
4522 ha_rows *dup_key_found)
4523 {
4524 DBUG_ASSERT(FALSE);
4525 return HA_ERR_WRONG_COMMAND;
4526 }
4527 /**
4528 This is called to delete all rows in a table
4529 If the handler don't support this, then this function will
4530 return HA_ERR_WRONG_COMMAND and MySQL will delete the rows one
4531 by one.
4532 */
4533 virtual int delete_all_rows()
4534 { return (my_errno=HA_ERR_WRONG_COMMAND); }
4535 /**
4536 Quickly remove all rows from a table.
4537
4538 @remark This method is responsible for implementing MySQL's TRUNCATE
4539 TABLE statement, which is a DDL operation. As such, a engine
4540 can bypass certain integrity checks and in some cases avoid
4541 fine-grained locking (e.g. row locks) which would normally be
4542 required for a DELETE statement.
4543
4544 @remark Typically, truncate is not used if it can result in integrity
4545 violation. For example, truncate is not used when a foreign
4546 key references the table, but it might be used if foreign key
4547 checks are disabled.
4548
4549 @remark Engine is responsible for resetting the auto-increment counter.
4550
4551 @remark The table is locked in exclusive mode.
4552 */
4553 virtual int truncate()
4554 {
4555 int error= delete_all_rows();
4556 return error ? error : reset_auto_increment(0);
4557 }
4558 /**
4559 Reset the auto-increment counter to the given value, i.e. the next row
4560 inserted will get the given value.
4561 */
4562 virtual int reset_auto_increment(ulonglong value)
4563 { return 0; }
4564 virtual int optimize(THD* thd, HA_CHECK_OPT* check_opt)
4565 { return HA_ADMIN_NOT_IMPLEMENTED; }
4566 virtual int analyze(THD* thd, HA_CHECK_OPT* check_opt)
4567 { return HA_ADMIN_NOT_IMPLEMENTED; }
4568 virtual bool check_and_repair(THD *thd) { return TRUE; }
4569 virtual int disable_indexes(uint mode) { return HA_ERR_WRONG_COMMAND; }
4570 virtual int enable_indexes(uint mode) { return HA_ERR_WRONG_COMMAND; }
4571 virtual int discard_or_import_tablespace(my_bool discard)
4572 { return (my_errno=HA_ERR_WRONG_COMMAND); }
4573 virtual void prepare_for_alter() { return; }
4574 virtual void drop_table(const char *name);
4575 virtual int create(const char *name, TABLE *form, HA_CREATE_INFO *info)=0;
4576
4577 virtual int create_partitioning_metadata(const char *name, const char *old_name,
4578 int action_flag)
4579 { return FALSE; }
4580
4581 virtual int change_partitions(HA_CREATE_INFO *create_info,
4582 const char *path,
4583 ulonglong * const copied,
4584 ulonglong * const deleted,
4585 const uchar *pack_frm_data,
4586 size_t pack_frm_len)
4587 { return HA_ERR_WRONG_COMMAND; }
4588 virtual int drop_partitions(const char *path)
4589 { return HA_ERR_WRONG_COMMAND; }
4590 virtual int rename_partitions(const char *path)
4591 { return HA_ERR_WRONG_COMMAND; }
4592 virtual bool set_ha_share_ref(Handler_share **arg_ha_share)
4593 {
4594 DBUG_ASSERT(!ha_share);
4595 DBUG_ASSERT(arg_ha_share);
4596 if (ha_share || !arg_ha_share)
4597 return true;
4598 ha_share= arg_ha_share;
4599 return false;
4600 }
4601 int get_lock_type() const { return m_lock_type; }
4602public:
4603 /* XXX to be removed, see ha_partition::partition_ht() */
4604 virtual handlerton *partition_ht() const
4605 { return ht; }
4606 inline int ha_write_tmp_row(uchar *buf);
4607 inline int ha_delete_tmp_row(uchar *buf);
4608 inline int ha_update_tmp_row(const uchar * old_data, uchar * new_data);
4609
4610 virtual void set_lock_type(enum thr_lock_type lock);
4611
4612 friend enum icp_result handler_index_cond_check(void* h_arg);
4613
4614 /**
4615 Find unique record by index or unique constrain
4616
4617 @param record record to find (also will be fillded with
4618 actual record fields)
4619 @param unique_ref index or unique constraiun number (depends
4620 on what used in the engine
4621
4622 @retval -1 Error
4623 @retval 1 Not found
4624 @retval 0 Found
4625 */
4626 virtual int find_unique_row(uchar *record, uint unique_ref)
4627 { return -1; /*unsupported */}
4628
4629 bool native_versioned() const
4630 { DBUG_ASSERT(ht); return partition_ht()->flags & HTON_NATIVE_SYS_VERSIONING; }
4631 virtual void update_partition(uint part_id)
4632 {}
4633protected:
4634 Handler_share *get_ha_share_ptr();
4635 void set_ha_share_ptr(Handler_share *arg_ha_share);
4636 void lock_shared_ha_data();
4637 void unlock_shared_ha_data();
4638};
4639
4640#include "multi_range_read.h"
4641#include "group_by_handler.h"
4642
4643bool key_uses_partial_cols(TABLE_SHARE *table, uint keyno);
4644
4645 /* Some extern variables used with handlers */
4646
4647extern const char *ha_row_type[];
4648extern MYSQL_PLUGIN_IMPORT const char *tx_isolation_names[];
4649extern MYSQL_PLUGIN_IMPORT const char *binlog_format_names[];
4650extern TYPELIB tx_isolation_typelib;
4651extern const char *myisam_stats_method_names[];
4652extern ulong total_ha, total_ha_2pc;
4653
4654/* lookups */
4655plugin_ref ha_resolve_by_name(THD *thd, const LEX_CSTRING *name, bool tmp_table);
4656plugin_ref ha_lock_engine(THD *thd, const handlerton *hton);
4657handlerton *ha_resolve_by_legacy_type(THD *thd, enum legacy_db_type db_type);
4658handler *get_new_handler(TABLE_SHARE *share, MEM_ROOT *alloc,
4659 handlerton *db_type);
4660handlerton *ha_checktype(THD *thd, handlerton *hton, bool no_substitute);
4661
4662static inline handlerton *ha_checktype(THD *thd, enum legacy_db_type type,
4663 bool no_substitute = 0)
4664{
4665 return ha_checktype(thd, ha_resolve_by_legacy_type(thd, type), no_substitute);
4666}
4667
4668static inline enum legacy_db_type ha_legacy_type(const handlerton *db_type)
4669{
4670 return (db_type == NULL) ? DB_TYPE_UNKNOWN : db_type->db_type;
4671}
4672
4673static inline const char *ha_resolve_storage_engine_name(const handlerton *db_type)
4674{
4675 return db_type == NULL ? "UNKNOWN" : hton_name(db_type)->str;
4676}
4677
4678static inline bool ha_check_storage_engine_flag(const handlerton *db_type, uint32 flag)
4679{
4680 return db_type == NULL ? FALSE : MY_TEST(db_type->flags & flag);
4681}
4682
4683static inline bool ha_storage_engine_is_enabled(const handlerton *db_type)
4684{
4685 return (db_type && db_type->create) ?
4686 (db_type->state == SHOW_OPTION_YES) : FALSE;
4687}
4688
4689#define view_pseudo_hton ((handlerton *)1)
4690
4691/* basic stuff */
4692int ha_init_errors(void);
4693int ha_init(void);
4694int ha_end(void);
4695int ha_initialize_handlerton(st_plugin_int *plugin);
4696int ha_finalize_handlerton(st_plugin_int *plugin);
4697
4698TYPELIB *ha_known_exts(void);
4699int ha_panic(enum ha_panic_function flag);
4700void ha_close_connection(THD* thd);
4701void ha_kill_query(THD* thd, enum thd_kill_levels level);
4702bool ha_flush_logs(handlerton *db_type);
4703void ha_drop_database(char* path);
4704void ha_checkpoint_state(bool disable);
4705void ha_commit_checkpoint_request(void *cookie, void (*pre_hook)(void *));
4706int ha_create_table(THD *thd, const char *path,
4707 const char *db, const char *table_name,
4708 HA_CREATE_INFO *create_info, LEX_CUSTRING *frm);
4709int ha_delete_table(THD *thd, handlerton *db_type, const char *path,
4710 const LEX_CSTRING *db, const LEX_CSTRING *alias, bool generate_warning);
4711
4712/* statistics and info */
4713bool ha_show_status(THD *thd, handlerton *db_type, enum ha_stat_type stat);
4714
4715/* discovery */
4716#ifdef MYSQL_SERVER
4717class Discovered_table_list: public handlerton::discovered_list
4718{
4719 THD *thd;
4720 const char *wild, *wend;
4721 bool with_temps; // whether to include temp tables in the result
4722public:
4723 Dynamic_array<LEX_CSTRING*> *tables;
4724
4725 Discovered_table_list(THD *thd_arg, Dynamic_array<LEX_CSTRING*> *tables_arg,
4726 const LEX_CSTRING *wild_arg);
4727 Discovered_table_list(THD *thd_arg, Dynamic_array<LEX_CSTRING*> *tables_arg)
4728 : thd(thd_arg), wild(NULL), with_temps(true), tables(tables_arg) {}
4729 ~Discovered_table_list() {}
4730
4731 bool add_table(const char *tname, size_t tlen);
4732 bool add_file(const char *fname);
4733
4734 void sort();
4735 void remove_duplicates(); // assumes that the list is sorted
4736#ifndef DBUG_OFF
4737 /*
4738 Used to find unstable mtr tests querying
4739 INFORMATION_SCHEMA.TABLES without ORDER BY.
4740 */
4741 void sort_desc();
4742#endif
4743};
4744
4745int ha_discover_table(THD *thd, TABLE_SHARE *share);
4746int ha_discover_table_names(THD *thd, LEX_CSTRING *db, MY_DIR *dirp,
4747 Discovered_table_list *result, bool reusable);
4748bool ha_table_exists(THD *thd, const LEX_CSTRING *db, const LEX_CSTRING *table_name,
4749 handlerton **hton= 0, bool *is_sequence= 0);
4750#endif
4751
4752/* key cache */
4753extern "C" int ha_init_key_cache(const char *name, KEY_CACHE *key_cache, void *);
4754int ha_resize_key_cache(KEY_CACHE *key_cache);
4755int ha_change_key_cache_param(KEY_CACHE *key_cache);
4756int ha_repartition_key_cache(KEY_CACHE *key_cache);
4757int ha_change_key_cache(KEY_CACHE *old_key_cache, KEY_CACHE *new_key_cache);
4758
4759/* transactions: interface to handlerton functions */
4760int ha_start_consistent_snapshot(THD *thd);
4761int ha_commit_or_rollback_by_xid(XID *xid, bool commit);
4762int ha_commit_one_phase(THD *thd, bool all);
4763int ha_commit_trans(THD *thd, bool all);
4764int ha_rollback_trans(THD *thd, bool all);
4765int ha_prepare(THD *thd);
4766int ha_recover(HASH *commit_list);
4767
4768/* transactions: these functions never call handlerton functions directly */
4769int ha_enable_transaction(THD *thd, bool on);
4770
4771/* savepoints */
4772int ha_rollback_to_savepoint(THD *thd, SAVEPOINT *sv);
4773bool ha_rollback_to_savepoint_can_release_mdl(THD *thd);
4774int ha_savepoint(THD *thd, SAVEPOINT *sv);
4775int ha_release_savepoint(THD *thd, SAVEPOINT *sv);
4776#ifdef WITH_WSREP
4777int ha_abort_transaction(THD *bf_thd, THD *victim_thd, my_bool signal);
4778void ha_fake_trx_id(THD *thd);
4779#else
4780inline void ha_fake_trx_id(THD *thd) { }
4781#endif
4782
4783/* these are called by storage engines */
4784void trans_register_ha(THD *thd, bool all, handlerton *ht);
4785
4786/*
4787 Storage engine has to assume the transaction will end up with 2pc if
4788 - there is more than one 2pc-capable storage engine available
4789 - in the current transaction 2pc was not disabled yet
4790*/
4791#define trans_need_2pc(thd, all) ((total_ha_2pc > 1) && \
4792 !((all ? &thd->transaction.all : &thd->transaction.stmt)->no_2pc))
4793
4794const char *get_canonical_filename(handler *file, const char *path,
4795 char *tmp_path);
4796bool mysql_xa_recover(THD *thd);
4797void commit_checkpoint_notify_ha(handlerton *hton, void *cookie);
4798
4799inline const LEX_CSTRING *table_case_name(HA_CREATE_INFO *info, const LEX_CSTRING *name)
4800{
4801 return ((lower_case_table_names == 2 && info->alias.str) ? &info->alias : name);
4802}
4803
4804typedef bool Log_func(THD*, TABLE*, bool, const uchar*, const uchar*);
4805int binlog_log_row(TABLE* table,
4806 const uchar *before_record,
4807 const uchar *after_record,
4808 Log_func *log_func);
4809
4810#define TABLE_IO_WAIT(TRACKER, PSI, OP, INDEX, FLAGS, PAYLOAD) \
4811 { \
4812 Exec_time_tracker *this_tracker; \
4813 if (unlikely((this_tracker= tracker))) \
4814 tracker->start_tracking(); \
4815 \
4816 MYSQL_TABLE_IO_WAIT(PSI, OP, INDEX, FLAGS, PAYLOAD); \
4817 \
4818 if (unlikely(this_tracker)) \
4819 tracker->stop_tracking(); \
4820 }
4821
4822void print_keydup_error(TABLE *table, KEY *key, const char *msg, myf errflag);
4823void print_keydup_error(TABLE *table, KEY *key, myf errflag);
4824
4825int del_global_index_stat(THD *thd, TABLE* table, KEY* key_info);
4826int del_global_table_stat(THD *thd, LEX_CSTRING *db, LEX_CSTRING *table);
4827#endif /* HANDLER_INCLUDED */
4828