MSSQL/Introspect/Indexes.pm

filter: Statementstmt 100% 22 / 22 Branchbran 83.3% 10 / 12 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 3 / 3 Podpod 100% 1 / 1 totaltotal 86.3% 38 / 44 CC 9 SCAR 20.4
CC
9
Cov
89%
fetch
9.2
BEGIN
1.0
BEGIN
1.0
CRAP 7.7
« MSSQL/Diff/Index.pm ↑ Summary MSSQL/Diff/Schema.pm »
1 package DBIO::MSSQL::Introspect::Indexes;
2 # ABSTRACT: Introspect MSSQL indexes
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  
10  
11 2 ▶sub fetch {
Subroutine fetch: called CC=9 SCAR=22.2
Pod documented
12 2 my ($class, $dbh, $schema, $tables) = @_;
13 2 ▶ $schema //= 'dbo';
Condition50%1/2
$schema //= "dbo"
l!l
20
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table50%1/2
A
A= $schema
Aresult
11
00
14 2 my %indexes;
15  
16 # Every column is aliased explicitly and in lower case: an alias is echoed
17 # into the result metadata verbatim, which is the one way to be sure of the
18 # hash keys below without depending on the handle's FetchHashKeyName.
19 #
20 # Every index but the one of the primary key. The one of a unique
21 # constraint is kept and flagged: it is a constraint to ADD and DROP.
22 2 my $sth = $dbh->prepare(q{
23 SELECT i.name AS index_name, t.name AS table_name,
24 i.is_unique AS is_unique,
25 i.is_unique_constraint AS is_unique_constraint, i.type AS index_type,
26 i.type_desc AS index_kind,
27 i.has_filter AS has_filter, i.filter_definition AS filter_definition,
28 c.name AS column_name, ic.key_ordinal AS key_ordinal
29 FROM sys.indexes i
30 JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
31 JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
32 JOIN sys.tables t ON i.object_id = t.object_id
33 JOIN sys.schemas s ON t.schema_id = s.schema_id
34 WHERE s.name = ?
35 AND i.is_primary_key = 0
36 AND i.type > 0 -- exclude heaps
37 ORDER BY t.name, i.name, ic.key_ordinal
38 });
39 2 $sth->execute($schema);
40  
41 2 my %seen;
42 2 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 4 2 6
43 4 ▶ next unless exists $tables->{ $row->{table_name} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tables{$row->{"table_name"}} 0 4 4
44 4 my $idx_key = "$row->{table_name}:$row->{index_name}";
45 4 ▶ if (!$seen{$idx_key}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$idx_key}++) 4 0 4
46 $indexes{ $row->{table_name} }{ $row->{index_name} } = {
47 index_name => $row->{index_name},
48 columns => [],
49 is_unique => $row->{is_unique} ? 1 : 0,
50 is_unique_constraint => $row->{is_unique_constraint} ? 1 : 0,
51 kind => lc($row->{index_kind} // 'nonclustered'),
52 filter => $row->{has_filter} ? $row->{filter_definition} : undef,
53 4 ▶ };
Branch: 3 branches
BranchTrueFalseTotal
$row->{'is_unique'} ? : 1 3 4
$row->{'is_unique_constraint'} ? : 1 3 4
$row->{'has_filter'} ? : 2 2 4
Condition50%1/2
$row->{"index_kind"} // "nonclustered"
l!l
40
MC/DC0%0/1
$row->{"index_kind"} // "nonclustered"
$row->{"index_kind"}
Truth table50%1/2
A
A= $row->{"index_kind"}
Aresult
11
00
54 }
55 4 push @{ $indexes{ $row->{table_name} }{ $row->{index_name} }{columns} }, $row->{column_name};
56 }
57  
58 2 return \%indexes;
59 }
60  
61 29 1;
62  
63 __END__
« MSSQL/Diff/Index.pm ↑ Summary MSSQL/Diff/Schema.pm »