| 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
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table0%0/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 18 | 0 | my $self = {}; | |||||||||||||||||||
| 19 | 0 | bless $self, $class; | |||||||||||||||||||
| 20 | 0 | ▶ | $self->Initialise(@_) || return undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 0 | return $self; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | %XMLCHARS = ( | |||||||||||||||||||
| 25 | '&' => '&', | ||||||||||||||||||||
| 26 | '<' => '<', | ||||||||||||||||||||
| 27 | '>' => '>', | ||||||||||||||||||||
| 28 | '"' => '"', | ||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 48 | 0 | print STDERR "Connection failed\n"; | |||||||||||||||||||
| 49 | 0 | return 0; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | if ($dbname) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 0 | ▶ | if(!$self->{dbh}->do("use $dbname")) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||
| 74 | 0 | ▶ | $self->{sth}->execute || die $self->{sth}->errstr; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 91 | 0 | ▶ | print STDERR "Row: ", $row++, "\n" if $self->{verbose}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||