| 1 | package DBIO::Schema::Type; | ||||||||||||||||||||
| 2 | # ABSTRACT: portable base-type vocabulary for multi-engine schemas | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 10 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 10 | our $VERSION = '0.900007'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 10 | ▶ | use Carp qw/croak/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 10 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 10 | our @BASE_TYPES = qw/integer text char boolean numeric double blob timestamp/; | |||||||||||||||||||
| 13 | 10 | my %IS_BASE = map { $_ => 1 } @BASE_TYPES; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | our %ALIAS = ( | ||||||||||||||||||||
| 16 | 70 | (map { $_ => 'integer' } qw/int integer bigint smallint tinyint serial bigserial/), | |||||||||||||||||||
| 17 | 60 | (map { $_ => 'text' } qw/text varchar string clob mediumtext longtext/), | |||||||||||||||||||
| 18 | 20 | (map { $_ => 'char' } qw/char character/), | |||||||||||||||||||
| 19 | 20 | (map { $_ => 'boolean' } qw/boolean bool/), | |||||||||||||||||||
| 20 | 20 | (map { $_ => 'numeric' } qw/numeric decimal/), | |||||||||||||||||||
| 21 | 'double' => 'double', 'double precision' => 'double', real => 'double', float => 'double', | ||||||||||||||||||||
| 22 | 40 | (map { $_ => 'blob' } qw/blob bytea binary varbinary/), | |||||||||||||||||||
| 23 | 10 | (map { $_ => 'timestamp' } qw/timestamp datetime timestamptz/), | |||||||||||||||||||
| 24 | ); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 14 | ▶ | sub base_types { @BASE_TYPES } | ||||||||||||||||||
|
Subroutine
base_types: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 23 | ▶ | sub is_base_type { $IS_BASE{ $_[0] // '' } ? 1 : 0 } | ||||||||||||||||||
|
Subroutine
is_base_type: called CC=3 SCAR=11.2
Pod
documented
Branch: 1 branch
Condition50%1/2
$_[0] // ""
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 49 | ▶ | sub normalize { | ||||||||||||||||||
|
Subroutine
normalize: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 34 | 49 | my ($type) = @_; | |||||||||||||||||||
| 35 | 49 | ▶ | croak "undef data_type" unless defined $type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 49 | my $key = lc $type; | |||||||||||||||||||
| 37 | 49 | $key =~ s/\(.*\)\s*$//; # strip parameters: varchar(255) -> varchar | |||||||||||||||||||
| 38 | 49 | $key =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 39 | 49 | $key =~ s/\s+/ /g; | |||||||||||||||||||
| 40 | 49 | ▶ | my $base = $ALIAS{$key} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | or croak "unknown data_type '$type' (not in portable base-type vocabulary)"; | ||||||||||||||||||||
| 42 | 48 | return $base; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 38 | ▶ | sub canonical_column { | ||||||||||||||||||
|
Subroutine
canonical_column: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 47 | 38 | my ($name, $info) = @_; | |||||||||||||||||||
| 48 | 38 | my $base = normalize($info->{data_type}); | |||||||||||||||||||
| 49 | my %col = ( | ||||||||||||||||||||
| 50 | column_name => $name, | ||||||||||||||||||||
| 51 | base_type => $base, | ||||||||||||||||||||
| 52 | not_null => ($info->{is_nullable} ? 0 : 1), | ||||||||||||||||||||
| 53 | default => $info->{default_value}, | ||||||||||||||||||||
| 54 | 38 | ▶ | auto_increment => ($info->{is_auto_increment} ? 1 : 0), | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||
| 56 | 38 | ▶ | if ($base eq 'char') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 57 | 9 | my ($width) = _size_parts($name, $base, $info->{size}); | |||||||||||||||||||
| 58 | 8 | $col{size} = $width; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | elsif ($base eq 'numeric') { | ||||||||||||||||||||
| 61 | 7 | my @parts = _size_parts($name, $base, $info->{size}); | |||||||||||||||||||
| 62 | 6 | ▶ | @col{qw/precision scale/} = @parts[0, 1] if @parts; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | elsif ($base eq 'timestamp') { | ||||||||||||||||||||
| 65 | # defined, not true: 0 is a precision (whole seconds). | ||||||||||||||||||||
| 66 | 13 | my ($fractional) = _size_parts($name, $base, $info->{size}); | |||||||||||||||||||
| 67 | 12 | ▶ | $col{size} = $fractional if defined $fractional; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 35 | return \%col; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # The form of size, read once for every base type that consumes it. | ||||||||||||||||||||
| 73 | # A scalar is the single value the form names (width for char, precision for | ||||||||||||||||||||
| 74 | # numeric, fractional seconds for timestamp); an arrayref names them in order | ||||||||||||||||||||
| 75 | # and anything past the last meaningful element is dropped; an empty arrayref | ||||||||||||||||||||
| 76 | # names nothing and counts as absent. That is the reading the drivers already | ||||||||||||||||||||
| 77 | # established on their own alias names. Any other reference form means nothing | ||||||||||||||||||||
| 78 | # in any of these readings and would reach a driver as a stringified address | ||||||||||||||||||||
| 79 | # -- CHAR(ARRAY(0x...)) -- so it is refused here rather than passed on. Only | ||||||||||||||||||||
| 80 | # the form is checked, never the content. | ||||||||||||||||||||
| 81 | 29 | ▶ | sub _size_parts { | ||||||||||||||||||
|
Subroutine
_size_parts: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 82 | 29 | my ($name, $base, $size) = @_; | |||||||||||||||||||
| 83 | 29 | ▶ | return () unless defined $size; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 25 | my $ref = ref $size; | |||||||||||||||||||
| 85 | 25 | ▶ | return ($size) unless $ref; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 14 | ▶ | return (@$size) if $ref eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 3 | croak "size for column '$name' (base type '$base') must be a scalar or " | |||||||||||||||||||
| 88 | . "an arrayref, got a $ref reference"; | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 10 | 1; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||