Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Re: DBIx::Simple

by Juerd (Abbot)
on Mar 28, 2002 at 13:51 UTC ( [id://154968]=note: print w/replies, xml ) Need Help??


in reply to Re: DBIx::Simple
in thread DBIx::Simple

# untested but realistic my $dbh = DBI->connect("...",{RaiseError => 1} ) or die "can't\n"; my $cust_sth = $dbh->prepare( qq{SELECT cust_ID, cust_name from customer}); $cust_sth->execute(); while (my $cust = $cust_sth->fetchrow_hashref()) { my $orders_sth = $dbh->prepare( qq{select * from orders where cust_ID = ? }); # do something smart with customer name $order_sth->execute($cust->{cust_ID}); while (my $order = $orders_sth->fetchrow_hashref()) { # do something even smarter with the orders data } }
With DBIx::Simple, I should either copy the result of the first query to an array, and suffer the copy delay, or create two objects (and bear in mind that in a real application I need to use more than that), thus having two connections and their relative overhead.

I didn't want to talk about statement handles, but I get the impression that you think DBIx::Simple can use only one. However, there are result objects, that internally are just objects holding a sth., with an alternative, consistent interface.

I don't think existing scripts should be re-written, but here's yours for demonstration purposes:

use DBIx::Simple; my $db = DBIx::Simple->connect("...",{RaiseError => 1} ) or die "can't +\n"; my $cust_result = $db->query('SELECT cust_ID, cust_name FROM customer' +); while (my $cust = $cust_result->hash) { my $orders_result = $db->query( 'SELECT * FROM orders WHERE cust_ID = ?', $cust->{cust_ID} ); while (my $order = $orders_result->hash) { # code } }
Or, when slurping into memory is not a problem, you can write beautiful code with DBIx::Simple:
use DBIx::Simple; my $db = DBIx::Simple->connect("...",{RaiseError => 1} ) or die "can't +\n"; for my $cust ($db->query('SELECT cust_ID, cust_name FROM customer')->h +ashes) { for my $order ($db->query('SELECT * FROM orders WHERE cust_ID = ?' +, $cust->{cust_ID}->hashes) { # code } }

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://154968]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found