| 1 | package DBIO::Oracle::Introspect; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect an Oracle database via all_* views | ||||||||||||||||||||||||||||
| 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::Oracle::Introspect::Tables (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 27 | ▶ | use DBIO::Oracle::Introspect::Columns (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 27 | ▶ | use DBIO::Oracle::Introspect::Indexes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 27 | ▶ | use DBIO::Oracle::Introspect::ForeignKeys (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 27 | ▶ | use DBIO::Oracle::Introspect::Keys (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 27 | ▶ | use DBIO::Oracle::Introspect::Views (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | sub schema { $_[0]->{schema} //= $_[0]->_default_schema } | ||||||||||||||||||||||||||
|
Subroutine
schema: called CC=2 SCAR=9.2
Pod
documented
Condition33%1/3
$_[0]{"schema"} //= $_[0]->_default_schema
MC/DC0%0/2
$_[0]{"schema"} //= $_[0]->_default_schema
$_[0]{"schema"}
$_[0]->_default_schema
Truth table33%1/3
A //= B
A= $_[0]{"schema"}B= $_[0]->_default_schema
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub schemas { $_[0]->{schemas} //= [] } | ||||||||||||||||||||||||||
|
Subroutine
schemas: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
$_[0]{"schemas"} //= []
MC/DC100%1/1
$_[0]{"schemas"} //= []
$_[0]{"schemas"}
Truth table100%2/2
A
A= $_[0]{"schemas"}
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _default_schema { | ||||||||||||||||||||||||||
|
Subroutine
_default_schema: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 27 | 0 | my ($schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL'); | |||||||||||||||||||||||||||
| 28 | 0 | return $schema; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub _build_model { | ||||||||||||||||||||||||||
|
Subroutine
_build_model: called CC=16 SCAR=32.4
| |||||||||||||||||||||||||||||
| 32 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 33 | 2 | my $own = $self->schema; | |||||||||||||||||||||||||||
| 34 | 2 | my $model = $self->_schema_model($own); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # The tables of the other schemas are named schema.table (karr k58); every | ||||||||||||||||||||||||||||
| 37 | # section of the model keeps one key per table. | ||||||||||||||||||||||||||||
| 38 | 2 | my %seen = ($own => 1); | |||||||||||||||||||||||||||
| 39 | 2 | for my $other (grep { !$seen{$_}++ } @{ $self->schemas }) { | |||||||||||||||||||||||||||
| 40 | 1 | my $m = $self->_schema_model($other); | |||||||||||||||||||||||||||
| 41 | 1 | for my $section (qw(tables columns indexes foreign_keys primary_keys unique_constraints views)) { | |||||||||||||||||||||||||||
| 42 | $model->{$section}{"$other.$_"} = $m->{$section}{$_} | ||||||||||||||||||||||||||||
| 43 | 7 | ▶ | for keys %{ $m->{$section} || {} }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$m->{$section} || {}
MC/DC0%0/1
$m->{$section} || {}
$m->{$section}
Truth table50%1/2
A
A= $m->{$section}
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | # likewise a view names the views it rests on (karr k65) | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | for my $key (keys %{ $m->{views} || {} }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$m->{'views'} || {}
MC/DC0%0/1
$m->{'views'} || {}
$m->{'views'}
Truth table50%1/2
A
A= $m->{'views'}
| |||||||||||||||||||||||||||||
| 47 | 0 | my $v = $model->{views}{"$other.$key"}; | |||||||||||||||||||||||||||
| 48 | $_ = $_ =~ /\A\Q$own\E\.(.+)\z/s ? $1 : /\./ ? $_ : "$other.$_" | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | for @{ $v->{depends_on} || [] }; | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$v->{'depends_on'} || []
MC/DC0%0/1
$v->{'depends_on'} || []
$v->{'depends_on'}
Truth table0%0/2
A
A= $v->{'depends_on'}
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 1 | for my $key (keys %{ $m->{tables} }) { | |||||||||||||||||||||||||||
| 52 | 1 | ▶ | for my $fk (@{ $model->{foreign_keys}{"$other.$key"} || [] }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$model->{'foreign_keys'}{"$other.$key"} || []
MC/DC0%0/1
$model->{'foreign_keys'}{"$other.$key"} || []
$model->{'foreign_keys'}{"$other.$key"}
Truth table50%1/2
A
A= $model->{'foreign_keys'}{"$other.$key"}
| |||||||||||||||||||||||||||||
| 53 | 2 | $fk->{from_table} = "$other.$key"; | |||||||||||||||||||||||||||
| 54 | # ForeignKeys names a table of the schema it reads bare and one of | ||||||||||||||||||||||||||||
| 55 | # another schema as owner.table. Here the key of a table of $other | ||||||||||||||||||||||||||||
| 56 | # is other.table, and the key of one in the connected user's schema | ||||||||||||||||||||||||||||
| 57 | # is its bare name. | ||||||||||||||||||||||||||||
| 58 | $fk->{to_table} = $fk->{to_table} =~ /\A\Q$own\E\.(.+)\z/s ? $1 | ||||||||||||||||||||||||||||
| 59 | : $fk->{to_table} =~ /\./ ? $fk->{to_table} | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | : "$other.$fk->{to_table}"; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 2 | return $model; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # The model of one schema, keyed by the bare table names. | ||||||||||||||||||||||||||||
| 68 | 3 | ▶ | sub _schema_model { | ||||||||||||||||||||||||||
|
Subroutine
_schema_model: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 69 | 3 | my ($self, $schema) = @_; | |||||||||||||||||||||||||||
| 70 | 3 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | my $tables = DBIO::Oracle::Introspect::Tables->fetch($dbh, $schema); | |||||||||||||||||||||||||||
| 73 | 3 | my $columns = DBIO::Oracle::Introspect::Columns->fetch($dbh, $schema, $tables); | |||||||||||||||||||||||||||
| 74 | 3 | my $indexes = DBIO::Oracle::Introspect::Indexes->fetch($dbh, $schema, $tables); | |||||||||||||||||||||||||||
| 75 | 3 | my $fks = DBIO::Oracle::Introspect::ForeignKeys->fetch($dbh, $schema, $tables); | |||||||||||||||||||||||||||
| 76 | 3 | my $keys = DBIO::Oracle::Introspect::Keys->fetch($dbh, $schema, $tables); | |||||||||||||||||||||||||||
| 77 | 3 | my $views = DBIO::Oracle::Introspect::Views->fetch($dbh, $schema, $tables); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Primary key membership is also marked on the columns (is_pk, pk_position), | ||||||||||||||||||||||||||||
| 80 | # the way the other drivers' models carry it and the way | ||||||||||||||||||||||||||||
| 81 | # DBIO::Oracle::Diff::Table reads it for a CREATE TABLE. | ||||||||||||||||||||||||||||
| 82 | 3 | ▶ | for my $table (keys %{ $keys->{primary} || {} }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$keys->{'primary'} || {}
MC/DC0%0/1
$keys->{'primary'} || {}
$keys->{'primary'}
Truth table50%1/2
A
A= $keys->{'primary'}
| |||||||||||||||||||||||||||||
| 83 | 3 | ▶ | my @pk = @{ $keys->{primary}{$table} || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$keys->{'primary'}{$table} || []
MC/DC0%0/1
$keys->{'primary'}{$table} || []
$keys->{'primary'}{$table}
Truth table50%1/2
A
A= $keys->{'primary'}{$table}
| |||||||||||||||||||||||||||||
| 84 | 3 | my %pos; @pos{@pk} = 1 .. @pk; | |||||||||||||||||||||||||||
| 85 | 3 | ▶ | for my $col (@{ $columns->{$table} || [] }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$columns->{$table} || []
MC/DC0%0/1
$columns->{$table} || []
$columns->{$table}
Truth table50%1/2
A
A= $columns->{$table}
| |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | next unless exists $pos{ $col->{column_name} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 3 | $col->{is_pk} = 1; | |||||||||||||||||||||||||||
| 88 | 3 | $col->{pk_position} = $pos{ $col->{column_name} }; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | return { | ||||||||||||||||||||||||||||
| 93 | tables => $tables, | ||||||||||||||||||||||||||||
| 94 | columns => $columns, | ||||||||||||||||||||||||||||
| 95 | indexes => $indexes, | ||||||||||||||||||||||||||||
| 96 | foreign_keys => $fks, | ||||||||||||||||||||||||||||
| 97 | primary_keys => $keys->{primary}, | ||||||||||||||||||||||||||||
| 98 | unique_constraints => $keys->{unique}, | ||||||||||||||||||||||||||||
| 99 | 3 | views => $views, | |||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 8 | ▶ | sub table_columns_info { | ||||||||||||||||||||||||||
|
Subroutine
table_columns_info: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 8 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 106 | 8 | ▶ | my $cols = $self->model->{columns}{$key} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->model->{"columns"}{$key} // []
MC/DC0%0/1
$self->model->{"columns"}{$key} // []
$self->model->{"columns"}{$key}
Truth table50%1/2
A
A= $self->model->{"columns"}{$key}
| |||||||||||||||||||||||||||||
| 107 | # The primary key is folded into is_nullable, the way core's base now does | ||||||||||||||||||||||||||||
| 108 | # (core k126); membership is read from the primary_keys section via | ||||||||||||||||||||||||||||
| 109 | # table_pk_info. | ||||||||||||||||||||||||||||
| 110 | 8 | my %pk = map { $_ => 1 } @{ $self->table_pk_info($key) }; | |||||||||||||||||||||||||||
| 111 | 8 | my %info; | |||||||||||||||||||||||||||
| 112 | 8 | for my $col (@$cols) { | |||||||||||||||||||||||||||
| 113 | my %col_info = ( | ||||||||||||||||||||||||||||
| 114 | data_type => $col->{data_type}, | ||||||||||||||||||||||||||||
| 115 | 16 | ▶ | is_nullable => ($col->{not_null} || $pk{ $col->{column_name} }) ? 0 : 1, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$col->{"not_null"} or $pk{$col->{"column_name"}}
MC/DC100%2/2
$col->{"not_null"} or $pk{$col->{"column_name"}}
$col->{"not_null"}
$pk{$col->{"column_name"}}
Truth table100%3/3
A or B
A= $col->{"not_null"}B= $pk{$col->{"column_name"}}
| |||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | 16 | ▶ | $col_info{size} = $col->{size} if defined $col->{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 16 | ▶ | $col_info{default_value} = $col->{default_value} if exists $col->{default_value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 16 | ▶ | $col_info{is_auto_increment} = 1 if $col->{is_auto_increment}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 16 | ▶ | $col_info{sequence} = $col->{sequence} if defined $col->{sequence}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 16 | $info{ $col->{column_name} } = \%col_info; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 8 | return \%info; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 23 | ▶ | sub table_pk_info { | ||||||||||||||||||||||||||
|
Subroutine
table_pk_info: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 23 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 129 | 23 | ▶ | return $self->model->{primary_keys}{$key} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->model->{"primary_keys"}{$key} // []
MC/DC0%0/1
$self->model->{"primary_keys"}{$key} // []
$self->model->{"primary_keys"}{$key}
Truth table50%1/2
A
A= $self->model->{"primary_keys"}{$key}
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | ▶ | sub table_fk_info { | ||||||||||||||||||||||||||
|
Subroutine
table_fk_info: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 6 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 135 | 6 | ▶ | my $fks = $self->model->{foreign_keys}{$key} // []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->model->{"foreign_keys"}{$key} // []
MC/DC0%0/1
$self->model->{"foreign_keys"}{$key} // []
$self->model->{"foreign_keys"}{$key}
Truth table50%1/2
A
A= $self->model->{"foreign_keys"}{$key}
| |||||||||||||||||||||||||||||
| 136 | return [ | ||||||||||||||||||||||||||||
| 137 | map { | ||||||||||||||||||||||||||||
| 138 | 6 | { | |||||||||||||||||||||||||||
| 139 | local_columns => $_->{from_columns}, | ||||||||||||||||||||||||||||
| 140 | remote_table => $_->{to_table}, | ||||||||||||||||||||||||||||
| 141 | remote_schema => undef, | ||||||||||||||||||||||||||||
| 142 | remote_columns => $_->{to_columns}, | ||||||||||||||||||||||||||||
| 143 | attrs => { | ||||||||||||||||||||||||||||
| 144 | on_delete => $_->{on_delete}, | ||||||||||||||||||||||||||||
| 145 | on_update => $_->{on_update}, | ||||||||||||||||||||||||||||
| 146 | 2 | ▶ | is_deferrable => $_->{is_deferrable} ? 1 : 0, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | }, | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } @$fks | ||||||||||||||||||||||||||||
| 150 | ]; | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 6 | ▶ | sub view_definition { | ||||||||||||||||||||||||||
|
Subroutine
view_definition: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 6 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 156 | 6 | ▶ | return undef unless $self->table_is_view($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 2 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||
| 159 | 2 | local $dbh->{LongReadLen} = 1_000_000; | |||||||||||||||||||||||||||
| 160 | 2 | local $dbh->{LongTruncOk} = 0; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | my ($text) = $dbh->selectrow_array( | |||||||||||||||||||||||||||
| 163 | 'SELECT text FROM all_views WHERE owner = ? AND view_name = ?', | ||||||||||||||||||||||||||||
| 164 | undef, $self->schema, $key, | ||||||||||||||||||||||||||||
| 165 | ); | ||||||||||||||||||||||||||||
| 166 | 2 | ▶ | return undef unless defined $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 2 | $text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 168 | 2 | $text =~ s/\s+\z//; | |||||||||||||||||||||||||||
| 169 | 2 | $text =~ s/\s*;\s*\z//; | |||||||||||||||||||||||||||
| 170 | 2 | return $text; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 27 | 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||