Object/Metadata/Auto/SQLite.pm

filter: Statementstmt 19.5% 25 / 128 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.8% 7 / 13 Podpod 100% 3 / 3 totaltotal 16.8% 35 / 208 CC 32 SCAR 50.0
CC
32
Cov
14%
auto_generate_foreign_keys
313.4
auto_generate_columns
56.1
auto_generate_unique_keys
56.0
CRAP 147.8
« Object/Metadata/Auto/Pg.pm ↑ Summary Object/Metadata/Relationship.pm »
1 package Rose::DB::Object::Metadata::Auto::SQLite;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Rose::DB::Object::Metadata::ForeignKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Rose::DB::Object::Metadata::UniqueKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Rose::DB::Object::Metadata::Auto;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @ISA = qw(Rose::DB::Object::Metadata::Auto);
12  
13 1 our $VERSION = '0.784';
14  
15 sub auto_generate_columns
16 0 ▶{
Subroutine auto_generate_columns: not called CC=8 SCAR=40.3
Pod documented
17 0 my($self) = shift;
18  
19 0 my($class, %columns, $error);
20  
21 TRY:
22 {
23 0 local $@;
24  
25 eval
26 0 {
27 0 ▶ my $col_info = ($self->_table_info)[0] || [];
Condition0%0/2
($self->_table_info)[0] || []
l!l
00
MC/DC0%0/1
($self->_table_info)[0] || []
($self->_table_info)[0]
Truth table0%0/2
A
A= ($self->_table_info)[0]
Aresult
11
00
28  
29 0 ▶ die "No columns found" unless(@$col_info);
Branch: 1 branch
BranchTrueFalseTotal
unless @$col_info 0 0 0
30  
31 0 my $db = $self->db;
32 0 ▶ my $dbh = $db->dbh or die $db->error;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $db->dbh or die $db->error 0 0 0
33  
34 0 foreach my $info (@$col_info)
35 {
36 0 $db->refine_dbi_column_info($info);
37  
38 $columns{$info->{'COLUMN_NAME'}} =
39 0 $self->auto_generate_column($info->{'COLUMN_NAME'}, $info);
40 }
41 };
42  
43 0 $error = $@;
44 }
45  
46 0 ▶ if($error || !keys %columns)
Branch: 1 branch
BranchTrueFalseTotal
if ($error or not keys %columns) 0 0 0
Condition0%0/3
$error or not keys %columns
l!l&&r!l&&!r
000
MC/DC0%0/2
$error or not keys %columns
$error not keys %columns
Truth table0%0/3
A or not(B)
A= $errorB= not keys %columns
ABresult
1X1
011
000
47 {
48 1 ▶ no warnings; # undef strings okay
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 Carp::croak "Could not auto-generate columns for class $class, table '",
50 $self->table, "' - $error";
51 }
52  
53 0 $self->auto_alias_columns(values %columns);
54  
55 0 ▶ return wantarray ? values %columns : \%columns;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
56 }
57  
58 1 my $UK_Num = 1;
59  
60 sub auto_generate_unique_keys
61 0 ▶{
Subroutine auto_generate_unique_keys: not called CC=7 SCAR=40.3
Pod documented
62 0 my($self) = shift;
63  
64 0 ▶ unless(defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
65 {
66 0 Carp::croak "Useless call to auto_generate_unique_keys() in void context";
67 }
68  
69 0 my($class, %unique_keys, $error);
70  
71 TRY:
72 {
73 0 local $@;
74  
75 eval
76 0 {
77 0 ▶ my $uk_info = ($self->_table_info)[2] || [];
Condition0%0/2
($self->_table_info)[2] || []
l!l
00
MC/DC0%0/1
($self->_table_info)[2] || []
($self->_table_info)[2]
Truth table0%0/2
A
A= ($self->_table_info)[2]
Aresult
11
00
78  
79 0 foreach my $info (@$uk_info)
80 {
81 0 my $uk_name = 'unique_key_' . $UK_Num++;
82  
83 0 my $uk = $unique_keys{$uk_name} =
84 Rose::DB::Object::Metadata::UniqueKey->new(name => $uk_name,
85 parent => $self);
86  
87 0 foreach my $column (@$info)
88 {
89 0 $uk->add_column($column);
90 }
91  
92 0 $unique_keys{$uk_name} = $uk;
93 }
94 };
95  
96 0 $error = $@;
97 }
98  
99 0 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
100 {
101 0 Carp::croak "Could not auto-retrieve unique keys for class $class - $error";
102 }
103  
104 # This sort order is part of the API, and is essential to make the
105 # test suite work.
106 0 my @uk = map { $unique_keys{$_} } sort map { lc } keys(%unique_keys);
107  
108 0 ▶ return wantarray ? @uk : \@uk;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
109 }
110  
111 sub auto_generate_foreign_keys
112 0 ▶{
Subroutine auto_generate_foreign_keys: not called CC=18 SCAR=57.5
Pod documented
113 0 my($self, %args) = @_;
114  
115 0 ▶ unless(defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
116 {
117 0 Carp::croak "Useless call to auto_generate_foreign_keys() in void context";
118 }
119  
120 0 my $no_warnings = $args{'no_warnings'};
121  
122 0 my($class, @foreign_keys, $total_fks, $error);
123  
124 TRY:
125 {
126 0 local $@;
127  
128 eval
129 0 {
130 0 ▶ $class = $self->class or die "Missing class!";
Branch: 1 branch
BranchTrueFalseTotal
$class = $self->class or die "Missing class!" 0 0 0
131  
132 0 my $db = $self->db;
133 0 ▶ my $dbh = $db->dbh or die $db->error;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $db->dbh or die $db->error 0 0 0
134 0 my $table_quoted = $db->quote_table_name($self->table);
135  
136 # Silence this stupid warning when a table has no foreign keys:
137 # DBD::SQLite::st fetchrow_hashref warning: not an error(0) at dbdimp.c line 504
138 0 local $dbh->{'PrintWarn'} = 0;
139  
140 0 my $sth = $dbh->prepare("PRAGMA foreign_key_list($table_quoted)");
141 0 $sth->execute;
142  
143 0 my %fk_info;
144  
145 0 ▶ while(my $row = $sth->fetchrow_hashref)
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
146 {
147 0 push(@{$fk_info{$row->{'id'}}}, $row);
148 }
149  
150 0 my $cm = $self->convention_manager;
151  
152 0 FK: foreach my $id (sort { $a <=> $b } keys(%fk_info))
153 {
154 0 my $col_info = $fk_info{$id};
155  
156 0 my $foreign_table = $col_info->[0]{'table'};
157  
158 0 my $foreign_class = $self->class_for(table => $foreign_table);
159  
160 0 ▶ unless($foreign_class)
Branch: 1 branch
BranchTrueFalseTotal
unless ($foreign_class) 0 0 0
161 {
162 # Add deferred task
163 $self->add_deferred_task(
164 {
165 class => $self->class,
166 method => 'auto_init_foreign_keys',
167 args => \%args,
168  
169 code => sub
170 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 $self->auto_init_foreign_keys(%args);
172 0 $self->make_foreign_key_methods(%args, preserve_existing => 1);
173 },
174  
175 check => sub
176 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
177 0 my $fks = $self->foreign_keys;
178 0 ▶ return @$fks == $total_fks ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$fks == $total_fks ? : 0 0 0
179 }
180 0 });
181  
182 0 ▶ unless($no_warnings || $self->allow_auto_initialization)
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_warnings or $self->allow_auto_initialization) 0 0 0
Condition0%0/3
$no_warnings or $self->allow_auto_initialization
l!l&&r!l&&!r
000
MC/DC0%0/2
$no_warnings or $self->allow_auto_initialization
$no_warnings $self->allow_auto_initialization
Truth table0%0/3
A or B
A= $no_warningsB= $self->allow_auto_initialization
ABresult
1X1
011
000
183 {
184 1 ▶ no warnings; # Allow undef coercion to empty string
Subroutine BEGIN: called CC=1 SCAR=0.0
185 0 warn "No Rose::DB::Object-derived class found for table ",
186 "'$foreign_table'";
187 }
188  
189 0 $total_fks++;
190 0 next FK;
191 }
192  
193 0 my(@local_columns, @foreign_columns);
194  
195 0 foreach my $item (@$col_info)
196 {
197 0 push(@local_columns, $item->{'from'});
198 0 push(@foreign_columns, $item->{'to'});
199 }
200  
201 0 ▶ unless(@local_columns > 0 && @local_columns == @foreign_columns)
Branch: 1 branch
BranchTrueFalseTotal
unless (@local_columns > 0 and @local_columns == @foreign_columns) 0 0 0
Condition0%0/3
@local_columns > 0 and @local_columns == @foreign_columns
!ll&&!rl&&r
000
MC/DC0%0/2
@local_columns > 0 and @local_columns == @foreign_columns
@local_columns > 0 @local_columns == @foreign_columns
Truth table0%0/3
A and B
A= @local_columns > 0B= @local_columns == @foreign_columns
ABresult
0X0
100
111
202 {
203 0 die "Failed to extract a matched set of columns from ",
204 'PRAGMA foreign_key_list(', $self->table, ')';
205 }
206  
207 0 my %key_columns;
208 0 @key_columns{@local_columns} = @foreign_columns;
209  
210 0 my $fk =
211 Rose::DB::Object::Metadata::ForeignKey->new(
212 class => $foreign_class,
213 key_columns => \%key_columns);
214  
215 0 push(@foreign_keys, $fk);
216 0 $total_fks++;
217 }
218  
219 # This step is important! It ensures that foreign keys will be created
220 # in a deterministic order, which in turn allows the "auto-naming" of
221 # foreign keys to work in a predictable manner. This exact sort order
222 # (lowercase table name comparisons) is part of the API for foreign
223 # key auto generation.
224 @foreign_keys =
225 0 sort { lc $a->class->meta->table cmp lc $b->class->meta->table }
226 @foreign_keys;
227  
228 0 my %used_names;
229  
230 0 foreach my $fk (@foreign_keys)
231 {
232 0 my $name =
233 $cm->auto_foreign_key_name($fk->class, $fk->name, scalar $fk->key_columns, \%used_names);
234  
235 0 ▶ unless(defined $name)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
236 {
237 0 $fk->name($name = $self->foreign_key_name_generator->($self, $fk));
238 }
239  
240 0 ▶ unless(defined $name && $name =~ /^\w+$/)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and $name =~ /^\w+$/) 0 0 0
Condition0%0/3
defined $name and $name =~ /^\w+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $name =~ /^\w+$/
defined $name $name =~ /^\w+$/
Truth table0%0/3
A and B
A= defined $nameB= $name =~ /^\w+$/
ABresult
0X0
100
111
241 {
242 0 die "Missing or invalid key name '$name' for foreign key ",
243 "generated in $class for ", $fk->class;
244 }
245  
246 0 $used_names{$name}++;
247  
248 0 $fk->name($name);
249 }
250 };
251  
252 0 $error = $@;
253 }
254  
255 0 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
256 {
257 0 Carp::croak "Could not auto-generate foreign keys for class $class - $error";
258 }
259  
260 0 @foreign_keys = sort { lc $a->name cmp lc $b->name } @foreign_keys;
261  
262 0 ▶ return wantarray ? @foreign_keys : \@foreign_keys;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
263 }
264  
265 sub _table_info
266 0 ▶{
Subroutine _table_info: not called CC=1 SCAR=6.9
267 0 my($self) = shift;
268  
269 # XXX: I'm in the process of moving all introspection to Rose::DB.
270 # XXX: _table_info is an undocumented method of Rose::DB::SQLite
271 0 $self->db->_table_info($self->table);
272 }
273  
274 1 1;
« Object/Metadata/Auto/Pg.pm ↑ Summary Object/Metadata/Relationship.pm »