Sybase/Introspect/Columns.pm

filter: Statementstmt 100% 56 / 56 Branchbran 82.3% 28 / 34 Conditioncond 74.0% 20 / 27 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 85.7% 120 / 140 CC 33 SCAR 22.6
CC
33
Cov
89%
_size
14.7
fetch
8.1
_mark_defaults
7.1
CRAP 9.5
« Sybase/Diff/ForeignKey.pm ↑ Summary Sybase/Storage/ASE.pm »
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"
l!l
170
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table50%1/2
A
A= $schema
Aresult
11
00
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
BranchTrueFalseTotal
unless exists $$tables{$row->{"table_name"}} 0 165 165
33 165 ▶ my $status = $row->{status} // 0;
Condition50%1/2
$row->{"status"} // 0
l!l
1650
MC/DC0%0/1
$row->{"status"} // 0
$row->{"status"}
Truth table50%1/2
A
A= $row->{"status"}
Aresult
11
00
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
BranchTrueFalseTotal
$status & 8 ? : 79 86 165
$status & 128 ? : 14 151 165
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
BranchTrueFalseTotal
if defined $size 66 99 165
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"} // ""
l!l
1650
MC/DC0%0/1
$row->{"type_name"} // ""
$row->{"type_name"}
Truth table50%1/2
A
A= $row->{"type_name"}
Aresult
11
00
57 165 ▶ if ($type eq 'numeric' || $type eq 'decimal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "numeric" or $type eq "decimal") 15 150 165
Condition100%3/3
$type eq "numeric" or $type eq "decimal"
l!l&&r!l&&!r
141150
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"
ABresult
1X1
011
000
58 15 ▶ return defined $row->{prec} ? [ $row->{prec}, $row->{scale} // 0 ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{'prec'} ? : 15 0 15
Condition50%1/2
$row->{"scale"} // 0
l!l
150
MC/DC0%0/1
$row->{"scale"} // 0
$row->{"scale"}
Truth table50%1/2
A
A= $row->{"scale"}
Aresult
11
00
59 }
60 150 ▶ if ($type =~ /char\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /char\z/) 48 102 150
61 48 ▶ return undef unless defined $row->{length};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{"length"} 0 48 48
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
BranchTrueFalseTotal
if $type =~ /\Auni/ 15 33 48
68 33 ▶ return int($row->{length} / ($row->{ncharsize} || 1)) if $type =~ /\An/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\An/ 3 30 33
Condition50%1/2
$row->{'ncharsize'} || 1
l!l
30
MC/DC0%0/1
$row->{'ncharsize'} || 1
$row->{'ncharsize'}
Truth table50%1/2
A
A= $row->{'ncharsize'}
Aresult
11
00
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
BranchTrueFalseTotal
if $type eq "binary" || $type eq "varbinary" and defined $row->{"length"} 3 99 102
Condition83%5/6
$type eq "binary" || $type eq "varbinary" and defined $row->{"length"}
!ll&&!rl&&r
9903
$type eq "binary" or $type eq "varbinary"
l!l&&r!l&&!r
1299
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"}
ABCresult
00X0
1X00
0100
1X11
0111
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"} // ""
l!l
230
MC/DC0%0/1
$row->{"text"} // ""
$row->{"text"}
Truth table50%1/2
A
A= $row->{"text"}
Aresult
11
00
96 }
97  
98 17 for my $table (keys %text) {
99 14 ▶ my $list = $columns->{$table} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $columns->{$table} or next 14 0 14
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
BranchTrueFalseTotal
if defined $t 20 92 112
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
BranchTrueFalseTotal
$text =~ s/\A\s*DEFAULT\s+//i or $text =~ s/\A\s*create\s+default\s+\S+\s+as\s+//i 20 0 20
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
BranchTrueFalseTotal
unless length $text 0 20 20
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
BranchTrueFalseTotal
if $numeric and DBIO::Sybase::DDL::_is_numeric_literal($text) 8 12 20
Condition100%3/3
$numeric and DBIO::Sybase::DDL::_is_numeric_literal($text)
!ll&&!rl&&r
1118
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)
ABresult
0X0
100
111
122 12 ▶ if (!$numeric && $text =~ /\A'((?:[^']|'')*)'\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $numeric and $text =~ /\A'((?:[^']|'')*)'\z/s) 7 5 12
Condition100%3/3
not $numeric and $text =~ /\A'((?:[^']|'')*)'\z/s
!ll&&!rl&&r
147
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
ABresult
0X0
100
111
123 7 (my $lit = $1) =~ s/''/'/g;
124 7 return $lit;
125 }
126 5 return \$text;
127 }
128  
129 29 1;
130  
131 __END__
« Sybase/Diff/ForeignKey.pm ↑ Summary Sybase/Storage/ASE.pm »