File Coverage

blib/lib/MongoDB/BulkWriteView.pm
Criterion Covered Total %
statement 27 62 43.5
branch 0 26 0.0
condition 0 3 0.0
subroutine 9 18 50.0
pod 8 8 100.0
total 44 117 37.6


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   415 use strict;
  59         159  
  59         1658  
16 59     59   345 use warnings;
  59         145  
  59         2694  
17             package MongoDB::BulkWriteView;
18              
19             # ABSTRACT: Bulk write operations against a query document
20              
21 59     59   350 use version;
  59         145  
  59         321  
22             our $VERSION = 'v2.2.1';
23              
24 59     59   4369 use Moo;
  59         159  
  59         388  
25              
26 59     59   20698 use MongoDB::Error;
  59         180  
  59         6233  
27 59         517 use MongoDB::_Types qw(
28             Boolish
29             Document
30             IxHash
31 59     59   444 );
  59         160  
32 59         424 use Types::Standard qw(
33             Maybe
34             ArrayRef
35             InstanceOf
36 59     59   82478 );
  59         165  
37 59     59   56475 use boolean;
  59         142  
  59         470  
38 59     59   4413 use namespace::clean -except => 'meta';
  59         163  
  59         472  
39              
40             # A hash reference containing a MongoDB query document
41             has _query => (
42             is => 'ro',
43             isa => IxHash,
44             coerce => IxHash->coercion,
45             required => 1
46             );
47              
48             # Originating bulk write object for executing write operations.
49             has _bulk => (
50             is => 'ro',
51             isa => InstanceOf['MongoDB::BulkWrite'],
52             required => 1,
53             handles => [qw/_enqueue_write/]
54             );
55              
56             has _collation => (
57             is => 'ro',
58             isa => Maybe [Document],
59             );
60              
61             has _array_filters => (
62             is => 'ro',
63             isa => Maybe [ArrayRef[Document]],
64             );
65              
66             has _upsert => (
67             is => 'ro',
68             isa => Boolish,
69             default => 0,
70             );
71              
72             sub collation {
73 0     0 1   my ($self, $collation) = @_;
74 0           return $self->new( %$self, _collation => $collation );
75             }
76              
77             sub arrayFilters {
78 0     0 1   my ( $self, $array_filters ) = @_;
79 0           return $self->new( %$self, _array_filters => $array_filters );
80             }
81              
82             sub upsert {
83 0     0 1   my ($self) = @_;
84 0 0         unless ( @_ == 1 ) {
85 0           MongoDB::UsageError->throw("the upsert method takes no arguments");
86             }
87 0           return $self->new( %$self, _upsert => true );
88             }
89              
90             sub update_many {
91 0     0 1   push @_, "update_many";
92 0           goto &_update;
93             }
94              
95             sub update_one {
96 0     0 1   push @_, "update_one";
97 0           goto &_update;
98             }
99              
100             sub replace_one {
101 0     0 1   push @_, "replace_one";
102 0           goto &_update;
103             }
104              
105             sub _update {
106 0     0     my $method = pop @_;
107 0           my ( $self, $doc ) = @_;
108              
109 0           my $type = ref $doc;
110 0 0 0       unless ( @_ == 2 && grep { $type eq $_ } qw/HASH ARRAY Tie::IxHash BSON::Array/ ) {
  0            
111 0           MongoDB::UsageError->throw("argument to $method must be a single hashref, arrayref, Tie::IxHash or BSON::Array");
112             }
113              
114 0 0         if ( ref $doc eq 'ARRAY' ) {
    0          
115 0 0         MongoDB::UsageError->throw("array reference to $method must have key/value pairs")
116             if @$doc % 2;
117 0           $doc = Tie::IxHash->new(@$doc);
118             }
119             elsif ( ref $doc eq 'HASH' ) {
120 0           $doc = Tie::IxHash->new(%$doc);
121             }
122              
123 0 0         $self->_bulk->_retryable( 0 ) if $method eq 'update_many';
124              
125 0 0         my $update = {
    0          
    0          
126             q => $self->_query,
127             u => $doc,
128             multi => $method eq 'update_many' ? true : false,
129             upsert => boolean( $self->_upsert ),
130             is_replace => $method eq 'replace_one',
131             (defined $self->_collation ? (collation => $self->_collation) : ()),
132             (defined $self->_array_filters ? (arrayFilters => $self->_array_filters) : ()),
133             };
134              
135 0           $self->_enqueue_write( [ update => $update ] );
136              
137 0           return;
138             }
139              
140             sub delete_many {
141 0     0 1   my ($self) = @_;
142 0           $self->_bulk->_retryable( 0 );
143 0 0         $self->_enqueue_write(
    0          
144             [
145             delete => {
146             q => $self->_query,
147             limit => 0,
148             ( defined $self->_collation ? ( collation => $self->_collation ) : () ),
149             (defined $self->_array_filters ? (arrayFilters => $self->_array_filters) : ()),
150             }
151             ]
152             );
153 0           return;
154             }
155              
156             sub delete_one {
157 0     0 1   my ($self) = @_;
158 0 0         $self->_enqueue_write(
    0          
159             [
160             delete => {
161             q => $self->_query,
162             limit => 1,
163             ( defined $self->_collation ? ( collation => $self->_collation ) : () ),
164             (defined $self->_array_filters ? (arrayFilters => $self->_array_filters) : ()),
165             }
166             ]
167             );
168 0           return;
169             }
170              
171             1;
172              
173             __END__