Skip Menu |

This queue is for tickets about the DBI CPAN distribution.

Report information
The Basics
Id: 72276
Status: rejected
Priority: 0/
Queue: DBI

People
Owner: Nobody in particular
Requestors: EDAVIS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.616
Fixed in: (no value)



Subject: Wish: fetchrow_hashref_ordered to preserve column ordering
fetchrow_hashref returns a hash mapping name to value. Because hash keys are unordered this loses the original column ordering from the query. You can reconstruct it using the NAME attribute but it would be handy for DBI to have a method returning a hash with ordered keys, using Tie::IxHash. I imagine it would be called fetchrow_hashref_ordered and would work just as fetchrow_hashref, except it would return a reference to a Tie::IxHash tied hash where the key ordering is the same as that given by NAME. If you like the idea I could write a patch?
I'd vote against that for two reasons 1. It adds a dependency to DBI 2. It is much faster to use bind_columns (and have the correct order in an array), like $ cat test.csv a,b,c,d,e,f 1,2,3,4,5,6 2,3,4,5,6,7 3,4,5,6,7,8 4,5,6,7,8,9 $ cat test.pl use v5.12; use warnings; use autodie; use DBI; my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_ext => ".csv/r", PrintError => 1, RaiseError => 1, }); my $sth = $dbh->prepare ("select * from test"); $sth->execute; my %rec; my @columns = @{$sth->{NAME_lc}}; $sth->bind_columns (\@rec{@columns}); while ($sth->fetch) { say join ", " => map { "$_: $rec{$_}" } @columns; } $ perl test.pl a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 a: 2, b: 3, c: 4, d: 5, e: 6, f: 7 a: 3, b: 4, c: 5, d: 6, e: 7, f: 8 a: 4, b: 5, c: 6, d: 7, e: 8, f: 9 $
Hmm, you're right that this would add a dependency. I was mistakenly thinking Tie::IxHash was a core module (surprising it is not, really). It is true that bind_columns is faster. fetchrow_hashref is really just a convenience function for code where raw speed is not important. My suggested function is in the same spirit.
I think there's no need to change the DBI for this. It would be easy to add to user code for the very few people who would want it. Thanks for the suggestion.