File Coverage

blib/lib/MongoDB/Op/_FindAndDelete.pm
Criterion Covered Total %
statement 27 45 60.0
branch 0 12 0.0
condition 0 5 0.0
subroutine 9 10 90.0
pod 0 1 0.0
total 36 73 49.3


line stmt bran cond sub pod time code
1             # Copyright 2015 - present MongoDB, Inc.
2             #
3             # Licensed under the Apache License, Version 2.0 (the "License");
4             # you may not use this file except in compliance with the License.
5             # You may obtain a copy of the License at
6             #
7             # http://www.apache.org/licenses/LICENSE-2.0
8             #
9             # Unless required by applicable law or agreed to in writing, software
10             # distributed under the License is distributed on an "AS IS" BASIS,
11             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12             # See the License for the specific language governing permissions and
13             # limitations under the License.
14              
15 59     59   375 use strict;
  59         139  
  59         1728  
16 59     59   286 use warnings;
  59         122  
  59         2127  
17             package MongoDB::Op::_FindAndDelete;
18              
19             # Encapsulate find_and_delete operation; atomically delete and return doc
20              
21 59     59   295 use version;
  59         122  
  59         346  
22             our $VERSION = 'v2.2.2';
23              
24 59     59   4118 use Moo;
  59         126  
  59         323  
25              
26 59     59   16778 use MongoDB::Error;
  59         163  
  59         5748  
27 59     59   385 use MongoDB::Op::_Command;
  59         131  
  59         1499  
28 59         588 use Types::Standard qw(
29             HashRef
30 59     59   347 );
  59         130  
31 59     59   29300 use boolean;
  59         123  
  59         491  
32              
33 59     59   3805 use namespace::clean;
  59         145  
  59         367  
34              
35             has filter => (
36             is => 'ro',
37             required => 1,
38             isa => HashRef,
39             );
40              
41             has options => (
42             is => 'ro',
43             required => 1,
44             isa => HashRef,
45             );
46              
47             with $_ for qw(
48             MongoDB::Role::_PrivateConstructor
49             MongoDB::Role::_CollectionOp
50             MongoDB::Role::_WriteOp
51             );
52              
53             sub execute {
54 0     0 0   my ( $self, $link, $topology ) = @_;
55              
56 0 0 0       if ( defined $self->options->{collation} and !$link->supports_collation ) {
57 0           MongoDB::UsageError->throw(
58             "MongoDB host '" . $link->address . "' doesn't support collation" );
59             }
60              
61             my $command = [
62             findAndModify => $self->coll_name,
63             query => $self->filter,
64             remove => true,
65             ($link->supports_find_modify_write_concern ?
66 0           (@{ $self->write_concern->as_args })
67             : () ),
68 0 0         %{ $self->options },
  0            
69             ];
70              
71 0           my $op = MongoDB::Op::_Command->_new(
72             db_name => $self->db_name,
73             query => $command,
74             query_flags => {},
75             bson_codec => $self->bson_codec,
76             session => $self->session,
77             retryable_write => $self->retryable_write,
78             monitoring_callback => $self->monitoring_callback,
79             );
80              
81             # XXX more special error handling that will be a problem for
82             # command monitoring
83 0           my $result;
84             eval {
85 0           $result = $op->execute( $link, $topology );
86 0           $result = $result->{output};
87 0           1;
88 0 0         } or do {
89 0   0       my $error = $@ || "Unknown error";
90 0 0         die $error unless $error eq 'No matching object found';
91             };
92              
93             # findAndModify returns ok:1 even for write concern errors, so
94             # we must check and throw explicitly
95 0 0         if ( $result->{writeConcernError} ) {
96             MongoDB::WriteConcernError->throw(
97             message => $result->{writeConcernError}{errmsg},
98 0           result => $result,
99             code => WRITE_CONCERN_ERROR,
100             );
101             }
102              
103 0 0         return $result->{value} if $result;
104 0           return;
105             }
106              
107             1;