An Introduction to Class::DBI - First Example 18

Example - And the winner is...

Using Class::DBI

  • Setup

        package Nomination;
        use base 'Class::DBI';
    
        Nomination->set_db('Main', 'dbi:pg', 'dbi_user', 'dbi_password');
        Nomination->table('nomination');
        Nomination->columns('id','type', 'year', 'film_id', 'credit_id', 'takes_the_oscar');
        Nomination->columns(Primary => 'id');
    
        package Voter;
        use base 'Class::DBI';
    
        Voter->set_db('Main', 'dbi:pg', 'dbi_user', 'dbi_password');
        Voter->table('vote');
        Voter->columns('voter_id', 'nomination_id');
        Voter->columns('Primary' => 'id');
    
  • The program

            my $winner    = 0;
            my $max_votes = 0;
    
            foreach my $nominee (Nomination->search(year => 1965, type => 'Best Editing')) {
    
                my $votes = 0;
    
                foreach my $vote (Voter->search(type => 'Best Editing', nomination_id => $nominee) {
                    $votes++;
                );
    
                if ($votes > $max_votes) {
                    $winner    = $nominee;
                    $max_votes = $votes;
                }
    
            }
            if ($winner) {
                $winner->takes_the_oscar(1);
            }
    

 

YAPC::Canada << Previous | Index | Next >>
Copyright © 2003 Michael Graham