X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FOracle%2FGeneric.pm;h=838c8daa603d2b70f0f10aa1ddb1e2a707dfed30;hb=7b731f1eca0005daa869c61a96e48434af5635dc;hp=b9bf0953f67bab8746364d76b1862e29cd29a4ce;hpb=90d7422fc60a3bad71cc67dc20106ef68046664e;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm index b9bf095..838c8da 100644 --- a/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm +++ b/lib/DBIx/Class/Storage/DBI/Oracle/Generic.pm @@ -2,14 +2,20 @@ package DBIx::Class::Storage::DBI::Oracle::Generic; use strict; use warnings; +use base qw/DBIx::Class::Storage::DBI/; +use mro 'c3'; +use DBIx::Class::Carp; use Scope::Guard (); use Context::Preserve 'preserve_context'; -use Try::Tiny; -use List::Util 'first'; +use DBIx::Class::_Util qw( modver_gt_or_eq modver_gt_or_eq_and_lt dbic_internal_try ); use namespace::clean; __PACKAGE__->sql_limit_dialect ('RowNum'); __PACKAGE__->sql_quote_char ('"'); +__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::Oracle'); +__PACKAGE__->datetime_parser_type('DateTime::Format::Oracle'); + +sub __cache_queries_with_max_lob_parts { 2 } =head1 NAME @@ -77,14 +83,6 @@ versions before 9.0. =cut -use base qw/DBIx::Class::Storage::DBI/; -use mro 'c3'; - -__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::Oracle'); -__PACKAGE__->datetime_parser_type('DateTime::Format::Oracle'); - -sub __cache_queries_with_max_lob_parts { 2 } - sub _determine_supports_insert_returning { my $self = shift; @@ -104,9 +102,6 @@ sub deployment_statements { my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; $sqltargs ||= {}; - my $quote_char = $self->schema->storage->sql_maker->quote_char; - $sqltargs->{quote_table_names} = $quote_char ? 1 : 0; - $sqltargs->{quote_field_names} = $quote_char ? 1 : 0; if ( ! exists $sqltargs->{producer_args}{oracle_version} @@ -159,13 +154,13 @@ sub _dbh_get_autoinc_seq { my ( $schema, $table ) = $source_name =~ /( (?:${ql})? \w+ (?:${qr})? ) \. ( (?:${ql})? \w+ (?:${qr})? )/x; # if no explicit schema was requested - use the default schema (which in the case of Oracle is the db user) - $schema ||= uc( ($self->_dbi_connect_info||[])->[1] || ''); + $schema ||= \'= USER'; my ($sql, @bind) = $sql_maker->select ( 'ALL_TRIGGERS', [qw/TRIGGER_BODY TABLE_OWNER TRIGGER_NAME/], { - $schema ? (OWNER => $schema) : (), + OWNER => $schema, TABLE_NAME => $table || $source_name, TRIGGERING_EVENT => { -like => '%INSERT%' }, # this will also catch insert_or_update TRIGGER_TYPE => { -like => '%BEFORE%' }, # we care only about 'before' triggers @@ -175,6 +170,7 @@ sub _dbh_get_autoinc_seq { # to find all the triggers that mention the column in question a simple # regex grep since the trigger_body above is a LONG and hence not searchable + # via -like my @triggers = ( map { my %inf; @inf{qw/body schema name/} = @$_; \%inf } ( grep @@ -183,10 +179,15 @@ sub _dbh_get_autoinc_seq { ) ); - # extract all sequence names mentioned in each trigger - for (@triggers) { - $_->{sequences} = [ $_->{body} =~ / ( "? [\.\w\"\-]+ "? ) \. nextval /xig ]; - } + # extract all sequence names mentioned in each trigger, throw away + # triggers without apparent sequences + @triggers = map { + my @seqs = $_->{body} =~ / ( [\.\w\"\-]+ ) \. nextval /xig; + @seqs + ? { %$_, sequences => \@seqs } + : () + ; + } @triggers; my $chosen_trigger; @@ -198,7 +199,7 @@ sub _dbh_get_autoinc_seq { } else { $self->throw_exception( sprintf ( - "Unable to introspect trigger '%s' for column %s.%s (references multiple sequences). " + "Unable to introspect trigger '%s' for column '%s.%s' (references multiple sequences). " . "You need to specify the correct 'sequence' explicitly in '%s's column_info.", $triggers[0]{name}, $source_name, @@ -220,7 +221,7 @@ sub _dbh_get_autoinc_seq { } else { $self->throw_exception( sprintf ( - "Unable to reliably select a BEFORE INSERT trigger for column %s.%s (possibilities: %s). " + "Unable to reliably select a BEFORE INSERT trigger for column '%s.%s' (possibilities: %s). " . "You need to specify the correct 'sequence' explicitly in '%s's column_info.", $source_name, $col, @@ -241,7 +242,7 @@ sub _dbh_get_autoinc_seq { } $self->throw_exception( sprintf ( - "No suitable BEFORE INSERT triggers found for column %s.%s. " + "No suitable BEFORE INSERT triggers found for column '%s.%s'. " . "You need to specify the correct 'sequence' explicitly in '%s's column_info.", $source_name, $col, @@ -253,8 +254,12 @@ sub _sequence_fetch { my ( $self, $type, $seq ) = @_; # use the maker to leverage quoting settings - my $sql_maker = $self->sql_maker; - my ($id) = $self->_get_dbh->selectrow_array ($sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] ) ); + my $sth = $self->_dbh->prepare_cached( + $self->sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] ) + ); + $sth->execute; + my ($id) = $sth->fetchrow_array; + $sth->finish; return $id; } @@ -266,65 +271,66 @@ sub _ping { local $dbh->{RaiseError} = 1; local $dbh->{PrintError} = 0; - return try { + ( dbic_internal_try { $dbh->do('select 1 from dual'); 1; - } catch { - 0; - }; + }) + ? 1 + : 0 + ; } sub _dbh_execute { - my ($self, $dbh, $sql, @args) = @_; + #my ($self, $dbh, $sql, $bind, $bind_attrs) = @_; + my ($self, $sql, $bind) = @_[0,2,3]; - my (@res, $tried); - my $want = wantarray; - my $next = $self->next::can; - do { - try { - my $exec = sub { - # Turn off sth caching for multi-part LOBs. See _prep_for_execute above. - local $self->{disable_sth_caching} = 1 - if first { - ($_->[0]{_ora_lob_autosplit_part}||0) - > (__cache_queries_with_max_lob_parts-1) - } @{ $args[0] }; - - $self->$next($dbh, $sql, @args) - }; + # Turn off sth caching for multi-part LOBs. See _prep_for_execute below + local $self->{disable_sth_caching} = 1 if grep { + ($_->[0]{_ora_lob_autosplit_part}||0) + > + (__cache_queries_with_max_lob_parts - 1) + } @$bind; - if (!defined $want) { - $exec->(); - } - elsif (! $want) { - $res[0] = $exec->(); - } - else { - @res = $exec->(); - } + my $next = $self->next::can; - $tried++; - } - catch { - if (! $tried and $_ =~ /ORA-01003/) { - # ORA-01003: no statement parsed (someone changed the table somehow, - # invalidating your cursor.) + # if we are already in a txn we can't retry anything + return shift->$next(@_) + if $self->transaction_depth; + + # cheat the blockrunner we are just about to create + # we do want to rerun things regardless of outer state + local $self->{_in_do_block}; + + return DBIx::Class::Storage::BlockRunner->new( + storage => $self, + wrap_txn => 0, + retry_handler => sub { + # ORA-01003: no statement parsed (someone changed the table somehow, + # invalidating your cursor.) + if ( + $_[0]->failed_attempt_count == 1 + and + $_[0]->last_exception =~ /ORA-01003/ + and + my $dbh = $_[0]->storage->_dbh + ) { delete $dbh->{CachedKids}{$sql}; + return 1; } else { - $self->throw_exception($_); + return 0; } - }; - } while (! $tried++); - - return wantarray ? @res : $res[0]; + }, + )->run( $next, @_ ); } -sub _dbh_execute_array { - #my ($self, $sth, $tuple_status, @extra) = @_; +sub _dbh_execute_for_fetch { + #my ($self, $source, $sth, $proto_bind, $cols, $data) = @_; - # DBD::Oracle warns loudly on partial execute_array failures - local $_[1]->{PrintWarn} = 0; + # Older DBD::Oracle warns loudly on partial execute_for_fetch failures + # before https://metacpan.org/source/PYTHIAN/DBD-Oracle-1.28/Changes#L7-9 + local $_[2]->{PrintWarn} = 0 + unless modver_gt_or_eq( 'DBD::Oracle', '1.28' ); shift->next::method(@_); } @@ -415,37 +421,42 @@ sub _dbi_attrs_for_bind { my $attrs = $self->next::method($ident, $bind); - for my $i (0 .. $#$attrs) { - if (keys %{$attrs->[$i]||{}} and my $col = $bind->[$i][0]{dbic_colname}) { - $attrs->[$i]{ora_field} = $col; - } - } + # Push the column name into all bind attrs, make sure to *NOT* write into + # the existing $attrs->[$idx]{..} hashref, as it is cached by the call to + # next::method above. + # FIXME - this code will go away when the LobWriter refactor lands + $attrs->[$_] + and + keys %{ $attrs->[$_] } + and + $bind->[$_][0]{dbic_colname} + and + $attrs->[$_] = { %{$attrs->[$_]}, ora_field => $bind->[$_][0]{dbic_colname} } + for 0 .. $#$attrs; $attrs; } -my $dbd_loaded; sub bind_attribute_by_data_type { my ($self, $dt) = @_; - $dbd_loaded ||= do { - require DBD::Oracle; - if ($DBD::Oracle::VERSION eq '1.23') { - $self->throw_exception( - "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later ". - "version.\n\nSee: https://rt.cpan.org/Public/Bug/Display.html?id=46016\n" - ); - } - 1; - }; - if ($self->_is_lob_type($dt)) { + + # no earlier - no later + $self->throw_exception( + "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later " + . "version (https://rt.cpan.org/Public/Bug/Display.html?id=46016)" + ) if modver_gt_or_eq_and_lt( 'DBD::Oracle', '1.23', '1.24' ); + return { ora_type => $self->_is_text_lob_type($dt) ? DBD::Oracle::ORA_CLOB() : DBD::Oracle::ORA_BLOB() }; } + else { + return undef; + } } # Handle blob columns in WHERE. @@ -486,68 +497,117 @@ sub _prep_for_execute { my $self = shift; my ($op) = @_; + return $self->next::method(@_) + if $op eq 'insert'; + my ($sql, $bind) = $self->next::method(@_); - return ($sql, $bind) if $op ne 'select'; + my $lob_bind_indices = { map { + ( + $bind->[$_][0]{sqlt_datatype} + and + $self->_is_lob_type($bind->[$_][0]{sqlt_datatype}) + ) ? ( $_ => 1 ) : () + } ( 0 .. $#$bind ) }; + + return ($sql, $bind) unless %$lob_bind_indices; + + my ($final_sql, @final_binds); + if ($op eq 'update') { + $self->throw_exception('Update with complex WHERE clauses involving BLOB columns currently not supported') + if $sql =~ /\bWHERE\b .+ \bWHERE\b/xs; - my @sql_part = split /\?/, $sql; - my ($new_sql, @new_binds); + my $where_sql; + ($final_sql, $where_sql) = $sql =~ /^ (.+?) ( \bWHERE\b .+) /xs; - foreach my $bound (@$bind) { - my $data_type = $bound->[0]{sqlt_datatype}||''; + if (my $set_bind_count = $final_sql =~ y/?//) { - if ($self->_is_lob_type($data_type)) { - if (my ($col, $eq) = $sql_part[0] =~ /(?<=\s)([\w."]+)(\s*=\s*)$/) { - my $data = $bound->[1]; + delete $lob_bind_indices->{$_} for (0 .. ($set_bind_count - 1)); - $data = "$data" if ref $data; + # bail if only the update part contains blobs + return ($sql, $bind) unless %$lob_bind_indices; - my @parts = unpack '(a2000)*', $data; + @final_binds = splice @$bind, 0, $set_bind_count; + $lob_bind_indices = { map + { $_ - $set_bind_count => $lob_bind_indices->{$_} } + keys %$lob_bind_indices + }; + } + + # if we got that far - assume the where SQL is all we got + # (the first part is already shoved into $final_sql) + $sql = $where_sql; + } + elsif ($op ne 'select' and $op ne 'delete') { + $self->throw_exception("Unsupported \$op: $op"); + } - my @sql_frag; + my @sql_parts = split /\?/, $sql; - for my $idx (0..$#parts) { - push @sql_frag, -"UTL_RAW.CAST_TO_VARCHAR2(RAWTOHEX(DBMS_LOB.SUBSTR($col, 2000, ".($idx*2000+1)."))) = ?"; - } + my $col_equality_re = qr/ (?<=\s) ([\w."]+) (\s*=\s*) $/x; - my $sql_frag = '( ' . (join ' AND ', @sql_frag) . ' )'; + for my $b_idx (0 .. $#$bind) { + my $bound = $bind->[$b_idx]; - $sql_part[0] =~ s/(?<=\s)([\w."]+)(\s*=\s*)$/$sql_frag/; + if ( + $lob_bind_indices->{$b_idx} + and + my ($col, $eq) = $sql_parts[0] =~ $col_equality_re + ) { + my $data = $bound->[1]; - $new_sql .= shift @sql_part; + $data = "$data" if ref $data; - for my $idx (0..$#parts) { - push @new_binds, [ - { - %{ $bound->[0] }, - _ora_lob_autosplit_part => $idx, - dbd_attrs => undef, - }, - $parts[$idx] - ]; - } + my @parts = unpack '(a2000)*', $data; + + my @sql_frag; + + for my $idx (0..$#parts) { + push @sql_frag, sprintf ( + 'UTL_RAW.CAST_TO_VARCHAR2(RAWTOHEX(DBMS_LOB.SUBSTR(%s, 2000, %d))) = ?', + $col, ($idx*2000 + 1), + ); } - else { - $new_sql .= shift(@sql_part) . '?'; - push @new_binds, [ + my $sql_frag = '( ' . (join ' AND ', @sql_frag) . ' )'; + + $sql_parts[0] =~ s/$col_equality_re/$sql_frag/; + + $final_sql .= shift @sql_parts; + + for my $idx (0..$#parts) { + push @final_binds, [ { %{ $bound->[0] }, + _ora_lob_autosplit_part => $idx, dbd_attrs => undef, }, - $bound->[1], + $parts[$idx] ]; } } else { - $new_sql .= shift(@sql_part) . '?'; - push @new_binds, $bound; + $final_sql .= shift(@sql_parts) . '?'; + push @final_binds, $lob_bind_indices->{$b_idx} + ? [ + { + %{ $bound->[0] }, + dbd_attrs => undef, + }, + $bound->[1], + ] : $bound + ; } } - $new_sql .= join '', @sql_part; - return ($new_sql, \@new_binds); + if (@sql_parts > 1) { + carp "There are more placeholders than binds, this should not happen!"; + @sql_parts = join ('?', @sql_parts); + } + + $final_sql .= $sql_parts[0]; + + return ($final_sql, \@final_binds); } # Savepoints stuff. @@ -575,7 +635,7 @@ Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so the L name is shortened and appended with half of an MD5 hash. -See L. +See L. =cut @@ -586,7 +646,7 @@ sub relname_to_table_alias { my $alias = $self->next::method(@_); # we need to shorten here in addition to the shortening in SQLA itself, - # since the final relnames are a crucial for the join optimizer + # since the final relnames are crucial for the join optimizer return $self->sql_maker->_shorten_identifier($alias); } @@ -644,7 +704,7 @@ and child rows of the hierarchy. # person me # CONNECT BY # parentid = prior persionid - + connect_by_nocycle => { parentid => 'prior personid' } @@ -703,13 +763,16 @@ It uses the same syntax as L # ORDER SIBLINGS BY # firstname ASC -=head1 AUTHOR +=head1 FURTHER QUESTIONS? -See L and L. +Check the list of L. -=head1 LICENSE +=head1 COPYRIGHT AND LICENSE -You may distribute this code under the same terms as Perl itself. +This module is free software L +by the L. You can +redistribute it and/or modify it under the same terms as the +L. =cut