| 1 | package DBIO::MSSQL::Introspect; | ||||||||||||||||||||
| 2 | # ABSTRACT: Introspect a Microsoft SQL Server database via information_schema | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 27 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 27 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 27 | our $VERSION = '0.900007'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 27 | ▶ | use base 'DBIO::Introspect::Base'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 27 | ▶ | use DBIO::MSSQL::Introspect::Tables; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 27 | ▶ | use DBIO::MSSQL::Introspect::Columns; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 27 | ▶ | use DBIO::MSSQL::Introspect::Indexes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 27 | ▶ | use DBIO::MSSQL::Introspect::ForeignKeys; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 27 | ▶ | use DBIO::MSSQL::Introspect::Views; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | ▶ | sub schema { $_[0]->{schema} // 'dbo' } | ||||||||||||||||||
|
Subroutine
schema: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$_[0]{"schema"} // "dbo"
MC/DC0%0/1
$_[0]{"schema"} // "dbo"
$_[0]{"schema"}
Truth table0%0/2
A
A= $_[0]{"schema"}
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | sub schemas { $_[0]->{schemas} // [] } | ||||||||||||||||||
|
Subroutine
schemas: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$_[0]{"schemas"} // []
MC/DC0%0/1
$_[0]{"schemas"} // []
$_[0]{"schemas"}
Truth table0%0/2
A
A= $_[0]{"schemas"}
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | ▶ | sub _build_model { | ||||||||||||||||||
|
Subroutine
_build_model: not called CC=13 SCAR=52.0
| |||||||||||||||||||||
| 24 | 0 | my ($self) = @_; | |||||||||||||||||||
| 25 | 0 | my $dbh = $self->dbh; | |||||||||||||||||||
| 26 | 0 | my $base = $self->schema; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | my %model = (tables => {}, columns => {}, indexes => {}, foreign_keys => {}, schemas => {}); | |||||||||||||||||||
| 29 | 0 | my %seen; | |||||||||||||||||||
| 30 | 0 | for my $schema (grep { !$seen{$_}++ } $base, @{ $self->schemas }) { | |||||||||||||||||||
| 31 | 0 | ▶ | my $key = $schema eq $base ? sub { $_[0] } : sub { "$schema.$_[0]" }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | my ($exists) = $dbh->selectrow_array( | |||||||||||||||||||
| 34 | 'SELECT COUNT(*) FROM sys.schemas WHERE name = ?', undef, $schema); | ||||||||||||||||||||
| 35 | 0 | ▶ | next unless $exists; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | $model{schemas}{$schema} = { schema_name => $schema }; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | my $tables = DBIO::MSSQL::Introspect::Tables->fetch($dbh, $schema); | |||||||||||||||||||
| 39 | 0 | my $columns = DBIO::MSSQL::Introspect::Columns->fetch($dbh, $schema, $tables); | |||||||||||||||||||
| 40 | 0 | my $indexes = DBIO::MSSQL::Introspect::Indexes->fetch($dbh, $schema, $tables); | |||||||||||||||||||
| 41 | 0 | my $fks = DBIO::MSSQL::Introspect::ForeignKeys->fetch($dbh, $schema, $tables); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # a view carries its stored text (karr k82) | ||||||||||||||||||||
| 44 | 0 | my $defs = DBIO::MSSQL::Introspect::Views->fetch($dbh, $schema); | |||||||||||||||||||
| 45 | 0 | for my $name (grep { $tables->{$_}{kind} eq 'view' } keys %$defs) { | |||||||||||||||||||
| 46 | 0 | $tables->{$name}{definition} = $defs->{$name}; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | # the views a view reads, as model keys (dbo bare, the others schema.name) | ||||||||||||||||||||
| 49 | 0 | my $deps = DBIO::MSSQL::Introspect::Views->dependencies($dbh, $schema); | |||||||||||||||||||
| 50 | 0 | ▶ | for my $name (grep { $tables->{$_} && $tables->{$_}{kind} eq 'view' } keys %$deps) { | ||||||||||||||||||
|
Condition0%0/3
$tables->{$_} && $tables->{$_}{'kind'} eq 'view'
MC/DC0%0/2
$tables->{$_} && $tables->{$_}{'kind'} eq 'view'
$tables->{$_}
$tables->{$_}{'kind'} eq 'view'
Truth table0%0/3
A && B
A= $tables->{$_}B= $tables->{$_}{'kind'} eq 'view'
| |||||||||||||||||||||
| 51 | $tables->{$name}{depends_on} = [ map { | ||||||||||||||||||||
| 52 | 0 | ▶ | $_->[0] eq $base ? $_->[1] : "$_->[0].$_->[1]" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 0 | } @{ $deps->{$name} } ]; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | for my $t (keys %$tables) { | |||||||||||||||||||
| 57 | 0 | $model{tables}{ $key->($t) } = { %{ $tables->{$t} }, table_name => $key->($t) }; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 0 | $model{columns}{ $key->($_) } = $columns->{$_} for keys %$columns; | |||||||||||||||||||
| 60 | 0 | $model{indexes}{ $key->($_) } = $indexes->{$_} for keys %$indexes; | |||||||||||||||||||
| 61 | 0 | for my $t (keys %$fks) { | |||||||||||||||||||
| 62 | $model{foreign_keys}{ $key->($t) } = [ map { | ||||||||||||||||||||
| 63 | +{ %$_, | ||||||||||||||||||||
| 64 | from_table => $key->($_->{from_table}), | ||||||||||||||||||||
| 65 | 0 | ▶ | to_table => ($_->{to_schema} eq $base ? $_->{to_table} : "$_->{to_schema}.$_->{to_table}") } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | } @{ $fks->{$t} } ]; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | $model{collation} = $self->_fetch_collation($dbh); | |||||||||||||||||||
| 71 | 0 | return \%model; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 4 | ▶ | sub _fetch_collation { | ||||||||||||||||||
|
Subroutine
_fetch_collation: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 75 | 4 | my ($self, $dbh) = @_; | |||||||||||||||||||
| 76 | 4 | my ($name, $style) = $dbh->selectrow_array(q{ | |||||||||||||||||||
| 77 | SELECT CAST(DATABASEPROPERTYEX(DB_NAME(), 'Collation') AS nvarchar(128)), | ||||||||||||||||||||
| 78 | CAST(COLLATIONPROPERTY( | ||||||||||||||||||||
| 79 | CAST(DATABASEPROPERTYEX(DB_NAME(), 'Collation') AS sysname), | ||||||||||||||||||||
| 80 | 'ComparisonStyle') AS int) | ||||||||||||||||||||
| 81 | }); | ||||||||||||||||||||
| 82 | 4 | ▶ | return { name => $name, case_sensitive => (($style // 0) & 1) ? 0 : 1 }; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$style // 0
MC/DC0%0/1
$style // 0
$style
Truth table50%1/2
A
A= $style
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 27 | 1; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||