Skip to content

Commit

Permalink
add lookup plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekokak committed Nov 14, 2011
1 parent e180157 commit 10bc2f4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lib/Teng/Iterator.pm
lib/Teng/Plugin/BulkInsert.pm
lib/Teng/Plugin/Count.pm
lib/Teng/Plugin/FindOrCreate.pm
lib/Teng/Plugin/Lookup.pm
lib/Teng/Plugin/Pager.pm
lib/Teng/Plugin/Pager/MySQLFoundRows.pm
lib/Teng/Plugin/Replace.pm
Expand Down Expand Up @@ -44,6 +45,8 @@ t/001_basic/023_bulk_insert.t
t/001_basic/024_bind_column.t
t/001_basic/025_pager.t
t/001_basic/026_fork.t
t/001_basic/027_fork_self_reconnect.t
t/001_basic/028_lookup.t
t/002_common/000_new.t
t/002_common/001_insert.t
t/002_common/002_update.t
Expand Down Expand Up @@ -71,6 +74,7 @@ t/999_regression/guess_table_name.t
t/999_regression/inflate_bug.t
t/999_regression/no_schema.t
t/999_regression/prepare_error_handling.t
t/999_regression/reconnect.t
t/lib/Mock/Basic.pm
t/lib/Mock/Basic/Schema.pm
t/lib/Mock/BasicBindColumn.pm
Expand Down
71 changes: 71 additions & 0 deletions lib/Teng/Plugin/Lookup.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package Teng::Plugin::Lookup;
use strict;
use warnings;
use utf8;

our @EXPORT = qw/lookup/;

sub lookup {
my ($self, $table_name, $where, $opt) = @_;

my $table = $self->{schema}->get_table( $table_name );
Carp::croak("No such table $table_name") unless $table;

my $cond = join ' AND', map {"$_ = ?"} keys %$where;
my $sql = sprintf('SELECT %s FROM %s WHERE %s %s',
join(',', @{$table->{columns}}),
$table_name,
$cond,
$opt->{for_update} ? 'FOR UPDATE' : '',
);

my $sth = $self->_execute($sql, [values %$where]);
my $row = $sth->fetchrow_hashref('NAME_lc');

return unless $row;
return $row if $self->{suppress_row_objects};

$table->{row_class}->new(
{
sql => $sql,
row_data => $row,
teng => $self,
table => $table,
table_name => $table_name,
}
);
}

1;
__END__
=head1 NAME
Teng::Plugin::Lookup - lookup single row.
=head1 NAME
package MyDB;
use parent qw/Teng/;
__PACKAGE__->load_plugin('Lookup');
package main;
my $db = MyDB->new(...);
$db->lookup('user' => +{id => 1}); # => get single row
=head1 DESCRIPTION
This plugin provides fast lookup row .
=head1 METHODS
=over 4
=item $row = $db->lookup($table_name, \%search_condition, [\%attr]);
lookup single row records.
Teng#single is heavy.
=back
26 changes: 26 additions & 0 deletions t/001_basic/028_lookup.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use t::Utils;
use Mock::Basic;
use Test::More;

my $dbh = t::Utils->setup_dbh;
my $db_basic = Mock::Basic->new({dbh => $dbh});
$db_basic->setup_test_db;
Mock::Basic->load_plugin('Lookup');

subtest 'bulk_insert method' => sub {
$db_basic->insert('mock_basic', => +{
id => 1,
name => 'perl',
});

my $row = $db_basic->lookup('mock_basic', +{id => 1});
isa_ok $row, 'Mock::Basic::Row::MockBasic';
is_deeply $row->get_columns, +{
id => 1,
name => 'perl',
delete_fg => 0,
};
};

done_testing;

0 comments on commit 10bc2f4

Please sign in to comment.