Grinnz / Mojo-SQLite

Mojo::SQLite - A tiny Mojolicious wrapper for SQLite
https://metacpan.org/pod/Mojo::SQLite
Other
27 stars 12 forks source link

last insert id - a question #13

Closed kiwiroy closed 5 years ago

kiwiroy commented 5 years ago

@Grinnz I'd appreciate your thoughts/insight on this patch.

I have some code that does some insert or ignore, the table has a unique index, for which I'd like to simply count the successful inserts. Testing for truth of $result->last_insert_id seemed obvious. Having read the following I didn't expect most recent to survive failed inserts...

https://github.com/Grinnz/Mojo-SQLite/blob/9dffe657ff073f1c353a199e006cdbaa980f132b/lib/Mojo/SQLite/Results.pm#L196-L197

I expected an insert query to clear the last_insert_id as is the intent in this patch.

The same can be achieved with localised hash, but there's no interface for this, or by comparing to previous value of last_insert_id.

local $sqlite->db->dbh->{private_mojo_last_insert_id} = 0;

Thanks.

Grinnz commented 5 years ago

Note: "most recent successful insert". It is not designed to indicate success and I don't wish to introduce further complications/slowdowns to do so. (The LAST_INSERT_ID() function in most databases functions the same way.) Unless I'm mistaken the returned value of the insert or ignore query should indicate the number of rows inserted (also available as $result->rows).

kiwiroy commented 5 years ago

A case of RTFM. Thanks for your time.