File Coverage

blib/lib/MyConText/String.pm
Criterion Covered Total %
statement 9 37 24.3
branch 0 10 0.0
condition 0 4 0.0
subroutine 3 7 42.8
pod 2 3 66.6
total 14 61 22.9


line stmt bran cond sub pod time code
1              
2             package MyConText::String;
3 1     1   631 use MyConText;
  1         2  
  1         25  
4 1     1   14 use strict;
  1         2  
  1         30  
5 1     1   5 use vars qw! @ISA !;
  1         1  
  1         507  
6             @ISA = qw! MyConText !;
7              
8             # Create creates the conversion table that converts string names of
9             # documents to numbers
10             sub _create_tables {
11 0     0     my $ctx = shift;
12 0 0         $ctx->{'doc_id_table'} = $ctx->{'table'} . '_docid'
13             unless defined $ctx->{'doc_id_table'};
14              
15 0           my $CREATE_DOCID = <
16             create table $ctx->{'doc_id_table'} (
17             name varchar($ctx->{'name_length'}) binary not null,
18             id $MyConText::BITS_TO_INT{$ctx->{'doc_id_bits'}} unsigned not null auto_increment,
19             primary key (id),
20             unique (name)
21             )
22             EOF
23 0           my $dbh = $ctx->{'dbh'};
24 0 0         $dbh->do($CREATE_DOCID) or return $dbh->errstr;
25 0           push @{$ctx->{'created_tables'}}, $ctx->{'doc_id_table'};
  0            
26 0           return;
27             }
28              
29             sub get_id_for_name {
30 0     0 0   my ($self, $string) = @_;
31 0           my $dbh = $self->{'dbh'};
32 0           my $doc_id_table = $self->{'doc_id_table'};
33              
34 0   0       my $name_to_id_sth = ( defined $self->{'name_to_id_sth'}
35             ? $self->{'name_to_id_sth'}
36             : $self->{'name_to_id_sth'} = $dbh->prepare("select id from $doc_id_table where name = ?") or die $dbh->errstr);
37 0           my $id = $dbh->selectrow_array($name_to_id_sth, {}, $string);
38 0 0         if (not defined $id) {
39 0   0       my $new_name_sth = (defined $self->{'new_name_sth'}
40             ? $self->{'new_name_sth'}
41             : $self->{'new_name_sth'} =
42             $dbh->prepare("insert into $doc_id_table values (?, null)") or die $dbh->errstr );
43 0 0         $new_name_sth->execute($string) or die $new_name_sth->errstr;
44 0           $id = $new_name_sth->{'mysql_insertid'};
45             }
46 0           $id;
47             }
48             sub index_document {
49 0     0 1   my ($self, $string, $data) = @_;
50 0           my $id = $self->get_id_for_name($string);
51 0           $self->SUPER::index_document($id, $data);
52             }
53              
54             sub contains_hashref {
55 0     0 1   my $self = shift;
56 0           my $res = $self->SUPER::contains_hashref(@_);
57 0 0         return unless keys %$res;
58              
59 0           my $doc_id_table = $self->{'doc_id_table'};
60              
61 0           my $data = $self->{'dbh'}->selectall_arrayref("select name, id from $doc_id_table where " . join(' or ', ('id = ?') x keys %$res), {}, keys %$res);
62 0           return { map { ( $_->[0], $res->{$_->[1]} ) } @$data };
  0            
63             }
64              
65              
66             1;
67