lib/SQL/Loader.pm

filter: Statementstmt 40.6% 65 / 160 Branchbran 37.9% 22 / 58 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 59.0% 13 / 22 Podpod 100% 12 / 12 totaltotal 41.4% 112 / 270 CC 36 SCAR 32.9
CC
36
Cov
38%
run
110.0
_print_http_headers
30.0
_work_out_table_cols
20.0
CRAP 27.0
« lib/SQL/Loader/MySQL.pm ↑ Summary blib/lib/SQL/sql_loader_example.pl »
1 package SQL::Loader;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.02';
10  
11 =head1 NAME
12  
13 SQL::Loader
14  
15 =head1 SYNOPSIS
16  
17 use base qw( SQL::Loader );
18  
19 =head1 DESCRIPTION
20  
21 Base class for L<SQL::Loader::MySQL>
22  
23 =head1 SEE ALSO
24  
25 L<SQL::Loader::MySQL>
26  
27 =head1 METHODS
28  
29 =cut
30  
31 =head2 new
32  
33 constructor
34  
35 =cut
36 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
37 5 my $class = shift;
38 5 ▶ if ( $class eq 'SQL::Loader' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "SQL::Loader") 1 4 5
39 1 die __PACKAGE__ . "cannot be called directly\n";
40 }
41 4 my $self = {};
42 4 bless $self, $class;
43 4 $self->_init(@_);
44 3 return $self;
45 }
46  
47 =head2 run
48  
49 main app loop
50  
51 =cut
52 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod documented
53 0 my $self = shift;
54  
55 0 my $tcounter = 0;
56 0 my $tcounted = 0;
57 0 my $purpose = 0;
58 0 my $table = 0;
59 0 my $content;
60 0 my $title = undef;
61  
62 # sql
63 0 my $titles = [];
64 0 my $col_vals = [];
65 0 my $table_col = [];
66  
67 0 my $url = $self->{url};
68 0 my $print_http_headers = $self->print_http_headers();
69  
70 # server response test only and quit
71 0 ▶ return $self->_print_http_headers if $print_http_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $print_http_headers 0 0 0
72  
73 0 $content = get($url);
74 0 ▶ die "couldn't get URL $url: $!\n" if !$content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
75
76 0 my @arr = split(/\n+/, $content);
77  
78 # start loop - find each table
79 0 LINE: foreach my $line (@arr) {
80 0 chomp($line);
81  
82 # find title
83 0 ▶ if (!defined($title)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $title) 0 0 0
84 0 $title = $self->_set_table_title($line, $titles, $title);
85 0 next LINE;
86 }
87  
88 # find the 'purpose' keyword and start of new table, indicating new, valid table
89 0 ($purpose, $table) = $self->_set_purpose_and_table($line, $purpose, $table);
90 0 ▶ next LINE unless ($purpose && $table);
Branch: 1 branch
BranchTrueFalseTotal
unless $purpose and $table 0 0 0
Condition0%0/3
$purpose and $table
!ll&&!rl&&r
000
MC/DC0%0/2
$purpose and $table
$purpose $table
Truth table0%0/3
A and B
A= $purposeB= $table
ABresult
0X0
100
111
91  
92 # we've found title, purpose and hit start of a table for db
93 0 ▶ if ($purpose && $table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($purpose and $table) 0 0 0
Condition0%0/3
$purpose and $table
!ll&&!rl&&r
000
MC/DC0%0/2
$purpose and $table
$purpose $table
Truth table0%0/3
A and B
A= $purposeB= $table
ABresult
0X0
100
111
94 # get all the column names
95 0 $self->_work_out_table_cols($line, $col_vals, $table_col);
96 0 $col_vals = [];
97  
98 0 ▶ if ($line =~ /^<\/table/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^</table]) 0 0 0
99 # end of table - create this one
100 0 $self->create_table($titles, $table_col);
101 0 $purpose = 0;
102 0 $table = 0;
103 0 $title = undef;
104 0 $titles = [];
105 0 $table_col = [];
106 0 next LINE;
107 }
108 }
109 }
110 0 $self->dbh->disconnect();
111 }
112  
113 =head2 create_table
114  
115 create the sql tables. must be overridden in subclass.
116  
117 =cut
118 0 ▶sub create_table {
Subroutine create_table: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift;
120 0 die __PACKAGE__ . "->create_table is abstract\n";
121 }
122  
123 =head2 _work_out_table_cols
124  
125 define the columns of the given table
126  
127 =cut
128 0 ▶sub _work_out_table_cols {
Subroutine _work_out_table_cols: not called CC=4 SCAR=30.0
129 0 my $self = shift;
130 0 my $line = shift;
131 0 my $col_vals = shift;
132 0 my $table_col = shift;
133  
134 0 ▶ if ($line =~ /^<tr><td/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^<tr><td/) 0 0 0
135 # print "found new col..\n";
136 0 my @row_items = split(/<\/td>/, $line);
137 0 ▶ ROW: for (my $i=0;$i<$#row_items;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#row_items) 0 0 0
138 0 ▶ if ($row_items[$i] =~ /">\s*([\w -\\\/]+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_items[$i] =~ m[">\s*([\w -\\/]+)\s*$]) 0 0 0
139 0 my $rname = $1;
140 0 $rname = $self->_clean($rname);
141 0 push @{$col_vals}, $rname; # sql
142 }
143 }
144 0 push @{$table_col}, $col_vals;
145 }
146 0 return $table_col;
147 }
148  
149 =head2 _set_purpose_and_table
150  
151 helper sub to determine if a table is valid and should be created. a table is considered valid if it has
152 a 'Purpose' defined on the twiki page.
153  
154 This sub gets called repeatedly once a purpose has been found until a <table> line is found,
155 indicating start of a db table.
156  
157 =cut
158 0 ▶sub _set_purpose_and_table {
Subroutine _set_purpose_and_table: not called CC=4 SCAR=30.0
159 0 my $self = shift;
160 0 my $line = shift;
161 0 my $purpose = shift;
162 0 my $table = shift;
163  
164 0 ▶ if ($line =~ /Purpose\s*<\/h3>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[Purpose\s*</h3>]) 0 0 0
165 # <h3> line indicates purpose
166 0 $purpose = 1;
167 }
168 0 ▶ if (($purpose) && ($line =~ /<table/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($purpose and $line =~ /<table/) 0 0 0
Condition0%0/3
$purpose and $line =~ /<table/
!ll&&!rl&&r
000
MC/DC0%0/2
$purpose and $line =~ /<table/
$purpose $line =~ /<table/
Truth table0%0/3
A and B
A= $purposeB= $line =~ /<table/
ABresult
0X0
100
111
169 0 $table = 1;
170 }
171  
172 0 return ($purpose, $table);
173 }
174  
175 =head2 _set_table_title
176  
177 helper sub to extract the name that should be used for the table currently being created.
178  
179 =cut
180 0 ▶sub _set_table_title {
Subroutine _set_table_title: not called CC=2 SCAR=17.9
181 0 my $self = shift;
182 0 my $line = shift;
183 0 my $titles = shift;
184 0 my $title = shift;
185  
186 0 ▶ if ($line =~ /(\w+)\s*<\/h2>$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[(\w+)\s*</h2>$]) 0 0 0
187 # <h2> line is considered title of table
188 0 $title = $1;
189 0 $self->_clean($title);
190 0 push @{$titles}, $title;
191 }
192  
193 0 return $title;
194 }
195  
196 =head2 _clean
197  
198 helper sub to clean leading, tailing and excessive whitespace from a string.
199  
200 =cut
201 0 ▶sub _clean {
Subroutine _clean: not called CC=1 SCAR=6.9
202 0 my $self = shift;
203 0 my $s = shift;
204 0 $s =~ s/^\s+//;
205 0 $s =~ s/\s+$//;
206 0 $s =~ s/\s+/ /;
207 0 $s;
208 }
209  
210 =head2 _print_http_headers
211  
212 print headers option, invoked if the --print-http-headers switch is used. Use to test server response for example. Does not rebuild database.
213  
214 =cut
215 0 ▶sub _print_http_headers {
Subroutine _print_http_headers: not called CC=5 SCAR=34.0
216 0 my $self = shift;
217 0 my $url = $self->{url};
218  
219 0 my @head = head($url);
220 0 ▶ if (!@head) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @head) { } 0 0 0
221 0 die "couldn't get URL $url: $!\n";
222 }
223 else {
224 0 foreach (@head) {
225 0 ▶ next if !defined($_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
226 0 chomp();
227 0 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
228 0 print $_, "\n";
229 }
230 }
231 }
232  
233 =head2 _init
234  
235 initialize class
236  
237 =cut
238 4 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.0
239 4 my $self = shift;
240 4 my (%args) = @_;
241  
242 4 $self->print_http_headers( $args{print_http_headers} );
243 4 $self->url( $args{url} );
244 4 ▶ die "no URL specified\n" if ( !$self->url() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->url 1 3 4
245 3 ▶ if ( $self->print_http_headers() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->print_http_headers) 1 2 3
246 1 $self->initialized( 1 );
247 1 return $self;
248 }
249
250 2 $self->dbname( $args{dbname} );
251 2 ▶ die "no dbname specified\n" if ( !$self->dbname );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbname 0 2 2
252 2 $self->dbuser( $args{dbuser} );
253 2 ▶ die "no dbuser specified\n" if ( !$self->dbuser );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbuser 0 2 2
254 2 $self->dbpass( $args{dbpass} );
255 2 ▶ die "no dbpass specified\n" if ( !$self->dbpass );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dbpass 0 2 2
256 2 $self->quiet( $args{quiet} );
257  
258 2 $self->initialized( 1 );
259  
260 2 return $self;
261 }
262  
263 =head2 initialized
264  
265 get/set initialized param
266  
267 =cut
268 4 ▶sub initialized {
Subroutine initialized: called CC=2 SCAR=6.9
Pod documented
269 4 my ( $self, $initialized ) = @_;
270 4 ▶ if ( $initialized ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($initialized) 3 1 4
271 3 $self->{initialized} = $initialized;
272 }
273 4 return $self->{initialized};
274 }
275  
276 =head2 print_http_headers
277  
278 get/set print_http_headers param
279  
280 =cut
281 7 ▶sub print_http_headers {
Subroutine print_http_headers: called CC=2 SCAR=6.9
Pod documented
282 7 my ( $self, $print_http_headers ) = @_;
283 7 ▶ if ( $print_http_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print_http_headers) 1 6 7
284 1 $self->{print_http_headers} = $print_http_headers;
285 }
286 7 return $self->{print_http_headers};
287 }
288  
289 =head2 url
290  
291 get/set url to be scraped
292  
293 =cut
294 9 ▶sub url {
Subroutine url: called CC=2 SCAR=6.9
Pod documented
295 9 my ( $self, $url ) = @_;
296 9 ▶ if ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 3 6 9
297 3 $self->{url} = $url;
298 }
299 9 return $self->{url};
300 }
301  
302 =head2 dbname
303  
304 get/set dbname
305  
306 =cut
307 5 ▶sub dbname {
Subroutine dbname: called CC=2 SCAR=6.9
Pod documented
308 5 my ( $self, $dbname ) = @_;
309 5 ▶ if ( $dbname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbname) 2 3 5
310 2 $self->{dbname} = $dbname;
311 }
312 5 return $self->{dbname};
313 }
314  
315 =head2 dbuser
316  
317 get/set dbuser
318  
319 =cut
320 5 ▶sub dbuser {
Subroutine dbuser: called CC=2 SCAR=6.9
Pod documented
321 5 my ( $self, $dbuser ) = @_;
322 5 ▶ if ( $dbuser ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbuser) 2 3 5
323 2 $self->{dbuser} = $dbuser;
324 }
325 5 return $self->{dbuser};
326 }
327  
328 =head2 dbpass
329  
330 get/set dbpass
331  
332 =cut
333 5 ▶sub dbpass {
Subroutine dbpass: called CC=2 SCAR=6.9
Pod documented
334 5 my ( $self, $dbpass ) = @_;
335 5 ▶ if ( $dbpass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbpass) 2 3 5
336 2 $self->{dbpass} = $dbpass;
337 }
338 5 return $self->{dbpass};
339 }
340  
341 =head2 dbh
342  
343 get/set database handle
344  
345 =cut
346 0 ▶sub dbh {
Subroutine dbh: not called CC=3 SCAR=24.8
Pod documented
347 0 my $self = shift;
348 0 ▶ if ( !defined( $self->{dbh} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"dbh"}) 0 0 0
349 0 ▶ my $dbh = DBI->connect( $self->connect_string(),
Condition0%0/2
'DBI'->connect($self->connect_string, {'RaiseError', 1, 'AutoCommit', 0, 'ChopBlanks', 1}) || die('DBI'->errstr)
l!l
00
MC/DC0%0/1
'DBI'->connect($self->connect_string, {'RaiseError', 1, 'AutoCommit', 0, 'ChopBlanks', 1}) || die('DBI'->errstr)
'DBI'->connect($self->connect_string, {'RaiseError', 1, 'AutoCommit', 0, 'ChopBlanks', 1})
Truth table0%0/2
A
A= 'DBI'->connect($self->connect_string, {'RaiseError', 1, 'AutoCommit', 0, 'ChopBlanks', 1})
Aresult
11
00
350 { RaiseError => 1,
351 AutoCommit => 0,
352 ChopBlanks => 1
353 }
354 ) || die DBI->errstr;
355 0 $self->{dbh} = $dbh;
356 }
357 0 return $self->{dbh};
358 }
359  
360 =head2 quiet
361  
362 get/set quiet param
363  
364 =cut
365 2 ▶sub quiet {
Subroutine quiet: called CC=2 SCAR=7.6
Pod documented
366 2 my ( $self, $quiet ) = @_;
367 2 ▶ if ( $quiet ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quiet) 0 2 2
368 0 $self->{quiet} = $quiet;
369 }
370 2 return $self->{quiet};
371 }
372  
373 =head2 connect_string
374  
375 return dbh connect string. must be overridden in subclass.
376  
377 =cut
378 0 ▶sub connect_string {
Subroutine connect_string: not called CC=1 SCAR=6.9
Pod documented
379 0 my $self = shift;
380 0 die __PACKAGE__ . "->connect_string() is abstract\n";
381 }
382  
383 2 1;
384  
385 __END__
« lib/SQL/Loader/MySQL.pm ↑ Summary blib/lib/SQL/sql_loader_example.pl »