blib/lib/ZConf/DBI/utils.pm

filter: Statementstmt 7.3% 14 / 190 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 28.5% 4 / 14 Podpod 100% 10 / 10 totaltotal 10.6% 28 / 262 CC 25 SCAR 32.0
CC
25
Cov
6%
create_table
56.0
drop_table
42.0
do
20.0
CRAP 24.5
↑ Summary
1 package ZConf::DBI::utils;
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 2 ▶use DBIx::Admin::TableInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DBIx::Admin::CreateTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 ZConf::DBI::utils - Assorted utilities for ZConf::DBI.
11  
12 =head1 VERSION
13  
14 Version 0.0.0
15  
16 =cut
17  
18 2 our $VERSION = '0.0.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 This is primarily meant for quick small things. If you are going to calling a lot
24 of stuff here repeatively/heavily, you are probally going to be better off making use of what
25 ever is being called by the function directly.
26  
27 =head1 METHODS
28  
29 =head2 new
30  
31 This initiates the object.
32  
33 One arguement is required and it is the 'ZConf::DBI' object.
34  
35 my $foo=ZConf::DBI::util->new($zcdbi);
36 if($foo->error){
37 warn('error code:'.$foo->error.': '.$foo->errorString);
38 }
39  
40 =cut
41  
42 0 ▶sub new{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
43 0 my $zcdbi=$_[1];
44 0 my $function='new';
45
46 0 my $self={error=>undef,
47 perror=>undef,
48 errorString=>undef,
49 module=>'ZConf-DBI-util',
50 zcdbi=>$zcdbi,
51 };
52 0 bless $self;
53  
54 #make sure a object was not passed
55 0 ▶ if (!defined( $self->{zcdbi} )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"zcdbi"}) 0 0 0
56 0 $self->{perror}=1;
57 0 $self->{error}=1;
58 0 $self->{errorString}='No ZConf::DBI object passed';
59 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
60 0 return $self;
61 }
62  
63 #make sure it is the correct type of object
64 0 ▶ if (ref($self->{zcdbi}) ne 'ZConf::DBI') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"zcdbi"} ne "ZConf::DBI") 0 0 0
65 0 $self->{perror}=1;
66 0 $self->{error}=2;
67 0 $self->{errorString}='No ZConf::DBI object passed';
68 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
69 0 return $self;
70 }
71  
72 0 return $self;
73 }
74  
75 =head2 create_table
76  
77 This creates a new table using DBIx::Admin::CreateTable->create_table.
78  
79 Three arguements are required. The first is the data source name. The second is
80 the table name. The third is a SQL string describing the columns.
81  
82 $foo->create_table('whatever', 'sometable', 'id char(32) primary key, data varchar(255) not null');
83 if($foo->error){
84 warn('error code:'.$error.': '.$foo->errorString);
85 }
86  
87 =cut
88  
89 0 ▶sub create_table{
Subroutine create_table: not called CC=7 SCAR=40.3
Pod documented
90 0 my $self=$_[0];
91 0 my $dsName=$_[1];
92 0 my $table=$_[2];
93 0 my $sql=$_[3];
94 0 my $function='create_table';
95  
96 #makes sure we have a data source
97 0 ▶ if (!defined($dsName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsName) 0 0 0
98 0 $self->{error}=3;
99 0 $self->{errorString}='No data source name specified';
100 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
101 0 return undef;
102 }
103  
104 #makes sure we have a table
105 0 ▶ if (!defined($table)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table) 0 0 0
106 0 $self->{error}=5;
107 0 $self->{errorString}='No table name specified';
108 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
109 0 return undef;
110 }
111  
112 #makes sure we have a table
113 0 ▶ if (!defined($sql)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sql) 0 0 0
114 0 $self->{error}=7;
115 0 $self->{errorString}='No SQL defined';
116 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
117 0 return undef;
118 }
119  
120 #connect
121 0 my $dbh=$self->{zcdbi}->connect($dsName);
122 0 ▶ if ($self->{zcdbi}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"zcdbi"}->error) 0 0 0
123 0 $self->{error}=6;
124 0 $self->{errorString}='Connect errored';
125 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
126 0 return undef;
127 }
128  
129 #create the dbix object
130 0 my($admin) = DBIx::Admin::CreateTable->new(
131 dbh=>$dbh,
132 );
133 0 ▶ if (!defined($admin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $admin) 0 0 0
134 0 $self->{error}=4;
135 0 $self->{errorString}='DBIx::Admin::CreateTable->new returned undef';
136 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
137 0 return undef;
138 }
139  
140 #create it
141 0 my $returned=$admin->create_table('create table '.$table.' ( '.$sql.' )');
142
143 #if the returned value is empty, but defined, it worked
144 0 ▶ if ($returned eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($returned eq "") 0 0 0
145 0 return 1;
146 }
147  
148 #it did not work as the returned value is not equal to ''
149 0 $self->{error}=8;
150 0 $self->{errorString}='DBIx::Admin::CreateTable->create_table errored. error="'.$returned.'"';
151 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
152 0 return undef;
153 }
154  
155 =head2 do
156  
157 This executes the do statement on a DBH created from the data source.
158  
159 Two arguements are required. The first is the data source. The second
160 is the SQL.
161  
162 $foo->create_table('whatever', 'drop sequence fubar;');
163 if($foo->error){
164 warn('error code:'.$error.': '.$foo->errorString);
165 }
166  
167 =cut
168  
169 0 ▶sub do{
Subroutine do: not called CC=4 SCAR=30.0
Pod documented
170 0 my $self=$_[0];
171 0 my $dsName=$_[1];
172 0 my $sql=$_[2];
173 0 my $function='do';
174  
175 #makes sure we have a data source
176 0 ▶ if (!defined($dsName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsName) 0 0 0
177 0 $self->{error}=3;
178 0 $self->{errorString}='No data source name specified';
179 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
180 0 return undef;
181 }
182  
183 #makes sure we have a table
184 0 ▶ if (!defined($sql)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sql) 0 0 0
185 0 $self->{error}=7;
186 0 $self->{errorString}='No SQL defined';
187 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
188 0 return undef;
189 }
190  
191 #connect
192 0 my $dbh=$self->{zcdbi}->connect($dsName);
193 0 ▶ if ($self->{zcdbi}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"zcdbi"}->error) 0 0 0
194 0 $self->{error}=6;
195 0 $self->{errorString}='Connect errored';
196 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
197 0 return undef;
198 }
199  
200 0 return $dbh->do($sql);
201 }
202  
203 =head2 drop_table
204  
205 This drops a table using DBIx::Admin::CreateTable->drop_table.
206  
207 Two arguements are required. The first is the data source name. The second is
208 the table name.
209  
210 $foo->create_table('whatever', 'sometable');
211 if($foo->error){
212 warn('error code:'.$error.': '.$foo->errorString);
213 }
214  
215 =cut
216  
217 0 ▶sub drop_table{
Subroutine drop_table: not called CC=6 SCAR=37.4
Pod documented
218 0 my $self=$_[0];
219 0 my $dsName=$_[1];
220 0 my $table=$_[2];
221 0 my $function='drop_table';
222  
223 #makes sure we have a data source
224 0 ▶ if (!defined($dsName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsName) 0 0 0
225 0 $self->{error}=3;
226 0 $self->{errorString}='No data source name specified';
227 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
228 0 return undef;
229 }
230  
231 #makes sure we have a table
232 0 ▶ if (!defined($table)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table) 0 0 0
233 0 $self->{error}=5;
234 0 $self->{errorString}='No table name specified';
235 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
236 0 return undef;
237 }
238  
239 #connect
240 0 my $dbh=$self->{zcdbi}->connect($dsName);
241 0 ▶ if ($self->{zcdbi}->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"zcdbi"}->error) 0 0 0
242 0 $self->{error}=6;
243 0 $self->{errorString}='Connect errored';
244 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
245 0 return undef;
246 }
247  
248 #create the dbix object
249 0 my($admin) = DBIx::Admin::CreateTable->new(
250 dbh=>$dbh,
251 );
252 0 ▶ if (!defined($admin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $admin) 0 0 0
253 0 $self->{error}=4;
254 0 $self->{errorString}='DBIx::Admin::CreateTable->new returned undef';
255 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
256 0 return undef;
257 }
258  
259 #create it
260 0 my $returned=$admin->drop_table($table);
261
262 #if the returned value is empty, but defined, it worked
263 0 ▶ if ($returned eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($returned eq "") 0 0 0
264 0 return 1;
265 }
266  
267 #it did not work as the returned value is not equal to ''
268 0 $self->{error}=8;
269 0 $self->{errorString}='DBIx::Admin::CreateTable->drop_table errored. error="'.$returned.'"';
270 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
271 0 return undef;
272 }
273  
274 =head2 error
275  
276 Returns the current error code and true if there is an error.
277  
278 If there is no error, undef is returned.
279  
280 my $error=$foo->error;
281 if($error){
282 warn('error code:'.$error.': '.$foo->errorString);
283 }
284  
285 =cut
286  
287 0 ▶sub error{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
288 0 return $_[0]->{error};
289 }
290  
291 =head2 errorString
292  
293 Returns the error string if there is one. If there is not,
294 it will return undef.
295  
296 my $error=$foo->error;
297 if($error){
298 warn('error code:'.$error.': '.$foo->errorString);
299 }
300  
301 =cut
302  
303 0 ▶sub errorString{
Subroutine errorString: not called CC=1 SCAR=6.9
Pod documented
304 0 return $_[0]->{errorString};
305 }
306  
307 =head2 table_columns
308  
309 This returns a array reference of table column names found by
310 DBIx::Admin::TableInfo->columns.
311  
312 There are three arguements taken. The first, and required, is the data source name.
313 The second, and optional, is the schema name. The third, and optional, is the schema.
314  
315 my $tables=$foo->table_columns('tigerline', 'geometry_columns');
316 if($foo->error){
317 warn('error code:'.$foo->error.': '.$foo->errorString);
318 }
319  
320 =cut
321  
322 0 ▶sub table_columns{
Subroutine table_columns: not called CC=4 SCAR=30.0
Pod documented
323 0 my $self=$_[0];
324 0 my $dsName=$_[1];
325 0 my $table=$_[2];
326 0 my $schema=$_[3];
327 0 my $function='table_columns';
328  
329 0 ▶ if (!defined($dsName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsName) 0 0 0
330 0 $self->{error}=3;
331 0 $self->{errorString}='No data source name specified';
332 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
333 0 return undef;
334 }
335  
336 0 ▶ if (!defined($table)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table) 0 0 0
337 0 $self->{error}=5;
338 0 $self->{errorString}='No table name specified';
339 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
340 0 return undef;
341 }
342  
343 0 my $dbh=$self->{zcdbi}->connect($dsName);
344  
345 0 my($admin) = DBIx::Admin::TableInfo->new(
346 dbh=>$dbh,
347 schema=>$schema,
348 );
349  
350 0 ▶ if (!defined($admin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $admin) 0 0 0
351 0 $self->{error}=4;
352 0 $self->{errorString}='DBIx::Admin::TableInfo->new returned undef';
353 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
354 0 return undef;
355 }
356  
357 0 return $admin->columns($table);
358 }
359  
360 =head2 table_info
361  
362 This returns a hash reference of table column names found by
363 DBIx::Admin::TableInfo->info.
364  
365 There are two arguements taken. The first, and required, is the data source name.
366 The second, and optional, is the schema name.
367  
368 my $tables=$foo->table_info('tigerline', 'geometry_columns');
369 if($foo->error){
370 warn('error "'.$foo->error.'"');
371 }
372  
373 =cut
374  
375 0 ▶sub table_info{
Subroutine table_info: not called CC=3 SCAR=24.8
Pod documented
376 0 my $self=$_[0];
377 0 my $dsName=$_[1];
378 0 my $schema=$_[2];
379 0 my $function='table_info';
380  
381 0 ▶ if (!defined($dsName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsName) 0 0 0
382 0 $self->{error}=3;
383 0 $self->{errorString}='No data source name specified';
384 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
385 0 return undef;
386 }
387  
388 0 my $dbh=$self->{zcdbi}->connect($dsName);
389  
390 0 my($admin) = DBIx::Admin::TableInfo->new(
391 dbh=>$dbh,
392 schema=>$schema,
393 );
394  
395 0 ▶ if (!defined($admin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $admin) 0 0 0
396 0 $self->{error}=4;
397 0 $self->{errorString}='DBIx::Admin::TableInfo->new returned undef';
398 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
399 0 return undef;
400 }
401  
402 0 return $admin->info;
403 }
404  
405 =head2 tables
406  
407 This returns a array refernce of table names found by
408 DBIx::Admin::TableInfo->tables.
409  
410 There are two arguements taken. The first, and required, is the data source name.
411 The second, and optional, is the schema name.
412  
413 my $tables=$foo->tables('tigerline');
414 if($foo->error){
415 warn('error "'.$foo->error.'"');
416 }
417  
418 =cut
419  
420 0 ▶sub tables{
Subroutine tables: not called CC=3 SCAR=24.8
Pod documented
421 0 my $self=$_[0];
422 0 my $dsName=$_[1];
423 0 my $schema=$_[2];
424 0 my $function='tables';
425  
426 0 ▶ if (!defined($dsName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dsName) 0 0 0
427 0 $self->{error}=3;
428 0 $self->{errorString}='No data source name specified';
429 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
430 0 return undef;
431 }
432  
433 0 my $dbh=$self->{zcdbi}->connect($dsName);
434  
435 0 my($admin) = DBIx::Admin::TableInfo->new(
436 dbh=>$dbh,
437 schema=>$schema,
438 );
439  
440 0 ▶ if (!defined($admin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $admin) 0 0 0
441 0 $self->{error}=4;
442 0 $self->{errorString}='DBIx::Admin::TableInfo->new returned undef';
443 0 warn($self->{module}.' '.$function.':'.$self->{error}.': '.$self->{errorString});
444 0 return undef;
445 }
446  
447 0 return $admin->tables;
448 }
449  
450 =head2 errorblank
451  
452 This blanks the error storage and is only meant for internal usage.
453  
454 It does the following.
455  
456 $self->{error}=undef;
457 $self->{errorString}=undef;
458  
459 =cut
460  
461 #blanks the error flags
462 0 ▶sub errorblank{
Subroutine errorblank: not called CC=2 SCAR=17.9
Pod documented
463 0 my $self=$_[0];
464  
465 0 ▶ if ($self->{perror}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perror"}) 0 0 0
466 0 warn('ZConf-DevTemplate errorblank: A permanent error is set');
467 0 return undef;
468 }
469  
470 0 $self->{error}=undef;
471 0 $self->{errorString}=undef;
472  
473 0 return 1;
474 }
475  
476 =head1 ERROR CODES
477  
478 =head2 1
479  
480 No ZConf::DBI object passed.
481  
482 =head2 2
483  
484 The passed object is not a ZConf::DBI object.
485  
486 =head2 3
487  
488 No data source name specified.
489  
490 =head2 4
491  
492 DBIx::Admin::TableInfo->new returned undef.
493  
494 =head2 5
495  
496 No table specified.
497  
498 =head2 6
499  
500 Connect errored.
501  
502 =head2 7
503  
504 No SQL defined.
505  
506 =head2 8
507  
508 Creating the table frailed.
509  
510 =head2 9
511  
512 Dropping the table failed.
513  
514 =head1 AUTHOR
515  
516 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
517  
518 =head1 BUGS
519  
520 Please report any bugs or feature requests to C<bug-zconf-dbi-utils at rt.cpan.org>, or through
521 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ZConf-DBI-utils>. I will be notified, and then you'll
522 automatically be notified of progress on your bug as I make changes.
523  
524  
525  
526  
527 =head1 SUPPORT
528  
529 You can find documentation for this module with the perldoc command.
530  
531 perldoc ZConf::DBI::utils
532  
533  
534 You can also look for information at:
535  
536 =over 4
537  
538 =item * RT: CPAN's request tracker
539  
540 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ZConf-DBI-utils>
541  
542 =item * AnnoCPAN: Annotated CPAN documentation
543  
544 L<http://annocpan.org/dist/ZConf-DBI-utils>
545  
546 =item * CPAN Ratings
547  
548 L<http://cpanratings.perl.org/d/ZConf-DBI-utils>
549  
550 =item * Search CPAN
551  
552 L<http://search.cpan.org/dist/ZConf-DBI-utils/>
553  
554 =back
555  
556  
557 =head1 ACKNOWLEDGEMENTS
558  
559  
560 =head1 COPYRIGHT & LICENSE
561  
562 Copyright 2010 Zane C. Bowers, all rights reserved.
563  
564 This program is free software; you can redistribute it and/or modify it
565 under the same terms as Perl itself.
566  
567  
568 =cut
569  
570 2 1; # End of ZConf::DBI::utils
↑ Summary