| 1 | package DBIO::Oracle::Introspect::Columns; | ||
| 2 | # ABSTRACT: Introspect Oracle columns | ||
| 3 | |||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 3 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use DBD::Oracle; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use DBIO::Oracle::Type; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | sub fetch { | ||
| 14 | my ($class, $dbh, $schema, $tables) = @_; | ||
| 15 | |||
| 16 | my %columns; | ||
| 17 | my @table_names = sort keys %$tables; | ||
| 18 | |||
| 19 | # Old DBD::Oracle reports size in (UTF-16) bytes, not characters | ||
| 20 | my $nchar_size_factor = $DBD::Oracle::VERSION >= 1.52 ? 1 : 2; | ||
| 21 | |||
| 22 | for my $table_name (@table_names) { | ||
| 23 | my @col_list; | ||
| 24 | |||
| 25 | my $col_sth = $dbh->prepare_cached(q{ | ||
| 26 | SELECT column_name, data_type, data_length, data_precision, data_scale, | ||
| 27 | nullable, data_default, column_id | ||
| 28 | FROM all_tab_columns | ||
| 29 | WHERE table_name = ? AND owner = ? | ||
| 30 | ORDER BY column_id | ||
| 31 | }); | ||
| 32 | $col_sth->execute($table_name, $schema); | ||
| 33 | |||
| 34 | while (my $row = $col_sth->fetchrow_hashref) { | ||
| 35 | my $col = DBIO::Oracle::Type::map_dbd_type_to_dbio( | ||
| 36 | $row->{data_type}, | ||
| 37 | data_length => $row->{data_length}, | ||
| 38 | data_precision => $row->{data_precision}, | ||
| 39 | data_scale => $row->{data_scale}, | ||
| 40 | nchar_size_factor => $nchar_size_factor, | ||
| 41 | ); | ||
| 42 | $col->{column_name} = $row->{column_name}; | ||
| 43 | $col->{not_null} = (uc($row->{nullable} // 'Y') eq 'N') ? 1 : 0; | ||
| 44 | |||
| 45 | # Handle default value | ||
| 46 | my $default = $row->{data_default}; | ||
| 47 | if (defined $default) { | ||
| 48 | $default =~ s/^\s+|\s+\z//g; | ||
| 49 | if ($default eq 'NULL') { | ||
| 50 | $col->{default_value} = \'null'; | ||
| 51 | } | ||
| 52 | elsif ($default =~ /^'(.*)'\z/) { | ||
| 53 | $col->{default_value} = $1; | ||
| 54 | } | ||
| 55 | elsif ($default =~ /^(-?[\d.]+)\z/) { | ||
| 56 | $col->{default_value} = $1; | ||
| 57 | } | ||
| 58 | elsif (lc($default) eq 'sysdate') { | ||
| 59 | my $ts = 'current_timestamp'; | ||
| 60 | $col->{default_value} = \$ts; | ||
| 61 | } | ||
| 62 | elsif ($default ne '') { | ||
| 63 | $col->{default_value} = \$default; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | push @col_list, $col; | ||
| 68 | } | ||
| 69 | $col_sth->finish; | ||
| 70 | |||
| 71 | # Detect sequences from BEFORE INSERT triggers | ||
| 72 | my $trig_sth = $dbh->prepare_cached(q{ | ||
| 73 | SELECT trigger_body | ||
| 74 | FROM all_triggers | ||
| 75 | WHERE table_name = ? AND table_owner = ? | ||
| 76 | AND status = 'ENABLED' | ||
| 77 | AND UPPER(trigger_type) LIKE '%BEFORE EACH ROW%' | ||
| 78 | AND LOWER(triggering_event) LIKE '%insert%' | ||
| 79 | }); | ||
| 80 | $trig_sth->execute($table_name, $schema); | ||
| 81 | |||
| 82 | my %seq_for_col; | ||
| 83 | while (my ($body) = $trig_sth->fetchrow_array) { | ||
| 84 | if (my ($seq_schema, $seq_name) = $body =~ /(?:"?(\w+)"?\.)?"?(\w+)"?\.nextval/i) { | ||
| 85 | if (my ($col_name) = $body =~ /:new\.(\w+)/i) { | ||
| 86 | $col_name = lc($col_name); | ||
| 87 | $seq_schema = lc($seq_schema || $schema); | ||
| 88 | $seq_name = lc($seq_name); | ||
| 89 | $seq_for_col{$col_name} = "$seq_schema.$seq_name"; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | $trig_sth->finish; | ||
| 94 | |||
| 95 | # Attach sequence info to columns | ||
| 96 | for my $col (@col_list) { | ||
| 97 | my $col_lc = lc($col->{column_name}); | ||
| 98 | if (my $seq = $seq_for_col{$col_lc}) { | ||
| 99 | $col->{is_auto_increment} = 1; | ||
| 100 | $col->{sequence} = $seq; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | $columns{$table_name} = \@col_list if @col_list; | ||
| 105 | } | ||
| 106 | |||
| 107 | return \%columns; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ | ||
| 113 | |||
| 114 | =pod | ||
| 115 | |||
| 116 | =encoding UTF-8 | ||
| 117 | |||
| 118 | =head1 NAME | ||
| 119 | |||
| 120 | DBIO::Oracle::Introspect::Columns - Introspect Oracle columns | ||
| 121 | |||
| 122 | =head1 VERSION | ||
| 123 | |||
| 124 | version 0.900001 | ||
| 125 | |||
| 126 | =head1 DESCRIPTION | ||
| 127 | |||
| 128 | Fetches Oracle column metadata via C<all_tab_columns> and C<all_triggers>. | ||
| 129 | Handles Oracle-specific data types (NUMBER, CHAR, DATE, LOB, etc.) and | ||
| 130 | detects sequences via trigger inspection. | ||
| 131 | |||
| 132 | =head1 METHODS | ||
| 133 | |||
| 134 | =head2 fetch | ||
| 135 | |||
| 136 | my $columns = DBIO::Oracle::Introspect::Columns->fetch($dbh, $schema, $tables); | ||
| 137 | |||
| 138 | Given the tables hashref from L<DBIO::Oracle::Introspect::Tables>, | ||
| 139 | returns a hashref keyed by table name. Each value is an arrayref of | ||
| 140 | column hashrefs in C<column_id> order. | ||
| 141 | |||
| 142 | =head1 AUTHOR | ||
| 143 | |||
| 144 | DBIO & DBIx::Class Authors | ||
| 145 | |||
| 146 | =head1 COPYRIGHT AND LICENSE | ||
| 147 | |||
| 148 | Copyright (C) 2026 DBIO Authors | ||
| 149 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||
| 150 | Based on DBIx::Class, heavily modified. | ||
| 151 | |||
| 152 | This is free software; you can redistribute it and/or modify it under | ||
| 153 | the same terms as the Perl 5 programming language system itself. | ||
| 154 | |||
| 155 | =cut | ||