File Coverage

blib/lib/MongoDB/Op/_Delete.pm
Criterion Covered Total %
statement 31 42 73.8
branch 0 20 0.0
condition 0 2 0.0
subroutine 11 13 84.6
pod 0 1 0.0
total 42 78 53.8


line stmt bran cond sub pod time code
1             # Copyright 2014 - 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   416 use strict;
  59         155  
  59         1876  
16 59     59   347 use warnings;
  59         140  
  59         2131  
17             package MongoDB::Op::_Delete;
18              
19             # Encapsulate a delete operation; returns a MongoDB::DeleteResult
20              
21 59     59   349 use version;
  59         142  
  59         351  
22             our $VERSION = 'v2.2.1';
23              
24 59     59   4451 use Moo;
  59         169  
  59         346  
25              
26 59     59   42184 use MongoDB::DeleteResult;
  59         188  
  59         1980  
27 59     59   436 use MongoDB::_Protocol;
  59         143  
  59         1598  
28 59         305 use MongoDB::_Types qw(
29             Boolish
30             Document
31 59     59   351 );
  59         138  
32 59         422 use Types::Standard qw(
33             Maybe
34 59     59   37724 );
  59         147  
35              
36 59     59   41497 use namespace::clean;
  59         141  
  59         302  
37              
38             has filter => (
39             is => 'ro',
40             required => 1,
41             isa => Document,
42             );
43              
44             has just_one => (
45             is => 'ro',
46             required => 1,
47             isa => Boolish,
48             );
49              
50             has collation => (
51             is => 'ro',
52             isa => Maybe( [Document] ),
53             );
54              
55             with $_ for qw(
56             MongoDB::Role::_PrivateConstructor
57             MongoDB::Role::_CollectionOp
58             MongoDB::Role::_SingleBatchDocWrite
59             );
60              
61             sub execute {
62 0     0 0   my ( $self, $link ) = @_;
63              
64 0 0         if ( defined $self->collation ) {
65 0 0         MongoDB::UsageError->throw(
66             "MongoDB host '" . $link->address . "' doesn't support collation" )
67             if !$link->supports_collation;
68 0 0         MongoDB::UsageError->throw(
69             "Unacknowledged deletes that specify a collation are not allowed")
70             if ! $self->_should_use_acknowledged_write;
71             }
72              
73             my $filter =
74             ref( $self->filter ) eq 'ARRAY'
75 0 0         ? { @{ $self->filter } }
  0            
76             : $self->filter;
77              
78 0 0         my $op_doc = {
    0          
79             q => $filter,
80             limit => $self->just_one ? 1 : 0,
81             ( defined $self->collation ? ( collation => $self->collation ) : () ),
82             };
83              
84             return (
85             ! $self->_should_use_acknowledged_write
86             ? (
87             $self->_send_legacy_op_noreply(
88             $link,
89             MongoDB::_Protocol::write_delete(
90             $self->full_name,
91             $self->bson_codec->encode_one( $self->filter ),
92             { just_one => $self->just_one ? 1 : 0 }
93             ),
94             $op_doc,
95             "MongoDB::DeleteResult",
96             "delete",
97             )
98             )
99             : $link->supports_write_commands
100             ? (
101             $self->_send_write_command(
102             $link,
103             [
104             delete => $self->coll_name,
105             deletes => [$op_doc],
106 0 0         @{ $self->write_concern->as_args },
  0 0          
    0          
    0          
107             ],
108             $op_doc,
109             "MongoDB::DeleteResult"
110             )->assert
111             )
112             : (
113             $self->_send_legacy_op_with_gle(
114             $link,
115             MongoDB::_Protocol::write_delete(
116             $self->full_name,
117             $self->bson_codec->encode_one( $self->filter ),
118             { just_one => $self->just_one ? 1 : 0 }
119             ),
120             $op_doc,
121             "MongoDB::DeleteResult",
122             "delete",
123             )->assert
124             )
125             );
126             }
127              
128             sub _parse_cmd {
129 0     0     my ( $self, $res ) = @_;
130 0   0       return ( deleted_count => $res->{n} || 0 );
131             }
132              
133             BEGIN {
134 59     59   36551 no warnings 'once';
  59         189  
  59         2908  
135 59     59   1874 *_parse_gle = \&_parse_cmd;
136             }
137              
138             1;