[Dbix-class] counter handling

Jason Kohles email at jasonkohles.com
Mon Feb 26 21:14:17 GMT 2007


On Feb 26, 2007, at 3:46 PM, Jim Spath wrote:
> I am somewhat new to DBIx::Class and was wondering if the following  
> method is the best way to handle counter values.  I've tested it,  
> and it works, but wasn't sure if there was a better way.
>
> ==================================================
> (all model, table, and column names are made up)
> (I'm using DBIx::Class under Catalyst, hence the $c)
> ==================================================
>

Seems like you could save yourself a lot of unnecessary logic...

$c->model( 'SomeDB::Counter' )->find_or_create({
     counter_name => $counter_name
})->update({
     counter => \'counter+1';
});

> my $rows = $c->model('SomeDB::Counter')->search({
>   counter_name => $counter_name,
> })->update({
>   counter => \'counter + 1',
> });
>
> if ($rows eq '0E0') {
>
>   eval {
>     $c->model('SomeDB::Counter')->create({
>       counter_name => $counter_name,
>       counter      => 1,
>     });
>   };
>
>   if ($@) {
>     if ($@ =~ /duplicate entry/i) {
>
>       $rows = $c->model('SomeDB::Counter')->search({
>         counter_name => $counter_name,
>       })->update({
>         counter => \'counter + 1',
>       });
>
>       if ($rows eq '0E0') {
>         # couldn't update or insert counter
>       }
>
>     } else {
>       # unknown error on attempted insert
>     }
>   }
> }
>
>
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> Searchable Archive: http://www.mail-archive.com/dbix- 
> class at lists.rawmode.org/
>

-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20070226/75b7b186/attachment.htm


More information about the Dbix-class mailing list