Estendere CREATE TABLE

Stai visualizzando una vecchia versione di questo article. Visualizza la versione più recente.

A partire da MariaDB 5.2, gli Storage Engine possono dare la possibilità all'utente di specificare nuovi attributi per gli indici, i campi e le tabella. Lo SE deve dichiarare quali attributi introduce.

API

There are three new members in the hanlerton structure, they can be set in the engine's initialization function as follows:

example_hton->table_options= example_table_option_array;
example_hton->field_options= example_field_option_array;
example_hton->index_options= example_index_option_array;

The arrays are declared statically, as in the following example:

struct ha_table_option_struct
{
  char *strparam;
  ulonglong ullparam;
  uint enumparam;
  bool boolparam;
};

ha_create_table_option example_table_option_list[]=
{
  HA_TOPTION_NUMBER("NUMBER", ullparam, UINT_MAX32, 0, UINT_MAX32, 10),
  HA_TOPTION_STRING("STR", strparam),
  HA_TOPTION_ENUM("one_or_two", enumparam, "one,two", 0),
  HA_TOPTION_BOOL("YESNO", boolparam, 1),
  HA_TOPTION_END
};

The engine declares a structure ha_table_option_struct that will hold values of these new attributes.

And it describes these attributes to MySQL by creating an array of HA_TOPTION_* macros. Note a detail: these macros expect a structure called ha_table_option_struct , if the structure is called differently, a #define will be needed.

There are four supported types of attributes:

typecorresponding C typemacro nameadditional parameters of a macro
an integer numberunsigned long longHA_TOPTION_NUMBERa default value, minimal allowed value, maximal allowed value, a factor, that any allowed should be a multiple of.
a stringchar *HA_TOPTION_STRINGnone. The default value is a null pointer.
one value from a list of allowed valuesunsigned intHA_TOPTION_ENUMa string with a comma-separated list of allowed values, and a default value as a number, starting from 0.
a booleanboolHA_TOPTION_BOOLa default value

Do not use enum for your HA_TOPTION_ENUM C structure members, the size of the enum depends on the compiler, and even on the compilation options, and the plugin API uses only types with known storage sizes.

In all macros the first two parameters are name of the attribute as should be used in SQL in the CREATE TABLE statement, and the name of the corresponding member of the ha_table_option_struct structure.

The array ends with a HA_TOPTION_END macro.

Field and index (key) attributes are declared similarly using HA_FOPTION_* and HA_IOPTION_* macros.

When in a CREATE TABLE statement, the ::create() handler method is called, the table attributes are available in the table_arg->s->option_struct, field attributes - in the option_struct member of the individual fields (objects of the Field class), index attributes - in the option_struct member of the individual keys (objects of the KEY class).

Additionally, they are available in most other handler methods: the attributes are stored in the .frm file and on every open MySQL makes them available to the engine by filling the corresponding option_struct members of the table, fields, and keys.

The ALTER TABLE needs a special support from the engine. MySQL compares old and new table definitions to decide whether it needs to rebuild the table or not. As the semantics of the engine declared attributes is unknown, MySQL cannot make this decision by analyzing attribute values - this is delegated to the engine. The HA_CREATE_INFO structure has three new members:

ha_table_option_struct *option_struct;           ///< structure with parsed table options
ha_field_option_struct **fields_option_struct;   ///< array of field option structures
ha_index_option_struct **indexes_option_struct;  ///< array of index option structures

The engine (in the ::check_if_incompatible_data() method) is responsible for comparing new values of the attributes from the HA_CREATE_INFO structure with the old values from the table and returning COMPATIBLE_DATA_NO if they were changed in such a way that requires the table to be rebuild.

The example of declaring the attributes and comparing the values for the ALTER TABLE can be found in the EXAMPLE engine.

SQL

The engine declared attributes can be specified per field, index, or table in the CREATE TABLE or ALTER TABLE. The syntax is the conventional:

CREATE TABLE ... (
  field ... [attribute=value [attribute=value ...]],
  ...
  index ... [attribute=value [attribute=value ...]],
  ...
) ...  [attribute=value [attribute=value ...]]

All values must be specified as literals, not expressions. The value of a boolean option may be specified as one of YES, NO, ON, OFF, 1, or 0. A string value may be specified either quoted or not, as an identifier (if it is a valid identifier, of course). Compare with the old behavior:

CREATE TABLE ... (
) ENGINE=FEDERATED CONNECTION='mysql://[email protected]';

where the value of the ENGINE attribute is specified not quoted, while the value of the CONNECTION is quoted.

When an attribute is set, it will be stored with the table definition and shown in the SHOW CREATE TABLE; . To remove an attribute from a table definition use ALTER TABLE to set its value to a DEFAULT .

The values of unknown attributes or attributes with the illegal values cause an error by default. But by enabling IGNORE_BAD_TABLE_OPTIONS sql mode this error can be downgraded to a warning. In this case invalid attributes are stored in the .frm file verbatim, as later, if the table is altered to use a different storage engine, they may become valid for the new engine. This mode is implicitly enabled in the replication slave thread.

See Also

Commenti

Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.