15.11. 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
write_row()
function:
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()
function.
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)
{
statistic_increment(table->in_use->status_var.ha_write_count,&LOCK_status);
/* If we have a timestamp column, update it to the current time */
if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_INSERT)
table->timestamp_field->set_time();
/*
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])
update_auto_increment();
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.