Class::DBI::Sweet::Pie - aggregate function for Class::DBI::Sweet
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');
This module is for using an aggregate function easily by Class::DBI::Sweet.
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' );
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', );
# 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');
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'} );
ASAKURA Takuji <asakura.takuji+cpan@gmail.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Class::DBI::Sweet
Class::DBI::Plugin::AggregateFunction
To install Class::DBI::Sweet::Pie, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::DBI::Sweet::Pie
CPAN shell
perl -MCPAN -e shell install Class::DBI::Sweet::Pie
For more information on module installation, please visit the detailed CPAN module installation guide.