The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::DBI::Sweet::Pie - aggregate function for Class::DBI::Sweet

SYNOPSYS

  package MyData::CD;
  use base qw/Class::DBI::Sweet/;
  __PACKAGE__->has_a( artist => 'MyData::Artist' );
  use Class::DBI::Sweet::Pie;
  __PACKAGE__->mk_aggregate_function('sum');
  __PACKAGE__->mk_aggregate_function( max => 'maximum');
  
  package MyData::Artist;
  use base qw/Class::DBI::Sweet/;
  __PACKAGE__->has_many( cds => 'MyData::CD' );
  use Class::DBI::Sweet::Pie;
  __PACKAGE__->mk_aggregate_function('min');
  __PACKAGE__->mk_aggregate_function( max => 'maximum');
  
  package main;
  
  $max_price = MyData::CD->maximum( 'price' );
  
  $total_price = MyData::CD->sum( 'price',
        { 'artist.name' => 'foo', }
  );

  $artist = MyData::Artist->search( name => 'foo' );
  $min_price = $artist->min('cds.price');

DESCRIPTION

This module is for using an aggregate function easily by Class::DBI::Sweet.

HOW TO USE

Make Metod of Aggregate Function

The aggregate function is added by using the mk_aggregate_function method.

The 1st argument is an aggregate function used by SQL.

The 2nd argument is a method name. When it is omitted, the aggregate function becomes a method name.

  __PACKAGE__->mk_aggregate_function( 'max' );

or

  __PACKAGE__->mk_aggregate_function( 'max' => 'maximum' );

Use Metod of Aggregate Function

The 1st argument of the aggregate function method is the target column name.

  $max_price = MyData::CD->maximum( 'price' );

It is the same as the search method of Class::DBI::Sweet after the 2nd argument.

  # SELECT SUM(price) FROM __TABLE__ WHERE artist = 'foo'
  $total_price = MyData::CD->sum( 'price',
        'artist' => 'foo',
  );

or

  # SELECT SUM(price) FROM __TABLE__ WHERE price >= 1000
  $total_price = MyData::CD->sum( 'price',
      {
        'price' => {'>=', 1000},
      }
  );


  $max_price = MyData::Artist->maximum( 'cds.price' );


  $artist = MyData::Artist->search( name => 'foo' );
  $min_price = $artist->min('cds.price');

search_with_*

  my @artists = MyData::Artist->search( $criteria );
  foreach my $artist (@artists) {
    print $artist->name, "\t", $artist->maximum('cds.price'), "\n";
  }

  my @artists = MyData::Artist->search_with_maximum( 'cds.price', $criteria );
  foreach my $artist (@artists) {
    print $artist->name, "\t", $artist->maximum, "\n";
  }

  my @artists = MyData::Artist->search_with_maximum( 'cds.price',
        $criteria,
        {order_by => 'maximum DESC'}
  );

AUTHOR

ASAKURA Takuji <asakura.takuji+cpan@gmail.com>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Class::DBI::Sweet

Class::DBI::Plugin::AggregateFunction