our
$VERSION
=
'v0.999.998.5'
;
has
db_name
=> (
is
=>
'ro'
,
isa
=> Str,
required
=> 1,
);
has
coll_name
=> (
is
=>
'ro'
,
isa
=> Str,
required
=> 1,
);
has
document
=> (
is
=>
'ro'
,
isa
=> IxHash,
coerce
=> 1,
required
=> 1,
);
sub
execute {
my
(
$self
,
$link
) =
@_
;
my
$max_size
=
$link
->max_bson_object_size;
my
$bson_doc
= MongoDB::BSON::encode_bson(
$self
->document, 1,
$max_size
);
my
$insert_doc
=
bless
\
$bson_doc
,
"MongoDB::BSON::Raw"
;
my
$res
=
$link
->accepts_wire_version(2)
?
$self
->_command_insert(
$link
,
$insert_doc
)
:
$self
->_legacy_op_insert(
$link
,
$insert_doc
);
$res
->assert;
return
$res
;
}
sub
_command_insert {
my
(
$self
,
$link
,
$insert_doc
) =
@_
;
my
$cmd
= Tie::IxHash->new(
insert
=>
$self
->coll_name,
documents
=> [
$insert_doc
],
writeConcern
=>
$self
->write_concern->as_struct,
);
return
$self
->_send_write_command(
$link
,
$cmd
,
$self
->document,
"MongoDB::InsertOneResult"
);
}
sub
_legacy_op_insert {
my
(
$self
,
$link
,
$insert_doc
) =
@_
;
my
$ns
=
$self
->db_name .
"."
.
$self
->coll_name;
my
$op_bson
= MongoDB::_Protocol::write_insert(
$ns
,
$$insert_doc
);
return
$self
->_send_legacy_op_with_gle(
$link
,
$op_bson
,
$self
->document,
"MongoDB::InsertOneResult"
);
}
sub
_parse_cmd {
my
(
$self
,
$res
) =
@_
;
return
(
$res
->{ok} ? (
inserted_id
=>
$self
->document->FETCH(
"_id"
) ) : () );
}
BEGIN {
no
warnings
'once'
;
*_parse_gle
= \
&_parse_cmd
;
}
1;