| 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
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | my $source_name; | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | if (ref $source->name eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | $source_name = ${$source->name}; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | $source_name = uc($source_name) if $source_name !~ /\"/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | else { | ||||||||||||||||||||||||||||
| 23 | 0 | $source_name = $source->name; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | $source_name = uc($source_name) unless $ql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$schema ||= \"= USER"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my ($sql, @bind) = $sql_maker->select( | ||||||||||||||||||||||||||
|
Condition0%0/3
$table || $source_name
MC/DC0%0/2
$table || $source_name
$table
$source_name
Truth table0%0/3
A || B
A= $tableB= $source_name
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | } @triggers; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $chosen_trigger; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (@triggers == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if (@{$triggers[0]{sequences}} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
@candidates == 1 and @{$candidates[0]{"sequences"};} == 1
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 86 | 0 | my $seq_name = $chosen_trigger->{sequences}[0]; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $seq_name = "$chosen_trigger->{schema}.$seq_name" unless $seq_name =~ /\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | return \$seq_name if $seq_name =~ /\"/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||