| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Model::Mixin::FindOrCreate; |
|
2
|
1
|
|
|
1
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
25
|
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp (); |
|
|
1
|
|
|
|
|
16
|
|
|
|
1
|
|
|
|
|
204
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub register_method { |
|
8
|
|
|
|
|
|
|
+{ |
|
9
|
1
|
|
|
1
|
0
|
4
|
find_or_create => \&find_or_create, |
|
10
|
|
|
|
|
|
|
}; |
|
11
|
|
|
|
|
|
|
} |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub find_or_create { |
|
14
|
17
|
|
|
17
|
0
|
2122
|
my($self, $model, $key, $columns) = @_; |
|
15
|
17
|
|
|
|
|
49
|
my $row; |
|
16
|
17
|
100
|
|
|
|
89
|
if (ref($key) eq 'HASH') { |
|
17
|
|
|
|
|
|
|
# use on unique index |
|
18
|
9
|
|
|
|
|
19
|
my($index, $value) = %{ $key }; |
|
|
9
|
|
|
|
|
36
|
|
|
19
|
9
|
50
|
|
|
|
33
|
Carp::corak('index name is required') unless $index; |
|
20
|
|
|
|
|
|
|
|
|
21
|
9
|
|
|
|
|
51
|
my $schema = $self->get_schema($model); |
|
22
|
9
|
100
|
|
|
|
44
|
Carp::croak("'$index' is not unique index") unless $schema->unique->{$index}; |
|
23
|
|
|
|
|
|
|
|
|
24
|
8
|
|
|
|
|
56
|
($row) = $self->get($model, { index => { $index => $value } }); |
|
25
|
8
|
100
|
|
|
|
50
|
return $row if $row; |
|
26
|
4
|
|
|
|
|
18
|
$self->set( $model => $columns ); |
|
27
|
|
|
|
|
|
|
} else { |
|
28
|
|
|
|
|
|
|
# primary key |
|
29
|
|
|
|
|
|
|
|
|
30
|
8
|
|
|
|
|
71
|
$row = $self->lookup( $model => $key ); |
|
31
|
8
|
100
|
|
|
|
35
|
return $row if $row; |
|
32
|
4
|
|
|
|
|
33
|
$self->set( $model => $key => $columns ); |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
1; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 NAME |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Data::Model::Mixin::FindOrCreate - add find_or_create method |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
use Data::Model::Mixin modules => ['FindOrCreate']; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$model->find_or_create( |
|
48
|
|
|
|
|
|
|
tablename => key => { |
|
49
|
|
|
|
|
|
|
field1 => 'value', |
|
50
|
|
|
|
|
|
|
field2 => 'value', |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$model->find_or_create( |
|
55
|
|
|
|
|
|
|
tablename => [qw/ key1 key2 /] => { |
|
56
|
|
|
|
|
|
|
field1 => 'value', |
|
57
|
|
|
|
|
|
|
field2 => 'value', |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
); |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# using unique index, but not use normal index |
|
62
|
|
|
|
|
|
|
$model->find_or_create( |
|
63
|
|
|
|
|
|
|
tablename => { unique_idx => 'key' } => { |
|
64
|
|
|
|
|
|
|
field1 => 'value', |
|
65
|
|
|
|
|
|
|
field2 => 'value', |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
); |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$model->find_or_create( |
|
70
|
|
|
|
|
|
|
tablename => { unique_idx => [qw/ key1 key2 /] } => { |
|
71
|
|
|
|
|
|
|
field1 => 'value', |
|
72
|
|
|
|
|
|
|
field2 => 'value', |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
); |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
L |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 AUTHOR |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Kazuhiro Osawa Eyappo shibuya plE |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 LICENSE |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
87
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
|
90
|
|
|
|
|
|
|
|