patch: selectall_hashref

--------------D3C7021450705A0BA38BBC34
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


Hello!  First, thanks to you all for your work on DBI. I use it
frequently and find it well put together as well as useful tool.
Attached is a simple patch to the code AND pod of 1.14 which adds the
function 'selectall_hashref', which is just like selectall_arrayref, but
it defaults to returning an array of hashes instead of an array of
arrays. This is extremely useful in some cases. For example, this sort
of construct can be passed directly into an HTML::Template object. 

Thanks.

   -mark

http://mark.stosberg.com/
--------------D3C7021450705A0BA38BBC34
Content-Type: text/plain; charset=us-ascii;
 name="dbi-hashref-patch.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="dbi-hashref-patch.txt"

--- DBI.pm.org	Wed Mar 21 20:22:02 2001
+++ DBI.pm	Wed Mar 21 21:20:09 2001
@@ -29,6 +29,7 @@
   $rv  = $dbh->do($statement, \%attr, @bind_values);
 
   $ary_ref = $dbh->selectall_arrayref($statement);
+  $ary_ref = $dbh->selectall_hashref($statement);
   @row_ary = $dbh->selectrow_array($statement);
   $ary_ref = $dbh->selectcol_arrayref($statement);
 
@@ -275,8 +276,9 @@
 	prepare    	=> { U =>[2,3,'$statement [, \%attr]'] },
 	prepare_cached	=> { U =>[2,4,'$statement [, \%attr [, $allow_active ] ]'] },
 	selectrow_array	=> { U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
-	selectall_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
 	selectcol_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
+	selectall_hashref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
+	selectall_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
 	handler    	=> { U =>[2,2,'\&handler'] },
 	ping       	=> { U =>[1,1] },
 	disconnect 	=> { U =>[1,1] },
@@ -955,6 +957,16 @@
 	return $sth->fetchall_arrayref($slice);
     }
 
+    sub selectall_hashref {
+	my ($dbh, $stmt, $attr, @bind) = @_;
+	my $sth = (ref $stmt) ? $stmt
+			      : $dbh->prepare($stmt, $attr);
+	return unless $sth;
+	$sth->execute(@bind) || return;
+	my $slice = $attr->{dbi_fetchall_arrayref_attr} ||= {};
+	return $sth->fetchall_arrayref($slice);
+    }
+
     sub selectcol_arrayref {
 	my ($dbh, $stmt, $attr, @bind) = @_;
 	my $sth = (ref $stmt) ? $stmt
@@ -2236,6 +2248,24 @@
 L</RaiseError> is not set, then it will return with whatever data it
 has fetched thus far.
 
+=item C<selectall_hashref>
+
+  $ary_ref = $dbh->selectall_hashref($statement);
+  $ary_ref = $dbh->selectall_hashref($statement, \%attr);
+  $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);
+
+This utility method combines L</prepare>, L</execute> and
+L</fetchall_arrayref> into a single call. It returns a reference to an
+array containing a reference to an hash for each row of data fetched.
+
+The C<$statement> parameter can be a previously prepared statement handle,
+in which case the C<prepare> is skipped. This is recommended if the
+statement is going to be executed many times.
+
+If any method except C<fetch> fails, and L</RaiseError> is not set,
+C<selectall_arrayref> will return C<undef>.  If C<fetch> fails and
+L</RaiseError> is not set, then it will return with whatever data it
+has fetched thus far.
 
 =item C<selectcol_arrayref>
 

--------------D3C7021450705A0BA38BBC34--

0
mark6 (51)
3/22/2001 2:34:15 AM
perl.dbi.dev 1960 articles. 0 followers. Follow

1 Replies
535 Views

Similar Articles

[PageSpeed] 38

Thanks, but that's already in the next release. I hope to work on
actually making a release within the next week or so.

Tim.

On Wed, Mar 21, 2001 at 09:34:15PM -0500, Mark Stosberg wrote:
> 
> Hello!  First, thanks to you all for your work on DBI. I use it
> frequently and find it well put together as well as useful tool.
> Attached is a simple patch to the code AND pod of 1.14 which adds the
> function 'selectall_hashref', which is just like selectall_arrayref, but
> it defaults to returning an array of hashes instead of an array of
> arrays. This is extremely useful in some cases. For example, this sort
> of construct can be passed directly into an HTML::Template object. 
> 
> Thanks.
> 
>    -mark
> 
> http://mark.stosberg.com/
> --- DBI.pm.org	Wed Mar 21 20:22:02 2001
> +++ DBI.pm	Wed Mar 21 21:20:09 2001
> @@ -29,6 +29,7 @@
>    $rv  = $dbh->do($statement, \%attr, @bind_values);
>  
>    $ary_ref = $dbh->selectall_arrayref($statement);
> +  $ary_ref = $dbh->selectall_hashref($statement);
>    @row_ary = $dbh->selectrow_array($statement);
>    $ary_ref = $dbh->selectcol_arrayref($statement);
>  
> @@ -275,8 +276,9 @@
>  	prepare    	=> { U =>[2,3,'$statement [, \%attr]'] },
>  	prepare_cached	=> { U =>[2,4,'$statement [, \%attr [, $allow_active ] ]'] },
>  	selectrow_array	=> { U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
> -	selectall_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
>  	selectcol_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
> +	selectall_hashref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
> +	selectall_arrayref=>{U =>[2,0,'$statement [, \%attr [, @bind_params ] ]'] },
>  	handler    	=> { U =>[2,2,'\&handler'] },
>  	ping       	=> { U =>[1,1] },
>  	disconnect 	=> { U =>[1,1] },
> @@ -955,6 +957,16 @@
>  	return $sth->fetchall_arrayref($slice);
>      }
>  
> +    sub selectall_hashref {
> +	my ($dbh, $stmt, $attr, @bind) = @_;
> +	my $sth = (ref $stmt) ? $stmt
> +			      : $dbh->prepare($stmt, $attr);
> +	return unless $sth;
> +	$sth->execute(@bind) || return;
> +	my $slice = $attr->{dbi_fetchall_arrayref_attr} ||= {};
> +	return $sth->fetchall_arrayref($slice);
> +    }
> +
>      sub selectcol_arrayref {
>  	my ($dbh, $stmt, $attr, @bind) = @_;
>  	my $sth = (ref $stmt) ? $stmt
> @@ -2236,6 +2248,24 @@
>  L</RaiseError> is not set, then it will return with whatever data it
>  has fetched thus far.
>  
> +=item C<selectall_hashref>
> +
> +  $ary_ref = $dbh->selectall_hashref($statement);
> +  $ary_ref = $dbh->selectall_hashref($statement, \%attr);
> +  $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);
> +
> +This utility method combines L</prepare>, L</execute> and
> +L</fetchall_arrayref> into a single call. It returns a reference to an
> +array containing a reference to an hash for each row of data fetched.
> +
> +The C<$statement> parameter can be a previously prepared statement handle,
> +in which case the C<prepare> is skipped. This is recommended if the
> +statement is going to be executed many times.
> +
> +If any method except C<fetch> fails, and L</RaiseError> is not set,
> +C<selectall_arrayref> will return C<undef>.  If C<fetch> fails and
> +L</RaiseError> is not set, then it will return with whatever data it
> +has fetched thus far.
>  
>  =item C<selectcol_arrayref>
>  

0
Tim
3/22/2001 2:04:33 PM
Reply:

Similar Artilces:

Patches, patches, patches...
To forestall potential incidents of Warnock's Dillemma... I'm about to apply a whole heap 'o patches to Parrot. (With appropriate [APPLIED] responses, I hope) If, at the end of the day, I have *not* applied or commented on a patch you've sent, it means I've Officially Missed It, so give another try. Sorry 'bout this. Too much mail in the mailbox, and going back in time is tricky, as it can miss subsequent discussions on patches 'n stuff. -- Dan --------------------------------------"it's like th...

[dbi-dev] Proposed fetchall_hashref and selectall_hashref extensions
DBI Developers, > Speak before you patch I would like to extend DBI's fetchall_hashref and selectall_hashref methods to handle multiple keys. Being new to DBI, I miss Michael Peppler's Sybperl::Simple's useful HashOfHashOfHash method. The following are my proposed new syntaxes: $sth->fetchall_hashref(@key_field); $dbh->selectall_hashref($statement, \@key_field [, \%attr[, @bind_values]]); For example, let FOO and BAR form a composite key. Then: $sth = $dbh->prepare("SELECT FOO, BAR, NAME, BAZ FROM TABLE"); $sth->execute; $hash_r...

[Patch] DBI tests on Win32 dealing with /dev/null.
------=_NextPart_000_00B3_01C49592.36A1E540 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii" Tim, Attached is a minor patch to fix skipping tests on Win32. It would be nice if Cygwin users could validate this on their platform... Regards, Jeff Index: t/01basics.t =================================================================== --- t/01basics.t (revision 452) +++ t/01basics.t (working copy) @@ -147,10 +147,12 @@ ## testing dbi_debug cmp_ok($DBI::dbi_debug, '==', 0, "... DBI::dbi_debug's ...

[PATCH] File::Path::mkpath, /dev, /dev/dir and /dev/000000 changes
While fetching GD with CPAN.PM, I ran into some problems with File::Path::mkpath, in particular with rooted logicals: $ define/job/trans=conceal root_abcd_1 disk:[perl.cpan.build.id.blah.] mkpath('/root_abcd_1/lds') fails with a "invalid parameter" error when it tries to mkdir('/root_abcd_1'). There's a test in the code for a '000000' directory, but not for the lack of a directory....I suspect that the test was put in to prevent endless loops, prior to a modification of the File::Basename code. So here's a change that does the...

DBI-1.21 DBI.xs patch
--------------ms10979709E84C5FA689C39B1B Content-Type: multipart/mixed; boundary="------------EF8C4696EAE17FB10D560866" This is a multi-part message in MIME format. --------------EF8C4696EAE17FB10D560866 Content-Type: text/plain; charset=koi8-r Content-Transfer-Encoding: 7bit Dear Tim, I receive the error "Can't read $DBI::err, last handle unknown or destroyed during global destruction" many times (and for $DBI::errstr too). I'm not able to say what is the reason of these errors. Setting the trace levelel to 10 didn't help, and trace files w...

Patches, patches, patches... #2
OK, this is advance warning that I'm about to be reposting a bunch of patches of mine which appear to have succumbed to Warnock's Dilemma. The first three should be uncontroversial: there's a documentation fix-up for core.ops, and new tests for strings and stacks. The last one is rather larger, and consists of a greatly expanded set of tests for PerlString's, plus a bunch of bug-fixes. I'd be grateful if Jeff or Dan could take a look at this, as I have the feeling that a lot of the scalar PMCs are going to need similar fixes. Simon ...

[PATCH] DBI::TINYINT constant used instead of DBI::SQL_TINYINT
--------------040602020800070105040607 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit I think that DBD::File at line 392 has a mistake in DBI::SQL_TINYINT() constant name. Attached is a patch against DBI 1.51 (and DBD::File v0.34) that should solve this problem. The actual result of this problem is the following error message: Can't locate auto/DBI/TINYINT.al in @INC (@INC contains: ... .) at /opt/perl/lib/site_perl/5.8.6/x86_64-linux/DBD/File.pm line 384 I didn't understand clearly what triggers the error, may be...

[PATCH] DBI::TINYINT constant used instead of DBI::SQL_TINYINT
--------------080302070000000302070906 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit I think that DBD::File at line 392 has a mistake in DBI::SQL_TINYINT() constant name (`DBI::TINYINT'). Attached is a patch against DBI 1.51 (and DBD::File v0.34) that should solve this problem. The actual result of this problem is the following error message: Can't locate auto/DBI/TINYINT.al in @INC (@INC contains: ... .) at /opt/perl/lib/site_perl/5.8.6/x86_64-linux/DBD/File.pm line 384 I didn't understand clearly what trig...

[perl5-dbi/dbi] 49ca1e: Give Ville the credits for his patch
----==_mimepart_51beb4f9be5b3_6f961289dd41210e5 Date: Mon, 17 Jun 2013 00:04:25 -0700 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-ID: <51beb4f9bf058_6f961289dd412119e@hookshot-fe2-pe1-prd.aws.github.net.mail> Branch: refs/heads/master Home: https://github.com/perl5-dbi/dbi Commit: 49ca1e9b915d32710e37dac3e7aa82a65c5c622b https://github.com/perl5-dbi/dbi/commit/49ca1e9b915d32710e37dac3e7aa82a65c5c622b Author: H.Merijn Brand - Tux <h.m.brand@xs4all.nl> Date: 2013-06-17 (Mon, 17 Jun 2013) Cha...

A patch for DBI
------=_Part_3271_11957558.1139419104203 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline To whom it may concern, While doing some development recently, I got an error saying "bind_columns called with 3 refs when 2 needed. at C:/Perl/site/lib/DBI.pm line 1705.". I knew what the problem was, but not where in my code it was. The patch that I'm submitting changes the "die" on that line to a "Carp::croak", as it's truly a user error when that happens. Thanks, Ben ------=...

Patches for DBI v1.32
---559023410-851401618-1039913737=:6184 Content-Type: TEXT/PLAIN; charset=US-ASCII Dear Tim, Here's a new set of patches against the original DBI v1.32 for the files: Driver.xst dbd_xsh.h lib/DBI/DBD.pm The patches are gzipped. To apply them, use: gunzip DBI.patches.gz cd DBI-1.32 patch -p0 <../DBI.patches The DBD.pm file has undergone a whole lot more change -- it is, I think, in even better shape than the version shipped earlier. If you need them, I can redo the patches against that revised version of the file, but I'd rather no...

[PATCH DBI] RE: [BUG] DBI 1.20, bleadperl and 64 bit ints not mixing too well
Tim The attached patch corrects various pointer cast, printf format and "computed value not used" warning from DBI; which I found following up the following messages. I was using perl-5.7.2@11938, configured with -Duse64bitint. My solution to the setlinebuf warning was to delete all the PerlIO definitions, perhaps not the best solution. Robin > From perl5-porters-return-43379-rmb1=cise.npl.co.uk@perl.org Sat Sep 8 09:50:45 2001 > Mailing-List: contact perl5-porters-help@perl.org; run by ezmlm > Precedence: bulk > list-help: <mailto:perl5-porters-h...

[PATCH] Patch to patch for callbacks in core
I should have tested this better before submitting it. This patch is to Perl 11722 after applying my previous patch. Now callbacks will actually work. :-) - D <dmlloyd@tds.net> --- mg.c-old Tue Aug 21 13:26:52 2001 +++ perl-devel/mg.c Tue Aug 21 15:09:16 2001 @@ -1068,7 +1068,18 @@ void Perl_despatch_signals(pTHX) { - PL_event_pending = 0; + if (PL_callback_head != NULL) { + CALLBACK *cur, *next; + + for (cur = PL_callback_head; cur != NULL; cur = next) { + next = cur->next; + (*cur->callback)(cur->data); + ...

[PATCH] byteorder.dev
--=-zQfBppUPBXkoRH2CBE9g Content-Type: text/plain Content-Transfer-Encoding: 7bit The web conversion (www.parrotcode.org/docs) results in a blank page as this file isn't PODified. Patch remedies. (I forget whether attachments are verboten or preferred - I can't seem to find any included documentation on how to submit patches. I should probably add that to the list of things I won't get to.) -- Bryan C. Warnock bwarnock@(gtemail.net|raba.com) --=-zQfBppUPBXkoRH2CBE9g Content-Disposition: attachment; filename=byteorder.patch Content-Transfer-Encoding: quoted-p...

Web resources about - patch: selectall_hashref - perl.dbi.dev

Pugs - Wikipedia, the free encyclopedia
The Pugs project aims to bootstrap Perl 6 by implementing the full Perl 6 specification, as detailed in the Synopses . It is written in Haskell ...

Resources last updated: 12/16/2015 3:36:25 PM