line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MongoX::Context; |
2
|
|
|
|
|
|
|
# ABSTRACT: Implements DSL interface,context container. |
3
|
5
|
|
|
5
|
|
23804
|
use strict; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
218
|
|
4
|
5
|
|
|
5
|
|
27
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
151
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
26
|
use Carp 'croak'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
354
|
|
7
|
5
|
|
|
5
|
|
7830
|
use MongoDB; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Data::Dumper; |
9
|
|
|
|
|
|
|
my %registry = (); |
10
|
|
|
|
|
|
|
my %connection_pool = (); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# context |
13
|
|
|
|
|
|
|
our ($_context_connection,$_context_db,$_context_collection); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub get_connection { |
16
|
|
|
|
|
|
|
my ($id) = @_; |
17
|
|
|
|
|
|
|
$id ||= 'default'; |
18
|
|
|
|
|
|
|
if (exists $connection_pool{$id}) { |
19
|
|
|
|
|
|
|
return $connection_pool{$id}; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
croak "connection_id:$id not exists in registry,forgot to add it?(add_connection)" unless exists $registry{$id}; |
22
|
|
|
|
|
|
|
my $new_con = MongoDB::Connection->new(%{ $registry{$id} }); |
23
|
|
|
|
|
|
|
$connection_pool{$id} = $new_con; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub get_db { |
27
|
|
|
|
|
|
|
my ($dbname,$connection_id) = @_; |
28
|
|
|
|
|
|
|
if ($connection_id) { |
29
|
|
|
|
|
|
|
return get_connection($connection_id)->get_database($dbname); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
return $_context_connection->get_database($dbname); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub use_db { |
35
|
|
|
|
|
|
|
$_context_db = $_context_connection->get_database(shift); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub add_connection { |
39
|
|
|
|
|
|
|
my (%opts) = @_; |
40
|
|
|
|
|
|
|
my $id = $opts{id} || 'default'; |
41
|
|
|
|
|
|
|
$registry{$id} = { @_ }; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub use_connection { |
45
|
|
|
|
|
|
|
my ($id) = @_; |
46
|
|
|
|
|
|
|
$id ||= 'default'; |
47
|
|
|
|
|
|
|
$_context_connection = get_connection($id); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub use_collection { |
51
|
|
|
|
|
|
|
my ($collection_name) = @_; |
52
|
|
|
|
|
|
|
$_context_collection = $_context_db->get_collection($collection_name); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub get_collection { |
56
|
|
|
|
|
|
|
my ($collection_name) = @_; |
57
|
|
|
|
|
|
|
$_context_db->get_collection($collection_name); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub context_db { $_context_db } |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub context_connection { $_context_connection } |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub context_collection { $_context_collection } |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub boot { |
68
|
|
|
|
|
|
|
my (%opts) = @_; |
69
|
|
|
|
|
|
|
return unless %opts; |
70
|
|
|
|
|
|
|
$MongoDB::BSON::utf8_flag_on = $opts{utf8} ? 1 : 0 if exists $opts{utf8}; |
71
|
|
|
|
|
|
|
add_connection(%opts); |
72
|
|
|
|
|
|
|
use_connection; |
73
|
|
|
|
|
|
|
use_db($opts{db}) if exists $opts{db}; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub reset { |
77
|
|
|
|
|
|
|
($_context_connection,$_context_collection,$_context_db) = undef; |
78
|
|
|
|
|
|
|
%registry = (); |
79
|
|
|
|
|
|
|
%connection_pool = (); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub with_context { |
83
|
|
|
|
|
|
|
local ($_context_connection,$_context_db,$_context_collection) = ($_context_connection,$_context_db,$_context_collection); |
84
|
|
|
|
|
|
|
if (@_ == 1) { |
85
|
|
|
|
|
|
|
return $_[0]->(); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
my $code = shift; |
88
|
|
|
|
|
|
|
my %new_context = @_; |
89
|
|
|
|
|
|
|
if ($new_context{connection}) { |
90
|
|
|
|
|
|
|
if (ref $new_context{connection} eq 'MongoDB::Connection') { |
91
|
|
|
|
|
|
|
$_context_db = $new_context{connection}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
|
|
|
|
|
|
use_connection $new_context{connection}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
if ($new_context{db}) { |
98
|
|
|
|
|
|
|
if (ref $new_context{db} eq 'MongoDB::Database') { |
99
|
|
|
|
|
|
|
$_context_db = $new_context{db}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
else { |
102
|
|
|
|
|
|
|
use_db $new_context{db}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
if ($new_context{collection}) { |
106
|
|
|
|
|
|
|
if (ref $new_context{collection} eq 'MongoDB::Collection') { |
107
|
|
|
|
|
|
|
$_context_collection = $new_context{collection}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
|
|
|
|
|
|
use_collection $new_context{collection}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
$code->(); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub for_collections { |
117
|
|
|
|
|
|
|
my ($code,@cols) = @_; |
118
|
|
|
|
|
|
|
for my $col (@cols){ |
119
|
|
|
|
|
|
|
local ($_context_connection,$_context_db,$_context_collection) = ($_context_connection,$_context_db,$_context_collection); |
120
|
|
|
|
|
|
|
use_collection $col; |
121
|
|
|
|
|
|
|
$code->($_context_collection); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub for_dbs { |
126
|
|
|
|
|
|
|
my ($code,@dbs) = @_; |
127
|
|
|
|
|
|
|
for my $db (@dbs) { |
128
|
|
|
|
|
|
|
local ($_context_connection,$_context_db,$_context_collection) = ($_context_connection,$_context_db,$_context_collection); |
129
|
|
|
|
|
|
|
use_db $db; |
130
|
|
|
|
|
|
|
$code->($_context_db); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub for_connections { |
135
|
|
|
|
|
|
|
my ($code,@connections) = @_; |
136
|
|
|
|
|
|
|
for my $con_id (@connections){ |
137
|
|
|
|
|
|
|
local ($_context_connection,$_context_db,$_context_collection) = ($_context_connection,$_context_db,$_context_collection); |
138
|
|
|
|
|
|
|
use_connection $con_id; |
139
|
|
|
|
|
|
|
$code->($_context_collection); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=pod |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 NAME |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
MongoX::Context - Implements DSL interface,context container. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 VERSION |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
version 0.05 |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 SYNOPSIS |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
use MongoX::Context; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
MongoX::Context::add_connection host => 'mongodb:://127.0.0.1'; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
MongoX::Context::use_connection; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
MongoX::Context::use_db 'test'; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
MongoX::Context::reset; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
MongoX::Context::boot host => 'mongodb://127.0.0.1',db => 'test2'; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $col2 = MongoX::Context::context_db->get_collection('foo2'); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 DESCRIPTION |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
MongoX::Context implements the DSL syntax, track and hold internal MongoDB related objects. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 AUTHOR |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Pan Fan(nightsailer) <nightsailer at gmail dot com> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Pan Fan(nightsailer). |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
185
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
__END__ |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|