resultsets
- MSSQL limits now don't require nearly as many applications of
the unsafe_subselect_ok attribute, due to optimized queries
+ - Fix as_subselect_rs to not inject resultset class-wide where
+ conditions outside of the resulting subquery
- Depend on optimized SQL::Abstract (faster SQL generation)
+ - update on row not in database now OK if no changes -
+ fixes problems with cascaded unnecessary updates
+
0.08121 2010-04-11 18:43:00 (UTC)
- Support for Firebird RDBMS with DBD::InterBase and ODBC
# This is to be called by the author only (automatically in Makefile.PL)
sub _gen_pod {
+
my $class = shift;
my $modfn = __PACKAGE__ . '.pm';
$modfn =~ s/\:\:/\//g;
- require DBIx::Class;
- my $distver = DBIx::Class->VERSION;
+ my $podfn = __FILE__;
+ $podfn =~ s/\.pm$/\.pod/;
+
+ my $distver =
+ eval { require DBIx::Class; DBIx::Class->VERSION; }
+ ||
+ do {
+ warn
+"\n\n---------------------------------------------------------------------\n" .
+'Unable to load core DBIx::Class module to determine current version, '.
+'possibly due to missing dependencies. Author-mode autodocumentation ' .
+"halted\n\n" . $@ .
+"\n\n---------------------------------------------------------------------\n"
+ ;
+ '*UNKNOWN*'; # rv
+ }
+ ;
+
my $sqltver = $class->req_list_for ('deploy')->{'SQL::Translator'}
or die "Hrmm? No sqlt dep?";
'You may distribute this code under the same terms as Perl itself',
);
- my $fn = __FILE__;
- $fn =~ s/\.pm$/\.pod/;
-
- open (my $fh, '>', $fn) or croak "Unable to write to $fn: $!";
+ open (my $fh, '>', $podfn) or croak "Unable to write to $podfn: $!";
print $fh join ("\n\n", @chunks);
close ($fh);
}
should, set this attribute to a true or false value to override the detection
of when to create constraints.
+=item cascade_copy
+
+If C<cascade_copy> is true on a C<has_many> relationship for an
+object, then when you copy the object all the related objects will
+be copied too. To turn this behaviour off, pass C<< cascade_copy => 0 >>
+in the C<$attr> hashref.
+
+The behaviour defaults to C<< cascade_copy => 1 >> for C<has_many>
+relationships.
+
+=item cascade_delete
+
+By default, DBIx::Class cascades deletes across C<has_many>,
+C<has_one> and C<might_have> relationships. You can disable this
+behaviour on a per-relationship basis by supplying
+C<< cascade_delete => 0 >> in the relationship attributes.
+
+The cascaded operations are performed after the requested delete,
+so if your database has a constraint on the relationship, it will
+have deleted/updated the related records or raised an exception
+before DBIx::Class gets to perform the cascaded operation.
+
+=item cascade_update
+
+By default, DBIx::Class cascades updates across C<has_one> and
+C<might_have> relationships. You can disable this behaviour on a
+per-relationship basis by supplying C<< cascade_update => 0 >> in
+the relationship attributes.
+
+This is not a RDMS style cascade update - it purely means that when
+an object has update called on it, all the related objects also
+have update called. It will not change foreign keys automatically -
+you must arrange to do this yourself.
+
=item on_delete / on_update
If you are using L<SQL::Translator> to create SQL for you, you can use these
$sub_attrs->{select} = @pcols ? \@pcols : [ 1 ];
}
-
- # this is so that the query can be simplified e.g.
- # * ordering can be thrown away in things like Top limit
- $sub_attrs->{-for_count_only} = 1;
-
return $rsrc->resultset_class
->new ($rsrc, $sub_attrs)
->as_subselect_rs
my $attrs = $self->_resolved_attrs;
- return $self->result_source->resultset->search( undef, {
+ my $fresh_rs = (ref $self)->new (
+ $self->result_source
+ );
+
+ # these pieces will be locked in the subquery
+ delete $fresh_rs->{cond};
+ delete @{$fresh_rs->{attrs}}{qw/where bind/};
+
+ return $fresh_rs->search( {}, {
from => [{
$attrs->{alias} => $self->as_query,
-alias => $attrs->{alias},
-source_handle => $self->result_source->handle,
}],
- map { $_ => $attrs->{$_} } qw/select as alias/
-
- });
+ alias => $attrs->{alias},
+ });
}
# This code is called by search_related, and makes sure there
sub update {
my ($self, $upd) = @_;
- $self->throw_exception( "Not in database" ) unless $self->in_storage;
my $ident_cond = $self->{_orig_ident} || $self->ident_condition;
- $self->throw_exception('Unable to update a row with incomplete or no identity')
- if ! keys %$ident_cond;
-
$self->set_inflated_columns($upd) if $upd;
my %to_update = $self->get_dirty_columns;
return $self unless keys %to_update;
+
+ $self->throw_exception( "Not in database" ) unless $self->in_storage;
+
+ $self->throw_exception('Unable to update a row with incomplete or no identity')
+ if ! keys %$ident_cond;
+
my $rows = $self->result_source->storage->update(
$self->result_source, \%to_update, $ident_cond
);
$table = $self->_quote($table);
}
- local $self->{rownum_hack_count} = 1
- if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
@rest = (-1) unless defined $rest[0];
croak "LIMIT 0 Does Not Compute" if $rest[0] == 0;
# and anyway, SQL::Abstract::Limit will cause a barf if we don't first
my $sql = '';
- if (my $g = $self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }) ) {
+ if (my $g = $self->_recurse_fields($arg->{group_by}) ) {
$sql .= $self->_sqlcase(' group by ') . $g;
}
my %info;
- my $server_version = $self->_get_server_version;
+ my $server_version = do {
+ local $@; # might be happenin in some sort of destructor
+ eval { $self->_get_server_version };
+ };
if (defined $server_version) {
$info{dbms_version} = $server_version;
}
sub _get_server_version {
- eval { shift->_get_dbh->get_info(18) };
+ shift->_get_dbh->get_info(18);
}
sub _determine_driver {
unless ($self->_sql_maker) {
unless ($self->{_sql_maker_opts}{limit_dialect}) {
+ my $have_rno = 0;
- my $version = $self->_server_info->{normalized_dbms_version} || 0;
+ if (exists $self->_server_info->{normalized_dbms_version}) {
+ $have_rno = 1 if $self->_server_info->{normalized_dbms_version} >= 9;
+ }
+ else {
+ # User is connecting via DBD::Sybase and has no permission to run
+ # stored procedures like xp_msver, or version detection failed for some
+ # other reason.
+ # So, we use a query to check if RNO is implemented.
+ $have_rno = 1 if (eval { local $@; ($self->_get_dbh
+ ->selectrow_array('SELECT row_number() OVER (ORDER BY rand())')
+ )[0] });
+ }
$self->{_sql_maker_opts} = {
- limit_dialect => ($version >= 9 ? 'RowNumberOver' : 'Top'),
+ limit_dialect => ($have_rno ? 'RowNumberOver' : 'Top'),
%{$self->{_sql_maker_opts}||{}}
};
}
is(scalar(keys(%fake_dirty)), 1, '1 fake dirty column');
ok(grep($_ eq 'name', keys(%fake_dirty)), 'name is fake dirty');
+ok($art->update, 'Update run');
+
my $record_jp = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => 'cds' })->next;
ok($record_jp, "prefetch on same rel okay");
is($art->in_storage, 0, "It knows it's dead");
+lives_ok { $art->update } 'No changes so update should be OK';
+
dies_ok ( sub { $art->delete }, "Can't delete twice");
is($art->name, 'We Are In Rehab', 'But the object is still live');
is $rs->first, undef, 'rolled back';
$rs->reset;
+
+ # test RNO detection when version detection fails
+ SKIP: {
+ my $storage = $schema->storage;
+ my $version = $storage->_server_info->{normalized_dbms_version};
+
+ skip 1, 'could not detect SQL Server version' if not defined $version;
+
+ my $have_rno = $version >= 9 ? 1 : 0;
+
+ # Delete version information to force RNO check when rebuilding SQLA
+ # instance.
+ no strict 'refs';
+ no warnings 'redefine';
+ local *{(ref $storage).'::_get_server_version'} = sub { undef };
+
+ my $server_info = { %{ $storage->_server_info_hash } }; # clone
+
+ delete @$server_info{qw/dbms_version normalized_dbms_version/};
+
+ local $storage->{_server_info_hash} = $server_info;
+ local $storage->{_sql_maker} = undef;
+ local $storage->{_sql_maker_opts} = undef;
+
+ $storage->sql_maker;
+
+ my $rno_detected =
+ ($storage->{_sql_maker_opts}{limit_dialect} eq 'RowNumberOver');
+
+ ok ((not ($have_rno xor $rno_detected)),
+ 'row_number() over support detected correctly');
+ }
}
# test op-induced autoconnect
'... and chaining off the virtual view works';
dies_ok { $new_rs->as_subselect_rs->search({'artwork_to_artist.artwork_cd_id'=> 1})->count }
q{... but chaining off of a virtual view using join doesn't work};
+
+my $book_rs = $schema->resultset ('BooksInLibrary')->search ({}, { join => 'owner' });
+
+is_same_sql_bind (
+ $book_rs->as_subselect_rs->as_query,
+ '(SELECT me.id, me.source, me.owner, me.title, me.price
+ FROM (
+ SELECT me.id, me.source, me.owner, me.title, me.price
+ FROM books me
+ JOIN owners owner ON owner.id = me.owner
+ WHERE ( source = ? )
+ ) me
+ )',
+ [ [ source => 'Library' ] ],
+ 'Resultset-class attributes do not seep outside of the subselect',
+);
+
done_testing;