Oracle/Introspect.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Oracle/Diff.pm ↑ Summary Oracle/Diff/Column.pm »
1 package DBIO::Oracle::Introspect;
2 # ABSTRACT: Introspect an Oracle database via all_* views
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 2 ▶use base 'DBIO::Introspect::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 2 ▶use DBIO::Oracle::Introspect::Tables ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DBIO::Oracle::Introspect::Columns ();
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use DBIO::Oracle::Introspect::Indexes ();
13 use DBIO::Oracle::Introspect::ForeignKeys ();
14 use DBIO::Oracle::Introspect::Keys ();
15  
16  
17 sub schema { $_[0]->{schema} //= $_[0]->_default_schema }
18  
19 sub _default_schema {
20 my ($self) = @_;
21 my ($schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL');
22 return $schema;
23 }
24  
25 sub _build_model {
26 my ($self) = @_;
27 my $dbh = $self->dbh;
28 my $schema = $self->schema;
29  
30 my $tables = DBIO::Oracle::Introspect::Tables->fetch($dbh, $schema);
31 my $columns = DBIO::Oracle::Introspect::Columns->fetch($dbh, $schema, $tables);
32 my $indexes = DBIO::Oracle::Introspect::Indexes->fetch($dbh, $schema, $tables);
33 my $fks = DBIO::Oracle::Introspect::ForeignKeys->fetch($dbh, $schema, $tables);
34 my $keys = DBIO::Oracle::Introspect::Keys->fetch($dbh, $schema, $tables);
35  
36 return {
37 tables => $tables,
38 columns => $columns,
39 indexes => $indexes,
40 foreign_keys => $fks,
41 primary_keys => $keys->{primary},
42 unique_constraints => $keys->{unique},
43 };
44 }
45  
46  
47 sub table_columns_info {
48 my ($self, $key) = @_;
49 my $cols = $self->model->{columns}{$key} // [];
50 my %info;
51 for my $col (@$cols) {
52 my %col_info = (
53 data_type => $col->{data_type},
54 is_nullable => $col->{not_null} ? 0 : 1,
55 );
56 $col_info{size} = $col->{size} if defined $col->{size};
57 $col_info{default_value} = $col->{default_value} if exists $col->{default_value};
58 $col_info{is_auto_increment} = 1 if $col->{is_auto_increment};
59 $col_info{sequence} = $col->{sequence} if defined $col->{sequence};
60 $info{ $col->{column_name} } = \%col_info;
61 }
62 return \%info;
63 }
64  
65  
66 sub table_pk_info {
67 my ($self, $key) = @_;
68 return $self->model->{primary_keys}{$key} // [];
69 }
70  
71  
72 sub table_fk_info {
73 my ($self, $key) = @_;
74 my $fks = $self->model->{foreign_keys}{$key} // [];
75 return [
76 map {
77 {
78 local_columns => $_->{from_columns},
79 remote_table => $_->{to_table},
80 remote_schema => undef,
81 remote_columns => $_->{to_columns},
82 attrs => {
83 on_delete => $_->{on_delete},
84 on_update => $_->{on_update},
85 is_deferrable => $_->{is_deferrable} ? 1 : 0,
86 },
87 }
88 } @$fks
89 ];
90 }
91  
92  
93 sub view_definition {
94 my ($self, $key) = @_;
95 return undef unless $self->table_is_view($key);
96  
97 my $dbh = $self->dbh;
98 local $dbh->{LongReadLen} = 1_000_000;
99 local $dbh->{LongTruncOk} = 0;
100  
101 my ($text) = $dbh->selectrow_array(
102 'SELECT text FROM all_views WHERE owner = ? AND view_name = ?',
103 undef, $self->schema, $key,
104 );
105 return undef unless defined $text;
106 $text =~ s/^\s+//;
107 $text =~ s/\s+\z//;
108 $text =~ s/\s*;\s*\z//;
109 return $text;
110 }
111  
112  
113 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 DBIO::Oracle::Introspect - Introspect an Oracle database via all_* views
124  
125 =head1 VERSION
126  
127 version 0.900001
128  
129 =head1 DESCRIPTION
130  
131 C<DBIO::Oracle::Introspect> reads the live state of an Oracle database
132 via the C<all_*> data dictionary views (C<all_tables>, C<all_tab_columns>,
133 C<all_indexes>, C<all_constraints>, etc.). It is the source side of the
134 test-deploy-and-compare strategy used by L<DBIO::Oracle::Deploy>.
135  
136 my $intro = DBIO::Oracle::Introspect->new(
137 dbh => $dbh,
138 schema => 'MYUSER',
139 );
140 my $model = $intro->model;
141  
142 Model shape:
143  
144 {
145 tables => { $name => { ... } },
146 columns => { $table => [ { ... }, ... ] },
147 indexes => { $table => { $name => { ... } } },
148 foreign_keys => { $table => [ { ... }, ... ] },
149 primary_keys => { $table => [ $col, ... ] },
150 unique_constraints => { $table => [ [ $name => \@cols ], ... ] },
151 }
152  
153 The Oracle introspection is built on the C<all_*> data dictionary views,
154 providing sequence detection via trigger inspection, LOB type handling,
155 and other Oracle-specific behaviors.
156  
157 =head1 ATTRIBUTES
158  
159 =head2 schema
160  
161 Schema (user) name to introspect. Defaults to the current connected user
162 (via C<SELECT USER FROM DUAL>).
163  
164 =head1 METHODS
165  
166 =head2 table_columns_info
167  
168 my \%info = %{ $intro->table_columns_info($key) };
169  
170 Hashref of normalized per-column metadata. The Oracle override adds the
171 C<sequence> field (populated from BEFORE INSERT trigger inspection) on
172 top of the canonical C<data_type> / C<size> / C<is_nullable> /
173 C<default_value> / C<is_auto_increment> fields.
174  
175 =head2 table_pk_info
176  
177 my \@pk_cols = @{ $intro->table_pk_info($key) };
178  
179 Ordered list of primary key column names. The Oracle introspector
180 populates PKs in a separate C<primary_keys> section (keyed by table),
181 not as C<is_pk> flags on the columns -- so the canonical default, which
182 walks the C<columns> section, would return an empty list. This override
183 reads the dedicated section.
184  
185 =head2 table_fk_info
186  
187 my \@fks = @{ $intro->table_fk_info($key) };
188  
189 Each FK is a hashref in the normalized contract shape. The Oracle
190 override adds C<is_deferrable> to C<attrs> -- the canonical
191 implementation drops it, and the Oracle storage layer (C<ALTER SESSION
192 SET CONSTRAINTS = DEFERRED>) needs to know whether a constraint is
193 DEFERRABLE.
194  
195 =head2 view_definition
196  
197 SQL text of the view definition, or C<undef>. Read from C<all_views> (a
198 C<LONG> column, so C<LongReadLen> is raised for the fetch).
199  
200 =head1 NORMALIZED CONTRACT
201  
202 The canonical L<DBIO::Introspect::Base> contract methods
203 (C<table_keys>, C<table_columns>, C<table_uniq_info>, C<table_is_view>)
204 are inherited unchanged -- the Oracle model uses bare table names as keys
205 and a C<unique_constraints> section, both of which match the canonical
206 shape. The four methods below override the base to surface Oracle-specific
207 data that the canonical model would drop.
208  
209 =seealso
210  
211 =over 4
212  
213 =item * L<DBIO::Oracle::Deploy> - uses this class to compare current and desired state
214  
215 =item * L<DBIO::Oracle::Diff> - compares two models produced by this class
216  
217 =back
218  
219 =head1 AUTHOR
220  
221 DBIO & DBIx::Class Authors
222  
223 =head1 COPYRIGHT AND LICENSE
224  
225 Copyright (C) 2026 DBIO Authors
226 Portions Copyright (C) 2005-2025 DBIx::Class Authors
227 Based on DBIx::Class, heavily modified.
228  
229 This is free software; you can redistribute it and/or modify it under
230 the same terms as the Perl 5 programming language system itself.
231  
232 =cut
« Oracle/Diff.pm ↑ Summary Oracle/Diff/Column.pm »