blib/lib/DBIx/JSON.pm

filter: Statementstmt 21.2% 17 / 80 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 33.3% 5 / 15 Podpod 100% 8 / 8 totaltotal 17.3% 30 / 173 CC 21 SCAR 34.0
CC
21
Cov
12%
do_select
90.0
_init
20.0
do_sql
20.0
CRAP 30.0
↑ Summary
1 package DBIx::JSON;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 DBIx::JSON - Perl extension for creating JSON from existing DBI datasources
9  
10 =head1 DESCRIPTION
11  
12 This module is perl extension for creating JSON from existing DBI datasources.
13  
14 One use of this module might be to extract data on the web
15 server, and send the raw data (in JSON format) to a client's
16 browser, and then JavaScript do eval it to generate dynamic HTML.
17  
18 This module was inspired by DBIx::XML_RDB.
19  
20 =head1 VERSION
21  
22 Version 0.02
23  
24 =cut
25  
26 2 our $VERSION = '0.02';
27  
28 =head1 SYNOPSIS
29  
30 my $dsn = "dbname=$dbname;host=$host;port=$port";
31 print DBIx::JSON->new( $dsn, "mysql", $dbusername, $dbpasswd )
32 ->do_select("select * from table;")->get_json;
33  
34 or
35  
36 my $dsn = "dbname=$dbname;host=$host;port=$port";
37 my $obj = DBIx::JSON->new($dsn, "mysql", $dbusername, $dbpasswd);
38 $obj->do_select("select * from table;", "colmun1", 1);
39 $obj->err && die $obj->errstr;
40 print $obj->get_json;
41  
42 =head1 EXPORT
43  
44 None.
45  
46 =cut
47  
48 2 ▶use DBI 1.15 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use JSON::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 sub new {
53 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
54 0 my $self = {};
55 0 bless $self, $class;
56 0 ▶ $self->_init(@_) || return ();
Branch: 1 branch
BranchTrueFalseTotal
$self->_init(@_) or return () 0 0 0
57 0 return $self;
58 }
59  
60 sub _init {
61 0 ▶ my $self = shift;
Subroutine _init: not called CC=4 SCAR=30.0
62 0 my $dsn = shift;
63 0 my $driver = shift;
64 0 my $userid = shift;
65 0 my $passwd = shift;
66  
67 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"dbh"} = "DBI"->connect("dbi:${driver}:$dsn", $userid, $passwd, {"PrintWarm", 0, "PrintError", 1}) } } or $@ and Carp::croak($@) 0 0 0
Condition0%0/3
eval { do { $self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarm', 0, 'PrintError', 1}) } } || $@ && Carp::croak($@)
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarm', 0, 'PrintError', 1}) } } || $@ && Carp::croak($@)
eval { do { $self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarm', 0, 'PrintError', 1}) } } $@ && Carp::croak($@)
Truth table0%0/3
A || B
A= eval { do { $self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarm', 0, 'PrintError', 1}) } }B= $@ && Carp::croak($@)
ABresult
1X1
011
000
68 $self->{dbh} =
69 0 ▶ DBI->connect( "dbi:$driver:$dsn", $userid, $passwd,
Condition0%0/3
$@ and Carp::croak($@)
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and Carp::croak($@)
$@ Carp::croak($@)
Truth table0%0/3
A and B
A= $@B= Carp::croak($@)
ABresult
0X0
100
111
70 { PrintWarm => 0, PrintError => 1 } );
71 }
72 or $@ && Carp::croak $@;
73 0 ▶ if ( !$self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'dbh'}) { } 0 0 0
74 0 return ();
75 }
76 else {
77 0 $self->{dbh}->{PrintError} = 0;
78 }
79 0 1;
80 }
81  
82 sub do_select {
83 0 ▶ my $self = shift;
Subroutine do_select: not called CC=9 SCAR=45.0
Pod documented
84 0 my $sql = shift;
85 0 my $key_field = shift;
86 0 my $hash_array = shift;
87 0 ▶ if ($key_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_field) { } 0 0 0
88 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"data"} = $self->{"dbh"}->selectall_hashref($sql, $key_field) } } or $@ and Carp::croak($@) 0 0 0
Condition0%0/3
eval { do { $self->{'data'} = $self->{'dbh'}->selectall_hashref($sql, $key_field) } } || $@ && Carp::croak($@)
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $self->{'data'} = $self->{'dbh'}->selectall_hashref($sql, $key_field) } } || $@ && Carp::croak($@)
eval { do { $self->{'data'} = $self->{'dbh'}->selectall_hashref($sql, $key_field) } } $@ && Carp::croak($@)
Truth table0%0/3
A || B
A= eval { do { $self->{'data'} = $self->{'dbh'}->selectall_hashref($sql, $key_field) } }B= $@ && Carp::croak($@)
ABresult
1X1
011
000
89 0 ▶ $self->{data} = $self->{dbh}->selectall_hashref( $sql, $key_field );
Condition0%0/3
$@ and Carp::croak($@)
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and Carp::croak($@)
$@ Carp::croak($@)
Truth table0%0/3
A and B
A= $@B= Carp::croak($@)
ABresult
0X0
100
111
90 }
91 or $@ && Carp::croak $@;
92 0 ▶ if ( $self->{dbh}->err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}->err) 0 0 0
93 0 Carp::carp $self->{dbh}->errstr;
94 }
95 0 ▶ if ($hash_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_array) 0 0 0
96 0 $self->{data} = [ values( %{ $self->{data} } ) ];
97 }
98 }
99 else {
100 0 ▶ eval { $self->{data} = $self->{dbh}->selectall_arrayref($sql); }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"data"} = $self->{"dbh"}->selectall_arrayref($sql) } } or $@ and Carp::croak($@) 0 0 0
Condition0%0/6
eval { do { $self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql) } } || $@ && Carp::croak($@)
l!l&&r!l&&!r
000
$@ and Carp::croak($@)
!ll&&!rl&&r
000
MC/DC0%0/3
eval { do { $self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql) } } || $@ && Carp::croak($@)
eval { do { $self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql) } } $@ Carp::croak($@)
Truth table0%0/4
A || B and C
A= eval { do { $self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql) } }B= $@C= Carp::croak($@)
ABCresult
1XX1
0111
00X0
0100
101 or $@ && Carp::croak $@;
102 0 ▶ if ( $self->{dbh}->err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}->err) 0 0 0
103 0 Carp::carp $self->{dbh}->errstr;
104 }
105 }
106 0 return $self;
107 }
108  
109 sub do_sql {
110 0 ▶ my $self = shift;
Subroutine do_sql: not called CC=4 SCAR=30.0
Pod documented
111 0 my $sql = shift;
112 0 ▶ eval { $self->{dbh}->do($sql); }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"dbh"}->do($sql) } } or $@ and Carp::croak($@) 0 0 0
Condition0%0/6
eval { do { $self->{'dbh'}->do($sql) } } || $@ && Carp::croak($@)
l!l&&r!l&&!r
000
$@ and Carp::croak($@)
!ll&&!rl&&r
000
MC/DC0%0/3
eval { do { $self->{'dbh'}->do($sql) } } || $@ && Carp::croak($@)
eval { do { $self->{'dbh'}->do($sql) } } $@ Carp::croak($@)
Truth table0%0/4
A || B and C
A= eval { do { $self->{'dbh'}->do($sql) } }B= $@C= Carp::croak($@)
ABCresult
1XX1
0111
00X0
0100
113 or $@ && Carp::croak $@;
114 0 ▶ if ( $self->{dbh}->err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}->err) 0 0 0
115 0 Carp::carp $self->{dbh}->errstr;
116 }
117 0 return $self;
118 }
119  
120 sub has_data {
121 0 ▶ my $self = shift;
Subroutine has_data: not called CC=2 SCAR=17.9
Pod documented
122 0 ▶ if ( ref $self->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"data"}) 0 0 0
123 0 return 1;
124 }
125 0 return ();
126 }
127  
128 sub get_json {
129 0 ▶ my $self = shift;
Subroutine get_json: not called CC=2 SCAR=17.9
Pod documented
130 0 ▶ if ( $self->has_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_data) 0 0 0
131 0 return JSON::Syck::Dump( $self->{data} );
132 }
133 0 return ();
134 }
135  
136 sub clear_data {
137 0 ▶ my $self = shift;
Subroutine clear_data: not called CC=1 SCAR=6.9
Pod documented
138 0 $self->{data} = ();
139 0 1;
140 }
141  
142 sub errstr {
143 0 ▶ my $self = shift;
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
144 0 ▶ if ( $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dbh'}) { } 0 0 0
145 0 return $self->{dbh}->errstr;
146 }
147 else {
148 0 return ();
149 }
150 }
151  
152 sub err {
153 0 ▶ my $self = shift;
Subroutine err: not called CC=2 SCAR=17.9
Pod documented
154 0 ▶ if ( $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dbh'}) { } 0 0 0
155 0 return $self->{dbh}->err;
156 }
157 else {
158 0 return ();
159 }
160 }
161  
162 sub DESTROY {
163 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=2 SCAR=17.9
164 0 ▶ if ( $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dbh'}) { } 0 0 0
165 0 $self->{dbh}->disconnect;
166 }
167 else {
168 0 return ();
169 }
170 }
171  
172 2 1; # End of DBIx::JSON
173  
174 __END__
↑ Summary