15.12. Adding Support for UPDATE
to a Storage Engine
The MySQL server executes UPDATE
statements by
performing a (table/index/range/etc.) scan until it locates a row
matching the WHERE
clause of the
UPDATE
statement, then calling the
update_row()
function:
int ha_foo::update_row(const byte *old_data, byte *new_data)
The *old_data
parameter contains the data that
existed in the row prior to the update, while the
*new_data
parameter contains the new contents
of the row (in the MySQL internal row format).
Performing an update will depend on row format and storage
implementation. Some storage engines will replace data in-place,
while other implementations delete the existing row and append the
new row at the end of the data file.
Non-indexed storage engines can typically ignore the contents of
the *old_data
parameter and just deal with the
*new_data
buffer. Transactional engines may
need to compare the buffers to determine what changes have been
made for a later rollback.
If the table being updated contains timestamp columns, the
updating of the timestamp will have to be managed in the
update_row()
call. The following example is
from the CSV
engine:
int ha_tina::update_row(const byte * old_data, byte * new_data)
{
int size;
DBUG_ENTER("ha_tina::update_row");
statistic_increment(table->in_use->status_var.ha_read_rnd_next_count,
&LOCK_status);
if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
table->timestamp_field->set_time();
size= encode_quote(new_data);
if (chain_append())
DBUG_RETURN(-1);
if (my_write(share->data_file, buffer.ptr(), size, MYF(MY_WME | MY_NABP)))
DBUG_RETURN(-1);
DBUG_RETURN(0);
}
Note the setting of the timestamp in the previous example.