line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Teng::Plugin::Lookup; |
2
|
1
|
|
|
1
|
|
603
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
26
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our @EXPORT = qw/lookup/; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub lookup { |
9
|
6
|
|
|
6
|
1
|
42
|
my ($self, $table_name, $where, $opt) = @_; |
10
|
|
|
|
|
|
|
|
11
|
6
|
|
|
|
|
26
|
my $table = $self->{schema}->get_table( $table_name ); |
12
|
6
|
50
|
|
|
|
16
|
Carp::croak("No such table $table_name") unless $table; |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
|
|
10
|
my (@keys, $values); |
15
|
6
|
100
|
|
|
|
20
|
if ( ref $where eq 'ARRAY' ) { |
16
|
2
|
|
|
|
|
9
|
my @w = @$where; |
17
|
2
|
|
|
|
|
11
|
while (my ($key, $val) = splice @w, 0, 2) { |
18
|
3
|
|
|
|
|
7
|
push @keys, $key; |
19
|
3
|
|
|
|
|
16
|
push @$values, $val; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
else { |
23
|
4
|
|
|
|
|
17
|
@keys = sort keys %$where; |
24
|
4
|
|
|
|
|
11
|
$values = [@$where{@keys}]; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
6
|
|
|
|
|
18
|
my $dbh = $self->dbh; |
28
|
6
|
|
|
|
|
20
|
my $columns = $self->_get_select_columns($table, $opt); |
29
|
6
|
|
|
|
|
13
|
my $cond = join ' AND ', map {$dbh->quote_identifier($_) . ' = ?'} @keys; |
|
8
|
|
|
|
|
77
|
|
30
|
|
|
|
|
|
|
my $sql = sprintf('SELECT %s FROM %s WHERE %s %s', |
31
|
17
|
100
|
|
|
|
53
|
join(',', map { ref $_ ? $$_ : $_ } @{$columns}), |
|
6
|
|
|
|
|
13
|
|
32
|
|
|
|
|
|
|
$dbh->quote_identifier($table_name), |
33
|
|
|
|
|
|
|
$cond, |
34
|
6
|
50
|
|
|
|
168
|
$opt->{for_update} ? 'FOR UPDATE' : '', |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
6
|
|
|
|
|
166
|
my $sth = $self->execute($sql, $values); |
38
|
6
|
|
|
|
|
139
|
my $row = $sth->fetchrow_hashref($self->{fields_case}); |
39
|
|
|
|
|
|
|
|
40
|
6
|
50
|
|
|
|
26
|
return unless $row; |
41
|
6
|
50
|
|
|
|
15
|
return $row if $self->{suppress_row_objects}; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$table->{row_class}->new( |
44
|
|
|
|
|
|
|
{ |
45
|
6
|
|
|
|
|
44
|
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
|
|
|
|
|
|
|
NOTE: Unlike Teng#single, this method returns a blank list in list context when no desired records are found. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=back |
88
|
|
|
|
|
|
|
|