blib/lib/DBIx/Class/ResultSet/Void.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package DBIx::Class::ResultSet::Void;
2 our $VERSION = '0.06';
3  
4 # ABSTRACT: improve DBIx::Class::ResultSet with void context
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp::Clan qw/^DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use base qw(DBIx::Class::ResultSet);
11  
12 sub exists {
13 my ( $self, $query ) = @_;
14  
15 return $self->search( $query, { rows => 1, select => [1] } )->single;
16 }
17  
18 sub find_or_create {
19 my $self = shift;
20  
21 return $self->next::method(@_) if ( defined wantarray );
22  
23 my $attrs = ( @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {} );
24 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
25  
26 my $query = $self->___get_primary_or_unique_key( $hash, $attrs );
27 my $exists = $self->exists($query);
28 $self->create($hash) unless $exists;
29 }
30  
31 sub update_or_create {
32 my $self = shift;
33  
34 return $self->next::method(@_) if ( defined wantarray );
35  
36 my $attrs = ( @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {} );
37 my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
38  
39 my $query = $self->___get_primary_or_unique_key( $cond, $attrs );
40 my $exists = $self->exists($query);
41  
42 if ($exists) {
43  
44 # dirty hack, to remove WHERE cols from SET
45 my $query_array = ref $query eq 'ARRAY' ? $query : [$query];
46 foreach my $_query (@$query_array) {
47 foreach my $_key ( keys %$_query ) {
48 delete $cond->{$_key};
49 delete $cond->{$1} if $_key =~ /\w+\.(\w+)/; # $alias.$col
50 }
51 }
52 $self->search($query)->update($cond) if keys %$cond;
53 }
54 else {
55 $self->create($cond);
56 }
57 }
58  
59 # mostly copied from sub find
60 sub ___get_primary_or_unique_key {
61 my $self = shift;
62 my $attrs = ( @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {} );
63  
64 # Default to the primary key, but allow a specific key
65 my @cols =
66 exists $attrs->{key}
67 ? $self->result_source->unique_constraint_columns( $attrs->{key} )
68 : $self->result_source->primary_columns;
69 $self->throw_exception(
70 "Can't find unless a primary key is defined or unique constraint is specified"
71 ) unless @cols;
72  
73 # Parse out a hashref from input
74 my $input_query;
75 if ( ref $_[0] eq 'HASH' ) {
76 $input_query = { %{ $_[0] } };
77 }
78 elsif ( @_ == @cols ) {
79 $input_query = {};
80 @{$input_query}{@cols} = @_;
81 }
82 else {
83  
84 # Compatibility: Allow e.g. find(id => $value)
85 carp "Find by key => value deprecated; please use a hashref instead";
86 $input_query = {@_};
87 }
88  
89 my ( %related, $info );
90  
91 KEY: foreach my $key ( keys %$input_query ) {
92 if ( ref( $input_query->{$key} )
93 && ( $info = $self->result_source->relationship_info($key) ) )
94 {
95 my $val = delete $input_query->{$key};
96 next KEY if ( ref($val) eq 'ARRAY' ); # has_many for multi_create
97 my $rel_q =
98 $self->result_source->resolve_condition( $info->{cond}, $val,
99 $key );
100 die "Can't handle OR join condition in find"
101 if ref($rel_q) eq 'ARRAY';
102 @related{ keys %$rel_q } = values %$rel_q;
103 }
104 }
105 if ( my @keys = keys %related ) {
106 @{$input_query}{@keys} = values %related;
107 }
108  
109 # Build the final query: Default to the disjunction of the unique queries,
110 # but allow the input query in case the ResultSet defines the query or the
111 # user is abusing find
112 my $alias =
113 exists $attrs->{alias} ? $attrs->{alias} : $self->{attrs}{alias};
114 my $query;
115 if ( exists $attrs->{key} ) {
116 my @unique_cols =
117 $self->result_source->unique_constraint_columns( $attrs->{key} );
118 my $unique_query =
119 $self->_build_unique_query( $input_query, \@unique_cols );
120 $query = $self->_add_alias( $unique_query, $alias );
121 }
122 else {
123 my @unique_queries = $self->_unique_queries( $input_query, $attrs );
124 $query =
125 @unique_queries
126 ? [ map { $self->_add_alias( $_, $alias ) } @unique_queries ]
127 : $self->_add_alias( $input_query, $alias );
128 }
129  
130 return $query;
131 }
132  
133 1;
134 __END__
↑ Summary