develooper Front page | perl.dbi.users | Postings from April 2018

Extend API for last_insert_id

Thread Next
April 27, 2018 14:05
Extend API for last_insert_id
Message ID:

I would like to propose two new changes to DBI which extends
last_insert_id API.

First one: Allow to call $dbh->last_insert_id() method without
arguments. Currently this method needs to take four arguments (plus
$dbh) and XS code validates their count:

        dVAR; dXSARGS;
        if (items < 5 || items > 6)
           croak_xs_usage(cv,  "dbh, catalog, schema, table, field, attr=Nullsv");

More databases does not process these arguments and caller needs to
supply four undefs.

So I'm proposing change that caller would be allowed to call
$dbh->last_insert_id() without any argument and DBI would fill those
missing arguments by undefs prior to calling DBI driver function. This
does not change driver API, so all existing DBI drivers would work as
before and allows application to not specify those useless four undef

Second change: Add a new statement method $sth->last_insert_id().
Some databases (e.g. MariaDB) supports tracking last insert id when more
statement or cursor handles are open. This would allow to call e.g.


for drivers which would support it. $sth1->last_insert_id() would return
insert it which belongs to last ->execute of $sth1 even there were more
INSERT/execute calls (e.g. by $sth2).

Currently database handle method $dbh->last_insert_id() returns id of
the most recent INSERT statement.

What do you think about those two enhancements?

Here is my prototype implementation for DBI:

DBI drivers can then implement own dbd_st_last_insert_id() function and
provide correct id for the selected $sth statement.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About