Skip to content

Commit

Permalink
Merge 1fae077 into 11ebb31
Browse files Browse the repository at this point in the history
  • Loading branch information
tsucchi committed May 12, 2015
2 parents 11ebb31 + 1fae077 commit ec88004
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Takuya Tsuchida <tsucchi@cpan.org>"
],
"dynamic_config" : 0,
"generated_by" : "Minilla/v2.1.1, CPAN::Meta::Converter version 2.141170",
"generated_by" : "Minilla/v2.3.0, CPAN::Meta::Converter version 2.141170",
"license" : [
"perl_5"
],
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Otogiri::Plugin::Count - Otogiri plugin to count rows in database.
my $count = $db->count('some_table'); # SELECT COUNT(*) FROM some_table

my $count2 = $db->count('some_table', 'column1', { group_id => 123 }); # SELECT COUNT(column1) WHERE group_id=123
my $count3 = $db->count('some_table', { group_id => 123 }); # same as above

# DESCRIPTION

Expand Down
8 changes: 5 additions & 3 deletions lib/Otogiri/Plugin/Count.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use Otogiri::Plugin;

our @EXPORT = qw(count);

# this code is taken from Teng::Plugin::Count
sub count {
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]]])');
if ( ref $column eq 'HASH' ) {
$opt = $where;
$where = $column;
$column = '*';
}

$column ||= '*';
Expand Down Expand Up @@ -44,6 +45,7 @@ Otogiri::Plugin::Count - Otogiri plugin to count rows in database.
my $count = $db->count('some_table'); # SELECT COUNT(*) FROM some_table
my $count2 = $db->count('some_table', 'column1', { group_id => 123 }); # SELECT COUNT(column1) WHERE group_id=123
my $count3 = $db->count('some_table', { group_id => 123 }); # same as above
=head1 DESCRIPTION
Expand Down
17 changes: 16 additions & 1 deletion t/01_sqlite.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,27 @@ subtest 'count', sub {
is( $count_with_cond, 2 );
};

subtest 'cond in 2nd argument', sub {
my $db = _setup();
$db->load_plugin('Count');
$db->fast_insert('person', { name => 'Sherlock Shellingford', age => 15 });
$db->fast_insert('person', { name => 'Nero Yuzurizaki', age => 15 });
$db->fast_insert('person', { name => 'Hercule Barton', age => 16 });
$db->fast_insert('person', { name => 'Cordelia Glauca', age => 17 });

my $count_with_cond= $db->count('person', { name => { like => '%e%' } }, { group_by => 'age', having => { age => 17 } });
is( $count_with_cond, 1);
};


done_testing;


sub _setup {
my $db = Otogiri->new( connect_info => ["dbi:SQLite:dbname=$dbfile", '', '', { RaiseError => 1, PrintError => 0 }] );
my $db = Otogiri->new(
connect_info => ["dbi:SQLite:dbname=$dbfile", '', '', { RaiseError => 1, PrintError => 0 }],
strict => 0,
);

my @sql_statements = split /\n\n/, <<EOSQL;
PRAGMA foreign_keys = ON;
Expand Down

0 comments on commit ec88004

Please sign in to comment.