File Coverage

blib/lib/DBIx/JSON.pm
Criterion Covered Total %
statement 15 78 19.2
branch 0 32 0.0
condition 0 12 0.0
subroutine 5 15 33.3
pod 8 8 100.0
total 28 145 19.3


line stmt bran cond sub pod time code
1             package DBIx::JSON;
2              
3 1     1   53464 use warnings;
  1         2  
  1         28  
4 1     1   4 use strict;
  1         2  
  1         36  
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             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 1     1   1327 use DBI 1.15 ();
  1         14119  
  1         31  
47 1     1   5 use Carp ();
  1         1  
  1         15  
48 1     1   401 use JSON::Syck;
  1         2594  
  1         545  
49              
50             sub new {
51 0     0 1   my $class = shift;
52 0           my $self = {};
53 0           bless $self, $class;
54 0 0         $self->_init(@_) || return ();
55 0           return $self;
56             }
57              
58             sub _init {
59 0     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 0 0       eval {
66             $self->{dbh} =
67 0           DBI->connect( "dbi:$driver:$dsn", $userid, $passwd,
68             { PrintWarn => 0, PrintError => 1 } );
69             }
70             or $@ && Carp::croak $@;
71 0 0         if ( !$self->{dbh} ) {
72 0           return ();
73             }
74             else {
75 0           $self->{dbh}->{PrintError} = 0;
76             }
77 0           1;
78             }
79              
80             sub do_select {
81 0     0 1   my $self = shift;
82 0           my $sql = shift;
83 0           my $key_field = shift;
84 0           my $hash_array = shift;
85 0 0         if ($key_field) {
86 0 0 0       eval {
87 0           $self->{data} = $self->{dbh}->selectall_hashref( $sql, $key_field );
88             }
89             or $@ && Carp::croak $@;
90 0 0         if ( $self->{dbh}->err ) {
91 0           Carp::carp $self->{dbh}->errstr;
92             }
93 0 0         if ($hash_array) {
94 0           $self->{data} = [ values( %{ $self->{data} } ) ];
  0            
95             }
96             }
97             else {
98 0 0 0       eval { $self->{data} = $self->{dbh}->selectall_arrayref($sql); }
  0            
99             or $@ && Carp::croak $@;
100 0 0         if ( $self->{dbh}->err ) {
101 0           Carp::carp $self->{dbh}->errstr;
102             }
103             }
104 0           return $self;
105             }
106              
107             sub do_sql {
108 0     0 1   my $self = shift;
109 0           my $sql = shift;
110 0 0 0       eval { $self->{dbh}->do($sql); }
  0            
111             or $@ && Carp::croak $@;
112 0 0         if ( $self->{dbh}->err ) {
113 0           Carp::carp $self->{dbh}->errstr;
114             }
115 0           return $self;
116             }
117              
118             sub has_data {
119 0     0 1   my $self = shift;
120 0 0         if ( ref $self->{data} ) {
121 0           return 1;
122             }
123 0           return ();
124             }
125              
126             sub get_json {
127 0     0 1   my $self = shift;
128 0 0         if ( $self->has_data ) {
129 0           return JSON::Syck::Dump( $self->{data} );
130             }
131 0           return ();
132             }
133              
134             sub clear_data {
135 0     0 1   my $self = shift;
136 0           $self->{data} = ();
137 0           1;
138             }
139              
140             sub errstr {
141 0     0 1   my $self = shift;
142 0 0         if ( $self->{dbh} ) {
143 0           return $self->{dbh}->errstr;
144             }
145             else {
146 0           return ();
147             }
148             }
149              
150             sub err {
151 0     0 1   my $self = shift;
152 0 0         if ( $self->{dbh} ) {
153 0           return $self->{dbh}->err;
154             }
155             else {
156 0           return ();
157             }
158             }
159              
160             sub DESTROY {
161 0     0     my $self = shift;
162 0 0         if ( $self->{dbh} ) {
163 0           $self->{dbh}->disconnect;
164             }
165             else {
166 0           return ();
167             }
168             }
169              
170             1; # End of DBIx::JSON
171              
172             __END__