| 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"
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table50%1/2
A
A= $schema
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 4 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 4 | my $idx_key = "$row->{table_name}:$row->{index_name}"; | |||||||||||||||||||||||||||
| 45 | 4 | ▶ | if (!$seen{$idx_key}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition50%1/2
$row->{"index_kind"} // "nonclustered"
MC/DC0%0/1
$row->{"index_kind"} // "nonclustered"
$row->{"index_kind"}
Truth table50%1/2
A
A= $row->{"index_kind"}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||