1#ifndef HANDLER_INCLUDED
2#define HANDLER_INCLUDED
3
4/*
5   Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
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#include "my_pthread.h"
25#include <algorithm>
26#include "sql_const.h"
27#include "mysqld.h"                             /* server_id */
28#include "sql_plugin.h"        /* plugin_ref, st_plugin_int, plugin */
29#include "thr_lock.h"          /* thr_lock_type, THR_LOCK_DATA */
30#include "sql_cache.h"
31#include "structs.h"                            /* SHOW_COMP_OPTION */
32
33#include <my_global.h>
34#include <my_compare.h>
35#include <ft_global.h>
36#include <keycache.h>
37
38class Alter_info;
39typedef struct xid_t XID;
40
41class SE_cost_constants;                        // see opt_costconstants.h
42
43// the following is for checking tables
44
45#define HA_ADMIN_ALREADY_DONE	  1
46#define HA_ADMIN_OK               0
47#define HA_ADMIN_NOT_IMPLEMENTED -1
48#define HA_ADMIN_FAILED		 -2
49#define HA_ADMIN_CORRUPT         -3
50#define HA_ADMIN_INTERNAL_ERROR  -4
51#define HA_ADMIN_INVALID         -5
52#define HA_ADMIN_REJECT          -6
53#define HA_ADMIN_TRY_ALTER       -7
54#define HA_ADMIN_WRONG_CHECKSUM  -8
55#define HA_ADMIN_NOT_BASE_TABLE  -9
56#define HA_ADMIN_NEEDS_UPGRADE  -10
57#define HA_ADMIN_NEEDS_ALTER    -11
58#define HA_ADMIN_NEEDS_CHECK    -12
59
60/**
61   Return values for check_if_supported_inplace_alter().
62
63   @see check_if_supported_inplace_alter() for description of
64   the individual values.
65*/
66enum enum_alter_inplace_result {
67  HA_ALTER_ERROR,
68  HA_ALTER_INPLACE_NOT_SUPPORTED,
69  HA_ALTER_INPLACE_EXCLUSIVE_LOCK,
70  HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE,
71  HA_ALTER_INPLACE_SHARED_LOCK,
72  HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE,
73  HA_ALTER_INPLACE_NO_LOCK
74};
75
76/* Bits in table_flags() to show what database can do */
77
78#define HA_NO_TRANSACTIONS     (1 << 0) /* Doesn't support transactions */
79#define HA_PARTIAL_COLUMN_READ (1 << 1) /* read may not return all columns */
80#define HA_TABLE_SCAN_ON_INDEX (1 << 2) /* No separate data/index file */
81/*
82  The following should be set if the following is not true when scanning
83  a table with rnd_next()
84  - We will see all rows (including deleted ones)
85  - Row positions are 'table->s->db_record_offset' apart
86  If this flag is not set, filesort will do a position() call for each matched
87  row to be able to find the row later.
88*/
89#define HA_REC_NOT_IN_SEQ      (1 << 3)
90#define HA_CAN_GEOMETRY        (1 << 4)
91/*
92  Reading keys in random order is as fast as reading keys in sort order
93  (Used in records.cc to decide if we should use a record cache and by
94  filesort to decide if we should sort key + data or key + pointer-to-row
95*/
96#define HA_FAST_KEY_READ       (1 << 5)
97/*
98  Set the following flag if we on delete should force all key to be read
99  and on update read all keys that changes
100*/
101#define HA_REQUIRES_KEY_COLUMNS_FOR_DELETE (1 << 6)
102#define HA_NULL_IN_KEY         (1 << 7) /* One can have keys with NULL */
103#define HA_DUPLICATE_POS       (1 << 8)    /* position() gives dup row */
104#define HA_NO_BLOBS            (1 << 9) /* Doesn't support blobs */
105#define HA_CAN_INDEX_BLOBS     (1 << 10)
106#define HA_AUTO_PART_KEY       (1 << 11) /* auto-increment in multi-part key */
107#define HA_REQUIRE_PRIMARY_KEY (1 << 12) /* .. and can't create a hidden one */
108#define HA_STATS_RECORDS_IS_EXACT (1 << 13) /* stats.records is exact */
109/// Not in use.
110#define HA_UNUSED  (1 << 14)
111/*
112  If we get the primary key columns for free when we do an index read
113  (usually, it also implies that HA_PRIMARY_KEY_REQUIRED_FOR_POSITION
114  flag is set).
115*/
116#define HA_PRIMARY_KEY_IN_READ_INDEX (1 << 15)
117/*
118  If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, it means that to position()
119  uses a primary key given by the record argument.
120  Without primary key, we can't call position().
121  If not set, the position is returned as the current rows position
122  regardless of what argument is given.
123*/
124#define HA_PRIMARY_KEY_REQUIRED_FOR_POSITION (1 << 16)
125#define HA_CAN_RTREEKEYS       (1 << 17)
126#define HA_NOT_DELETE_WITH_CACHE (1 << 18)
127/*
128  The following is we need to a primary key to delete (and update) a row.
129  If there is no primary key, all columns needs to be read on update and delete
130*/
131#define HA_PRIMARY_KEY_REQUIRED_FOR_DELETE (1 << 19)
132#define HA_NO_PREFIX_CHAR_KEYS (1 << 20)
133#define HA_CAN_FULLTEXT        (1 << 21)
134#define HA_CAN_SQL_HANDLER     (1 << 22)
135#define HA_NO_AUTO_INCREMENT   (1 << 23)
136#define HA_HAS_CHECKSUM        (1 << 24)
137/* Table data are stored in separate files (for lower_case_table_names) */
138#define HA_FILE_BASED	       (1 << 26)
139#define HA_NO_VARCHAR	       (1 << 27)
140#define HA_CAN_BIT_FIELD       (1 << 28) /* supports bit fields */
141#define HA_ANY_INDEX_MAY_BE_UNIQUE (1 << 30)
142#define HA_NO_COPY_ON_ALTER    (LL(1) << 31)
143#define HA_HAS_RECORDS	       (LL(1) << 32) /* records() gives exact count*/
144/* Has it's own method of binlog logging */
145#define HA_HAS_OWN_BINLOGGING  (LL(1) << 33)
146/*
147  Engine is capable of row-format and statement-format logging,
148  respectively
149*/
150#define HA_BINLOG_ROW_CAPABLE  (LL(1) << 34)
151#define HA_BINLOG_STMT_CAPABLE (LL(1) << 35)
152/*
153    When a multiple key conflict happens in a REPLACE command mysql
154    expects the conflicts to be reported in the ascending order of
155    key names.
156
157    For e.g.
158
159    CREATE TABLE t1 (a INT, UNIQUE (a), b INT NOT NULL, UNIQUE (b), c INT NOT
160                     NULL, INDEX(c));
161
162    REPLACE INTO t1 VALUES (1,1,1),(2,2,2),(2,1,3);
163
164    MySQL expects the conflict with 'a' to be reported before the conflict with
165    'b'.
166
167    If the underlying storage engine does not report the conflicting keys in
168    ascending order, it causes unexpected errors when the REPLACE command is
169    executed.
170
171    This flag helps the underlying SE to inform the server that the keys are not
172    ordered.
173*/
174#define HA_DUPLICATE_KEY_NOT_IN_ORDER    (LL(1) << 36)
175/*
176  Engine supports REPAIR TABLE. Used by CHECK TABLE FOR UPGRADE if an
177  incompatible table is detected. If this flag is set, CHECK TABLE FOR UPGRADE
178  will report ER_TABLE_NEEDS_UPGRADE, otherwise ER_TABLE_NEED_REBUILD.
179*/
180#define HA_CAN_REPAIR                    (LL(1) << 37)
181
182/*
183  Set of all binlog flags. Currently only contain the capabilities
184  flags.
185 */
186#define HA_BINLOG_FLAGS (HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE)
187
188/**
189  The handler supports read before write removal optimization
190
191  Read before write removal may be used for storage engines which support
192  write without previous read of the row to be updated. Handler returning
193  this flag must implement start_read_removal() and end_read_removal().
194  The handler may return "fake" rows constructed from the key of the row
195  asked for. This is used to optimize UPDATE and DELETE by reducing the
196  numer of roundtrips between handler and storage engine.
197
198  Example:
199  UPDATE a=1 WHERE pk IN (<keys>)
200
201  mysql_update()
202  {
203    if (<conditions for starting read removal>)
204      start_read_removal()
205      -> handler returns true if read removal supported for this table/query
206
207    while(read_record("pk=<key>"))
208      -> handler returns fake row with column "pk" set to <key>
209
210      ha_update_row()
211      -> handler sends write "a=1" for row with "pk=<key>"
212
213    end_read_removal()
214    -> handler returns the number of rows actually written
215  }
216
217  @note This optimization in combination with batching may be used to
218        remove even more roundtrips.
219*/
220#define HA_READ_BEFORE_WRITE_REMOVAL  (LL(1) << 38)
221
222/*
223  Engine supports extended fulltext API
224 */
225#define HA_CAN_FULLTEXT_EXT              (LL(1) << 39)
226
227/*
228  Storage engine doesn't synchronize result set with expected table contents.
229  Used by replication slave to check if it is possible to retrieve rows from
230  the table when deciding whether to do a full table scan, index scan or hash
231  scan while applying a row event.
232 */
233#define HA_READ_OUT_OF_SYNC              (LL(1) << 40)
234
235/*
236  Storage engine supports table export using the
237  FLUSH TABLE <table_list> FOR EXPORT statement.
238 */
239#define HA_CAN_EXPORT                 (LL(1) << 41)
240
241/*
242  The handler don't want accesses to this table to
243  be const-table optimized
244*/
245#define HA_BLOCK_CONST_TABLE          (LL(1) << 42)
246
247/*
248  Handler supports FULLTEXT hints
249*/
250#define HA_CAN_FULLTEXT_HINTS         (LL(1) << 43)
251
252/**
253  Storage engine doesn't support LOCK TABLE ... READ LOCAL locks
254  but doesn't want to use handler::store_lock() API for upgrading
255  them to LOCK TABLE ... READ locks, for example, because it doesn't
256  use THR_LOCK locks at all.
257*/
258#define HA_NO_READ_LOCAL_LOCK         (LL(1) << 44)
259
260/**
261  A storage engine is compatible with the attachable transaction requirements
262  means that
263
264    - either SE detects the fact that THD::ha_data was reset and starts a new
265      attachable transaction, closes attachable transaction on close_connection
266      and resumes regular (outer) transaction when THD::ha_data is restored;
267
268    - or SE completely ignores THD::ha_data and close_connection like MyISAM
269      does.
270*/
271#define HA_ATTACHABLE_TRX_COMPATIBLE  (LL(1) << 45)
272
273
274/* bits in index_flags(index_number) for what you can do with index */
275#define HA_READ_NEXT            1       /* TODO really use this flag */
276#define HA_READ_PREV            2       /* supports ::index_prev */
277#define HA_READ_ORDER           4       /* index_next/prev follow sort order */
278#define HA_READ_RANGE           8       /* can find all records in a range */
279#define HA_ONLY_WHOLE_INDEX	16	/* Can't use part key searches */
280#define HA_KEYREAD_ONLY         64	/* Support HA_EXTRA_KEYREAD */
281/*
282  Index scan will not return records in rowid order. Not guaranteed to be
283  set for unordered (e.g. HASH) indexes.
284*/
285#define HA_KEY_SCAN_NOT_ROR     128
286#define HA_DO_INDEX_COND_PUSHDOWN  256 /* Supports Index Condition Pushdown */
287
288
289
290/**
291  bits in alter_table_flags:
292
293  HA_PARTITION_FUNCTION_SUPPORTED indicates that the function is
294  supported at all.
295  HA_FAST_CHANGE_PARTITION means that optimised variants of the changes
296  exists but they are not necessarily done online.
297
298  HA_ONLINE_DOUBLE_WRITE means that the handler supports writing to both
299  the new partition and to the old partitions when updating through the
300  old partitioning schema while performing a change of the partitioning.
301  This means that we can support updating of the table while performing
302  the copy phase of the change. For no lock at all also a double write
303  from new to old must exist and this is not required when this flag is
304  set.
305  This is actually removed even before it was introduced the first time.
306  The new idea is that handlers will handle the lock level already in
307  store_lock for ALTER TABLE partitions.
308
309  HA_PARTITION_ONE_PHASE is a flag that can be set by handlers that take
310  care of changing the partitions online and in one phase. Thus all phases
311  needed to handle the change are implemented inside the storage engine.
312  The storage engine must also support auto-discovery since the frm file
313  is changed as part of the change and this change must be controlled by
314  the storage engine. A typical engine to support this is NDB (through
315  WL #2498).
316*/
317#define HA_PARTITION_FUNCTION_SUPPORTED         (1L << 0)
318#define HA_FAST_CHANGE_PARTITION                (1L << 1)
319#define HA_PARTITION_ONE_PHASE                  (1L << 2)
320
321/* operations for disable/enable indexes */
322#define HA_KEY_SWITCH_NONUNIQ      0
323#define HA_KEY_SWITCH_ALL          1
324#define HA_KEY_SWITCH_NONUNIQ_SAVE 2
325#define HA_KEY_SWITCH_ALL_SAVE     3
326
327/*
328  Note: the following includes binlog and closing 0.
329  so: innodb + bdb + ndb + binlog + myisam + myisammrg + archive +
330      example + csv + heap + blackhole + federated + 0
331  (yes, the sum is deliberately inaccurate)
332  TODO remove the limit, use dynarrays
333*/
334#define MAX_HA 15
335
336/*
337  Use this instead of 0 as the initial value for the slot number of
338  handlerton, so that we can distinguish uninitialized slot number
339  from slot 0.
340*/
341#define HA_SLOT_UNDEF ((uint)-1)
342
343/*
344  Parameters for open() (in register form->filestat)
345  HA_GET_INFO does an implicit HA_ABORT_IF_LOCKED
346*/
347
348#define HA_OPEN_KEYFILE		1
349#define HA_OPEN_RNDFILE		2
350#define HA_GET_INDEX		4
351#define HA_GET_INFO		8	/* do a ha_info() after open */
352#define HA_READ_ONLY		16	/* File opened as readonly */
353/* Try readonly if can't open with read and write */
354#define HA_TRY_READ_ONLY	32
355#define HA_WAIT_IF_LOCKED	64	/* Wait if locked on open */
356#define HA_ABORT_IF_LOCKED	128	/* skip if locked on open.*/
357#define HA_BLOCK_LOCK		256	/* unlock when reading some records */
358#define HA_OPEN_TEMPORARY	512
359
360	/* Some key definitions */
361#define HA_KEY_NULL_LENGTH	1
362#define HA_KEY_BLOB_LENGTH	2
363
364#define HA_LEX_CREATE_TMP_TABLE	1
365#define HA_LEX_CREATE_IF_NOT_EXISTS 2
366#define HA_LEX_CREATE_TABLE_LIKE 4
367#define HA_LEX_CREATE_INTERNAL_TMP_TABLE 8
368#define HA_OPTION_NO_CHECKSUM	(1L << 17)
369#define HA_OPTION_NO_DELAY_KEY_WRITE (1L << 18)
370#define HA_MAX_REC_LENGTH	65535U
371
372/* Table caching type */
373#define HA_CACHE_TBL_NONTRANSACT 0
374#define HA_CACHE_TBL_NOCACHE     1
375#define HA_CACHE_TBL_ASKTRANSACT 2
376#define HA_CACHE_TBL_TRANSACT    4
377
378/**
379  Options for the START TRANSACTION statement.
380
381  Note that READ ONLY and READ WRITE are logically mutually exclusive.
382  This is enforced by the parser and depended upon by trans_begin().
383
384  We need two flags instead of one in order to differentiate between
385  situation when no READ WRITE/ONLY clause were given and thus transaction
386  is implicitly READ WRITE and the case when READ WRITE clause was used
387  explicitly.
388*/
389
390// WITH CONSISTENT SNAPSHOT option
391static const uint MYSQL_START_TRANS_OPT_WITH_CONS_SNAPSHOT = 1;
392// READ ONLY option
393static const uint MYSQL_START_TRANS_OPT_READ_ONLY          = 2;
394// READ WRITE option
395static const uint MYSQL_START_TRANS_OPT_READ_WRITE         = 4;
396
397enum legacy_db_type
398{
399  DB_TYPE_UNKNOWN=0,DB_TYPE_DIAB_ISAM=1,
400  DB_TYPE_HASH,DB_TYPE_MISAM,DB_TYPE_PISAM,
401  DB_TYPE_RMS_ISAM, DB_TYPE_HEAP, DB_TYPE_ISAM,
402  DB_TYPE_MRG_ISAM, DB_TYPE_MYISAM, DB_TYPE_MRG_MYISAM,
403  DB_TYPE_BERKELEY_DB, DB_TYPE_INNODB,
404  DB_TYPE_GEMINI, DB_TYPE_NDBCLUSTER,
405  DB_TYPE_EXAMPLE_DB, DB_TYPE_ARCHIVE_DB, DB_TYPE_CSV_DB,
406  DB_TYPE_FEDERATED_DB,
407  DB_TYPE_BLACKHOLE_DB,
408  DB_TYPE_PARTITION_DB,
409  DB_TYPE_BINLOG,
410  DB_TYPE_SOLID,
411  DB_TYPE_PBXT,
412  DB_TYPE_TABLE_FUNCTION,
413  DB_TYPE_MEMCACHE,
414  DB_TYPE_FALCON,
415  DB_TYPE_MARIA,
416  /** Performance schema engine. */
417  DB_TYPE_PERFORMANCE_SCHEMA,
418  DB_TYPE_FIRST_DYNAMIC=42,
419  DB_TYPE_DEFAULT=127 // Must be last
420};
421
422enum row_type { ROW_TYPE_NOT_USED=-1, ROW_TYPE_DEFAULT, ROW_TYPE_FIXED,
423		ROW_TYPE_DYNAMIC, ROW_TYPE_COMPRESSED,
424		ROW_TYPE_REDUNDANT, ROW_TYPE_COMPACT,
425                /** Unused. Reserved for future versions. */
426                ROW_TYPE_PAGE };
427
428/* Specifies data storage format for individual columns */
429enum column_format_type {
430  COLUMN_FORMAT_TYPE_DEFAULT=   0, /* Not specified (use engine default) */
431  COLUMN_FORMAT_TYPE_FIXED=     1, /* FIXED format */
432  COLUMN_FORMAT_TYPE_DYNAMIC=   2  /* DYNAMIC format */
433};
434
435enum enum_binlog_func {
436  BFN_RESET_LOGS=        1,
437  BFN_RESET_SLAVE=       2,
438  BFN_BINLOG_WAIT=       3,
439  BFN_BINLOG_END=        4,
440  BFN_BINLOG_PURGE_FILE= 5
441};
442
443enum enum_binlog_command {
444  LOGCOM_CREATE_TABLE,
445  LOGCOM_ALTER_TABLE,
446  LOGCOM_RENAME_TABLE,
447  LOGCOM_DROP_TABLE,
448  LOGCOM_CREATE_DB,
449  LOGCOM_ALTER_DB,
450  LOGCOM_DROP_DB,
451  LOGCOM_ACL_NOTIFY
452};
453
454/* struct to hold information about the table that should be created */
455
456/* Bits in used_fields */
457#define HA_CREATE_USED_AUTO             (1L << 0)
458#define HA_CREATE_USED_RAID             (1L << 1) //RAID is no longer availble
459#define HA_CREATE_USED_UNION            (1L << 2)
460#define HA_CREATE_USED_INSERT_METHOD    (1L << 3)
461#define HA_CREATE_USED_MIN_ROWS         (1L << 4)
462#define HA_CREATE_USED_MAX_ROWS         (1L << 5)
463#define HA_CREATE_USED_AVG_ROW_LENGTH   (1L << 6)
464#define HA_CREATE_USED_PACK_KEYS        (1L << 7)
465#define HA_CREATE_USED_CHARSET          (1L << 8)
466#define HA_CREATE_USED_DEFAULT_CHARSET  (1L << 9)
467#define HA_CREATE_USED_DATADIR          (1L << 10)
468#define HA_CREATE_USED_INDEXDIR         (1L << 11)
469#define HA_CREATE_USED_ENGINE           (1L << 12)
470#define HA_CREATE_USED_CHECKSUM         (1L << 13)
471#define HA_CREATE_USED_DELAY_KEY_WRITE  (1L << 14)
472#define HA_CREATE_USED_ROW_FORMAT       (1L << 15)
473#define HA_CREATE_USED_COMMENT          (1L << 16)
474#define HA_CREATE_USED_PASSWORD         (1L << 17)
475#define HA_CREATE_USED_CONNECTION       (1L << 18)
476#define HA_CREATE_USED_KEY_BLOCK_SIZE   (1L << 19)
477/** Unused. Reserved for future versions. */
478#define HA_CREATE_USED_TRANSACTIONAL    (1L << 20)
479/** Unused. Reserved for future versions. */
480#define HA_CREATE_USED_PAGE_CHECKSUM    (1L << 21)
481/** This is set whenever STATS_PERSISTENT=0|1|default has been
482specified in CREATE/ALTER TABLE. See also HA_OPTION_STATS_PERSISTENT in
483include/my_base.h. It is possible to distinguish whether
484STATS_PERSISTENT=default has been specified or no STATS_PERSISTENT= is
485given at all. */
486#define HA_CREATE_USED_STATS_PERSISTENT (1L << 22)
487/**
488   This is set whenever STATS_AUTO_RECALC=0|1|default has been
489   specified in CREATE/ALTER TABLE. See enum_stats_auto_recalc.
490   It is possible to distinguish whether STATS_AUTO_RECALC=default
491   has been specified or no STATS_AUTO_RECALC= is given at all.
492*/
493#define HA_CREATE_USED_STATS_AUTO_RECALC (1L << 23)
494/**
495   This is set whenever STATS_SAMPLE_PAGES=N|default has been
496   specified in CREATE/ALTER TABLE. It is possible to distinguish whether
497   STATS_SAMPLE_PAGES=default has been specified or no STATS_SAMPLE_PAGES= is
498   given at all.
499*/
500#define HA_CREATE_USED_STATS_SAMPLE_PAGES (1L << 24)
501
502
503/*
504  This is master database for most of system tables. However there
505  can be other databases which can hold system tables. Respective
506  storage engines define their own system database names.
507*/
508extern const char *mysqld_system_database;
509
510/*
511  Structure to hold list of system_database.system_table.
512  This is used at both mysqld and storage engine layer.
513*/
514struct st_system_tablename
515{
516  const char *db;
517  const char *tablename;
518};
519
520#define MAXGTRIDSIZE 64
521#define MAXBQUALSIZE 64
522
523#define COMPATIBLE_DATA_YES 0
524#define COMPATIBLE_DATA_NO  1
525
526namespace AQP {
527  class Join_plan;
528};
529
530/*
531  These structures are used to pass information from a set of SQL commands
532  on add/drop/change tablespace definitions to the proper hton.
533*/
534#define UNDEF_NODEGROUP 65535
535enum ts_command_type
536{
537  TS_CMD_NOT_DEFINED = -1,
538  CREATE_TABLESPACE = 0,
539  ALTER_TABLESPACE = 1,
540  CREATE_LOGFILE_GROUP = 2,
541  ALTER_LOGFILE_GROUP = 3,
542  DROP_TABLESPACE = 4,
543  DROP_LOGFILE_GROUP = 5,
544  CHANGE_FILE_TABLESPACE = 6,
545  ALTER_ACCESS_MODE_TABLESPACE = 7
546};
547
548enum ts_alter_tablespace_type
549{
550  TS_ALTER_TABLESPACE_TYPE_NOT_DEFINED = -1,
551  ALTER_TABLESPACE_ADD_FILE = 1,
552  ALTER_TABLESPACE_DROP_FILE = 2
553};
554
555enum tablespace_access_mode
556{
557  TS_NOT_DEFINED= -1,
558  TS_READ_ONLY = 0,
559  TS_READ_WRITE = 1,
560  TS_NOT_ACCESSIBLE = 2
561};
562
563struct handlerton;
564class st_alter_tablespace : public Sql_alloc
565{
566  public:
567  const char *tablespace_name;
568  const char *logfile_group_name;
569  enum ts_command_type ts_cmd_type;
570  enum ts_alter_tablespace_type ts_alter_tablespace_type;
571  const char *data_file_name;
572  const char *undo_file_name;
573  const char *redo_file_name;
574  ulonglong extent_size;
575  ulonglong undo_buffer_size;
576  ulonglong redo_buffer_size;
577  ulonglong initial_size;
578  ulonglong autoextend_size;
579  ulonglong max_size;
580  uint nodegroup_id;
581  handlerton *storage_engine;
582  bool wait_until_completed;
583  const char *ts_comment;
584  enum tablespace_access_mode ts_access_mode;
585  st_alter_tablespace()
586  {
587    tablespace_name= NULL;
588    logfile_group_name= "DEFAULT_LG"; //Default log file group
589    ts_cmd_type= TS_CMD_NOT_DEFINED;
590    data_file_name= NULL;
591    undo_file_name= NULL;
592    redo_file_name= NULL;
593    extent_size= 1024*1024;        //Default 1 MByte
594    undo_buffer_size= 8*1024*1024; //Default 8 MByte
595    redo_buffer_size= 8*1024*1024; //Default 8 MByte
596    initial_size= 128*1024*1024;   //Default 128 MByte
597    autoextend_size= 0;            //No autoextension as default
598    max_size= 0;                   //Max size == initial size => no extension
599    storage_engine= NULL;
600    nodegroup_id= UNDEF_NODEGROUP;
601    wait_until_completed= TRUE;
602    ts_comment= NULL;
603    ts_access_mode= TS_NOT_DEFINED;
604  }
605};
606
607/* The handler for a table type.  Will be included in the TABLE structure */
608
609struct TABLE;
610
611/*
612  Make sure that the order of schema_tables and enum_schema_tables are the same.
613*/
614enum enum_schema_tables
615{
616  SCH_CHARSETS= 0,
617  SCH_COLLATIONS,
618  SCH_COLLATION_CHARACTER_SET_APPLICABILITY,
619  SCH_COLUMNS,
620  SCH_COLUMN_PRIVILEGES,
621  SCH_ENGINES,
622  SCH_EVENTS,
623  SCH_FILES,
624  SCH_GLOBAL_STATUS,
625  SCH_GLOBAL_VARIABLES,
626  SCH_KEY_COLUMN_USAGE,
627  SCH_OPEN_TABLES,
628  SCH_OPTIMIZER_TRACE,
629  SCH_PARAMETERS,
630  SCH_PARTITIONS,
631  SCH_PLUGINS,
632  SCH_PROCESSLIST,
633  SCH_PROFILES,
634  SCH_REFERENTIAL_CONSTRAINTS,
635  SCH_PROCEDURES,
636  SCH_SCHEMATA,
637  SCH_SCHEMA_PRIVILEGES,
638  SCH_SESSION_STATUS,
639  SCH_SESSION_VARIABLES,
640  SCH_STATISTICS,
641  SCH_STATUS,
642  SCH_TABLES,
643  SCH_TABLESPACES,
644  SCH_TABLE_CONSTRAINTS,
645  SCH_TABLE_NAMES,
646  SCH_TABLE_PRIVILEGES,
647  SCH_TRIGGERS,
648  SCH_USER_PRIVILEGES,
649  SCH_VARIABLES,
650  SCH_VIEWS
651};
652
653struct TABLE_SHARE;
654struct st_foreign_key_info;
655typedef struct st_foreign_key_info FOREIGN_KEY_INFO;
656typedef bool (stat_print_fn)(THD *thd, const char *type, size_t type_len,
657                             const char *file, size_t file_len,
658                             const char *status, size_t status_len);
659enum ha_stat_type { HA_ENGINE_STATUS, HA_ENGINE_LOGS, HA_ENGINE_MUTEX };
660extern st_plugin_int *hton2plugin[MAX_HA];
661
662/* Transaction log maintains type definitions */
663enum log_status
664{
665  HA_LOG_STATUS_FREE= 0,      /* log is free and can be deleted */
666  HA_LOG_STATUS_INUSE= 1,     /* log can't be deleted because it is in use */
667  HA_LOG_STATUS_NOSUCHLOG= 2  /* no such log (can't be returned by
668                                the log iterator status) */
669};
670/*
671  Function for signaling that the log file changed its state from
672  LOG_STATUS_INUSE to LOG_STATUS_FREE
673
674  Now it do nothing, will be implemented as part of new transaction
675  log management for engines.
676  TODO: implement the function.
677*/
678void signal_log_not_needed(struct handlerton, char *log_file);
679/*
680  Data of transaction log iterator.
681*/
682struct handler_log_file_data {
683  LEX_STRING filename;
684  enum log_status status;
685};
686
687
688enum handler_iterator_type
689{
690  /* request of transaction log iterator */
691  HA_TRANSACTLOG_ITERATOR= 1
692};
693enum handler_create_iterator_result
694{
695  HA_ITERATOR_OK,          /* iterator created */
696  HA_ITERATOR_UNSUPPORTED, /* such type of iterator is not supported */
697  HA_ITERATOR_ERROR        /* error during iterator creation */
698};
699
700/*
701  Iterator structure. Can be used by handler/handlerton for different purposes.
702
703  Iterator should be created in the way to point "before" the first object
704  it iterate, so next() call move it to the first object or return !=0 if
705  there is nothing to iterate through.
706*/
707struct handler_iterator {
708  /*
709    Moves iterator to next record and return 0 or return !=0
710    if there is no records.
711    iterator_object will be filled by this function if next() returns 0.
712    Content of the iterator_object depend on iterator type.
713  */
714  int (*next)(struct handler_iterator *, void *iterator_object);
715  /*
716    Free resources allocated by iterator, after this call iterator
717    is not usable.
718  */
719  void (*destroy)(struct handler_iterator *);
720  /*
721    Pointer to buffer for the iterator to use.
722    Should be allocated by function which created the iterator and
723    destroied by freed by above "destroy" call
724  */
725  void *buffer;
726};
727
728class handler;
729/*
730  handlerton is a singleton structure - one instance per storage engine -
731  to provide access to storage engine functionality that works on the
732  "global" level (unlike handler class that works on a per-table basis)
733
734  usually handlerton instance is defined statically in ha_xxx.cc as
735
736  static handlerton { ... } xxx_hton;
737
738  savepoint_*, prepare, recover, and *_by_xid pointers can be 0.
739*/
740struct handlerton
741{
742  /*
743    Historical marker for if the engine is available of not
744  */
745  SHOW_COMP_OPTION state;
746
747  /*
748    Historical number used for frm file to determine the correct storage engine.
749    This is going away and new engines will just use "name" for this.
750  */
751  enum legacy_db_type db_type;
752  /*
753    each storage engine has it's own memory area (actually a pointer)
754    in the thd, for storing per-connection information.
755    It is accessed as
756
757      thd->ha_data[xxx_hton.slot]
758
759   slot number is initialized by MySQL after xxx_init() is called.
760   */
761   uint slot;
762   /*
763     to store per-savepoint data storage engine is provided with an area
764     of a requested size (0 is ok here).
765     savepoint_offset must be initialized statically to the size of
766     the needed memory to store per-savepoint information.
767     After xxx_init it is changed to be an offset to savepoint storage
768     area and need not be used by storage engine.
769     see binlog_hton and binlog_savepoint_set/rollback for an example.
770   */
771   uint savepoint_offset;
772   /*
773     handlerton methods:
774
775     close_connection is only called if
776     thd->ha_data[xxx_hton.slot] is non-zero, so even if you don't need
777     this storage area - set it to something, so that MySQL would know
778     this storage engine was accessed in this connection
779   */
780   int  (*close_connection)(handlerton *hton, THD *thd);
781   /* Terminate connection/statement notification. */
782   void (*kill_connection)(handlerton *hton, THD *thd);
783   /*
784     sv points to an uninitialized storage area of requested size
785     (see savepoint_offset description)
786   */
787   int  (*savepoint_set)(handlerton *hton, THD *thd, void *sv);
788   /*
789     sv points to a storage area, that was earlier passed
790     to the savepoint_set call
791   */
792   int  (*savepoint_rollback)(handlerton *hton, THD *thd, void *sv);
793   /**
794     Check if storage engine allows to release metadata locks which were
795     acquired after the savepoint if rollback to savepoint is done.
796     @return true  - If it is safe to release MDL locks.
797             false - If it is not.
798   */
799   bool (*savepoint_rollback_can_release_mdl)(handlerton *hton, THD *thd);
800   int  (*savepoint_release)(handlerton *hton, THD *thd, void *sv);
801   /*
802     'all' is true if it's a real commit, that makes persistent changes
803     'all' is false if it's not in fact a commit but an end of the
804     statement that is part of the transaction.
805     NOTE 'all' is also false in auto-commit mode where 'end of statement'
806     and 'real commit' mean the same event.
807   */
808   int  (*commit)(handlerton *hton, THD *thd, bool all);
809   int  (*rollback)(handlerton *hton, THD *thd, bool all);
810   int  (*prepare)(handlerton *hton, THD *thd, bool all);
811   int  (*recover)(handlerton *hton, XID *xid_list, uint len);
812   int  (*commit_by_xid)(handlerton *hton, XID *xid);
813   int  (*rollback_by_xid)(handlerton *hton, XID *xid);
814   handler *(*create)(handlerton *hton, TABLE_SHARE *table, MEM_ROOT *mem_root);
815   void (*drop_database)(handlerton *hton, char* path);
816   int (*panic)(handlerton *hton, enum ha_panic_function flag);
817   int (*start_consistent_snapshot)(handlerton *hton, THD *thd);
818   bool (*flush_logs)(handlerton *hton);
819   bool (*show_status)(handlerton *hton, THD *thd, stat_print_fn *print, enum ha_stat_type stat);
820   uint (*partition_flags)();
821   uint (*alter_table_flags)(uint flags);
822   int (*alter_tablespace)(handlerton *hton, THD *thd, st_alter_tablespace *ts_info);
823   int (*fill_is_table)(handlerton *hton, THD *thd, TABLE_LIST *tables,
824                        class Item *cond,
825                        enum enum_schema_tables);
826   uint32 flags;                                /* global handler flags */
827   /*
828      Those handlerton functions below are properly initialized at handler
829      init.
830   */
831   int (*binlog_func)(handlerton *hton, THD *thd, enum_binlog_func fn, void *arg);
832   void (*binlog_log_query)(handlerton *hton, THD *thd,
833                            enum_binlog_command binlog_command,
834                            const char *query, uint query_length,
835                            const char *db, const char *table_name);
836   int (*release_temporary_latches)(handlerton *hton, THD *thd);
837
838   /*
839     Get log status.
840     If log_status is null then the handler do not support transaction
841     log information (i.e. log iterator can't be created).
842     (see example of implementation in handler.cc, TRANS_LOG_MGM_EXAMPLE_CODE)
843
844   */
845   enum log_status (*get_log_status)(handlerton *hton, char *log);
846
847   /*
848     Iterators creator.
849     Presence of the pointer should be checked before using
850   */
851   enum handler_create_iterator_result
852     (*create_iterator)(handlerton *hton, enum handler_iterator_type type,
853                        struct handler_iterator *fill_this_in);
854   int (*discover)(handlerton *hton, THD* thd, const char *db,
855                   const char *name,
856                   uchar **frmblob,
857                   size_t *frmlen);
858   int (*find_files)(handlerton *hton, THD *thd,
859                     const char *db,
860                     const char *path,
861                     const char *wild, bool dir, List<LEX_STRING> *files);
862   int (*table_exists_in_engine)(handlerton *hton, THD* thd, const char *db,
863                                 const char *name);
864   int (*make_pushed_join)(handlerton *hton, THD* thd,
865                           const AQP::Join_plan* plan);
866
867  /**
868    List of all system tables specific to the SE.
869    Array element would look like below,
870     { "<database_name>", "<system table name>" },
871    The last element MUST be,
872     { (const char*)NULL, (const char*)NULL }
873
874    @see ha_example_system_tables in ha_example.cc
875
876    This interface is optional, so every SE need not implement it.
877  */
878  const char* (*system_database)();
879
880  /**
881    Check if the given db.tablename is a system table for this SE.
882
883    @param db                         Database name to check.
884    @param table_name                 table name to check.
885    @param is_sql_layer_system_table  if the supplied db.table_name is a SQL
886                                      layer system table.
887
888    @see example_is_supported_system_table in ha_example.cc
889
890    is_sql_layer_system_table is supplied to make more efficient
891    checks possible for SEs that support all SQL layer tables.
892
893    This interface is optional, so every SE need not implement it.
894  */
895  bool (*is_supported_system_table)(const char *db,
896                                    const char *table_name,
897                                    bool is_sql_layer_system_table);
898
899  /**
900    Retrieve cost constants to be used for this storage engine.
901
902    A storage engine that wants to provide its own cost constants to
903    be used in the optimizer cost model, should implement this function.
904    The server will call this function to get a cost constant object
905    that will be used for tables stored in this storage engine instead
906    of using the default cost constants.
907
908    Life cycle for the cost constant object: The storage engine must
909    allocate the cost constant object on the heap. After the function
910    returns, the server takes over the ownership of this object.
911    The server will eventually delete the object by calling delete.
912
913    @note In the initial version the storage_category parameter will
914    not be used. The only valid value this will have is DEFAULT_STORAGE_CLASS
915    (see declartion in opt_costconstants.h).
916
917    @param storage_category the storage type that the cost constants will
918                            be used for
919
920    @return a pointer to the cost constant object, if NULL is returned
921            the default cost constants will be used
922  */
923
924  SE_cost_constants *(*get_cost_constants)(uint storage_category);
925
926   uint32 license; /* Flag for Engine License */
927   void *data; /* Location for engines to keep personal structures */
928};
929
930
931/* Possible flags of a handlerton (there can be 32 of them) */
932#define HTON_NO_FLAGS                 0
933#define HTON_CLOSE_CURSORS_AT_COMMIT (1 << 0)
934#define HTON_ALTER_NOT_SUPPORTED     (1 << 1) //Engine does not support alter
935#define HTON_CAN_RECREATE            (1 << 2) //Delete all is used fro truncate
936#define HTON_HIDDEN                  (1 << 3) //Engine does not appear in lists
937#define HTON_FLUSH_AFTER_RENAME      (1 << 4)
938#define HTON_NOT_USER_SELECTABLE     (1 << 5)
939#define HTON_TEMPORARY_NOT_SUPPORTED (1 << 6) //Having temporary tables not supported
940#define HTON_SUPPORT_LOG_TABLES      (1 << 7) //Engine supports log tables
941#define HTON_NO_PARTITION            (1 << 8) //You can not partition these tables
942
943/*
944  This flag should be set when deciding that the engine does not allow row based
945  binary logging (RBL) optimizations.
946
947  Currently, setting this flag, means that table's read/write_set will be left
948  untouched when logging changes to tables in this engine. In practice this
949  means that the server will not mess around with table->write_set and/or
950  table->read_set when using RBL and deciding whether to log full or minimal rows.
951
952  It's valuable for instance for virtual tables, eg: Performance Schema which have
953  no meaning for replication.
954*/
955#define HTON_NO_BINLOG_ROW_OPT       (1 << 9)
956
957/**
958  Engine supports extended keys. The flag allows to
959  use 'extended key' feature if the engine is able to
960  do it (has primary key values in the secondary key).
961  Note that handler flag HA_PRIMARY_KEY_IN_READ_INDEX is
962  actually partial case of HTON_SUPPORTS_EXTENDED_KEYS.
963*/
964
965#define HTON_SUPPORTS_EXTENDED_KEYS  (1 << 10)
966
967
968enum enum_tx_isolation { ISO_READ_UNCOMMITTED, ISO_READ_COMMITTED,
969			 ISO_REPEATABLE_READ, ISO_SERIALIZABLE};
970
971
972typedef struct {
973  ulonglong data_file_length;
974  ulonglong max_data_file_length;
975  ulonglong index_file_length;
976  ulonglong delete_length;
977  ha_rows records;
978  ulong mean_rec_length;
979  ulong create_time;
980  ulong check_time;
981  ulong update_time;
982  ulonglong check_sum;
983} PARTITION_STATS;
984
985#define UNDEF_NODEGROUP 65535
986class Item;
987struct st_table_log_memory_entry;
988
989class partition_info;
990
991struct st_partition_iter;
992
993enum enum_ha_unused { HA_CHOICE_UNDEF, HA_CHOICE_NO, HA_CHOICE_YES };
994
995enum enum_stats_auto_recalc { HA_STATS_AUTO_RECALC_DEFAULT= 0,
996                              HA_STATS_AUTO_RECALC_ON,
997                              HA_STATS_AUTO_RECALC_OFF };
998
999typedef struct st_ha_create_information
1000{
1001  const CHARSET_INFO *table_charset, *default_table_charset;
1002  LEX_STRING connect_string;
1003  const char *password, *tablespace;
1004  LEX_STRING comment;
1005  const char *data_file_name, *index_file_name;
1006  const char *alias;
1007  ulonglong max_rows,min_rows;
1008  ulonglong auto_increment_value;
1009  ulong table_options;
1010  ulong avg_row_length;
1011  ulong used_fields;
1012  ulong key_block_size;
1013  uint stats_sample_pages;		/* number of pages to sample during
1014					stats estimation, if used, otherwise 0. */
1015  enum_stats_auto_recalc stats_auto_recalc;
1016  SQL_I_List<TABLE_LIST> merge_list;
1017  handlerton *db_type;
1018  /**
1019    Row type of the table definition.
1020
1021    Defaults to ROW_TYPE_DEFAULT for all non-ALTER statements.
1022    For ALTER TABLE defaults to ROW_TYPE_NOT_USED (means "keep the current").
1023
1024    Can be changed either explicitly by the parser.
1025    If nothing speficied inherits the value of the original table (if present).
1026  */
1027  enum row_type row_type;
1028  uint null_bits;                       /* NULL bits at start of record */
1029  uint options;				/* OR of HA_CREATE_ options */
1030  uint merge_insert_method;
1031  uint extra_size;                      /* length of extra data segment */
1032  bool varchar;                         /* 1 if table has a VARCHAR */
1033  enum ha_storage_media storage_media;  /* DEFAULT, DISK or MEMORY */
1034} HA_CREATE_INFO;
1035
1036
1037/**
1038  Structure describing changes to an index to be caused by ALTER TABLE.
1039*/
1040
1041struct KEY_PAIR
1042{
1043  /**
1044    Pointer to KEY object describing old version of index in
1045    TABLE::key_info array for TABLE instance representing old
1046    version of table.
1047  */
1048  KEY *old_key;
1049  /**
1050    Pointer to KEY object describing new version of index in
1051    Alter_inplace_info::key_info_buffer array.
1052  */
1053  KEY *new_key;
1054};
1055
1056
1057/**
1058  In-place alter handler context.
1059
1060  This is a superclass intended to be subclassed by individual handlers
1061  in order to store handler unique context between in-place alter API calls.
1062
1063  The handler is responsible for creating the object. This can be done
1064  as early as during check_if_supported_inplace_alter().
1065
1066  The SQL layer is responsible for destroying the object.
1067  The class extends Sql_alloc so the memory will be mem root allocated.
1068
1069  @see Alter_inplace_info
1070*/
1071
1072class inplace_alter_handler_ctx : public Sql_alloc
1073{
1074public:
1075  inplace_alter_handler_ctx() {}
1076
1077  virtual ~inplace_alter_handler_ctx() {}
1078};
1079
1080
1081/**
1082  Class describing changes to be done by ALTER TABLE.
1083  Instance of this class is passed to storage engine in order
1084  to determine if this ALTER TABLE can be done using in-place
1085  algorithm. It is also used for executing the ALTER TABLE
1086  using in-place algorithm.
1087*/
1088
1089class Alter_inplace_info
1090{
1091public:
1092  /**
1093     Bits to show in detail what operations the storage engine is
1094     to execute.
1095
1096     All these operations are supported as in-place operations by the
1097     SQL layer. This means that operations that by their nature must
1098     be performed by copying the table to a temporary table, will not
1099     have their own flags here (e.g. ALTER TABLE FORCE, ALTER TABLE
1100     ENGINE).
1101
1102     We generally try to specify handler flags only if there are real
1103     changes. But in cases when it is cumbersome to determine if some
1104     attribute has really changed we might choose to set flag
1105     pessimistically, for example, relying on parser output only.
1106  */
1107  typedef ulong HA_ALTER_FLAGS;
1108
1109  // Add non-unique, non-primary index
1110  static const HA_ALTER_FLAGS ADD_INDEX                  = 1L << 0;
1111
1112  // Drop non-unique, non-primary index
1113  static const HA_ALTER_FLAGS DROP_INDEX                 = 1L << 1;
1114
1115  // Add unique, non-primary index
1116  static const HA_ALTER_FLAGS ADD_UNIQUE_INDEX           = 1L << 2;
1117
1118  // Drop unique, non-primary index
1119  static const HA_ALTER_FLAGS DROP_UNIQUE_INDEX          = 1L << 3;
1120
1121  // Add primary index
1122  static const HA_ALTER_FLAGS ADD_PK_INDEX               = 1L << 4;
1123
1124  // Drop primary index
1125  static const HA_ALTER_FLAGS DROP_PK_INDEX              = 1L << 5;
1126
1127  // Add column
1128  static const HA_ALTER_FLAGS ADD_COLUMN                 = 1L << 6;
1129
1130  // Drop column
1131  static const HA_ALTER_FLAGS DROP_COLUMN                = 1L << 7;
1132
1133  // Rename column
1134  static const HA_ALTER_FLAGS ALTER_COLUMN_NAME          = 1L << 8;
1135
1136  // Change column datatype
1137  static const HA_ALTER_FLAGS ALTER_COLUMN_TYPE          = 1L << 9;
1138
1139  /**
1140    Change column datatype in such way that new type has compatible
1141    packed representation with old type, so it is theoretically
1142    possible to perform change by only updating data dictionary
1143    without changing table rows.
1144  */
1145  static const HA_ALTER_FLAGS ALTER_COLUMN_EQUAL_PACK_LENGTH = 1L << 10;
1146
1147  // Reorder column
1148  static const HA_ALTER_FLAGS ALTER_COLUMN_ORDER         = 1L << 11;
1149
1150  // Change column from NOT NULL to NULL
1151  static const HA_ALTER_FLAGS ALTER_COLUMN_NULLABLE      = 1L << 12;
1152
1153  // Change column from NULL to NOT NULL
1154  static const HA_ALTER_FLAGS ALTER_COLUMN_NOT_NULLABLE  = 1L << 13;
1155
1156  // Set or remove default column value
1157  static const HA_ALTER_FLAGS ALTER_COLUMN_DEFAULT       = 1L << 14;
1158
1159  // Add foreign key
1160  static const HA_ALTER_FLAGS ADD_FOREIGN_KEY            = 1L << 15;
1161
1162  // Drop foreign key
1163  static const HA_ALTER_FLAGS DROP_FOREIGN_KEY           = 1L << 16;
1164
1165  // table_options changed, see HA_CREATE_INFO::used_fields for details.
1166  static const HA_ALTER_FLAGS CHANGE_CREATE_OPTION       = 1L << 17;
1167
1168  // Table is renamed
1169  static const HA_ALTER_FLAGS ALTER_RENAME               = 1L << 18;
1170
1171  // Change the storage type of column
1172  static const HA_ALTER_FLAGS ALTER_COLUMN_STORAGE_TYPE = 1L << 19;
1173
1174  // Change the column format of column
1175  static const HA_ALTER_FLAGS ALTER_COLUMN_COLUMN_FORMAT = 1L << 20;
1176
1177  // Add partition
1178  static const HA_ALTER_FLAGS ADD_PARTITION              = 1L << 21;
1179
1180  // Drop partition
1181  static const HA_ALTER_FLAGS DROP_PARTITION             = 1L << 22;
1182
1183  // Changing partition options
1184  static const HA_ALTER_FLAGS ALTER_PARTITION            = 1L << 23;
1185
1186  // Coalesce partition
1187  static const HA_ALTER_FLAGS COALESCE_PARTITION         = 1L << 24;
1188
1189  // Reorganize partition ... into
1190  static const HA_ALTER_FLAGS REORGANIZE_PARTITION       = 1L << 25;
1191
1192  // Reorganize partition
1193  static const HA_ALTER_FLAGS ALTER_TABLE_REORG          = 1L << 26;
1194
1195  // Remove partitioning
1196  static const HA_ALTER_FLAGS ALTER_REMOVE_PARTITIONING  = 1L << 27;
1197
1198  // Partition operation with ALL keyword
1199  static const HA_ALTER_FLAGS ALTER_ALL_PARTITION        = 1L << 28;
1200
1201  /**
1202    Rename index. Note that we set this flag only if there are no other
1203    changes to the index being renamed. Also for simplicity we don't
1204    detect renaming of indexes which is done by dropping index and then
1205    re-creating index with identical definition under different name.
1206  */
1207  static const HA_ALTER_FLAGS RENAME_INDEX               = 1L << 29;
1208
1209  /**
1210    Recreate the table for ALTER TABLE FORCE, ALTER TABLE ENGINE
1211    and OPTIMIZE TABLE operations.
1212  */
1213  static const HA_ALTER_FLAGS RECREATE_TABLE             = 1L << 30;
1214
1215  // Add spatial index
1216  static const HA_ALTER_FLAGS ADD_SPATIAL_INDEX          = 1L << 31;
1217
1218  /**
1219    Create options (like MAX_ROWS) for the new version of table.
1220
1221    @note The referenced instance of HA_CREATE_INFO object was already
1222          used to create new .FRM file for table being altered. So it
1223          has been processed by mysql_prepare_create_table() already.
1224          For example, this means that it has HA_OPTION_PACK_RECORD
1225          flag in HA_CREATE_INFO::table_options member correctly set.
1226  */
1227  HA_CREATE_INFO *create_info;
1228
1229  /**
1230    Alter options, fields and keys for the new version of table.
1231
1232    @note The referenced instance of Alter_info object was already
1233          used to create new .FRM file for table being altered. So it
1234          has been processed by mysql_prepare_create_table() already.
1235          In particular, this means that in Create_field objects for
1236          fields which were present in some form in the old version
1237          of table, Create_field::field member points to corresponding
1238          Field instance for old version of table.
1239  */
1240  Alter_info *alter_info;
1241
1242  /**
1243    Array of KEYs for new version of table - including KEYs to be added.
1244
1245    @note Currently this array is produced as result of
1246          mysql_prepare_create_table() call.
1247          This means that it follows different convention for
1248          KEY_PART_INFO::fieldnr values than objects in TABLE::key_info
1249          array.
1250
1251    @todo This is mainly due to the fact that we need to keep compatibility
1252          with removed handler::add_index() call. We plan to switch to
1253          TABLE::key_info numbering later.
1254
1255    KEYs are sorted - see sort_keys().
1256  */
1257  KEY  *key_info_buffer;
1258
1259  /** Size of key_info_buffer array. */
1260  uint key_count;
1261
1262  /** Size of index_drop_buffer array. */
1263  uint index_drop_count;
1264
1265  /**
1266     Array of pointers to KEYs to be dropped belonging to the TABLE instance
1267     for the old version of the table.
1268  */
1269  KEY  **index_drop_buffer;
1270
1271  /** Size of index_add_buffer array. */
1272  uint index_add_count;
1273
1274  /**
1275     Array of indexes into key_info_buffer for KEYs to be added,
1276     sorted in increasing order.
1277  */
1278  uint *index_add_buffer;
1279
1280  /** Size of index_rename_buffer array. */
1281  uint index_rename_count;
1282
1283  /**
1284    Array of KEY_PAIR objects describing indexes being renamed.
1285    For each index renamed it contains object with KEY_PAIR::old_key
1286    pointing to KEY object belonging to the TABLE instance for old
1287    version of table representing old version of index and with
1288    KEY_PAIR::new_key pointing to KEY object for new version of
1289    index in key_info_buffer member.
1290  */
1291  KEY_PAIR  *index_rename_buffer;
1292
1293  /**
1294     Context information to allow handlers to keep context between in-place
1295     alter API calls.
1296
1297     @see inplace_alter_handler_ctx for information about object lifecycle.
1298  */
1299  inplace_alter_handler_ctx *handler_ctx;
1300
1301  /**
1302    If the table uses several handlers, like ha_partition uses one handler
1303    per partition, this contains a Null terminated array of ctx pointers
1304    that should all be committed together.
1305    Or NULL if only handler_ctx should be committed.
1306    Set to NULL if the low level handler::commit_inplace_alter_table uses it,
1307    to signal to the main handler that everything was committed as atomically.
1308
1309    @see inplace_alter_handler_ctx for information about object lifecycle.
1310  */
1311  inplace_alter_handler_ctx **group_commit_ctx;
1312
1313  /**
1314     Flags describing in detail which operations the storage engine is to execute.
1315  */
1316  HA_ALTER_FLAGS handler_flags;
1317
1318  /**
1319     Partition_info taking into account the partition changes to be performed.
1320     Contains all partitions which are present in the old version of the table
1321     with partitions to be dropped or changed marked as such + all partitions
1322     to be added in the new version of table marked as such.
1323  */
1324  partition_info *modified_part_info;
1325
1326  /** true for online operation (LOCK=NONE) */
1327  bool online;
1328
1329  /**
1330     Can be set by handler to describe why a given operation cannot be done
1331     in-place (HA_ALTER_INPLACE_NOT_SUPPORTED) or why it cannot be done
1332     online (HA_ALTER_INPLACE_NO_LOCK or HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE)
1333     If set, it will be used with ER_ALTER_OPERATION_NOT_SUPPORTED_REASON if
1334     results from handler::check_if_supported_inplace_alter() doesn't match
1335     requirements set by user. If not set, the more generic
1336     ER_ALTER_OPERATION_NOT_SUPPORTED will be used.
1337
1338     Please set to a properly localized string, for example using
1339     my_get_err_msg(), so that the error message as a whole is localized.
1340  */
1341  const char *unsupported_reason;
1342
1343  Alter_inplace_info(HA_CREATE_INFO *create_info_arg,
1344                     Alter_info *alter_info_arg,
1345                     KEY *key_info_arg, uint key_count_arg,
1346                     partition_info *modified_part_info_arg)
1347    : create_info(create_info_arg),
1348    alter_info(alter_info_arg),
1349    key_info_buffer(key_info_arg),
1350    key_count(key_count_arg),
1351    index_drop_count(0),
1352    index_drop_buffer(NULL),
1353    index_add_count(0),
1354    index_add_buffer(NULL),
1355    index_rename_count(0),
1356    index_rename_buffer(NULL),
1357    handler_ctx(NULL),
1358    group_commit_ctx(NULL),
1359    handler_flags(0),
1360    modified_part_info(modified_part_info_arg),
1361    online(false),
1362    unsupported_reason(NULL)
1363  {}
1364
1365  ~Alter_inplace_info()
1366  {
1367    delete handler_ctx;
1368  }
1369
1370  /**
1371    Used after check_if_supported_inplace_alter() to report
1372    error if the result does not match the LOCK/ALGORITHM
1373    requirements set by the user.
1374
1375    @param not_supported  Part of statement that was not supported.
1376    @param try_instead    Suggestion as to what the user should
1377                          replace not_supported with.
1378  */
1379  void report_unsupported_error(const char *not_supported,
1380                                const char *try_instead);
1381
1382  /** Add old and new version of key to array of indexes to be renamed. */
1383  void add_renamed_key(KEY *old_key, KEY *new_key)
1384  {
1385    KEY_PAIR *key_pair= index_rename_buffer + index_rename_count++;
1386    key_pair->old_key= old_key;
1387    key_pair->new_key= new_key;
1388    DBUG_PRINT("info", ("index renamed: '%s' to '%s'",
1389                        old_key->name, new_key->name));
1390  }
1391
1392  /**
1393    Add old and new version of modified key to arrays of indexes to
1394    be dropped and added (correspondingly).
1395  */
1396  void add_modified_key(KEY *old_key, KEY *new_key)
1397  {
1398    index_drop_buffer[index_drop_count++]= old_key;
1399    index_add_buffer[index_add_count++]= (uint) (new_key - key_info_buffer);
1400    DBUG_PRINT("info", ("index changed: '%s'", old_key->name));
1401  }
1402
1403  /** Drop key to array of indexes to be dropped. */
1404  void add_dropped_key(KEY *old_key)
1405  {
1406    index_drop_buffer[index_drop_count++]= old_key;
1407    DBUG_PRINT("info", ("index dropped: '%s'", old_key->name));
1408  }
1409
1410  /** Add key to array of indexes to be added. */
1411  void add_added_key(KEY *new_key)
1412  {
1413    index_add_buffer[index_add_count++]= (uint) (new_key - key_info_buffer);
1414    DBUG_PRINT("info", ("index added: '%s'", new_key->name));
1415  }
1416};
1417
1418
1419typedef struct st_key_create_information
1420{
1421  enum ha_key_alg algorithm;
1422  ulong block_size;
1423  LEX_STRING parser_name;
1424  LEX_STRING comment;
1425  /**
1426    A flag to determine if we will check for duplicate indexes.
1427    This typically means that the key information was specified
1428    directly by the user (set by the parser) or a column
1429    associated with it was dropped.
1430  */
1431  bool check_for_duplicate_indexes;
1432} KEY_CREATE_INFO;
1433
1434
1435/*
1436  Class for maintaining hooks used inside operations on tables such
1437  as: create table functions, delete table functions, and alter table
1438  functions.
1439
1440  Class is using the Template Method pattern to separate the public
1441  usage interface from the private inheritance interface.  This
1442  imposes no overhead, since the public non-virtual function is small
1443  enough to be inlined.
1444
1445  The hooks are usually used for functions that does several things,
1446  e.g., create_table_from_items(), which both create a table and lock
1447  it.
1448 */
1449class TABLEOP_HOOKS
1450{
1451public:
1452  TABLEOP_HOOKS() {}
1453  virtual ~TABLEOP_HOOKS() {}
1454
1455  inline void prelock(TABLE **tables, uint count)
1456  {
1457    do_prelock(tables, count);
1458  }
1459
1460  inline int postlock(TABLE **tables, uint count)
1461  {
1462    return do_postlock(tables, count);
1463  }
1464private:
1465  /* Function primitive that is called prior to locking tables */
1466  virtual void do_prelock(TABLE **tables, uint count)
1467  {
1468    /* Default is to do nothing */
1469  }
1470
1471  /**
1472     Primitive called after tables are locked.
1473
1474     If an error is returned, the tables will be unlocked and error
1475     handling start.
1476
1477     @return Error code or zero.
1478   */
1479  virtual int do_postlock(TABLE **tables, uint count)
1480  {
1481    return 0;                           /* Default is to do nothing */
1482  }
1483};
1484
1485typedef struct st_savepoint SAVEPOINT;
1486extern ulong savepoint_alloc_size;
1487extern KEY_CREATE_INFO default_key_create_info;
1488
1489typedef struct st_ha_check_opt
1490{
1491  st_ha_check_opt() {}                        /* Remove gcc warning */
1492  uint flags;       /* isam layer flags (e.g. for myisamchk) */
1493  uint sql_flags;   /* sql layer flags - for something myisamchk cannot do */
1494  KEY_CACHE *key_cache;	/* new key cache when changing key cache */
1495  void init();
1496} HA_CHECK_OPT;
1497
1498
1499
1500/*
1501  This is a buffer area that the handler can use to store rows.
1502  'end_of_used_area' should be kept updated after calls to
1503  read-functions so that other parts of the code can use the
1504  remaining area (until next read calls is issued).
1505*/
1506
1507typedef struct st_handler_buffer
1508{
1509  uchar *buffer;         /* Buffer one can start using */
1510  uchar *buffer_end;     /* End of buffer */
1511  uchar *end_of_used_area;     /* End of area that was used by handler */
1512} HANDLER_BUFFER;
1513
1514typedef struct system_status_var SSV;
1515
1516
1517typedef void *range_seq_t;
1518
1519typedef struct st_range_seq_if
1520{
1521  /*
1522    Initialize the traversal of range sequence
1523
1524    SYNOPSIS
1525      init()
1526        init_params  The seq_init_param parameter
1527        n_ranges     The number of ranges obtained
1528        flags        A combination of HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
1529
1530    RETURN
1531      An opaque value to be used as RANGE_SEQ_IF::next() parameter
1532  */
1533  range_seq_t (*init)(void *init_params, uint n_ranges, uint flags);
1534
1535
1536  /*
1537    Get the next range in the range sequence
1538
1539    SYNOPSIS
1540      next()
1541        seq    The value returned by RANGE_SEQ_IF::init()
1542        range  OUT Information about the next range
1543
1544    RETURN
1545      0 - Ok, the range structure filled with info about the next range
1546      1 - No more ranges
1547  */
1548  uint (*next) (range_seq_t seq, KEY_MULTI_RANGE *range);
1549
1550  /*
1551    Check whether range_info orders to skip the next record
1552
1553    SYNOPSIS
1554      skip_record()
1555        seq         The value returned by RANGE_SEQ_IF::init()
1556        range_info  Information about the next range
1557                    (Ignored if MRR_NO_ASSOCIATION is set)
1558        rowid       Rowid of the record to be checked (ignored if set to 0)
1559
1560    RETURN
1561      1 - Record with this range_info and/or this rowid shall be filtered
1562          out from the stream of records returned by multi_range_read_next()
1563      0 - The record shall be left in the stream
1564  */
1565  bool (*skip_record) (range_seq_t seq, char *range_info, uchar *rowid);
1566
1567  /*
1568    Check if the record combination matches the index condition
1569    SYNOPSIS
1570      skip_index_tuple()
1571        seq         The value returned by RANGE_SEQ_IF::init()
1572        range_info  Information about the next range
1573
1574    RETURN
1575      0 - The record combination satisfies the index condition
1576      1 - Otherwise
1577  */
1578  bool (*skip_index_tuple) (range_seq_t seq, char *range_info);
1579} RANGE_SEQ_IF;
1580
1581uint16 &mrr_persistent_flag_storage(range_seq_t seq, uint idx);
1582char* &mrr_get_ptr_by_idx(range_seq_t seq, uint idx);
1583
1584/**
1585  Used to store optimizer cost estimates.
1586
1587  The class consists of PODs only: default operator=, copy constructor
1588  and destructor are used.
1589 */
1590class Cost_estimate
1591{
1592private:
1593  double io_cost;                               ///< cost of I/O operations
1594  double cpu_cost;                              ///< cost of CPU operations
1595  double import_cost;                           ///< cost of remote operations
1596  double mem_cost;                              ///< memory used (bytes)
1597
1598public:
1599  Cost_estimate() :
1600    io_cost(0),
1601    cpu_cost(0),
1602    import_cost(0),
1603    mem_cost(0)
1604  {}
1605
1606  /// Returns sum of time-consuming costs, i.e., not counting memory cost
1607  double total_cost() const  { return io_cost + cpu_cost + import_cost; }
1608  double get_io_cost()     const { return io_cost; }
1609  double get_cpu_cost()    const { return cpu_cost; }
1610  double get_import_cost() const { return import_cost; }
1611  double get_mem_cost()    const { return mem_cost; }
1612
1613  /**
1614    Whether or not all costs in the object are zero
1615
1616    @return true if all costs are zero, false otherwise
1617  */
1618  bool is_zero() const
1619  {
1620    return !(io_cost || cpu_cost || import_cost || mem_cost);
1621  }
1622  /**
1623    Whether or not the total cost is the maximal double
1624
1625    @return true if total cost is the maximal double, false otherwise
1626  */
1627  bool is_max_cost()  const { return io_cost == DBL_MAX; }
1628  /// Reset all costs to zero
1629  void reset()
1630  {
1631    io_cost= cpu_cost= import_cost= mem_cost= 0;
1632  }
1633  /// Set current cost to the maximal double
1634  void set_max_cost()
1635  {
1636    reset();
1637    io_cost= DBL_MAX;
1638  }
1639
1640  /// Multiply io, cpu and import costs by parameter
1641  void multiply(double m)
1642  {
1643    DBUG_ASSERT(!is_max_cost());
1644
1645    io_cost *= m;
1646    cpu_cost *= m;
1647    import_cost *= m;
1648    /* Don't multiply mem_cost */
1649  }
1650
1651  Cost_estimate& operator+= (const Cost_estimate &other)
1652  {
1653    DBUG_ASSERT(!is_max_cost() && !other.is_max_cost());
1654
1655    io_cost+= other.io_cost;
1656    cpu_cost+= other.cpu_cost;
1657    import_cost+= other.import_cost;
1658    mem_cost+= other.mem_cost;
1659
1660    return *this;
1661  }
1662
1663  Cost_estimate operator+ (const Cost_estimate &other)
1664  {
1665    Cost_estimate result= *this;
1666    result+= other;
1667
1668    return result;
1669  }
1670
1671  Cost_estimate operator- (const Cost_estimate &other)
1672  {
1673    Cost_estimate result;
1674
1675    DBUG_ASSERT(!other.is_max_cost());
1676
1677    result.io_cost= io_cost - other.io_cost;
1678    result.cpu_cost= cpu_cost - other.cpu_cost;
1679    result.import_cost= import_cost - other.import_cost;
1680    result.mem_cost= mem_cost - other.mem_cost;
1681    return result;
1682  }
1683
1684  bool operator> (const Cost_estimate &other) const
1685  {
1686    return total_cost() > other.total_cost() ? true : false;
1687  }
1688
1689  bool operator< (const Cost_estimate &other) const
1690  {
1691    return other > *this ? true : false;
1692  }
1693
1694  /// Add to IO cost
1695  void add_io(double add_io_cost)
1696  {
1697    DBUG_ASSERT(!is_max_cost());
1698    io_cost+= add_io_cost;
1699  }
1700
1701  /// Add to CPU cost
1702  void add_cpu(double add_cpu_cost)
1703  {
1704    DBUG_ASSERT(!is_max_cost());
1705    cpu_cost+= add_cpu_cost;
1706  }
1707
1708  /// Add to import cost
1709  void add_import(double add_import_cost)
1710  {
1711    DBUG_ASSERT(!is_max_cost());
1712    import_cost+= add_import_cost;
1713  }
1714
1715  /// Add to memory cost
1716  void add_mem(double add_mem_cost)
1717  {
1718    DBUG_ASSERT(!is_max_cost());
1719    mem_cost+= add_mem_cost;
1720  }
1721};
1722
1723void get_sweep_read_cost(TABLE *table, ha_rows nrows, bool interrupted,
1724                         Cost_estimate *cost);
1725
1726/*
1727  The below two are not used (and not handled) in this milestone of this WL
1728  entry because there seems to be no use for them at this stage of
1729  implementation.
1730*/
1731#define HA_MRR_SINGLE_POINT 1
1732#define HA_MRR_FIXED_KEY  2
1733
1734/*
1735  Indicates that RANGE_SEQ_IF::next(&range) doesn't need to fill in the
1736  'range' parameter.
1737*/
1738#define HA_MRR_NO_ASSOCIATION 4
1739
1740/*
1741  The MRR user will provide ranges in key order, and MRR implementation
1742  must return rows in key order.
1743  Passing this flag to multi_read_range_init() may cause the
1744  default MRR handler to be used even if HA_MRR_USE_DEFAULT_IMPL
1745  was not specified.
1746  (If the native MRR impl. can not provide SORTED result)
1747*/
1748#define HA_MRR_SORTED 8
1749
1750/* MRR implementation doesn't have to retrieve full records */
1751#define HA_MRR_INDEX_ONLY 16
1752
1753/*
1754  The passed memory buffer is of maximum possible size, the caller can't
1755  assume larger buffer.
1756*/
1757#define HA_MRR_LIMITS 32
1758
1759
1760/*
1761  Flag set <=> default MRR implementation is used
1762  (The choice is made by **_info[_const]() function which may set this
1763   flag. SQL layer remembers the flag value and then passes it to
1764   multi_read_range_init().
1765*/
1766#define HA_MRR_USE_DEFAULT_IMPL 64
1767
1768/*
1769  Used only as parameter to multi_range_read_info():
1770  Flag set <=> the caller guarantees that the bounds of the scanned ranges
1771  will not have NULL values.
1772*/
1773#define HA_MRR_NO_NULL_ENDPOINTS 128
1774
1775/*
1776  Set by the MRR implementation to signal that it will natively
1777  produced sorted result if multi_range_read_init() is called with
1778  the HA_MRR_SORTED flag - Else multi_range_read_init(HA_MRR_SORTED)
1779  will revert to use the default MRR implementation.
1780*/
1781#define HA_MRR_SUPPORT_SORTED 256
1782
1783
1784class ha_statistics
1785{
1786public:
1787  ulonglong data_file_length;		/* Length off data file */
1788  ulonglong max_data_file_length;	/* Length off data file */
1789  ulonglong index_file_length;
1790  ulonglong max_index_file_length;
1791  ulonglong delete_length;		/* Free bytes */
1792  ulonglong auto_increment_value;
1793  /*
1794    The number of records in the table.
1795      0    - means the table has exactly 0 rows
1796    other  - if (table_flags() & HA_STATS_RECORDS_IS_EXACT)
1797               the value is the exact number of records in the table
1798             else
1799               it is an estimate
1800  */
1801  ha_rows records;
1802  ha_rows deleted;			/* Deleted records */
1803  ulong mean_rec_length;		/* physical reclength */
1804  ulong create_time;			/* When table was created */
1805  ulong check_time;
1806  ulong update_time;
1807  uint block_size;			/* index block size */
1808
1809  /*
1810    number of buffer bytes that native mrr implementation needs,
1811  */
1812  uint mrr_length_per_rec;
1813
1814  ha_statistics():
1815    data_file_length(0), max_data_file_length(0),
1816    index_file_length(0), delete_length(0), auto_increment_value(0),
1817    records(0), deleted(0), mean_rec_length(0), create_time(0),
1818    check_time(0), update_time(0), block_size(0)
1819  {}
1820};
1821
1822uint calculate_key_len(TABLE *, uint, const uchar *, key_part_map);
1823/*
1824  bitmap with first N+1 bits set
1825  (keypart_map for a key prefix of [0..N] keyparts)
1826*/
1827#define make_keypart_map(N) (((key_part_map)2 << (N)) - 1)
1828/*
1829  bitmap with first N bits set
1830  (keypart_map for a key prefix of [0..N-1] keyparts)
1831*/
1832#define make_prev_keypart_map(N) (((key_part_map)1 << (N)) - 1)
1833
1834
1835/** Base class to be used by handlers different shares */
1836class Handler_share
1837{
1838public:
1839  Handler_share() {}
1840  virtual ~Handler_share() {}
1841};
1842
1843
1844/**
1845  Wrapper for struct ft_hints.
1846*/
1847
1848class Ft_hints: public Sql_alloc
1849{
1850private:
1851  struct ft_hints hints;
1852
1853public:
1854  Ft_hints(uint ft_flags)
1855  {
1856    hints.flags= ft_flags;
1857    hints.op_type= FT_OP_UNDEFINED;
1858    hints.op_value= 0.0;
1859    hints.limit= HA_POS_ERROR;
1860  }
1861
1862  /**
1863    Set comparison operation type and and value for master MATCH function.
1864
1865     @param type   comparison operation type
1866     @param value  comparison operation value
1867  */
1868  void set_hint_op(enum ft_operation type, double value)
1869  {
1870    hints.op_type= type;
1871    hints.op_value= value;
1872  }
1873
1874  /**
1875    Set Ft_hints flag.
1876
1877    @param ft_flag Ft_hints flag
1878  */
1879  void set_hint_flag(uint ft_flag)
1880  {
1881    hints.flags|= ft_flag;
1882  }
1883
1884  /**
1885    Set Ft_hints limit.
1886
1887    @param Ft_hints limit
1888  */
1889  void set_hint_limit(ha_rows ft_limit)
1890  {
1891    hints.limit= ft_limit;
1892  }
1893
1894  /**
1895    Get Ft_hints limit.
1896
1897    @return Ft_hints limit
1898  */
1899  ha_rows get_limit()
1900  {
1901    return hints.limit;
1902  }
1903
1904  /**
1905    Get Ft_hints operation value.
1906
1907    @return operation value
1908  */
1909  double get_op_value()
1910  {
1911    return hints.op_value;
1912  }
1913
1914  /**
1915    Get Ft_hints operation type.
1916
1917    @return operation type
1918  */
1919  enum ft_operation get_op_type()
1920  {
1921    return hints.op_type;
1922  }
1923
1924  /**
1925    Get Ft_hints flags.
1926
1927    @return Ft_hints flags
1928  */
1929  uint get_flags()
1930  {
1931    return hints.flags;
1932  }
1933
1934 /**
1935    Get ft_hints struct.
1936
1937    @return pointer to ft_hints struct
1938  */
1939  struct ft_hints* get_hints()
1940  {
1941    return &hints;
1942  }
1943};
1944
1945
1946/**
1947  The handler class is the interface for dynamically loadable
1948  storage engines. Do not add ifdefs and take care when adding or
1949  changing virtual functions to avoid vtable confusion
1950
1951  Functions in this class accept and return table columns data. Two data
1952  representation formats are used:
1953  1. TableRecordFormat - Used to pass [partial] table records to/from
1954     storage engine
1955
1956  2. KeyTupleFormat - used to pass index search tuples (aka "keys") to
1957     storage engine. See opt_range.cc for description of this format.
1958
1959  TableRecordFormat
1960  =================
1961  [Warning: this description is work in progress and may be incomplete]
1962  The table record is stored in a fixed-size buffer:
1963
1964    record: null_bytes, column1_data, column2_data, ...
1965
1966  The offsets of the parts of the buffer are also fixed: every column has
1967  an offset to its column{i}_data, and if it is nullable it also has its own
1968  bit in null_bytes.
1969
1970  The record buffer only includes data about columns that are marked in the
1971  relevant column set (table->read_set and/or table->write_set, depending on
1972  the situation).
1973  <not-sure>It could be that it is required that null bits of non-present
1974  columns are set to 1</not-sure>
1975
1976  VARIOUS EXCEPTIONS AND SPECIAL CASES
1977
1978  f the table has no nullable columns, then null_bytes is still
1979  present, its length is one byte <not-sure> which must be set to 0xFF
1980  at all times. </not-sure>
1981
1982  If the table has columns of type BIT, then certain bits from those columns
1983  may be stored in null_bytes as well. Grep around for Field_bit for
1984  details.
1985
1986  For blob columns (see Field_blob), the record buffer stores length of the
1987  data, following by memory pointer to the blob data. The pointer is owned
1988  by the storage engine and is valid until the next operation.
1989
1990  If a blob column has NULL value, then its length and blob data pointer
1991  must be set to 0.
1992*/
1993
1994class handler :public Sql_alloc
1995{
1996public:
1997  typedef ulonglong Table_flags;
1998protected:
1999  TABLE_SHARE *table_share;             /* The table definition */
2000  TABLE *table;                         /* The current open table */
2001  Table_flags cached_table_flags;       /* Set on init() and open() */
2002
2003  ha_rows estimation_rows_to_insert;
2004public:
2005  handlerton *ht;                 /* storage engine of this handler */
2006  uchar *ref;				/* Pointer to current row */
2007  uchar *dup_ref;			/* Pointer to duplicate row */
2008
2009  ha_statistics stats;
2010
2011  /* MultiRangeRead-related members: */
2012  range_seq_t mrr_iter;    /* Interator to traverse the range sequence */
2013  RANGE_SEQ_IF mrr_funcs;  /* Range sequence traversal functions */
2014  HANDLER_BUFFER *multi_range_buffer; /* MRR buffer info */
2015  uint ranges_in_seq; /* Total number of ranges in the traversed sequence */
2016  /* TRUE <=> source MRR ranges and the output are ordered */
2017  bool mrr_is_output_sorted;
2018
2019  /* TRUE <=> we're currently traversing a range in mrr_cur_range. */
2020  bool mrr_have_range;
2021  /* Current range (the one we're now returning rows from) */
2022  KEY_MULTI_RANGE mrr_cur_range;
2023
2024  /*
2025    The direction of the current range or index scan. This is used by
2026    the ICP implementation to determine if it has reached the end
2027    of the current range.
2028  */
2029  enum enum_range_scan_direction {
2030    RANGE_SCAN_ASC,
2031    RANGE_SCAN_DESC
2032  };
2033private:
2034  /*
2035    Storage space for the end range value. Should only be accessed using
2036    the end_range pointer. The content is invalid when end_range is NULL.
2037  */
2038  key_range save_end_range;
2039  enum_range_scan_direction range_scan_direction;
2040  int key_compare_result_on_equal;
2041
2042protected:
2043  KEY_PART_INFO *range_key_part;
2044  bool eq_range;
2045  /*
2046    TRUE <=> the engine guarantees that returned records are within the range
2047    being scanned.
2048  */
2049  bool in_range_check_pushed_down;
2050
2051public:
2052  /*
2053    End value for a range scan. If this is NULL the range scan has no
2054    end value. Should also be NULL when there is no ongoing range scan.
2055    Used by the read_range() functions and also evaluated by pushed
2056    index conditions.
2057  */
2058  key_range *end_range;
2059  uint errkey;				/* Last dup key */
2060  uint key_used_on_scan;
2061  uint active_index;
2062  /** Length of ref (1-8 or the clustered key length) */
2063  uint ref_length;
2064  FT_INFO *ft_handler;
2065  enum {NONE=0, INDEX, RND} inited;
2066  bool implicit_emptied;                /* Can be !=0 only if HEAP */
2067  const Item *pushed_cond;
2068
2069  Item *pushed_idx_cond;
2070  uint pushed_idx_cond_keyno;  /* The index which the above condition is for */
2071
2072  /**
2073    next_insert_id is the next value which should be inserted into the
2074    auto_increment column: in a inserting-multi-row statement (like INSERT
2075    SELECT), for the first row where the autoinc value is not specified by the
2076    statement, get_auto_increment() called and asked to generate a value,
2077    next_insert_id is set to the next value, then for all other rows
2078    next_insert_id is used (and increased each time) without calling
2079    get_auto_increment().
2080  */
2081  ulonglong next_insert_id;
2082  /**
2083    insert id for the current row (*autogenerated*; if not
2084    autogenerated, it's 0).
2085    At first successful insertion, this variable is stored into
2086    THD::first_successful_insert_id_in_cur_stmt.
2087  */
2088  ulonglong insert_id_for_cur_row;
2089  /**
2090    Interval returned by get_auto_increment() and being consumed by the
2091    inserter.
2092  */
2093  Discrete_interval auto_inc_interval_for_cur_row;
2094  /**
2095     Number of reserved auto-increment intervals. Serves as a heuristic
2096     when we have no estimation of how many records the statement will insert:
2097     the more intervals we have reserved, the bigger the next one. Reset in
2098     handler::ha_release_auto_increment().
2099  */
2100  uint auto_inc_intervals_count;
2101
2102  /**
2103    Instrumented table associated with this handler.
2104  */
2105  PSI_table *m_psi;
2106
2107private:
2108#ifdef HAVE_PSI_TABLE_INTERFACE
2109
2110  /** Internal state of the batch instrumentation. */
2111  enum batch_mode_t
2112  {
2113    /** Batch mode not used. */
2114    PSI_BATCH_MODE_NONE,
2115    /** Batch mode used, before first table io. */
2116    PSI_BATCH_MODE_STARTING,
2117    /** Batch mode used, after first table io. */
2118    PSI_BATCH_MODE_STARTED
2119  };
2120
2121  /**
2122    Batch mode state.
2123    @sa start_psi_batch_mode.
2124    @sa end_psi_batch_mode.
2125  */
2126  batch_mode_t m_psi_batch_mode;
2127  /**
2128    The number of rows in the batch.
2129    @sa start_psi_batch_mode.
2130    @sa end_psi_batch_mode.
2131  */
2132  ulonglong m_psi_numrows;
2133  /**
2134    The current event in a batch.
2135    @sa start_psi_batch_mode.
2136    @sa end_psi_batch_mode.
2137  */
2138  PSI_table_locker *m_psi_locker;
2139  /**
2140    Storage for the event in a batch.
2141    @sa start_psi_batch_mode.
2142    @sa end_psi_batch_mode.
2143  */
2144  PSI_table_locker_state m_psi_locker_state;
2145#endif
2146
2147public:
2148  virtual void unbind_psi();
2149  virtual void rebind_psi();
2150  /**
2151    Put the handler in 'batch' mode when collecting
2152    table io instrumented events.
2153    When operating in batch mode:
2154    - a single start event is generated in the performance schema.
2155    - all table io performed between @c start_psi_batch_mode
2156      and @c end_psi_batch_mode is not instrumented:
2157      the number of rows affected is counted instead in @c m_psi_numrows.
2158    - a single end event is generated in the performance schema
2159      when the batch mode ends with @c end_psi_batch_mode.
2160  */
2161  void start_psi_batch_mode();
2162  /** End a batch started with @c start_psi_batch_mode. */
2163  void end_psi_batch_mode();
2164
2165private:
2166  friend class DsMrr_impl;
2167  /**
2168    The lock type set by when calling::ha_external_lock(). This is
2169    propagated down to the storage engine. The reason for also storing
2170    it here, is that when doing MRR we need to create/clone a second handler
2171    object. This cloned handler object needs to know about the lock_type used.
2172  */
2173  int m_lock_type;
2174  /**
2175    Pointer where to store/retrieve the Handler_share pointer.
2176    For non partitioned handlers this is &TABLE_SHARE::ha_share.
2177  */
2178  Handler_share **ha_share;
2179
2180public:
2181  handler(handlerton *ht_arg, TABLE_SHARE *share_arg)
2182    :table_share(share_arg), table(0),
2183    estimation_rows_to_insert(0), ht(ht_arg),
2184    ref(0), range_scan_direction(RANGE_SCAN_ASC),
2185    in_range_check_pushed_down(false), end_range(NULL),
2186    key_used_on_scan(MAX_KEY), active_index(MAX_KEY),
2187    ref_length(sizeof(my_off_t)),
2188    ft_handler(0), inited(NONE),
2189    implicit_emptied(0),
2190    pushed_cond(0), pushed_idx_cond(NULL), pushed_idx_cond_keyno(MAX_KEY),
2191    next_insert_id(0), insert_id_for_cur_row(0),
2192    auto_inc_intervals_count(0),
2193    m_psi(NULL),
2194#ifdef HAVE_PSI_TABLE_INTERFACE
2195    m_psi_batch_mode(PSI_BATCH_MODE_NONE),
2196    m_psi_numrows(0),
2197    m_psi_locker(NULL),
2198#endif
2199    m_lock_type(F_UNLCK), ha_share(NULL)
2200    {
2201      DBUG_PRINT("info",
2202                 ("handler created F_UNLCK %d F_RDLCK %d F_WRLCK %d",
2203                  F_UNLCK, F_RDLCK, F_WRLCK));
2204    }
2205  virtual ~handler(void)
2206  {
2207    DBUG_ASSERT(m_psi == NULL);
2208#ifdef HAVE_PSI_TABLE_INTERFACE
2209    DBUG_ASSERT(m_psi_batch_mode == PSI_BATCH_MODE_NONE);
2210    DBUG_ASSERT(m_psi_locker == NULL);
2211#endif
2212    DBUG_ASSERT(m_lock_type == F_UNLCK);
2213    DBUG_ASSERT(inited == NONE);
2214  }
2215  virtual handler *clone(const char *name, MEM_ROOT *mem_root);
2216  /** This is called after create to allow us to set up cached variables */
2217  void init()
2218  {
2219    cached_table_flags= table_flags();
2220  }
2221  /* ha_ methods: public wrappers for private virtual API */
2222
2223  int ha_open(TABLE *table, const char *name, int mode, int test_if_locked);
2224  int ha_close(void);
2225  int ha_index_init(uint idx, bool sorted);
2226  int ha_index_end();
2227  int ha_rnd_init(bool scan);
2228  int ha_rnd_end();
2229  int ha_rnd_next(uchar *buf);
2230  int ha_rnd_pos(uchar * buf, uchar *pos);
2231  int ha_index_read_map(uchar *buf, const uchar *key,
2232                        key_part_map keypart_map,
2233                        enum ha_rkey_function find_flag);
2234  int ha_index_read_last_map(uchar * buf, const uchar * key,
2235                             key_part_map keypart_map);
2236  int ha_index_read_idx_map(uchar *buf, uint index, const uchar *key,
2237                           key_part_map keypart_map,
2238                           enum ha_rkey_function find_flag);
2239  int ha_index_next(uchar * buf);
2240  int ha_index_prev(uchar * buf);
2241  int ha_index_first(uchar * buf);
2242  int ha_index_last(uchar * buf);
2243  int ha_index_next_same(uchar *buf, const uchar *key, uint keylen);
2244  int ha_index_read(uchar *buf, const uchar *key, uint key_len,
2245                    enum ha_rkey_function find_flag);
2246  int ha_index_read_last(uchar *buf, const uchar *key, uint key_len);
2247  int ha_reset();
2248  /* this is necessary in many places, e.g. in HANDLER command */
2249  int ha_index_or_rnd_end()
2250  {
2251    return inited == INDEX ? ha_index_end() : inited == RND ? ha_rnd_end() : 0;
2252  }
2253  /**
2254    The cached_table_flags is set at ha_open and ha_external_lock
2255  */
2256  Table_flags ha_table_flags() const { return cached_table_flags; }
2257  /**
2258    These functions represent the public interface to *users* of the
2259    handler class, hence they are *not* virtual. For the inheritance
2260    interface, see the (private) functions write_row(), update_row(),
2261    and delete_row() below.
2262  */
2263  int ha_external_lock(THD *thd, int lock_type);
2264  int ha_write_row(uchar * buf);
2265  int ha_update_row(const uchar * old_data, uchar * new_data);
2266  int ha_delete_row(const uchar * buf);
2267  void ha_release_auto_increment();
2268
2269  int check_collation_compatibility();
2270  int ha_check_for_upgrade(HA_CHECK_OPT *check_opt);
2271  /** to be actually called to get 'check()' functionality*/
2272  int ha_check(THD *thd, HA_CHECK_OPT *check_opt);
2273  int ha_repair(THD* thd, HA_CHECK_OPT* check_opt);
2274  void ha_start_bulk_insert(ha_rows rows);
2275  int ha_end_bulk_insert();
2276  int ha_bulk_update_row(const uchar *old_data, uchar *new_data,
2277                         uint *dup_key_found);
2278  int ha_delete_all_rows();
2279  int ha_truncate();
2280  int ha_reset_auto_increment(ulonglong value);
2281  int ha_optimize(THD* thd, HA_CHECK_OPT* check_opt);
2282  int ha_analyze(THD* thd, HA_CHECK_OPT* check_opt);
2283  bool ha_check_and_repair(THD *thd);
2284  int ha_disable_indexes(uint mode);
2285  int ha_enable_indexes(uint mode);
2286  int ha_discard_or_import_tablespace(my_bool discard);
2287  int ha_rename_table(const char *from, const char *to);
2288  int ha_delete_table(const char *name);
2289  void ha_drop_table(const char *name);
2290
2291  int ha_create(const char *name, TABLE *form, HA_CREATE_INFO *info);
2292
2293  int ha_create_handler_files(const char *name, const char *old_name,
2294                              int action_flag, HA_CREATE_INFO *info);
2295
2296  int ha_change_partitions(HA_CREATE_INFO *create_info,
2297                           const char *path,
2298                           ulonglong * const copied,
2299                           ulonglong * const deleted,
2300                           const uchar *pack_frm_data,
2301                           size_t pack_frm_len);
2302  int ha_drop_partitions(const char *path);
2303  int ha_rename_partitions(const char *path);
2304
2305  void adjust_next_insert_id_after_explicit_value(ulonglong nr);
2306  int update_auto_increment();
2307  virtual void print_error(int error, myf errflag);
2308  virtual bool get_error_message(int error, String *buf);
2309  uint get_dup_key(int error);
2310  /**
2311    Retrieves the names of the table and the key for which there was a
2312    duplicate entry in the case of HA_ERR_FOREIGN_DUPLICATE_KEY.
2313
2314    If any of the table or key name is not available this method will return
2315    false and will not change any of child_table_name or child_key_name.
2316
2317    @param child_table_name[out]    Table name
2318    @param child_table_name_len[in] Table name buffer size
2319    @param child_key_name[out]      Key name
2320    @param child_key_name_len[in]   Key name buffer size
2321
2322    @retval  true                  table and key names were available
2323                                   and were written into the corresponding
2324                                   out parameters.
2325    @retval  false                 table and key names were not available,
2326                                   the out parameters were not touched.
2327  */
2328  virtual bool get_foreign_dup_key(char *child_table_name,
2329                                   uint child_table_name_len,
2330                                   char *child_key_name,
2331                                   uint child_key_name_len)
2332  { DBUG_ASSERT(false); return(false); }
2333  virtual void change_table_ptr(TABLE *table_arg, TABLE_SHARE *share)
2334  {
2335    table= table_arg;
2336    table_share= share;
2337  }
2338  /* Estimates calculation */
2339
2340  /**
2341    @deprecated This function is deprecated and will be removed in a future
2342                version. Use table_scan_cost() instead.
2343  */
2344
2345  virtual double scan_time()
2346  { return ulonglong2double(stats.data_file_length) / IO_SIZE + 2; }
2347
2348  /**
2349    The cost of reading a set of ranges from the table using an index
2350    to access it.
2351
2352    @deprecated This function is deprecated and will be removed in a future
2353                version. Use read_cost() instead.
2354
2355    @param index  The index number.
2356    @param ranges The number of ranges to be read.
2357    @param rows   Total number of rows to be read.
2358
2359    This method can be used to calculate the total cost of scanning a table
2360    using an index by calling it using read_time(index, 1, table_size).
2361  */
2362
2363  virtual double read_time(uint index, uint ranges, ha_rows rows)
2364  { return rows2double(ranges+rows); }
2365
2366  /**
2367    @deprecated This function is deprecated and will be removed in a future
2368                version. Use index_scan_cost() instead.
2369  */
2370
2371  virtual double index_only_read_time(uint keynr, double records);
2372
2373  /**
2374    Cost estimate for doing a complete table scan.
2375
2376    @note For this version it is recommended that storage engines continue
2377    to override scan_time() instead of this function.
2378
2379    @returns the estimated cost
2380  */
2381
2382  virtual Cost_estimate table_scan_cost();
2383
2384  /**
2385    Cost estimate for reading a number of ranges from an index.
2386
2387    The cost estimate will only include the cost of reading data that
2388    is contained in the index. If the records need to be read, use
2389    read_cost() instead.
2390
2391    @note The ranges parameter is currently ignored and is not taken
2392    into account in the cost estimate.
2393
2394    @note For this version it is recommended that storage engines continue
2395    to override index_only_read_time() instead of this function.
2396
2397    @param index  the index number
2398    @param ranges the number of ranges to be read
2399    @param rows   total number of rows to be read
2400
2401    @returns the estimated cost
2402  */
2403
2404  virtual Cost_estimate index_scan_cost(uint index, double ranges, double rows);
2405
2406  /**
2407    Cost estimate for reading a set of ranges from the table using an index
2408    to access it.
2409
2410    @note For this version it is recommended that storage engines continue
2411    to override read_time() instead of this function.
2412
2413    @param index  the index number
2414    @param ranges the number of ranges to be read
2415    @param rows   total number of rows to be read
2416
2417    @returns the estimated cost
2418  */
2419
2420  virtual Cost_estimate read_cost(uint index, double ranges, double rows);
2421
2422  /**
2423    Return an estimate on the amount of memory the storage engine will
2424    use for caching data in memory. If this is unknown or the storage
2425    engine does not cache data in memory -1 is returned.
2426  */
2427  virtual longlong get_memory_buffer_size() const { return -1; }
2428
2429  virtual ha_rows multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
2430                                              void *seq_init_param,
2431                                              uint n_ranges, uint *bufsz,
2432                                              uint *flags,
2433                                              Cost_estimate *cost);
2434  virtual ha_rows multi_range_read_info(uint keyno, uint n_ranges, uint keys,
2435                                        uint *bufsz, uint *flags,
2436                                        Cost_estimate *cost);
2437  virtual int multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
2438                                    uint n_ranges, uint mode,
2439                                    HANDLER_BUFFER *buf);
2440  virtual int multi_range_read_next(char **range_info);
2441
2442
2443  virtual const key_map *keys_to_use_for_scanning() { return &key_map_empty; }
2444  bool has_transactions()
2445  { return (ha_table_flags() & HA_NO_TRANSACTIONS) == 0; }
2446  virtual uint extra_rec_buf_length() const { return 0; }
2447
2448  /**
2449    @brief Determine whether an error can be ignored or not.
2450
2451    @details This method is used to analyze the error to see whether the
2452    error is ignorable or not. Such errors will be reported as warnings
2453    instead of errors for IGNORE statements. This means that the statement
2454    will not abort, but instead continue to the next row.
2455
2456    HA_ERR_FOUND_DUP_UNIQUE is a special case in MyISAM that means the
2457    same thing as HA_ERR_FOUND_DUP_KEY, but can in some cases lead to
2458    a slightly different error message.
2459
2460    @param error  error code received from the handler interface (HA_ERR_...)
2461
2462    @return   whether the error is ignorablel or not
2463      @retval true  the error is ignorable
2464      @retval false the error is not ignorable
2465  */
2466
2467  virtual bool is_ignorable_error(int error);
2468
2469  /**
2470    @brief Determine whether an error is fatal or not.
2471
2472    @details This method is used to analyze the error to see whether the
2473    error is fatal or not. A fatal error is an error that will not be
2474    possible to handle with SP handlers and will not be subject to
2475    retry attempts on the slave.
2476
2477    @param error  error code received from the handler interface (HA_ERR_...)
2478
2479    @return   whether the error is fatal or not
2480      @retval true  the error is fatal
2481      @retval false the error is not fatal
2482  */
2483
2484  virtual bool is_fatal_error(int error);
2485
2486protected:
2487  /**
2488    Number of rows in table. It will only be called if
2489    (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
2490    @param[out]  num_rows number of rows in table.
2491    @retval 0 for OK, one of the HA_xxx values in case of error.
2492  */
2493  virtual int records(ha_rows *num_rows)
2494  {
2495    *num_rows= stats.records;
2496    return 0;
2497  }
2498
2499public:
2500 /**
2501   Public function wrapping the actual handler call, and doing error checking.
2502    @param[out]  num_rows number of rows in table.
2503    @retval 0 for OK, one of the HA_xxx values in case of error.
2504 */
2505  int ha_records(ha_rows *num_rows)
2506  {
2507    int error= records(num_rows);
2508    // A return value of HA_POS_ERROR was previously used to indicate error.
2509    if (error != 0)
2510      DBUG_ASSERT(*num_rows == HA_POS_ERROR);
2511    if (*num_rows == HA_POS_ERROR)
2512      DBUG_ASSERT(error != 0);
2513    if (error != 0)
2514    {
2515      /*
2516        ha_innobase::records may have rolled back internally.
2517        In this case, thd_mark_transaction_to_rollback() will have been called.
2518        For the errors below, we need to abort right away.
2519      */
2520      switch (error) {
2521      case HA_ERR_LOCK_DEADLOCK:
2522      case HA_ERR_LOCK_TABLE_FULL:
2523      case HA_ERR_LOCK_WAIT_TIMEOUT:
2524      case HA_ERR_QUERY_INTERRUPTED:
2525        print_error(error, MYF(0));
2526        return error;
2527      default:
2528        return error;
2529      }
2530    }
2531    return 0;
2532  }
2533
2534  /**
2535    Return upper bound of current number of records in the table
2536    (max. of how many records one will retrieve when doing a full table scan)
2537    If upper bound is not known, HA_POS_ERROR should be returned as a max
2538    possible upper bound.
2539  */
2540  virtual ha_rows estimate_rows_upper_bound()
2541  { return stats.records+EXTRA_RECORDS; }
2542
2543  /**
2544    Get the row type from the storage engine.  If this method returns
2545    ROW_TYPE_NOT_USED, the information in HA_CREATE_INFO should be used.
2546  */
2547  virtual enum row_type get_row_type() const { return ROW_TYPE_NOT_USED; }
2548
2549  virtual const char *index_type(uint key_number) { DBUG_ASSERT(0); return "";}
2550
2551
2552  /**
2553    Signal that the table->read_set and table->write_set table maps changed
2554    The handler is allowed to set additional bits in the above map in this
2555    call. Normally the handler should ignore all calls until we have done
2556    a ha_rnd_init() or ha_index_init(), write_row(), update_row or delete_row()
2557    as there may be several calls to this routine.
2558  */
2559  virtual void column_bitmaps_signal();
2560  uint get_index(void) const { return active_index; }
2561
2562  /**
2563    @retval  0   Bulk update used by handler
2564    @retval  1   Bulk update not used, normal operation used
2565  */
2566  virtual bool start_bulk_update() { return 1; }
2567  /**
2568    @retval  0   Bulk delete used by handler
2569    @retval  1   Bulk delete not used, normal operation used
2570  */
2571  virtual bool start_bulk_delete() { return 1; }
2572  /**
2573    After this call all outstanding updates must be performed. The number
2574    of duplicate key errors are reported in the duplicate key parameter.
2575    It is allowed to continue to the batched update after this call, the
2576    handler has to wait until end_bulk_update with changing state.
2577
2578    @param    dup_key_found       Number of duplicate keys found
2579
2580    @retval  0           Success
2581    @retval  >0          Error code
2582  */
2583  virtual int exec_bulk_update(uint *dup_key_found)
2584  {
2585    DBUG_ASSERT(FALSE);
2586    return HA_ERR_WRONG_COMMAND;
2587  }
2588  /**
2589    Perform any needed clean-up, no outstanding updates are there at the
2590    moment.
2591  */
2592  virtual void end_bulk_update() { return; }
2593  /**
2594    Execute all outstanding deletes and close down the bulk delete.
2595
2596    @retval 0             Success
2597    @retval >0            Error code
2598  */
2599  virtual int end_bulk_delete()
2600  {
2601    DBUG_ASSERT(FALSE);
2602    return HA_ERR_WRONG_COMMAND;
2603  }
2604protected:
2605  /**
2606     @brief
2607     Positions an index cursor to the index specified in the handle
2608     ('active_index'). Fetches the row if available. If the key value is null,
2609     begin at the first key of the index.
2610     @returns 0 if success (found a record, and function has set table->status
2611     to 0); non-zero if no record (function has set table->status to
2612     STATUS_NOT_FOUND).
2613  */
2614  virtual int index_read_map(uchar * buf, const uchar * key,
2615                             key_part_map keypart_map,
2616                             enum ha_rkey_function find_flag)
2617  {
2618    uint key_len= calculate_key_len(table, active_index, key, keypart_map);
2619    return  index_read(buf, key, key_len, find_flag);
2620  }
2621  /**
2622     @brief
2623     Positions an index cursor to the index specified in argument. Fetches
2624     the row if available. If the key value is null, begin at the first key of
2625     the index.
2626     @returns @see index_read_map().
2627  */
2628  virtual int index_read_idx_map(uchar * buf, uint index, const uchar * key,
2629                                 key_part_map keypart_map,
2630                                 enum ha_rkey_function find_flag);
2631  /// @returns @see index_read_map().
2632  virtual int index_next(uchar * buf)
2633   { return  HA_ERR_WRONG_COMMAND; }
2634  /// @returns @see index_read_map().
2635  virtual int index_prev(uchar * buf)
2636   { return  HA_ERR_WRONG_COMMAND; }
2637  /// @returns @see index_read_map().
2638  virtual int index_first(uchar * buf)
2639   { return  HA_ERR_WRONG_COMMAND; }
2640  /// @returns @see index_read_map().
2641  virtual int index_last(uchar * buf)
2642   { return  HA_ERR_WRONG_COMMAND; }
2643  /// @returns @see index_read_map().
2644  virtual int index_next_same(uchar *buf, const uchar *key, uint keylen);
2645  /**
2646     @brief
2647     The following functions works like index_read, but it find the last
2648     row with the current key value or prefix.
2649     @returns @see index_read_map().
2650  */
2651  virtual int index_read_last_map(uchar * buf, const uchar * key,
2652                                  key_part_map keypart_map)
2653  {
2654    uint key_len= calculate_key_len(table, active_index, key, keypart_map);
2655    return index_read_last(buf, key, key_len);
2656  }
2657public:
2658  virtual int read_range_first(const key_range *start_key,
2659                               const key_range *end_key,
2660                               bool eq_range, bool sorted);
2661  virtual int read_range_next();
2662
2663  /**
2664    Set the end position for a range scan. This is used for checking
2665    for when to end the range scan and by the ICP code to determine
2666    that the next record is within the current range.
2667
2668    @param range     The end value for the range scan
2669    @param direction Direction of the range scan
2670  */
2671  void set_end_range(const key_range* range,
2672                     enum_range_scan_direction direction);
2673  int compare_key(key_range *range);
2674  int compare_key_icp(const key_range *range) const;
2675  virtual int ft_init() { return HA_ERR_WRONG_COMMAND; }
2676  void ft_end() { ft_handler=NULL; }
2677  virtual FT_INFO *ft_init_ext(uint flags, uint inx,String *key)
2678    { return NULL; }
2679  virtual FT_INFO *ft_init_ext_with_hints(uint inx, String *key,
2680                                          Ft_hints *hints)
2681  {
2682    return ft_init_ext(hints->get_flags(), inx, key);
2683  }
2684  virtual int ft_read(uchar *buf) { return HA_ERR_WRONG_COMMAND; }
2685protected:
2686  /// @returns @see index_read_map().
2687  virtual int rnd_next(uchar *buf)=0;
2688  /// @returns @see index_read_map().
2689  virtual int rnd_pos(uchar * buf, uchar *pos)=0;
2690public:
2691  /**
2692    This function only works for handlers having
2693    HA_PRIMARY_KEY_REQUIRED_FOR_POSITION set.
2694    It will return the row with the PK given in the record argument.
2695  */
2696  virtual int rnd_pos_by_record(uchar *record)
2697    {
2698      DBUG_ASSERT(table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_POSITION);
2699      position(record);
2700      return ha_rnd_pos(record, ref);
2701    }
2702  virtual int read_first_row(uchar *buf, uint primary_key);
2703  /**
2704    The following function is only needed for tables that may be temporary
2705    tables during joins.
2706  */
2707  virtual int restart_rnd_next(uchar *buf, uchar *pos)
2708    { return HA_ERR_WRONG_COMMAND; }
2709  virtual int rnd_same(uchar *buf, uint inx)
2710    { return HA_ERR_WRONG_COMMAND; }
2711  virtual ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key)
2712    { return (ha_rows) 10; }
2713  /*
2714    If HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set, then it sets ref
2715    (reference to the row, aka position, with the primary key given in
2716    the record).
2717    Otherwise it set ref to the current row.
2718  */
2719  virtual void position(const uchar *record)=0;
2720  virtual int info(uint)=0; // see my_base.h for full description
2721  virtual void get_dynamic_partition_info(PARTITION_STATS *stat_info,
2722                                          uint part_id);
2723  virtual uint32 calculate_key_hash_value(Field **field_array)
2724  { DBUG_ASSERT(0); return 0; }
2725  virtual int extra(enum ha_extra_function operation)
2726  { return 0; }
2727  virtual int extra_opt(enum ha_extra_function operation, ulong cache_size)
2728  { return extra(operation); }
2729
2730  /**
2731    Start read (before write) removal on the current table.
2732    @see HA_READ_BEFORE_WRITE_REMOVAL
2733  */
2734  virtual bool start_read_removal(void)
2735  { DBUG_ASSERT(0); return false; }
2736
2737  /**
2738    End read (before write) removal and return the number of rows
2739    really written
2740    @see HA_READ_BEFORE_WRITE_REMOVAL
2741  */
2742  virtual ha_rows end_read_removal(void)
2743  { DBUG_ASSERT(0); return (ha_rows) 0; }
2744
2745  /**
2746    In an UPDATE or DELETE, if the row under the cursor was locked by another
2747    transaction, and the engine used an optimistic read of the last
2748    committed row value under the cursor, then the engine returns 1 from this
2749    function. MySQL must NOT try to update this optimistic value. If the
2750    optimistic value does not match the WHERE condition, MySQL can decide to
2751    skip over this row. Currently only works for InnoDB. This can be used to
2752    avoid unnecessary lock waits.
2753
2754    If this method returns nonzero, it will also signal the storage
2755    engine that the next read will be a locking re-read of the row.
2756  */
2757  virtual bool was_semi_consistent_read() { return 0; }
2758  /**
2759    Tell the engine whether it should avoid unnecessary lock waits.
2760    If yes, in an UPDATE or DELETE, if the row under the cursor was locked
2761    by another transaction, the engine may try an optimistic read of
2762    the last committed row value under the cursor.
2763  */
2764  virtual void try_semi_consistent_read(bool) {}
2765  virtual void unlock_row() {}
2766  virtual int start_stmt(THD *thd, thr_lock_type lock_type) {return 0;}
2767  virtual void get_auto_increment(ulonglong offset, ulonglong increment,
2768                                  ulonglong nb_desired_values,
2769                                  ulonglong *first_value,
2770                                  ulonglong *nb_reserved_values);
2771  void set_next_insert_id(ulonglong id)
2772  {
2773    DBUG_PRINT("info",("auto_increment: next value %lu", (ulong)id));
2774    next_insert_id= id;
2775  }
2776  void restore_auto_increment(ulonglong prev_insert_id)
2777  {
2778    /*
2779      Insertion of a row failed, re-use the lastly generated auto_increment
2780      id, for the next row. This is achieved by resetting next_insert_id to
2781      what it was before the failed insertion (that old value is provided by
2782      the caller). If that value was 0, it was the first row of the INSERT;
2783      then if insert_id_for_cur_row contains 0 it means no id was generated
2784      for this first row, so no id was generated since the INSERT started, so
2785      we should set next_insert_id to 0; if insert_id_for_cur_row is not 0, it
2786      is the generated id of the first and failed row, so we use it.
2787    */
2788    next_insert_id= (prev_insert_id > 0) ? prev_insert_id :
2789      insert_id_for_cur_row;
2790  }
2791
2792  virtual void update_create_info(HA_CREATE_INFO *create_info) {}
2793  int check_old_types();
2794  virtual int assign_to_keycache(THD* thd, HA_CHECK_OPT* check_opt)
2795  { return HA_ADMIN_NOT_IMPLEMENTED; }
2796  virtual int preload_keys(THD* thd, HA_CHECK_OPT* check_opt)
2797  { return HA_ADMIN_NOT_IMPLEMENTED; }
2798  /* end of the list of admin commands */
2799
2800  virtual int indexes_are_disabled(void) {return 0;}
2801  virtual void append_create_info(String *packet) {}
2802  /**
2803    If index == MAX_KEY then a check for table is made and if index <
2804    MAX_KEY then a check is made if the table has foreign keys and if
2805    a foreign key uses this index (and thus the index cannot be dropped).
2806
2807    @param  index            Index to check if foreign key uses it
2808
2809    @retval   TRUE            Foreign key defined on table or index
2810    @retval   FALSE           No foreign key defined
2811  */
2812  virtual bool is_fk_defined_on_table_or_index(uint index)
2813  { return FALSE; }
2814  virtual char* get_foreign_key_create_info()
2815  { return(NULL);}  /* gets foreign key create string from InnoDB */
2816  /**
2817    Used in ALTER TABLE to check if changing storage engine is allowed.
2818
2819    @note Called without holding thr_lock.c lock.
2820
2821    @retval true   Changing storage engine is allowed.
2822    @retval false  Changing storage engine not allowed.
2823  */
2824  virtual bool can_switch_engines() { return true; }
2825  /**
2826    Get the list of foreign keys in this table.
2827
2828    @remark Returns the set of foreign keys where this table is the
2829            dependent or child table.
2830
2831    @param thd  The thread handle.
2832    @param f_key_list[out]  The list of foreign keys.
2833
2834    @return The handler error code or zero for success.
2835  */
2836  virtual int
2837  get_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list)
2838  { return 0; }
2839  /**
2840    Get the list of foreign keys referencing this table.
2841
2842    @remark Returns the set of foreign keys where this table is the
2843            referenced or parent table.
2844
2845    @param thd  The thread handle.
2846    @param f_key_list[out]  The list of foreign keys.
2847
2848    @return The handler error code or zero for success.
2849  */
2850  virtual int
2851  get_parent_foreign_key_list(THD *thd, List<FOREIGN_KEY_INFO> *f_key_list)
2852  { return 0; }
2853  virtual uint referenced_by_foreign_key() { return 0;}
2854  virtual void init_table_handle_for_HANDLER()
2855  { return; }       /* prepare InnoDB for HANDLER */
2856  virtual void free_foreign_key_create_info(char* str) {}
2857  /** The following can be called without an open handler */
2858  virtual const char *table_type() const =0;
2859  /**
2860    If frm_error() is called then we will use this to find out what file
2861    extentions exist for the storage engine. This is also used by the default
2862    rename_table and delete_table method in handler.cc.
2863
2864    For engines that have two file name extentions (separate meta/index file
2865    and data file), the order of elements is relevant. First element of engine
2866    file name extentions array should be meta/index file extention. Second
2867    element - data file extention. This order is assumed by
2868    prepare_for_repair() when REPAIR TABLE ... USE_FRM is issued.
2869  */
2870  virtual const char **bas_ext() const =0;
2871
2872  virtual int get_default_no_partitions(HA_CREATE_INFO *info) { return 1;}
2873  virtual void set_auto_partitions(partition_info *part_info) { return; }
2874
2875  /**
2876    Get number of partitions for table in SE
2877
2878    @param name normalized path(same as open) to the table
2879
2880    @param[out] no_parts Number of partitions
2881
2882    @retval false for success
2883    @retval true for failure, for example table didn't exist in engine
2884  */
2885  virtual bool get_no_parts(const char *name,
2886                            uint *no_parts)
2887  {
2888    *no_parts= 0;
2889    return 0;
2890  }
2891  virtual void set_part_info(partition_info *part_info, bool early) {return;}
2892
2893  virtual ulong index_flags(uint idx, uint part, bool all_parts) const =0;
2894
2895  uint max_record_length() const
2896  {
2897    return std::min(HA_MAX_REC_LENGTH, max_supported_record_length());
2898  }
2899  uint max_keys() const
2900  {
2901    return std::min<uint>(MAX_KEY, max_supported_keys());
2902  }
2903  uint max_key_parts() const
2904  {
2905    return std::min(MAX_REF_PARTS, max_supported_key_parts());
2906  }
2907  uint max_key_length() const
2908  {
2909    return std::min(MAX_KEY_LENGTH, max_supported_key_length());
2910  }
2911  uint max_key_part_length() const
2912  {
2913    return std::min(MAX_KEY_LENGTH, max_supported_key_part_length());
2914  }
2915
2916  virtual uint max_supported_record_length() const { return HA_MAX_REC_LENGTH; }
2917  virtual uint max_supported_keys() const { return 0; }
2918  virtual uint max_supported_key_parts() const { return MAX_REF_PARTS; }
2919  virtual uint max_supported_key_length() const { return MAX_KEY_LENGTH; }
2920  virtual uint max_supported_key_part_length() const { return 255; }
2921  virtual uint min_record_length(uint options) const { return 1; }
2922
2923  virtual bool low_byte_first() const { return 1; }
2924  virtual uint checksum() const { return 0; }
2925  virtual bool is_crashed() const  { return 0; }
2926  virtual bool auto_repair() const { return 0; }
2927
2928
2929#define CHF_CREATE_FLAG 0
2930#define CHF_DELETE_FLAG 1
2931#define CHF_RENAME_FLAG 2
2932#define CHF_INDEX_FLAG  3
2933
2934
2935  /**
2936    @note lock_count() can return > 1 if the table is MERGE or partitioned.
2937  */
2938  virtual uint lock_count(void) const { return 1; }
2939  /**
2940    Is not invoked for non-transactional temporary tables.
2941
2942    @note store_lock() can return more than one lock if the table is MERGE
2943    or partitioned.
2944
2945    @note that one can NOT rely on table->in_use in store_lock().  It may
2946    refer to a different thread if called from mysql_lock_abort_for_thread().
2947
2948    @note If the table is MERGE, store_lock() can return less locks
2949    than lock_count() claimed. This can happen when the MERGE children
2950    are not attached when this is called from another thread.
2951  */
2952  virtual THR_LOCK_DATA **store_lock(THD *thd,
2953				     THR_LOCK_DATA **to,
2954				     enum thr_lock_type lock_type)=0;
2955
2956  /** Type of table for caching query */
2957  virtual uint8 table_cache_type() { return HA_CACHE_TBL_NONTRANSACT; }
2958
2959
2960  /**
2961    @brief Register a named table with a call back function to the query cache.
2962
2963    @param thd The thread handle
2964    @param table_key A pointer to the table name in the table cache
2965    @param key_length The length of the table name
2966    @param[out] engine_callback The pointer to the storage engine call back
2967      function
2968    @param[out] engine_data Storage engine specific data which could be
2969      anything
2970
2971    This method offers the storage engine, the possibility to store a reference
2972    to a table name which is going to be used with query cache.
2973    The method is called each time a statement is written to the cache and can
2974    be used to verify if a specific statement is cachable. It also offers
2975    the possibility to register a generic (but static) call back function which
2976    is called each time a statement is matched against the query cache.
2977
2978    @note If engine_data supplied with this function is different from
2979      engine_data supplied with the callback function, and the callback returns
2980      FALSE, a table invalidation on the current table will occur.
2981
2982    @return Upon success the engine_callback will point to the storage engine
2983      call back function, if any, and engine_data will point to any storage
2984      engine data used in the specific implementation.
2985      @retval TRUE Success
2986      @retval FALSE The specified table or current statement should not be
2987        cached
2988  */
2989
2990  virtual my_bool register_query_cache_table(THD *thd, char *table_key,
2991                                             size_t key_length,
2992                                             qc_engine_callback
2993                                             *engine_callback,
2994                                             ulonglong *engine_data)
2995  {
2996    *engine_callback= 0;
2997    return TRUE;
2998  }
2999
3000
3001 /*
3002   @retval TRUE   Primary key (if there is one) is clustered
3003                  key covering all fields
3004   @retval FALSE  otherwise
3005 */
3006 virtual bool primary_key_is_clustered() { return FALSE; }
3007 virtual int cmp_ref(const uchar *ref1, const uchar *ref2)
3008 {
3009   return memcmp(ref1, ref2, ref_length);
3010 }
3011
3012 /*
3013   Condition pushdown to storage engines
3014 */
3015
3016 /**
3017   Push condition down to the table handler.
3018
3019   @param  cond   Condition to be pushed. The condition tree must not be
3020                  modified by the by the caller.
3021
3022   @return
3023     The 'remainder' condition that caller must use to filter out records.
3024     NULL means the handler will not return rows that do not match the
3025     passed condition.
3026
3027   @note
3028   The pushed conditions form a stack (from which one can remove the
3029   last pushed condition using cond_pop).
3030   The table handler filters out rows using (pushed_cond1 AND pushed_cond2
3031   AND ... AND pushed_condN)
3032   or less restrictive condition, depending on handler's capabilities.
3033
3034   handler->ha_reset() call empties the condition stack.
3035   Calls to rnd_init/rnd_end, index_init/index_end etc do not affect the
3036   condition stack.
3037 */
3038 virtual const Item *cond_push(const Item *cond) { return cond; };
3039 /**
3040   Pop the top condition from the condition stack of the handler instance.
3041
3042   Pops the top if condition stack, if stack is not empty.
3043 */
3044 virtual void cond_pop() { return; }
3045
3046 /**
3047   Push down an index condition to the handler.
3048
3049   The server will use this method to push down a condition it wants
3050   the handler to evaluate when retrieving records using a specified
3051   index. The pushed index condition will only refer to fields from
3052   this handler that is contained in the index (but it may also refer
3053   to fields in other handlers). Before the handler evaluates the
3054   condition it must read the content of the index entry into the
3055   record buffer.
3056
3057   The handler is free to decide if and how much of the condition it
3058   will take responsibility for evaluating. Based on this evaluation
3059   it should return the part of the condition it will not evaluate.
3060   If it decides to evaluate the entire condition it should return
3061   NULL. If it decides not to evaluate any part of the condition it
3062   should return a pointer to the same condition as given as argument.
3063
3064   @param keyno    the index number to evaluate the condition on
3065   @param idx_cond the condition to be evaluated by the handler
3066
3067   @return The part of the pushed condition that the handler decides
3068           not to evaluate
3069  */
3070
3071 virtual Item *idx_cond_push(uint keyno, Item* idx_cond) { return idx_cond; }
3072
3073 /** Reset information about pushed index conditions */
3074 virtual void cancel_pushed_idx_cond()
3075 {
3076   pushed_idx_cond= NULL;
3077   pushed_idx_cond_keyno= MAX_KEY;
3078   in_range_check_pushed_down= false;
3079 }
3080
3081  /**
3082    Reports #tables included in pushed join which this
3083    handler instance is part of. ==0 -> Not pushed
3084  */
3085  virtual uint number_of_pushed_joins() const
3086  { return 0; }
3087
3088  /**
3089    If this handler instance is part of a pushed join sequence
3090    returned TABLE instance being root of the pushed query?
3091  */
3092  virtual const TABLE* root_of_pushed_join() const
3093  { return NULL; }
3094
3095  /**
3096    If this handler instance is a child in a pushed join sequence
3097    returned TABLE instance being my parent?
3098  */
3099  virtual const TABLE* parent_of_pushed_join() const
3100  { return NULL; }
3101
3102  virtual int index_read_pushed(uchar * buf, const uchar * key,
3103                             key_part_map keypart_map)
3104  { return  HA_ERR_WRONG_COMMAND; }
3105
3106  virtual int index_next_pushed(uchar * buf)
3107  { return  HA_ERR_WRONG_COMMAND; }
3108
3109 /**
3110   Part of old, deprecated in-place ALTER API.
3111 */
3112 virtual bool check_if_incompatible_data(HA_CREATE_INFO *create_info,
3113					 uint table_changes)
3114 { return COMPATIBLE_DATA_NO; }
3115
3116 /* On-line/in-place ALTER TABLE interface. */
3117
3118 /*
3119   Here is an outline of on-line/in-place ALTER TABLE execution through
3120   this interface.
3121
3122   Phase 1 : Initialization
3123   ========================
3124   During this phase we determine which algorithm should be used
3125   for execution of ALTER TABLE and what level concurrency it will
3126   require.
3127
3128   *) This phase starts by opening the table and preparing description
3129      of the new version of the table.
3130   *) Then we check if it is impossible even in theory to carry out
3131      this ALTER TABLE using the in-place algorithm. For example, because
3132      we need to change storage engine or the user has explicitly requested
3133      usage of the "copy" algorithm.
3134   *) If in-place ALTER TABLE is theoretically possible, we continue
3135      by compiling differences between old and new versions of the table
3136      in the form of HA_ALTER_FLAGS bitmap. We also build a few
3137      auxiliary structures describing requested changes and store
3138      all these data in the Alter_inplace_info object.
3139   *) Then the handler::check_if_supported_inplace_alter() method is called
3140      in order to find if the storage engine can carry out changes requested
3141      by this ALTER TABLE using the in-place algorithm. To determine this,
3142      the engine can rely on data in HA_ALTER_FLAGS/Alter_inplace_info
3143      passed to it as well as on its own checks. If the in-place algorithm
3144      can be used for this ALTER TABLE, the level of required concurrency for
3145      its execution is also returned.
3146      If any errors occur during the handler call, ALTER TABLE is aborted
3147      and no further handler functions are called.
3148   *) Locking requirements of the in-place algorithm are compared to any
3149      concurrency requirements specified by user. If there is a conflict
3150      between them, we either switch to the copy algorithm or emit an error.
3151
3152   Phase 2 : Execution
3153   ===================
3154
3155   In this phase the operations are executed.
3156
3157   *) As the first step, we acquire a lock corresponding to the concurrency
3158      level which was returned by handler::check_if_supported_inplace_alter()
3159      and requested by the user. This lock is held for most of the
3160      duration of in-place ALTER (if HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE
3161      or HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE were returned we acquire an
3162      exclusive lock for duration of the next step only).
3163   *) After that we call handler::ha_prepare_inplace_alter_table() to give the
3164      storage engine a chance to update its internal structures with a higher
3165      lock level than the one that will be used for the main step of algorithm.
3166      After that we downgrade the lock if it is necessary.
3167   *) After that, the main step of this phase and algorithm is executed.
3168      We call the handler::ha_inplace_alter_table() method, which carries out the
3169      changes requested by ALTER TABLE but does not makes them visible to other
3170      connections yet.
3171   *) We ensure that no other connection uses the table by upgrading our
3172      lock on it to exclusive.
3173   *) a) If the previous step succeeds, handler::ha_commit_inplace_alter_table() is
3174         called to allow the storage engine to do any final updates to its structures,
3175         to make all earlier changes durable and visible to other connections.
3176      b) If we have failed to upgrade lock or any errors have occured during the
3177         handler functions calls (including commit), we call
3178         handler::ha_commit_inplace_alter_table()
3179         to rollback all changes which were done during previous steps.
3180
3181  Phase 3 : Final
3182  ===============
3183
3184  In this phase we:
3185
3186  *) Update SQL-layer data-dictionary by installing .FRM file for the new version
3187     of the table.
3188  *) Inform the storage engine about this change by calling the
3189     handler::ha_notify_table_changed() method.
3190  *) Destroy the Alter_inplace_info and handler_ctx objects.
3191
3192 */
3193
3194 /**
3195    Check if a storage engine supports a particular alter table in-place
3196
3197    @param    altered_table     TABLE object for new version of table.
3198    @param    ha_alter_info     Structure describing changes to be done
3199                                by ALTER TABLE and holding data used
3200                                during in-place alter.
3201
3202    @retval   HA_ALTER_ERROR                  Unexpected error.
3203    @retval   HA_ALTER_INPLACE_NOT_SUPPORTED  Not supported, must use copy.
3204    @retval   HA_ALTER_INPLACE_EXCLUSIVE_LOCK Supported, but requires X lock.
3205    @retval   HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE
3206                                              Supported, but requires SNW lock
3207                                              during main phase. Prepare phase
3208                                              requires X lock.
3209    @retval   HA_ALTER_INPLACE_SHARED_LOCK    Supported, but requires SNW lock.
3210    @retval   HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE
3211                                              Supported, concurrent reads/writes
3212                                              allowed. However, prepare phase
3213                                              requires X lock.
3214    @retval   HA_ALTER_INPLACE_NO_LOCK        Supported, concurrent
3215                                              reads/writes allowed.
3216
3217    @note The default implementation uses the old in-place ALTER API
3218    to determine if the storage engine supports in-place ALTER or not.
3219
3220    @note Called without holding thr_lock.c lock.
3221 */
3222 virtual enum_alter_inplace_result
3223 check_if_supported_inplace_alter(TABLE *altered_table,
3224                                  Alter_inplace_info *ha_alter_info);
3225
3226
3227 /**
3228    Public functions wrapping the actual handler call.
3229    @see prepare_inplace_alter_table()
3230 */
3231 bool ha_prepare_inplace_alter_table(TABLE *altered_table,
3232                                     Alter_inplace_info *ha_alter_info);
3233
3234
3235 /**
3236    Public function wrapping the actual handler call.
3237    @see inplace_alter_table()
3238 */
3239 bool ha_inplace_alter_table(TABLE *altered_table,
3240                             Alter_inplace_info *ha_alter_info)
3241 {
3242   return inplace_alter_table(altered_table, ha_alter_info);
3243 }
3244
3245
3246 /**
3247    Public function wrapping the actual handler call.
3248    Allows us to enforce asserts regardless of handler implementation.
3249    @see commit_inplace_alter_table()
3250 */
3251 bool ha_commit_inplace_alter_table(TABLE *altered_table,
3252                                    Alter_inplace_info *ha_alter_info,
3253                                    bool commit);
3254
3255
3256 /**
3257    Public function wrapping the actual handler call.
3258    @see notify_table_changed()
3259 */
3260 void ha_notify_table_changed()
3261 {
3262   notify_table_changed();
3263 }
3264
3265
3266protected:
3267 /**
3268    Allows the storage engine to update internal structures with concurrent
3269    writes blocked. If check_if_supported_inplace_alter() returns
3270    HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE or
3271    HA_ALTER_INPLACE_SHARED_AFTER_PREPARE, this function is called with
3272    exclusive lock otherwise the same level of locking as for
3273    inplace_alter_table() will be used.
3274
3275    @note Storage engines are responsible for reporting any errors by
3276    calling my_error()/print_error()
3277
3278    @note If this function reports error, commit_inplace_alter_table()
3279    will be called with commit= false.
3280
3281    @note For partitioning, failing to prepare one partition, means that
3282    commit_inplace_alter_table() will be called to roll back changes for
3283    all partitions. This means that commit_inplace_alter_table() might be
3284    called without prepare_inplace_alter_table() having been called first
3285    for a given partition.
3286
3287    @param    altered_table     TABLE object for new version of table.
3288    @param    ha_alter_info     Structure describing changes to be done
3289                                by ALTER TABLE and holding data used
3290                                during in-place alter.
3291
3292    @retval   true              Error
3293    @retval   false             Success
3294 */
3295 virtual bool prepare_inplace_alter_table(TABLE *altered_table,
3296                                          Alter_inplace_info *ha_alter_info)
3297 { return false; }
3298
3299
3300 /**
3301    Alter the table structure in-place with operations specified using HA_ALTER_FLAGS
3302    and Alter_inplace_info. The level of concurrency allowed during this
3303    operation depends on the return value from check_if_supported_inplace_alter().
3304
3305    @note Storage engines are responsible for reporting any errors by
3306    calling my_error()/print_error()
3307
3308    @note If this function reports error, commit_inplace_alter_table()
3309    will be called with commit= false.
3310
3311    @param    altered_table     TABLE object for new version of table.
3312    @param    ha_alter_info     Structure describing changes to be done
3313                                by ALTER TABLE and holding data used
3314                                during in-place alter.
3315
3316    @retval   true              Error
3317    @retval   false             Success
3318 */
3319 virtual bool inplace_alter_table(TABLE *altered_table,
3320                                  Alter_inplace_info *ha_alter_info)
3321 { return false; }
3322
3323
3324 /**
3325    Commit or rollback the changes made during prepare_inplace_alter_table()
3326    and inplace_alter_table() inside the storage engine.
3327    Note that in case of rollback the allowed level of concurrency during
3328    this operation will be the same as for inplace_alter_table() and thus
3329    might be higher than during prepare_inplace_alter_table(). (For example,
3330    concurrent writes were blocked during prepare, but might not be during
3331    rollback).
3332
3333    @note Storage engines are responsible for reporting any errors by
3334    calling my_error()/print_error()
3335
3336    @note If this function with commit= true reports error, it will be called
3337    again with commit= false.
3338
3339    @note In case of partitioning, this function might be called for rollback
3340    without prepare_inplace_alter_table() having been called first.
3341    Also partitioned tables sets ha_alter_info->group_commit_ctx to a NULL
3342    terminated array of the partitions handlers and if all of them are
3343    committed as one, then group_commit_ctx should be set to NULL to indicate
3344    to the partitioning handler that all partitions handlers are committed.
3345    @see prepare_inplace_alter_table().
3346
3347    @param    altered_table     TABLE object for new version of table.
3348    @param    ha_alter_info     Structure describing changes to be done
3349                                by ALTER TABLE and holding data used
3350                                during in-place alter.
3351    @param    commit            True => Commit, False => Rollback.
3352
3353    @retval   true              Error
3354    @retval   false             Success
3355 */
3356 virtual bool commit_inplace_alter_table(TABLE *altered_table,
3357                                         Alter_inplace_info *ha_alter_info,
3358                                         bool commit)
3359{
3360  /* Nothing to commit/rollback, mark all handlers committed! */
3361  ha_alter_info->group_commit_ctx= NULL;
3362  return false;
3363}
3364
3365
3366 /**
3367    Notify the storage engine that the table structure (.FRM) has been updated.
3368
3369    @note No errors are allowed during notify_table_changed().
3370 */
3371 virtual void notify_table_changed();
3372
3373public:
3374 /* End of On-line/in-place ALTER TABLE interface. */
3375
3376
3377  /**
3378    use_hidden_primary_key() is called in case of an update/delete when
3379    (table_flags() and HA_PRIMARY_KEY_REQUIRED_FOR_DELETE) is defined
3380    but we don't have a primary key
3381  */
3382  virtual void use_hidden_primary_key();
3383  virtual uint alter_table_flags(uint flags)
3384  {
3385    if (ht->alter_table_flags)
3386      return ht->alter_table_flags(flags);
3387    return 0;
3388  }
3389
3390protected:
3391  /* Service methods for use by storage engines. */
3392  void ha_statistic_increment(ulonglong SSV::*offset) const;
3393  void **ha_data(THD *) const;
3394  THD *ha_thd(void) const;
3395
3396  /**
3397    Acquire the instrumented table information from a table share.
3398    @param share a table share
3399    @return an instrumented table share, or NULL.
3400  */
3401  PSI_table_share *ha_table_share_psi(const TABLE_SHARE *share) const;
3402
3403  /**
3404    Default rename_table() and delete_table() rename/delete files with a
3405    given name and extensions from bas_ext().
3406
3407    These methods can be overridden, but their default implementation
3408    provide useful functionality.
3409  */
3410  virtual int rename_table(const char *from, const char *to);
3411  /**
3412    Delete a table in the engine. Called for base as well as temporary
3413    tables.
3414  */
3415  virtual int delete_table(const char *name);
3416private:
3417  /* Private helpers */
3418  inline void mark_trx_read_write();
3419  /*
3420    Low-level primitives for storage engines.  These should be
3421    overridden by the storage engine class. To call these methods, use
3422    the corresponding 'ha_*' method above.
3423  */
3424
3425  virtual int open(const char *name, int mode, uint test_if_locked)=0;
3426  virtual int close(void)=0;
3427  virtual int index_init(uint idx, bool sorted) { active_index= idx; return 0; }
3428  virtual int index_end() { active_index= MAX_KEY; return 0; }
3429  /**
3430    rnd_init() can be called two times without rnd_end() in between
3431    (it only makes sense if scan=1).
3432    then the second call should prepare for the new table scan (e.g
3433    if rnd_init allocates the cursor, second call should position it
3434    to the start of the table, no need to deallocate and allocate it again
3435  */
3436  virtual int rnd_init(bool scan)= 0;
3437  virtual int rnd_end() { return 0; }
3438  virtual int write_row(uchar *buf __attribute__((unused)))
3439  {
3440    return HA_ERR_WRONG_COMMAND;
3441  }
3442
3443  /**
3444    Update a single row.
3445
3446    Note: If HA_ERR_FOUND_DUPP_KEY is returned, the handler must read
3447    all columns of the row so MySQL can create an error message. If
3448    the columns required for the error message are not read, the error
3449    message will contain garbage.
3450  */
3451  virtual int update_row(const uchar *old_data __attribute__((unused)),
3452                         uchar *new_data __attribute__((unused)))
3453  {
3454    return HA_ERR_WRONG_COMMAND;
3455  }
3456
3457  virtual int delete_row(const uchar *buf __attribute__((unused)))
3458  {
3459    return HA_ERR_WRONG_COMMAND;
3460  }
3461  /**
3462    Reset state of file to after 'open'.
3463    This function is called after every statement for all tables used
3464    by that statement.
3465  */
3466  virtual int reset() { return 0; }
3467  virtual Table_flags table_flags(void) const= 0;
3468  /**
3469    Is not invoked for non-transactional temporary tables.
3470
3471    Tells the storage engine that we intend to read or write data
3472    from the table. This call is prefixed with a call to handler::store_lock()
3473    and is invoked only for those handler instances that stored the lock.
3474
3475    Calls to rnd_init/index_init are prefixed with this call. When table
3476    IO is complete, we call external_lock(F_UNLCK).
3477    A storage engine writer should expect that each call to
3478    ::external_lock(F_[RD|WR]LOCK is followed by a call to
3479    ::external_lock(F_UNLCK). If it is not, it is a bug in MySQL.
3480
3481    The name and signature originate from the first implementation
3482    in MyISAM, which would call fcntl to set/clear an advisory
3483    lock on the data file in this method.
3484
3485    @param   lock_type    F_RDLCK, F_WRLCK, F_UNLCK
3486
3487    @return  non-0 in case of failure, 0 in case of success.
3488    When lock_type is F_UNLCK, the return value is ignored.
3489  */
3490  virtual int external_lock(THD *thd __attribute__((unused)),
3491                            int lock_type __attribute__((unused)))
3492  {
3493    return 0;
3494  }
3495  virtual void release_auto_increment() { return; };
3496  /** admin commands - called from mysql_admin_table */
3497  virtual int check_for_upgrade(HA_CHECK_OPT *check_opt)
3498  { return 0; }
3499  virtual int check(THD* thd, HA_CHECK_OPT* check_opt)
3500  { return HA_ADMIN_NOT_IMPLEMENTED; }
3501
3502  /**
3503     In this method check_opt can be modified
3504     to specify CHECK option to use to call check()
3505     upon the table.
3506  */
3507  virtual int repair(THD* thd, HA_CHECK_OPT* check_opt)
3508  {
3509    DBUG_ASSERT(!(ha_table_flags() & HA_CAN_REPAIR));
3510    return HA_ADMIN_NOT_IMPLEMENTED;
3511  }
3512  virtual void start_bulk_insert(ha_rows rows) {}
3513  virtual int end_bulk_insert() { return 0; }
3514protected:
3515  virtual int index_read(uchar * buf, const uchar * key, uint key_len,
3516                         enum ha_rkey_function find_flag)
3517   { return  HA_ERR_WRONG_COMMAND; }
3518  virtual int index_read_last(uchar * buf, const uchar * key, uint key_len)
3519   { return (my_errno= HA_ERR_WRONG_COMMAND); }
3520public:
3521  /**
3522    This method is similar to update_row, however the handler doesn't need
3523    to execute the updates at this point in time. The handler can be certain
3524    that another call to bulk_update_row will occur OR a call to
3525    exec_bulk_update before the set of updates in this query is concluded.
3526
3527    Note: If HA_ERR_FOUND_DUPP_KEY is returned, the handler must read
3528    all columns of the row so MySQL can create an error message. If
3529    the columns required for the error message are not read, the error
3530    message will contain garbage.
3531
3532    @param    old_data       Old record
3533    @param    new_data       New record
3534    @param    dup_key_found  Number of duplicate keys found
3535
3536  */
3537  virtual int bulk_update_row(const uchar *old_data, uchar *new_data,
3538                              uint *dup_key_found)
3539  {
3540    DBUG_ASSERT(FALSE);
3541    return HA_ERR_WRONG_COMMAND;
3542  }
3543  /**
3544    This is called to delete all rows in a table
3545    If the handler don't support this, then this function will
3546    return HA_ERR_WRONG_COMMAND and MySQL will delete the rows one
3547    by one.
3548  */
3549  virtual int delete_all_rows()
3550  { return (my_errno=HA_ERR_WRONG_COMMAND); }
3551  /**
3552    Quickly remove all rows from a table.
3553
3554    @remark This method is responsible for implementing MySQL's TRUNCATE
3555            TABLE statement, which is a DDL operation. As such, a engine
3556            can bypass certain integrity checks and in some cases avoid
3557            fine-grained locking (e.g. row locks) which would normally be
3558            required for a DELETE statement.
3559
3560    @remark Typically, truncate is not used if it can result in integrity
3561            violation. For example, truncate is not used when a foreign
3562            key references the table, but it might be used if foreign key
3563            checks are disabled.
3564
3565    @remark Engine is responsible for resetting the auto-increment counter.
3566
3567    @remark The table is locked in exclusive mode.
3568  */
3569  virtual int truncate()
3570  { return HA_ERR_WRONG_COMMAND; }
3571  /**
3572    Reset the auto-increment counter to the given value, i.e. the next row
3573    inserted will get the given value. HA_ERR_WRONG_COMMAND is returned by
3574    storage engines that don't support this operation.
3575  */
3576  virtual int reset_auto_increment(ulonglong value)
3577  { return HA_ERR_WRONG_COMMAND; }
3578  virtual int optimize(THD* thd, HA_CHECK_OPT* check_opt)
3579  { return HA_ADMIN_NOT_IMPLEMENTED; }
3580  virtual int analyze(THD* thd, HA_CHECK_OPT* check_opt)
3581  { return HA_ADMIN_NOT_IMPLEMENTED; }
3582  virtual bool check_and_repair(THD *thd) { return TRUE; }
3583  virtual int disable_indexes(uint mode) { return HA_ERR_WRONG_COMMAND; }
3584  virtual int enable_indexes(uint mode) { return HA_ERR_WRONG_COMMAND; }
3585  virtual int discard_or_import_tablespace(my_bool discard)
3586  { return (my_errno=HA_ERR_WRONG_COMMAND); }
3587  virtual void drop_table(const char *name);
3588  virtual int create(const char *name, TABLE *form, HA_CREATE_INFO *info)=0;
3589
3590  virtual int create_handler_files(const char *name, const char *old_name,
3591                                   int action_flag, HA_CREATE_INFO *info)
3592  { return FALSE; }
3593
3594  virtual int change_partitions(HA_CREATE_INFO *create_info,
3595                                const char *path,
3596                                ulonglong * const copied,
3597                                ulonglong * const deleted,
3598                                const uchar *pack_frm_data,
3599                                size_t pack_frm_len)
3600  { return HA_ERR_WRONG_COMMAND; }
3601  virtual int drop_partitions(const char *path)
3602  { return HA_ERR_WRONG_COMMAND; }
3603  virtual int rename_partitions(const char *path)
3604  { return HA_ERR_WRONG_COMMAND; }
3605  virtual bool set_ha_share_ref(Handler_share **arg_ha_share)
3606  {
3607    DBUG_ASSERT(!ha_share);
3608    DBUG_ASSERT(arg_ha_share);
3609    if (ha_share || !arg_ha_share)
3610      return true;
3611    ha_share= arg_ha_share;
3612    return false;
3613  }
3614  int get_lock_type() const { return m_lock_type; }
3615protected:
3616  Handler_share *get_ha_share_ptr();
3617  void set_ha_share_ptr(Handler_share *arg_ha_share);
3618  void lock_shared_ha_data();
3619  void unlock_shared_ha_data();
3620};
3621
3622
3623bool key_uses_partial_cols(TABLE *table, uint keyno);
3624
3625/*
3626  A Disk-Sweep MRR interface implementation
3627
3628  This implementation makes range (and, in the future, 'ref') scans to read
3629  table rows in disk sweeps.
3630
3631  Currently it is used by MyISAM and InnoDB. Potentially it can be used with
3632  any table handler that has non-clustered indexes and on-disk rows.
3633*/
3634
3635class DsMrr_impl
3636{
3637public:
3638  typedef void (handler::*range_check_toggle_func_t)(bool on);
3639
3640  DsMrr_impl() : h2(NULL) {}
3641
3642  ~DsMrr_impl()
3643  {
3644    /*
3645      If ha_reset() has not been called then the h2 dialog might still
3646      exist. This must be closed and deleted (this is the case for
3647      internally created temporary tables).
3648    */
3649    if (h2)
3650      reset();
3651    DBUG_ASSERT(h2 == NULL);
3652  }
3653
3654  /*
3655    The "owner" handler object (the one that calls dsmrr_XXX functions.
3656    It is used to retrieve full table rows by calling rnd_pos().
3657  */
3658  handler *h;
3659  TABLE *table; /* Always equal to h->table */
3660private:
3661  /* Secondary handler object.  It is used for scanning the index */
3662  handler *h2;
3663
3664  /* Buffer to store rowids, or (rowid, range_id) pairs */
3665  uchar *rowids_buf;
3666  uchar *rowids_buf_cur;   /* Current position when reading/writing */
3667  uchar *rowids_buf_last;  /* When reading: end of used buffer space */
3668  uchar *rowids_buf_end;   /* End of the buffer */
3669
3670  bool dsmrr_eof; /* TRUE <=> We have reached EOF when reading index tuples */
3671
3672  /* TRUE <=> need range association, buffer holds {rowid, range_id} pairs */
3673  bool is_mrr_assoc;
3674
3675  bool use_default_impl; /* TRUE <=> shortcut all calls to default MRR impl */
3676public:
3677  /**
3678    Initialize the DsMrr_impl object.
3679
3680    This object is used for both doing default MRR scans and DS-MRR scans.
3681    This function just initializes the object. To do a DS-MRR scan,
3682    this must also be initialized by calling dsmrr_init().
3683
3684    @param h_arg     pointer to the handler that owns this object
3685    @param table_arg pointer to the TABLE that owns the handler
3686  */
3687
3688  void init(handler *h_arg, TABLE *table_arg)
3689  {
3690    DBUG_ASSERT(h_arg != NULL);
3691    DBUG_ASSERT(table_arg != NULL);
3692    h= h_arg;
3693    table= table_arg;
3694  }
3695
3696  int dsmrr_init(handler *h, RANGE_SEQ_IF *seq_funcs, void *seq_init_param,
3697                 uint n_ranges, uint mode, HANDLER_BUFFER *buf);
3698  void dsmrr_close();
3699
3700  /**
3701    Resets the DS-MRR object to the state it had after being intialized.
3702
3703    If there is an open scan then this will be closed.
3704
3705    This function should be called by handler::ha_reset() which is called
3706    when a statement is completed in order to make the handler object ready
3707    for re-use by a different statement.
3708  */
3709
3710  void reset();
3711  int dsmrr_fill_buffer();
3712  int dsmrr_next(char **range_info);
3713
3714  ha_rows dsmrr_info(uint keyno, uint n_ranges, uint keys, uint *bufsz,
3715                     uint *flags, Cost_estimate *cost);
3716
3717  ha_rows dsmrr_info_const(uint keyno, RANGE_SEQ_IF *seq,
3718                            void *seq_init_param, uint n_ranges, uint *bufsz,
3719                            uint *flags, Cost_estimate *cost);
3720private:
3721  bool choose_mrr_impl(uint keyno, ha_rows rows, uint *flags, uint *bufsz,
3722                       Cost_estimate *cost);
3723  bool get_disk_sweep_mrr_cost(uint keynr, ha_rows rows, uint flags,
3724                               uint *buffer_size, Cost_estimate *cost);
3725};
3726	/* Some extern variables used with handlers */
3727
3728extern const char *ha_row_type[];
3729extern MYSQL_PLUGIN_IMPORT const char *tx_isolation_names[];
3730extern MYSQL_PLUGIN_IMPORT const char *binlog_format_names[];
3731extern TYPELIB tx_isolation_typelib;
3732extern const char *myisam_stats_method_names[];
3733extern ulong total_ha, total_ha_2pc;
3734
3735/* lookups */
3736handlerton *ha_default_handlerton(THD *thd);
3737handlerton *ha_default_temp_handlerton(THD *thd);
3738plugin_ref ha_resolve_by_name(THD *thd, const LEX_STRING *name,
3739                              bool is_temp_table);
3740plugin_ref ha_lock_engine(THD *thd, const handlerton *hton);
3741handlerton *ha_resolve_by_legacy_type(THD *thd, enum legacy_db_type db_type);
3742handler *get_new_handler(TABLE_SHARE *share, MEM_ROOT *alloc,
3743                         handlerton *db_type);
3744handlerton *ha_checktype(THD *thd, enum legacy_db_type database_type,
3745                          bool no_substitute, bool report_error);
3746
3747
3748static inline enum legacy_db_type ha_legacy_type(const handlerton *db_type)
3749{
3750  return (db_type == NULL) ? DB_TYPE_UNKNOWN : db_type->db_type;
3751}
3752
3753static inline const char *ha_resolve_storage_engine_name(const handlerton *db_type)
3754{
3755  return db_type == NULL ? "UNKNOWN" : hton2plugin[db_type->slot]->name.str;
3756}
3757
3758static inline bool ha_check_storage_engine_flag(const handlerton *db_type, uint32 flag)
3759{
3760  return db_type == NULL ? FALSE : MY_TEST(db_type->flags & flag);
3761}
3762
3763static inline bool ha_storage_engine_is_enabled(const handlerton *db_type)
3764{
3765  return (db_type && db_type->create) ?
3766         (db_type->state == SHOW_OPTION_YES) : FALSE;
3767}
3768
3769/* basic stuff */
3770int ha_init_errors(void);
3771int ha_init(void);
3772int ha_end(void);
3773int ha_initialize_handlerton(st_plugin_int *plugin);
3774int ha_finalize_handlerton(st_plugin_int *plugin);
3775
3776TYPELIB* ha_known_exts();
3777int ha_panic(enum ha_panic_function flag);
3778void ha_close_connection(THD* thd);
3779void ha_kill_connection(THD *thd);
3780bool ha_flush_logs(handlerton *db_type);
3781void ha_drop_database(char* path);
3782int ha_create_table(THD *thd, const char *path,
3783                    const char *db, const char *table_name,
3784                    HA_CREATE_INFO *create_info,
3785		                bool update_create_info,
3786                    bool is_temp_table= false);
3787
3788int ha_delete_table(THD *thd, handlerton *db_type, const char *path,
3789                    const char *db, const char *alias, bool generate_warning);
3790
3791/* statistics and info */
3792bool ha_show_status(THD *thd, handlerton *db_type, enum ha_stat_type stat);
3793
3794/* discovery */
3795int ha_create_table_from_engine(THD* thd, const char *db, const char *name);
3796bool ha_check_if_table_exists(THD* thd, const char *db, const char *name,
3797                             bool *exists);
3798int ha_discover(THD* thd, const char* dbname, const char* name,
3799                uchar** frmblob, size_t* frmlen);
3800int ha_find_files(THD *thd,const char *db,const char *path,
3801                  const char *wild, bool dir, List<LEX_STRING>* files);
3802int ha_table_exists_in_engine(THD* thd, const char* db, const char* name);
3803bool ha_check_if_supported_system_table(handlerton *hton, const char* db,
3804                                        const char* table_name);
3805
3806/* key cache */
3807extern "C" int ha_init_key_cache(const char *name, KEY_CACHE *key_cache);
3808int ha_resize_key_cache(KEY_CACHE *key_cache);
3809int ha_change_key_cache_param(KEY_CACHE *key_cache);
3810int ha_change_key_cache(KEY_CACHE *old_key_cache, KEY_CACHE *new_key_cache);
3811
3812/* report to InnoDB that control passes to the client */
3813int ha_release_temporary_latches(THD *thd);
3814
3815/* transactions: interface to handlerton functions */
3816int ha_start_consistent_snapshot(THD *thd);
3817int ha_commit_trans(THD *thd, bool all, bool ignore_global_read_lock= false);
3818int ha_rollback_trans(THD *thd, bool all);
3819int ha_prepare(THD *thd);
3820
3821
3822/**
3823  recover() step of xa.
3824
3825  @note
3826    there are three modes of operation:
3827    - automatic recover after a crash
3828    in this case commit_list != 0, tc_heuristic_recover==0
3829    all xids from commit_list are committed, others are rolled back
3830    - manual (heuristic) recover
3831    in this case commit_list==0, tc_heuristic_recover != 0
3832    DBA has explicitly specified that all prepared transactions should
3833    be committed (or rolled back).
3834    - no recovery (MySQL did not detect a crash)
3835    in this case commit_list==0, tc_heuristic_recover == 0
3836    there should be no prepared transactions in this case.
3837*/
3838
3839int ha_recover(HASH *commit_list);
3840
3841/*
3842 transactions: interface to low-level handlerton functions. These are
3843 intended to be used by the transaction coordinators to
3844 commit/prepare/rollback transactions in the engines.
3845*/
3846int ha_commit_low(THD *thd, bool all, bool run_after_commit= true);
3847int ha_prepare_low(THD *thd, bool all);
3848int ha_rollback_low(THD *thd, bool all);
3849
3850/* transactions: these functions never call handlerton functions directly */
3851int ha_enable_transaction(THD *thd, bool on);
3852
3853/* savepoints */
3854int ha_rollback_to_savepoint(THD *thd, SAVEPOINT *sv);
3855bool ha_rollback_to_savepoint_can_release_mdl(THD *thd);
3856int ha_savepoint(THD *thd, SAVEPOINT *sv);
3857int ha_release_savepoint(THD *thd, SAVEPOINT *sv);
3858
3859/* Build pushed joins in handlers implementing this feature */
3860int ha_make_pushed_joins(THD *thd, const AQP::Join_plan* plan);
3861
3862/* these are called by storage engines */
3863void trans_register_ha(THD *thd, bool all, handlerton *ht,
3864                       const ulonglong *trxid);
3865/*
3866  Storage engine has to assume the transaction will end up with 2pc if
3867   - there is more than one 2pc-capable storage engine available
3868   - in the current transaction 2pc was not disabled yet
3869*/
3870#define trans_need_2pc(thd, all)                   ((total_ha_2pc > 1) && \
3871        !((all ? &thd->transaction.all : &thd->transaction.stmt)->no_2pc))
3872
3873int ha_reset_logs(THD *thd);
3874int ha_binlog_index_purge_file(THD *thd, const char *file);
3875void ha_reset_slave(THD *thd);
3876void ha_binlog_log_query(THD *thd, handlerton *db_type,
3877                         enum_binlog_command binlog_command,
3878                         const char *query, size_t query_length,
3879                         const char *db, const char *table_name);
3880void ha_binlog_wait(THD *thd);
3881
3882/* It is required by basic binlog features on both MySQL server and libmysqld */
3883int ha_binlog_end(THD *thd);
3884
3885const char *ha_legacy_type_name(legacy_db_type legacy_type);
3886const char *get_canonical_filename(handler *file, const char *path,
3887                                   char *tmp_path);
3888
3889inline const char *table_case_name(HA_CREATE_INFO *info, const char *name)
3890{
3891  return ((lower_case_table_names == 2 && info->alias) ? info->alias : name);
3892}
3893
3894void print_keydup_error(TABLE *table, KEY *key, const char *msg, myf errflag);
3895void print_keydup_error(TABLE *table, KEY *key, myf errflag);
3896
3897#endif /* HANDLER_INCLUDED */
3898