| 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
MC/DC0%0/1
$version // 0
$version
Truth table50%1/2
A
A= $version
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 758 | ▶ | $version // '(unknown)', | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%4/8
$version // "(unknown)"
$part[0] // 0
$part[1] // 0
$part[2] // 0
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
Truth table50%1/2
A
A= $part[0]
Truth table50%1/2
A
A= $part[1]
Truth table50%1/2
A
A= $part[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $value and length $value
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$context // "unknown foreign key"
MC/DC0%0/1
$context // "unknown foreign key"
$context
Truth table50%1/2
A
A= $context
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 2240 | ▶ | return 1 unless $is_pk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 496 | ▶ | return $pk_is_rowid_alias ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$_->{"origin"} // ""
MC/DC0%0/1
$_->{"origin"} // ""
$_->{"origin"}
Truth table50%1/2
A
A= $_->{"origin"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 10 | ▶ | return $default =~ /--/ ? "($default\n)" : "($default)"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $name and not ref $name and $name =~ /\./
defined $name and not ref $name
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 =~ /\./
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 3703 | ▶ | return ($type, undef) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 10 | ▶ | return $type . '(' . join(',', @$size) . ')' if ref $size eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 //= ""
MC/DC100%1/1
$type //= ""
$type
Truth table100%2/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1315 | ▶ | return 'INTEGER' if $type =~ /INT/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 459 | ▶ | return 'TEXT' if $type =~ /CHAR|CLOB|TEXT/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 146 | ▶ | return 'BLOB' if $type =~ /BLOB/i || $type eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type =~ /BLOB/i or $type eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 107 | ▶ | return 'REAL' if $type =~ /REAL|FLOA|DOUB/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$value =~ /\A-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?\z/ and type_affinity($type) ne "TEXT"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 13001 | ▶ | return $token->[0] eq 'word' ? uc $token->[1] : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 4934 | }; | |||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 4934 | ▶ | return unless $word->(0) eq 'CREATE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 4869 | ▶ | $word->($_ + 1) eq $words[$_] or return for 0 .. $#words; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Only the three words together are the clause. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 3073 | my $name = @words + 1; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 3073 | ▶ | $name += 3 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
&$word($name) eq "IF" and &$word($name + 1) eq "NOT" and &$word($name + 2) eq "EXISTS"
&$word($name) eq "IF" and &$word($name + 1) eq "NOT"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 3072 | my $kind = $tokens->[ $at[$name] ][0]; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 3072 | ▶ | return $kind eq 'word' || $kind eq 'quoted' ? $at[$name] : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "word" or $kind eq "quoted"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$sql // ""
$sql
Truth table50%1/2
A
A= $sql
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 124 | my $at = create_name_index(\@tokens, @words); | |||||||||||||||||||||||||||||||||||||||||
| 218 | 124 | ▶ | $tokens[$at][1] = quote_identifier(token_identifier(@{ $tokens[$at] })) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | if defined $at; | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 124 | return join ' ', map { $_->[1] } grep { $_->[0] ne 'skip' } @tokens; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 221 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | __END__ | ||||||||||||||||||||||||||||||||||||||||||