[Milan-pm] New note on page5notebook

Oha oha at oha.it
Thu Oct 7 03:08:45 PDT 2010


On 10/07/2010 11:27 AM, marcos rebelo wrote:
> 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 ($@) {
>     ...
> }
>    

it's about 6 months i'm working on something like this, here a quick 
example:

         # obtain a single row from db
         my %row = select_uniq 'select * from foo where id = ?', $id;

         # when needed you may open a transaction
         tx_new
         {
                 # if not differently specified, the default conenction 
will be used
                 my %codes = select_map { $_{id} => $_{code} } 'SELECT * 
FROM codes';

                 if($code{0}) {
                         tx_db 'master'; # going to use a specific 
connection

                         my $new = select_uniq 'SELECT max(id)+1 FROM 
codes';
                         tx_do 'UPDATE codes SET id = ? WHERE id = 0', $new;
                 }
                 # now the connection is again the default, cauz tx_db 
got out of scope

                 # transaction may be nested, connection will be kept 
separated
                 tx_new
                 {
                         # if more then 1 rows is fetched, the following 
will confess
                         my %foo = select_uniq 'SELECT * FROM codes 
WHERE id = ?', 'foo';
                 }
         } # connection will be committed and released, or rollbacked if 
an error is passing thru

It is somewhat stable and i will probably put it on CPAN, but if you are 
interested i can share the code

I'll gladly ear your thoughts about it.

Oha


More information about the Milan-pm mailing list