blib/lib/DBIO/SQLite/Util.pm

filter: Statementstmt 100% 111 / 111 Branchbran 96.8% 62 / 64 Conditioncond 69.0% 29 / 42 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 100% 22 / 22 Podpod 100% 17 / 17 totaltotal 90.3% 252 / 279 CC 51 SCAR 15.9
CC
51
Cov
93%
create_name_index
10.0
libsqlite_version
7.6
type_affinity
7.0
CRAP 4.9
« blib/lib/DBIO/SQLite/Test.pm ↑ Summary blib/lib/DBIO/SQLite/Extension.pm »
1 package DBIO::SQLite::Util;
2 # ABSTRACT: Shared SQLite-specific helper functions for DBIO
3  
4 221 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 221 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 221 our $VERSION = '0.900007';
8  
9 221 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 221 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 221 our @EXPORT_OK = qw(
14 column_is_nullable comparable_statement create_name_index
15 foreign_key_action libsqlite_version
16 create_table_name_index default_literal_sql default_value_sql
17 pk_is_rowid_alias quote_identifier refuse_qualified_name
18 selectall_hashrefs split_type_size sql_tokens
19 token_identifier type_affinity type_with_size
20 );
21  
22  
23  
24 758 ▶sub libsqlite_version {
Subroutine libsqlite_version: called CC=6 SCAR=20.3
Pod documented
25 758 my ($dbh) = @_;
26 758 my ($version) = $dbh->selectrow_array('SELECT sqlite_version()');
27 758 ▶ my @part = split /\./, ($version // '0');
Condition50%1/2
$version // 0
l!l
7580
MC/DC0%0/1
$version // 0
$version
Truth table50%1/2
A
A= $version
Aresult
11
00
28 return (
29 758 ▶ $version // '(unknown)',
Condition50%4/8
$version // "(unknown)"
l!l
7580
$part[0] // 0
l!l
7580
$part[1] // 0
l!l
7580
$part[2] // 0
l!l
7580
MC/DC0%0/1
$version // "(unknown)"
$version
MC/DC0%0/1
$part[0] // 0
$part[0]
MC/DC0%0/1
$part[1] // 0
$part[1]
MC/DC0%0/1
$part[2] // 0
$part[2]
Truth table50%1/2
A
A= $version
Aresult
11
00
Truth table50%1/2
A
A= $part[0]
Aresult
11
00
Truth table50%1/2
A
A= $part[1]
Aresult
11
00
Truth table50%1/2
A
A= $part[2]
Aresult
11
00
30 ($part[0] // 0) * 1_000_000 + ($part[1] // 0) * 1_000 + ($part[2] // 0),
31 );
32 }
33  
34  
35 221 my %FK_ACTION = map { $_ => 1 } ('NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', 'SET DEFAULT');
36  
37 26011 ▶sub foreign_key_action {
Subroutine foreign_key_action: called CC=5 SCAR=16.1
Pod documented
38 26011 my ($value, $context) = @_;
39 26011 ▶ return undef unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 22982 3029 26011
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
22971113029
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
40  
41 3029 my $action = $value;
42 3029 $action =~ s/\s+/ /g;
43 3029 $action =~ s/^ //;
44 3029 $action =~ s/ $//;
45 3029 ▶ return $action if $FK_ACTION{ uc $action };
Branch: 1 branch
BranchTrueFalseTotal
if $FK_ACTION{uc $action} 3020 9 3029
46  
47 9 ▶ DBIO::Exception->throw(sprintf
Condition50%1/2
$context // "unknown foreign key"
l!l
90
MC/DC0%0/1
$context // "unknown foreign key"
$context
Truth table50%1/2
A
A= $context
Aresult
11
00
48 "Unknown foreign key action '%s' (%s): expected one of NO ACTION, RESTRICT, CASCADE, SET NULL, SET DEFAULT",
49 $value, $context // 'unknown foreign key');
50 }
51  
52  
53 3725 ▶sub column_is_nullable {
Subroutine column_is_nullable: called CC=4 SCAR=13.9
Pod documented
54 3725 my ($not_null, $is_pk, $pk_is_rowid_alias) = @_;
55 3725 ▶ return 0 if $not_null;
Branch: 1 branch
BranchTrueFalseTotal
if $not_null 1485 2240 3725
56 2240 ▶ return 1 unless $is_pk;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_pk 1744 496 2240
57 496 ▶ return $pk_is_rowid_alias ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$pk_is_rowid_alias ? : 424 72 496
58 }
59  
60  
61 130523 ▶sub quote_identifier {
Subroutine quote_identifier: called CC=1 SCAR=0.0
Pod documented
62 130523 my ($name) = @_;
63 130523 $name =~ s/"/""/g;
64 130523 return qq{"$name"};
65 }
66  
67  
68 1357 ▶sub pk_is_rowid_alias {
Subroutine pk_is_rowid_alias: called CC=4 SCAR=14.1
Pod documented
69 1357 my ($dbh, $table) = @_;
70 1357 ▶ my $list = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $list = eval { do { selectall_hashrefs($dbh, sprintf("PRAGMA index_list(%s)", quote_identifier($table))) } } or return 1 1357 0 1357
71 1357 selectall_hashrefs($dbh,
72 sprintf('PRAGMA index_list(%s)', quote_identifier($table)));
73 } or return 1;
74 1357 ▶ return (grep { ($_->{origin} // '') eq 'pk' } @$list) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({($_->{'origin'} // '') eq 'pk';} @$list) ? : 145 1212 1357
Condition50%1/2
$_->{"origin"} // ""
l!l
4460
MC/DC0%0/1
$_->{"origin"} // ""
$_->{"origin"}
Truth table50%1/2
A
A= $_->{"origin"}
Aresult
11
00
75 }
76  
77  
78 6203 ▶sub selectall_hashrefs {
Subroutine selectall_hashrefs: called CC=2 SCAR=6.9
Pod documented
79 6203 my ($dbh, $sql) = @_;
80 6203 my $sth = $dbh->prepare($sql);
81 6203 $sth->execute;
82 6203 ▶ return [] unless $sth->{NUM_OF_FIELDS};
Branch: 1 branch
BranchTrueFalseTotal
unless $sth->{"NUM_OF_FIELDS"} 2 6201 6203
83 6201 return $sth->fetchall_arrayref({});
84 }
85  
86  
87 221 my $BARE_DEFAULT = qr{\A(?:
88 [+-]?(?:0[xX][0-9a-fA-F]+|(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?) # number
89 | [xX]?'(?:[^']|'')*' # string, blob
90 | [A-Za-z_\x{80}-\x{10FFFF}][A-Za-z0-9_\$\x{80}-\x{10FFFF}]* # keyword, identifier
91 | "(?:[^"]|"")*" | `(?:[^`]|``)*` | \[[^\]]*\] # quoted identifier
92 )\z}x;
93  
94 40 ▶sub default_value_sql {
Subroutine default_value_sql: called CC=3 SCAR=11.0
Pod documented
95 40 my ($default) = @_;
96 40 ▶ return $default if $default =~ $BARE_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if $default =~ /$BARE_DEFAULT/ 30 10 40
97 10 ▶ return $default =~ /--/ ? "($default\n)" : "($default)";
Branch: 1 branch
BranchTrueFalseTotal
$default =~ /--/ ? : 1 9 10
98 }
99  
100  
101 15247 ▶sub refuse_qualified_name {
Subroutine refuse_qualified_name: called CC=4 SCAR=14.6
Pod documented
102 15247 my ($kind, $name) = @_;
103 15247 ▶ return unless defined $name and !ref $name and $name =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and $name =~ /\./ 15240 7 15247
Condition50%3/6
defined $name and not ref $name and $name =~ /\./
!ll&&!rl&&r
0152407
defined $name and not ref $name
!ll&&!rl&&r
0015247
MC/DC33%1/3
defined $name and not ref $name and $name =~ /\./
defined $name not ref $name $name =~ /\./
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ /\./
ABCresult
0XX0
10X0
1100
1111
104 7 DBIO::Exception->throw(sprintf
105 "SQLite refuses the %s name '%s': SQLite has no schemas to qualify a name "
106 . "with, so it would be one %s named literally '%s'. A name that is "
107 . "meant as SQL is declared as a scalar ref, \\'%s', and is written as given",
108 $kind, $name, $kind, $name, $name);
109 }
110  
111  
112 3704 ▶sub split_type_size {
Subroutine split_type_size: called CC=4 SCAR=13.9
Pod documented
113 3704 my ($type) = @_;
114 3704 ▶ return ($type, undef) unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 3703 3704
115 3703 ▶ return ($type, undef)
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /\A\s*(\w[\w ]*?)\s*\(\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)\s*\z/ 3286 417 3703
116 unless $type =~ /\A\s*(\w[\w ]*?)\s*\(\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)\s*\z/;
117 417 my ($base, $first, $second) = ($1, $2, $3);
118 417 ▶ return ($base, defined $second ? [ $first + 0, $second + 0 ] : $first + 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $second ? : 18 399 417
119 }
120  
121  
122 65 ▶sub type_with_size {
Subroutine type_with_size: called CC=3 SCAR=11.0
Pod documented
123 65 my ($type, $size) = @_;
124 65 ▶ return $type unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 55 10 65
125 10 ▶ return $type . '(' . join(',', @$size) . ')' if ref $size eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $size eq "ARRAY" 1 9 10
126 9 return $type . "($size)";
127 }
128  
129  
130 1315 ▶sub type_affinity {
Subroutine type_affinity: called CC=7 SCAR=19.5
Pod documented
131 1315 my ($type) = @_;
132 1315 ▶ $type //= '';
Condition100%2/2
$type //= ""
l!l
130510
MC/DC100%1/1
$type //= ""
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
133 1315 ▶ return 'INTEGER' if $type =~ /INT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /INT/i 856 459 1315
134 459 ▶ return 'TEXT' if $type =~ /CHAR|CLOB|TEXT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /CHAR|CLOB|TEXT/i 313 146 459
135 146 ▶ return 'BLOB' if $type =~ /BLOB/i || $type eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /BLOB/i or $type eq "" 39 107 146
Condition100%3/3
$type =~ /BLOB/i or $type eq ""
l!l&&r!l&&!r
1722107
MC/DC100%2/2
$type =~ /BLOB/i or $type eq ""
$type =~ /BLOB/i $type eq ""
Truth table100%3/3
A or B
A= $type =~ /BLOB/iB= $type eq ""
ABresult
1X1
011
000
136 107 ▶ return 'REAL' if $type =~ /REAL|FLOA|DOUB/i;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /REAL|FLOA|DOUB/i 40 67 107
137 67 return 'NUMERIC';
138 }
139  
140  
141 1004 ▶sub default_literal_sql {
Subroutine default_literal_sql: called CC=3 SCAR=11.0
Pod documented
142 1004 my ($value, $type) = @_;
143 # A number goes bare only when it is the canonical form of that number
144 # (5, -1, 1.5; not 007 or 1.50): anything else is a string that merely holds
145 # digits and keeps its quotes, so '007' stays '007' in a column of any type.
146 1004 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/ and type_affinity($type) ne "TEXT" 753 251 1004
Condition100%3/3
$value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/ and type_affinity($type) ne "TEXT"
!ll&&!rl&&r
20150753
MC/DC100%2/2
$value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/ and type_affinity($type) ne "TEXT"
$value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/ type_affinity($type) ne "TEXT"
Truth table100%3/3
A and B
A= $value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/B= type_affinity($type) ne "TEXT"
ABresult
0X0
100
111
147 if $value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/ && type_affinity($type) ne 'TEXT';
148 251 (my $quoted = $value) =~ s/'/''/g;
149 251 return "'$quoted'";
150 }
151  
152  
153 221 my $QUOTED = qr{ '(?:[^']|'')*' | "(?:[^"]|"")*" | `(?:[^`]|``)*` | \[[^\]]*\] }x;
154  
155 221 my $TOKEN = qr{\G(?:
156 (?<skip> (?: \s+ | --[^\n]*\n? | /\*.*?(?:\*/|\z) )+ )
157 | (?<quoted> $QUOTED )
158 | (?<punct> [(),] )
159 | (?<word> [^\s'"`\[(),/-]+ | . )
160 )}xs;
161  
162 5338 ▶sub sql_tokens {
Subroutine sql_tokens: called CC=2 SCAR=6.9
Pod documented
163 5338 my ($sql) = @_;
164 5338 my @tokens;
165 5338 ▶ while ($sql =~ /$TOKEN/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /$TOKEN/cg) 163173 5338 168511
166 163173 my ($kind) = grep { defined $+{$_} } qw(skip quoted punct word);
167 163173 push @tokens, [ $kind, $+{$kind} ];
168 }
169 5338 return @tokens;
170 }
171  
172  
173 4888 ▶sub token_identifier {
Subroutine token_identifier: called CC=3 SCAR=11.0
Pod documented
174 4888 my ($kind, $text) = @_;
175 4888 ▶ return $text unless $kind eq 'quoted';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "quoted" 1912 2976 4888
176 2976 my $quote = substr $text, 0, 1;
177 2976 my $name = substr $text, 1, -1;
178 2976 ▶ $name =~ s/\Q$quote$quote\E/$quote/g unless $quote eq '[';
Branch: 1 branch
BranchTrueFalseTotal
unless $quote eq "[" 2956 20 2976
179 2976 return $name;
180 }
181  
182  
183 4934 ▶sub create_name_index {
Subroutine create_name_index: called CC=10 SCAR=23.0
Pod documented
184 4934 my ($tokens, @words) = @_;
185  
186 # The tokens that count, by their place in the list.
187 4934 my @at = grep { $tokens->[$_][0] ne 'skip' } 0 .. $#$tokens;
188 13067 ▶ my $word = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
189 13067 ▶ my $token = defined $at[ $_[0] ] ? $tokens->[ $at[ $_[0] ] ] : return '';
Branch: 1 branch
BranchTrueFalseTotal
defined $at[$_[0]] ? : 13001 66 13067
190 13001 ▶ return $token->[0] eq 'word' ? uc $token->[1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$token->[0] eq 'word' ? : 11088 1913 13001
191 4934 };
192  
193 4934 ▶ return unless $word->(0) eq 'CREATE';
Branch: 1 branch
BranchTrueFalseTotal
unless &$word(0) eq "CREATE" 65 4869 4934
194 4869 ▶ $word->($_ + 1) eq $words[$_] or return for 0 .. $#words;
Branch: 1 branch
BranchTrueFalseTotal
&$word($_ + 1) eq $words[$_] or return 3260 1796 5056
195  
196 # Only the three words together are the clause.
197 3073 my $name = @words + 1;
198 3073 ▶ $name += 3
Branch: 1 branch
BranchTrueFalseTotal
if &$word($name) eq "IF" and &$word($name + 1) eq "NOT" and &$word($name + 2) eq "EXISTS" 2 3071 3073
Condition66%4/6
&$word($name) eq "IF" and &$word($name + 1) eq "NOT" and &$word($name + 2) eq "EXISTS"
!ll&&!rl&&r
307102
&$word($name) eq "IF" and &$word($name + 1) eq "NOT"
!ll&&!rl&&r
307102
MC/DC33%1/3
&$word($name) eq "IF" and &$word($name + 1) eq "NOT" and &$word($name + 2) eq "EXISTS"
&$word($name) eq "IF" &$word($name + 1) eq "NOT" &$word($name + 2) eq "EXISTS"
Truth table50%2/4
A and B and C
A= &$word($name) eq "IF"B= &$word($name + 1) eq "NOT"C= &$word($name + 2) eq "EXISTS"
ABCresult
0XX0
10X0
1100
1111
199 if $word->($name) eq 'IF' && $word->($name + 1) eq 'NOT'
200 && $word->($name + 2) eq 'EXISTS';
201 3073 ▶ return unless defined $at[$name];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at[$name] 1 3072 3073
202  
203 3072 my $kind = $tokens->[ $at[$name] ][0];
204 3072 ▶ return $kind eq 'word' || $kind eq 'quoted' ? $at[$name] : ();
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'word' || $kind eq 'quoted' ? : 3071 1 3072
Condition100%3/3
$kind eq "word" or $kind eq "quoted"
l!l&&r!l&&!r
115919121
MC/DC100%2/2
$kind eq "word" or $kind eq "quoted"
$kind eq "word" $kind eq "quoted"
Truth table100%3/3
A or B
A= $kind eq "word"B= $kind eq "quoted"
ABresult
1X1
011
000
205 }
206  
207  
208 2877 ▶sub create_table_name_index {
Subroutine create_table_name_index: called CC=1 SCAR=0.0
Pod documented
209 2877 my ($tokens) = @_;
210 2877 return create_name_index($tokens, 'TABLE');
211 }
212  
213  
214 124 ▶sub comparable_statement {
Subroutine comparable_statement: called CC=3 SCAR=11.1
Pod documented
215 124 my ($sql, @words) = @_;
216 124 ▶ my @tokens = sql_tokens($sql // '');
Condition50%1/2
$sql // ""
l!l
1240
MC/DC0%0/1
$sql // ""
$sql
Truth table50%1/2
A
A= $sql
Aresult
11
00
217 124 my $at = create_name_index(\@tokens, @words);
218 124 ▶ $tokens[$at][1] = quote_identifier(token_identifier(@{ $tokens[$at] }))
Branch: 1 branch
BranchTrueFalseTotal
if defined $at 124 0 124
219 if defined $at;
220 124 return join ' ', map { $_->[1] } grep { $_->[0] ne 'skip' } @tokens;
221 }
222  
223 221 1;
224  
225 __END__
« blib/lib/DBIO/SQLite/Test.pm ↑ Summary blib/lib/DBIO/SQLite/Extension.pm »