Documentation Home
MySQL Internals Manual

MySQL Internals Manual  /  Writing a Custom Storage Engine  /  Adding Support for INSERT to a Storage Engine

23.12 Adding Support for INSERT to a Storage Engine

Once you have read support in your storage engine, the next feature to implement is support for INSERT statements. With INSERT support in place, your storage engine can handle WORM (write once, read many) applications such as logging and archiving for later analysis.

All INSERT operations are handled through the [custom-engine.html#custom-engine-api-reference-write_row write_row()] method:

int ha_foo::write_row(byte *buf)

The *buf parameter contains the row to be inserted in the internal MySQL format. A basic storage engine could simply advance to the end of the data file and append the contents of the buffer directly (this would also make reading rows easier as you could read the row and pass it directly into the buffer parameter of the rnd_next() method.

The process for writing a row is the opposite of the process for reading one: take the data from the MySQL internal row format and write it to the data file. The following example is from the MyISAM storage engine:

int ha_myisam::write_row(byte * buf)

  /* If we have a timestamp column, update it to the current time */
  if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)

    If we have an auto_increment column and we are writing a changed row
    or a new row, then update the auto_increment value in the record.
  if (table->next_number_field && buf == table->record[0])
  return mi_write(file,buf);

Three items of note in the preceding example include the updating of table statistics for writes, the setting of the timestamp prior to writing the row, and the updating of AUTO_INCREMENT values.