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   422 use strict;
  59         156  
  59         1963  
16 59     59   363 use warnings;
  59         147  
  59         2291  
17             package MongoDB::Op::_FindAndDelete;
18              
19             # Encapsulate find_and_delete operation; atomically delete and return doc
20              
21 59     59   334 use version;
  59         132  
  59         352  
22             our $VERSION = 'v2.2.1';
23              
24 59     59   4402 use Moo;
  59         154  
  59         411  
25              
26 59     59   18986 use MongoDB::Error;
  59         224  
  59         6431  
27 59     59   466 use MongoDB::Op::_Command;
  59         157  
  59         1832  
28 59         506 use Types::Standard qw(
29             HashRef
30 59     59   375 );
  59         190  
31 59     59   30259 use boolean;
  59         145  
  59         506  
32              
33 59     59   4305 use namespace::clean;
  59         168  
  59         400  
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;