| 1 | package DBIO::Informix::Introspect::Columns; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect Informix columns | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 22 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 22 | ▶ | use DBIO::Informix::Types; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use DBIO::Informix::Introspect::Util (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | my %columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | my %colnos; | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | my $q = DBIO::Informix::Introspect::Util::qualifier($schema); | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | for my $table_name (sort keys %$tables) { | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | my $sth = $dbh->prepare(qq{ | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | SELECT c.colname, c.colno, c.coltype, c.collength, c.extended_id, | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | d.type as deflt_type, d.default as deflt | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | FROM $q.syscolumns c | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | JOIN $q.systables t | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ON c.tabid = t.tabid | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | LEFT JOIN $q.sysdefaults d | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ON t.tabid = d.tabid AND c.colno = d.colno | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | WHERE t.tabname = ? | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ORDER BY c.colno | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | $sth->execute($table_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | my @cols; | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | my $type = $row->{coltype} % 256; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 10 | $colnos{$table_name}{ $row->{colno} } = $row->{colname}; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 10 | my $data_type = _map_type($type, $row->{collength}, $row->{extended_id}); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | my %col = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | column_name => $row->{colname}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | data_type => $data_type, | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | not_null => ($row->{coltype} & 256) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 10 | ▶ | default_value => _map_default($type, $row->{deflt_type}, $row->{deflt}, $row->{collength}), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
$type == 6 or $type == 18 or $type == 53
$type == 6 or $type == 18
MC/DC100%3/3
$type == 6 or $type == 18 or $type == 53
$type == 6
$type == 18
$type == 53
Truth table100%4/4
A or B or C
A= $type == 6B= $type == 18C= $type == 53
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | is_pk => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | pk_position => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | is_auto_increment => ($type == 6 || $type == 18 || $type == 53) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | my $size = DBIO::Informix::Types::size_for_coltype($type, $row->{collength}); | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 10 | ▶ | $col{size} = $size if defined $size; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | push @cols, \%col; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | $columns{$table_name} = \@cols; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # Fetch primary key info | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | $class->_fetch_pk_info($dbh, $q, \%columns, \%colnos); | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | return \%columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 45 | ▶ | sub _map_type { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_map_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 45 | my ($type, $collength, $extended_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 45 | return DBIO::Informix::Types::logical_for_coltype($type, $collength, $extended_id); | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # sysdefaults.type: L literal, U USER, S DBSERVERNAME, N NULL, T TODAY, | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # C CURRENT. A literal of a type other than a character one is stored as | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # "<encoded value> <text>" ('AAM 3' for SMALLINT 3); the text is the part | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # after the first blank. Character literals and BOOLEAN are stored as is. | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 22 | my %LITERAL_AS_IS = map { $_ => 1 } (0, 13, 15, 16, 40, 41); | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # A DATETIME or INTERVAL literal is written with the qualifier of its column | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # (DATETIME(2026-01-01) YEAR TO DAY), as the server takes it back; so is CURRENT | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # (the qualifier of the column, it is not stored in sysdefaults). Without | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # a collength the qualifier is the one the server gives a bare CURRENT. | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 34 | ▶ | sub _map_default { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_map_default: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 34 | my ($type, $deflt_type, $deflt, $collength) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 34 | ▶ | return undef unless $deflt_type; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 23 | my $q = DBIO::Informix::Types::qualifier_for_coltype($type, $collength); | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 23 | ▶ | if ($deflt_type eq 'C') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | my $cq = $q // 'YEAR TO FRACTION(5)'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$q // "YEAR TO FRACTION(5)"
MC/DC100%1/1
$q // "YEAR TO FRACTION(5)"
$q
Truth table100%2/2
A
A= $q
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | return \"CURRENT $cq"; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | ▶ | if ($deflt_type eq 'T') { return \'today' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 18 | ▶ | if ($deflt_type eq 'U') { return \'user' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 17 | ▶ | if ($deflt_type eq 'S') { return \'dbservername' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 16 | ▶ | return undef if $deflt_type eq 'N'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 15 | ▶ | return $deflt unless defined $deflt; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 15 | $deflt =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 15 | ▶ | $deflt =~ s/\A\S+\s+// unless $LITERAL_AS_IS{$type}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 15 | ▶ | if (defined $q && ($type == 10 || $type == 14)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $q and $type == 10 || $type == 14
$type == 10 or $type == 14
MC/DC33%1/3
defined $q and $type == 10 || $type == 14
defined $q
$type == 10
$type == 14
Truth table75%3/4
A and B or C
A= defined $qB= $type == 10C= $type == 14
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | return \sprintf '%s(%s) %s', ($type == 10 ? 'DATETIME' : 'INTERVAL'), $deflt, $q; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 11 | return $deflt; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | sub _fetch_pk_info { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_pk_info: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | my ($class, $dbh, $q, $columns, $colnos) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my $parts = DBIO::Informix::Introspect::Util::_parts_sql(); | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | for my $table (sort keys %$columns) { | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | my $sth = $dbh->prepare(qq{ | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | SELECT $parts | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | FROM $q.sysconstraints c | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | JOIN $q.systables t ON t.tabid = c.tabid | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | JOIN $q.sysindexes i ON i.tabid = c.tabid AND i.idxname = c.idxname | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | WHERE c.constrtype = 'P' | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | AND t.tabname = ? | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | $sth->execute($table); | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my $row = $sth->fetchrow_hashref; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | next unless $row; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | my %pos; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | for my $colno (DBIO::Informix::Introspect::Util::part_columns($row, 'part')) { | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | my $name = $colnos->{$table}{$colno}; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | $pos{$name} = ++$pos if defined $name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | for my $col (@{ $columns->{$table} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | ▶ | if (my $p = $pos{ $col->{column_name} }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | $col->{is_pk} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | $col->{pk_position} = $p; | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 22 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||