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