dbMan.pm

filter: Statementstmt 14.0% 23 / 164 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 8 totaltotal 9.8% 30 / 306 CC 49 SCAR 52.7
CC
49
Cov
9%
load_extensions
380.0
handle_action
240.0
start
72.0
CRAP 193.6
« dbMan/Extension/CmdConnections.pm ↑ Summary dbMan/Extension/SQLOutputOldTable.pm »
1 package DBIx::dbMan;
2  
3 =comment
4  
5 dbMan 0.47
6 (c) Copyright 1999-2023 by Milan Sorm, sorm@is4u.cz
7 All rights reserved.
8  
9 This software provides some functionality in database managing
10 (SQL console).
11  
12 This program is free software; you can redistribute it and/or modify it
13 under the same terms as Perl itself.
14  
15 =cut
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use DBIx::dbMan::Config; # configuration handling package
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use DBIx::dbMan::Lang; # I18N package - EXPERIMENTAL
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use DBIx::dbMan::DBI; # dbMan DBI interface package
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use DBIx::dbMan::MemPool; # dbMan memory management system package
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 our $VERSION = '0.47';
25  
26 # constructor, arguments are hash of style -option => value, stored in internal attributes hash
27 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
28 1 my $class = shift;
29 1 my $obj = bless { @_ }, $class;
30 1 return $obj;
31 }
32  
33 # main loop of dbMan life-cycle, called from exe file
34 0 ▶sub start {
Subroutine start: not called CC=8 SCAR=42.8
Pod undocumented
35 0 my $obj = shift; # main dbMan core object
36  
37 0 ▶ $obj->{ -trace } = $ENV{ DBMAN_TRACE } || 0; # standard extension tracing activity - DISABLED
Condition0%0/2
$ENV{'DBMAN_TRACE'} || 0
l!l
00
MC/DC0%0/1
$ENV{'DBMAN_TRACE'} || 0
$ENV{'DBMAN_TRACE'}
Truth table0%0/2
A
A= $ENV{'DBMAN_TRACE'}
Aresult
11
00
38  
39 # what interface exe file want ??? making package name from it
40 0 my $interface = $obj->{ -interface };
41 0 $interface = 'DBIx/dbMan/Interface/' . $interface . '.pm';
42  
43 # we try to require interface package - found in @INC, syntax check,
44 # load it by require instead of use because we know only filename
45 0 eval { require $interface; };
46 0 ▶ if ( $@ ) { # if something goes wrong
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
47 0 $interface =~ s/\//::/g;
48 0 $interface =~ s/\.pm$//;
49  
50 # bad information for user :-(
51 0 print STDERR "Can't locate interface module $interface\n";
52 0 return; # see you later...
53 }
54  
55 # making class name from interface package filename
56 0 $interface =~ s/\//::/g;
57 0 $interface =~ s/\.pm$//;
58  
59 # creating memory management object - mempool
60 0 $obj->{ mempool } = new DBIx::dbMan::MemPool;
61  
62 # creating configuration object
63 0 $obj->{ config } = new DBIx::dbMan::Config;
64  
65 # creating I18N specifics object with configuration object as argument
66 0 $obj->{ lang } = new DBIx::dbMan::Lang -config => $obj->{ config };
67  
68 # creating loaded interface object, all objects as arguments
69 # included dbMan core object
70 $obj->{ interface } = $interface->new(
71 -config => $obj->{ config },
72 0 -lang => $obj->{ lang }, -mempool => $obj->{ mempool }, -core => $obj
73 );
74  
75 # we have interface now, we can produce messages and errors by object
76 # method $obj->{interface}->print('what we can say to user...')
77  
78 # dbMan interface, please introduce us to our user (welcome message, splash etc.)
79 0 $obj->{ interface }->hello();
80  
81 # creating dbMan DBI object - encapsulation of DBI with multiple connections
82 # support, configuration, interface and mempool as arguments
83 $obj->{ dbi } = new DBIx::dbMan::DBI -config => $obj->{ config },
84 0 -interface => $obj->{ interface }, -mempool => $obj->{ mempool };
85  
86 # looking for and loading all extensions
87 0 $obj->load_extensions;
88  
89 # we say to the interface that extensions are loaded and menu can be build
90 0 $obj->{ interface }->rebuild_menu();
91  
92 # main loop derived by interface - get_action & handle_action calling cycle
93 # NOT CALLED if we are in $main::TEST mode (tested initialization from make test)
94 0 ▶ $obj->{ interface }->loop() unless defined $main::TEST && $main::TEST;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::TEST and $main::TEST 0 0 0
Condition0%0/3
defined $main::TEST and $main::TEST
!ll&&!rl&&r
000
MC/DC0%0/2
defined $main::TEST and $main::TEST
defined $main::TEST $main::TEST
Truth table0%0/3
A and B
A= defined $main::TESTB= $main::TEST
ABresult
0X0
100
111
95  
96 # unloading all loaded extensions
97 0 $obj->unload_extensions;
98  
99 # close all opened DBI connections by dbMan DBI object
100 0 $obj->{ dbi }->close_all();
101  
102 # dbMan interface, please say good bye to our user...
103 0 $obj->{ interface }->goodbye();
104  
105 # test result OK if we are in $main::TEST mode (tested initialization from make test)
106 0 ▶ $main::TEST_RESULT = 1 if defined $main::TEST && $main::TEST;
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::TEST and $main::TEST 0 0 0
Condition0%0/3
defined $main::TEST and $main::TEST
!ll&&!rl&&r
000
MC/DC0%0/2
defined $main::TEST and $main::TEST
defined $main::TEST $main::TEST
Truth table0%0/3
A and B
A= defined $main::TESTB= $main::TEST
ABresult
0X0
100
111
107  
108 # program must correctly exit if we want 'test ok' for make test' tests
109 0 ▶ exit if $main::TEST_RESULT;
Branch: 1 branch
BranchTrueFalseTotal
if $main::TEST_RESULT 0 0 0
110 }
111  
112 # looking for and loading extensions
113 0 ▶sub load_extensions {
Subroutine load_extensions: not called CC=19 SCAR=59.4
Pod undocumented
114 0 my $obj = shift; # main dbMan core object
115  
116 0 $obj->{ extensions } = []; # currently loaded extensions = no extensions
117  
118 # 1st phase : candidate searching algorithm
119 0 my %candidates = (); # what are my candidates for extensions ?
120 0 for my $dir ( $obj->extensions_directories ) { # all extensions directories
121 0 opendir D, $dir; # search in directory
122 0 for ( grep /\.pm$/, readdir D ) { # for each found package
123 0 eval { require "$dir/$_"; }; # try to require
124 0 ▶ next if $@; # not candidate if fail
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
125 0 s/\.pm$//; # make class name from filename
126 0 my $candidate = "DBIx::dbMan::Extension::" . $_;
127  
128 # search for extension version limit (class method) - low and high
129 0 my ( $low, $high ) = ( '', '' );
130 0 eval { ( $low, $high ) = $candidate->for_version(); };
131  
132 # not candidate if our version isn't between low and high
133 # we must delete filename from include list
134 0 ▶ if ( ( $low and $VERSION < $low ) or ( $high and $VERSION > $high ) ) { delete $INC{ "$dir/$_.pm" }; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($low and $DBIx::dbMan::VERSION < $low or $high and $DBIx::dbMan::VERSION > $high) 0 0 0
Condition0%0/9
$low and $DBIx::dbMan::VERSION < $low or $high and $DBIx::dbMan::VERSION > $high
l!l&&r!l&&!r
000
$low and $DBIx::dbMan::VERSION < $low
!ll&&!rl&&r
000
$high and $DBIx::dbMan::VERSION > $high
!ll&&!rl&&r
000
MC/DC0%0/4
$low and $DBIx::dbMan::VERSION < $low or $high and $DBIx::dbMan::VERSION > $high
$low $DBIx::dbMan::VERSION < $low $high $DBIx::dbMan::VERSION > $high
Truth table0%0/7
A and B or C and D
A= $lowB= $DBIx::dbMan::VERSION < $lowC= $highD= $DBIx::dbMan::VERSION > $high
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
135  
136 # fetching identification from extension (class method)
137 0 my $id = '';
138 0 eval { $id = $candidate->IDENTIFICATION(); };
139  
140 # not candidate if identification not specified
141 0 ▶ unless ( $id or $@ ) { delete $INC{ "$dir/$_.pm" }; next; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($id or $@) 0 0 0
Condition0%0/3
$id or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$id or $@
$id $@
Truth table0%0/3
A or B
A= $idB= $@
ABresult
1X1
011
000
142  
143 # parsing identification AUTHOR-MODULE-VERSION
144 0 my ( $ident, $ver ) = ( $id =~ /^(.*)-(.*)$/ );
145  
146 # not candidate if AUTHOR-MODULE isn't overloaded
147 0 ▶ if ( $ident eq '000001-000001' ) { delete $INC{ "$dir/$_.pm" }; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ident eq "000001-000001") 0 0 0
148  
149 # deleting filename from include list
150 0 delete $INC{ "$dir/$_.pm" };
151  
152 # not candidate if exist this identification with same or higher version
153 0 ▶ next if exists $candidates{ $ident } && $candidates{ $ident }->{ -ver } >= $ver;
Branch: 1 branch
BranchTrueFalseTotal
if exists $candidates{$ident} and $candidates{$ident}{"-ver"} >= $ver 0 0 0
Condition0%0/3
exists $candidates{$ident} and $candidates{$ident}{"-ver"} >= $ver
!ll&&!rl&&r
000
MC/DC0%0/2
exists $candidates{$ident} and $candidates{$ident}{"-ver"} >= $ver
exists $candidates{$ident} $candidates{$ident}{"-ver"} >= $ver
Truth table0%0/3
A and B
A= exists $candidates{$ident}B= $candidates{$ident}{"-ver"} >= $ver
ABresult
0X0
100
111
154  
155 # save candidate to candidates list
156 0 $candidates{ $ident } = { -file => "$dir/$_.pm", -candidate => $candidate, -ver => $ver };
157 }
158  
159 0 closedir D; # close searched directory
160 }
161  
162 # 2nd phase : candidate loading algorithm
163 0 my %extensions = (); # all objects of extensions
164  
165 0 $obj->{ extension_iterator } = 0; # randomize iterator
166 0 for my $candidate ( keys %candidates ) { # for each candidate
167 0 my $ext = undef; # undefined extension
168 0 eval { # try require file and create object
169 0 require $candidates{ $candidate }->{ -file };
170  
171 # object pass all five instances of base objects as argument
172 $ext = $candidates{ $candidate }->{ -candidate }->new(
173 -config => $obj->{ config },
174 -interface => $obj->{ interface },
175 -dbi => $obj->{ dbi },
176 -core => $obj,
177 -mempool => $obj->{ mempool }
178 0 );
179  
180 0 ▶ die unless $ext->load_ok();
Branch: 1 branch
BranchTrueFalseTotal
unless $ext->load_ok 0 0 0
181 };
182 0 ▶ if ( defined $ext and not $@ ) { # successful loading ?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ext and not $@) 0 0 0
Condition0%0/3
defined $ext and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ext and not $@
defined $ext not $@
Truth table0%0/3
A and not(B)
A= defined $extB= not $@
ABresult
0X0
100
111
183 0 my $preference = 0; # standard preference level
184 0 eval { $preference = $ext->preference(); }; # trying to fetch preference
185  
186 # sorting criteria are: preference, random iterator
187 # saving sort criteria for later using
188 0 $ext->{ '___sort_criteria___' } = $preference . '_' . $obj->{ extension_iterator };
189  
190 # save instance of object to hash indexed by preference
191 0 $extensions{ $preference . '_' . $obj->{ extension_iterator } } = $ext;
192  
193 0 ++$obj->{ extension_iterator }; # increase random iterator
194 }
195 }
196  
197 # 3rd phase : building candidates list sorted by preference (for action handling)
198 0 for (
199 sort { # sorting criteria - first time by preference, second time loading order
200 0 my ( $fa, $sa, $fb, $sb ) = split /_/, $a . '_' . $b;
201 0 ▶ ( $fa == $fb ) ? ( $sa <=> $sb ) : ( $fb <=> $fa );
Branch: 1 branch
BranchTrueFalseTotal
$fa == $fb ? : 0 0 0
202 } keys %extensions
203 ) { # for all loaded extensions
204  
205 # save extension into sorted list
206 0 push @{ $obj->{ extensions } }, $extensions{ $_ };
207  
208 # call init() for initializing extension (all extensions in correct order)
209 0 $extensions{ $_ }->init();
210 }
211  
212 # all extensions are loaded and sorted by preference into $obj->{extensions} list
213 }
214  
215 # unloading all extensions
216 0 ▶sub unload_extensions {
Subroutine unload_extensions: not called CC=2 SCAR=17.9
Pod undocumented
217 0 my $obj = shift; # main dbMan core object
218  
219 0 for ( @{ $obj->{ extensions } } ) { # for all extensions in standard order
220 0 $_->done(); # call done() for finalizing extension
221 0 undef $_; # destroy extension instance of object
222 }
223 }
224  
225 # produce list of all extensions directories
226 0 ▶sub extensions_directories {
Subroutine extensions_directories: not called CC=2 SCAR=17.9
Pod undocumented
227 0 my $obj = shift; # main dbMan core object
228  
229 # grep criteria - only directories which contains DBIx/dbMan/Extension subfolder are wanted
230 # tested dirs are: @INC, extensions_dir configuration directive, current folder
231 # WARNING: i must call extensions_dir in list context if I want list of directories
232 0 ▶ return grep { -d $_ } map { my $t = $_; $t =~ s/\/$//; "$t/DBIx/dbMan/Extension" } ( @INC, ( $obj->{ config }->extensions_dir ? ( $obj->{ config }->extensions_dir ) : () ), '.' );
Branch: 1 branch
BranchTrueFalseTotal
$obj->{'config'}->extensions_dir ? : 0 0 0
233 }
234  
235 # show tracing record via interface object
236 0 ▶sub trace {
Subroutine trace: not called CC=8 SCAR=42.8
Pod undocumented
237 0 my ( $obj, $direction, $where, %action ) = @_; # main dbMan core object,
238 # direction string (passed to interface), extension object and action record
239  
240 # change $where to readable form
241 0 $where =~ s/=.*$//;
242 0 $where =~ s/^DBIx::dbMan::Extension:://;
243 0 my $params = '';
244 0 for ( sort keys %action ) { # for all actions
245 0 ▶ next if $_ eq 'action'; # action tag ignore
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "action" 0 0 0
246 0 my $p = $action{ $_ };
247 0 ▶ $p = "'$p'" if $p !~ /^[-a-z0-9_.]+$/i; # stringify
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /^[-a-z0-9_.]+$/i 0 0 0
248 0 ▶ $params .= ", " if $params;
Branch: 1 branch
BranchTrueFalseTotal
if $params 0 0 0
249 0 $params .= "$_: $p"; # concat
250 }
251  
252 # change non-selected chars in $params to <hexa> style
253 $params = join '', # joining transformed chars
254 0 ▶ map { ( $_ >= 32 && $_ != 255 && $_ != 127 ) ? chr : sprintf "<%02x>", $_; } unpack "C*", $params; # disassemble $params into chars
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 32 && $_ != 255 && $_ != 127 ? : 0 0 0
Condition0%0/6
$_ >= 32 and $_ != 255 and $_ != 127
!ll&&!rl&&r
000
$_ >= 32 and $_ != 255
!ll&&!rl&&r
000
MC/DC0%0/3
$_ >= 32 and $_ != 255 and $_ != 127
$_ >= 32 $_ != 255 $_ != 127
Truth table0%0/4
A and B and C
A= $_ >= 32B= $_ != 255C= $_ != 127
ABCresult
0XX0
10X0
1100
1111
255  
256 # sending tracing report via interface object
257 0 $obj->{ interface }->trace( "$direction $where / $action{action} / $params\n" );
258 }
259  
260 # main loop for handling one action
261 0 ▶sub handle_action {
Subroutine handle_action: not called CC=15 SCAR=54.8
Pod undocumented
262 0 my ( $obj, %action ) = @_; # main dbMan core object, action to process
263  
264 0 $action{ processed } = undef; # save signature of old action for deep recursion test
265 0 my $oldaction = \%action;
266  
267 0 for my $ext ( @{ $obj->{ extensions } } ) { # going down through all extensions in preference order
268 0 $action{ processed } = 1;
269 0 ▶ last if $action{ action } eq 'NONE'; # stop on NONE actions
Branch: 1 branch
BranchTrueFalseTotal
if $action{"action"} eq "NONE" 0 0 0
270  
271 0 my $acts = undef;
272 0 eval { $acts = $ext->known_actions; }; # hack - which actions extension want ???
273 next
274 if $@
275 || ( defined $acts
276 && ref $acts eq 'ARRAY'
277 0 ▶ && ! grep { $_ eq $action{ action } } @$acts ); # use hacked knowledge
Branch: 1 branch
BranchTrueFalseTotal
if $@ or defined $acts and ref $acts eq "ARRAY" and not grep({$_ eq $action{"action"};} @$acts) 0 0 0
Condition0%0/9
$@ or defined $acts and ref $acts eq "ARRAY" and not grep({$_ eq $action{"action"};} @$acts)
l!l&&r!l&&!r
000
defined $acts and ref $acts eq "ARRAY" and not grep({$_ eq $action{"action"};} @$acts)
!ll&&!rl&&r
000
defined $acts and ref $acts eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/4
$@ or defined $acts and ref $acts eq "ARRAY" and not grep({$_ eq $action{"action"};} @$acts)
$@ defined $acts ref $acts eq "ARRAY" not grep({$_ eq $action{"action"};} @$acts)
Truth table0%0/5
A or B and C and not(D)
A= $@B= defined $actsC= ref $acts eq "ARRAY"D= not grep({$_ eq $action{"action"};} @$acts)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
278  
279 0 ▶ $obj->trace( "<==", $ext, %action ) if $obj->{ -trace }; # trace if user want
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"-trace"} 0 0 0
280  
281 0 $action{ processed } = undef; # standard behaviour - action not processed
282 0 eval { %action = $ext->handle_action( %action ); }; # handling action
283 0 ▶ if ( $@ && $@ !~ /^Catched signal INT/ ) { # error - exception
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /^Catched signal INT/) 0 0 0
Condition0%0/3
$@ and not $@ =~ /^Catched signal INT/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $@ =~ /^Catched signal INT/
$@ not $@ =~ /^Catched signal INT/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /^Catched signal INT/
ABresult
0X0
100
111
284 0 $obj->{ interface }->error( "Exception catched: $@" );
285 0 $action{ processed } = 1;
286 0 $action{ action } = 'NONE';
287 }
288  
289 0 ▶ $obj->trace( "==>", $ext, %action ) if $obj->{ -trace }; # trace if user want
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"-trace"} 0 0 0
290  
291 0 ▶ last unless $action{ processed }; # action wasn't processed corectly
Branch: 1 branch
BranchTrueFalseTotal
unless $action{"processed"} 0 0 0
292 # ... prefix probably set - return to get_event (and called once again we hope)
293 }
294  
295 0 $obj->{ -deep_detected } = 0;
296  
297 # deep recursion detection
298 0 ▶ unless ( $action{ processed } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action{"processed"}) 0 0 0
299 0 my $newaction = \%action;
300 0 ▶ if ( $obj->compare_struct( $oldaction, $newaction ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->compare_struct($oldaction, $newaction)) 0 0 0
301 0 ▶ if ( $obj->{ -deep_detected } >= 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{'-deep_detected'} >= 100) { } 0 0 0
302 0 $obj->trace( "Deep recursion detected...\n", '- new:', %action );
303 0 $obj->trace( "", '- old:', %$oldaction );
304 0 $action{ processed } = 1;
305 }
306 else {
307 0 ++$obj->{ -deep_detected };
308 }
309 }
310 }
311  
312 # action processed correctly, good bye with modified action record
313 0 return %action;
314 }
315  
316 # return 1 if structs are identical
317 0 ▶sub compare_struct {
Subroutine compare_struct: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my $obj = shift;
319 0 my ( $a, $b ) = @_;
320  
321 0 my $first = Data::Dumper->Dump( [ $a ] );
322 0 my $second = Data::Dumper->Dump( [ $b ] );
323 0 return $a eq $b;
324  
325 0 return 0;
326 }
327  
328 2 1; # all is O.K.
« dbMan/Extension/CmdConnections.pm ↑ Summary dbMan/Extension/SQLOutputOldTable.pm »