[Dbix-class] last insert ID from MSSQL?

Marc Mims marc at questright.com
Wed Oct 17 20:44:42 GMT 2007


* Matt S Trout <dbix-class at trout.me.uk> [071017 12:30]:
> On Tue, Oct 16, 2007 at 04:48:49PM -0700, Michael Higgins wrote:
> > Folks --
> > 
> > Something changed to break the lousy hack I had to get this before...
> > can't figure it out from the related code and changes. Was doing a
> > "single" result on the auto_increment key, \"order by \"invoice #\"
> > desc" knd of thing. And it broke after an update of perl modules.
> > 
> > Anyway, being unable to find out what broke it, I found a way to do it
> > more directly... sort of. 
> > 
> > In the interest of posting some working code, this is what I have come
> > up with:
> > 
> > The IDEA: "SELECT IDENT_CURRENT('tablename')"
> > 
> > The CODE:
> > 
> > our $last_bol = [$trex_schema->storage->dbh->selectall_arrayref("SELECT
> > IDENT_CURRENT('data')")]->[0]->[0]->[0];
> > 
> > ... which actually works(!). At least, so it appears.
> > 
> > But, it seems like I should be able to do a ->last_insert_id or at
> > least come up with some something less... nested..??
> > 
> > Is it because I'm using freeTDS? IOW, is there some way to get MSSQL
> > quirks handled transparently with, like a
> > 'schema->storage('::DBI::ODBC::MSSQL') type of call?
> 
> Yeah, storage objects are expected to provide a last_insert_id method which
> DBIC then calls. Have a poke around at the existing implementations, the
> ODBC storage already reblesses based on the db type so if you can find the
> right name to create your class as it should all Just Work.
> 
> I'd be delighted to get you a commit bit so we can maintain this in mainline
> DBIC if you're willing?

What does $schema->storage->dbh->get_info(17) return?

Assuming get_info(17) returns something useful, what's needed is a
trivial little module based on that name that provides
_dbh_last_insert_id and _sql_maker_opts.  See the source for
DBIx::Class::Storage::DBI::ODBC::DB2_400_SQL as an example.

If you're not up to writing the module, I'd be happy to put one together
you can test.

	-Marc



More information about the DBIx-Class mailing list