Store/DBI.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 156 CC 15 SCAR 41.7
CC
15
Cov
0%
new
132.0
store
20.0
has
6.0
CRAP 64.9
1 package FileMetadata::Store::DBI;
2
3 our $VERSION = '0.1';
4
5 =head1 NAME
6
7 FileMetadata::Store::DBI
8
9 =cut
10
11 use DBI;
12 use DBI qw(:sql_types);
13 use strict;
14
15 =head1 DESCRIPTION
16
17 This modules implements a store for the FileMetadata framework using a
18 Perl DBI supported database. Such a database should be available and a
19 table must be designed to hold the required data. This module does not
20 implement any methods to retrieve the information from the database
21 - except for the has() method.
22
23 =head1 METHODS
24
25 =head2 new
26
27 See L<FileMetadata::Store>
28
29 The new method validates the config hash passed to it. It also connects to
30 the database and prepares a statement used for inserting values into
31 the database by store when all columns are available. This method will
32 call 'die' if any errors occur with the config hash or while performing
33 database operations.
34
35 The format for the config hash is as follows
36
37 {
38
39 database =&gt; 'DBI:mysql:test',
40
41 username =&gt; 'user',
42
43 password =&gt; 'pass',
44
45 table =&gt; 'table',
46
47 column =&gt; [{name =&gt; 'id_column',
48
49 property =&gt; 'ID',
50
51 type =&gt; 'SQL_STRING'},
52
53 {name =&gt; 'timestamp_column',
54
55 property =&gt; 'TIMESTAMP',
56
57 type =&gt; 'SQL_INTEGER'},
58
59 {name =&gt; 'FileMetadata::Miner::HTML::author',
60
61 default =&gt; 'Jules Verne'}]
62
63 }
64
65 database, username and password are used to make a connection to the
66 SQL database. See L<DBI/"connect"> for the correct format for these
67 keys. The column key defines association of column names to property names.
68 The name key and the property key are required. Optionally the SQL type
69 of the value can be specified using 'type'. See L<DBI> for more information on
70 possible values. The 'default' key specifies values to be used if no
71 information is available for the corresponding attribute.
72
73 This XML is parsed into the above hash by XML::Simple::XMLin($xml,
74 keyattr =&gt; [])
75
76 =cut
77
78 sub new {
79
80 my $self = {};
81 bless $self, shift;
82 my $config = shift;
83 die "Config hash was not supplied" unless defined $config;
84
85 # Construct columns array from config hash
86
87 die "config/column not present"
88 unless defined $config->{'column'};
89 die "config/column is incorrect"
90 unless (ref ($config->{'column'}) == 'HASH');
91 die "config/table not present"
92 unless defined $config->{'table'};
93
94 my @columns = @{$config->{'column'}};
95
96 $self->{'columns'} = $config->{'column'};
97 $self->{'table'} = $config->{'table'};
98
99 #
100 # Construct database handle and the default prepared statement
101 #
102
103 $self->{'dbh'} = DBI->connect ($config->{'database'},
104 $config->{'username'},
105 $config->{'password'})
106 or die DBI->errstr;
107
108 my $sql = "INSERT into $config->{'table'} (";
109 my $sql_vals = "";
110
111 foreach (@columns) {
112
113 die "config/column/name is needed" unless defined $_->{'name'};
114 die "config/column/property is needed" unless defined $_->{'property'};
115
116 $sql .= "$_->{'name'},";
117 $sql_vals .= "?,";
118
119 # Record coulmn names for ID and TIMESTAMP. Used by other methods
120
121 if ($_->{'property'} eq "ID") {
122 $self->{'index_col'} = $_->{'name'};
123 } elsif ($_->{'property'} eq "TIMESTAMP") {
124 $self->{'timestamp_col'} = $_->{'name'};
125 }
126
127 }
128
129 die "config/column/name='ID' needs to be defined"
130 unless defined $self->{'index_col'};
131
132 die "config/column/name='TIMESTAMP' needs to be defined"
133 unless defined $self->{'timestamp_col'};
134
135 # Delete trailing commas
136
137 $sql =~ s/\,$//;
138 $sql_vals =~ s/\,$//;
139
140 $sql .= ") VALUES (" . $sql_vals . ")";
141
142 #
143 # This prepared statement is used by the store() method when all columns
144 # are available
145 #
146
147 $self->{'sth'} = $self->{'dbh'}->prepare ($sql) or die DBI->errstr;
148 return $self;
149 }
150
151 =head2 store
152
153 See L<FileMetadata::Store/"store">
154
155 The store performs an INSERT into the database. Only properties specified in
156 the config hash passed to the new() method are inserted.
157
158 =cut
159
160 sub store {
161
162 my ($self, $meta) = @_;
163 my @columns = @{$self->{'columns'}};
164 my $use_prepared = 1; # USe to flag if any columns are missing
165 my $i = 0; # Index while looping through the columns
166
167 #
168 # The following variables are used to construct a new SQL statemrnt
169 # in case the prepared statement cannot be used.
170 #
171
172 my $sql = "INSERT into $self->{'table'} (";
173 my $sql_vals = "";
174
175 foreach (@columns) {
176
177 $i++;
178
179 my ($item, $value);
180 $item = $_->{'property'};
181
182 # See if the item is in the hash
183
184 if (defined ($meta->{$item})) {
185 $value = $meta->{$item};
186 } else {
187 $value = $_->{'default'};
188 }
189
190 if (defined $value) {
191 $sql .= "$_->{'name'},";
192 $sql_vals .= $self->{'dbh'}->quote($value) . ",";
193 $self->{'sth'}->bind_param ($i,
194 $value,
195 eval ($_->{'type'}));
196 } else {
197 $use_prepared = 0;
198 }
199 }
200
201 # Remove any items with the same id
202 $self->remove ($meta->{'ID'});
203
204 # Do the insert
205
206 if ($use_prepared) {
207 $self->{'sth'}->execute() or die DBI->errstr;
208 } else {
209 $sql =~ s/\,$//;
210 $sql_vals =~ s/\,$//;
211 $sql .= ") VALUES (" . $sql_vals . ")";
212
213 $self->{'dbh'}->do ($sql) or die DBI->errstr;
214 }
215 }
216
217 =head2 remove
218
219 See L<FileMetadata::Store/"remove">
220
221 The store performs a DELETE on the database.
222
223 =cut
224
225 sub remove {
226
227 my ($self, $id) = @_;
228 $id = $self->{'dbh'}->quote($id), "\n";
229
230 $self->{'dbh'}->do ("DELETE from $self->{'table'}"
231 . " WHERE $self->{'index_col'}=$id")
232 or die DBI->errstr;
233 }
234
235 =head2 clear
236
237 See L<FileMetadata::Store/"clear">
238
239 The store performs a DELETE on the database.
240
241 =cut
242
243 sub clear {
244
245 my $self = shift;
246
247 $self->{'dbh'}->do ("DELETE from $self->{'table'}")
248 or die DBI->errstr;
249
250 }
251
252 =head2 has
253
254 See L<FileMetadata::Store/"has">
255
256 The has method queries the database for the row containing a 'ID' or equivalent
257 column value of the requested identifier. The column name for the 'ID' property
258 is identified from the config has passed to the constructor. The insertion
259 time of this row is identified from the timestamp column specified in the
260 config hash passed to the new() method.
261
262 =cut
263
264 sub has {
265
266 my ($self, $id) = @_;
267
268 $id = $self->{'dbh'}->quote($id);
269 my $list_st = $self->{'dbh'}->prepare (
270 "SELECT $self->{'timestamp_col'} "
271 . "FROM $self->{'table'}"
272 . " WHERE $self->{'index_col'}=$id");
273 $list_st->execute();
274 my $results = $list_st->fetchall_arrayref();
275
276 # Return true only if one result will be returned
277 if ($#{$results} == 0) {
278 return @{@{$results}[0]}[0];
279 } else {
280 return undef;
281 }
282 }
283
284 =head2 list
285
286 See L<FileMetadata::Store/"has">
287
288 The list method queries the database for the 'ID' or equivalent column of
289 all rows and returns an aray reference to a list of these values. The column
290 name for the 'ID' property is identified from the config has passed to the
291 new () method.
292
293 =cut
294
295 sub list {
296
297 my $self = shift;
298 my $list_st =
299 $self->{'dbh'}->prepare (
300 "SELECT $self->{'index_col'} FROM $self->{'table'}");
301 $list_st->execute();
302 my $results = $list_st->fetchall_arrayref();
303
304 # Make a list from the two dimensional array references
305
306 my @list = ();
307 foreach (@{$results}) {
308 push @list, $$_[0];
309 }
310
311 return \@list;
312 }
313
314 =head2 finish
315
316 This method releases the prepared statement and disconnects from the
317 database.
318
319 =cut
320
321 sub finish {
322
323 my $self = shift;
324
325 # Free up database resources explicitly
326 $self->{'sth'}->finish();
327 $self->{'dbh'}->disconnect();
328
329 }
330
331 1;
332
333 =head1 VERSION
334
335 0.1 - This is the first release
336
337 =head1 REQUIRES
338
339 DBI
340
341 =head1 AUTHOR
342
343 Midh Mulpuri midh@enjine.com
344
345 =head1 LICENSE
346
347 This software can be used under the terms of any Open Source Initiative
348 approved license. A list of these licenses are available at the OSI site -
349 http://www.opensource.org/licenses/
350
351 =cut