File Coverage

blib/lib/MongoDB/Op/_InsertOne.pm
Criterion Covered Total %
statement 28 37 75.6
branch 0 6 0.0
condition n/a
subroutine 10 12 83.3
pod 0 1 0.0
total 38 56 67.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   396 use strict;
  59         118  
  59         1681  
16 59     59   271 use warnings;
  59         114  
  59         2316  
17             package MongoDB::Op::_InsertOne;
18              
19             # Encapsulate a single-document insert operation; returns a
20             # MongoDB::InsertOneResult
21              
22 59     59   298 use version;
  59         115  
  59         423  
23             our $VERSION = 'v2.2.2';
24              
25 59     59   4693 use Moo;
  59         114  
  59         363  
26              
27 59     59   19891 use MongoDB::Error;
  59         139  
  59         22827  
28 59     59   20603 use MongoDB::InsertOneResult;
  59         182  
  59         1892  
29 59     59   404 use MongoDB::_Protocol;
  59         124  
  59         1162  
30              
31 59     59   272 use namespace::clean;
  59         123  
  59         250  
32              
33             has document => (
34             is => 'ro',
35             required => 1,
36             );
37              
38             # this starts undef and gets initialized during processing
39             has _doc_id => (
40             is => 'ro',
41             init_arg => undef,
42             writer => '_set_doc_id',
43             );
44              
45             with $_ for qw(
46             MongoDB::Role::_PrivateConstructor
47             MongoDB::Role::_CollectionOp
48             MongoDB::Role::_SingleBatchDocWrite
49             MongoDB::Role::_InsertPreEncoder
50             MongoDB::Role::_BypassValidation
51             );
52              
53             sub execute {
54 0     0 0   my ( $self, $link ) = @_;
55 0           my ( $orig_doc, $insert_doc ) = ( $self->document );
56              
57             ( $insert_doc = $self->_pre_encode_insert( $link->max_bson_object_size, $orig_doc, '.' ) ),
58 0           ( $self->_set_doc_id( $insert_doc->{metadata}{_id} ) );
59              
60             return $self->_send_legacy_op_noreply( $link,
61 0 0         MongoDB::_Protocol::write_insert( $self->full_name, $insert_doc->{bson} ),
62             $orig_doc, "MongoDB::UnacknowledgedResult", "insert" )
63             if ! $self->_should_use_acknowledged_write;
64              
65             return $self->_send_write_command(
66             $link,
67             $self->_maybe_bypass(
68             $link->supports_document_validation,
69             [
70             insert => $self->coll_name,
71             documents => [$insert_doc],
72 0 0         @{ $self->write_concern->as_args },
  0            
73             ]
74             ),
75             $orig_doc,
76             "MongoDB::InsertOneResult"
77             )->assert
78             if $link->supports_write_commands;
79              
80             return $self->_send_legacy_op_with_gle( $link,
81 0           MongoDB::_Protocol::write_insert( $self->full_name, $insert_doc->{bson} ),
82             $orig_doc, "MongoDB::InsertOneResult", "insert" )->assert;
83             }
84              
85             sub _parse_cmd {
86 0     0     my ( $self, $res ) = @_;
87 0 0         return ( $res->{ok} ? ( inserted_id => $self->_doc_id ) : () );
88             }
89              
90             BEGIN {
91 59     59   48137 no warnings 'once';
  59         139  
  59         2628  
92 59     59   1709 *_parse_gle = \&_parse_cmd;
93             }
94              
95             1;