Teng/Plugin/Lookup.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 54 CC 7 SCAR 36.8
CC
7
Cov
0%
lookup
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 39.8
1 package Teng::Plugin::Lookup;
2 use strict;
3 use warnings;
4 use utf8;
5  
6 our @EXPORT = qw/lookup/;
7  
8 sub lookup {
9 my ($self, $table_name, $where, $opt) = @_;
10  
11 my $table = $self->{schema}->get_table( $table_name );
12 Carp::croak("No such table $table_name") unless $table;
13  
14 my (@keys, $values);
15 if ( ref $where eq 'ARRAY' ) {
16 my @w = @$where;
17 while (my ($key, $val) = splice @w, 0, 2) {
18 push @keys, $key;
19 push @$values, $val;
20 }
21 }
22 else {
23 @keys = sort keys %$where;
24 $values = [@$where{@keys}];
25 }
26  
27 my $dbh = $self->dbh;
28 my $columns = $self->_get_select_columns($table, $opt);
29 my $cond = join ' AND ', map {$dbh->quote_identifier($_) . ' = ?'} @keys;
30 my $sql = sprintf('SELECT %s FROM %s WHERE %s %s',
31 join(',', map { ref $_ ? $$_ : $_ } @{$columns}),
32 $table_name,
33 $cond,
34 $opt->{for_update} ? 'FOR UPDATE' : '',
35 );
36  
37 my $sth = $self->_execute($sql, $values);
38 my $row = $sth->fetchrow_hashref($self->{fields_case});
39  
40 return unless $row;
41 return $row if $self->{suppress_row_objects};
42  
43 $table->{row_class}->new(
44 {
45 sql => $sql,
46 row_data => $row,
47 teng => $self,
48 table => $table,
49 table_name => $table_name,
50 }
51 );
52 }
53  
54 1;
55 __END__
56  
57 =head1 NAME
58  
59 Teng::Plugin::Lookup - lookup single row.
60  
61 =head1 NAME
62  
63 package MyDB;
64 use parent qw/Teng/;
65 __PACKAGE__->load_plugin('Lookup');
66  
67 package main;
68 my $db = MyDB->new(...);
69 $db->lookup('user' => +{id => 1}); # => get single row
70  
71 =head1 DESCRIPTION
72  
73 This plugin provides fast lookup row .
74  
75 =head1 METHODS
76  
77 =over 4
78  
79 =item $row = $db->lookup($table_name, \%search_condition, [\%attr]);
80  
81 lookup single row records.
82  
83 Teng#single is heavy.
84  
85 =back
86