| 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 - JSON serialization plugin for DBIx | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | This module is a DBIx extension to fetch data in JSON format from | ||||||||||||||||||||||||||||||||
| 13 | datasources. | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | One of use cases of this module is to simply implement a back-end | ||||||||||||||||||||||||||||||||
| 16 | service for a WEB application. | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | Version 0.05 | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | my $dsn = "dbname=$dbname;host=$host;port=$port"; | ||||||||||||||||||||||||||||||||
| 29 | print DBIx::JSON->new( $dsn, "mysql", $dbusername, $dbpasswd ) | ||||||||||||||||||||||||||||||||
| 30 | ->do_select("select * from table;")->get_json; | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | or | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | my $dsn = "dbname=$dbname;host=$host;port=$port"; | ||||||||||||||||||||||||||||||||
| 35 | my $obj = DBIx::JSON->new($dsn, "mysql", $dbusername, $dbpasswd); | ||||||||||||||||||||||||||||||||
| 36 | $obj->do_select("select * from table;", "colmun1", 1); | ||||||||||||||||||||||||||||||||
| 37 | $obj->err && die $obj->errstr; | ||||||||||||||||||||||||||||||||
| 38 | print $obj->get_json; | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | =head1 EXPORT | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | None. | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | use DBI 1.15 (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | use JSON::Syck; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 51 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 52 | 0 | my $self = {}; | |||||||||||||||||||||||||||||||
| 53 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $self->_init(@_) || return (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub _init { | ||||||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 59 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 60 | 0 | my $dsn = shift; | |||||||||||||||||||||||||||||||
| 61 | 0 | my $driver = shift; | |||||||||||||||||||||||||||||||
| 62 | 0 | my $userid = shift; | |||||||||||||||||||||||||||||||
| 63 | 0 | my $passwd = shift; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | eval { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval {
do {
$self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarn', 0, 'PrintError', 1})
}
} || $@ && Carp::croak($@)
MC/DC0%0/2
eval {
do {
$self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarn', 0, 'PrintError', 1})
}
} || $@ && Carp::croak($@)
eval {
do {
$self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarn', 0, 'PrintError', 1})
}
}
$@ && Carp::croak($@)
Truth table0%0/3
A || B
A= eval {
do {
$self->{'dbh'} = 'DBI'->connect("dbi:${driver}:$dsn", $userid, $passwd, {'PrintWarn', 0, 'PrintError', 1})
}
}B= $@ && Carp::croak($@)
| |||||||||||||||||||||||||||||||||
| 66 | $self->{dbh} = | ||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | DBI->connect( "dbi:$driver:$dsn", $userid, $passwd, | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ and Carp::croak($@)
MC/DC0%0/1
$@ and Carp::croak($@)
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||
| 68 | { PrintWarn => 0, PrintError => 1 } ); | ||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | or $@ && Carp::croak $@; | ||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( !$self->{dbh} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 0 | return (); | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{dbh}->{PrintError} = 0; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | 0 | 1; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub do_select { | ||||||||||||||||||||||||||||||
|
Subroutine
do_select: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 82 | 0 | my $sql = shift; | |||||||||||||||||||||||||||||||
| 83 | 0 | my $key_field = shift; | |||||||||||||||||||||||||||||||
| 84 | 0 | my $hash_array = shift; | |||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($key_field) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | eval { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval {
do {
$self->{'data'} = $self->{'dbh'}->selectall_hashref($sql, $key_field)
}
} || $@ && Carp::croak($@)
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($@)
| |||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $self->{data} = $self->{dbh}->selectall_hashref( $sql, $key_field ); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$@ and Carp::croak($@)
MC/DC0%0/1
$@ and Carp::croak($@)
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | or $@ && Carp::croak $@; | ||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( $self->{dbh}->err ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | 0 | Carp::carp $self->{dbh}->errstr; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($hash_array) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 0 | $self->{data} = [ values( %{ $self->{data} } ) ]; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | eval { $self->{data} = $self->{dbh}->selectall_arrayref($sql); } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
eval {
do {
$self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql)
}
} || $@ && Carp::croak($@)
$@ and Carp::croak($@)
MC/DC0%0/2
eval {
do {
$self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql)
}
} || $@ && Carp::croak($@)
eval {
do {
$self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql)
}
}
$@
Truth table0%0/3
A || B
A= eval {
do {
$self->{'data'} = $self->{'dbh'}->selectall_arrayref($sql)
}
}B= $@
| |||||||||||||||||||||||||||||||||
| 99 | or $@ && Carp::croak $@; | ||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ( $self->{dbh}->err ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 0 | Carp::carp $self->{dbh}->errstr; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub do_sql { | ||||||||||||||||||||||||||||||
|
Subroutine
do_sql: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 109 | 0 | my $sql = shift; | |||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | eval { $self->{dbh}->do($sql); } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
eval {
do {
$self->{'dbh'}->do($sql)
}
} || $@ && Carp::croak($@)
$@ and Carp::croak($@)
MC/DC0%0/2
eval {
do {
$self->{'dbh'}->do($sql)
}
} || $@ && Carp::croak($@)
eval {
do {
$self->{'dbh'}->do($sql)
}
}
$@
Truth table0%0/3
A || B
A= eval {
do {
$self->{'dbh'}->do($sql)
}
}B= $@
| |||||||||||||||||||||||||||||||||
| 111 | or $@ && Carp::croak $@; | ||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ( $self->{dbh}->err ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | 0 | Carp::carp $self->{dbh}->errstr; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub has_data { | ||||||||||||||||||||||||||||||
|
Subroutine
has_data: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( ref $self->{data} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | 0 | return (); | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub get_json { | ||||||||||||||||||||||||||||||
|
Subroutine
get_json: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 127 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ( $self->has_data ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 0 | return JSON::Syck::Dump( $self->{data} ); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | 0 | return (); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub clear_data { | ||||||||||||||||||||||||||||||
|
Subroutine
clear_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 135 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 136 | 0 | $self->{data} = (); | |||||||||||||||||||||||||||||||
| 137 | 0 | 1; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub errstr { | ||||||||||||||||||||||||||||||
|
Subroutine
errstr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( $self->{dbh} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 0 | return $self->{dbh}->errstr; | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||
| 146 | 0 | return (); | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub err { | ||||||||||||||||||||||||||||||
|
Subroutine
err: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ( $self->{dbh} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 0 | return $self->{dbh}->err; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||||||
| 156 | 0 | return (); | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 161 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if ( $self->{dbh} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | 0 | $self->{dbh}->disconnect; | |||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | else { | ||||||||||||||||||||||||||||||||
| 166 | 0 | return (); | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 2 | 1; # End of DBIx::JSON | |||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||||||