Teng::Plugin::CountはあるのになんでMinとMaxは無いんだろう?
よくわかってないのでほんとにこれでだいじょうぶなんだろうか・・・。
--- /home/ymko/perl5/lib/perl5/Teng/Plugin/Count.pm 2014-03-15 21:53:40.000000000 +0900
+++ /home/ymko/perl5/lib/perl5/Teng/Plugin/Min.pm 2014-04-13 03:37:45.648043227 +0900
@@ -1,20 +1,20 @@
-package Teng::Plugin::Count;
+package Teng::Plugin::Min;
use strict;
use warnings;
use utf8;
-our @EXPORT = qw/count/;
+our @EXPORT = qw/min/;
-sub count {
+sub min {
my ($self, $table, $column, $where, $opt) = @_;
if (ref $column eq 'HASH') {
- Carp::croak('Do not pass HashRef to second argument. Usage: $db->count($table[, $column[, $where[, $opt]]])');
+ Carp::croak('Do not pass HashRef to second argument. Usage: $db->min($table[, $column[, $where[, $opt]]])');
}
$column ||= '*';
- my ($sql, @binds) = $self->sql_builder->select($table, [\"COUNT($column)"], $where, $opt);
+ my ($sql, @binds) = $self->sql_builder->select($table, [\"MIN($column)"], $where, $opt);
my ($cnt) = $self->dbh->selectrow_array($sql, {}, @binds);
return $cnt;
Teng/Plugin/Min.pm
package Teng::Plugin::Min;
use strict;
use warnings;
use utf8;
our @EXPORT = qw/min/;
sub min {
my ($self, $table, $column, $where, $opt) = @_;
if (ref $column eq 'HASH') {
Carp::croak('Do not pass HashRef to second argument. Usage: $db->min($table[, $column[, $where[, $opt]]])');
}
$column ||= '*';
my ($sql, @binds) = $self->sql_builder->select($table, [\"MIN($column)"], $where, $opt);
my ($cnt) = $self->dbh->selectrow_array($sql, {}, @binds);
return $cnt;
}
1;
Teng/Plugin/Max.pm
package Teng::Plugin::Max;
use strict;
use warnings;
use utf8;
our @EXPORT = qw/max/;
sub max {
my ($self, $table, $column, $where, $opt) = @_;
if (ref $column eq 'HASH') {
Carp::croak('Do not pass HashRef to second argument. Usage: $db->max($table[, $column[, $where[, $opt]]])');
}
$column ||= '*';
my ($sql, @binds) = $self->sql_builder->select($table, [\"MAX($column)"], $where, $opt);
my ($cnt) = $self->dbh->selectrow_array($sql, {}, @binds);
return $cnt;
}
1;
test.pl
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
use Teng;
use Teng::Schema::Loader;
Teng->load_plugin('Count');
Teng->load_plugin('Min');
Teng->load_plugin('Max');
my $tbl = "tbl";
my %schema = (
id => "integer primary key",
num => "integer",
);
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","");
my $cmd = "create table $tbl (";
while (my ($key, $val) = each %schema) {
$cmd .= " $key $val,";
}
$cmd =~ s/,$//;
$cmd .= ")";
$dbh->do($cmd);
my $teng = Teng::Schema::Loader->load(
dbh => $dbh,
namespace => 'MyApp::DB',
);
foreach my $i (10..30) {
$teng->fast_insert($tbl => {num => $i});
};
print "count=" . $teng->count($tbl, '*') . "\n";
print " min=" . $teng->min($tbl, 'num') . "\n";
print " max=" . $teng->count($tbl, 'num') . "\n";
__END__
count=21
min=10
max=21