File Coverage

blib/lib/DBIx/File2do.pm
Criterion Covered Total %
statement 24 47 51.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 7 10 70.0
pod 0 3 0.0
total 33 65 50.7


line stmt bran cond sub pod time code
1             package DBIx::File2do;
2              
3 1     1   56362 use strict;
  1         3  
  1         46  
4 1     1   10985 use DBI;
  1         59197  
  1         74  
5              
6             BEGIN {
7 1     1   12 use Carp;
  1         8  
  1         97  
8 1     1   5 use vars qw($VERSION $PACKAGE);
  1         2  
  1         84  
9 1     1   2 $VERSION = '0.001';
10              
11 1         2 $Carp::CarpLevel = 1;
12 1         402 $PACKAGE = "DBIx::File2do";
13              
14 1     1   5 use constant DEBUG => 0;
  1         1  
  1         75  
15             }
16              
17              
18             sub new {
19 1     1 0 12 my ($class, @args) = @_;
20              
21 1   33     8 my $self = bless ({}, ref ($class) || $class);
22              
23 1 50       5 if ( !defined $args[0] ) {
24 0         0 croak "$PACKAGE->new requires one value. \$dbh\n";
25             }
26 1         6 $self->{_dbh} = $args[0];
27              
28 1         2 if (DEBUG) {
29             select (STDOUT);
30             $| = 1;
31             #use Data::Dumper;
32             }
33              
34 1         4 return $self;
35             }
36              
37 0     0     sub DESTROY() {
38             }
39              
40             =head1 NAME
41              
42             DBIx::File2do - Execute SQL scripts from Perl.
43              
44             =head1 SYNOPSIS
45              
46             use File2do;
47             $SQL = File2do->new($dbh);
48              
49             ... do something ...
50              
51             $SQL->execute_script('filename.txt');
52              
53             ... do something else ...
54              
55             $SQL->execute_script('file_name.sql');
56              
57             =head1 DESCRIPTION
58              
59             This module will run a SQL script from Perl.
60              
61             =head1 USAGE
62              
63             use File2do;
64             $SQL=File2do->new($dbh); ### pass the db handle.
65             $SQL->execute_script('filename.txt');
66              
67             =head1 AUTHOR
68              
69             Jack Bilemjian
70             CPAN ID: JACKB
71             jck000@gmail.com
72              
73             =head1 COPYRIGHT
74              
75             This program is free software; you can redistribute
76             it and/or modify it under the same terms as Perl itself.
77              
78             The full text of the license can be found in the
79             LICENSE file included with this module.
80              
81              
82             =head1 SEE ALSO
83              
84             perl(1), DBI.
85              
86             =cut
87              
88             sub execute_script($) {
89              
90 0     0 0   my $self = shift;
91 0           my $script_name = shift;
92 0           my ($script, ### script content
93             @sql_commands, ### individual sql commands
94             $cmd,
95             $lineno,
96             $rv);
97              
98 0           $script = slurp_script("$script_name"); ### Read the script
99 0           $script =~ s/\n/ /g;
100 0           $script =~ s/$/ /g;
101 0           @sql_commands = split('\;', $script); ### Break up into individual commands
102              
103 0           $lineno=0;
104 0           foreach $cmd (@sql_commands) {
105 0           $lineno++;
106 0           if (DEBUG) {
107             print "$lineno) $cmd\n";
108             }
109 0           $rv = $self->{_dbh}->do("$cmd");
110             }
111              
112 0           return $rv;
113             }
114              
115             sub slurp_script($) {
116 0     0 0   my $file_name = shift;
117 0           my $script;
118 0           local $/;
119              
120 0           $file_name = $file_name;
121              
122 0           open(READFILE, $file_name);
123 0           $script = ;
124 0           close(READFILE);
125              
126 0           return $script;
127             }
128              
129              
130             1;