MLite/Store/DBI.pm

filter: Statementstmt 62.2% 56 / 90 Branchbran 23.6% 18 / 76 Conditioncond 29.7% 14 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 61.5% 8 / 13 Podpod 100% 6 / 6 totaltotal 39.2% 102 / 260 CC 58 SCAR 44.2
CC
58
Cov
41%
new
153.7
ping
42.0
reconnect
30.0
CRAP 83.1
« MLite/Transaction.pm ↑ Summary MLite/Config.pm »
1 package WWW::MLite::Store::DBI; # $Id: DBI.pm 15 2014-06-04 06:24:25Z minus $
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 =head1 NAME
5
6 WWW::MLite::Store::DBI - Database independent interface for WWW::MLite on CTK::DBI based
7
8 =head1 VERSION
9
10 Version 1.01
11
12 =head1 SYNOPSIS
13
14 use WWW::MLite::Store::DBI;
15
16 # MySQL connect
17 my $mysql = new WWW::MLite::Store::DBI (
18 -mlite => $mlite, # OPTIONAL
19 -dsn => 'DBI:mysql:database=TEST;host=192.168.1.1',
20 -user => 'login',
21 -pass => 'password',
22 -connect_to => 5,
23 -request_to => 60
24 -attr => {
25 mysql_enable_utf8 => 1,
26 RaiseError => 0,
27 PrintError => 0,
28 },
29 ); # See CTK::DBI
30
31 # MySQL connect (old style, without DSN)
32 my $mysql = new WWW::MLite::Store::DBI (
33 -mlite => $mlite, # OPTIONAL
34
35 -driver => 'mysql', # Driver name. See DBI module
36 # Available drivers:
37 # CSV, DBM, ExampleP, File, Gofer, ODBC, Oracle,
38 # Pg, Proxy, SQLite, Sponge, mysql
39 -host => '192.168.1.1',
40 -port => '3306', # default
41 -database => 'TEST',
42
43 -user => 'login',
44 -pass => 'password',
45 -attr => {
46 mysql_enable_utf8 => 1,
47 RaiseError => 0,
48 PrintError => 0,
49 },
50 );
51
52 my $dbh = $mysql->connect;
53
54 my $pingstat = $mysql->ping if $mysql;
55
56 $mysql->reconnect() unless $pingstat;
57
58 # Table select (as array)
59 my @result = $mysql->table($sql, @inargs);
60
61 # Table select (as hash)
62 my %result = $mysql->tableh($key, $sql, @inargs); # $key - primary index field name
63
64 # Record (as array)
65 my @result = $mysql->record($sql, @inargs);
66
67 # Record (as hash)
68 my %result = $mysql->recordh($sql, @inargs);
69
70 # Fiels (as scalar)
71 my $result = $mysql->field($sql, @inargs);
72
73 # SQL/PL-SQL
74 my $sth = $mysql->execute($sql, @inargs);
75 ...
76 $sth->finish;
77
78 =head1 DESCRIPTION
79
80 Database independent interface for WWW::MLite on CTK::DBI based.
81
82 =head2 DEBUG
83
84 Set $WWW::MLite::Store::DBI::DEBUG_FORCE = 1 for enable debugging in STDERR where object $mlite undefined
85
86 Coming soon
87
88 =head1 METHODS
89
90 =over 8
91
92 =item B<ping>
93
94 my $status = $mysql->ping();
95
96 Returns connection's life status
97
98 =item B<reconnect>
99
100 $mysql->reconnect unless $mysql->ping();
101
102 =item B<err, errstr, state>
103
104 my $err = $mysql->err;
105 my $errstr = $mysql->errstr;
106 my $state = $mysql->state;
107
108 Methods returns DBI values: err, errstr and state.
109
110 See L<DBI/"METHODS_COMMON_TO_ALL_HANDLES">
111
112 =back
113
114 =head1 EXAMPLES
115
116 =over 8
117
118 =item B<Example 1>
119
120 use WWW::MLite::Store::DBI;
121 # eval 'sub CTK::DBI::_error {1}'; # For supressing CTK::DBI errors
122
123 my $mysql => new WWW::MLite::Store::DBI (
124 -mlite => $mlite,
125 -dsn => 'DBI:mysql:database=NAME;host=HOST',
126 -user => 'USER',
127 -pass => 'PASSWORD',
128 -attr => {
129 mysql_enable_utf8 => 1,
130 RaiseError => 0,
131 PrintError => 0,
132 HandleError => sub { $m->log_error(shift || '') },
133 },
134 )
135
136 ...
137
138 my @data = $mysql->table('select * from table');
139
140 =item B<Example 2: with reconnection>
141
142 use WWW::MLite::Store::DBI;
143
144 my $mysql => new WWW::MLite::Store::DBI (
145 -mlite => $mlite, # OPTIONAL
146 -dsn => 'DBI:mysql:database=NAME;host=HOST',
147 -user => 'USER',
148 -pass => 'PASSWORD',
149 -attr => {
150 mysql_enable_utf8 => 1,
151 RaiseError => 0,
152 PrintError => 0,
153 HandleError => sub { $m->log_error(shift || '') },
154 },
155 )
156
157 ...
158
159 $mysql->reconnect unless $mysql->ping;
160
161 ...
162
163 my @data = $mysql->table('select * from table');
164
165 =item B<Example 3: Oracle>
166
167 # Oracle connect
168 my $oracle = new WWW::MLite::Store::DBI (
169 -mlite => $mlite, # OPTIONAL
170 -driver => 'Oracle',
171 -host => '192.168.1.1',
172 -database => 'TEST',
173 -user => 'login',
174 -pass => 'password',
175 -attr => {
176 RaiseError => 0,
177 PrintError => 0,
178 },
179 )
180
181 ...
182
183 my $value = mysql->field('select sysdate from dual');
184
185
186 =item B<Simple example>
187
188 use WWW::MLite::Store::DBI;
189
190 $WWW::MLite::Store::DBI::DEBUG_FORCE = 1;
191 my $dbi = new WWW::MLite::Store::DBI (
192 -driver => 'mysql',
193 -name => 'mylocaldb',
194 -user => 'user',
195 -password => 'password'
196 );
197 ...
198 my @table = $dbi->table("select * from tablename where date = ?", "01.01.2000");
199
200 =item B<Sponge example>
201
202 use WWW::MLite::Store::DBI;
203
204 $WWW::MLite::Store::DBI::DEBUG_FORCE = 1;
205 my $o = new WWW::MLite::Store::DBI(
206 -driver => 'Sponge',
207 -attr => { RaiseError => 1 },
208 );
209 my $dbh = $o->connect();
210 my $sth = $dbh->prepare("select * from table", {
211 rows => [
212 [qw/foo bar baz/],
213 [qw/qux quux corge/],
214 [qw/grault garply waldo/],
215 ],
216 NAME => [qw/h1 h2 h3/],
217 });
218
219 $sth->execute();
220 my $result = $sth->fetchall_arrayref;
221 $sth->finish;
222 print Dumper($result);
223
224 =back
225
226 =head1 HISTORY
227
228 See C<CHANGES> file
229
230 =head1 SEE ALSO
231
232 L<CTK::DBI>, L<DBI>
233
234 =head1 AUTHOR
235
236 Serz Minus (Lepenkov Sergey) L<http://www.serzik.com> E<lt>minus@mail333.comE<gt>
237
238 =head1 COPYRIGHT
239
240 Copyright (C) 1998-2014 D&D Corporation. All Rights Reserved
241
242 =head1 LICENSE
243
244 This program is free software: you can redistribute it and/or modify
245 it under the terms of the GNU General Public License as published by
246 the Free Software Foundation, either version 3 of the License, or
247 (at your option) any later version.
248
249 This program is distributed in the hope that it will be useful,
250 but WITHOUT ANY WARRANTY; without even the implied warranty of
251 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
252 GNU General Public License for more details.
253
254 See C<LICENSE> file
255
256 =cut
257
258 1 ▶use vars qw($VERSION $DEBUG_FORCE);
Subroutine BEGIN: called CC=1 SCAR=0.0
259 1 $VERSION = '1.01';
260
261 use constant {
262 1 ATTR_NAMES => [
263 ['M', 'MLITE', 'GLOBAL', 'GLOB', 'OBJECT'], # 0
264 ['DSN','STRING','STR'], # 1
265 ['HOST','HOSTNAME','SERVER','SERVERNAME','ADDRESS','ADDR','SERVERADDR'], # 2
266 ['DB','BD','DBNAME','DATABASE','NAME','DATABASENAME'], # 3
267 ['PORT',], # 4
268 ['USER','USERNAME','LOGIN'], # 5
269 ['PASSWORD','PASS'], # 6
270 ['DRIVER','DRIVERNAME'], # 7
271 ['TIMEOUT_CONNECT','CONNECT_TIMEOUT','CNT_TIMEOUT','TIMEOUT_CNT','TO_CONNECT','CONNECT_TO'],# 8
272 ['TIMEOUT_REQUEST','REQUEST_TIMEOUT','REQ_TIMEOUT','TIMEOUT_REQ','TO_REQUEST','REQUEST_TO'],# 9
273 ['ATTRIBUTES','ATTR','ATTRHASH','PARAMS'], # 10
274 ],
275 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
276
277 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 1 ▶use base qw/CTK::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
279 1 ▶use CTK::Util qw/ :API /;
Subroutine BEGIN: called CC=1 SCAR=0.0
280
281 sub new {
282 1 ▶ my $class = shift;
Subroutine new: called CC=33 SCAR=50.4
Pod documented
283 1 my @in = read_attributes(ATTR_NAMES,@_);
284
285 # Îñíîâíûå àòðèáóòû ñîåäèíåíèÿ MySQL
286 1 my $m = $in[0];
287 1 ▶ my $dsn = $in[1] || '';
Condition50%1/2
$in[1] || ''
l!l
01
MC/DC0%0/1
$in[1] || ''
$in[1]
Truth table50%1/2
A
A= $in[1]
Aresult
11
00
288 1 ▶ my $host = $in[2] || '';
Condition50%1/2
$in[2] || ''
l!l
01
MC/DC0%0/1
$in[2] || ''
$in[2]
Truth table50%1/2
A
A= $in[2]
Aresult
11
00
289 1 ▶ my $db = $in[3] || '';
Condition50%1/2
$in[3] || ''
l!l
01
MC/DC0%0/1
$in[3] || ''
$in[3]
Truth table50%1/2
A
A= $in[3]
Aresult
11
00
290 1 ▶ my $port = $in[4] || '';
Condition50%1/2
$in[4] || ''
l!l
01
MC/DC0%0/1
$in[4] || ''
$in[4]
Truth table50%1/2
A
A= $in[4]
Aresult
11
00
291 1 ▶ my $user = $in[5] || '';
Condition50%1/2
$in[5] || ''
l!l
01
MC/DC0%0/1
$in[5] || ''
$in[5]
Truth table50%1/2
A
A= $in[5]
Aresult
11
00
292 1 ▶ my $pass = $in[6] || '';
Condition50%1/2
$in[6] || ''
l!l
01
MC/DC0%0/1
$in[6] || ''
$in[6]
Truth table50%1/2
A
A= $in[6]
Aresult
11
00
293 1 ▶ my $driver = $in[7] || '';
Condition50%1/2
$in[7] || ''
l!l
10
MC/DC0%0/1
$in[7] || ''
$in[7]
Truth table50%1/2
A
A= $in[7]
Aresult
11
00
294 1 ▶ my $toc = $in[8] || 0;
Condition50%1/2
$in[8] || 0
l!l
01
MC/DC0%0/1
$in[8] || 0
$in[8]
Truth table50%1/2
A
A= $in[8]
Aresult
11
00
295 1 ▶ my $tor = $in[9] || 0;
Condition50%1/2
$in[9] || 0
l!l
01
MC/DC0%0/1
$in[9] || 0
$in[9]
Truth table50%1/2
A
A= $in[9]
Aresult
11
00
296 1 ▶ my $attr = $in[10] || undef;
Condition50%1/2
$in[10] || undef
l!l
10
MC/DC0%0/1
$in[10] || undef
$in[10]
Truth table50%1/2
A
A= $in[10]
Aresult
11
00
297
298 1 ▶ unless ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dsn) 1 0 1
299 1 my @adrivers = DBI->available_drivers();
300 1 ▶ if (grep {$driver eq $_} @adrivers) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$driver eq $_;} @adrivers) { } 1 0 1
301 1 ▶ if ($driver =~ /mysql/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($driver =~ /mysql/i) { } 0 1 1
elsif ($driver =~ /Oracle/i) { } 0 1 1
302 0 ▶ $dsn = "DBI:mysql:database=$db".($host?";host=$host":'').($port?";port=$port":'');
Branch: 2 branches
BranchTrueFalseTotal
$host ? : 0 0 0
$port ? : 0 0 0
303 } elsif ($driver =~ /Oracle/i) {
304 0 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) { } 0 0 0
305 0 ▶ $dsn = "DBI:Oracle:host=$host".($db?";service_name=$db":'').($port?";port=$port":'');
Branch: 2 branches
BranchTrueFalseTotal
$db ? : 0 0 0
$port ? : 0 0 0
306 } else {
307 0 ▶ $dsn = "DBI:Oracle:".($db?"$db":'').($port?";port=$port":'');
Branch: 2 branches
BranchTrueFalseTotal
$db ? : 0 0 0
$port ? : 0 0 0
308 }
309 } else {
310 # dbi:DriverName:database=database_name;host=hostname;port=port
311 1 ▶ $dsn = "DBI:".$driver.":"
Branch: 3 branches
BranchTrueFalseTotal
$db ? : 0 1 1
$host ? : 0 1 1
$port ? : 0 1 1
312 .($db?"database=$db":'')
313 .($host?";host=$host":'')
314 .($port?";port=$port":'');
315 }
316 } else {
317 0 carp("Driver \"$driver\" not availebled. Available drivers: ",join(", ",@adrivers));
318 }
319 }
320 1 my %args = (
321 -dsn => $dsn,
322 -user => $user,
323 -pass => $pass,
324 -timeout_connect => $toc,
325 -timeout_request => $tor,
326 -attr => $attr,
327 );
328
329 1 ▶ if ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn) { } 1 0 1
330 1 my $obj = $class->SUPER::new(%args);
331 1 ▶ $obj = bless({}, $class) unless $obj && ref($obj) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj and ref $obj eq __PACKAGE__ 0 1 1
Condition33%1/3
$obj and ref $obj eq __PACKAGE__
!ll&&!rl&&r
001
MC/DC0%0/2
$obj and ref $obj eq __PACKAGE__
$obj ref $obj eq __PACKAGE__
Truth table33%1/3
A and B
A= $objB= ref $obj eq __PACKAGE__
ABresult
0X0
100
111
332 1 $obj->{mlite} = $m;
333 1 ▶ return $obj unless $obj->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"dbh"} 0 1 1
334 1 ▶ if ($m && ref($m) eq 'WWW::MLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($m and ref $m eq 'WWW::MLite') { } 0 1 1
Condition33%1/3
$m and ref $m eq "WWW::MLite"
!ll&&!rl&&r
100
MC/DC0%0/2
$m and ref $m eq "WWW::MLite"
$m ref $m eq "WWW::MLite"
Truth table33%1/3
A and B
A= $mB= ref $m eq "WWW::MLite"
ABresult
0X0
100
111
335 0 $m->debug("--- CONNECT {$dsn} AS $obj ---");
336 } else {
337 1 ▶ carp("--- CONNECT {$dsn} AS $obj ---") if $DEBUG_FORCE;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::MLite::Store::DBI::DEBUG_FORCE 0 1 1
338 }
339 1 ▶ return $obj if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 1 0 1
340 } else {
341 0 return bless({
342 mlite=>$m,
343 }, $class);
344 }
345 0 return undef;
346 }
347 sub ping {
348 0 ▶ my $self = shift;
Subroutine ping: not called CC=6 SCAR=37.4
Pod documented
349 0 ▶ return 0 unless $self && ref($self) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless $self and ref $self eq __PACKAGE__ 0 0 0
Condition0%0/3
$self and ref $self eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
$self and ref $self eq __PACKAGE__
$self ref $self eq __PACKAGE__
Truth table0%0/3
A and B
A= $selfB= ref $self eq __PACKAGE__
ABresult
0X0
100
111
350 0 ▶ return 0 unless $self->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dsn"} 0 0 0
351 0 ▶ return 0 unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
352 0 ▶ return 0 unless $self->{dbh}->can('ping');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"}->can("ping") 0 0 0
353 0 return $self->{dbh}->ping();
354 }
355 sub reconnect {
356 0 ▶ my $self = shift;
Subroutine reconnect: not called CC=5 SCAR=34.0
Pod documented
357
358 0 my $m = $self->{mlite};
359 0 my $dsn = $self->{dsn};
360
361 # See CTK::DBI::DBI_CONNECT
362 $self->{dbh} = CTK::DBI::DBI_CONNECT(
363 $dsn,
364 $self->{user},
365 $self->{password},
366 $self->{attr},
367 $self->{connect_to},
368 0 );
369 0 ▶ if ($self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}) 0 0 0
370 0 ▶ if ($m && ref($m) eq 'WWW::MLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($m and ref $m eq 'WWW::MLite') { } 0 0 0
Condition0%0/3
$m and ref $m eq "WWW::MLite"
!ll&&!rl&&r
000
MC/DC0%0/2
$m and ref $m eq "WWW::MLite"
$m ref $m eq "WWW::MLite"
Truth table0%0/3
A and B
A= $mB= ref $m eq "WWW::MLite"
ABresult
0X0
100
111
371 0 $m->debug("--- RECONNECT {$dsn} AS $self ---");
372 } else {
373 0 ▶ carp("--- RECONNECT {$dsn} AS $self ---") if $DEBUG_FORCE;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::MLite::Store::DBI::DEBUG_FORCE 0 0 0
374 }
375 0 return 1;
376 }
377 0 return undef;
378 }
379 sub err {
380 0 ▶ my $self = shift;
Subroutine err: not called CC=4 SCAR=30.0
Pod documented
381 0 ▶ return $self->{dbh}->err if $self->{dbh} && $self->{dbh}->can('err');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} and $self->{"dbh"}->can("err") 0 0 0
Condition0%0/3
$self->{"dbh"} and $self->{"dbh"}->can("err")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dbh"} and $self->{"dbh"}->can("err")
$self->{"dbh"} $self->{"dbh"}->can("err")
Truth table0%0/3
A and B
A= $self->{"dbh"}B= $self->{"dbh"}->can("err")
ABresult
0X0
100
111
382 0 ▶ return defined $DBI::err ? $DBI::err : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $DBI::err ? : 0 0 0
383 }
384 sub errstr {
385 0 ▶ my $self = shift;
Subroutine errstr: not called CC=4 SCAR=30.0
Pod documented
386 0 ▶ return $self->{dbh}->errstr if $self->{dbh} && $self->{dbh}->can('errstr');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} and $self->{"dbh"}->can("errstr") 0 0 0
Condition0%0/3
$self->{"dbh"} and $self->{"dbh"}->can("errstr")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dbh"} and $self->{"dbh"}->can("errstr")
$self->{"dbh"} $self->{"dbh"}->can("errstr")
Truth table0%0/3
A and B
A= $self->{"dbh"}B= $self->{"dbh"}->can("errstr")
ABresult
0X0
100
111
387 0 ▶ return defined $DBI::errstr ? $DBI::errstr : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $DBI::errstr ? : 0 0 0
388 }
389 sub state {
390 0 ▶ my $self = shift;
Subroutine state: not called CC=4 SCAR=30.0
Pod documented
391 0 ▶ return $self->{dbh}->state if $self->{dbh} && $self->{dbh}->can('state');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} and $self->{"dbh"}->can("state") 0 0 0
Condition0%0/3
$self->{"dbh"} and $self->{"dbh"}->can("state")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dbh"} and $self->{"dbh"}->can("state")
$self->{"dbh"} $self->{"dbh"}->can("state")
Truth table0%0/3
A and B
A= $self->{"dbh"}B= $self->{"dbh"}->can("state")
ABresult
0X0
100
111
392 0 ▶ return defined $DBI::state ? $DBI::state : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $DBI::state ? : 0 0 0
393 }
394 sub DESTROY {
395 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=8 SCAR=24.9
396 1 my $dsn = '';
397 1 ▶ $dsn = $self->{dsn} if $self->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dsn"} 1 0 1
398 1 my $m = '';
399 1 ▶ $m = $self->{mlite} if $self->{mlite};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mlite"} 0 1 1
400
401 1 ▶ if ($dsn && $self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn and $self->{"dbh"}) 1 0 1
Condition33%1/3
$dsn and $self->{"dbh"}
!ll&&!rl&&r
001
MC/DC0%0/2
$dsn and $self->{"dbh"}
$dsn $self->{"dbh"}
Truth table33%1/3
A and B
A= $dsnB= $self->{"dbh"}
ABresult
0X0
100
111
402 1 ▶ if($m && ref($m) eq 'WWW::MLite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($m and ref $m eq 'WWW::MLite') { } 0 1 1
Condition33%1/3
$m and ref $m eq "WWW::MLite"
!ll&&!rl&&r
100
MC/DC0%0/2
$m and ref $m eq "WWW::MLite"
$m ref $m eq "WWW::MLite"
Truth table33%1/3
A and B
A= $mB= ref $m eq "WWW::MLite"
ABresult
0X0
100
111
403 0 $m->debug("--- DISCONNECT {$dsn} ---");
404 } else {
405 1 ▶ carp("--- DISCONNECT {$dsn} ---") if $DEBUG_FORCE;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::MLite::Store::DBI::DEBUG_FORCE 0 1 1
406 }
407 }
408 }
409
410 1 1;
« MLite/Transaction.pm ↑ Summary MLite/Config.pm »