Oracle/Storage/AutoIncrement.pm

filter: Statementstmt 37.3% 34 / 91 Branchbran 20.8% 10 / 48 Conditioncond 21.4% 3 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 66.6% 6 / 9 Podpod 0.0% 0 / 1 totaltotal 30.8% 53 / 172 CC 31 SCAR 50.9
CC
31
Cov
31%
_dbh_get_autoinc_seq
342.0
_dbh_identity_seq
12.9
_sequence_fetch
6.0
CRAP 161.9
« Oracle/Test/Conformance.pm ↑ Summary Oracle/DDL.pm »
1 package DBIO::Oracle::Storage::AutoIncrement;
2 # ABSTRACT: Auto-increment / sequence detection for Oracle
3  
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 our $VERSION = '0.900007';
8  
9 23 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 0 ▶sub _dbh_get_autoinc_seq {
Subroutine _dbh_get_autoinc_seq: not called CC=18 SCAR=58.3
14 0 my ($self, $dbh, $source, $col) = @_;
15  
16 0 my $sql_maker = $self->sql_maker;
17 0 ▶ my ($ql, $qr) = map { $_ ? (quotemeta $_) : '' } $sql_maker->_quote_chars;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
18  
19 0 my $source_name;
20 0 ▶ if (ref $source->name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source->name eq 'SCALAR') { } 0 0 0
21 0 $source_name = ${$source->name};
22 0 ▶ $source_name = uc($source_name) if $source_name !~ /\"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_name =~ /\"/ 0 0 0
23 }
24 else {
25 0 $source_name = $source->name;
26 0 ▶ $source_name = uc($source_name) unless $ql;
Branch: 1 branch
BranchTrueFalseTotal
unless $ql 0 0 0
27 }
28  
29 0 ▶ local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"LongReadLen"} < 65536 0 0 0
30 0 local $sql_maker->{bindtype} = 'normal';
31  
32 0 my ($schema, $table) = $source_name =~ /( (?:${ql})? \w+ (?:${qr})? ) \. ( (?:${ql})? \w+ (?:${qr})? )/x;
33 0 ▶ $schema ||= \'= USER';
Condition0%0/2
$schema ||= \"= USER"
l!l
00
MC/DC0%0/1
$schema ||= \"= USER"
$schema
Truth table0%0/2
A
A= $schema
Aresult
11
00
34  
35 0 ▶ my ($sql, @bind) = $sql_maker->select(
Condition0%0/3
$table || $source_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$table || $source_name
$table $source_name
Truth table0%0/3
A || B
A= $tableB= $source_name
ABresult
1X1
011
000
36 'ALL_TRIGGERS',
37 [qw/TRIGGER_BODY TABLE_OWNER TRIGGER_NAME/],
38 {
39 OWNER => $schema,
40 TABLE_NAME => $table || $source_name,
41 TRIGGERING_EVENT => { -like => '%INSERT%' },
42 TRIGGER_TYPE => { -like => '%BEFORE%' },
43 STATUS => 'ENABLED',
44 },
45 );
46  
47 my @triggers = (map {
48 0 my %inf; @inf{qw/body schema name/} = @$_; \%inf
49 } (grep {
50 0 $_->[0] =~ /\:new\.${ql}${col}${qr} | \:new\.$col/xi
51 0 } @{ $dbh->selectall_arrayref($sql, {}, @bind) }
52 ));
53  
54 @triggers = map {
55 0 my @seqs = $_->{body} =~ / ( [\.\w\"\-]+ ) \. nextval /xig;
56 0 ▶ @seqs ? { %$_, sequences => \@seqs } : ()
Branch: 1 branch
BranchTrueFalseTotal
@seqs ? : 0 0 0
57 } @triggers;
58  
59 0 my $chosen_trigger;
60  
61 0 ▶ if (@triggers == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@triggers == 1) { } 0 0 0
elsif (@triggers > 1) { } 0 0 0
62 0 ▶ if (@{$triggers[0]{sequences}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$triggers[0]{'sequences'};} == 1) { } 0 0 0
63 0 $chosen_trigger = $triggers[0];
64 }
65 else {
66 $self->throw_exception(sprintf(
67 "Unable to introspect trigger '%s' for column '%s.%s' (references multiple sequences). "
68 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
69 0 $triggers[0]{name}, $source_name, $col, $col,
70 ));
71 }
72 }
73 elsif (@triggers > 1) {
74 0 my @candidates = grep { $_->{body} =~ / into \s+ \:new\.$col /xi } @triggers;
75 0 ▶ if (@candidates == 1 && @{$candidates[0]{sequences}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates == 1 and @{$candidates[0]{'sequences'};} == 1) { } 0 0 0
Condition0%0/3
@candidates == 1 and @{$candidates[0]{"sequences"};} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@candidates == 1 and @{$candidates[0]{"sequences"};} == 1
@candidates == 1 @{$candidates[0]{"sequences"};} == 1
Truth table0%0/3
A and B
A= @candidates == 1B= @{$candidates[0]{"sequences"};} == 1
ABresult
0X0
100
111
76 0 $chosen_trigger = $candidates[0];
77 }
78 else {
79 $self->throw_exception(sprintf(
80 "Unable to reliably select a BEFORE INSERT trigger for column '%s.%s' (possibilities: %s). "
81 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
82 0 $source_name, $col, (join ', ', map { "'$_->{name}'" } @triggers), $col,
83 ));
84 }
85 }
86  
87 0 ▶ if ($chosen_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chosen_trigger) 0 0 0
88 0 my $seq_name = $chosen_trigger->{sequences}[0];
89 0 ▶ $seq_name = "$chosen_trigger->{schema}.$seq_name" unless $seq_name =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $seq_name =~ /\./ 0 0 0
90 0 ▶ return \$seq_name if $seq_name =~ /\"/;
Branch: 1 branch
BranchTrueFalseTotal
if $seq_name =~ /\"/ 0 0 0
91 0 return $seq_name;
92 }
93  
94 # No trigger: an IDENTITY column (12c and later) has none. Oracle keeps a
95 # sequence of its own for it, named in all_tab_identity_cols (karr k41).
96 0 ▶ if (my $seq = $self->_dbh_identity_seq($dbh, $source_name, $schema, $table, $ql, $qr, $col)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seq = $self->_dbh_identity_seq($dbh, $source_name, $schema, $table, $ql, $qr, $col)) 0 0 0
97 0 return $seq;
98 }
99  
100 0 $self->throw_exception(sprintf(
101 "No suitable BEFORE INSERT triggers found for column '%s.%s'. "
102 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
103 $source_name, $col, $col,
104 ));
105 }
106  
107 # The sequence behind a GENERATED ... AS IDENTITY column, as a quoted
108 # "OWNER"."NAME" (a scalar ref, like a quoted sequence from a trigger), or
109 # nothing when the column is no IDENTITY column or the server has no such
110 # view (before 12c).
111 2 ▶sub _dbh_identity_seq {
Subroutine _dbh_identity_seq: called CC=9 SCAR=25.6
112 2 my ($self, $dbh, $source_name, $schema, $table, $ql, $qr, $col) = @_;
113  
114 2 ▶ my $unquote = sub { my $n = shift; $n =~ s/^"|"\z//g; $n };
Subroutine __ANON__: called CC=1 SCAR=0.0
115 2 ▶ my $owner = ref $schema ? undef : $unquote->($schema);
Branch: 1 branch
BranchTrueFalseTotal
ref $schema ? : 2 0 2
116 2 ▶ my $tname = $unquote->($table || $source_name);
Condition33%1/3
$table || $source_name
l!l&&r!l&&!r
200
MC/DC0%0/2
$table || $source_name
$table $source_name
Truth table33%1/3
A || B
A= $tableB= $source_name
ABresult
1X1
011
000
117 2 ▶ my $cname = $ql ? $col : uc $col;
Branch: 1 branch
BranchTrueFalseTotal
$ql ? : 2 0 2
118  
119 # Only "that view does not exist" (a server before 12c, ORA-00942) means no
120 # IDENTITY column; any other error, a lost connection say, goes through.
121 2 my $row = eval {
122 2 ▶ $dbh->selectrow_arrayref(
Branch: 2 branches
BranchTrueFalseTotal
defined $owner ? : 0 2 2
defined $owner ? : 0 2 2
123 'SELECT owner, sequence_name FROM all_tab_identity_cols '
124 . 'WHERE table_name = ? AND column_name = ? AND owner = '
125 . ( defined $owner ? '?' : 'USER' ),
126 {}, $tname, $cname, ( defined $owner ? $owner : () ),
127 );
128 };
129 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 0 2
130 2 ▶ die $err unless "$err" =~ /ORA-00942/;
Branch: 1 branch
BranchTrueFalseTotal
unless "$err" =~ /ORA-00942/ 1 1 2
131 1 return;
132 }
133 0 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
134  
135 0 my $seq = qq{"$row->[0]"."$row->[1]"};
136 0 return \$seq;
137 }
138  
139 4 ▶sub _dbh_last_insert_id {
Subroutine _dbh_last_insert_id: called CC=5 SCAR=16.2
140 4 my ($self, $dbh, $source, @columns) = @_;
141 4 my @ids;
142 4 for my $col (@columns) {
143 # A sequence the class declares is used as it is. One that was looked up
144 # is remembered by this storage only: column_info belongs to the result
145 # class, which outlives the connection, and the table may have been
146 # dropped and created again since (a new IDENTITY sequence, ORA-02289).
147 4 my $seq = $source->column_info($col)->{sequence};
148 4 ▶ unless (defined $seq) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seq) 3 1 4
149 3 my $sname = $source->name;
150 3 ▶ my $key = join "\0", (ref $sname ? $$sname : $sname), $col;
Branch: 1 branch
BranchTrueFalseTotal
ref $sname ? : 0 3 3
151 3 ▶ $seq = ($self->{_autoinc_sequence}{$key} ||= $self->get_autoinc_seq($source, $col));
Condition66%2/3
$self->{"_autoinc_sequence"}{$key} ||= $self->get_autoinc_seq($source, $col)
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"_autoinc_sequence"}{$key} ||= $self->get_autoinc_seq($source, $col)
$self->{"_autoinc_sequence"}{$key} $self->get_autoinc_seq($source, $col)
Truth table66%2/3
A ||= B
A= $self->{"_autoinc_sequence"}{$key}B= $self->get_autoinc_seq($source, $col)
ABresult
1X1
011
000
152 }
153 4 push @ids, $self->_sequence_fetch('CURRVAL', $seq);
154 }
155 4 return @ids;
156 }
157  
158 0 ▶sub _sequence_fetch {
Subroutine _sequence_fetch: not called CC=2 SCAR=17.9
159 0 my ($self, $type, $seq) = @_;
160 0 ▶ my $sth = $self->_dbh->prepare_cached(
Branch: 1 branch
BranchTrueFalseTotal
ref $seq ? : 0 0 0
161 $self->sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] )
162 );
163 0 $sth->execute;
164 0 my ($id) = $sth->fetchrow_array;
165 0 $sth->finish;
166 0 return $id;
167 }
168  
169 0 ▶sub get_autoinc_seq {
Subroutine get_autoinc_seq: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my ($self, $source, $col) = @_;
171 0 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
172 }
173  
174 23 1;
175  
176 __END__
« Oracle/Test/Conformance.pm ↑ Summary Oracle/DDL.pm »