| 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
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | my $source_name; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | if (ref $source->name eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | $source_name = ${$source->name}; | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | $source_name = uc($source_name) if $source_name !~ /\"/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||||||||||
| 25 | 0 | $source_name = $source->name; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | $source_name = uc($source_name) unless $ql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$schema ||= \"= USER"
$schema
Truth table0%0/2
A
A= $schema
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | } @triggers; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $chosen_trigger; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (@triggers == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (@{$triggers[0]{sequences}} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 0 | my $seq_name = $chosen_trigger->{sequences}[0]; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | $seq_name = "$chosen_trigger->{schema}.$seq_name" unless $seq_name =~ /\./; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return \$seq_name if $seq_name =~ /\"/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | my $tname = $unquote->($table || $source_name); | ||||||||||||||||||||||||||
|
Condition33%1/3
$table || $source_name
MC/DC0%0/2
$table || $source_name
$table
$source_name
Truth table33%1/3
A || B
A= $tableB= $source_name
| |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | my $cname = $ql ? $col : uc $col; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | die $err unless "$err" =~ /ORA-00942/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | return; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | return unless $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 149 | 3 | my $sname = $source->name; | |||||||||||||||||||||||||||
| 150 | 3 | ▶ | my $key = join "\0", (ref $sname ? $$sname : $sname), $col; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||