| 1 | package DBIO::DB2::Type; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: DB2 column type utilities | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 52 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 52 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 52 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 52 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 52 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 52 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 52 | our @EXPORT_OK = qw(_db2_column_type _db2_mapped_type); | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # Types Db2 writes without a length: INTEGER(4), DATE(4) or XML(0) is a syntax | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # error (SQL0104N), whatever size came with the column. A size does come with | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # every one of them from the catalog -- SYSCAT.COLUMNS.LENGTH is the storage | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # length (SMALLINT 2, INTEGER 4, BIGINT 8, REAL 4, DOUBLE 8, DATE 4, TIME 3, | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # XML 0) and DBIO::DB2::Introspect::Columns carries it as the column's size -- | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # and one can come from a result class written for another engine | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # (data_type => 'integer', size => 11). Keyed by the mapped name: the catalog's | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # DOUBLE arrives here as DOUBLE PRECISION; the catalog's BOOLEAN carries a LENGTH | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # of 1 and BOOLEAN(1) is SQL0104N. Measured on Db2 12.1.5 (karr k41). | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 52 | my %NO_LENGTH = map { $_ => 1 } ( | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 'SMALLINT', 'INTEGER', 'BIGINT', 'REAL', 'DOUBLE PRECISION', | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 'DATE', 'TIME', 'XML', 'BOOLEAN', | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Types Db2 refuses without a length (SQL0604N), keyed by the mapped name: | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # varchar and json arrive as VARCHAR. No length is guessed for them (karr k45); | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # text without a length is a CLOB (karr k57). CHAR (= CHAR(1)), BINARY (= BINARY(1)), BLOB and CLOB are valid bare and not listed. | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 52 | my %NEEDS_LENGTH = map { $_ => 1 } ('VARCHAR', 'VARBINARY'); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # Byte limits of the binary types, measured on Db2 12.1.5 (karr k55): BINARY(n) takes | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # n <= 255, VARBINARY(n) n <= 32672; above, SQL0604N. A binary above the first is | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # written as the VARBINARY it can only be; above the second there is nothing to map to. | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 52 | my $BINARY_MAX = 255; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 52 | my $VARBINARY_MAX = 32672; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Db2 has no INTERVAL, ENUM or BIT type (SQL0204N / SQL0104N); there is nothing | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # to map them to (karr k57: enum and bit were written as they came and left to | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # the server to refuse). | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 52 | my %UNSUPPORTED = map { $_ => 1 } ('INTERVAL', 'ENUM', 'BIT'); | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 52 | my %type_map = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | tinyint => 'SMALLINT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | smallint => 'SMALLINT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | int => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | integer => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | bigint => 'BIGINT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | serial => 'INTEGER', | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | bigserial => 'BIGINT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | real => 'REAL', | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | float => 'FLOAT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | double => 'DOUBLE PRECISION', | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 'double precision' => 'DOUBLE PRECISION', | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | numeric => 'DECIMAL', | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | decimal => 'DECIMAL', | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | decfloat => 'DECFLOAT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | text => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | varchar => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | char => 'CHAR', | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | clob => 'CLOB', | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | boolean => 'BOOLEAN', | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | bool => 'BOOLEAN', | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | blob => 'BLOB', | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | binary => 'BINARY', | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | varbinary => 'VARBINARY', | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | date => 'DATE', | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | time => 'TIME', | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | datetime => 'TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | timestamp => 'TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | timestamptz => 'TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 'timestamp with time zone' => 'TIMESTAMP', | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | interval => 'INTERVAL', | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | uuid => 'CHAR(16)', | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | json => 'VARCHAR', | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # The name a data_type maps to, with no size and no refusal. For callers that | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # only classify a type (the character-type test of DBIO::DB2::DDL). | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 26 | ▶ | sub _db2_mapped_type { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_db2_mapped_type: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 26 | my ($type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 26 | ▶ | $type //= 'VARCHAR'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$type //= "VARCHAR"
MC/DC0%0/1
$type //= "VARCHAR"
$type
Truth table50%1/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 26 | ▶ | return $type if $type =~ /\(.+\)$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 25 | ▶ | return $type_map{ lc $type } // uc $type; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$type_map{lc $type} // uc $type
MC/DC100%1/1
$type_map{lc $type} // uc $type
$type_map{lc $type}
Truth table100%2/2
A
A= $type_map{lc $type}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # $column (optional, third argument) only names the column in a refusal. | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 531 | ▶ | sub _db2_column_type { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_db2_column_type: called CC=23 SCAR=31.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 531 | my ($info, $size, $column) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Support both hashref form (DDL.pm) and ($type, $size) form (Diff modules) | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 531 | ▶ | if (ref $info eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 94 | $size = $info->{size}; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 94 | $info = $info->{data_type}; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 531 | ▶ | my $type = $info // 'VARCHAR'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info // "VARCHAR"
MC/DC0%0/1
$info // "VARCHAR"
$info
Truth table50%1/2
A
A= $info
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 531 | ▶ | return $type if $type =~ /\(.+\)$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # A size of more than one part comes as an array reference (the core form: | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # numeric(10,2) is size => [10, 2]) and is written with a comma. | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 531 | ▶ | $size = join ',', @$size if ref $size eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 531 | ▶ | my $mapped = $type_map{ lc $type } // uc $type; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$type_map{lc $type} // uc $type
MC/DC100%1/1
$type_map{lc $type} // uc $type
$type_map{lc $type}
Truth table100%2/2
A
A= $type_map{lc $type}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # text without a size is the type that holds a string of any length, a CLOB | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # (karr k57, maintainer decision); nothing is guessed, there is no bound to | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # guess. With a size it stays a VARCHAR(n) below. | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 531 | ▶ | return 'CLOB' if lc($type) eq 'text' && !(defined $size && length $size); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
lc $type eq "text" and not defined $size && length $size
defined $size && length $size
MC/DC100%3/3
lc $type eq "text" and not defined $size && length $size
lc $type eq "text"
defined $size
length $size
Truth table100%4/4
A and not(B && C)
A= lc $type eq "text"B= defined $sizeC= length $size
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 527 | ▶ | my $where = defined $column ? " (column $column)" : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # A refusal is a DBIO::Exception (the convention of DBIO::Test::Conformance): | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # the driver's decision, thrown before the server is asked. | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | DBIO::Exception->throw("Db2 has no $type type$where; it is refused, there is nothing to map it to") | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 527 | ▶ | if $UNSUPPORTED{$mapped}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | DBIO::Exception->throw("Type $type$where needs a size: Db2 refuses $mapped without a length (SQL0604N) and none is guessed") | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 521 | ▶ | if $NEEDS_LENGTH{$mapped} && !(defined $size && length $size); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$NEEDS_LENGTH{$mapped} and not defined $size && length $size
defined $size && length $size
MC/DC100%3/3
$NEEDS_LENGTH{$mapped} and not defined $size && length $size
$NEEDS_LENGTH{$mapped}
defined $size
length $size
Truth table100%4/4
A and not(B && C)
A= $NEEDS_LENGTH{$mapped}B= defined $sizeC= length $size
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 508 | ▶ | return $mapped unless defined $size && length $size; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $size and length $size
MC/DC100%2/2
defined $size and length $size
defined $size
length $size
Truth table100%3/3
A and B
A= defined $sizeB= length $size
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 424 | ▶ | return $mapped if $NO_LENGTH{$mapped}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 221 | ▶ | if (($mapped eq 'BINARY' || $mapped eq 'VARBINARY') && $size =~ /\A\d+\z/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$mapped eq "BINARY" || $mapped eq "VARBINARY" and $size =~ /\A\d+\z/
$mapped eq "BINARY" or $mapped eq "VARBINARY"
MC/DC66%2/3
$mapped eq "BINARY" || $mapped eq "VARBINARY" and $size =~ /\A\d+\z/
$mapped eq "BINARY"
$mapped eq "VARBINARY"
$size =~ /\A\d+\z/
Truth table60%3/5
A or B and C
A= $mapped eq "BINARY"B= $mapped eq "VARBINARY"C= $size =~ /\A\d+\z/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 23 | ▶ | Carp::croak("Type $type$where of size $size is beyond what Db2 holds: VARBINARY takes at most $VARBINARY_MAX bytes " | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | . "(SQL0604N); use a blob for more") | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | if $size > $VARBINARY_MAX; | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 18 | ▶ | return "VARBINARY($size)" if $mapped eq 'BINARY' && $size > $BINARY_MAX; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mapped eq "BINARY" and $size > $BINARY_MAX
MC/DC100%2/2
$mapped eq "BINARY" and $size > $BINARY_MAX
$mapped eq "BINARY"
$size > $BINARY_MAX
Truth table100%3/3
A and B
A= $mapped eq "BINARY"B= $size > $BINARY_MAX
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 213 | return "$mapped($size)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 52 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||