Tuesday, 15 September 2015

sql server - What is the best way to get identity of records inserted using DBIx::Class::ResultSet::populate()? -



sql server - What is the best way to get identity of records inserted using DBIx::Class::ResultSet::populate()? -

as know, populate() method in dbix::class::resultset provides efficient mass inserts.

however, need identity of inserted records farther processing.

we have this:

my @created_records = $self->schema->instrument_rs->populate(\@records_to_create_rs); foreach $record ( @created_records ) { $id_of->{ $record->name } = $record->id; }

but, when populate() used in non-void context, list context in case, acts wrapper create() method , not efficient mass insert.

given limitation, recommend best way identity records bulk-inserted using dbix::class::resultset::populate()?

if have auto-incremented primary key, , there no other insertions going on @ same time, not solution mass insert n-1 records, insert lastly via normal create?

for illustration ( worked me ):

my $last_info = pop @records_to_create_rs; $items = scalar(@records_to_create_rs); $rs->populate( \@records_to_create_rs ); $last = $rs->create( $last_info ); ( $first_id, $last_id ) = ( $last->id - $items, $last->id ); @inserted_ids = ( $first_id .. $last_id );

edit: if wanted check whether insertion went planned ( instead of ensuring it, or anyway ), 1 expand upon shifting , inserting first info , comparing whether id count off. if is, method default records via search ( no speed gain then, sorry ), if not, went , done.

my $first_info = shift @records; $items = scalar( @records ); $last_info = pop @records; $rs->create( $first_info ); $rs->populate( \@records ); $last = $rs->create( $last_info ); $expected = $first_id +1; if ( $expected == $last->id - $items ){ homecoming [ $first_id .. $last->id ]; }else{ # records way, did not check @records = $rs->search( \@records )->all; unshift @records, $first; force @records, $last; homecoming [ map { $_->id } ]; }

i set first version 1 of add_to methods ( given array, bulk_insert ), appears working fine far.

i test sec version 1 of days , study if fails...

sql-server perl dbix-class

No comments:

Post a Comment