Skip to content

Commit

Permalink
specify columns support for search_with_pager, lookup methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nihen committed Dec 20, 2011
1 parent cc9e4a4 commit 64b342a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Teng/Plugin/Lookup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sub lookup {

my $cond = join ' AND ', map {"$_ = ?"} @sorted_keys;
my $sql = sprintf('SELECT %s FROM %s WHERE %s %s',
join(',', @{$table->{columns}}),
join(',', @{$opt->{columns} || $table->{columns}}),
$table_name,
$cond,
$opt->{for_update} ? 'FOR UPDATE' : '',
Expand Down
2 changes: 1 addition & 1 deletion lib/Teng/Plugin/Pager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sub search_with_pager {

my ($sql, @binds) = $self->sql_builder->select(
$table_name,
$table->columns,
($opt->{columns} || $table->{columns}),
$where,
+{
%$opt,
Expand Down
2 changes: 1 addition & 1 deletion lib/Teng/Plugin/Pager/MySQLFoundRows.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sub search_with_pager {

my ($sql, @binds) = $self->sql_builder->select(
$table_name,
$table->columns,
($opt->{columns} || $table->{columns}),
$where,
+{
%$opt,
Expand Down
14 changes: 14 additions & 0 deletions t/001_basic/025_pager.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,19 @@ subtest 'last' => sub {
is $pager->prev_page, 10;
};

subtest 'simple_with_columns' => sub {
my ($rows, $pager) = $db->search_with_pager(mock_basic => {}, {columns => [qw/id/], rows => 3, page => 1});
is join(',', map { $_->id } @$rows), '1,2,3';
is_deeply $rows->[0]->get_columns, +{ id => 1 };
is_deeply $rows->[1]->get_columns, +{ id => 2 };
is_deeply $rows->[2]->get_columns, +{ id => 3 };
is $pager->entries_per_page(), 3;
is $pager->entries_on_this_page(), 3;
is $pager->current_page(), 1;
is $pager->next_page, 2, 'next_page';
ok $pager->has_next, 'has_next';
is $pager->prev_page, undef;
};

done_testing;

13 changes: 13 additions & 0 deletions t/001_basic/028_lookup.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,18 @@ subtest 'lookup method' => sub {
};
};

subtest 'lookup_with_columns' => sub {
$db_basic->insert('mock_basic', => +{
id => 2,
name => 'ruby',
});

my $row = $db_basic->lookup('mock_basic', +{id => 2}, { columns => [qw/id/]});
isa_ok $row, 'Mock::Basic::Row::MockBasic';
is_deeply $row->get_columns, +{
id => 2,
};
};

done_testing;

13 changes: 13 additions & 0 deletions xt/mysql/pager_mysql_found_rows.t
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ subtest 'last' => sub {
is $pager->previous_page, 10;
};

subtest 'simple_with_columns' => sub {
my ($rows, $pager) = $db->search_with_pager(mock_basic => {}, {columns => [qw/id/], rows => 3, page => 1});
is join(',', map { $_->id } @$rows), '1,2,3';
is_deeply $rows->[0]->get_columns, +{ id => 1 };
is_deeply $rows->[1]->get_columns, +{ id => 2 };
is_deeply $rows->[2]->get_columns, +{ id => 3 };
is $pager->total_entries(), 32;
is $pager->entries_per_page(), 3;
is $pager->current_page(), 1;
is $pager->next_page, 2, 'next_page';
is $pager->previous_page, undef;
};

done_testing;


0 comments on commit 64b342a

Please sign in to comment.