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