blib/lib/DBIx/Class/Schema/Loader/DBI/SQLite.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 87.5% 49 / 56 Conditioncond 90.4% 19 / 21 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 94.5% 209 / 221 CC 39 SCAR 22.1
CC
39
Cov
95%
_table_fk_info
16.0
_columns_info_for
11.0
_tables_list
5.0
CRAP 9.1
« blib/lib/DBIx/Class/Schema/Loader/DBI/ADO.pm ↑ Summary blib/lib/DBIx/Class/Schema/Loader.pm »
1 package DBIx::Class::Schema::Loader::DBI::SQLite;
2  
3 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 48 ▶use DBIx::Class::Schema::Loader::Table ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 48 our $VERSION = '0.07053';
10  
11 =head1 NAME
12  
13 DBIx::Class::Schema::Loader::DBI::SQLite - DBIx::Class::Schema::Loader::DBI SQLite Implementation.
14  
15 =head1 DESCRIPTION
16  
17 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
18  
19 =head1 METHODS
20  
21 =head2 rescan
22  
23 SQLite will fail all further commands on a connection if the underlying schema
24 has been modified. Therefore, any runtime changes requiring C<rescan> also
25 require us to re-connect to the database. The C<rescan> method here handles
26 that reconnection for you, but beware that this must occur for any other open
27 sqlite connections as well.
28  
29 =cut
30  
31 142 ▶sub _setup {
Subroutine _setup: called CC=4 SCAR=14.0
32 142 my $self = shift;
33  
34 142 $self->next::method(@_);
35  
36 142 ▶ if (not defined $self->preserve_case) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->preserve_case) 116 26 142
37 116 $self->preserve_case(0);
38 }
39  
40 142 ▶ if ($self->db_schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_schema) 12 130 142
41 12 warn <<'EOF';
42 db_schema is not supported on SQLite, the option is implemented only for qualify_objects testing.
43 EOF
44 12 ▶ if ($self->db_schema->[0] eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_schema->[0] eq "%") 0 12 12
45 0 $self->db_schema(undef);
46 }
47 }
48 }
49  
50 5 ▶sub rescan {
Subroutine rescan: called CC=2 SCAR=7.1
Pod documented
51 5 my ($self, $schema) = @_;
52  
53 5 ▶ $schema->storage->disconnect if $schema->storage;
Branch: 1 branch
BranchTrueFalseTotal
if $schema->storage 5 0 5
54 5 $self->next::method($schema);
55 }
56  
57 827 ▶sub _columns_info_for {
Subroutine _columns_info_for: called CC=11 SCAR=24.0
58 827 my $self = shift;
59 827 my ($table) = @_;
60  
61 827 my $result = $self->next::method(@_);
62  
63 827 local $self->dbh->{FetchHashKeyName} = 'NAME_lc';
64  
65 827 my $sth = $self->dbh->prepare(
66 "pragma table_info(" . $self->dbh->quote_identifier($table) . ")"
67 );
68 827 $sth->execute;
69 827 my $cols = $sth->fetchall_hashref('name');
70  
71 # copy and case according to preserve_case mode
72 # no need to check for collisions, SQLite does not allow them
73 827 my %cols;
74 827 ▶ while (my ($col, $info) = each %$cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$cols) 2310 827 3137
75 2310 $cols{ $self->_lc($col) } = $info;
76 }
77  
78 827 my ($num_pk, $pk_col) = (0);
79 # SQLite doesn't give us the info we need to do this nicely :(
80 # If there is exactly one column marked PK, and its type is integer,
81 # set it is_auto_increment. This isn't 100%, but it's better than the
82 # alternatives.
83 827 ▶ while (my ($col_name, $info) = each %$result) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$result) 2310 827 3137
84 2310 ▶ if ($cols{$col_name}{pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols{$col_name}{"pk"}) 901 1409 2310
85 901 $num_pk++;
86 901 ▶ if (lc($cols{$col_name}{type}) eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $cols{$col_name}{"type"} eq "integer") 887 14 901
87 887 $pk_col = $col_name;
88 }
89 }
90 }
91  
92 827 ▶ while (my ($col, $info) = each %$result) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$result) 2310 827 3137
93 2310 ▶ if ((eval { ${ $info->{default_value} } }||'') eq 'CURRENT_TIMESTAMP') {
Branch: 1 branch
BranchTrueFalseTotal
if ((eval { do { ${$info->{"default_value"};} } } || "") eq "CURRENT_TIMESTAMP") 104 2206 2310
Condition100%2/2
eval { do { ${$info->{'default_value'};} } } || ''
l!l
1112199
MC/DC100%1/1
eval { do { ${$info->{'default_value'};} } } || ''
eval { do { ${$info->{'default_value'};} } }
Truth table100%2/2
A
A= eval { do { ${$info->{'default_value'};} } }
Aresult
11
00
94 104 ${ $info->{default_value} } = 'current_timestamp';
95 }
96 2310 ▶ if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_pk == 1 and defined $pk_col and $pk_col eq $col) 711 1599 2310
Condition100%6/6
$num_pk == 1 and defined $pk_col and $pk_col eq $col
!ll&&!rl&&r
2951304711
$num_pk == 1 and defined $pk_col
!ll&&!rl&&r
281142015
MC/DC100%3/3
$num_pk == 1 and defined $pk_col and $pk_col eq $col
$num_pk == 1 defined $pk_col $pk_col eq $col
Truth table100%4/4
A and B and C
A= $num_pk == 1B= defined $pk_colC= $pk_col eq $col
ABCresult
0XX0
10X0
1100
1111
97 711 $info->{is_auto_increment} = 1;
98 }
99 }
100  
101 827 return $result;
102 }
103  
104 1648 ▶sub _table_fk_info {
Subroutine _table_fk_info: called CC=16 SCAR=27.7
105 1648 my ($self, $table) = @_;
106  
107 1648 my $sth = $self->dbh->prepare(
108 "pragma foreign_key_list(" . $self->dbh->quote_identifier($table) . ")"
109 );
110 1648 $sth->execute;
111  
112 1648 my @rels;
113 1648 ▶ while (my $fk = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fk = $sth->fetchrow_hashref) 1399 1648 3047
114 my $rel = $rels[ $fk->{id} ] ||= {
115 local_columns => [],
116 remote_columns => undef,
117 remote_table => DBIx::Class::Schema::Loader::Table->new(
118 loader => $self,
119 name => $fk->{table},
120 1399 ▶ ($self->db_schema ? (
Branch: 1 branch
BranchTrueFalseTotal
$self->db_schema ? : 20 1281 1301
Condition100%2/2
$rels[$fk->{"id"}] ||= {"local_columns", [], "remote_columns", undef, "remote_table", "DBIx::Class::Schema::Loader::Table"->new("loader", $self, "name", $fk->{"table"}, $self->db_schema ? ("schema", $self->db_schema->[0], "ignore_schema", 1) : ())}
l!l
981301
MC/DC100%1/1
$rels[$fk->{"id"}] ||= {"local_columns", [], "remote_columns", undef, "remote_table", "DBIx::Class::Schema::Loader::Table"->new("loader", $self, "name", $fk->{"table"}, $self->db_schema ? ("schema", $self->db_schema->[0], "ignore_schema", 1) : ())}
$rels[$fk->{"id"}]
Truth table100%2/2
A
A= $rels[$fk->{"id"}]
Aresult
11
00
121 schema => $self->db_schema->[0],
122 ignore_schema => 1,
123 ) : ()),
124 ),
125 };
126  
127 1399 push @{ $rel->{local_columns} }, $self->_lc($fk->{from});
128 1399 ▶ push @{ $rel->{remote_columns} }, $self->_lc($fk->{to}) if defined $fk->{to};
Branch: 1 branch
BranchTrueFalseTotal
if defined $fk->{"to"} 1371 28 1399
129  
130 $rel->{attrs} ||= {
131 on_delete => uc $fk->{on_delete},
132 on_update => uc $fk->{on_update},
133 1399 ▶ };
Condition100%2/2
$rel->{"attrs"} ||= {"on_delete", uc $fk->{"on_delete"}, "on_update", uc $fk->{"on_update"}}
l!l
981301
MC/DC100%1/1
$rel->{"attrs"} ||= {"on_delete", uc $fk->{"on_delete"}, "on_update", uc $fk->{"on_update"}}
$rel->{"attrs"}
Truth table100%2/2
A
A= $rel->{"attrs"}
Aresult
11
00
134  
135 warn "This is supposed to be the same rel but remote_table changed from ",
136 $rel->{remote_table}->name, " to ", $fk->{table}
137 1399 ▶ if $rel->{remote_table}->name ne $fk->{table};
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"remote_table"}->name ne $fk->{"table"} 0 1399 1399
138 }
139 1648 $sth->finish;
140  
141 # now we need to determine whether each FK is DEFERRABLE, this can only be
142 # done by parsing the DDL from sqlite_master
143  
144 1648 my $ddl = $self->dbh->selectcol_arrayref(<<"EOF", undef, $table->name, $table->name)->[0];
145 select sql from sqlite_master
146 where name = ? and tbl_name = ?
147 EOF
148  
149 1648 foreach my $fk (@rels) {
150 1301 my $local_cols = '"?' . (join '"? \s* , \s* "?', map quotemeta, @{ $fk->{local_columns} }) . '"?';
151 1301 ▶ my $remote_cols = '"?' . (join '"? \s* , \s* "?', map quotemeta, @{ $fk->{remote_columns} || [] }) . '"?';
Condition100%2/2
$fk->{'remote_columns'} || []
l!l
127328
MC/DC100%1/1
$fk->{'remote_columns'} || []
$fk->{'remote_columns'}
Truth table100%2/2
A
A= $fk->{'remote_columns'}
Aresult
11
00
152 1301 my ($deferrable_clause) = $ddl =~ /
153 foreign \s+ key \s* \( \s* $local_cols \s* \) \s* references \s* (?:\S+|".+?(?<!")") \s*
154 (?:\( \s* $remote_cols \s* \) \s*)?
155 (?:(?:
156 on \s+ (?:delete|update) \s+ (?:set \s+ null|set \s+ default|cascade|restrict|no \s+ action)
157 |
158 match \s* (?:\S+|".+?(?<!")")
159 ) \s*)*
160 ((?:not)? \s* deferrable)?
161 /sxi;
162  
163 1301 ▶ if ($deferrable_clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deferrable_clause) { } 14 1287 1301
164 14 ▶ $fk->{attrs}{is_deferrable} = $deferrable_clause =~ /not/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$deferrable_clause =~ /not/i ? : 0 14 14
165 }
166 else {
167 # check for inline constraint if 1 local column
168 1287 ▶ if (@{ $fk->{local_columns} } == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$fk->{'local_columns'};} == 1) { } 1189 98 1287
169 1189 my ($local_col) = @{ $fk->{local_columns} };
170 1189 ▶ my ($remote_col) = @{ $fk->{remote_columns} || [] };
Condition100%2/2
$fk->{'remote_columns'} || []
l!l
116128
MC/DC100%1/1
$fk->{'remote_columns'} || []
$fk->{'remote_columns'}
Truth table100%2/2
A
A= $fk->{'remote_columns'}
Aresult
11
00
171 1189 ▶ $remote_col ||= '';
Condition100%2/2
$remote_col ||= ""
l!l
116128
MC/DC100%1/1
$remote_col ||= ""
$remote_col
Truth table100%2/2
A
A= $remote_col
Aresult
11
00
172  
173 1189 my ($deferrable_clause) = $ddl =~ /
174 "?\Q$local_col\E"? \s* (?:\w+\s*)* (?: \( \s* \d\+ (?:\s*,\s*\d+)* \s* \) )? \s*
175 references \s+ (?:\S+|".+?(?<!")") (?:\s* \( \s* "?\Q$remote_col\E"? \s* \))? \s*
176 (?:(?:
177 on \s+ (?:delete|update) \s+ (?:set \s+ null|set \s+ default|cascade|restrict|no \s+ action)
178 |
179 match \s* (?:\S+|".+?(?<!")")
180 ) \s*)*
181 ((?:not)? \s* deferrable)?
182 /sxi;
183  
184 1189 ▶ if ($deferrable_clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deferrable_clause) { } 14 1175 1189
185 14 ▶ $fk->{attrs}{is_deferrable} = $deferrable_clause =~ /not/i ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$deferrable_clause =~ /not/i ? : 0 14 14
186 }
187 else {
188 1175 $fk->{attrs}{is_deferrable} = 0;
189 }
190 }
191 else {
192 98 $fk->{attrs}{is_deferrable} = 0;
193 }
194 }
195 }
196  
197 1648 return \@rels;
198 }
199  
200 1648 ▶sub _table_uniq_info {
Subroutine _table_uniq_info: called CC=4 SCAR=13.9
201 1648 my ($self, $table) = @_;
202  
203 1648 my $sth = $self->dbh->prepare(
204 "pragma index_list(" . $self->dbh->quote($table) . ")"
205 );
206 1648 $sth->execute;
207  
208 1648 my @uniqs;
209 1648 ▶ while (my $idx = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $idx = $sth->fetchrow_hashref) 556 1648 2204
210 556 ▶ next unless $idx->{unique};
Branch: 1 branch
BranchTrueFalseTotal
unless $idx->{"unique"} 0 556 556
211  
212 556 my $name = $idx->{name};
213  
214 556 my $get_idx_sth = $self->dbh->prepare("pragma index_info(" . $self->dbh->quote($name) . ")");
215 556 $get_idx_sth->execute;
216 556 my @cols;
217 556 ▶ while (my $idx_row = $get_idx_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $idx_row = $get_idx_sth->fetchrow_hashref) 788 556 1344
218 788 push @cols, $self->_lc($idx_row->{name});
219 }
220 556 $get_idx_sth->finish;
221  
222 # Rename because SQLite complains about sqlite_ prefixes on identifiers
223 # and ignores constraint names in DDL.
224 556 $name = (join '_', @cols) . '_unique';
225  
226 556 push @uniqs, [ $name => \@cols ];
227 }
228 1648 $sth->finish;
229 1648 return [ sort { $a->[0] cmp $b->[0] } @uniqs ];
230 }
231  
232 145 ▶sub _tables_list {
Subroutine _tables_list: called CC=5 SCAR=16.1
233 145 my ($self) = @_;
234  
235 145 my $sth = $self->dbh->prepare("SELECT * FROM sqlite_master");
236 145 $sth->execute;
237 145 my @tables;
238 145 ▶ while ( my $row = $sth->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 1120 145 1265
239 1120 ▶ next unless $row->{type} =~ /^(?:table|view)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"type"} =~ /^(?:table|view)\z/i 278 842 1120
240 842 ▶ next if $row->{tbl_name} =~ /^sqlite_/;
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"tbl_name"} =~ /^sqlite_/ 7 835 842
241 push @tables, DBIx::Class::Schema::Loader::Table->new(
242 loader => $self,
243 name => $row->{tbl_name},
244 835 ▶ ($self->db_schema ? (
Branch: 1 branch
BranchTrueFalseTotal
$self->db_schema ? : 24 811 835
245 schema => $self->db_schema->[0],
246 ignore_schema => 1, # for qualify_objects tests
247 ) : ()),
248 );
249 }
250 145 $sth->finish;
251 145 return $self->_filter_tables(\@tables);
252 }
253  
254 2455 ▶sub _table_info_matches {
Subroutine _table_info_matches: called CC=3 SCAR=12.0
255 2455 my ($self, $table, $info) = @_;
256  
257 2455 my $table_schema = $table->schema;
258 2455 ▶ $table_schema = 'main' if !defined $table_schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table_schema 2455 0 2455
259 return $info->{TABLE_SCHEM} eq $table_schema
260 2455 ▶ && $info->{TABLE_NAME} eq $table->name;
Condition33%1/3
$info->{'TABLE_SCHEM'} eq $table_schema && $info->{'TABLE_NAME'} eq $table->name
!ll&&!rl&&r
002455
MC/DC0%0/2
$info->{'TABLE_SCHEM'} eq $table_schema && $info->{'TABLE_NAME'} eq $table->name
$info->{'TABLE_SCHEM'} eq $table_schema $info->{'TABLE_NAME'} eq $table->name
Truth table33%1/3
A && B
A= $info->{'TABLE_SCHEM'} eq $table_schemaB= $info->{'TABLE_NAME'} eq $table->name
ABresult
0X0
100
111
261 }
262  
263 =head1 SEE ALSO
264  
265 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
266 L<DBIx::Class::Schema::Loader::DBI>
267  
268 =head1 AUTHORS
269  
270 See L<DBIx::Class::Schema::Loader/AUTHORS>.
271  
272 =head1 LICENSE
273  
274 This library is free software; you can redistribute it and/or modify it under
275 the same terms as Perl itself.
276  
277 =cut
278  
279 48 1;
280 # vim:et sts=4 sw=4 tw=0:
« blib/lib/DBIx/Class/Schema/Loader/DBI/ADO.pm ↑ Summary blib/lib/DBIx/Class/Schema/Loader.pm »