MyConText/Table.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 125 CC 12 SCAR 39.9
CC
12
Cov
0%
_create_tables
110.0
_open_tables
6.0
get_the_data_from_table
6.0
CRAP 54.2
1  
2 package MyConText::TableString;
3 use vars qw! @ISA !;
4 @ISA = qw! MyConText::String MyConText::Table !;
5  
6 sub index_document {
7 my ($self, $id) = @_;
8 my $data = $self->get_the_data_from_table($id);
9 $self->SUPER::index_document($id, $data);
10 }
11  
12 package MyConText::TableNum;
13 use vars qw! @ISA !;
14 @ISA = qw! MyConText::Table !;
15  
16 sub index_document {
17 my ($self, $id) = @_;
18 my $data = $self->get_the_data_from_table($id);
19 $self->SUPER::index_document($id, $data);
20 }
21  
22  
23 package MyConText::Table;
24 use MyConText;
25 use strict;
26 use vars qw! @ISA !;
27 @ISA = qw! MyConText !;
28  
29 sub _open_tables {
30 my $self = shift;
31 if (defined $self->{'doc_id_table'}) {
32 eval 'use MyConText::String';
33 bless $self, 'MyConText::TableString';
34 }
35 else {
36 bless $self, 'MyConText::TableNum';
37 }
38 }
39  
40 # we do not create any new tables, we just check that the parameters are
41 # OK (the table and columns exist, etc.)
42 sub _create_tables {
43 my $ctx = shift;
44 my ($table, $column, $id) = @{$ctx}{ qw! table_name column_name
45 column_id_name ! };
46 if (not defined $table and $column =~ /\./) {
47 ($table, $column) =~ ($column =~ /^(.*)\.(.*)$/s);
48 }
49 my $id_type;
50  
51 if (not defined $table) {
52 return "The parameter table_name has to be specified with the table frontend.";
53 }
54 if (not defined $column) {
55 return "The parameter column_name has to be specified with the table frontend.";
56 }
57 my $dbh = $ctx->{'dbh'};
58 my $sth = $dbh->prepare("show columns from $table");
59 $sth->{'PrintError'} = 0;
60 $sth->{'RaiseError'} = 0;
61 $sth->execute or return "The table `$table' doesn't exist.";
62  
63 my $info = $dbh->selectall_arrayref($sth,
64 { 'PrintError' => 0, 'RaiseError' => 0 });
65 if (not defined $info) {
66 return "The table `$table' doesn't exist.";
67 }
68  
69 ### use Data::Dumper; print Dumper $info;
70  
71 if (not defined $id) {
72 # search for column with primary key
73 my $pri_num = 0;
74 for my $i (0 .. $#$info) {
75 if ($info->[$i][3] eq 'PRI') {
76 $pri_num++;
77 $id = $info->[$i][0];
78 $id_type = $info->[$i][1];
79 }
80 }
81 if ($pri_num > 1) {
82 return 'The primary key has to be one-column.';
83 }
84 if ($pri_num == 0) {
85 return "No primary key found in the table `$table'.";
86 }
87 }
88  
89  
90 my $testcol = $dbh->prepare("select $column from $table where 1 = 0");
91 $testcol->execute or
92 return "Column `$column' doesn't exist in table `$table'.";
93 $testcol->finish;
94  
95 $ctx->{'column_id_name'} = $id;
96  
97 if ($id_type =~ /^\w*int\((\d+)\)$/) {
98 $ctx->{'doc_id_bits'} = $MyConText::PRECISION_TO_BITS{$1};
99 bless $ctx, 'MyConText::TableNum';
100 }
101 else {
102 my ($length) = ($id_type =~ /^\w+\((\d+)\)$/);
103 $ctx->{'name_length'} = $1;
104 eval 'use MyConText::String';
105 bless $ctx, 'MyConText::TableString';
106 $ctx->MyConText::String::_create_tables($ctx);
107 }
108 ### use Data::Dumper; print Dumper $ctx;
109 return;
110 }
111  
112 sub get_the_data_from_table {
113 my ($self, $id) = @_;
114 my $dbh = $self->{'dbh'};
115 my $get_data = ( defined $self->{'get_data_sth'}
116 ? $self->{'get_data_sth'}
117 : $self->{'get_data_sth'} = $dbh->prepare("
118 select $self->{'column_name'} from $self->{'table_name'}
119 where $self->{'column_id_name'} = ?
120 ") );
121  
122 my ($data) = $dbh->selectrow_array($get_data, {}, $id);
123 $data;
124 }
125  
126 1;
127