Sybase/Introspect.pm

filter: Statementstmt 100% 72 / 72 Branchbran 75.0% 6 / 8 Conditioncond 53.8% 7 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 87.8% 101 / 115 CC 15 SCAR 13.7
CC
15
Cov
91%
_mark_pk_columns
7.0
view_definition
5.5
_group_fks_by_constraint
4.0
CRAP 3.9
« Sybase/Storage/ASE/TxnManager.pm ↑ Summary Sybase/Introspect/Tables.pm »
1 package DBIO::Sybase::Introspect;
2 # ABSTRACT: Introspect a Sybase ASE database via its system catalog
3  
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 our $VERSION = '0.900007';
8  
9 28 ▶use base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 28 ▶use DBIO::Sybase::Introspect::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use DBIO::Sybase::Introspect::Columns;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use DBIO::Sybase::Introspect::Indexes;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 28 ▶use DBIO::Sybase::Introspect::ForeignKeys;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 28 ▶use DBIO::Sybase::Introspect::Views;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 17 ▶sub schema { $_[0]->{schema} // 'dbo' }
Subroutine schema: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$_[0]{"schema"} // "dbo"
l!l
017
MC/DC0%0/1
$_[0]{"schema"} // "dbo"
$_[0]{"schema"}
Truth table50%1/2
A
A= $_[0]{"schema"}
Aresult
11
00
19  
20  
21 14 ▶sub _build_model {
Subroutine _build_model: called CC=1 SCAR=0.0
22 14 my ($self) = @_;
23 14 my $dbh = $self->dbh;
24 14 my $schema = $self->schema;
25  
26 14 my $tables = DBIO::Sybase::Introspect::Tables->fetch($dbh, $schema);
27 14 my $columns = DBIO::Sybase::Introspect::Columns->fetch($dbh, $schema, $tables);
28 14 my $indexes = DBIO::Sybase::Introspect::Indexes->fetch($dbh, $schema, $tables);
29 14 my $fks_raw = DBIO::Sybase::Introspect::ForeignKeys->fetch($dbh, $schema, $tables);
30 14 my $views = DBIO::Sybase::Introspect::Views->fetch($dbh, $schema, $tables);
31  
32 # Group the per-column FK rows into one entry per constraint, in declared
33 # order (composite FKs must keep local<->remote column pairing). The
34 # order-preserving grouping lives in DBIO::Introspect::Base.
35 14 my $foreign_keys = $self->_group_fks_by_constraint($fks_raw);
36  
37 # The primary key is the index Indexes marked origin => 'pk' (sysindexes
38 # status & 2048); its key order is the pk_position.
39 14 $self->_mark_pk_columns($indexes, $columns);
40  
41 return {
42 14 tables => $tables,
43 columns => $columns,
44 indexes => $indexes,
45 foreign_keys => $foreign_keys,
46 views => $views,
47 };
48 }
49  
50 14 ▶sub _group_fks_by_constraint {
Subroutine _group_fks_by_constraint: called CC=4 SCAR=13.9
51 14 my ($self, $fks_raw) = @_;
52 14 my $out = {};
53 14 for my $table (sort keys %$fks_raw) {
54 14 ▶ my $rows = $fks_raw->{$table} // [];
Condition50%1/2
$fks_raw->{$table} // []
l!l
140
MC/DC0%0/1
$fks_raw->{$table} // []
$fks_raw->{$table}
Truth table50%1/2
A
A= $fks_raw->{$table}
Aresult
11
00
55 14 my $ordered = $self->_aggregate_by_ordered($rows, 'constraint_name');
56 14 my @entries;
57 14 for my $pair (@$ordered) {
58 14 my ($cn, $rows) = @$pair;
59 push @entries, {
60 constraint_name => $cn,
61 14 from_columns => [ map { $_->{column_name} } @$rows ],
62 to_table => $rows->[0]{ref_table_name},
63 14 to_columns => [ map { $_->{ref_column_name} } @$rows ],
64 on_update => $rows->[0]{update_rule},
65 on_delete => $rows->[0]{delete_rule},
66 14 };
67 }
68 14 $out->{$table} = \@entries;
69 }
70 14 return $out;
71 }
72  
73 14 ▶sub _mark_pk_columns {
Subroutine _mark_pk_columns: called CC=7 SCAR=19.5
74 14 my ($self, $indexes, $columns) = @_;
75 14 for my $table (sort keys %$indexes) {
76 28 ▶ my ($pk) = grep { ($_->{origin} // '') eq 'pk' } values %{ $indexes->{$table} };
Condition100%2/2
$_->{"origin"} // ""
l!l
2828
MC/DC100%1/1
$_->{"origin"} // ""
$_->{"origin"}
Truth table100%2/2
A
A= $_->{"origin"}
Aresult
11
00
77 28 ▶ next unless $pk;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk 0 28 28
78 28 my %pos;
79 28 @pos{ @{ $pk->{columns} } } = (1 .. @{ $pk->{columns} });
80 28 ▶ for my $col (@{ $columns->{$table} // [] }) {
Condition50%1/2
$columns->{$table} // []
l!l
280
MC/DC0%0/1
$columns->{$table} // []
$columns->{$table}
Truth table50%1/2
A
A= $columns->{$table}
Aresult
11
00
81 140 ▶ my $p = $pos{ $col->{column_name} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $p = $pos{$col->{"column_name"}} or next 42 98 140
82 42 $col->{is_pk} = 1;
83 42 $col->{pk_position} = $p;
84 }
85 }
86 }
87  
88  
89 8 ▶sub view_definition {
Subroutine view_definition: called CC=5 SCAR=17.0
Pod documented
90 8 my ($self, $key) = @_;
91 8 ▶ return undef unless $self->table_is_view($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->table_is_view($key) 5 3 8
92  
93 3 my $chunks = $self->dbh->selectcol_arrayref(q{
94 SELECT m.text
95 FROM syscomments m
96 JOIN sysobjects o ON o.id = m.id
97 JOIN sysusers u ON u.uid = o.uid
98 WHERE u.name = ? AND o.name = ? AND o.type = 'V'
99 ORDER BY m.colid2, m.colid
100 }, undef, $self->schema, $key);
101 3 ▶ return undef unless $chunks && @$chunks;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunks and @$chunks 0 3 3
Condition33%1/3
$chunks and @$chunks
!ll&&!rl&&r
003
MC/DC0%0/2
$chunks and @$chunks
$chunks @$chunks
Truth table33%1/3
A and B
A= $chunksB= @$chunks
ABresult
0X0
100
111
102 return DBIO::Sybase::Introspect::Views::strip_head(
103 3 ▶ join '', map { $_ // '' } @$chunks);
Condition50%1/2
$_ // ""
l!l
60
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
104 }
105  
106  
107 28 1;
108  
109 __END__
« Sybase/Storage/ASE/TxnManager.pm ↑ Summary Sybase/Introspect/Tables.pm »