SQLite/Util.pm

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