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

Extend API for last_insert_id

Thread Next
From:
pali
Date:
April 27, 2018 14:05
Subject:
Extend API for last_insert_id
Message ID:
20180427140359.tskw67w63rry32zh@pali
Hello,

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:

    XS_EUPXS(XS_DBD__Perl__db_last_insert_id)
    {
        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
arguments.

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.

    $sth1->execute();
    $sth2->execute();
    ...
    $sth1->last_insert_id();
    $sth2->last_insert_id();

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:
https://github.com/perl5-dbi/dbi/compare/master...pali:last-insert-id

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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About