blib/lib/LibWeb/Database.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 125 CC 12 SCAR 27.2
CC
12
Cov
0%
query
42.0
finish
12.0
disconnect
12.0
CRAP 15.2
1 #==============================================================================
2 # LibWeb::Database -- A generic database driver for libweb applications.
3  
4 package LibWeb::Database;
5  
6 # Copyright (C) 2000 Colin Kong
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #=============================================================================
22  
23 # $Id: Database.pm,v 1.5 2000/07/18 06:33:30 ckyc Exp $
24  
25 #-############################
26 # Use standard library.
27 use strict;
28 use vars qw(@ISA $VERSION);
29 use Carp;
30 require DBI;
31  
32 #-############################
33 # Use custom library.
34 require LibWeb::Core;
35  
36 #-############################
37 # Version.
38 $VERSION = '0.02';
39  
40 #-############################
41 # Inheritance.
42 @ISA = qw(LibWeb::Core);
43  
44 #-############################
45 # Methods.
46 sub new {
47 #
48 # Params: $class [, $rc_file]
49 #
50 # - $class is the class/package name of this package, be it a string
51 # or a reference.
52 # - $rc_file is the absolute path to the rc file for LibWeb.
53 #
54 # Usage: my $object = new LibWeb::Database([$rc_file]);
55 #
56 my ($class, $Class, $self);
57 $class = shift;
58 $Class = ref($class) || $class;
59  
60 # Inherit instance variables from the base class.
61 $self = $Class->SUPER::new(shift);
62 bless($self, $Class);
63  
64 # Any necessary initialization.
65 $self->_init();
66  
67 # Returns a reference to this object.
68 return $self;
69 }
70  
71 sub _init {
72 # Initialization whenever an object of this class is created.
73 my $self = shift;
74  
75 # Connect to the database.
76 eval {
77 $self->{__PACKAGE__.'.dbHandle'} =
78 DBI->connect($self->{DB_SOURCE}, $self->{DB_LOGIN},
79 $self->{DB_PASS}, $self->{DB_OPTIONS});
80 };
81  
82 $self->fatal(
83 -msg => 'Our database is under construction.',
84 -alertMsg =>
85 "LibWeb::Database::_init(): Couldn't connect to database.\n".
86 Carp::longmess("$@ "),
87 -HelpMsg => $self->{HHTML}->database_error()
88 )
89 if ( $@ && $self->{IS_DB_ENABLED} );
90 }
91  
92 sub DESTROY {
93 # Destructor: performs cleanup when this object is not being referenced
94 # any more. For example, disconnect a database connection, filehandle...etc.
95 my $self = shift;
96 $self->done();
97 }
98  
99 sub do {
100 #
101 # Params: ( -sql=> ).
102 #
103 # Pre:
104 # -sql is a valid sql statement.
105 #
106 # Post:
107 # -Return the number of rows affected by the SQL statement, -sql.
108 #
109 my ($self, $sql, $ret);
110 $self = shift;
111 ($sql) = $self->rearrange( ['SQL'], @_ );
112  
113 eval { $ret = $self->{__PACKAGE__. '.dbHandle'}->do($sql); };
114 $self->fatal(
115 -msg => 'Our database is under construction.',
116 -alertMsg =>
117 "LibWeb::Database::do(): $sql failed.\n" . Carp::longmess("$@ ") .
118 $self->{__PACKAGE__. '.dbHandle'}->errstr(),
119 -HelpMsg => $self->{HHTML}->database_error()
120 ) if $@;
121 return $ret;
122 }
123  
124 sub query {
125 #
126 # Params: ( -sql=>, -bind_cols=> [, -want_hash=>].
127 #
128 # Pre:
129 # -sql is the sql statement to perform the query.
130 # -bind_cols is an ARRAY ref. to SCALAR refs. to fields to be bound in
131 # the table specified in -sql.
132 # -want_hash indicates whether the returning function(a ref.) is a ref. to
133 # fetchrow_hashref() or not.
134 #
135 # Post:
136 # -Returns a fetching function(ref.) based on -sql.
137 # A ref. to fetchrow_arrayref() is returned if -want_hash is not defined;
138 # otherwise a ref. to fetchrow_hashref() is returned.
139 #
140 my ($self, $sql, $bind_cols, $want_hash);
141 $self = shift;
142 ($sql, $bind_cols, $want_hash)
143 = $self->rearrange( ['SQL', 'BIND_COLS', 'WANT_HASH'], @_ );
144  
145 # Prepare query statement handle.
146 eval {
147 undef $self->{__PACKAGE__. '.stHandle'} if
148 defined($self->{__PACKAGE__. '.stHandle'});
149 $self->{__PACKAGE__. '.stHandle'} =
150 ($self->{__PACKAGE__. '.dbHandle'}) -> prepare($sql);
151 };
152 $self->fatal(
153 -msg => 'Our database is under construction.',
154 -alertMsg =>
155 "LibWeb::Database::query(): $sql couldn't be prepared.\n " .
156 Carp::longmess("$@ ") . $self->{__PACKAGE__. '.dbHandle'}->errstr(),
157 -HelpMsg => $self->{HHTML}->database_error()
158 )
159 if $@;
160  
161 # Executes the statement.
162 eval { ($self->{__PACKAGE__. '.stHandle'}) -> execute };
163 $self->fatal(
164 -msg => 'Our database is under construction.',
165 -alertMsg =>
166 "LibWeb::Database::query(): $sql execution failed.\n " .
167 Carp::longmess("$@ ") . $self->{__PACKAGE__. '.dbHandle'}->errstr(),
168 -HelpMsg => $self->{HHTML}->database_error()
169 )
170 if $@;
171  
172 # Binds the columns. Note: bind columns need to be an array of SCALAR refs.
173 eval { $self->{__PACKAGE__. '.stHandle'}->bind_columns(undef, @$bind_cols) };
174 $self->fatal(
175 -msg => 'Our database is under construction.',
176 -alertMsg =>
177 "LibWeb::Database::query(): columns binding for $sql failed.\n " .
178 Carp::longmess("$@ ") . $self->{__PACKAGE__. '.dbHandle'}->errstr(),
179 -HelpMsg => $self->{HHTML}->database_error()
180 )
181 if $@;
182
183 # Returns a ref. to a fetching function.
184 return sub { ($self->{__PACKAGE__. '.stHandle'}) -> fetchrow_arrayref(); }
185 unless defined( $want_hash );
186 return sub { ($self->{__PACKAGE__. '.stHandle'}) -> fetchrow_hashref(); };
187 }
188  
189 sub finish {
190 # Finishes a statement execution.
191 my $self = shift;
192 eval {
193 ($self->{__PACKAGE__.'.stHandle'}) -> finish;
194 undef $self->{__PACKAGE__.'.stHandle'};
195 } if defined($self->{__PACKAGE__.'.stHandle'});
196 $self->fatal(-msg => 'Our database is under construction.',
197 -alertMsg =>
198 "LibWeb::Database::finish(): SQL statement failed to finish properly.\n " .
199 Carp::longmess("$@ ") . $self->{__PACKAGE__. '.dbHandle'}->errstr(),
200 -HelpMsg => $self->{HHTML}->database_error()
201 )
202 if $@;
203 }
204  
205 sub disconnect {
206 # Disconnect this database session.
207 my $self = shift;
208 eval {
209 ($self->{__PACKAGE__. '.dbHandle'}) -> disconnect;
210 undef $self->{__PACKAGE__.'.dbHandle'};
211 } if defined($self->{__PACKAGE__.'.dbHandle'});
212 $self->fatal(-msg => 'Our database is under construction.',
213 -alertMsg =>
214 "LibWeb::Database::disconnect(): Database disconnection failed.\n " .
215 Carp::longmess("$@ ") . $self->{__PACKAGE__. '.dbHandle'}->errstr(),
216 -HelpMsg => $self->{HHTML}->database_error()
217 )
218 if $@;
219 }
220  
221 sub done {
222 # Finishes a statement execution and disconnect this database session.
223 my $self = shift;
224 $self->finish();
225 $self->disconnect();
226 }
227  
228 1;
229 __DATA__