1
1

More than 5 years have passed since last update.

Teng::Plugin::Minを(コピペしてみた)書いてみた

Last updated at Posted at 2014-04-12

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
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1