| 1 | package DBIO::Oracle::Storage::LOBSupport; | ||
| 2 | # ABSTRACT: LOB binding and chunking for Oracle | ||
| 3 | |||
| 4 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 4 | ▶ | use Carp qw(carp); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 4 | ▶ | use DBD::Oracle; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use DBIO::Oracle::Type (); | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | sub _dbi_attrs_for_bind { | ||
| 14 | my ($self, $ident, $bind) = @_; | ||
| 15 | |||
| 16 | my $attrs = $self->next::method($ident, $bind); | ||
| 17 | |||
| 18 | $attrs->[$_] | ||
| 19 | and keys %{ $attrs->[$_] } | ||
| 20 | and $bind->[$_][0]{dbic_colname} | ||
| 21 | and $attrs->[$_] = { %{$attrs->[$_]}, ora_field => $bind->[$_][0]{dbic_colname} } | ||
| 22 | for 0 .. $#$attrs; | ||
| 23 | |||
| 24 | $attrs; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub bind_attribute_by_data_type { | ||
| 28 | my ($self, $dt) = @_; | ||
| 29 | |||
| 30 | if ($self->_is_lob_type($dt)) { | ||
| 31 | unless ($DBD::Oracle::__DBIO_DBD_VERSION_CHECK_OK__) { | ||
| 32 | if ($DBD::Oracle::VERSION eq '1.23') { | ||
| 33 | $self->throw_exception( | ||
| 34 | "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later version " | ||
| 35 | . "(https://rt.cpan.org/Public/Bug/Display.html?id=46016)" | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | $DBD::Oracle::__DBIO_DBD_VERSION_CHECK_OK__ = 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | return DBIO::Oracle::Type::oracle_lob_bind_attrs($self->_is_text_lob_type($dt)); | ||
| 42 | } | ||
| 43 | return undef; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _prep_for_execute { | ||
| 47 | my $self = shift; | ||
| 48 | my ($op) = @_; | ||
| 49 | |||
| 50 | return $self->next::method(@_) if $op eq 'insert'; | ||
| 51 | |||
| 52 | my ($sql, $bind) = $self->next::method(@_); | ||
| 53 | |||
| 54 | my $lob_bind_indices = { | ||
| 55 | map { | ||
| 56 | ( | ||
| 57 | $bind->[$_][0]{sqlt_datatype} | ||
| 58 | and | ||
| 59 | $self->_is_lob_type($bind->[$_][0]{sqlt_datatype}) | ||
| 60 | ) ? ( $_ => 1 ) : () | ||
| 61 | } ( 0 .. $#$bind ) | ||
| 62 | }; | ||
| 63 | |||
| 64 | return ($sql, $bind) unless %$lob_bind_indices; | ||
| 65 | |||
| 66 | my ($final_sql, @final_binds); | ||
| 67 | if ($op eq 'update') { | ||
| 68 | $self->throw_exception('Update with complex WHERE clauses involving BLOB columns currently not supported') | ||
| 69 | if $sql =~ /\bWHERE\b .+ \bWHERE\b/xs; | ||
| 70 | |||
| 71 | my $where_sql; | ||
| 72 | ($final_sql, $where_sql) = $sql =~ /^ (.+?) ( \bWHERE\b .+) /xs; | ||
| 73 | |||
| 74 | if (my $set_bind_count = $final_sql =~ y/?//) { | ||
| 75 | delete $lob_bind_indices->{$_} for (0 .. ($set_bind_count - 1)); | ||
| 76 | return ($sql, $bind) unless %$lob_bind_indices; | ||
| 77 | |||
| 78 | @final_binds = splice @$bind, 0, $set_bind_count; | ||
| 79 | $lob_bind_indices = { | ||
| 80 | map { $_ - $set_bind_count => $lob_bind_indices->{$_} } | ||
| 81 | keys %$lob_bind_indices | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | $sql = $where_sql; | ||
| 85 | } | ||
| 86 | elsif ($op ne 'select' and $op ne 'delete') { | ||
| 87 | $self->throw_exception("Unsupported \$op: $op"); | ||
| 88 | } | ||
| 89 | |||
| 90 | my @sql_parts = split /\?/, $sql; | ||
| 91 | my $col_equality_re = qr/ (?<=\s) ([\w."]+) (\s*=\s*) $/x; | ||
| 92 | |||
| 93 | for my $b_idx (0 .. $#$bind) { | ||
| 94 | my $bound = $bind->[$b_idx]; | ||
| 95 | |||
| 96 | if ($lob_bind_indices->{$b_idx} and my ($col, $eq) = $sql_parts[0] =~ $col_equality_re) { | ||
| 97 | my $data = $bound->[1]; | ||
| 98 | $data = "$data" if ref $data; | ||
| 99 | |||
| 100 | my @parts = unpack '(a2000)*', $data; | ||
| 101 | my @sql_frag; | ||
| 102 | |||
| 103 | for my $idx (0..$#parts) { | ||
| 104 | push @sql_frag, sprintf( | ||
| 105 | 'UTL_RAW.CAST_TO_VARCHAR2(RAWTOHEX(DBMS_LOB.SUBSTR(%s, 2000, %d))) = ?', | ||
| 106 | $col, ($idx * 2000 + 1), | ||
| 107 | ); | ||
| 108 | } | ||
| 109 | |||
| 110 | my $sql_frag = '( ' . (join ' AND ', @sql_frag) . ' )'; | ||
| 111 | $sql_parts[0] =~ s/$col_equality_re/$sql_frag/; | ||
| 112 | |||
| 113 | $final_sql .= shift @sql_parts; | ||
| 114 | |||
| 115 | for my $idx (0..$#parts) { | ||
| 116 | push @final_binds, [ | ||
| 117 | { %{ $bound->[0] }, _ora_lob_autosplit_part => $idx, dbd_attrs => undef }, | ||
| 118 | $parts[$idx], | ||
| 119 | ]; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | $final_sql .= shift(@sql_parts) . '?'; | ||
| 124 | push @final_binds, $lob_bind_indices->{$b_idx} | ||
| 125 | ? [{ %{ $bound->[0] }, dbd_attrs => undef }, $bound->[1]] | ||
| 126 | : $bound; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | if (@sql_parts > 1) { | ||
| 131 | carp "There are more placeholders than binds, this should not happen!"; | ||
| 132 | @sql_parts = join('?', @sql_parts); | ||
| 133 | } | ||
| 134 | |||
| 135 | $final_sql .= $sql_parts[0]; | ||
| 136 | return ($final_sql, \@final_binds); | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ | ||
| 142 | |||
| 143 | =pod | ||
| 144 | |||
| 145 | =encoding UTF-8 | ||
| 146 | |||
| 147 | =head1 NAME | ||
| 148 | |||
| 149 | DBIO::Oracle::Storage::LOBSupport - LOB binding and chunking for Oracle | ||
| 150 | |||
| 151 | =head1 VERSION | ||
| 152 | |||
| 153 | version 0.900001 | ||
| 154 | |||
| 155 | =head1 DESCRIPTION | ||
| 156 | |||
| 157 | Handles Oracle LOB binding and chunked comparison logic. | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | DBIO & DBIx::Class Authors | ||
| 162 | |||
| 163 | =head1 COPYRIGHT AND LICENSE | ||
| 164 | |||
| 165 | Copyright (C) 2026 DBIO Authors | ||
| 166 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||
| 167 | Based on DBIx::Class, heavily modified. | ||
| 168 | |||
| 169 | This is free software; you can redistribute it and/or modify it under | ||
| 170 | the same terms as the Perl 5 programming language system itself. | ||
| 171 | |||
| 172 | =cut | ||