Informix/ColumnDef.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 20 / 20 Conditioncond 73.3% 11 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 91.5% 87 / 95 CC 17 SCAR 18.9
CC
17
Cov
95%
column_def_sql
11.1
_default_fragment
4.0
_expression_default
4.0
CRAP 6.6
« Informix/Diff/Index.pm ↑ Summary Informix/Introspect.pm »
1 package DBIO::Informix::ColumnDef;
2 # ABSTRACT: Render a single Informix column definition fragment
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 our $VERSION = '0.900007';
8  
9 34 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 our @EXPORT_OK = qw(column_def_sql);
11  
12 34 ▶use DBIO::Informix::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use DBIO::Informix::Types qw(ddl_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # Character types whose plain-scalar default must be quoted.
17 # BOOLEAN ('t') and DATE ('01/02/2020') literals are quoted too: unquoted they
18 # are syntax errors (measured, Informix 15.0.1, karr k20).
19 34 my %CHAR_TYPE = map { $_ => 1 } qw(char varchar nchar nvarchar lvarchar text boolean date);
20  
21 333 ▶sub column_def_sql {
Subroutine column_def_sql: called CC=11 SCAR=24.1
Pod documented
22 333 my (%spec) = @_;
23  
24 # Informix SERIAL is itself a type -- never combine it with a base type,
25 # and a serial column carries no DEFAULT.
26 # SERIAL8 and BIGSERIAL are such types as well and stay what they are.
27 333 my $serial = $spec{is_auto_increment};
28 my $type = $serial
29 ? (lc($spec{type} // '') =~ /\A(serial8|bigserial)\z/ ? uc $1 : 'SERIAL')
30 333 ▶ : ($spec{type} // 'VARCHAR(255)');
Branch: 2 branches
BranchTrueFalseTotal
$serial ? : 7 326 333
lc($spec{'type'} // '') =~ /\A(serial8|bigserial)\z/ ? : 2 5 7
Condition50%2/4
$spec{"type"} // ""
l!l
70
$spec{"type"} // "VARCHAR(255)"
l!l
3260
MC/DC0%0/1
$spec{"type"} // ""
$spec{"type"}
MC/DC0%0/1
$spec{"type"} // "VARCHAR(255)"
$spec{"type"}
Truth table50%1/2
A
A= $spec{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $spec{"type"}
Aresult
11
00
31  
32 # an introspected column carries its size apart from the type name; a bare
33 # VARCHAR is VARCHAR(1) and a bare DECIMAL a floating DECIMAL(16)
34 $type = ddl_type($type, $spec{size})
35 333 ▶ if !$serial && defined $spec{size} && $type !~ /\(/;
Branch: 1 branch
BranchTrueFalseTotal
if not $serial and defined $spec{"size"} and not $type =~ /\(/ 4 329 333
Condition83%5/6
not $serial and defined $spec{"size"} and not $type =~ /\(/
!ll&&!rl&&r
32904
not $serial and defined $spec{"size"}
!ll&&!rl&&r
73224
MC/DC66%2/3
not $serial and defined $spec{"size"} and not $type =~ /\(/
not $serial defined $spec{"size"} not $type =~ /\(/
Truth table75%3/4
not(A) and B and not(C)
A= not $serialB= defined $spec{"size"}C= not $type =~ /\(/
ABCresult
0XX0
10X0
1100
1111
36  
37 333 my $def = sprintf '%s %s', _quote_ident($spec{name}), $type;
38  
39 333 ▶ if (!$serial && defined $spec{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $serial and defined $spec{"default_value"}) 19 314 333
Condition100%3/3
not $serial and defined $spec{"default_value"}
!ll&&!rl&&r
730719
MC/DC100%2/2
not $serial and defined $spec{"default_value"}
not $serial defined $spec{"default_value"}
Truth table100%3/3
not(A) and B
A= not $serialB= defined $spec{"default_value"}
ABresult
0X0
100
111
40 19 $def .= ' DEFAULT ' . _default_fragment($spec{type}, $spec{default_value});
41 }
42  
43 333 ▶ $def .= ' NOT NULL' if $spec{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $spec{"not_null"} 270 63 333
44  
45 333 return $def;
46 }
47  
48 # Render a default value: SCALAR ref => literal SQL; character-typed plain
49 # scalar => quoted; everything else => raw.
50 19 ▶sub _default_fragment {
Subroutine _default_fragment: called CC=4 SCAR=13.9
51 19 my ($type, $dv) = @_;
52 19 ▶ return _expression_default($$dv) if ref $dv eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $dv eq "SCALAR" 7 12 19
53  
54 12 ▶ my $base = lc($type // '');
Condition50%1/2
$type // ""
l!l
120
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
55 12 $base =~ s/\(.*//; # varchar(255) -> varchar
56 12 $base =~ s/\s.*//; # datetime year to second -> datetime
57 12 ▶ return $dv unless $CHAR_TYPE{$base};
Branch: 1 branch
BranchTrueFalseTotal
unless $CHAR_TYPE{$base} 6 6 12
58 6 (my $quoted = $dv) =~ s/'/''/g;
59 6 return "'$quoted'";
60 }
61  
62 # The expression defaults of the ANSI spelling, in the words Informix takes
63 # (CURRENT_TIMESTAMP is a syntax error there). Anything else is the user's
64 # SQL and stays as written.
65 7 ▶sub _expression_default {
Subroutine _expression_default: called CC=4 SCAR=13.9
66 7 my ($sql) = @_;
67 7 (my $e = $sql) =~ s/\A\s+|\s+\z//g;
68 7 ▶ return 'CURRENT YEAR TO FRACTION(5)' if $e =~ /\A(?:current_timestamp|now\(\)|localtimestamp)(?:\(\d?\))?\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /\A(?:current_timestamp|now\(\)|localtimestamp)(?:\(\d?\))?\z/i 2 5 7
69 5 ▶ return 'TODAY' if $e =~ /\Acurrent_date\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /\Acurrent_date\z/i 1 4 5
70 4 ▶ return 'USER' if $e =~ /\A(?:current_user|session_user)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /\A(?:current_user|session_user)\z/i 1 3 4
71 3 return $sql;
72 }
73  
74 34 1;
75  
76 __END__
« Informix/Diff/Index.pm ↑ Summary Informix/Introspect.pm »