Oracle/Identifier.pm

filter: Statementstmt 100% 61 / 61 Branchbran 93.7% 15 / 16 Conditioncond 75.0% 6 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 93.4% 99 / 106 CC 15 SCAR 17.5
CC
15
Cov
96%
shorten
11.0
_mapped
2.1
split_table
2.0
CRAP 5.7
« Oracle/Util.pm ↑ Summary Oracle/Diff/Index.pm »
1 package DBIO::Oracle::Identifier;
2 # ABSTRACT: Shorten Oracle identifiers to the 30-character limit
3  
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 our $VERSION = '0.900007';
8  
9 45 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use Math::Base36 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14  
15 247 ▶sub shorten {
Subroutine shorten: called CC=11 SCAR=24.0
Pod documented
16 247 my ($to_shorten, $keywords) = @_;
17  
18 247 my $max_len = 30;
19 247 my $min_entropy = 10;
20  
21 247 my $max_trunc = $max_len - $min_entropy - 1;
22  
23 247 ▶ return $to_shorten
Branch: 1 branch
BranchTrueFalseTotal
if length $to_shorten <= $max_len 142 105 247
24 if length($to_shorten) <= $max_len;
25  
26 105 ▶ die "'keywords' needs to be an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
if defined $keywords and ref $keywords ne "ARRAY" 0 105 105
Condition66%2/3
defined $keywords and ref $keywords ne "ARRAY"
!ll&&!rl&&r
10320
MC/DC0%0/2
defined $keywords and ref $keywords ne "ARRAY"
defined $keywords ref $keywords ne "ARRAY"
Truth table66%2/3
A and B
A= defined $keywordsB= ref $keywords ne "ARRAY"
ABresult
0X0
100
111
27 if defined $keywords && ref $keywords ne 'ARRAY';
28  
29 105 ▶ my @keywords = @{$keywords || []};
Condition100%2/2
$keywords || []
l!l
2103
MC/DC100%1/1
$keywords || []
$keywords
Truth table100%2/2
A
A= $keywords
Aresult
11
00
30 105 ▶ @keywords = $to_shorten unless @keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless @keywords 103 2 105
31  
32 105 my $b36sum = Math::Base36::encode_base36(
33 Math::BigInt->from_hex(
34 '0x' . Digest::MD5::md5_hex($to_shorten)
35 )
36 );
37  
38 105 my ($concat_len, @lengths);
39 105 for (@keywords) {
40 107 $_ = ucfirst(lc($_));
41 107 $_ =~ s/\_+(\w)/uc($1)/eg;
42  
43 107 push @lengths, length($_);
44 107 $concat_len += $lengths[-1];
45 }
46  
47 105 ▶ if ($concat_len > $max_trunc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($concat_len > $max_trunc) 103 2 105
48 103 $concat_len = 0;
49 103 @lengths = ();
50  
51 103 for (@keywords) {
52 103 $_ =~ s/[aeiou]//g;
53  
54 103 push @lengths, length($_);
55 103 $concat_len += $lengths[-1];
56 }
57 }
58  
59 105 ▶ if ($concat_len > $max_trunc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($concat_len > $max_trunc) 103 2 105
60 103 my $trim_ratio = $max_trunc / $concat_len;
61  
62 103 for my $i (0 .. $#keywords) {
63 103 $keywords[$i] = substr($keywords[$i], 0, int($trim_ratio * $lengths[$i]));
64 }
65 }
66  
67 105 my $fin = join('', @keywords);
68 105 my $fin_len = length $fin;
69  
70 105 return sprintf('%s_%s',
71 $fin,
72 substr($b36sum, 0, $max_len - $fin_len - 1),
73 );
74 }
75  
76  
77 899 ▶sub quote {
Subroutine quote: called CC=1 SCAR=0.0
Pod documented
78 899 my ($name) = @_;
79 899 $name =~ s/"/""/g;
80 899 return qq{"$name"};
81 }
82  
83  
84 985 ▶sub split_table {
Subroutine split_table: called CC=2 SCAR=6.9
Pod documented
85 985 my ($name) = @_;
86 985 ▶ return ($1, $2) if $name =~ /\A([^.]+)\.(.+)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A([^.]+)\.(.+)\z/s 322 663 985
87 663 return (undef, $name);
88 }
89  
90  
91 373 ▶sub quote_table {
Subroutine quote_table: called CC=2 SCAR=6.9
Pod documented
92 373 my ($schema, $table) = split_table($_[0]);
93 373 ▶ return defined $schema ? quote(_mapped($schema)) . '.' . quote($table) : quote($table);
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 76 297 373
94 }
95  
96  
97 131 ▶sub quote_in {
Subroutine quote_in: called CC=2 SCAR=6.9
Pod documented
98 131 my ($table_key, $name) = @_;
99 131 my ($schema) = split_table($table_key);
100 131 ▶ return defined $schema ? quote(_mapped($schema)) . '.' . quote($name) : quote($name);
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 65 66 131
101 }
102  
103  
104 45 our %SCHEMA_MAP;
105  
106 141 ▶sub _mapped { $SCHEMA_MAP{ $_[0] } // $_[0] }
Subroutine _mapped: called CC=2 SCAR=7.2
Condition66%2/3
$DBIO::Oracle::Identifier::SCHEMA_MAP{$_[0]} // $_[0]
l!l&&r!l&&!r
191220
MC/DC0%0/2
$DBIO::Oracle::Identifier::SCHEMA_MAP{$_[0]} // $_[0]
$DBIO::Oracle::Identifier::SCHEMA_MAP{$_[0]} $_[0]
Truth table66%2/3
A // B
A= $DBIO::Oracle::Identifier::SCHEMA_MAP{$_[0]}B= $_[0]
ABresult
1X1
011
000
107  
108 45 1;
109  
110 __END__
« Oracle/Util.pm ↑ Summary Oracle/Diff/Index.pm »