Oracle/Identifier.pm

filter: Statementstmt 100% 47 / 47 Branchbran 90.0% 9 / 10 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 93.0% 67 / 72 CC 11 SCAR 20.6
CC
11
Cov
97%
shorten
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.9
« Oracle/Diff/Table.pm ↑ Summary Oracle/Introspect/Tables.pm »
1 package DBIO::Oracle::Identifier;
2 # ABSTRACT: Shorten Oracle identifiers to the 30-character limit
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Math::Base36 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 6 ▶sub shorten {
Subroutine shorten: called CC=11 SCAR=24.0
Pod undocumented
14 6 my ($to_shorten, $keywords) = @_;
15  
16 6 my $max_len = 30;
17 6 my $min_entropy = 10;
18  
19 6 my $max_trunc = $max_len - $min_entropy - 1;
20  
21 6 ▶ return $to_shorten
Branch: 1 branch
BranchTrueFalseTotal
if length $to_shorten <= $max_len 3 3 6
22 if length($to_shorten) <= $max_len;
23  
24 3 ▶ die "'keywords' needs to be an arrayref"
Branch: 1 branch
BranchTrueFalseTotal
if defined $keywords and ref $keywords ne "ARRAY" 0 3 3
Condition66%2/3
defined $keywords and ref $keywords ne "ARRAY"
!ll&&!rl&&r
120
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
25 if defined $keywords && ref $keywords ne 'ARRAY';
26  
27 3 ▶ my @keywords = @{$keywords || []};
Condition100%2/2
$keywords || []
l!l
21
MC/DC100%1/1
$keywords || []
$keywords
Truth table100%2/2
A
A= $keywords
Aresult
11
00
28 3 ▶ @keywords = $to_shorten unless @keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless @keywords 1 2 3
29  
30 3 my $b36sum = Math::Base36::encode_base36(
31 Math::BigInt->from_hex(
32 '0x' . Digest::MD5::md5_hex($to_shorten)
33 )
34 );
35  
36 3 my ($concat_len, @lengths);
37 3 for (@keywords) {
38 5 $_ = ucfirst(lc($_));
39 5 $_ =~ s/\_+(\w)/uc($1)/eg;
40  
41 5 push @lengths, length($_);
42 5 $concat_len += $lengths[-1];
43 }
44  
45 3 ▶ if ($concat_len > $max_trunc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($concat_len > $max_trunc) 1 2 3
46 1 $concat_len = 0;
47 1 @lengths = ();
48  
49 1 for (@keywords) {
50 1 $_ =~ s/[aeiou]//g;
51  
52 1 push @lengths, length($_);
53 1 $concat_len += $lengths[-1];
54 }
55 }
56  
57 3 ▶ if ($concat_len > $max_trunc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($concat_len > $max_trunc) 1 2 3
58 1 my $trim_ratio = $max_trunc / $concat_len;
59  
60 1 for my $i (0 .. $#keywords) {
61 1 $keywords[$i] = substr($keywords[$i], 0, int($trim_ratio * $lengths[$i]));
62 }
63 }
64  
65 3 my $fin = join('', @keywords);
66 3 my $fin_len = length $fin;
67  
68 3 return sprintf('%s_%s',
69 $fin,
70 substr($b36sum, 0, $max_len - $fin_len - 1),
71 );
72 }
73  
74 3 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 DBIO::Oracle::Identifier - Shorten Oracle identifiers to the 30-character limit
85  
86 =head1 VERSION
87  
88 version 0.900001
89  
90 =head1 DESCRIPTION
91  
92 Oracle (pre-12.2) limits identifiers to 30 bytes. Both the SQL-generation
93 side (L<DBIO::Oracle::SQLMaker>, at query time) and the deploy side
94 (L<DBIO::Oracle::DDL>, when emitting generated sequence and index names)
95 need to fit names into that limit using the I<same> algorithm, so a name
96 generated at deploy time matches the name referenced at query time.
97  
98 This module owns that single algorithm. Names already within the limit are
99 returned unchanged; longer names are CamelCase-compressed (vowels trimmed if
100 needed) and suffixed with a base36-encoded MD5 hash so the result is stable
101 and collision-resistant.
102  
103 =func shorten
104  
105 my $name = DBIO::Oracle::Identifier::shorten($identifier);
106 my $name = DBIO::Oracle::Identifier::shorten($identifier, \@keywords);
107  
108 Returns C<$identifier> unchanged when it is 30 characters or fewer. Otherwise
109 returns a deterministic shortened form. The optional C<\@keywords> arrayref
110 controls the human-readable prefix (defaults to the identifier itself).
111  
112 =head1 AUTHOR
113  
114 DBIO & DBIx::Class Authors
115  
116 =head1 COPYRIGHT AND LICENSE
117  
118 Copyright (C) 2026 DBIO Authors
119 Portions Copyright (C) 2005-2025 DBIx::Class Authors
120 Based on DBIx::Class, heavily modified.
121  
122 This is free software; you can redistribute it and/or modify it under
123 the same terms as the Perl 5 programming language system itself.
124  
125 =cut
« Oracle/Diff/Table.pm ↑ Summary Oracle/Introspect/Tables.pm »