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 58     58   421 use strict;
  58         153  
  58         1931  
16 58     58   351 use warnings;
  58         143  
  58         2264  
17             package MongoDB::Op::_FindAndDelete;
18              
19             # Encapsulate find_and_delete operation; atomically delete and return doc
20              
21 58     58   337 use version;
  58         134  
  58         399  
22             our $VERSION = 'v2.2.0';
23              
24 58     58   4471 use Moo;
  58         154  
  58         415  
25              
26 58     58   18671 use MongoDB::Error;
  58         190  
  58         6162  
27 58     58   440 use MongoDB::Op::_Command;
  58         153  
  58         1787  
28 58         511 use Types::Standard qw(
29             HashRef
30 58     58   389 );
  58         162  
31 58     58   31090 use boolean;
  58         156  
  58         579  
32              
33 58     58   3970 use namespace::clean;
  58         163  
  58         463  
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;