line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Teng::Plugin::FindOrCreate; |
2
|
2
|
|
|
2
|
|
1362
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
55
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
52
|
|
4
|
2
|
|
|
2
|
|
8
|
use utf8; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our @EXPORT = qw/find_or_create/; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub find_or_create { |
9
|
5
|
|
|
5
|
1
|
3797
|
my ($self, $table, $args) = @_; |
10
|
5
|
|
|
|
|
26
|
my $row = $self->single($table, $args); |
11
|
5
|
100
|
|
|
|
23
|
return $row if $row; |
12
|
3
|
|
|
|
|
24
|
$self->insert($table, $args)->refetch; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
1; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
__END__ |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 NAME |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Teng::Plugin::FindOrCreate - provide find_or_create method for your Teng class. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 NAME |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
package MyDB; |
26
|
|
|
|
|
|
|
use parent qw/Teng/; |
27
|
|
|
|
|
|
|
__PACKAGE__->load_plugin('FindOrCreate'); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
package main; |
30
|
|
|
|
|
|
|
my $db = MyDB->new(...); |
31
|
|
|
|
|
|
|
my $row = $db->find_or_create('user',{name => 'lestrrat'}); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This plugin provides find_or_create method. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 METHODS |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=over 4 |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=item $row = $db->find_or_create($table[, \%args]]) |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
I<$table> table name for counting |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
I<\%args> : HashRef for row data. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=back |