Oracle/Diff/Table.pm

filter: Statementstmt 92.1% 59 / 64 Branchbran 63.6% 14 / 22 Conditioncond 62.5% 10 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 80.6% 100 / 124 CC 21 SCAR 22.6
CC
21
Cov
81%
as_sql
17.6
_oracle_type
4.2
__ANON__
3.0
CRAP 9.6
« Oracle/Diff/Index.pm ↑ Summary Oracle/Identifier.pm »
1 package DBIO::Oracle::Diff::Table;
2 # ABSTRACT: Diff operations for Oracle tables
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::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 __PACKAGE__->mk_diff_accessors(qw(table_name table_info columns foreign_keys));
12  
13  
14  
15 5 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.0
Pod documented
16 5 my ($class, $source, $target, $target_columns, $target_fks) = @_;
17 5 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
41
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
18 5 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
41
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
19  
20 return $class->diff_toplevel(
21 $source, $target,
22 4 ▶ create => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
23 4 my ($name) = @_;
24 $class->new(
25 action => 'create',
26 table_name => $name,
27 table_info => $target->{$name},
28 columns => $target_columns->{$name} // [],
29 4 ▶ foreign_keys => $target_fks->{$name} // [],
Condition75%3/4
$target_columns->{$name} // []
l!l
40
$target_fks->{$name} // []
l!l
22
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Aresult
11
00
Truth table100%2/2
A
A= $target_fks->{$name}
Aresult
11
00
30 );
31 },
32 1 ▶ drop => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 my ($name) = @_;
34 $class->new(
35 action => 'drop',
36 table_name => $name,
37 1 table_info => $source->{$name},
38 );
39 },
40 5 );
41 }
42  
43  
44 5 ▶sub as_sql {
Subroutine as_sql: called CC=14 SCAR=28.7
Pod documented
45 5 my ($self) = @_;
46  
47 5 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 1 4 5
48 1 return sprintf 'DROP TABLE %s CASCADE CONSTRAINTS;',
49 _quote_ident($self->table_name);
50 }
51  
52 4 my @col_defs;
53 my @pk_cols;
54 4 my @seq_stmts;
55  
56 4 for my $col (@{ $self->columns }) {
57 7 ▶ push @pk_cols, $col->{column_name} if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 5 2 7
58  
59 7 my $type = _oracle_type($col);
60 7 my $def = sprintf ' %s %s', _quote_ident($col->{column_name}), $type;
61 7 ▶ $def .= ' NOT NULL' if $col->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"not_null"} 7 0 7
62  
63 # Handle default value
64 7 ▶ if (defined $col->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"}) 0 7 7
65 0 my $dv = $col->{default_value};
66 0 ▶ if (ref $dv eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $dv eq 'SCALAR') { } 0 0 0
elsif (defined $dv and $dv ne 'null') { } 0 0 0
67 0 $def .= " DEFAULT $$dv";
68 }
69 ▶ elsif (defined $dv && $dv ne 'null') {
Condition0%0/3
defined $dv and $dv ne "null"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dv and $dv ne "null"
defined $dv $dv ne "null"
Truth table0%0/3
A and B
A= defined $dvB= $dv ne "null"
ABresult
0X0
100
111
70 0 $def .= " DEFAULT '$dv'";
71 }
72 }
73  
74 # Handle sequence-based auto-increment
75 7 ▶ if ($col->{is_auto_increment} && $col->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col->{"is_auto_increment"} and $col->{"sequence"}) 1 6 7
Condition66%2/3
$col->{"is_auto_increment"} and $col->{"sequence"}
!ll&&!rl&&r
601
MC/DC50%1/2
$col->{"is_auto_increment"} and $col->{"sequence"}
$col->{"is_auto_increment"} $col->{"sequence"}
Truth table66%2/3
A and B
A= $col->{"is_auto_increment"}B= $col->{"sequence"}
ABresult
0X0
100
111
76 push @seq_stmts, sprintf 'CREATE SEQUENCE %s;',
77 1 _quote_ident($col->{sequence});
78 }
79  
80 7 push @col_defs, $def;
81 }
82  
83 4 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 4 0 4
84 push @col_defs, sprintf ' PRIMARY KEY (%s)',
85 4 join(', ', map { _quote_ident($_) } @pk_cols);
86 }
87  
88 4 for my $fk (@{ $self->foreign_keys }) {
89 push @col_defs, sprintf ' FOREIGN KEY (%s) REFERENCES %s(%s)',
90 1 join(', ', map { _quote_ident($_) } @{ $fk->{from_columns} }),
91 _quote_ident($fk->{to_table}),
92 1 join(', ', map { _quote_ident($_) } @{ $fk->{to_columns} });
93 }
94  
95 4 my $create = sprintf "CREATE TABLE %s (\n%s\n);",
96 _quote_ident($self->table_name), join(",\n", @col_defs);
97  
98 4 ▶ if (@seq_stmts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@seq_stmts) 1 3 4
99 1 return (join "\n", @seq_stmts) . "\n$create";
100 }
101 3 return $create;
102 }
103  
104  
105 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
106 2 my ($self) = @_;
107 2 return sprintf '%s table: %s', $self->summary_prefix, $self->table_name;
108 }
109  
110 7 ▶sub _oracle_type {
Subroutine _oracle_type: called CC=4 SCAR=14.3
111 7 my ($col) = @_;
112 7 ▶ my $type = $col->{data_type} || 'VARCHAR2';
Condition50%1/2
$col->{'data_type'} || 'VARCHAR2'
l!l
70
MC/DC0%0/1
$col->{'data_type'} || 'VARCHAR2'
$col->{'data_type'}
Truth table50%1/2
A
A= $col->{'data_type'}
Aresult
11
00
113  
114 # Handle size spec
115 7 ▶ if (defined $col->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"size"}) 1 6 7
116 1 ▶ if (ref $col->{size} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col->{'size'} eq 'ARRAY') { } 0 1 1
117 0 return sprintf '%s(%d,%d)', uc($type), $col->{size}[0], $col->{size}[1];
118 }
119 else {
120 1 return sprintf '%s(%d)', uc($type), $col->{size};
121 }
122 }
123  
124 6 return uc($type);
125 }
126  
127 2 1;
128  
129 __END__
130  
131 =pod
132  
133 =encoding UTF-8
134  
135 =head1 NAME
136  
137 DBIO::Oracle::Diff::Table - Diff operations for Oracle tables
138  
139 =head1 VERSION
140  
141 version 0.900001
142  
143 =head1 DESCRIPTION
144  
145 Represents a table-level diff operation in Oracle: C<CREATE TABLE> or
146 C<DROP TABLE>. C<create> ops capture the target columns and foreign keys
147 so C<as_sql> can emit the full inline definition.
148  
149 =head1 ATTRIBUTES
150  
151 =head2 table_name
152  
153 The bare table name.
154  
155 =head2 table_info
156  
157 Hashref of table metadata from introspection (C<kind>, C<schema>, etc.).
158  
159 =head2 columns
160  
161 ArrayRef of column hashrefs for new tables (populated when C<action> is
162 C<create>), so that C<as_sql> can emit a complete CREATE TABLE with all
163 column definitions rather than an empty shell.
164  
165 =head2 foreign_keys
166  
167 ArrayRef of FK hashrefs for new tables (populated when C<action> is
168 C<create>), inlined in the CREATE TABLE.
169  
170 =head1 METHODS
171  
172 =head2 diff
173  
174 my @ops = DBIO::Oracle::Diff::Table->diff(
175 $source_tables, $target_tables,
176 $target_columns, $target_fks,
177 );
178  
179 =head2 as_sql
180  
181 =head2 summary
182  
183 =head1 AUTHOR
184  
185 DBIO & DBIx::Class Authors
186  
187 =head1 COPYRIGHT AND LICENSE
188  
189 Copyright (C) 2026 DBIO Authors
190 Portions Copyright (C) 2005-2025 DBIx::Class Authors
191 Based on DBIx::Class, heavily modified.
192  
193 This is free software; you can redistribute it and/or modify it under
194 the same terms as the Perl 5 programming language system itself.
195  
196 =cut
« Oracle/Diff/Index.pm ↑ Summary Oracle/Identifier.pm »