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