Prog/Aggregator/DBI.pm

filter: Statementstmt 21.3% 25 / 117 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 63.6% 7 / 11 Podpod 100% 2 / 2 totaltotal 16.1% 34 / 210 CC 40 SCAR 59.5
CC
40
Cov
14%
init
702.0
_do_table
72.0
crawl
42.0
CRAP 382.5
↑ Summary Prog/Aggregator/Spider.pm »
1 package SWISH::Prog::Aggregator::DBI;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw( SWISH::Prog::Aggregator );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use SWISH::Prog::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 __PACKAGE__->mk_accessors(
12 qw( db alias_columns schema use_quotes quote_char ));
13  
14 1 our $VERSION = '0.75';
15  
16 1 my $XMLer = Search::Tools::XML->new(); # included in Utils
17  
18 =pod
19  
20 =head1 NAME
21  
22 SWISH::Prog::Aggregator::DBI - index DB records with Swish-e
23  
24 =head1 SYNOPSIS
25
26 use SWISH::Prog::Aggregator::DBI;
27 use Carp;
28
29 my $aggregator = SWISH::Prog::Aggregator::DBI->new(
30 db => [
31 "DBI:mysql:database=movies;host=localhost;port=3306",
32 'some_user', 'some_secret_pass',
33 {
34 RaiseError => 1,
35 HandleError => sub { confess(shift) },
36 }
37 ],
38 schema => {
39 'moviesIlike' => {
40 title => {type => 'char', bias => 1},
41 synopsis => {type => 'char', bias => 1},
42 year => {type => 'int', bias => 1},
43 director => {type => 'char', bias => 1},
44 producer => {type => 'char', bias => 1},
45 awards => {type => 'char', bias => 1},
46 date => {type => 'date', bias => 1},
47 swishdescription => { synopsis => 1, producer => 1 },
48 swishtitle => 'title',
49 }
50 }
51 use_quotes => 1,
52 quote_char => '`', # backtick
53 alias_columns => 1,
54 indexer => SWISH::Prog::Indexer::Native->new,
55 );
56
57 $aggregator->crawl();
58  
59  
60 =head1 DESCRIPTION
61  
62 SWISH::Prog::Aggregator::DBI is a SWISH::Prog::Aggregator subclass
63 designed for providing full-text search for databases.
64  
65 =head1 METHODS
66  
67 Since SWISH::Prog::Aggregator::DBI inherits from SWISH::Prog::Aggregator,
68 read that documentation first. Any overridden methods are documented here.
69  
70 =head2 new( I<opts> )
71  
72 Create new aggregator object.
73  
74 The following I<opts> are required:
75  
76 =over
77  
78 =item db => I<connect_info>
79  
80 I<connect_info> is passed
81 directly to DBI's connect() method, so see the DBI docs for syntax.
82 If I<connect_info> is a DBI handle object, it is accepted as is.
83 If I<connect_info> is an array ref, it will be dereferenced and
84 passed to connect(). Otherwise it will be passed to connect as is.
85  
86 =item schema => I<db_schema>
87  
88 I<db_schema> is a hashref of table names and column descriptions.
89 Each key should be a table name. Each value should be a hashref of
90 column descriptions, where the key is the column name and the value
91 is a hashref of type and bias. See the SYNOPSIS.
92  
93 There are two special column names: swishtitle and swishdescription.
94 These are reserved for mapping real column names to Swish-e property names
95 for returning in search results. C<swishtitle> should be the name of a column,
96 and C<swishdescription> should be a hashref of column names to include
97 in the StoreDescription value.
98  
99 =item indexer => I<indexer_obj>
100  
101 A SWISH::Prog::Indexer-derived object.
102  
103 =back
104  
105 The following I<opts> are optional:
106  
107 =over
108  
109 =item alias_columns => 0|1
110  
111 The C<alias_columns> flag indicates whether all columns should be searchable
112 under the default MetaName of C<swishdefault>. The default is 1 (true). This
113 is B<not> the default behaviour of swish-e; this is a feature of SWISH::Prog.
114  
115 =item use_quotes
116  
117 Boolean indicating whether column and table names should be quoted.
118 This is typically DBD-specific (e.g., MySQL requires this be true).
119 Default is true.
120  
121 =item quote_char
122  
123 The character to use when C<use_quotes> is true. Default is B<`> (backtick).
124  
125 =back
126  
127 B<NOTE:> The new() method simply inherits from SWISH::Prog::Aggregator,
128 so any params valid for that method are allowed here.
129  
130 =head2 init
131  
132 See SWISH::Prog::Class. This method does all the setup.
133  
134 =cut
135  
136 0 ▶sub init {
Subroutine init: not called CC=26 SCAR=65.5
Pod documented
137 0 my $self = shift;
138 0 $self->SUPER::init(@_);
139  
140 0 ▶ $self->{use_quotes} = 1 unless defined $self->{use_quotes};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"use_quotes"} 0 0 0
141 0 ▶ $self->{quote_char} = '`' unless defined $self->{quote_char};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"quote_char"} 0 0 0
142  
143 # verify DBI connection
144 0 ▶ if ( defined( $self->db ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->db) { } 0 0 0
145  
146 0 ▶ if ( ref( $self->db ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->db eq 'ARRAY') { } 0 0 0
elsif (ref $self->db and $self->db->isa('DBI::db')) { } 0 0 0
147 0 $self->db( DBI->connect( @{ $self->{db} } ) );
148 }
149 ▶ elsif ( ref( $self->db ) && $self->db->isa('DBI::db') ) {
Condition0%0/3
ref $self->db and $self->db->isa("DBI::db")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->db and $self->db->isa("DBI::db")
ref $self->db $self->db->isa("DBI::db")
Truth table0%0/3
A and B
A= ref $self->dbB= $self->db->isa("DBI::db")
ABresult
0X0
100
111
150  
151 # do nothing
152 }
153 else {
154 0 $self->db( DBI->connect( $self->db ) );
155 }
156 }
157 else {
158 0 croak "db required";
159 }
160  
161 # verify schema
162 0 ▶ if ( defined $self->schema ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->schema) { } 0 0 0
163  
164 0 my $schema = $self->schema;
165 0 ▶ unless ( ref($schema) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH") 0 0 0
166 0 croak "schema must be a hashref";
167 }
168 0 for my $table ( keys %$schema ) {
169 0 my $cols = $schema->{$table};
170 0 ▶ unless ( ref($cols) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cols eq "HASH") 0 0 0
171 0 croak "column descriptions must be a hashref";
172 }
173 0 for my $colname ( keys %$cols ) {
174 0 my $desc = $cols->{$colname};
175 0 ▶ if ( $colname eq 'swishtitle' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colname eq "swishtitle") 0 0 0
176 0 ▶ if ( ref $desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $desc) 0 0 0
177 0 croak "swishtitle must be a column name string";
178 }
179 0 next;
180 }
181 0 ▶ unless ( ref($desc) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $desc eq "HASH") 0 0 0
182 0 croak "$colname description must be a hashref";
183 }
184 $desc->{type}
185 0 ▶ ||= 'char'; # TODO auto-make property types based on this.
Condition0%0/2
$desc->{"type"} ||= "char"
l!l
00
MC/DC0%0/1
$desc->{"type"} ||= "char"
$desc->{"type"}
Truth table0%0/2
A
A= $desc->{"type"}
Aresult
11
00
186 0 ▶ $desc->{bias} ||= 1;
Condition0%0/2
$desc->{"bias"} ||= 1
l!l
00
MC/DC0%0/1
$desc->{"bias"} ||= 1
$desc->{"bias"}
Truth table0%0/2
A
A= $desc->{"bias"}
Aresult
11
00
187 }
188 }
189 }
190 else {
191 0 croak "schema required";
192 }
193  
194 0 ▶ $self->{alias_columns} = 1 unless exists $self->{alias_columns};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"alias_columns"} 0 0 0
195  
196 # unless metanames are defined, use all the column names from schema
197 0 my $m = $self->config->MetaNames;
198 0 ▶ unless (@$m) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$m) 0 0 0
199 0 for my $table ( keys %{ $self->{schema} } ) {
200 0 my $columns = $self->{schema}->{$table};
201 0 my %ranks;
202 0 for my $col ( sort keys %$columns ) {
203 0 ▶ next if $col eq 'swishtitle';
Branch: 1 branch
BranchTrueFalseTotal
if $col eq "swishtitle" 0 0 0
204 0 ▶ next if $col eq 'swishdescription';
Branch: 1 branch
BranchTrueFalseTotal
if $col eq "swishdescription" 0 0 0
205 0 push( @{ $ranks{ $columns->{$col}->{bias} } }, $col );
206 }
207  
208 0 for my $rank ( keys %ranks ) {
209 $self->config->MetaNamesRank(
210 0 "$rank " . join( ' ', @{ $ranks{$rank} } ), 1 );
211 }
212 }
213 }
214  
215 # alias the top level tags to that default search
216 # will match any metaname in any table
217 0 ▶ if ( $self->alias_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->alias_columns) 0 0 0
218 $self->config->MetaNameAlias(
219 'swishdefault '
220 . join( ' ',
221 0 map { '_' . $_ . '_row' }
222 sort
223 0 ▶ grep { $_ ne 'swishtitle' and $_ ne 'swishdescription' }
Condition0%0/3
$_ ne 'swishtitle' && $_ ne 'swishdescription'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne 'swishtitle' && $_ ne 'swishdescription'
$_ ne 'swishtitle' $_ ne 'swishdescription'
Truth table0%0/3
A && B
A= $_ ne 'swishtitle'B= $_ ne 'swishdescription'
ABresult
0X0
100
111
224 0 keys %{ $self->{schema} } ),
225 1 # always append
226 );
227 }
228  
229 # add 'table' metaname
230 0 $self->config->MetaNames('table');
231  
232 # save all row text in the swishdescription property for excerpts
233 0 $self->config->StoreDescription('XML* <_desc>');
234  
235 }
236  
237 =head2 crawl
238  
239 Create index.
240  
241 Returns number of rows indexed.
242  
243 =cut
244  
245 0 ▶sub crawl {
Subroutine crawl: not called CC=6 SCAR=37.4
Pod documented
246 0 my $self = shift;
247  
248 0 my @tables = sort keys %{ $self->{schema} };
249  
250 0 T: for my $table (@tables) {
251  
252 0 my $table_info = $self->{schema}->{$table};
253  
254 # which columns to index
255 my @cols
256 0 ▶ = sort grep { $_ ne 'swishtitle' and $_ ne 'swishdescription' }
Condition0%0/3
$_ ne 'swishtitle' && $_ ne 'swishdescription'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne 'swishtitle' && $_ ne 'swishdescription'
$_ ne 'swishtitle' $_ ne 'swishdescription'
Truth table0%0/3
A && B
A= $_ ne 'swishtitle'B= $_ ne 'swishdescription'
ABresult
0X0
100
111
257 keys %$table_info;
258  
259 # special col names
260 0 ▶ my $desc = delete( $table_info->{swishdescription} ) || {};
Condition0%0/2
delete $table_info->{'swishdescription'} || {}
l!l
00
MC/DC0%0/1
delete $table_info->{'swishdescription'} || {}
delete $table_info->{'swishdescription'}
Truth table0%0/2
A
A= delete $table_info->{'swishdescription'}
Aresult
11
00
261 0 ▶ my $title = delete( $table_info->{swishtitle} ) || '';
Condition0%0/2
delete $table_info->{'swishtitle'} || ''
l!l
00
MC/DC0%0/1
delete $table_info->{'swishtitle'} || ''
delete $table_info->{'swishtitle'}
Truth table0%0/2
A
A= delete $table_info->{'swishtitle'}
Aresult
11
00
262  
263 0 ▶ my $quote_char = $self->use_quotes ? $self->quote_char : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->use_quotes ? : 0 0 0
264  
265 my $c = $self->_do_table(
266 name => $table . ".index",
267 sql => "SELECT "
268 0 . join( ",", map {qq/$quote_char$_$quote_char/} @cols )
269 . " FROM $table",
270 table => $table,
271 desc => $desc,
272 title => $title,
273 );
274 0 $self->_increment_count($c);
275 }
276  
277 0 return $self->{count};
278 }
279  
280 0 ▶sub _do_table {
Subroutine _do_table: not called CC=8 SCAR=42.8
281 0 my $self = shift;
282 0 my %opts = @_;
283  
284 0 ▶ if ( !$opts{sql} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"sql"}) 0 0 0
285 0 croak "need SQL statement to index with";
286 }
287  
288 0 ▶ $opts{table} ||= '';
Condition0%0/2
$opts{"table"} ||= ""
l!l
00
MC/DC0%0/1
$opts{"table"} ||= ""
$opts{"table"}
Truth table0%0/2
A
A= $opts{"table"}
Aresult
11
00
289 0 ▶ $opts{title} ||= '';
Condition0%0/2
$opts{"title"} ||= ""
l!l
00
MC/DC0%0/1
$opts{"title"} ||= ""
$opts{"title"}
Truth table0%0/2
A
A= $opts{"title"}
Aresult
11
00
290  
291 0 my $counter = 0;
292 0 my $indexer = $self->indexer;
293  
294 my $sth = $self->db->prepare( $opts{sql} )
295 0 ▶ or croak "DBI prepare() failed: " . $self->db->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->db->prepare($opts{"sql"}) or croak('DBI prepare() failed: ' . $self->db->errstr) 0 0 0
296 0 ▶ $sth->execute or croak "SELECT failed " . $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak('SELECT failed ' . $sth->errstr) 0 0 0
297  
298 0 ▶ while ( my $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
299  
300 0 ▶ my $title = $row->{ $opts{title} } || '[ no title ]';
Condition0%0/2
$$row{$opts{'title'}} || '[ no title ]'
l!l
00
MC/DC0%0/1
$$row{$opts{'title'}} || '[ no title ]'
$$row{$opts{'title'}}
Truth table0%0/2
A
A= $$row{$opts{'title'}}
Aresult
11
00
301  
302 0 my $xml = $self->_row2xml( $XMLer->tag_safe( $opts{table} ),
303 $row, $title, \%opts );
304  
305 0 my $doc = $self->doc_class->new(
306 content => $xml,
307 url => ++$counter,
308 modtime => time(),
309 parser => 'XML*',
310 type => 'application/xml',
311 data => $row
312 );
313  
314 0 $indexer->process($doc);
315 }
316  
317 0 $sth->finish;
318  
319 0 return $counter;
320  
321 }
322  
323 0 ▶sub _row2xml {
Subroutine _row2xml: not called CC=3 SCAR=24.8
324 0 my ( $self, $table, $row, $title, $opts ) = @_;
325  
326 0 my $xml
327 = "<_${table}_row>"
328 . "<table>"
329 . $table
330 . "</table>"
331 . "<swishtitle>"
332 . $XMLer->utf8_safe($title)
333 . "</swishtitle>"
334 . "<_body>";
335  
336 0 for my $col ( sort keys %$row ) {
337 my @x = (
338 $XMLer->start_tag($col),
339 0 $XMLer->utf8_safe( $row->{$col} ),
340 $XMLer->end_tag($col)
341 );
342  
343 0 ▶ if ( $opts->{desc}->{$col} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"desc"}{$col}) 0 0 0
344 0 unshift( @x, '<_desc>' );
345 0 push( @x, '</_desc>' );
346 }
347  
348 0 $xml .= join( '', @x );
349 }
350 0 $xml .= "</_body></_${table}_row>";
351  
352 #$self->debug and warn $xml . "\n";
353  
354 0 return $xml;
355 }
356  
357 1 1;
358  
359 __END__
↑ Summary Prog/Aggregator/Spider.pm »