SQLite/DBI/Index.pm

filter: Statementstmt 82.5% 132 / 160 Branchbran 52.2% 23 / 44 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.0% 24 / 25 Podpod 55.5% 5 / 9 totaltotal 76.1% 185 / 243 CC 28 SCAR 14.0
CC
28
Cov
75%
sth_update
12.0
create_tables
7.8
create_index
5.5
CRAP 4.0
« SQLite/META.pm ↑ Summary
1 # $Id: Index.pm 84 2020-05-31 06:29:34Z stro $
2  
3 package CPAN::SQLite::DBI::Index;
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN {
8 10 ▶ our $VERSION = '0.219';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 $CPAN::SQLite::DBI::Index::info::VERSION = $VERSION;
10 10 $CPAN::SQLite::DBI::Index::mods::VERSION = $VERSION;
11 10 $CPAN::SQLite::DBI::Index::dists::VERSION = $VERSION;
12 10 $CPAN::SQLite::DBI::Index::auths::VERSION = $VERSION;
13 }
14  
15 10 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use parent 'CPAN::SQLite::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 package CPAN::SQLite::DBI::Index::info;
19 10 ▶use parent 'CPAN::SQLite::DBI::Index';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 package CPAN::SQLite::DBI::Index::mods;
23 10 ▶use parent 'CPAN::SQLite::DBI::Index';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 package CPAN::SQLite::DBI::Index::dists;
27 10 ▶use parent 'CPAN::SQLite::DBI::Index';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub fetch_ids {
31 1 ▶ my $self = shift;
Subroutine fetch_ids: called CC=4 SCAR=14.8
32 1 my $sql = sprintf(qq{SELECT %s,%s,%s FROM %s}, $self->{id}, $self->{name}, 'dist_vers', $self->{table});
33 1 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Index::dists::dbh->prepare($sql) or do { $self->db_error; return } 0 1 1
34 0 $self->db_error();
35 0 return;
36 };
37 1 ▶ $sth->execute() or do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or do { $self->db_error($sth); return } 0 1 1
38 0 $self->db_error($sth);
39 0 return;
40 };
41 1 my ($ids, $versions);
42 1 ▶ while (my ($id, $key, $vers) = $sth->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 92 1 93
43 92 $ids->{$key} = $id;
44 92 $versions->{$key} = $vers;
45 }
46 1 $sth->finish;
47 1 undef $sth;
48 1 return ($ids, $versions);
49 }
50  
51 package CPAN::SQLite::DBI::Index::auths;
52 10 ▶use parent 'CPAN::SQLite::DBI::Index';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 10 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 package CPAN::SQLite::DBI::Index;
56 10 ▶use CPAN::SQLite::DBI qw($tables);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 10 ▶use CPAN::SQLite::DBI qw($dbh);
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 sub fetch_ids {
60 2 ▶ my $self = shift;
Subroutine fetch_ids: called CC=4 SCAR=14.9
Pod undocumented
61 2 my $sql = sprintf(qq{SELECT %s,%s from %s}, $self->{id}, $self->{name}, $self->{table});
62 2 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Index::dbh->prepare($sql) or do { $self->db_error; return } 0 2 2
63 0 $self->db_error();
64 0 return;
65 };
66 2 ▶ $sth->execute() or do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or do { $self->db_error($sth); return } 0 2 2
67 0 $self->db_error($sth);
68 0 return;
69 };
70 2 my $ids;
71 2 ▶ while (my ($id, $key) = $sth->fetchrow_array()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth->fetchrow_array) 548 2 550
72 548 $ids->{$key} = $id;
73 }
74 2 $sth->finish;
75 2 undef $sth;
76 2 return $ids;
77 }
78  
79 sub schema {
80 16 ▶ my ($self, $data) = @_;
Subroutine schema: called CC=3 SCAR=11.0
Pod undocumented
81 16 my $schema = '';
82 16 foreach my $type (qw(primary other)) {
83 32 foreach my $column (keys %{ $data->{$type} }) {
84 64 $schema .= $column . ' ' . $data->{$type}->{$column} . ", ";
85 }
86 }
87 16 $schema =~ s{, $}{};
88 16 return $schema;
89 }
90  
91 sub create_index {
92 16 ▶ my ($self, $data) = @_;
Subroutine create_index: called CC=5 SCAR=17.1
Pod undocumented
93 16 my $key = $data->{key};
94 16 my $table = $self->{table};
95 16 ▶ return 1 unless (defined $key and ref($key) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and ref $key eq "ARRAY" 0 16 16
Condition33%1/3
defined $key and ref $key eq "ARRAY"
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $key and ref $key eq "ARRAY"
defined $key ref $key eq "ARRAY"
Truth table33%1/3
A and B
A= defined $keyB= ref $key eq "ARRAY"
ABresult
0X0
100
111
96 16 foreach my $index (@$key) {
97 20 my $id_name = 'ix_' . $table . '_' . $index;
98 20 $id_name =~ s/\(\s*\d+\s*\)//;
99 20 my $sql = 'CREATE INDEX ' . $id_name . ' ON ' . $table . '( ' . $index . ' )';
100 20 my $sth = $dbh->prepare($sql);
101 20 ▶ $sth->execute() or do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or do { $self->db_error($sth); return } 0 20 20
102 0 $self->db_error($sth);
103 0 return;
104 };
105 20 $sth->finish;
106 20 undef $sth;
107 }
108 16 return 1;
109 }
110  
111 sub drop_table {
112 16 ▶ my $self = shift;
Subroutine drop_table: called CC=3 SCAR=11.2
Pod documented
113 16 my $table = $self->{table};
114 16 my $sql = qq{SELECT name FROM sqlite_master } . qq{ WHERE type='table' AND name=?};
115 16 my $sth = $dbh->prepare($sql);
116 16 $sth->execute($table);
117 16 ▶ if (defined $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth->fetchrow_array) 4 12 16
118 4 ▶ $dbh->do(qq{drop table $table}) or do {
Branch: 1 branch
BranchTrueFalseTotal
$CPAN::SQLite::DBI::Index::dbh->do("drop table $table") or do { $self->db_error($sth); return } 0 4 4
119 0 $self->db_error($sth);
120 0 return;
121 };
122 }
123 16 $sth->finish;
124 16 undef $sth;
125 16 return 1;
126 }
127  
128 sub create_table {
129 16 ▶ my ($self, $schema) = @_;
Subroutine create_table: called CC=3 SCAR=11.7
Pod undocumented
130 16 ▶ return unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 16 16
131 16 my $sql = sprintf(qq{CREATE TABLE %s (%s)}, $self->{table}, $schema);
132 16 my $sth = $dbh->prepare($sql);
133 16 ▶ $sth->execute() or do {
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or do { $self->db_error($sth); return } 0 16 16
134 0 $self->db_error($sth);
135 0 return;
136 };
137 16 $sth->finish;
138 16 undef $sth;
139 16 return 1;
140 }
141  
142 sub create_tables {
143 4 ▶ my ($self, %args) = @_;
Subroutine create_tables: called CC=7 SCAR=20.5
Pod documented
144 4 ▶ return unless $args{setup};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"setup"} 0 4 4
145 4 my $objs = $self->{objs};
146 4 foreach my $table (keys %$objs) {
147 16 ▶ next unless my $schema = $self->schema($tables->{$table});
Branch: 1 branch
BranchTrueFalseTotal
unless my $schema = $self->schema($CPAN::SQLite::DBI::Index::tables->{$table}) 0 16 16
148 16 my $obj = $objs->{$table};
149 16 ▶ $obj->drop_table or return;
Branch: 1 branch
BranchTrueFalseTotal
$obj->drop_table or return 0 16 16
150 16 ▶ $obj->create_table($schema) or return;
Branch: 1 branch
BranchTrueFalseTotal
$obj->create_table($schema) or return 0 16 16
151 16 ▶ $obj->create_index($tables->{$table}) or return;
Branch: 1 branch
BranchTrueFalseTotal
$obj->create_index($CPAN::SQLite::DBI::Index::tables->{$table}) or return 0 16 16
152 }
153 4 return 1;
154 }
155  
156 sub sth_insert {
157 16 ▶ my ($self, $fields) = @_;
Subroutine sth_insert: called CC=2 SCAR=7.2
Pod documented
158 16 my $flds = join ',', @{$fields};
159 16 my $vals = join ',', map { '?' } @{$fields};
160 16 my $sql = sprintf(qq{INSERT INTO %s (%s) VALUES (%s)}, $self->{table}, $flds, $vals);
161  
162 16 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Index::dbh->prepare($sql) or do { $self->db_error; return } 0 16 16
163 0 $self->db_error();
164 0 return;
165 };
166 16 return $sth;
167 }
168  
169 sub sth_update {
170 0 ▶ my ($self, $fields, $id, $rep_id) = @_;
Subroutine sth_update: not called CC=3 SCAR=24.8
Pod documented
171 0 my $set = join ',', map { "$_=?" } @{$fields};
172 0 my $sql = sprintf(qq{UPDATE %s SET %s WHERE %s = %s}, $self->{table}, $set, $self->{id}, $id);
173 0 ▶ $sql .= qq { AND rep_id = $rep_id } if ($rep_id);
Branch: 1 branch
BranchTrueFalseTotal
if $rep_id 0 0 0
174 0 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Index::dbh->prepare($sql) or do { $self->db_error; return } 0 0 0
175 0 $self->db_error();
176 0 return;
177 };
178 0 return $sth;
179 }
180  
181 sub sth_delete {
182 4 ▶ my ($self, $table_id, $rep_id) = @_;
Subroutine sth_delete: called CC=3 SCAR=12.3
Pod documented
183 4 my $sql = sprintf(qq{DELETE FROM %s where %s = ?}, $self->{table}, $table_id);
184 4 ▶ $sql .= qq { AND rep_id = $rep_id } if ($rep_id);
Branch: 1 branch
BranchTrueFalseTotal
if $rep_id 0 4 4
185 4 ▶ my $sth = $dbh->prepare($sql) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $CPAN::SQLite::DBI::Index::dbh->prepare($sql) or do { $self->db_error; return } 0 4 4
186 0 $self->db_error();
187 0 return;
188 };
189 4 return $sth;
190 }
191  
192 10 1;
193  
194 =head1 NAME
195  
196 CPAN::SQLite::DBI::Index - DBI information for indexing the CPAN::SQLite database
197  
198 =head1 VERSION
199  
200 version 0.219
201  
202 =head1 DESCRIPTION
203  
204 This module provides various methods for L<CPAN::SQLite::Index> in
205 indexing and populating the database from the index files.
206  
207 =over
208  
209 =item C<create_tables>
210  
211 This creates the database tables.
212  
213 =item C<drop_table>
214  
215 This drops a table.
216  
217 =item C<sth_insert>
218  
219 This returns an C<$sth> statement handle for inserting
220 values into a table.
221  
222 =item C<sth_update>
223  
224 This returns an C<$sth> statement handle for updating
225 values into a table.
226  
227 =item C<sth_delete>
228  
229 This returns an C<$sth> statement handle for deleting
230 values from a table.
231  
232 =back
233  
234 =head1 SEE ALSO
235  
236 L<CPAN::SQLite::Index>
237  
238 =cut
« SQLite/META.pm ↑ Summary