| 1 | package DBIO::Oracle::Introspect::Columns; | ||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect Oracle columns | ||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||
| 4 | 30 | ▶ | use strict; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 5 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||
| 7 | 30 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||
| 9 | 30 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 10 | 30 | ▶ | use DBIO::Oracle::Type; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||
| 13 | # One identifier of a trigger body: quoted ($1, a doubled quote inside is one | ||||||||||||||||||||||||||
| 14 | # quote) or bare ($2, folded to upper case by the server). | ||||||||||||||||||||||||||
| 15 | 30 | my $IDENT = qr/"((?:[^"]|"")+)"|([A-Za-z][\w\$#]*)/; | |||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | # The name as the server has it, from the two captures of $IDENT. | ||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub _ident { | ||||||||||||||||||||||||
|
Subroutine
_ident: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||
| 19 | 0 | my ($quoted, $bare) = @_; | |||||||||||||||||||||||||
| 20 | 0 | ▶ | return uc $bare if defined $bare; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 21 | 0 | ▶ | return undef unless defined $quoted; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 22 | 0 | (my $name = $quoted) =~ s/""/"/g; | |||||||||||||||||||||||||
| 23 | 0 | return $name; | |||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||
| 27 | 2 | ▶ | sub fetch { | ||||||||||||||||||||||||
|
Subroutine
fetch: called CC=25 SCAR=62.8
Pod
documented
| |||||||||||||||||||||||||||
| 28 | 2 | my ($class, $dbh, $schema, $tables) = @_; | |||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||
| 30 | 2 | my %columns; | |||||||||||||||||||||||||
| 31 | 2 | my @table_names = sort keys %$tables; | |||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||
| 33 | # data_default and trigger_body are LONG columns. Set before the statements | ||||||||||||||||||||||||||
| 34 | # are prepared: that is when DBD::Oracle sizes the buffer for them. | ||||||||||||||||||||||||||
| 35 | 2 | local $dbh->{LongReadLen} = 1_000_000; | |||||||||||||||||||||||||
| 36 | 2 | local $dbh->{LongTruncOk} = 0; | |||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||
| 38 | # ADR 0008: DBD::Oracle is loaded where it is needed, not at load time | ||||||||||||||||||||||||||
| 39 | 2 | require DBD::Oracle; | |||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||
| 41 | # Old DBD::Oracle reports size in (UTF-16) bytes, not characters | ||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $nchar_size_factor = $DBD::Oracle::VERSION >= 1.52 ? 1 : 2; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||
| 44 | # all_tab_columns.identity_column exists from 12c on; before that the | ||||||||||||||||||||||||||
| 45 | # column is not selected at all (ORA-00904) and no column is an IDENTITY. | ||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $has_identity = ( ($dbh->func('ora_server_version') || [0])->[0] >= 12 ) ? 1 : 0; | ||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$dbh->func('ora_server_version') || [0]
MC/DC0%0/1
$dbh->func('ora_server_version') || [0]
$dbh->func('ora_server_version')
Truth table0%0/2
A
A= $dbh->func('ora_server_version')
| |||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $identity_sel = $has_identity ? 'identity_column' : q{'NO' AS identity_column}; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||
| 49 | 0 | for my $table_name (@table_names) { | |||||||||||||||||||||||||
| 50 | 0 | my @col_list; | |||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||
| 52 | 0 | my $col_sth = $dbh->prepare_cached(qq{ | |||||||||||||||||||||||||
| 53 | SELECT column_name, data_type, data_length, char_length, char_used, | ||||||||||||||||||||||||||
| 54 | data_precision, data_scale, nullable, data_default, column_id, $identity_sel | ||||||||||||||||||||||||||
| 55 | FROM all_tab_columns | ||||||||||||||||||||||||||
| 56 | WHERE table_name = ? AND owner = ? | ||||||||||||||||||||||||||
| 57 | ORDER BY column_id | ||||||||||||||||||||||||||
| 58 | }); | ||||||||||||||||||||||||||
| 59 | 0 | $col_sth->execute($table_name, $schema); | |||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | while (my $row = $col_sth->fetchrow_hashref('NAME_lc')) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 62 | my $col = DBIO::Oracle::Type::map_dbd_type_to_dbio( | ||||||||||||||||||||||||||
| 63 | $row->{data_type}, | ||||||||||||||||||||||||||
| 64 | data_length => $row->{data_length}, | ||||||||||||||||||||||||||
| 65 | char_length => $row->{char_length}, | ||||||||||||||||||||||||||
| 66 | char_used => $row->{char_used}, | ||||||||||||||||||||||||||
| 67 | data_precision => $row->{data_precision}, | ||||||||||||||||||||||||||
| 68 | data_scale => $row->{data_scale}, | ||||||||||||||||||||||||||
| 69 | 0 | nchar_size_factor => $nchar_size_factor, | |||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||
| 71 | 0 | $col->{column_name} = $row->{column_name}; | |||||||||||||||||||||||||
| 72 | 0 | ▶ | $col->{not_null} = (uc($row->{nullable} // 'Y') eq 'N') ? 1 : 0; | ||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"nullable"} // "Y"
MC/DC0%0/1
$row->{"nullable"} // "Y"
$row->{"nullable"}
Truth table0%0/2
A
A= $row->{"nullable"}
| |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||
| 74 | # GENERATED ... AS IDENTITY: Oracle keeps a sequence of its own for the | ||||||||||||||||||||||||||
| 75 | # column and shows it as the expression default "ISEQ$$_n".nextval. | ||||||||||||||||||||||||||
| 76 | # That is not a default of the column; the column is auto-increment | ||||||||||||||||||||||||||
| 77 | # (karr k41). | ||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $identity = uc($row->{identity_column} // 'NO') eq 'YES'; | ||||||||||||||||||||||||
|
Condition0%0/2
$row->{"identity_column"} // "NO"
MC/DC0%0/1
$row->{"identity_column"} // "NO"
$row->{"identity_column"}
Truth table0%0/2
A
A= $row->{"identity_column"}
| |||||||||||||||||||||||||||
| 79 | 0 | ▶ | $col->{is_auto_increment} = 1 if $identity; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||
| 81 | # Handle default value | ||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $default = $identity ? undef : $row->{data_default}; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (defined $default) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 84 | 0 | $default =~ s/^\s+|\s+\z//g; | |||||||||||||||||||||||||
| 85 | 0 | ▶ | if (uc $default eq 'NULL') { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 86 | # DEFAULT NULL is no default (karr k63): what the diff writes to | ||||||||||||||||||||||||||
| 87 | # take a default away reads back as a column without one | ||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||
| 89 | elsif ($default =~ /^'((?:[^']|'')*)'\z/) { | ||||||||||||||||||||||||||
| 90 | # A string literal: the inverse of DBIO::Oracle::Type:: | ||||||||||||||||||||||||||
| 91 | # render_default_value (karr k47). The apostrophe is doubled in the | ||||||||||||||||||||||||||
| 92 | # literal; a quote in the middle that is not doubled ('a'||'b') | ||||||||||||||||||||||||||
| 93 | # makes it an expression, which falls through to the scalar ref. | ||||||||||||||||||||||||||
| 94 | 0 | ($col->{default_value} = $1) =~ s/''/'/g; | |||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||
| 96 | elsif ($default =~ /^(-?[\d.]+)\z/) { | ||||||||||||||||||||||||||
| 97 | 0 | $col->{default_value} = $1; | |||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||
| 99 | elsif (lc($default) eq 'sysdate') { | ||||||||||||||||||||||||||
| 100 | 0 | my $ts = 'current_timestamp'; | |||||||||||||||||||||||||
| 101 | 0 | $col->{default_value} = \$ts; | |||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||
| 103 | elsif ($default ne '') { | ||||||||||||||||||||||||||
| 104 | 0 | $col->{default_value} = \$default; | |||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||
| 108 | 0 | push @col_list, $col; | |||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||
| 110 | 0 | $col_sth->finish; | |||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||
| 112 | # Detect sequences from BEFORE INSERT triggers | ||||||||||||||||||||||||||
| 113 | 0 | my $trig_sth = $dbh->prepare_cached(q{ | |||||||||||||||||||||||||
| 114 | SELECT trigger_body | ||||||||||||||||||||||||||
| 115 | FROM all_triggers | ||||||||||||||||||||||||||
| 116 | WHERE table_name = ? AND table_owner = ? | ||||||||||||||||||||||||||
| 117 | AND status = 'ENABLED' | ||||||||||||||||||||||||||
| 118 | AND UPPER(trigger_type) LIKE '%BEFORE EACH ROW%' | ||||||||||||||||||||||||||
| 119 | AND LOWER(triggering_event) LIKE '%insert%' | ||||||||||||||||||||||||||
| 120 | }); | ||||||||||||||||||||||||||
| 121 | 0 | $trig_sth->execute($table_name, $schema); | |||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||
| 123 | 0 | my %seq_for_col; | |||||||||||||||||||||||||
| 124 | 0 | ▶ | while (my ($body) = $trig_sth->fetchrow_array) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 125 | # An identifier is quoted (kept as written, a doubled quote is one | ||||||||||||||||||||||||||
| 126 | # quote) or bare (folded to upper case by the server). The model | ||||||||||||||||||||||||||
| 127 | # spells the names as the server has them (karr k61). | ||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (my @m = $body =~ /(?:$IDENT\s*\.\s*)?$IDENT\s*\.\s*nextval/i) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $seq_schema = _ident(@m[0, 1]) // $schema; | ||||||||||||||||||||||||
|
Condition0%0/3
_ident(@m[0, 1]) // $schema
MC/DC0%0/2
_ident(@m[0, 1]) // $schema
_ident(@m[0, 1])
$schema
Truth table0%0/3
A // B
A= _ident(@m[0, 1])B= $schema
| |||||||||||||||||||||||||||
| 130 | 0 | my $seq_name = _ident(@m[2, 3]); | |||||||||||||||||||||||||
| 131 | # :new.id as a hand-written trigger has it, :new."id" as the install | ||||||||||||||||||||||||||
| 132 | # DDL writes it (karr k42) | ||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (my @c = $body =~ /:new\.$IDENT/i) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 134 | 0 | $seq_for_col{ _ident(@c) } = "$seq_schema.$seq_name"; | |||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||
| 138 | 0 | $trig_sth->finish; | |||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||
| 140 | # Attach sequence info to columns | ||||||||||||||||||||||||||
| 141 | 0 | for my $col (@col_list) { | |||||||||||||||||||||||||
| 142 | 0 | ▶ | if (my $seq = $seq_for_col{ $col->{column_name} }) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 143 | 0 | $col->{is_auto_increment} = 1; | |||||||||||||||||||||||||
| 144 | 0 | $col->{sequence} = $seq; | |||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | $columns{$table_name} = \@col_list if @col_list; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||
| 151 | 0 | return \%columns; | |||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||
| 154 | 30 | 1; | |||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||