Hatena::Groupasakura

浅倉卓司@blog風味? このページをアンテナに追加 RSSフィード

Error : RSSが取得できませんでした。

2006-03-27(Mon)

Class::DBI::Sweet::Pie

| 12:37 |  Class::DBI::Sweet::Pie - 浅倉卓司@blog風味? を含むブックマーク  Class::DBI::Sweet::Pie - 浅倉卓司@blog風味? のブックマークコメント

 気が向いたので集約関数を追加するClass::DBI::Sweet用のプラグインを作ってみました。

 Class::DBI::Plugin::AggregateFunctionとか作ってもいいのかもしれません。

  package MyData::CD;
  use base qw/Class::DBI::Sweet/;
  use Class::DBI::Sweet::Pie;
  __PACKAGE__->mk_aggregate_function('sum');
  __PACKAGE__->mk_aggregate_function( max => 'maximum');

  package main;
  # SELECT MAX(price) FROM __TABLE__
  my $max = MyData::CD->maximum( 'price' );

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

ソースコード

package Class::DBI::Sweet::Pie;
use strict;
use vars qw/$VERSION @EXPORT @EXPORT_OK/;
$VERSION = '0.01';

use Exporter::Lite;
@EXPORT = qw/mk_aggregate_function/;

sub mk_aggregate_function {
    my $class = shift;
    my ($aggregate_func, $alias) = @_;
    $alias ||= $aggregate_func;

    $class->set_sql( "Join_Retrieve_$aggregate_func" => <<__SQL__ );
  SELECT $aggregate_func( %s )
  FROM   %s
  WHERE  %s
__SQL__

    no strict 'refs';
    *{"$class\::$alias"} = sub {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $aggregate_column = shift;

	my ($criteria, $attributes) = $class->_search_args(@_);

	# make sure we take copy of $attribues since it can be reused
	my $sum_attr = { %{$attributes} };

	# no need for LIMIT/OFFSET and ORDER BY in AGGREGATE_FUNC()
	delete @{$sum_attr}{qw( rows offset order_by )};

	my ($sql_parts, $classes, $columns, $values) = $proto->_search( $criteria, $sum_attr );

	my $sql_method = 'sql_' . ($attributes->{sql_method} || 'Join_Retrieve') . "_$aggregate_func";

	my $sth = $class->$sql_method( "me.$aggregate_column", @{%$sql_parts}{qw/ from where /} );

	$class->_bind_param( $sth, $columns );
	return $sth->select_val(@$values);
    }
}

1;
__END__
=head1 NAME

Class::DBI::Sweet::Pie

=head1 SYNOPSYS

  package MyData::CD;
  use base qw/Class::DBI::Sweet/;
  use Class::DBI::Sweet::Pie;
  __PACKAGE__->mk_aggregate_function('sum');
  __PACKAGE__->mk_aggregate_function( max => 'maximum');

  package main;
  my $sum = MyData::CD->sum( 'price' ); 	# SELECT SUM(price) 
  my $max = MyData::CD->maximum( 'price' );	# SELECT MAX(price) 

=cut
トラックバック - http://asakura.g.hatena.ne.jp/asakura-t/20060327
2004 | 01 | 02 | 03 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2005 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 05 | 06 | 08 | 09 | 10 | 11 | 12 |
2007 | 02 | 03 | 04 | 05 | 06 | 07 | 10 | 11 | 12 |
2008 | 02 | 03 | 04 | 06 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2011 | 01 | 02 | 03 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2012 | 02 | 03 | 04 | 05 | 07 | 08 | 10 | 11 | 12 |
2013 | 01 | 05 | 07 |
2014 | 01 | 02 |
2016 | 01 |
2017 | 01 | 05 |