| 1 | 71 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 2 | 71 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 71 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | package BSON::DBRef; | ||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: BSON type wrapper for MongoDB DBRefs | ||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use version; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 71 | our $VERSION = 'v1.12.2'; | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use Tie::IxHash; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 71 | ▶ | use Moo 2.002004; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 71 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 71 | ▶ | use BSON (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | #pod =attr id | ||||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||||
| 19 | #pod Required. The C<_id> value of the referenced document. If the | ||||||||||||||||||||||||||||||
| 20 | #pod C<_id> is an ObjectID, then you must use a L<BSON::OID> object. | ||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||
| 22 | #pod =cut | ||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | # no type constraint since an _id can be anything | ||||||||||||||||||||||||||||||
| 25 | 71 | has id => ( | |||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||
| 27 | required => 1 | ||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | #pod =attr ref | ||||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||||
| 32 | #pod Required. The name of the collection in which the referenced document | ||||||||||||||||||||||||||||||
| 33 | #pod lives. Either a L<MongoDB::Collection> object or a string containing the | ||||||||||||||||||||||||||||||
| 34 | #pod collection name. The object will be coerced to string form. | ||||||||||||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||||||||||||
| 36 | #pod This may also be specified in the constructor as C<'$ref'>. | ||||||||||||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||||||||||||
| 38 | #pod =cut | ||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | has 'ref' => ( | ||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||
| 42 | required => 1, | ||||||||||||||||||||||||||||||
| 43 | 36 | ▶ | coerce => sub { CORE::ref($_[0]) eq 'MongoDB::Collection' ? $_[0]->name : $_[0] }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 44 | 36 | ▶ | isa => sub { die "must be a non-empty string" unless defined($_[0]) && length($_[0]) }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and length $_[0]
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0]
length $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= length $_[0]
| |||||||||||||||||||||||||||||||
| 45 | 71 | ); | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | #pod =attr db | ||||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||||
| 49 | #pod Optional. The database in which the referenced document lives. Either a | ||||||||||||||||||||||||||||||
| 50 | #pod L<MongoDB::Database> object or a string containing the database name. The | ||||||||||||||||||||||||||||||
| 51 | #pod object will be coerced to string form. | ||||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||||
| 53 | #pod Not all other language drivers support the C<$db> field, so using this | ||||||||||||||||||||||||||||||
| 54 | #pod field is not recommended. | ||||||||||||||||||||||||||||||
| 55 | #pod | ||||||||||||||||||||||||||||||
| 56 | #pod This may also be specified in the constructor as C<'$db'>. | ||||||||||||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||||||||||||
| 58 | #pod =cut | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | has db => ( | ||||||||||||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||||||||||||
| 62 | 36 | ▶ | coerce => sub { CORE::ref($_[0]) eq 'MongoDB::DataBase' ? $_[0]->name : $_[0] }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 63 | 36 | ▶ | isa => sub { return if ! defined($_[0]); die "must be a non-empty string" unless length($_[0]) }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 64 | 71 | ); | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | #pod =attr extra | ||||||||||||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||||||||||||
| 68 | #pod Optional. A hash reference of additional fields in the DBRef document. | ||||||||||||||||||||||||||||||
| 69 | #pod Not all MongoDB drivers support this feature and you B<should not> rely on | ||||||||||||||||||||||||||||||
| 70 | #pod it. This attribute exists solely to ensure DBRefs generated by drivers that | ||||||||||||||||||||||||||||||
| 71 | #pod do allow extra fields will round-trip correctly. | ||||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||||
| 73 | #pod B<USE OF THIS FIELD FOR NEW DBREFS IS NOT RECOMMENDED.> | ||||||||||||||||||||||||||||||
| 74 | #pod | ||||||||||||||||||||||||||||||
| 75 | #pod =cut | ||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | has extra => ( | ||||||||||||||||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||||||||||||||
| 79 | 36 | ▶ | isa => sub { return if ! defined($_[0]); die "must be a hashref" unless eval { scalar %{$_[0]}; 1 } }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 81 | 71 | ); | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | 36 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||||
| 84 | 36 | my $orig = shift; | |||||||||||||||||||||||||||||
| 85 | 36 | my $class = shift; | |||||||||||||||||||||||||||||
| 86 | 36 | my $hr = $class->$orig(@_); | |||||||||||||||||||||||||||||
| 87 | return { | ||||||||||||||||||||||||||||||
| 88 | id => ( | ||||||||||||||||||||||||||||||
| 89 | exists( $hr->{'$id'} ) ? delete $hr->{'$id'} | ||||||||||||||||||||||||||||||
| 90 | : exists( $hr->{id} ) ? delete $hr->{id} | ||||||||||||||||||||||||||||||
| 91 | : undef | ||||||||||||||||||||||||||||||
| 92 | ), | ||||||||||||||||||||||||||||||
| 93 | 'ref' => ( | ||||||||||||||||||||||||||||||
| 94 | exists( $hr->{'$ref'} ) ? delete $hr->{'$ref'} | ||||||||||||||||||||||||||||||
| 95 | : exists( $hr->{'ref'}) ? delete $hr->{'ref'} | ||||||||||||||||||||||||||||||
| 96 | : undef | ||||||||||||||||||||||||||||||
| 97 | ), | ||||||||||||||||||||||||||||||
| 98 | db => ( | ||||||||||||||||||||||||||||||
| 99 | exists( $hr->{'$db'} ) ? delete $hr->{'$db'} | ||||||||||||||||||||||||||||||
| 100 | : exists( $hr->{db} ) ? delete $hr->{db} | ||||||||||||||||||||||||||||||
| 101 | : undef | ||||||||||||||||||||||||||||||
| 102 | 36 | ▶ | ), | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 103 | extra => $hr, | ||||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||||
| 105 | 71 | }; | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | 15 | ▶ | sub _ordered { | ||||||||||||||||||||||||||||
|
Subroutine
_ordered: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 108 | 15 | my $self = shift; | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | return Tie::IxHash->new( | ||||||||||||||||||||||||||||||
| 111 | '$ref' => $self->ref, | ||||||||||||||||||||||||||||||
| 112 | '$id' => $self->id, | ||||||||||||||||||||||||||||||
| 113 | ( defined($self->db) ? ( '$db' => $self->db ) : () ), | ||||||||||||||||||||||||||||||
| 114 | 15 | ▶ | %{ $self->extra }, | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | #pod =method TO_JSON | ||||||||||||||||||||||||||||||
| 119 | #pod | ||||||||||||||||||||||||||||||
| 120 | #pod If the C<BSON_EXTJSON> option is true, returns a hashref compatible with | ||||||||||||||||||||||||||||||
| 121 | #pod MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||||
| 122 | #pod format, which represents it as a document as follows: | ||||||||||||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||||||||||||
| 124 | #pod { "$ref": "<collection name>", "$id": "<id>" } | ||||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||||
| 126 | #pod If the C<BSON_EXTJSON> option is false, an error is thrown, as this value | ||||||||||||||||||||||||||||||
| 127 | #pod can't otherwise be represented in JSON. | ||||||||||||||||||||||||||||||
| 128 | #pod | ||||||||||||||||||||||||||||||
| 129 | #pod =cut | ||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | 13 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 132 | 13 | my $self = shift; | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||
| 134 | 13 | ▶ | if ( $ENV{BSON_EXTJSON} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 135 | 13 | my $id = $self->id; | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | 13 | ▶ | if (ref $id) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 138 | 12 | $id = $id->TO_JSON; | |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||||
| 141 | 1 | $id = BSON->perl_to_extjson($id); | |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | 13 | my %data; | |||||||||||||||||||||||||||||
| 145 | 13 | tie( %data, 'Tie::IxHash' ); | |||||||||||||||||||||||||||||
| 146 | 13 | $data{'$ref'} = $self->ref; | |||||||||||||||||||||||||||||
| 147 | 13 | $data{'$id'} = $id; | |||||||||||||||||||||||||||||
| 148 | 13 | ▶ | $data{'$db'} = $self->db | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 149 | if defined $self->db; | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | 13 | my $extra = $self->extra; | |||||||||||||||||||||||||||||
| 152 | $data{$_} = $extra->{$_} | ||||||||||||||||||||||||||||||
| 153 | 13 | for keys %$extra; | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||
| 155 | 13 | return \%data; | |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | 0 | Carp::croak( "The value '$self' is illegal in JSON" ); | |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | 71 | 1; | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||
| 163 | =pod | ||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||
| 165 | =encoding UTF-8 | ||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||
| 167 | =head1 NAME | ||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | BSON::DBRef - BSON type wrapper for MongoDB DBRefs | ||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||
| 171 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||
| 173 | version v1.12.2 | ||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||
| 175 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | use BSON::Types ':all'; | ||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||
| 179 | my $dbref = bson_dbref( $oid, $collection_name ); | ||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||
| 181 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | This module provides a BSON type wrapper for L<MongoDB Database | ||||||||||||||||||||||||||||||
| 184 | References|http://docs.mongodb.org/manual/reference/database-references/>. | ||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||
| 186 | A DBRef is a special document format which references another document in | ||||||||||||||||||||||||||||||
| 187 | the database. DBRefs are not the same as foreign keys and do not provide | ||||||||||||||||||||||||||||||
| 188 | any referential integrity or constraint checking. For example, a DBRef may | ||||||||||||||||||||||||||||||
| 189 | point to a document that no longer exists (or never existed.) | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | Use of DBRefs is discouraged, so this module is provided for backwards | ||||||||||||||||||||||||||||||
| 192 | compatibility. L<"Manual | ||||||||||||||||||||||||||||||
| 193 | references"|https://docs.mongodb.com/manual/reference/database-references/#document-references> | ||||||||||||||||||||||||||||||
| 194 | are preferred when there is a need to reference other documents. | ||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||
| 196 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||
| 198 | =head2 id | ||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||
| 200 | Required. The C<_id> value of the referenced document. If the | ||||||||||||||||||||||||||||||
| 201 | C<_id> is an ObjectID, then you must use a L<BSON::OID> object. | ||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||
| 203 | =head2 ref | ||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||
| 205 | Required. The name of the collection in which the referenced document | ||||||||||||||||||||||||||||||
| 206 | lives. Either a L<MongoDB::Collection> object or a string containing the | ||||||||||||||||||||||||||||||
| 207 | collection name. The object will be coerced to string form. | ||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||
| 209 | This may also be specified in the constructor as C<'$ref'>. | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | =head2 db | ||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||
| 213 | Optional. The database in which the referenced document lives. Either a | ||||||||||||||||||||||||||||||
| 214 | L<MongoDB::Database> object or a string containing the database name. The | ||||||||||||||||||||||||||||||
| 215 | object will be coerced to string form. | ||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||
| 217 | Not all other language drivers support the C<$db> field, so using this | ||||||||||||||||||||||||||||||
| 218 | field is not recommended. | ||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||
| 220 | This may also be specified in the constructor as C<'$db'>. | ||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||
| 222 | =head2 extra | ||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||
| 224 | Optional. A hash reference of additional fields in the DBRef document. | ||||||||||||||||||||||||||||||
| 225 | Not all MongoDB drivers support this feature and you B<should not> rely on | ||||||||||||||||||||||||||||||
| 226 | it. This attribute exists solely to ensure DBRefs generated by drivers that | ||||||||||||||||||||||||||||||
| 227 | do allow extra fields will round-trip correctly. | ||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||
| 229 | B<USE OF THIS FIELD FOR NEW DBREFS IS NOT RECOMMENDED.> | ||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||
| 231 | =head1 METHODS | ||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||
| 233 | =head2 TO_JSON | ||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||
| 235 | If the C<BSON_EXTJSON> option is true, returns a hashref compatible with | ||||||||||||||||||||||||||||||
| 236 | MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||||
| 237 | format, which represents it as a document as follows: | ||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||
| 239 | { "$ref": "<collection name>", "$id": "<id>" } | ||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||
| 241 | If the C<BSON_EXTJSON> option is false, an error is thrown, as this value | ||||||||||||||||||||||||||||||
| 242 | can't otherwise be represented in JSON. | ||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||
| 244 | =for Pod::Coverage BUILDARGS | ||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||
| 246 | =head1 AUTHORS | ||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||
| 248 | =over 4 | ||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||
| 250 | =item * | ||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||
| 252 | David Golden <david@mongodb.com> | ||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||
| 254 | =item * | ||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||
| 256 | Stefan G. <minimalist@lavabit.com> | ||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||
| 258 | =back | ||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||
| 260 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||
| 262 | This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc. | ||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||
| 264 | This is free software, licensed under: | ||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||
| 266 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||
| 268 | =cut | ||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||
| 270 | __END__ | ||||||||||||||||||||||||||||||