| 1 | package Mango::Database; | ||||||||||||||||||||
| 2 | 9 | ▶ | use Mojo::Base -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 9 | ▶ | use Carp 'croak'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 9 | ▶ | use Mango::BSON qw(bson_code bson_doc); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 9 | ▶ | use Mango::Collection; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 9 | ▶ | use Mango::GridFS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 9 | has [qw(mango name)]; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 0 | ▶ | sub build_write_concern { | ||||||||||||||||||
|
Subroutine
build_write_concern: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 12 | 0 | my $mango = shift->mango; | |||||||||||||||||||
| 13 | return { | ||||||||||||||||||||
| 14 | 0 | ▶ | j => $mango->j ? \1 : \0, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 15 | w => $mango->w, | ||||||||||||||||||||
| 16 | wtimeout => $mango->wtimeout | ||||||||||||||||||||
| 17 | }; | ||||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | sub collection { | ||||||||||||||||||
|
Subroutine
collection: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 21 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 22 | 0 | return Mango::Collection->new(db => $self, name => $name); | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 0 | ▶ | sub collection_names { | ||||||||||||||||||
|
Subroutine
collection_names: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 26 | 0 | my $self = shift; | |||||||||||||||||||
| 27 | 0 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # Non-blocking | ||||||||||||||||||||
| 30 | 0 | ▶ | return $self->list_collections(@_ => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 31 | 0 | my ($self, $err, $cursor) = @_; | |||||||||||||||||||
| 32 | 0 | ▶ | return $self->$cb($err, []) if $err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 0 | ▶ | $cursor->all(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 34 | 0 | my ($cursor, $err, $docs) = @_; | |||||||||||||||||||
| 35 | 0 | @$docs = map { $_->{name} } @$docs; | |||||||||||||||||||
| 36 | 0 | $self->$cb($err, $docs); | |||||||||||||||||||
| 37 | 0 | }); | |||||||||||||||||||
| 38 | 0 | ▶ | }) if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # Blocking | ||||||||||||||||||||
| 41 | 0 | my $docs = $self->list_collections(@_)->all; | |||||||||||||||||||
| 42 | 0 | @$docs = map { $_->{name} } @$docs; | |||||||||||||||||||
| 43 | 0 | return $docs; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | sub command { | ||||||||||||||||||
|
Subroutine
command: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 47 | 0 | my ($self, $command) = (shift, shift); | |||||||||||||||||||
| 48 | 0 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | ▶ | $command = ref $command ? $command : bson_doc($command => 1, @_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # Non-blocking | ||||||||||||||||||||
| 52 | 0 | my $mango = $self->mango; | |||||||||||||||||||
| 53 | 0 | my $name = $self->name; | |||||||||||||||||||
| 54 | 0 | my $protocol = $mango->protocol; | |||||||||||||||||||
| 55 | return $mango->query( | ||||||||||||||||||||
| 56 | 0 | ▶ | ("$name.\$cmd", {}, 0, -1, $command, {}) => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 57 | 0 | my ($collection, $err, $reply) = @_; | |||||||||||||||||||
| 58 | 0 | my $doc = $reply->{docs}[0]; | |||||||||||||||||||
| 59 | 0 | ▶ | $err ||= $protocol->command_error($doc); | ||||||||||||||||||
|
Condition0%0/3
$err ||= $protocol->command_error($doc)
MC/DC0%0/2
$err ||= $protocol->command_error($doc)
$err
$protocol->command_error($doc)
Truth table0%0/3
A ||= B
A= $errB= $protocol->command_error($doc)
| |||||||||||||||||||||
| 60 | 0 | $self->$cb($err, $doc); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | 0 | ▶ | ) if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Blocking | ||||||||||||||||||||
| 65 | 0 | my $doc = $mango->query("$name.\$cmd", {}, 0, -1, $command, {})->{docs}[0]; | |||||||||||||||||||
| 66 | 0 | ▶ | if (my $err = $protocol->command_error($doc)) { croak $err } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 0 | return $doc; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | sub dereference { | ||||||||||||||||||
|
Subroutine
dereference: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 71 | 0 | my ($self, $dbref, $cb) = @_; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | # Non-blocking | ||||||||||||||||||||
| 74 | 0 | my $collection = $self->collection($dbref->{'$ref'}); | |||||||||||||||||||
| 75 | 0 | ▶ | return $collection->find_one($dbref->{'$id'} => sub { shift; $self->$cb(@_) } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 76 | 0 | ▶ | ) if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # Blocking | ||||||||||||||||||||
| 79 | 0 | return $collection->find_one($dbref->{'$id'}); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | sub gridfs { Mango::GridFS->new(db => shift) } | ||||||||||||||||||
|
Subroutine
gridfs: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | sub list_collections { | ||||||||||||||||||
|
Subroutine
list_collections: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||||||||||
| 86 | 0 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | my $command = bson_doc(listCollections => 1, @_); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # Non-blocking | ||||||||||||||||||||
| 91 | 0 | ▶ | return $self->command($command => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 92 | 0 | my ($self, $err, $res) = @_; | |||||||||||||||||||
| 93 | 0 | $res = $res->{cursor}; | |||||||||||||||||||
| 94 | my $cursor = Mango::Cursor->new(collection => $self->collection, | ||||||||||||||||||||
| 95 | 0 | id => $res->{id}, ns => $res->{ns})->add_batch($res->{firstBatch}); | |||||||||||||||||||
| 96 | 0 | $self->$cb($err, $cursor); | |||||||||||||||||||
| 97 | 0 | ▶ | }) if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # Blocking | ||||||||||||||||||||
| 100 | 0 | my $cursor = $self->command($command)->{cursor}; | |||||||||||||||||||
| 101 | return Mango::Cursor->new(collection => $self->collection, | ||||||||||||||||||||
| 102 | id => $cursor->{id}, ns => $cursor->{ns}) | ||||||||||||||||||||
| 103 | 0 | ->add_batch($cursor->{firstBatch}); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | sub stats { shift->command(bson_doc(dbstats => 1), @_) } | ||||||||||||||||||
|
Subroutine
stats: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 9 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =encoding utf8 | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head1 NAME | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Mango::Database - MongoDB database | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 SYNOPSIS | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | use Mango::Database; | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | my $db = Mango::Database->new(mango => $mango); | ||||||||||||||||||||
| 121 | my $collection = $db->collection('foo'); | ||||||||||||||||||||
| 122 | my $gridfs = $db->gridfs; | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | L<Mango::Database> is a container for MongoDB databases used by L<Mango>. | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | L<Mango::Database> implements the following attributes. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 mango | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | my $mango = $db->mango; | ||||||||||||||||||||
| 135 | $db = $db->mango(Mango->new); | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | L<Mango> object this database belongs to. Note that this reference is usually | ||||||||||||||||||||
| 138 | weakened, so the L<Mango> object needs to be referenced elsewhere as well. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head2 name | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | my $name = $db->name; | ||||||||||||||||||||
| 143 | $db = $db->name('bar'); | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Name of this database. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =head1 METHODS | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | L<Mango::Database> inherits all methods from L<Mojo::Base> and implements the | ||||||||||||||||||||
| 150 | following new ones. | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head2 build_write_concern | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | my $concern = $db->build_write_concern; | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Build write concern based on l</"mango"> settings. | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 collection | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | my $collection = $db->collection('foo'); | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Build L<Mango::Collection> object for collection. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 collection_names | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | my $names = $db->collection_names; | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Names of all collections in this database. You can filter the results by using | ||||||||||||||||||||
| 169 | the same arguments as for C<list_collections>. You can also append a callback | ||||||||||||||||||||
| 170 | to perform operation non-blocking. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | $db->collection_names(sub { | ||||||||||||||||||||
| 173 | my ($db, $err, $names) = @_; | ||||||||||||||||||||
| 174 | ... | ||||||||||||||||||||
| 175 | }); | ||||||||||||||||||||
| 176 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 command | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | my $doc = $db->command(bson_doc(text => 'foo.bar', search => 'test')); | ||||||||||||||||||||
| 181 | my $doc = $db->command(bson_doc(getLastError => 1, w => 2)); | ||||||||||||||||||||
| 182 | my $doc = $db->command('getLastError', w => 2); | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | Run command against database. You can also append a callback to run command | ||||||||||||||||||||
| 185 | non-blocking. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | $db->command(('getLastError', w => 2) => sub { | ||||||||||||||||||||
| 188 | my ($db, $err, $doc) = @_; | ||||||||||||||||||||
| 189 | ... | ||||||||||||||||||||
| 190 | }); | ||||||||||||||||||||
| 191 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | =head2 dereference | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | my $doc = $db->dereference($dbref); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | Resolve database reference. You can also append a callback to perform | ||||||||||||||||||||
| 198 | operation non-blocking. | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | $db->dereference($dbref => sub { | ||||||||||||||||||||
| 201 | my ($db, $err, $doc) = @_; | ||||||||||||||||||||
| 202 | ... | ||||||||||||||||||||
| 203 | }); | ||||||||||||||||||||
| 204 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =head2 gridfs | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | my $gridfs = $db->gridfs; | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Build L<Mango::GridFS> object. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =head2 list_collections | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | # return a cursor for all collections | ||||||||||||||||||||
| 215 | my $cursor = $db->list_collections; | ||||||||||||||||||||
| 216 | # only collections which name matchs a regex | ||||||||||||||||||||
| 217 | my $cursor = $db->list_collections(filter => { name => qr{^prefix} }); | ||||||||||||||||||||
| 218 | # only capped collections | ||||||||||||||||||||
| 219 | my $cursor = $db->list_collections(filter => { 'options.capped' => 1 }); | ||||||||||||||||||||
| 220 | # only the first 10 collections | ||||||||||||||||||||
| 221 | my $cursor = $db->list_collections(cursor => { batchSize => 10 }); | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | Returns a L<Mango::Cursor> of all collections in this database. Each collection | ||||||||||||||||||||
| 224 | is represented by a document containing at least the keys C<name> and | ||||||||||||||||||||
| 225 | C<options>. You can also append a callback to perform operation non-blocking. | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | $db->list_collections(sub { | ||||||||||||||||||||
| 228 | my ($db, $err, $cursor) = @_; | ||||||||||||||||||||
| 229 | ... | ||||||||||||||||||||
| 230 | }); | ||||||||||||||||||||
| 231 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =head2 stats | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | my $stats = $db->stats; | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | Get database statistics. You can also append a callback to perform operation | ||||||||||||||||||||
| 238 | non-blocking. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | $db->stats(sub { | ||||||||||||||||||||
| 241 | my ($db, $err, $stats) = @_; | ||||||||||||||||||||
| 242 | ... | ||||||||||||||||||||
| 243 | }); | ||||||||||||||||||||
| 244 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | =head1 SEE ALSO | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | =cut | ||||||||||||||||||||