line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Model::MongoDB; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GETTY'; |
3
|
|
|
|
|
|
|
$Catalyst::Model::MongoDB::VERSION = '0.13'; |
4
|
|
|
|
|
|
|
# ABSTRACT: MongoDB model class for Catalyst |
5
|
3
|
|
|
3
|
|
67467
|
use MongoDB; |
|
3
|
|
|
|
|
2927152
|
|
|
3
|
|
|
|
|
80
|
|
6
|
3
|
|
|
3
|
|
26
|
use MongoDB::OID; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
139
|
|
7
|
3
|
|
|
3
|
|
2923
|
use Moose; |
|
3
|
|
|
|
|
1887191
|
|
|
3
|
|
|
|
|
24
|
|
8
|
3
|
|
|
3
|
|
20803
|
use version; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
28
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
216
|
BEGIN { extends 'Catalyst::Model' } |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has host => ( isa => 'Str', is => 'ro', required => 1, default => sub { 'localhost' } ); |
13
|
|
|
|
|
|
|
has port => ( isa => 'Int', is => 'ro', required => 1, default => sub { 27017 } ); |
14
|
|
|
|
|
|
|
has dbname => ( isa => 'Str', is => 'ro' ); |
15
|
|
|
|
|
|
|
has collectionname => ( isa => 'Str', is => 'ro' ); |
16
|
|
|
|
|
|
|
has gridfsname => ( isa => 'Str', is => 'ro' ); |
17
|
|
|
|
|
|
|
has username => ( isa => 'Str', is => 'ro', predicate => 'has_username' ); |
18
|
|
|
|
|
|
|
has password => ( isa => 'Str', is => 'ro', predicate => 'has_password' ); |
19
|
|
|
|
|
|
|
has find_master => ( isa => 'Int', is => 'ro', default => sub { 0 } ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has 'connection' => ( |
22
|
|
|
|
|
|
|
isa => 'MongoDB::MongoClient', |
23
|
|
|
|
|
|
|
is => 'rw', |
24
|
|
|
|
|
|
|
lazy_build => 1, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _build_connection { |
28
|
2
|
|
|
2
|
|
4
|
my ($self) = @_; |
29
|
|
|
|
|
|
|
|
30
|
2
|
50
|
|
|
|
88
|
my $conn = MongoDB::MongoClient->new( |
31
|
|
|
|
|
|
|
host => $self->host, |
32
|
|
|
|
|
|
|
port => $self->port, |
33
|
|
|
|
|
|
|
find_master => $self->find_master, |
34
|
|
|
|
|
|
|
( $self->dbname ? ( dbname => $self->dbname ) : () ), |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# attempt authentication only if we have all three parameters for |
38
|
|
|
|
|
|
|
# MongoDB::Connection->authenticate() |
39
|
2
|
0
|
33
|
|
|
101454
|
if ($self->dbname && $self->has_username && $self->has_password) { |
|
|
|
33
|
|
|
|
|
40
|
0
|
0
|
|
|
|
0
|
$conn->authenticate($self->dbname, $self->username, $self->password) |
41
|
|
|
|
|
|
|
if version->parse($MongoDB::VERSION) < 1.0; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
85
|
return $conn; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has 'dbs' => ( |
48
|
|
|
|
|
|
|
isa => 'HashRef[MongoDB::Database]', |
49
|
|
|
|
|
|
|
is => 'rw', |
50
|
|
|
|
|
|
|
default => sub {{}}, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub db { |
54
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $dbname ) = @_; |
55
|
0
|
0
|
|
|
|
0
|
$dbname = $self->dbname if !$dbname; |
56
|
0
|
0
|
|
|
|
0
|
confess "no dbname given via parameter or config" if !$dbname; |
57
|
0
|
0
|
|
|
|
0
|
if (!$self->dbs->{$dbname}) { |
58
|
0
|
|
|
|
|
0
|
$self->dbs->{$dbname} = $self->connection->get_database($dbname); |
59
|
|
|
|
|
|
|
} |
60
|
0
|
|
|
|
|
0
|
return $self->dbs->{$dbname}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
*c = \&collection; |
64
|
|
|
|
|
|
|
*coll = \&collection; |
65
|
|
|
|
|
|
|
sub collection { |
66
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $param ) = @_; |
67
|
0
|
|
|
|
|
0
|
my $dbname; |
68
|
|
|
|
|
|
|
my $collname; |
69
|
0
|
|
|
|
|
0
|
my @params; |
70
|
0
|
0
|
|
|
|
0
|
if ($param) { |
71
|
0
|
|
|
|
|
0
|
@params = split(/\./,$param) |
72
|
|
|
|
|
|
|
} |
73
|
0
|
0
|
|
|
|
0
|
if (@params > 1) { |
74
|
0
|
|
|
|
|
0
|
$dbname = $params[0]; |
75
|
0
|
|
|
|
|
0
|
$collname = $params[1]; |
76
|
|
|
|
|
|
|
} else { |
77
|
0
|
|
|
|
|
0
|
$dbname = $self->dbname; |
78
|
0
|
0
|
|
|
|
0
|
if (@params == 1) { |
79
|
0
|
|
|
|
|
0
|
$collname = $params[0]; |
80
|
|
|
|
|
|
|
} else { |
81
|
0
|
|
|
|
|
0
|
$collname = $self->collectionname; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
0
|
0
|
|
|
|
0
|
confess "no dbname given via parameter or config" if !$dbname; |
85
|
0
|
0
|
|
|
|
0
|
confess "no collectionname given via parameter or config" if !$collname; |
86
|
0
|
|
|
|
|
0
|
$self->db($dbname)->get_collection($collname); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub run { |
90
|
0
|
|
|
0
|
1
|
0
|
my ( $self, @params ) = @_; |
91
|
0
|
0
|
|
|
|
0
|
confess "no dbname given via config" if !$self->dbname; |
92
|
0
|
|
|
|
|
0
|
$self->db->run_command(@params); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub eval { |
96
|
0
|
|
|
0
|
1
|
0
|
my ( $self, @params ) = @_; |
97
|
0
|
0
|
|
|
|
0
|
confess "no dbname given via config" if !$self->dbname; |
98
|
0
|
|
|
|
|
0
|
$self->db->eval(@params); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
*collnames = \&collection_names; |
102
|
|
|
|
|
|
|
sub collection_names { |
103
|
0
|
|
|
0
|
1
|
0
|
my ( $self, @params ) = @_; |
104
|
0
|
0
|
|
|
|
0
|
confess "no dbname given via config" if !$self->dbname; |
105
|
0
|
|
|
|
|
0
|
$self->db->collection_names(@params); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
*g = \&gridfs; |
109
|
|
|
|
|
|
|
sub gridfs { |
110
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $param ) = @_; |
111
|
0
|
|
|
|
|
0
|
my $dbname; |
112
|
|
|
|
|
|
|
my $gridfsname; |
113
|
0
|
|
|
|
|
0
|
my @params = split(/\./,$param); |
114
|
0
|
0
|
|
|
|
0
|
if (@params > 1) { |
115
|
0
|
|
|
|
|
0
|
$dbname = $params[0]; |
116
|
0
|
|
|
|
|
0
|
$gridfsname = $params[1]; |
117
|
|
|
|
|
|
|
} else { |
118
|
0
|
|
|
|
|
0
|
$dbname = $self->dbname; |
119
|
0
|
0
|
|
|
|
0
|
if (@params == 1) { |
120
|
0
|
|
|
|
|
0
|
$gridfsname = $params[0]; |
121
|
|
|
|
|
|
|
} else { |
122
|
0
|
|
|
|
|
0
|
$gridfsname = $self->gridfsname; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
0
|
0
|
|
|
|
0
|
confess "no dbname given via parameter or config" if !$dbname; |
126
|
0
|
0
|
|
|
|
0
|
confess "no gridfsname given via parameter or config" if !$gridfsname; |
127
|
0
|
|
|
|
|
0
|
$self->db($dbname)->get_gridfs($gridfsname); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
*dbnames = \&database_names; |
131
|
|
|
|
|
|
|
sub database_names { |
132
|
2
|
|
|
2
|
1
|
4354
|
my ( $self ) = @_; |
133
|
2
|
|
|
|
|
90
|
$self->connection->database_names; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub oid { |
137
|
0
|
|
|
0
|
1
|
|
my( $self, $_id ) = @_; |
138
|
0
|
|
|
|
|
|
return MongoDB::OID->new( value => $_id ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub authenticate { |
142
|
0
|
|
|
0
|
1
|
|
my( $self, @params ) = @_; |
143
|
0
|
|
|
|
|
|
return $self->connection->authenticate(@params); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
1; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
__END__ |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=pod |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 NAME |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Catalyst::Model::MongoDB - MongoDB model class for Catalyst |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 VERSION |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
version 0.13 |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SYNOPSIS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# |
163
|
|
|
|
|
|
|
# Config |
164
|
|
|
|
|
|
|
# |
165
|
|
|
|
|
|
|
<Model::MyModel> |
166
|
|
|
|
|
|
|
host localhost |
167
|
|
|
|
|
|
|
port 27017 |
168
|
|
|
|
|
|
|
dbname mydatabase |
169
|
|
|
|
|
|
|
username myuser |
170
|
|
|
|
|
|
|
password mypass |
171
|
|
|
|
|
|
|
collectionname preferedcollection |
172
|
|
|
|
|
|
|
gridfs preferedgridfs |
173
|
|
|
|
|
|
|
</Model::MyModel> |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# |
176
|
|
|
|
|
|
|
# Usage |
177
|
|
|
|
|
|
|
# |
178
|
|
|
|
|
|
|
$c->model('MyModel')->db # returns MongoDB::MongoClient->get_database |
179
|
|
|
|
|
|
|
$c->model('MyModel')->db('otherdb') # returns ->otherdb |
180
|
|
|
|
|
|
|
$c->model('MyModel')->collection # returns ->mydatabase->preferedcollection |
181
|
|
|
|
|
|
|
$c->model('MyModel')->coll # the same... |
182
|
|
|
|
|
|
|
$c->model('MyModel')->c # the same... |
183
|
|
|
|
|
|
|
$c->model('MyModel')->c('otherdb.othercollection') # returns ->otherdb->othercollection |
184
|
|
|
|
|
|
|
$c->model('MyModel')->c('somecollection') # returns ->mydatabase->somecollection |
185
|
|
|
|
|
|
|
$c->model('MyModel')->gridfs # returns ->mydatabase->get_gridfs('preferedgridfs') |
186
|
|
|
|
|
|
|
$c->model('MyModel')->g # the same... |
187
|
|
|
|
|
|
|
$c->model('MyModel')->g('somegridfs') # returns ->mydatabase->get_gridfs('somegridfs') |
188
|
|
|
|
|
|
|
$c->model('MyModel')->g('otherdb.othergridfs') # returns ->otherdb->get_gridfs('othergridfs') |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$c->model('MyModel')->run(...) # returns ->mydatabase->run_command(...) |
191
|
|
|
|
|
|
|
$c->model('MyModel')->eval(...) # returns ->mydatabase->eval(...) |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
$c->model('MyModel')->database_names # returns ->database_names |
194
|
|
|
|
|
|
|
$c->model('MyModel')->dbnames # the same... |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 DESCRIPTION |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
This model class exposes L<MongoDB::MongoClient> as a Catalyst model. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 CONFIGURATION |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
You can pass the same configuration fields as when you make a new L<MongoDB::MongoClient>. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
In addition you can also give a database name via dbname, a collection name via collectioname or |
205
|
|
|
|
|
|
|
a gridfs name via gridfsname. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 AUTHENTICATION |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
If all three of C<username>, C<password>, and C<dbname> are present, this class |
210
|
|
|
|
|
|
|
will authenticate via MongoDB::MongoClient->authenticate(). (See |
211
|
|
|
|
|
|
|
L<MongoDB::MongoClient|MongoDB::MongoClient> for details). |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 METHODS |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 dbnames |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 database_names |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
List of databases. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 collnames |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 collection_names |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
List of collection names of the default database. You cant give other database names here, if you need this please do: |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
$c->model('MyModel')->db('otherdatabase')->collection_names |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 collection |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head2 coll |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head2 c |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Gives back a MongoDB::Collection, you can also directly access other dbs collections, with "otherdb.othercollection". |
236
|
|
|
|
|
|
|
If no collectionname is given he uses the default collectionname given on config. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head2 gridfs |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head2 g |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Gives back a MongoDB::GridFS. If no gridfsname is given, he uses the default gridfsname given on config. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 run |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Run a command via MongoDB::Database->run_command on the default database. You cant give other database names here, |
247
|
|
|
|
|
|
|
if you need this please do: |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
$c->model('MyModel')->db('otherdatabase')->run_command(...) |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head2 eval |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Eval code via MongoDB::Database->eval on the default database. You cant give other database names here, |
254
|
|
|
|
|
|
|
if you need this please do: |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
$c->model('MyModel')->db('otherdatabase')->eval(...) |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 oid |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Creates MongoDB::OID object |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head2 authenticate |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
[re]authenticate after the initial connection, or |
265
|
|
|
|
|
|
|
authenticate to multiple databases within the same model. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head1 SUPPORT |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
IRC |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Join #catalyst on irc.perl.org and ask for Getty. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Repository |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
http://github.com/singingfish/p5-catalyst-model-mongodb |
276
|
|
|
|
|
|
|
Pull request and additional contributors are welcome |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
Issue Tracker |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
http://github.com/singingfish/p5-catalyst-model-mongodb/issues |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=head1 AUTHOR |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Torsten Raudssus <torsten@raudssus.de> L<http://www.raudssus.de/> |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Raudssus Social Software. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
291
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=cut |