| 1 | package DBIO::Oracle::Type; | ||
| 2 | # ABSTRACT: Oracle type mapping utilities | ||
| 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 DBD::Oracle; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | sub is_lob_type { | ||
| 12 | my ($self, $dt) = @_; | ||
| 13 | $dt && ($dt =~ /lob|bfile|text|image|bytea|memo/i | ||
| 14 | || $dt =~ /^long(?:\s+(?:raw|bit\s*varying|varbit|binary | ||
| 15 | |varchar|character\s*varying|nvarchar | ||
| 16 | |national\s*character\s*varying))?\z/xi); | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | sub is_text_lob_type { | ||
| 21 | my ($self, $dt) = @_; | ||
| 22 | $dt && ($dt =~ /^(?:n?clob|memo)\z/i | ||
| 23 | || $dt =~ /^long(?:\s+(?:varchar|character\s*varying|nvarchar | ||
| 24 | |national\s*character\s*varying))\z/xi); | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | sub oracle_lob_bind_attrs { | ||
| 29 | my ($is_text) = @_; | ||
| 30 | return { | ||
| 31 | ora_type => $is_text ? DBD::Oracle::ORA_CLOB() : DBD::Oracle::ORA_BLOB(), | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub normalize_type { | ||
| 37 | my ($t) = @_; | ||
| 38 | $t //= ''; | ||
| 39 | $t =~ s/\s+/ /g; | ||
| 40 | return uc $t; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub map_dbd_type_to_dbio { | ||
| 45 | my ($data_type, %args) = @_; | ||
| 46 | my $nchar_size_factor = $args{nchar_size_factor} // 1; | ||
| 47 | |||
| 48 | $data_type //= ''; | ||
| 49 | my $lc_dt = lc $data_type; | ||
| 50 | |||
| 51 | my %col = (data_type => $lc_dt); | ||
| 52 | |||
| 53 | if ($lc_dt =~ /^(?:n(?:var)?char2?|u?rowid|nclob)\z/i) { | ||
| 54 | $col{data_type} = $data_type; # keep case for special types | ||
| 55 | $col{size} = length($data_type) if $lc_dt =~ /^u?rowid\z/i; | ||
| 56 | } | ||
| 57 | elsif ($lc_dt =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) { | ||
| 58 | delete $col{size}; | ||
| 59 | } | ||
| 60 | elsif ($lc_dt =~ /^n(?:var)?char2?\z/i) { | ||
| 61 | $col{size} = $args{data_length} / $nchar_size_factor if $args{data_length}; | ||
| 62 | } | ||
| 63 | elsif ($lc_dt =~ /^(?:var)?char2?\z/i) { | ||
| 64 | $col{size} = $args{data_length}; | ||
| 65 | } | ||
| 66 | elsif ($lc_dt =~ /^(number|decimal)\z/i) { | ||
| 67 | $col{data_type} = 'numeric'; | ||
| 68 | if (defined $args{data_precision} && $args{data_precision} == 38 | ||
| 69 | && (!defined $args{data_scale} || $args{data_scale} == 0)) { | ||
| 70 | $col{data_type} = 'integer'; | ||
| 71 | } | ||
| 72 | elsif (defined $args{data_precision} && defined $args{data_scale}) { | ||
| 73 | $col{size} = [$args{data_precision}, $args{data_scale}]; | ||
| 74 | } | ||
| 75 | elsif (defined $args{data_precision}) { | ||
| 76 | $col{size} = $args{data_precision}; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | elsif (my ($precision) = $lc_dt =~ /^timestamp\((\d+)\)(?: with(?: local)? time zone)?\z/i) { | ||
| 80 | $col{data_type} = $data_type =~ /time zone/i ? 'timestamp with time zone' : 'timestamp'; | ||
| 81 | $col{size} = $precision unless $precision == 6; | ||
| 82 | } | ||
| 83 | elsif ($lc_dt =~ /^interval year to month\z/i) { | ||
| 84 | $col{data_type} = 'interval year to month'; | ||
| 85 | $col{size} = $args{data_precision} // 2; | ||
| 86 | } | ||
| 87 | elsif (my ($day_p, $sec_p) = $lc_dt =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) { | ||
| 88 | $col{data_type} = 'interval day to second'; | ||
| 89 | $col{size} = [$day_p, $sec_p] unless ($day_p == 2 && $sec_p == 6); | ||
| 90 | } | ||
| 91 | elsif ($lc_dt eq 'float') { | ||
| 92 | $col{data_type} = $args{data_length} <= 63 ? 'real' : 'double precision'; | ||
| 93 | } | ||
| 94 | elsif ($lc_dt eq 'date') { | ||
| 95 | $col{data_type} = 'datetime'; | ||
| 96 | } | ||
| 97 | elsif ($lc_dt eq 'binary_float') { | ||
| 98 | $col{data_type} = 'real'; | ||
| 99 | } | ||
| 100 | elsif ($lc_dt eq 'binary_double') { | ||
| 101 | $col{data_type} = 'double precision'; | ||
| 102 | } | ||
| 103 | elsif ($lc_dt eq 'raw') { | ||
| 104 | $col{size} = $args{data_length} / 2 if $args{data_length}; | ||
| 105 | } | ||
| 106 | |||
| 107 | return \%col; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub map_dbio_type_to_oracle { | ||
| 112 | my ($type, %args) = @_; | ||
| 113 | $type //= 'varchar2'; | ||
| 114 | my $lc = lc $type; | ||
| 115 | |||
| 116 | my $size = $args{size}; | ||
| 117 | |||
| 118 | # Render a base type with its optional size, e.g. VARCHAR2(128) or | ||
| 119 | # NUMBER(10,2). When no size is given, fall back to $default (which may | ||
| 120 | # itself be undef for an unsized base type). | ||
| 121 | my $sized = sub { | ||
| 122 | my ($base, $default) = @_; | ||
| 123 | my $spec = defined $size | ||
| 124 | ? (ref $size eq 'ARRAY' ? join(',', @$size) : $size) | ||
| 125 | : $default; | ||
| 126 | return defined $spec && length $spec ? "$base($spec)" : $base; | ||
| 127 | }; | ||
| 128 | |||
| 129 | # Numeric types | ||
| 130 | return 'NUMBER' if $lc eq 'integer' || $lc eq 'bigint' || $lc eq 'smallint'; | ||
| 131 | return 'NUMBER(10)' if $lc eq 'serial'; | ||
| 132 | return 'NUMBER(20)' if $lc eq 'bigserial'; | ||
| 133 | |||
| 134 | # Character types | ||
| 135 | return $sized->('VARCHAR2', 255) if $lc eq 'varchar'; | ||
| 136 | return $sized->('VARCHAR2', 255) if $lc eq 'nvarchar'; | ||
| 137 | return $sized->('CHAR', 1) if $lc eq 'char'; | ||
| 138 | return $sized->('NCHAR', 1) if $lc eq 'nchar'; | ||
| 139 | return 'CLOB' if $lc eq 'text' || $lc eq 'long'; | ||
| 140 | |||
| 141 | # Date/time types | ||
| 142 | return 'DATE' if $lc eq 'date'; | ||
| 143 | return 'TIMESTAMP' if $lc eq 'timestamp' || $lc eq 'datetime'; | ||
| 144 | return 'TIMESTAMP WITH TIME ZONE' if $lc eq 'timestamptz' || $lc eq 'timestamp with time zone'; | ||
| 145 | |||
| 146 | # Binary types | ||
| 147 | return 'BLOB' if $lc eq 'bytea' || $lc eq 'blob'; | ||
| 148 | return 'CLOB' if $lc eq 'clob'; | ||
| 149 | |||
| 150 | # Boolean | ||
| 151 | return 'NUMBER(1)' if $lc eq 'boolean'; | ||
| 152 | |||
| 153 | # Float/double | ||
| 154 | return 'BINARY_FLOAT' if $lc eq 'real'; | ||
| 155 | return 'BINARY_DOUBLE' if $lc eq 'float' || $lc eq 'double precision'; | ||
| 156 | |||
| 157 | # Numeric/decimal — size may be a scalar precision or [precision, scale] | ||
| 158 | return $sized->('NUMBER') if $lc eq 'numeric' || $lc eq 'decimal'; | ||
| 159 | |||
| 160 | # Fallback — pass through uppercase | ||
| 161 | return uc $type; | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; | ||
| 165 | |||
| 166 | __END__ | ||
| 167 | |||
| 168 | =pod | ||
| 169 | |||
| 170 | =encoding UTF-8 | ||
| 171 | |||
| 172 | =head1 NAME | ||
| 173 | |||
| 174 | DBIO::Oracle::Type - Oracle type mapping utilities | ||
| 175 | |||
| 176 | =head1 VERSION | ||
| 177 | |||
| 178 | version 0.900001 | ||
| 179 | |||
| 180 | =head1 DESCRIPTION | ||
| 181 | |||
| 182 | Consolidated Oracle type mapping logic used by L<DBIO::Oracle::Storage>, | ||
| 183 | L<DBIO::Oracle::DDL>, L<DBIO::Oracle::Introspect::Columns>, and | ||
| 184 | L<DBIO::Oracle::Diff::Column>. | ||
| 185 | |||
| 186 | =func is_lob_type | ||
| 187 | |||
| 188 | $self->is_lob_type($data_type) | ||
| 189 | |||
| 190 | Returns true if the given data-type string names a LOB type. Operates on the | ||
| 191 | plain C<data_type> string carried by a bind's C<sqlt_datatype> (e.g. C<'clob'>, | ||
| 192 | C<'blob'>, C<'text'>), matching the semantics of the upstream | ||
| 193 | L<DBIx::Class::Storage::DBI/_is_lob_type>. The leading C<$self> is ignored; it | ||
| 194 | exists so the function can be called as a method via the storage wrapper. | ||
| 195 | |||
| 196 | =func is_text_lob_type | ||
| 197 | |||
| 198 | $self->is_text_lob_type($data_type) | ||
| 199 | |||
| 200 | Returns true if the data-type string names a text LOB (CLOB/NCLOB), false for a | ||
| 201 | binary LOB. Mirrors L<DBIx::Class::Storage::DBI/_is_text_lob_type>. | ||
| 202 | |||
| 203 | =func oracle_lob_bind_attrs | ||
| 204 | |||
| 205 | DBIO::Oracle::Type::oracle_lob_bind_attrs($is_text) | ||
| 206 | |||
| 207 | Returns the bind attributes for a LOB bind (ora_type => ORA_BLOB or ORA_CLOB). | ||
| 208 | |||
| 209 | =func normalize_type | ||
| 210 | |||
| 211 | DBIO::Oracle::Type::normalize_type($type) | ||
| 212 | |||
| 213 | Normalizes an Oracle data type string for comparison (uppercase, collapse | ||
| 214 | whitespace). | ||
| 215 | |||
| 216 | =func map_dbd_type_to_dbio | ||
| 217 | |||
| 218 | DBIO::Oracle::Type::map_dbd_type_to_dbio($data_type, %args) | ||
| 219 | |||
| 220 | Maps an Oracle data type string (from ALL_TAB_COLUMNS) to a DBIO canonical | ||
| 221 | type name. %args may include C<nchar_size_factor> for UTF-16 correction. | ||
| 222 | |||
| 223 | Returns a hashref with keys: C<data_type>, C<size>, C<not_null>, etc. | ||
| 224 | |||
| 225 | =func map_dbio_type_to_oracle | ||
| 226 | |||
| 227 | DBIO::Oracle::Type::map_dbio_type_to_oracle($dbio_type, %args) | ||
| 228 | |||
| 229 | Maps a DBIO canonical type name to an Oracle DDL type string. | ||
| 230 | %args may include C<size> for VARCHAR2(n) etc. | ||
| 231 | |||
| 232 | =head1 AUTHOR | ||
| 233 | |||
| 234 | DBIO & DBIx::Class Authors | ||
| 235 | |||
| 236 | =head1 COPYRIGHT AND LICENSE | ||
| 237 | |||
| 238 | Copyright (C) 2026 DBIO Authors | ||
| 239 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||
| 240 | Based on DBIx::Class, heavily modified. | ||
| 241 | |||
| 242 | This is free software; you can redistribute it and/or modify it under | ||
| 243 | the same terms as the Perl 5 programming language system itself. | ||
| 244 | |||
| 245 | =cut | ||