Oracle/Storage/AutoIncrement.pm

filter: Statementstmt 14.7% 10 / 68 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 42.8% 3 / 7 Podpod 0.0% 0 / 1 totaltotal 10.6% 13 / 122 CC 20 SCAR 53.1
CC
20
Cov
9%
_dbh_get_autoinc_seq
306.0
_dbh_last_insert_id
12.0
_sequence_fetch
6.0
CRAP 202.1
↑ Summary Oracle/Introspect/ForeignKeys.pm »
1 package DBIO::Oracle::Storage::AutoIncrement;
2 # ABSTRACT: Auto-increment / sequence detection for Oracle
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 0 ▶sub _dbh_get_autoinc_seq {
Subroutine _dbh_get_autoinc_seq: not called CC=17 SCAR=57.2
12 0 my ($self, $dbh, $source, $col) = @_;
13  
14 0 my $sql_maker = $self->sql_maker;
15 0 ▶ my ($ql, $qr) = map { $_ ? (quotemeta $_) : '' } $sql_maker->_quote_chars;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
16  
17 0 my $source_name;
18 0 ▶ if (ref $source->name eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source->name eq 'SCALAR') { } 0 0 0
19 0 $source_name = ${$source->name};
20 0 ▶ $source_name = uc($source_name) if $source_name !~ /\"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_name =~ /\"/ 0 0 0
21 }
22 else {
23 0 $source_name = $source->name;
24 0 ▶ $source_name = uc($source_name) unless $ql;
Branch: 1 branch
BranchTrueFalseTotal
unless $ql 0 0 0
25 }
26  
27 0 ▶ local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"LongReadLen"} < 65536 0 0 0
28 0 local $sql_maker->{bindtype} = 'normal';
29  
30 0 my ($schema, $table) = $source_name =~ /( (?:${ql})? \w+ (?:${qr})? ) \. ( (?:${ql})? \w+ (?:${qr})? )/x;
31 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
32  
33 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
34 'ALL_TRIGGERS',
35 [qw/TRIGGER_BODY TABLE_OWNER TRIGGER_NAME/],
36 {
37 OWNER => $schema,
38 TABLE_NAME => $table || $source_name,
39 TRIGGERING_EVENT => { -like => '%INSERT%' },
40 TRIGGER_TYPE => { -like => '%BEFORE%' },
41 STATUS => 'ENABLED',
42 },
43 );
44  
45 my @triggers = (map {
46 0 my %inf; @inf{qw/body schema name/} = @$_; \%inf
47 } (grep {
48 0 $_->[0] =~ /\:new\.${ql}${col}${qr} | \:new\.$col/xi
49 0 } @{ $dbh->selectall_arrayref($sql, {}, @bind) }
50 ));
51  
52 @triggers = map {
53 0 my @seqs = $_->{body} =~ / ( [\.\w\"\-]+ ) \. nextval /xig;
54 0 ▶ @seqs ? { %$_, sequences => \@seqs } : ()
Branch: 1 branch
BranchTrueFalseTotal
@seqs ? : 0 0 0
55 } @triggers;
56  
57 0 my $chosen_trigger;
58  
59 0 ▶ if (@triggers == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@triggers == 1) { } 0 0 0
elsif (@triggers > 1) { } 0 0 0
60 0 ▶ if (@{$triggers[0]{sequences}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$triggers[0]{'sequences'};} == 1) { } 0 0 0
61 0 $chosen_trigger = $triggers[0];
62 }
63 else {
64 $self->throw_exception(sprintf(
65 "Unable to introspect trigger '%s' for column '%s.%s' (references multiple sequences). "
66 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
67 0 $triggers[0]{name}, $source_name, $col, $col,
68 ));
69 }
70 }
71 elsif (@triggers > 1) {
72 0 my @candidates = grep { $_->{body} =~ / into \s+ \:new\.$col /xi } @triggers;
73 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
74 0 $chosen_trigger = $candidates[0];
75 }
76 else {
77 $self->throw_exception(sprintf(
78 "Unable to reliably select a BEFORE INSERT trigger for column '%s.%s' (possibilities: %s). "
79 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
80 0 $source_name, $col, (join ', ', map { "'$_->{name}'" } @triggers), $col,
81 ));
82 }
83 }
84  
85 0 ▶ if ($chosen_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chosen_trigger) 0 0 0
86 0 my $seq_name = $chosen_trigger->{sequences}[0];
87 0 ▶ $seq_name = "$chosen_trigger->{schema}.$seq_name" unless $seq_name =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $seq_name =~ /\./ 0 0 0
88 0 ▶ return \$seq_name if $seq_name =~ /\"/;
Branch: 1 branch
BranchTrueFalseTotal
if $seq_name =~ /\"/ 0 0 0
89 0 return $seq_name;
90 }
91  
92 0 $self->throw_exception(sprintf(
93 "No suitable BEFORE INSERT triggers found for column '%s.%s'. "
94 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
95 $source_name, $col, $col,
96 ));
97 }
98  
99 0 ▶sub _dbh_last_insert_id {
Subroutine _dbh_last_insert_id: not called CC=3 SCAR=24.8
100 0 my ($self, $dbh, $source, @columns) = @_;
101 0 my @ids;
102 0 for my $col (@columns) {
103 0 ▶ my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source, $col));
Condition0%0/3
$source->column_info($col)->{"sequence"} ||= $self->get_autoinc_seq($source, $col)
l!l&&r!l&&!r
000
MC/DC0%0/2
$source->column_info($col)->{"sequence"} ||= $self->get_autoinc_seq($source, $col)
$source->column_info($col)->{"sequence"} $self->get_autoinc_seq($source, $col)
Truth table0%0/3
A ||= B
A= $source->column_info($col)->{"sequence"}B= $self->get_autoinc_seq($source, $col)
ABresult
1X1
011
000
104 0 push @ids, $self->_sequence_fetch('CURRVAL', $seq);
105 }
106 0 return @ids;
107 }
108  
109 0 ▶sub _sequence_fetch {
Subroutine _sequence_fetch: not called CC=2 SCAR=17.9
110 0 my ($self, $type, $seq) = @_;
111 0 ▶ my $sth = $self->_dbh->prepare_cached(
Branch: 1 branch
BranchTrueFalseTotal
ref $seq ? : 0 0 0
112 $self->sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] )
113 );
114 0 $sth->execute;
115 0 my ($id) = $sth->fetchrow_array;
116 0 $sth->finish;
117 0 return $id;
118 }
119  
120 0 ▶sub get_autoinc_seq {
Subroutine get_autoinc_seq: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self, $source, $col) = @_;
122 0 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
123 }
124  
125 1 1;
126  
127 __END__
128  
129 =pod
130  
131 =encoding UTF-8
132  
133 =head1 NAME
134  
135 DBIO::Oracle::Storage::AutoIncrement - Auto-increment / sequence detection for Oracle
136  
137 =head1 VERSION
138  
139 version 0.900001
140  
141 =head1 DESCRIPTION
142  
143 Detects Oracle sequences associated with autoincrement columns by inspecting
144 BEFORE INSERT triggers in ALL_TRIGGERS.
145  
146 =head1 AUTHOR
147  
148 DBIO & DBIx::Class Authors
149  
150 =head1 COPYRIGHT AND LICENSE
151  
152 Copyright (C) 2026 DBIO Authors
153 Portions Copyright (C) 2005-2025 DBIx::Class Authors
154 Based on DBIx::Class, heavily modified.
155  
156 This is free software; you can redistribute it and/or modify it under
157 the same terms as the Perl 5 programming language system itself.
158  
159 =cut
↑ Summary Oracle/Introspect/ForeignKeys.pm »