MPMinus/Store/MultiStore.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 126 SCAR 47.0
CC
10
Cov
0%
new
20.0
set
12.0
AUTOLOAD
12.0
CRAP 110.0
1 package MPMinus::Store::MultiStore; # $Id: MultiStore.pm 143 2013-05-21 09:13:44Z minus $
2 use strict;
3
4 =head1 NAME
5
6 MPMinus::Store::MultiStore - Multistoring
7
8 =head1 VERSION
9
10 Version 1.04
11
12 =head1 SYNOPSIS
13
14 use MPMinus::Store::MultiStore;
15
16 # Multistoring
17 my $mso = new MPMinus::Store::MultiStore (
18 -m => $m, # OPTIONAL
19 -mso => {
20
21 foo => {
22 -dsn => 'DBI:mysql:database=TEST;host=192.168.1.1',
23 -user => 'login',
24 -pass => 'password',
25 -attr => {
26 mysql_enable_utf8 => 1,
27 RaiseError => 0,
28 PrintError => 0,
29 },
30 },
31
32 bar => {
33 -dsn => 'DBI:Oracle:SID',
34 -user => 'login',
35 -pass => 'password',
36 -attr => {
37 RaiseError => 0,
38 PrintError => 0,
39 },
40 },
41 },
42 );
43
44 my @stores = $mso->stores; # foo, bar
45
46 $mso->set(baz => new MPMinus::Store::DBI( {
47 -dsn => 'DBI:Oracle:BAZSID',
48 -user => 'login',
49 -pass => 'password',
50 -attr => {
51 RaiseError => 0,
52 PrintError => 0,
53 },
54 })
55 );
56
57 my @stores = $mso->stores; # foo, bar, baz
58
59 my $foo = $mso->get('foo');
60 my $foo = $mso->store('foo');
61 my $foo = $mso->foo;
62
63 =head1 DESCRIPTION
64
65 Multistoring Database independent interface for MPMinus on MPMinus::Store::DBI based.
66
67 See L<MPMinus::Store::DBI>
68
69 =head1 METHODS
70
71 =over 8
72
73 =item B<get, store>
74
75 my $foo = $mso->get('foo');
76 my $foo = $mso->store('foo');
77 my $foo = $mso->foo;
78
79 Getting specified connection by name
80
81 =item B<set>
82
83 $mso->set(baz => new MPMinus::Store::DBI( {
84 -dsn => 'DBI:Oracle:BAZSID',
85 -user => 'login',
86 -pass => 'password',
87 -attr => {
88 RaiseError => 0,
89 PrintError => 0,
90 },
91 })
92 );
93
94 Setting specified connection by name and returns state of operation
95
96 =item B<stores>
97
98 my @stores = $mso->stores; # foo, bar, baz
99
100 Returns current connections as list (array)
101
102 =back
103
104 =head1 EXAMPLE
105
106 package MPM::foo::Handlers;
107 use strict;
108
109 use MPMinus::Store::MultiStore;
110 use MPMinus::MainTools qw/ msoconf2args /;
111
112 sub InitHandler {
113 my $pkg = shift;
114 my $m = shift;
115
116 # MSO Database Nodes
117 if ($m->multistore) {
118 my $mso = $m->multistore;
119 foreach ($mso->stores) {
120 $mso->get($_)->reconnect unless $mso->get($_)->ping;
121 }
122 } else {
123 $m->set( multistore => new MPMinus::Store::MultiStore (
124 -m => $m,
125 -mso => { msoconf2args($m->conf('store')) },
126 )
127 );
128 }
129
130 return __PACKAGE__->SUPER::InitHandler($m);
131 }
132
133 ...
134
135 package MPM::foo::Test;
136 use strict;
137
138 sub response {
139 my $m = shift;
140
141 my $mso = $m->multistore;
142
143 my $data = $mso->foo->errstr
144 ? $mso->foo->errstr
145 : $mso->foo->field('select sysdate() from dual');
146
147 ...
148
149 return Apache2::Const::OK;
150 }
151
152 In conf/mso.conf file:
153
154 <store foo>
155 dsn DBI:mysql:database=TEST;host=192.168.1.1
156 user login
157 pass password
158 <Attr>
159 mysql_enable_utf8 1
160 RaiseError 0
161 PrintError 0
162 </Attr>
163 </store>
164 <store bar>
165 dsn DBI:Oracle:FOOSID
166 user login
167 pass password
168 connect_to 10
169 request_to 50
170 <Attr>
171 RaiseError 0
172 PrintError 0
173 </Attr>
174 </store>
175 <store baz>
176 dsn DBI:Oracle:BARSID
177 user login
178 pass password
179 <Attr>
180 RaiseError 0
181 PrintError 0
182 </Attr>
183 </store>
184
185 =head1 HISTORY
186
187 =over 8
188
189 =item B<1.00 / 13.11.2010>
190
191 Init version
192
193 =item B<1.01 / 22.12.2010>
194
195 Added method for getting list of stores
196
197 =item B<1.02 / Wed Apr 24 14:53:38 2013 MSK>
198
199 General refactoring
200
201 =back
202
203 =head1 SEE ALSO
204
205 L<MPMinus::Store::DBI>, L<CTK::DBI>, L<Apache::DBI>, L<DBI>
206
207 =head1 AUTHOR
208
209 Serz Minus (Lepenkov Sergey) L<http://serzik.ru> E<lt>minus@mail333.comE<gt>
210
211 =head1 COPYRIGHT
212
213 Copyright (C) 1998-2013 D&D Corporation. All Rights Reserved
214
215 =head1 LICENSE
216
217 This program is free software: you can redistribute it and/or modify
218 it under the terms of the GNU General Public License as published by
219 the Free Software Foundation, either version 3 of the License, or
220 (at your option) any later version.
221
222 This program is distributed in the hope that it will be useful,
223 but WITHOUT ANY WARRANTY; without even the implied warranty of
224 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
225 GNU General Public License for more details.
226
227 See C<LICENSE> file
228
229 =cut
230
231 use vars qw($VERSION);
232 $VERSION = 1.04;
233
234 use MPMinus::Store::DBI;
235 use CTK::Util qw/ :API /;
236
237 sub new {
238 my $class = shift;
239 my @in = read_attributes([
240 ['M', 'GLOBAL', 'GLOB', 'MPMINUS', 'MPM'],
241 ['CONFIG','MSCONFIG','MSO','CONF','DATA','MULTISTORE','STORES'],
242 ],@_);
243 my $m = $in[0];
244 my $s = $in[1] || {};
245 unless ($s && ref($s) eq 'HASH') {
246 $s = {};
247 # Òóò ÷èòàåì äàííûå êîíôèãóðàöèè (íà ïåðñïåêòèâó)
248 # $s = ...
249 }
250 my @stores = keys %$s; # Ïðèíèìàåì çíà÷åíèÿ âñåõ ñîåäèíåíèé
251
252 # ïðîáåãàåìñÿ ïî âñåì ñîåäèíåíèÿì è óñòàíàâëèâàåì èõ â îáùèé ìàññèâ
253 my $ret = {};
254 foreach my $store (@stores) {
255 my $sc = $s->{$store};
256 if ($sc && ref($sc) eq 'HASH') {
257 $sc->{-m} = $m if $m;
258 $ret->{$store} = new MPMinus::Store::DBI(%$sc);
259 } else {
260 $ret->{$store} = undef;
261 }
262 }
263
264 return bless {
265 m => $m,
266 s => {%$s},
267 stores => $ret,
268 }, $class;
269 }
270 sub stores {
271 # Âîçâðàò ñïèñêà êîííåêòîâ
272 my $self = shift;
273 my $stores = $self->{stores};
274 return ($stores && ref($stores) eq 'HASH') ? keys(%$stores) : ();
275 }
276 sub get {
277 # Âîçâðàò êîíêðåòíîãî ñîåäèíåíèÿ
278 my $self = shift;
279 my $name = shift;
280 if ($name && $self->{stores} && $self->{stores}->{$name}) {
281 return $self->{stores}->{$name};
282 } else {
283 carp("Can't find store \"$name\"");
284 }
285 return undef;
286 }
287 sub store { goto &get };
288 sub set {
289 # Óñòàíîâêà êîíêðåòíîãî ñîåäèíåíèÿ
290 my $self = shift;
291 my $name = shift;
292 my $value = shift;
293 carp("Key name undefined") && return undef unless $name;
294 carp("Value incorrect or is't MPMinus::Store::DBI object") && return undef unless $value && ref($value) eq 'MPMinus::Store::DBI';
295
296 $self->{stores}->{$name} = $value;
297 }
298 sub AUTOLOAD {
299 my $self = shift;
300 our $AUTOLOAD;
301 my $AL = $AUTOLOAD;
302 my $ss = undef;
303 $ss = $1 if $AL=~/\:\:([^\:]+)$/;
304
305 if ($ss && $self->{stores} && $self->{stores}->{$ss}) {
306 return $self->{stores}->{$ss};
307 } else {
308 carp("Can't find store \"$ss\"");
309 }
310 return undef;
311 }
312 sub DESTROY {
313 my $self = shift;
314 undef $self;
315 }
316
317 1;