line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MongoX; |
2
|
|
|
|
|
|
|
# ABSTRACT: DSL sugar for MongoDB |
3
|
4
|
|
|
4
|
|
204264
|
use strict; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
171
|
|
4
|
4
|
|
|
4
|
|
25
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
493
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
3956
|
use parent qw( Exporter ); |
|
4
|
|
|
|
|
24440
|
|
|
4
|
|
|
|
|
32
|
|
9
|
4
|
|
|
4
|
|
2657
|
use MongoX::Context; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT = qw( |
12
|
|
|
|
|
|
|
boot |
13
|
|
|
|
|
|
|
add_connection |
14
|
|
|
|
|
|
|
use_connection |
15
|
|
|
|
|
|
|
use_collection |
16
|
|
|
|
|
|
|
use_db |
17
|
|
|
|
|
|
|
context_db |
18
|
|
|
|
|
|
|
context_connection |
19
|
|
|
|
|
|
|
context_collection |
20
|
|
|
|
|
|
|
with_context |
21
|
|
|
|
|
|
|
for_dbs |
22
|
|
|
|
|
|
|
for_collections |
23
|
|
|
|
|
|
|
for_connections |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub use_connection { MongoX::Context::use_connection(@_) } |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub use_db { MongoX::Context::use_db(@_) } |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub use_collection { MongoX::Context::use_collection(@_) } |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub context_db { MongoX::Context::context_db } |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub context_connection { MongoX::Context::context_connection } |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub context_collection { MongoX::Context::context_collection } |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub add_connection { MongoX::Context::add_connection(@_) } |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub boot { MongoX::Context::boot(@_) } |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub with_context(&@) { MongoX::Context::with_context(@_) } |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub for_dbs(&@) { MongoX::Context::for_dbs(shift,@_) }; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub for_connections(&@) { MongoX::Context::for_connections(shift,@_) }; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub for_collections(&@) { MongoX::Context::for_collections(shift,@_) }; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub import { |
64
|
|
|
|
|
|
|
my ( $class, %options ) = @_; |
65
|
|
|
|
|
|
|
$class->export_to_level( 1, $class, @EXPORT ); |
66
|
|
|
|
|
|
|
boot %options if %options; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=pod |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 NAME |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
MongoX - DSL sugar for MongoDB |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 VERSION |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
version 0.05 |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 SYNOPSIS |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# quick bootstrap, add connection and switch to db:'test' |
85
|
|
|
|
|
|
|
use MongoX ( host => 'mongodb://127.0.0.1',db => 'test' ); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# common way |
88
|
|
|
|
|
|
|
use MongoX; |
89
|
|
|
|
|
|
|
#register default connection; |
90
|
|
|
|
|
|
|
add_connection host => '127.0.0.1'; |
91
|
|
|
|
|
|
|
# switch to default connection; |
92
|
|
|
|
|
|
|
use_connection; |
93
|
|
|
|
|
|
|
# use database 'test' |
94
|
|
|
|
|
|
|
use_db 'test'; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
#add/register another connection with id "remote2" |
97
|
|
|
|
|
|
|
add_connection host => '192.168.1.1',id => 'remote2'; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# switch to this connection |
100
|
|
|
|
|
|
|
use_connection 'remote2'; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#get a collection object (from the db in current context) |
103
|
|
|
|
|
|
|
my $foo = get_collection 'foo'; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# use 'foo' as default context collection |
106
|
|
|
|
|
|
|
use_collection 'foo'; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# use context's db/collection |
109
|
|
|
|
|
|
|
say 'total rows:',context_collection->count(); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $id = context_collection->insert({ name => 'Pan', home => 'Beijing' }); |
112
|
|
|
|
|
|
|
my $gridfs = context_db->get_gridfs; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# loop dbs/collections |
115
|
|
|
|
|
|
|
for_dbs{ |
116
|
|
|
|
|
|
|
for_collections { |
117
|
|
|
|
|
|
|
db_ensure_index {created_on => 1}; |
118
|
|
|
|
|
|
|
} context_db->collection_names; |
119
|
|
|
|
|
|
|
} 'db1','db2'; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 DESCRIPTION |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
MongoX is a light wrapper to L<MongoDB> driver, it provide a very simple but handy DSL syntax. |
124
|
|
|
|
|
|
|
It also will provide some usefull helpers like builtin mongoshell, you can quick work with MongoDB. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 OVERVIEW |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
MongoX takes a set of options for the class construction at compile time |
129
|
|
|
|
|
|
|
as a HASH parameter to the "use" line. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
As a convenience, you can pass the default connection parameters and default database, |
132
|
|
|
|
|
|
|
then when MongoX import, it will apply these options to L</add_connection> and L</use_db>, |
133
|
|
|
|
|
|
|
so the following code: |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
use MongoX ( host => 'mongodb://127.0.0.1',db => 'test' ); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
is equivalent to: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
use MongoX; |
140
|
|
|
|
|
|
|
add_connection host => 'mongodb://127.0.0.1'; |
141
|
|
|
|
|
|
|
use_connection; |
142
|
|
|
|
|
|
|
use_db 'test'; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
C<context_connection>,C<context_db>, C<context_collection> are implicit MongoDB::Connection, |
145
|
|
|
|
|
|
|
MongoDB::Database and MongoDB::Collection. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 Options |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=over |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item host => mongodb server, mongodb connection scheme |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item db => default database |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item utf8 => Turn on/off UTF8 flag. default is turn on utf8 flag. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=back |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 DSL keywords |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=over |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item use_connection |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item use_db |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item use_collection |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=item with_context |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=back |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
use_* keywords can make/switch implicit MongoDB object in context. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
with_context allow build a sanbox to execute code block and do something, |
176
|
|
|
|
|
|
|
the context be restored when out the block. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=over |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item for_connections |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item for_dbs |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item for_collections |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=back |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
These are loop keywords, it will switch related context object in the given list and loop run |
189
|
|
|
|
|
|
|
the code block. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 context_db |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
my $db = context_db; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Return current L<MongoDB::Database> object in context; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 context_connection |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $con = context_connection; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Return current L<MongoDB::Connection> object in context. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 context_collection |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my $col = context_collection; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Return current L<MongoDB::Collection> object in context, you can replace the object |
210
|
|
|
|
|
|
|
with L</use_collection>. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 METHODS |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 use_connection |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# create a default connection |
217
|
|
|
|
|
|
|
use_connection; |
218
|
|
|
|
|
|
|
# use another connection with id:'con2' |
219
|
|
|
|
|
|
|
use_connection 'con2'; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Switch to given connection, set the context connection to this connection. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 use_db |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
use_db 'foo'; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Switch to the database, set the context database to this database; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 use_collection |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
use_collection 'user' |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Set 'user' collection as context collection. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head2 add_connection |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
add_connection id => 'default', host => 'mongodb://127.0.0.1:27017' |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Register a connnection with the id, if omit, will add as default connection. |
240
|
|
|
|
|
|
|
All options exclude 'id' will direct pass to L<MongoDB::Connection>. |
241
|
|
|
|
|
|
|
The host accept standard mongoDB uri scheme: |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database] |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
More about, see L<http://www.mongodb.org/display/DOCS/Connections>. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 boot |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
boot host => 'mongodb://127.0.0.1',db => 'test' |
250
|
|
|
|
|
|
|
# same as: |
251
|
|
|
|
|
|
|
add_connection host => 'mongodb://127.0.0.1', id => 'default'; |
252
|
|
|
|
|
|
|
use_connection; |
253
|
|
|
|
|
|
|
use_db 'test'; |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Boot is equivalent to call add_connection,use_connection,use_db. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head2 with_context BLOCK db => 'dbname', connection => 'connection_id', collection => 'foo' |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
# sandbox |
260
|
|
|
|
|
|
|
use_db 'test'; |
261
|
|
|
|
|
|
|
with_context { |
262
|
|
|
|
|
|
|
use_db 'tmp_db'; |
263
|
|
|
|
|
|
|
# now context db is 'tmp_db' |
264
|
|
|
|
|
|
|
... |
265
|
|
|
|
|
|
|
}; |
266
|
|
|
|
|
|
|
# context db auto restor to 'test' |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# temp context |
269
|
|
|
|
|
|
|
with_context { |
270
|
|
|
|
|
|
|
context_collection->do_something; |
271
|
|
|
|
|
|
|
} connection => 'id2', db => 'test2', 'collection' => 'user'; |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# alternate style |
274
|
|
|
|
|
|
|
my $db2 = context_connection->get_database('test2'); |
275
|
|
|
|
|
|
|
with_context { |
276
|
|
|
|
|
|
|
# context db is $db2,collection is 'foo' |
277
|
|
|
|
|
|
|
print context_collection->count; |
278
|
|
|
|
|
|
|
} db => $db2, 'collection' => 'foo'; |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
C<with_context> let you create a temporary context(sandbox) to invoke the code block. |
281
|
|
|
|
|
|
|
Before execute the code block, current context will be saved, them build a temporary |
282
|
|
|
|
|
|
|
context to invoke the code, after code executed, saved context will be restored. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
You can explicit setup the sandbox context include connection,db,collection, |
285
|
|
|
|
|
|
|
or just applied from parent container(context). |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
with_context allow nested, any with_context will build its context sanbox to run |
288
|
|
|
|
|
|
|
the attached code block. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
use_db 'test'; |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
with_context { |
293
|
|
|
|
|
|
|
# context db is 'db1' |
294
|
|
|
|
|
|
|
with_context { |
295
|
|
|
|
|
|
|
# context db is 'db2' |
296
|
|
|
|
|
|
|
} db => 'db2'; |
297
|
|
|
|
|
|
|
# context db restore to 'db1' |
298
|
|
|
|
|
|
|
} db => 'db1'; |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
# context db restore to 'test' |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
with_context options key: |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=over |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=item connection => connection id or L<MongoDB::Connection> |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=item db => database name or L<MongoDB::Database> |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item collection => collection name or L<MongoDB::Collection> |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=back |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=head2 for_dbs BLOCK, database List |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
for_dbs { |
317
|
|
|
|
|
|
|
print context_db->name; |
318
|
|
|
|
|
|
|
} 'test1','test2','test3; |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
for_dbs { |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
print context_db->name; |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
} context_connection->database_names; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
Evaluates the code BLOCK for each database of the list. In block scope, context_db will |
327
|
|
|
|
|
|
|
switch to the list value, and $_ is alias of this context_db value. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=head2 for_connections BLOCK connection_id_list |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
for_connections { |
332
|
|
|
|
|
|
|
for_dbs { map { print $_ } context_db->collection_names } $_->database_names; |
333
|
|
|
|
|
|
|
} 'con_id1', 'con_id2' |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
Evaluates the code BLOCK against each connection of connection id list. In block scope, |
336
|
|
|
|
|
|
|
context_connection will switch to the list value, and $_ is alais of the current context_connection. |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=head2 for_collections BLOCK collection_list |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
# print out all collection's count in the db |
341
|
|
|
|
|
|
|
for_collections { |
342
|
|
|
|
|
|
|
say $_->name, ' count:', db_count; |
343
|
|
|
|
|
|
|
} context_db->collection_names; |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
# reindex some collections |
346
|
|
|
|
|
|
|
for_collections { db_re_index } 'foo','foo2','foo3'; |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
# alternate |
349
|
|
|
|
|
|
|
for_collections { db_re_index } qw(foo foo2 foo3); |
350
|
|
|
|
|
|
|
# alternate |
351
|
|
|
|
|
|
|
for_collections { db_re_index } ('foo','foo2','foo3'); |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
=head1 Repository |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
Github: L<http://github.com/nightsailer/mongo-x> |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
=head1 SEE ALSO |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
MongoDB manual: L<http://www.mongodb.org/display/DOCS/Manual> |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
Official MongoDB driver: L<MongoDB> or L<http://github.com/mongodb/mongo-perl-driver> |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
My fork: L<http://github.com/nightsailer/mongo-perl-driver> |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
My blog about this project (Chinese only!): L<http://nightsailer.com/mongox> |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=head1 AUTHOR |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
Pan Fan(nightsailer) <nightsailer at gmail dot com> |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Pan Fan(nightsailer). |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
376
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=cut |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
__END__ |
382
|
|
|
|
|
|
|
|