Client/Mongodb.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Memcached.pm ↑ Summary Client/Mysql.pm »
1 #
2 # $Id$
3 #
4 # client::mongodb Brik
5 #
6 package Metabrik::Client::Mongodb;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 host => [ qw(host) ],
20 port => [ qw(port) ],
21 client => [ qw(INTERNAL) ],
22 database => [ qw(database) ],
23 collection => [ qw(collection) ],
24 },
25 attributes_default => {
26 host => 'localhost',
27 port => 27017,
28 },
29 commands => {
30 connect => [ qw(host|OPTIONAL port|OPTIONAL) ],
31 list_database_names => [ ],
32 list_database_collection_names => [ qw(database) ],
33 get_database => [ qw(database) ],
34 get_database_collection => [ qw(database collection) ],
35 get_database_collection_find_all => [ qw(database collection) ],
36 },
37 require_modules => {
38 MongoDB => [ ],
39 },
40 };
41 }
42  
43 #
44 # Some search examples:
45 # https://metacpan.org/pod/distribution/MongoDB/lib/MongoDB/Examples.pod
46 #
47  
48 sub connect {
49 my $self = shift;
50 my ($host, $port) = @_;
51  
52 $host ||= $self->host;
53 $port ||= $self->port;
54  
55 my $client = MongoDB::MongoClient->new(
56 host => "mongodb://$host:$port",
57 );
58  
59 return $self->client($client);
60 }
61  
62 sub list_database_names {
63 my $self = shift;
64  
65 my $client = $self->client;
66 $self->brik_help_run_undef_arg('connect', $client) or return;
67  
68 return [ $client->database_names ];
69 }
70  
71 sub list_database_collection_names {
72 my $self = shift;
73 my ($database) = @_;
74  
75 my $client = $self->client;
76 $self->brik_help_run_undef_arg('connect', $client) or return;
77 $self->brik_help_run_undef_arg('list_database_collection_names', $database) or return;
78  
79 my $db = $self->get_database($database) or return;
80  
81 return [ $db->collection_names ];
82 }
83  
84 sub get_database {
85 my $self = shift;
86 my ($database) = @_;
87  
88 my $client = $self->client;
89 $self->brik_help_run_undef_arg('connect', $client) or return;
90 $self->brik_help_run_undef_arg('get_database', $database) or return;
91  
92 return $client->get_database($database);
93 }
94  
95 sub get_database_collection {
96 my $self = shift;
97 my ($database, $collection) = @_;
98  
99 my $client = $self->client;
100 $self->brik_help_run_undef_arg('connect', $client) or return;
101 $self->brik_help_run_undef_arg('get_database_collection', $database) or return;
102 $self->brik_help_run_undef_arg('get_database_collection', $collection) or return;
103  
104 my $db = $self->get_database($database) or return;
105  
106 return $db->get_collection($collection);
107 }
108  
109 sub get_database_collection_find_all {
110 my $self = shift;
111 my ($database, $collection) = @_;
112  
113 my $client = $self->client;
114 $self->brik_help_run_undef_arg('connect', $client) or return;
115 $self->brik_help_run_undef_arg('get_database_collection', $database) or return;
116 $self->brik_help_run_undef_arg('get_database_collection', $collection) or return;
117  
118 my $coll = $self->get_database_collection($database, $collection) or return;
119  
120 return [ $coll->find()->all() ];
121 }
122  
123 1;
124  
125 __END__
« Client/Memcached.pm ↑ Summary Client/Mysql.pm »