[Milan-pm] New note on page5notebook

marcos rebelo oleber at gmail.com
Thu Oct 7 02:27:34 PDT 2010


the Idea is to do fast work, FAST. If I just need one exception, DBI
may do that to you.

I seen this pattern to often

eval {
   my $dbh = DBI->connect($dsn, $user, $password) or die $DBI::errstr;
   my $sth = $dbh->prepare("SELECT * FROM document WHERE id = ?") or
die $DBI::errstr;
   $sth->execute($id) // die $DBI::errstr;
   my $hash = $sth->fetchrow_hashref;
   die $DBI::errstr if $DBI::errstr;
   ...
};
if ($@) {
   ...
}

I prefer something like

eval {
   my $dbh = DBI->connect($dsn, $user, $password, {'RaiseError' => 1});
   my $sth = $dbh->prepare("SELECT * FROM document WHERE id = ?");
   my $rs = $sth->execute($id);
   my $hash = $sth->fetchrow_hashref;
   ...
};
if ($@) {
   ...
}

but for such a simple query, I would prefer

eval {
   my $ss = SmartSelect->new($dsn, $user, $password);
   my $hash = $ss->select_document_by_id($id)->[0];
   ...
};
if ($@) {
   ...
}

For having this in a production environment, I would have to develop
SmartSelect a little. Caching, more expressive queries, ... I didn't
found this in CPAN, I didn't look for it so much, but I would use it.

Like Einstein told: “Any fool can make things bigger, more complex,
and more violent. It takes a touch of genius-and a lot of courage-to
move in the opposite direction.”

Best Regards
Marcos Rebelo

2010/10/7 Gianluca Casati <casati_gianluca at yahoo.it>:
>
> I would use more $DBI::errstr after a prepare, an execute or a connect.
> Something like
>
>
> $dbh = DBI->connect( $NZ_SOURCE , $NZ_USER , $NZ_PASSWORD ) or die
> $log->abort(
> $connect_error_message . $DBI::errstr );
>
> other than that is a very comfortable approach, even if I prefere to keep
> queries in separate .sql files under a sql directory so other people that
> don't know Perl can edit them ( after that they accept the '?' special
> character :)
>
> Bye,
>
> see you the next meeting
>
> ________________________________
> Da: marcos rebelo <oleber at gmail.com>
> A: milan-pm at pm.org; nl-pm at amsterdam.pm.org; perl-recipes at googlegroups.com;
> perl at lisbon.pm.org; Perl Begginers <beginners at perl.org>
> Inviato: Mar 5 ottobre 2010, 14:11:01
> Oggetto: [Milan-pm] New note on page5notebook
>
> Hi all
>
> I did one more note at:
>
> http://perl5notebook.oleber.com/objects/smart-selects-with-dynamic-response-to-undefiend-method-calls
>
> One example of the use of AUTOLOAD, to do some SQL dinamically.
>
> Comments are well come
>
> Best Regards
> Marcos Rebelo
>
> --
> Marcos Rebelo
> http://oleber.freehostia.com
> Milan Perl Mongers leader http://milan.pm.org
> Webmaster of http://perl5notebook.oleber.com
> _______________________________________________
> Milan-pm mailing list
> Milan-pm at pm.org
> http://mail.pm.org/mailman/listinfo/milan-pm
>
>
> _______________________________________________
> Milan-pm mailing list
> Milan-pm at pm.org
> http://mail.pm.org/mailman/listinfo/milan-pm
>
>



-- 
Marcos Rebelo
http://oleber.freehostia.com
Milan Perl Mongers leader http://milan.pm.org
Webmaster of http://perl5notebook.oleber.com


More information about the Milan-pm mailing list