| 1 | package DBIO::Sybase::Introspect::Columns; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect Sybase ASE columns | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 29 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 29 | ▶ | use DBIO::Sybase::DDL (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 17 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | ▶ | $schema //= 'dbo'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$schema //= "dbo"
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table50%1/2
A
A= $schema
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 17 | my %columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 17 | my $rows = $dbh->selectall_arrayref(q{ | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | SELECT o.name AS table_name, c.name AS column_name, c.colid, | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | t.name AS type_name, c.length, c.prec, c.scale, | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | c.status, c.cdefault, @@ncharsize AS ncharsize | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | FROM syscolumns c | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | JOIN sysobjects o ON o.id = c.id | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | JOIN sysusers u ON u.uid = o.uid | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | JOIN systypes t ON t.usertype = c.usertype | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | WHERE o.type IN ('U', 'V') | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | AND u.name = ? | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ORDER BY o.name, c.colid | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | }, { Slice => {} }, $schema); | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 17 | for my $row (@$rows) { | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 165 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 165 | ▶ | my $status = $row->{status} // 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"status"} // 0
MC/DC0%0/1
$row->{"status"} // 0
$row->{"status"}
Truth table50%1/2
A
A= $row->{"status"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my $col = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | column_name => $row->{column_name}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | data_type => $row->{type_name}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 165 | ▶ | not_null => ($status & 8) ? 0 : 1, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | default_value => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | is_pk => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | pk_position => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | is_auto_increment => ($status & 128) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 165 | my $size = _size($row); | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 165 | ▶ | $col->{size} = $size if defined $size; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 165 | push @{ $columns{ $row->{table_name} } }, $col; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 17 | _mark_defaults($dbh, $schema, \%columns); | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | return \%columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # DBIO size convention from syscolumns length/prec/scale. | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 165 | ▶ | sub _size { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_size: called CC=14 SCAR=26.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 165 | my ($row) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 165 | ▶ | my $type = lc($row->{type_name} // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"type_name"} // ""
MC/DC0%0/1
$row->{"type_name"} // ""
$row->{"type_name"}
Truth table50%1/2
A
A= $row->{"type_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 165 | ▶ | if ($type eq 'numeric' || $type eq 'decimal') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "numeric" or $type eq "decimal"
MC/DC100%2/2
$type eq "numeric" or $type eq "decimal"
$type eq "numeric"
$type eq "decimal"
Truth table100%3/3
A or B
A= $type eq "numeric"B= $type eq "decimal"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 15 | ▶ | return defined $row->{prec} ? [ $row->{prec}, $row->{scale} // 0 ] : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$row->{"scale"} // 0
MC/DC0%0/1
$row->{"scale"} // 0
$row->{"scale"}
Truth table50%1/2
A
A= $row->{"scale"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 150 | ▶ | if ($type =~ /char\z/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 48 | ▶ | return undef unless defined $row->{length}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # length is bytes, the declared size characters. unichar/univarchar store | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # UTF-16, two bytes each. nchar/nvarchar reserve @@ncharsize bytes per | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # character -- the maximum width of the server's default character set: | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # 1 under iso_1, 3 under utf8 (karr k57) -- so nvarchar(10) is length 30 | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # on a utf8 server and read back there as 30 without the division. | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 48 | ▶ | return int($row->{length} / 2) if $type =~ /\Auni/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 33 | ▶ | return int($row->{length} / ($row->{ncharsize} || 1)) if $type =~ /\An/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$row->{'ncharsize'} || 1
MC/DC0%0/1
$row->{'ncharsize'} || 1
$row->{'ncharsize'}
Truth table50%1/2
A
A= $row->{'ncharsize'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 30 | return $row->{length}; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # binary(n) / varbinary(n): length is the n bytes, no character width (karr k93). | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 102 | ▶ | return $row->{length} if ($type eq 'binary' || $type eq 'varbinary') && defined $row->{length}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$type eq "binary" || $type eq "varbinary" and defined $row->{"length"}
$type eq "binary" or $type eq "varbinary"
MC/DC66%2/3
$type eq "binary" || $type eq "varbinary" and defined $row->{"length"}
$type eq "binary"
$type eq "varbinary"
defined $row->{"length"}
Truth table60%3/5
A or B and C
A= $type eq "binary"B= $type eq "varbinary"C= defined $row->{"length"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 99 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # Default text lives in syscomments under the id syscolumns.cdefault points at, | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # split over several rows when long (colid2/colid order). | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 17 | ▶ | sub _mark_defaults { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mark_defaults: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 17 | my ($dbh, $schema, $columns) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 17 | my $rows = $dbh->selectall_arrayref(q{ | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | SELECT o.name AS table_name, c.name AS column_name, m.text | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | FROM syscolumns c | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | JOIN sysobjects o ON o.id = c.id | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | JOIN sysusers u ON u.uid = o.uid | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | JOIN syscomments m ON m.id = c.cdefault | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | WHERE c.cdefault <> 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | AND o.type = 'U' | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | AND u.name = ? | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ORDER BY o.name, c.colid, m.colid2, m.colid | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | }, { Slice => {} }, $schema); | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 17 | my %text; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 17 | for my $row (@$rows) { | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 23 | ▶ | $text{ $row->{table_name} }{ $row->{column_name} } .= $row->{text} // ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$row->{"text"} // ""
MC/DC0%0/1
$row->{"text"} // ""
$row->{"text"}
Truth table50%1/2
A
A= $row->{"text"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 17 | for my $table (keys %text) { | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 14 | ▶ | my $list = $columns->{$table} or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 14 | for my $col (@$list) { | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 112 | my $t = $text{$table}{ $col->{column_name} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 112 | ▶ | $col->{default_value} = _normalize_default($t, $col->{data_type}) if defined $t; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # Inverse of DBIO::Sybase::DDL::sybase_default_clause, which renders a literal | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # by the column's type (karr k55): quoted on a character column, bare on a | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # numeric one. So the type decides here too. A bare number on a numeric column | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # is the literal it was written as. A QUOTED text on a numeric column is not -- | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # it is what the pre-k55 DDL wrote, a VARCHAR constant that fails every | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # defaulted INSERT -- and stays the SQL it is, so the diff sees it differ from | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # the desired literal and replaces it instead of reading both as '0'. | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 20 | ▶ | sub _normalize_default { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_default: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 20 | my ($text, $data_type) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 20 | ▶ | $text =~ s/\A\s*DEFAULT\s+//i | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | or $text =~ s/\A\s*create\s+default\s+\S+\s+as\s+//i; | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 20 | $text =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | 20 | ▶ | return undef unless length $text; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 20 | my $numeric = DBIO::Sybase::DDL::_is_numeric_type($data_type); | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | 20 | ▶ | return $text if $numeric && DBIO::Sybase::DDL::_is_numeric_literal($text); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$numeric and DBIO::Sybase::DDL::_is_numeric_literal($text)
MC/DC100%2/2
$numeric and DBIO::Sybase::DDL::_is_numeric_literal($text)
$numeric
DBIO::Sybase::DDL::_is_numeric_literal($text)
Truth table100%3/3
A and B
A= $numericB= DBIO::Sybase::DDL::_is_numeric_literal($text)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 12 | ▶ | if (!$numeric && $text =~ /\A'((?:[^']|'')*)'\z/s) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $numeric and $text =~ /\A'((?:[^']|'')*)'\z/s
MC/DC100%2/2
not $numeric and $text =~ /\A'((?:[^']|'')*)'\z/s
not $numeric
$text =~ /\A'((?:[^']|'')*)'\z/s
Truth table100%3/3
not(A) and B
A= not $numericB= $text =~ /\A'((?:[^']|'')*)'\z/s
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | (my $lit = $1) =~ s/''/'/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | return $lit; | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 5 | return \$text; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 29 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||