/usr/share/perl5/Mango/Database.pm is in libmango-perl 0.22-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | package Mango::Database;
use Mojo::Base -base;
use Carp 'croak';
use Mango::BSON qw(bson_code bson_doc);
use Mango::Collection;
use Mango::GridFS;
has [qw(mango name)];
sub collection {
my ($self, $name) = @_;
return Mango::Collection->new(db => $self, name => $name);
}
sub collection_names {
my ($self, $cb) = @_;
my $len = length $self->name;
my $collection = $self->collection('system.namespaces');
# Non-blocking
return $collection->find->all(
sub {
my ($cursor, $err, $docs) = @_;
$self->$cb($err, [map { substr $_->{name}, $len + 1 } @$docs]);
}
) if $cb;
# Blocking
my $docs = $collection->find->all;
return [map { substr $_->{name}, $len + 1 } @$docs];
}
sub command {
my ($self, $command) = (shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
$command = ref $command ? $command : bson_doc($command => 1, @_);
# Non-blocking
my $collection = $self->collection('$cmd');
my $protocol = $self->mango->protocol;
return $collection->find_one(
$command => sub {
my ($collection, $err, $doc) = @_;
$err ||= $protocol->command_error({docs => [$doc]});
$self->$cb($err, $doc // {});
}
) if $cb;
# Blocking
my $doc = $collection->find_one($command);
if (my $err = $protocol->command_error({docs => [$doc]})) { croak $err }
return $doc;
}
sub dereference {
my ($self, $dbref, $cb) = @_;
# Non-blocking
my $collection = $self->collection($dbref->{'$ref'});
return $collection->find_one($dbref->{'$id'} => sub { shift; $self->$cb(@_) }
) if $cb;
# Blocking
return $collection->find_one($dbref->{'$id'});
}
sub gridfs { Mango::GridFS->new(db => shift) }
sub stats { shift->command(bson_doc(dbstats => 1), @_) }
1;
=encoding utf8
=head1 NAME
Mango::Database - MongoDB database
=head1 SYNOPSIS
use Mango::Database;
my $db = Mango::Database->new(mango => $mango);
my $collection = $db->collection('foo');
my $gridfs = $db->gridfs;
=head1 DESCRIPTION
L<Mango::Database> is a container for MongoDB databases used by L<Mango>.
=head1 ATTRIBUTES
L<Mango::Database> implements the following attributes.
=head2 mango
my $mango = $db->mango;
$db = $db->mango(Mango->new);
L<Mango> object this database belongs to. Note that this reference is usually
weakened, so the L<Mango> object needs to be referenced elsewhere as well.
=head2 name
my $name = $db->name;
$db = $db->name('bar');
Name of this database.
=head1 METHODS
L<Mango::Database> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 collection
my $collection = $db->collection('foo');
Get L<Mango::Collection> object for collection.
=head2 collection_names
my $names = $db->collection_names;
Names of all collections in this database. You can also append a callback to
perform operation non-blocking.
$db->collection_names(sub {
my ($db, $err, $names) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 command
my $doc = $db->command(bson_doc(text => 'foo.bar', search => 'test'));
my $doc = $db->command(bson_doc(getLastError => 1, w => 2));
my $doc = $db->command('getLastError', w => 2);
Run command against database. You can also append a callback to run command
non-blocking.
$db->command(('getLastError', w => 2) => sub {
my ($db, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 dereference
my $doc = $db->dereference($dbref);
Resolve database reference. You can also append a callback to perform
operation non-blocking.
$db->dereference($dbref => sub {
my ($db, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 gridfs
my $gridfs = $db->gridfs;
Get L<Mango::GridFS> object.
=head2 stats
my $stats = $db->stats;
Get database statistics. You can also append a callback to perform operation
non-blocking.
$db->stats(sub {
my ($db, $err, $stats) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head1 SEE ALSO
L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|