| 1 | package DBIO::MSSQL::Introspect::Indexes; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect MSSQL indexes | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0 | ▶ | sub fetch { | ||||||||||||||||||
|
Subroutine
fetch: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 10 | 0 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||
| 11 | 0 | ▶ | $schema //= 'dbo'; | ||||||||||||||||||
|
Condition0%0/2
$schema //= "dbo"
MC/DC0%0/1
$schema //= "dbo"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||
| 12 | 0 | my %indexes; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | # Get indexes that are not primary keys or unique constraints (those are table-level) | ||||||||||||||||||||
| 15 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||
| 16 | SELECT i.name AS index_name, t.name AS table_name, | ||||||||||||||||||||
| 17 | i.is_unique, i.type AS index_type, | ||||||||||||||||||||
| 18 | i.type_desc AS index_kind, | ||||||||||||||||||||
| 19 | c.name AS column_name, ic.key_ordinal | ||||||||||||||||||||
| 20 | FROM sys.indexes i | ||||||||||||||||||||
| 21 | JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id | ||||||||||||||||||||
| 22 | JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id | ||||||||||||||||||||
| 23 | JOIN sys.tables t ON i.object_id = t.object_id | ||||||||||||||||||||
| 24 | JOIN sys.schemas s ON t.schema_id = s.schema_id | ||||||||||||||||||||
| 25 | WHERE s.name = ? | ||||||||||||||||||||
| 26 | AND i.is_primary_key = 0 | ||||||||||||||||||||
| 27 | AND i.type > 0 -- exclude heaps | ||||||||||||||||||||
| 28 | ORDER BY t.name, i.name, ic.key_ordinal | ||||||||||||||||||||
| 29 | }); | ||||||||||||||||||||
| 30 | 0 | $sth->execute($schema); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | my %seen; | |||||||||||||||||||
| 33 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 0 | ▶ | next unless exists $tables->{ $row->{table_name} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | my $idx_key = "$row->{table_name}:$row->{index_name}"; | |||||||||||||||||||
| 36 | 0 | ▶ | if (!$seen{$idx_key}++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | $indexes{ $row->{table_name} }{ $row->{index_name} } = { | ||||||||||||||||||||
| 38 | index_name => $row->{index_name}, | ||||||||||||||||||||
| 39 | columns => [], | ||||||||||||||||||||
| 40 | is_unique => $row->{is_unique} ? 1 : 0, | ||||||||||||||||||||
| 41 | 0 | ▶ | kind => lc($row->{index_kind} // 'nonclustered'), | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"index_kind"} // "nonclustered"
MC/DC0%0/1
$row->{"index_kind"} // "nonclustered"
$row->{"index_kind"}
Truth table0%0/2
A
A= $row->{"index_kind"}
| |||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | 0 | push @{ $indexes{ $row->{table_name} }{ $row->{index_name} }{columns} }, $row->{column_name}; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | return \%indexes; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | 1; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | __END__ | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =pod | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =encoding UTF-8 | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | DBIO::MSSQL::Introspect::Indexes - Introspect MSSQL indexes | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head1 VERSION | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | version 0.900001 | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Fetches index metadata via C<INFORMATION_SCHEMA.STATISTICS> and | ||||||||||||||||||||
| 69 | C<sys.indexes> (filtered to exclude PK and unique constraint indexes | ||||||||||||||||||||
| 70 | that are managed at table level). | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head1 METHODS | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head2 fetch | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | my $indexes = DBIO::MSSQL::Introspect::Indexes->fetch($dbh, $schema, $tables); | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | Given the tables hashref from L<DBIO::MSSQL::Introspect::Tables>, | ||||||||||||||||||||
| 79 | returns a hashref keyed by table name. Each value is a hashref of | ||||||||||||||||||||
| 80 | index hashrefs keyed by index name with keys: C<index_name>, | ||||||||||||||||||||
| 81 | C<columns> (arrayref), C<is_unique>, C<kind> (C<clustered>, C<nonclustered>, etc). | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head1 AUTHOR | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | DBIO & DBIx::Class Authors | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||
| 90 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||
| 91 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 94 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||