Informix/Types.pm

filter: Statementstmt 95.4% 63 / 66 Branchbran 75.0% 36 / 48 Conditioncond 81.9% 59 / 72 MC/DCmcdc 59.4% 22 / 37 Subroutinesub 88.8% 8 / 9 Podpod 100% 6 / 6 totaltotal 81.5% 194 / 238 CC 52 SCAR 24.7
CC
52
Cov
85%
ddl_type
16.0
qualifier_for_coltype
13.6
is_numeric_type
12.0
CRAP 11.8
« Informix/Introspect/Tables.pm ↑ Summary Informix/Diff/Table.pm »
1 package DBIO::Informix::Types;
2 # ABSTRACT: Canonical Informix type vocabulary
3  
4 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 46 our $VERSION = '0.900007';
8  
9 46 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 46 our @EXPORT_OK = qw(logical_for_coltype size_for_coltype ddl_type qualifier_for_coltype lob_kind is_numeric_type);
11  
12  
13 # Informix coltype (mod 256) -> logical type name. Measured against the
14 # syscolumns of Informix 15.0.1 (karr k20); 10 (DATETIME) and 41 (opaque)
15 # are refined in logical_for_coltype.
16 46 my %LOGICAL_FOR_COLTYPE = (
17 0 => 'char',
18 1 => 'smallint',
19 2 => 'integer',
20 3 => 'float',
21 4 => 'smallfloat',
22 5 => 'decimal',
23 6 => 'serial',
24 7 => 'date',
25 8 => 'money',
26 10 => 'datetime',
27 11 => 'bytea',
28 12 => 'text',
29 13 => 'varchar',
30 14 => 'interval',
31 15 => 'nchar',
32 16 => 'nvarchar',
33 17 => 'int8',
34 18 => 'serial8',
35 19 => 'set',
36 20 => 'multiset',
37 21 => 'list',
38 40 => 'lvarchar',
39 45 => 'boolean',
40 52 => 'bigint',
41 53 => 'bigserial',
42 );
43  
44 # syscolumns.extended_id of the fixed-length opaque types (coltype 41).
45 46 my %OPAQUE_FOR_EXTENDED_ID = (
46 5 => 'boolean',
47 10 => 'blob',
48 11 => 'clob',
49 );
50  
51 # DATETIME qualifier codes, as the nibbles of collength.
52 46 my %DT_QUALIFIER = (
53 0 => 'year', 2 => 'month', 4 => 'day', 6 => 'hour', 8 => 'minute',
54 10 => 'second', 11 => 'fraction(1)', 12 => 'fraction(2)',
55 13 => 'fraction(3)', 14 => 'fraction(4)', 15 => 'fraction(5)',
56 );
57  
58 # Logical type -> fixed (unsized) Informix SQL type string.
59 46 my %DDL_FIXED = (
60 integer => 'INTEGER',
61 int => 'INTEGER',
62 tinyint => 'SMALLINT',
63 bool => 'BOOLEAN',
64 double => 'DOUBLE PRECISION',
65 float => 'DOUBLE PRECISION',
66 timestamp => 'DATETIME YEAR TO FRACTION(5)',
67 datetime => 'DATETIME YEAR TO FRACTION(5)',
68 blob => 'BYTE',
69 binary => 'BYTE',
70 varbinary => 'BYTE',
71 mediumtext => 'TEXT',
72 longtext => 'TEXT',
73 clob => 'TEXT',
74 bigint => 'BIGINT',
75 smallint => 'SMALLINT',
76 'double precision' => 'DOUBLE PRECISION',
77 real => 'REAL',
78 date => 'DATE',
79 text => 'TEXT',
80 bytea => 'BYTE',
81 );
82  
83 # Logical type -> sized Informix SQL type. Rendered as TYPE(size), except
84 # decimal/numeric which take a [precision, scale] arrayref and render as
85 # TYPE(precision,scale).
86 46 my %DDL_SIZED = (
87 varchar => 'VARCHAR',
88 'character varying' => 'VARCHAR',
89 character => 'CHAR',
90 string => 'VARCHAR',
91 nvarchar => 'VARCHAR',
92 char => 'CHAR',
93 nchar => 'CHAR',
94 lvarchar => 'LVARCHAR',
95 decimal => 'DECIMAL',
96 money => 'MONEY',
97 numeric => 'NUMERIC',
98 );
99  
100  
101 45 ▶sub logical_for_coltype {
Subroutine logical_for_coltype: called CC=10 SCAR=23.1
Pod documented
102 45 my ($coltype, $collength, $extended_id) = @_;
103  
104 45 ▶ if (($coltype == 10 || $coltype == 14) && defined $collength) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coltype == 10 || $coltype == 14 and defined $collength) 7 38 45
Condition100%6/6
$coltype == 10 || $coltype == 14 and defined $collength
!ll&&!rl&&r
3717
$coltype == 10 or $coltype == 14
l!l&&r!l&&!r
4437
MC/DC100%3/3
$coltype == 10 || $coltype == 14 and defined $collength
$coltype == 10 $coltype == 14 defined $collength
Truth table80%4/5
A or B and C
A= $coltype == 10B= $coltype == 14C= defined $collength
ABCresult
00X0
1X00
0100
1X11
0111
105 7 my $q = qualifier_for_coltype($coltype, $collength);
106 7 ▶ return ($coltype == 10 ? 'datetime ' : 'interval ') . lc $q if defined $q;
Branch: 2 branches
BranchTrueFalseTotal
if defined $q 7 0 7
$coltype == 10 ? : 4 3 7
107 }
108 38 ▶ if ($coltype == 41) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coltype == 41) 4 34 38
109 4 ▶ return $OPAQUE_FOR_EXTENDED_ID{ $extended_id // 0 } // 'unknown';
Condition75%3/4
$OPAQUE_FOR_EXTENDED_ID{$extended_id // 0} // "unknown"
l!l
31
$extended_id // 0
l!l
40
MC/DC100%1/1
$OPAQUE_FOR_EXTENDED_ID{$extended_id // 0} // "unknown"
$OPAQUE_FOR_EXTENDED_ID{$extended_id // 0}
MC/DC0%0/1
$extended_id // 0
$extended_id
Truth table100%2/2
A
A= $OPAQUE_FOR_EXTENDED_ID{$extended_id // 0}
Aresult
11
00
Truth table50%1/2
A
A= $extended_id
Aresult
11
00
110 }
111 34 ▶ return $LOGICAL_FOR_COLTYPE{$coltype} // 'unknown';
Condition100%2/2
$LOGICAL_FOR_COLTYPE{$coltype} // "unknown"
l!l
331
MC/DC100%1/1
$LOGICAL_FOR_COLTYPE{$coltype} // "unknown"
$LOGICAL_FOR_COLTYPE{$coltype}
Truth table100%2/2
A
A= $LOGICAL_FOR_COLTYPE{$coltype}
Aresult
11
00
112 }
113  
114  
115  
116 30 ▶sub qualifier_for_coltype {
Subroutine qualifier_for_coltype: called CC=12 SCAR=26.1
Pod documented
117 30 my ($coltype, $collength) = @_;
118 30 ▶ return undef unless defined $collength && ($coltype == 10 || $coltype == 14);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $collength and $coltype == 10 || $coltype == 14 16 14 30
Condition66%4/6
defined $collength and $coltype == 10 || $coltype == 14
!ll&&!rl&&r
16014
$coltype == 10 or $coltype == 14
l!l&&r!l&&!r
950
MC/DC33%1/3
defined $collength and $coltype == 10 || $coltype == 14
defined $collength $coltype == 10 $coltype == 14
Truth table75%3/4
A and B or C
A= defined $collengthB= $coltype == 10C= $coltype == 14
ABCresult
0XX0
1000
11X1
1011
119  
120 14 my ($fc, $lc) = (($collength >> 4) & 15, $collength & 15);
121 14 my ($first, $last) = @DT_QUALIFIER{ $fc, $lc };
122 14 ▶ return undef unless defined $first && defined $last;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and defined $last 0 14 14
Condition33%1/3
defined $first and defined $last
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $first and defined $last
defined $first defined $last
Truth table33%1/3
A and B
A= defined $firstB= defined $last
ABresult
0X0
100
111
123  
124 14 ▶ if ($coltype == 14) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coltype == 14) 5 9 14
125 5 my $digits = $collength >> 8;
126 # the digits of the fields after the first, from the one after $fc to $lc
127 5 my $rest = 0;
128 5 ▶ for my $code (sort { $a <=> $b } grep { $_ > $fc && $_ <= $lc } keys %DT_QUALIFIER) {
Condition100%3/3
$_ > $fc && $_ <= $lc
!ll&&!rl&&r
12394
MC/DC100%2/2
$_ > $fc && $_ <= $lc
$_ > $fc $_ <= $lc
Truth table100%3/3
A && B
A= $_ > $fcB= $_ <= $lc
ABresult
0X0
100
111
129 4 ▶ $rest += $code >= 11 ? $code - 10 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$code >= 11 ? : 0 4 4
130 }
131 5 my $lead = $digits - $rest;
132 5 ▶ $first =~ s/\A(\w+)/$1($lead)/ if $lead > 0 && $fc < 11;
Branch: 1 branch
BranchTrueFalseTotal
if $lead > 0 and $fc < 11 5 0 5
Condition33%1/3
$lead > 0 and $fc < 11
!ll&&!rl&&r
005
MC/DC0%0/2
$lead > 0 and $fc < 11
$lead > 0 $fc < 11
Truth table33%1/3
A and B
A= $lead > 0B= $fc < 11
ABresult
0X0
100
111
133 }
134 14 return uc "$first TO $last";
135 }
136  
137  
138 20 ▶sub size_for_coltype {
Subroutine size_for_coltype: called CC=10 SCAR=23.0
Pod documented
139 20 my ($coltype, $collength) = @_;
140 20 ▶ return undef unless defined $collength;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $collength 0 20 20
141  
142 20 ▶ return $collength & 255 if $coltype == 13 || $coltype == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $coltype == 13 or $coltype == 16 4 16 20
Condition100%3/3
$coltype == 13 or $coltype == 16
l!l&&r!l&&!r
3116
MC/DC100%2/2
$coltype == 13 or $coltype == 16
$coltype == 13 $coltype == 16
Truth table100%3/3
A or B
A= $coltype == 13B= $coltype == 16
ABresult
1X1
011
000
143 16 ▶ return $collength if $coltype == 0 || $coltype == 15 || $coltype == 40;
Branch: 1 branch
BranchTrueFalseTotal
if $coltype == 0 or $coltype == 15 or $coltype == 40 3 13 16
Condition100%6/6
$coltype == 0 or $coltype == 15 or $coltype == 40
l!l&&r!l&&!r
2113
$coltype == 0 or $coltype == 15
l!l&&r!l&&!r
1114
MC/DC100%3/3
$coltype == 0 or $coltype == 15 or $coltype == 40
$coltype == 0 $coltype == 15 $coltype == 40
Truth table100%4/4
A or B or C
A= $coltype == 0B= $coltype == 15C= $coltype == 40
ABCresult
1XX1
01X1
0011
0000
144 13 ▶ if ($coltype == 5 || $coltype == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coltype == 5 or $coltype == 8) 3 10 13
Condition100%3/3
$coltype == 5 or $coltype == 8
l!l&&r!l&&!r
2110
MC/DC100%2/2
$coltype == 5 or $coltype == 8
$coltype == 5 $coltype == 8
Truth table100%3/3
A or B
A= $coltype == 5B= $coltype == 8
ABresult
1X1
011
000
145 3 my ($p, $s) = ($collength >> 8, $collength & 255);
146 3 ▶ return $s == 255 ? [ $p ] : [ $p, $s ];
Branch: 1 branch
BranchTrueFalseTotal
$s == 255 ? : 1 2 3
147 }
148 10 return undef;
149 }
150  
151  
152 302 ▶sub ddl_type {
Subroutine ddl_type: called CC=16 SCAR=27.7
Pod documented
153 302 my ($logical, $size) = @_;
154 302 ▶ my $lc = lc($logical // 'varchar');
Condition100%2/2
$logical // "varchar"
l!l
3011
MC/DC100%1/1
$logical // "varchar"
$logical
Truth table100%2/2
A
A= $logical
Aresult
11
00
155  
156 # string without a size is a TEXT; a VARCHAR holds 255 characters at most,
157 # a longer one is an LVARCHAR (up to 32739)
158 302 ▶ return 'TEXT' if $lc eq 'string' && !defined $size;
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "string" and not defined $size 1 301 302
Condition100%3/3
$lc eq "string" and not defined $size
!ll&&!rl&&r
30011
MC/DC100%2/2
$lc eq "string" and not defined $size
$lc eq "string" not defined $size
Truth table100%3/3
A and not(B)
A= $lc eq "string"B= not defined $size
ABresult
0X0
100
111
159 301 ▶ if (($lc eq 'varchar' || $lc eq 'nvarchar' || $lc eq 'string' || $lc eq 'character varying')
Branch: 1 branch
BranchTrueFalseTotal
if ($lc eq "varchar" || $lc eq "nvarchar" || $lc eq "string" || $lc eq "character varying" and defined $size and not ref $size and $size > 255) 1 300 301
Condition94%17/18
$lc eq "varchar" || $lc eq "nvarchar" || $lc eq "string" || $lc eq "character varying" and defined $size and not ref $size and $size > 255
!ll&&!rl&&r
272281
$lc eq "varchar" || $lc eq "nvarchar" || $lc eq "string" || $lc eq "character varying" and defined $size and not ref $size
!ll&&!rl&&r
272029
$lc eq "varchar" || $lc eq "nvarchar" || $lc eq "string" || $lc eq "character varying" and defined $size
!ll&&!rl&&r
270229
$lc eq "varchar" or $lc eq "nvarchar" or $lc eq "string" or $lc eq "character varying"
l!l&&r!l&&!r
301270
$lc eq "varchar" or $lc eq "nvarchar" or $lc eq "string"
l!l&&r!l&&!r
291271
$lc eq "varchar" or $lc eq "nvarchar"
l!l&&r!l&&!r
281272
MC/DC42%3/7
$lc eq "varchar" || $lc eq "nvarchar" || $lc eq "string" || $lc eq "character varying" and defined $size and not ref $size and $size > 255
$lc eq "varchar" $lc eq "nvarchar" $lc eq "string" $lc eq "character varying" defined $size not ref $size $size > 255
Truth table41%7/17
A or B or C or D and E and not(F) and G
A= $lc eq "varchar"B= $lc eq "nvarchar"C= $lc eq "string"D= $lc eq "character varying"E= defined $sizeF= not ref $sizeG= $size > 255
ABCDEFGresult
0000XXX0
1XXX0XX0
01XX0XX0
001X0XX0
00010XX0
1XXX10X0
01XX10X0
001X10X0
000110X0
1XXX1100
01XX1100
001X1100
00011100
1XXX1111
01XX1111
001X1111
00011111
160 && defined $size && !ref $size && $size > 255) {
161 1 return sprintf 'LVARCHAR(%d)', $size;
162 }
163  
164 300 ▶ if (exists $DDL_SIZED{$lc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DDL_SIZED{$lc}) 43 257 300
165 43 ▶ return sprintf '%s(%s)', $DDL_SIZED{$lc}, join ',', @$size
Branch: 1 branch
BranchTrueFalseTotal
if ref $size eq "ARRAY" 6 37 43
166 if ref $size eq 'ARRAY';
167 37 ▶ return sprintf '%s(%d)', $DDL_SIZED{$lc}, ($size // 255);
Condition100%2/2
$size // 255
l!l
343
MC/DC100%1/1
$size // 255
$size
Truth table100%2/2
A
A= $size
Aresult
11
00
168 }
169  
170 257 ▶ return $DDL_FIXED{$lc} if exists $DDL_FIXED{$lc};
Branch: 1 branch
BranchTrueFalseTotal
if exists $DDL_FIXED{$lc} 255 2 257
171  
172 2 ▶ return $logical // 'VARCHAR';
Condition50%1/2
$logical // "VARCHAR"
l!l
20
MC/DC0%0/1
$logical // "VARCHAR"
$logical
Truth table50%1/2
A
A= $logical
Aresult
11
00
173 }
174  
175  
176 6 ▶sub lob_kind {
Subroutine lob_kind: called CC=6 SCAR=20.9
Pod documented
177 6 my ($data_type, $size) = @_;
178 6 ▶ return undef unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 6 6
179 6 my $lc = lc $data_type;
180 6 ▶ return 'text' if $lc =~ /\A(?:text|clob|mediumtext|longtext)\z/ or ($lc eq 'string' and !defined $size);
Branch: 1 branch
BranchTrueFalseTotal
if $lc =~ /\A(?:text|clob|mediumtext|longtext)\z/ or $lc eq "string" and not defined $size 0 6 6
Condition33%2/6
$lc =~ /\A(?:text|clob|mediumtext|longtext)\z/ or $lc eq "string" and not defined $size
l!l&&r!l&&!r
006
$lc eq "string" and not defined $size
!ll&&!rl&&r
600
MC/DC0%0/3
$lc =~ /\A(?:text|clob|mediumtext|longtext)\z/ or $lc eq "string" and not defined $size
$lc =~ /\A(?:text|clob|mediumtext|longtext)\z/ $lc eq "string" not defined $size
Truth table25%1/4
A or B and not(C)
A= $lc =~ /\A(?:text|clob|mediumtext|longtext)\z/B= $lc eq "string"C= not defined $size
ABCresult
1XX1
0111
00X0
0100
181 6 ▶ return 'binary' if $lc =~ /\A(?:blob|bytea|binary|varbinary|byte)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $lc =~ /\A(?:blob|bytea|binary|varbinary|byte)\z/ 0 6 6
182 6 return undef;
183 }
184  
185  
186 0 ▶sub is_numeric_type {
Subroutine is_numeric_type: not called CC=3 SCAR=24.8
Pod documented
187 0 my ($data_type) = @_;
188 0 ▶ return 0 unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 0 0
189 0 ▶ return lc($data_type) =~ /\A(?:integer|int|smallint|tinyint|bigint|int8|serial|serial8|bigserial|decimal|numeric|money|float|double|double precision|real|smallfloat)\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lc($data_type) =~ /\A(?:integer|int|smallint|tinyint|bigint|int8|serial|serial8|bigserial|decimal|numeric|money|float|double|double precision|real|smallfloat)\z/ ? : 0 0 0
190 }
191  
192 46 1;
193  
194 __END__
« Informix/Introspect/Tables.pm ↑ Summary Informix/Diff/Table.pm »