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 58     58   436 use strict;
  58         148  
  58         1917  
16 58     58   369 use warnings;
  58         143  
  58         2529  
17             package MongoDB::Op::_InsertOne;
18              
19             # Encapsulate a single-document insert operation; returns a
20             # MongoDB::InsertOneResult
21              
22 58     58   352 use version;
  58         129  
  58         435  
23             our $VERSION = 'v2.2.0';
24              
25 58     58   4849 use Moo;
  58         160  
  58         407  
26              
27 58     58   21178 use MongoDB::Error;
  58         163  
  58         7196  
28 58     58   23219 use MongoDB::InsertOneResult;
  58         224  
  58         2037  
29 58     58   467 use MongoDB::_Protocol;
  58         153  
  58         1290  
30              
31 58     58   310 use namespace::clean;
  58         148  
  58         293  
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 58     58   53670 no warnings 'once';
  58         176  
  58         2770  
92 58     58   1876 *_parse_gle = \&_parse_cmd;
93             }
94              
95             1;