| 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
Condition50%2/4
$spec{"type"} // ""
$spec{"type"} // "VARCHAR(255)"
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"}
Truth table50%1/2
A
A= $spec{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not $serial and defined $spec{"size"} and not $type =~ /\(/
not $serial and defined $spec{"size"}
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 =~ /\(/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $serial and defined $spec{"default_value"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 12 | ▶ | my $base = lc($type // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$type // ""
MC/DC0%0/1
$type // ""
$type
Truth table50%1/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | return 'TODAY' if $e =~ /\Acurrent_date\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | return 'USER' if $e =~ /\A(?:current_user|session_user)\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | return $sql; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||||||||||||||||