lib/DBIx/XML_RDB.pm

filter: Statementstmt 21.1% 15 / 71 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 30.0% 3 / 10 Podpod 60.0% 3 / 5 totaltotal 19.0% 21 / 110 CC 12 SCAR 27.0
CC
12
Cov
16%
_CreateOutput
30.0
Initialise
20.0
new
12.0
CRAP 14.8
↑ Summary lib/DBIx/xml2sql.pl »
1 package DBIx::XML_RDB;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %XMLCHARS $REXMLCHARS);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7  
8 1 @ISA = qw(Exporter);
9 1 @EXPORT = qw(); # Not exporting anything - this is OO.
10  
11 1 $VERSION = '0.05';
12  
13 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
16 0 my $this = shift;
17 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
18 0 my $self = {};
19 0 bless $self, $class;
20 0 ▶ $self->Initialise(@_) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->Initialise(@_) or return undef 0 0 0
21 0 return $self;
22 }
23  
24 1 %XMLCHARS = (
25 '&' => '&',
26 '<' => '&lt;',
27 '>' => '&gt;',
28 '"' => '&quot;',
29 );
30  
31 0 ▶sub xmlenc {
Subroutine xmlenc: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my $str = shift;
33 0 $str =~ s/([&<>"])/$XMLCHARS{$1}/ge;
34 0 return $str;
35 }
36  
37 0 ▶sub Initialise {
Subroutine Initialise: not called CC=4 SCAR=30.0
Pod undocumented
38 0 my $self = shift;
39 0 $self->{datasource} = shift;
40 0 my $driver = shift;
41 0 my $userid = shift;
42 0 my $password = shift;
43 0 my $dbname = shift;
44 0 $self->{verbose} = shift;
45  
46 0 $self->{dbh} = DBI->connect("dbi:$driver:". $self->{datasource}, $userid, $password);
47 0 ▶ if (!$self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}) 0 0 0
48 0 print STDERR "Connection failed\n";
49 0 return 0;
50 }
51  
52 0 ▶ if ($dbname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbname) 0 0 0
53 0 ▶ if(!$self->{dbh}->do("use $dbname")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}->do("use $dbname")) 0 0 0
54 0 print STDERR "USE $dbname failed\n";
55 0 return 0;
56 }
57 }
58  
59 0 $self->{output} = "<?xml version=\"1.0\"?>\n";
60 0 $self->{output} .= "<DBI driver=\"". xmlenc($self->{datasource}) . "\">\n";
61  
62 0 return 1;
63 }
64  
65 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
66 0 my $self = shift;
67 0 $self->{dbh}->disconnect;
68 }
69  
70 0 ▶sub DoSql {
Subroutine DoSql: not called CC=3 SCAR=24.8
Pod documented
71 0 my $self = shift;
72 0 my $sql = shift;
73 0 ▶ $self->{sth} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr;
Condition0%0/2
$self->{'dbh'}->prepare($sql) || die($self->{'dbh'}->errstr)
l!l
00
MC/DC0%0/1
$self->{'dbh'}->prepare($sql) || die($self->{'dbh'}->errstr)
$self->{'dbh'}->prepare($sql)
Truth table0%0/2
A
A= $self->{'dbh'}->prepare($sql)
Aresult
11
00
74 0 ▶ $self->{sth}->execute || die $self->{sth}->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sth"}->execute or die $self->{"sth"}->errstr 0 0 0
75 0 $self->_CreateOutput;
76 0 $self->{sth}->finish;
77 }
78  
79 0 ▶sub _CreateOutput {
Subroutine _CreateOutput: not called CC=5 SCAR=34.0
80 0 my $self = shift;
81  
82 0 my $fields = $self->{sth}->{NAME};
83  
84 # Now insert the actual data.
85  
86 0 $self->{output} .= "\t<RESULTSET statement=\"". xmlenc($self->{sth}->{Statement}) ."\">\n";
87  
88 0 my $row = 0;
89 0 my @data;
90 0 ▶ while (@data = $self->{sth}->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data = $self->{"sth"}->fetchrow_array) 0 0 0
91 0 ▶ print STDERR "Row: ", $row++, "\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
92 0 my $i = 0;
93 0 $self->{output} .= "\t\t<ROW>\n";
94 0 foreach my $f (@data) {
95 0 ▶ if (defined $f) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f) 0 0 0
96 0 $self->{output} .= "\t\t\t<" . $fields->[$i] . '>' . xmlenc($f) . '</' . $fields->[$i] . ">\n";
97 }
98 0 $i++;
99 }
100 0 $self->{output} .= "\t\t</ROW>\n";
101 }
102 0 $self->{output} .= "\t</RESULTSET>\n";
103 }
104  
105 0 ▶sub GetData {
Subroutine GetData: not called CC=1 SCAR=6.9
Pod documented
106 0 my $self = shift;
107 0 my $output = $self->{output} . "</DBI>\n";
108  
109 # Return output to starting state, in case we want to do more...
110 0 $self->{output} = "<?xml version=\"1.0\"?>\n";
111 0 $self->{output} .= "<DBI driver=\"" . xmlenc($self->{datasource}) . "\">\n";
112  
113 0 return $output;
114 }
115  
116 1 1;
117 __END__
↑ Summary lib/DBIx/xml2sql.pl »