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