X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSource.pm;h=2874611cd3f2d229b8be4d289de4b7712442d23a;hb=f9080e4502d65c87e9c2486ca6a76b166cf1ca8f;hp=83f4c8eae8702b14b37a76e4dd76633779444e96;hpb=5e35b386e5db4433e028395819ed80924227417c;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 83f4c8e..2874611 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -6,8 +6,8 @@ use warnings; use DBIx::Class::ResultSet; use DBIx::Class::ResultSourceHandle; -use DBIx::Class::Exception; use DBIx::Class::Carp; +use Devel::GlobalDestruction; use Try::Tiny; use List::Util 'first'; use Scalar::Util qw/blessed weaken isweak/; @@ -15,13 +15,19 @@ use namespace::clean; use base qw/DBIx::Class/; -__PACKAGE__->mk_group_accessors('simple' => qw/_ordered_columns - _columns _primaries _unique_constraints name resultset_attributes - from _relationships column_info_from_storage source_info - source_name sqlt_deploy_callback/); +__PACKAGE__->mk_group_accessors(simple => qw/ + source_name name source_info + _ordered_columns _columns _primaries _unique_constraints + _relationships resultset_attributes + column_info_from_storage +/); -__PACKAGE__->mk_group_accessors('component_class' => qw/resultset_class - result_class/); +__PACKAGE__->mk_group_accessors(component_class => qw/ + resultset_class + result_class +/); + +__PACKAGE__->mk_classdata( sqlt_deploy_callback => 'default_sqlt_deploy_hook' ); =head1 NAME @@ -87,7 +93,7 @@ You can retrieve the result source at runtime in the following ways: $schema->source($source_name); -=item From a Row object: +=item From a Result object: $row->result_source; @@ -115,7 +121,6 @@ sub new { $new->{_relationships} = { %{$new->{_relationships}||{}} }; $new->{name} ||= "!!NAME NOT SET!!"; $new->{_columns_info_loaded} ||= 0; - $new->{sqlt_deploy_callback} ||= "default_sqlt_deploy_hook"; return $new; } @@ -127,7 +132,7 @@ sub new { =item Arguments: @columns -=item Return value: The ResultSource object +=item Return Value: L<$result_source|/new> =back @@ -140,7 +145,7 @@ pairs, uses the hashref as the L for that column. Repeated calls of this method will add more columns, not replace them. The column names given will be created as accessor methods on your -L objects. You can change the name of the accessor +L objects. You can change the name of the accessor by supplying an L in the column_info hash. If a column name beginning with a plus sign ('+col1') is provided, the @@ -293,7 +298,7 @@ L. =item Arguments: $colname, \%columninfo? -=item Return value: 1/0 (true/false) +=item Return Value: 1/0 (true/false) =back @@ -337,7 +342,7 @@ sub add_column { shift->add_columns(@_); } # DO NOT CHANGE THIS TO GLOB =item Arguments: $colname -=item Return value: 1/0 (true/false) +=item Return Value: 1/0 (true/false) =back @@ -358,7 +363,7 @@ sub has_column { =item Arguments: $colname -=item Return value: Hashref of info +=item Return Value: Hashref of info =back @@ -406,9 +411,9 @@ sub column_info { =over -=item Arguments: None +=item Arguments: none -=item Return value: Ordered list of column names +=item Return Value: Ordered list of column names =back @@ -432,7 +437,7 @@ sub columns { =item Arguments: \@colnames ? -=item Return value: Hashref of column name/info pairs +=item Return Value: Hashref of column name/info pairs =back @@ -506,7 +511,7 @@ sub columns_info { =item Arguments: @colnames -=item Return value: undefined +=item Return Value: not defined =back @@ -524,7 +529,7 @@ broken result source. =item Arguments: $colname -=item Return value: undefined +=item Return Value: not defined =back @@ -562,7 +567,7 @@ sub remove_column { shift->remove_columns(@_); } # DO NOT CHANGE THIS TO GLOB =item Arguments: @cols -=item Return value: undefined +=item Return Value: not defined =back @@ -596,9 +601,9 @@ sub set_primary_key { =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: Ordered list of primary column names +=item Return Value: Ordered list of primary column names =back @@ -635,7 +640,7 @@ will be applied to the L of each L =item Arguments: $sequence_name -=item Return value: undefined +=item Return Value: not defined =back @@ -658,7 +663,7 @@ sub sequence { =item Arguments: $name?, \@colnames -=item Return value: undefined +=item Return Value: not defined =back @@ -724,7 +729,7 @@ sub add_unique_constraint { =item Arguments: @constraints -=item Return value: undefined +=item Return Value: not defined =back @@ -776,7 +781,7 @@ sub add_unique_constraints { =item Arguments: \@colnames -=item Return value: Constraint name +=item Return Value: Constraint name =back @@ -810,9 +815,9 @@ sub name_unique_constraint { =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: Hash of unique constraint data +=item Return Value: Hash of unique constraint data =back @@ -834,9 +839,9 @@ sub unique_constraints { =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: Unique constraint names +=item Return Value: Unique constraint names =back @@ -860,7 +865,7 @@ sub unique_constraint_names { =item Arguments: $constraintname -=item Return value: List of constraint columns +=item Return Value: List of constraint columns =back @@ -886,12 +891,21 @@ sub unique_constraint_columns { =over -=item Arguments: $callback +=item Arguments: $callback_name | \&callback_code + +=item Return Value: $callback_name | \&callback_code =back __PACKAGE__->sqlt_deploy_callback('mycallbackmethod'); + or + + __PACKAGE__->sqlt_deploy_callback(sub { + my ($source_instance, $sqlt_table) = @_; + ... + } ); + An accessor to set a callback to be called during deployment of the schema via L or L. @@ -899,7 +913,7 @@ L. The callback can be set as either a code reference or the name of a method in the current result class. -If not set, the L is called. +Defaults to L. Your callback will be passed the $source object representing the ResultSource instance being deployed, and the @@ -919,19 +933,13 @@ and call L. =head2 default_sqlt_deploy_hook -=over - -=item Arguments: $source, $sqlt_table - -=item Return value: undefined - -=back - -This is the sensible default for L. - -If a method named C exists in your Result class, it -will be called and passed the current C<$source> and the -C<$sqlt_table> being deployed. +This is the default deploy hook implementation which checks if your +current Result class has a C method, and if present +invokes it B. This is to preserve the +semantics of C which was originally designed to expect +the Result class name and the +L<$sqlt_table instance|SQL::Translator::Schema::Table> of the table being +deployed. =cut @@ -940,8 +948,8 @@ sub default_sqlt_deploy_hook { my $class = $self->result_class; - if ($class and my $hook = $class->can('sqlt_deploy_hook')) { - $self->$hook(@_); + if ($class and $class->can('sqlt_deploy_hook')) { + $class->sqlt_deploy_hook(@_); } } @@ -952,13 +960,39 @@ sub _invoke_sqlt_deploy_hook { } } +=head2 result_class + +=over 4 + +=item Arguments: $classname + +=item Return Value: $classname + +=back + + use My::Schema::ResultClass::Inflator; + ... + + use My::Schema::Artist; + ... + __PACKAGE__->result_class('My::Schema::ResultClass::Inflator'); + +Set the default result class for this source. You can use this to create +and use your own result inflator. See L +for more details. + +Please note that setting this to something like +L will make every result unblessed +and make life more difficult. Inflators like those are better suited to +temporary usage via L. + =head2 resultset =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: $resultset +=item Return Value: L<$resultset|DBIx::Class::ResultSet> =back @@ -975,7 +1009,7 @@ but is cached from then on unless resultset_class changes. =item Arguments: $classname -=item Return value: $classname +=item Return Value: $classname =back @@ -999,9 +1033,9 @@ exists. =over 4 -=item Arguments: \%attrs +=item Arguments: L<\%attrs|DBIx::Class::ResultSet/ATTRIBUTES> -=item Return value: \%attrs +=item Return Value: L<\%attrs|DBIx::Class::ResultSet/ATTRIBUTES> =back @@ -1012,8 +1046,35 @@ exists. $source->resultset_attributes({ order_by => [ 'id' ] }); Store a collection of resultset attributes, that will be set on every -L produced from this result source. For a full -list see L. +L produced from this result source. + +B: C comes with its own set of issues and +bugs! While C isn't deprecated per se, its usage is +not recommended! + +Since relationships use attributes to link tables together, the "default" +attributes you set may cause unpredictable and undesired behavior. Furthermore, +the defaults cannot be turned off, so you are stuck with them. + +In most cases, what you should actually be using are project-specific methods: + + package My::Schema::ResultSet::Artist; + use base 'DBIx::Class::ResultSet'; + ... + + # BAD IDEA! + #__PACKAGE__->resultset_attributes({ prefetch => 'tracks' }); + + # GOOD IDEA! + sub with_tracks { shift->search({}, { prefetch => 'tracks' }) } + + # in your code + $schema->resultset('Artist')->with_tracks->... + +This gives you the flexibility of not using it when you don't need it. + +For more complex situations, another solution would be to use a virtual view +via L. =cut @@ -1033,6 +1094,20 @@ sub resultset { ); } +=head2 name + +=over 4 + +=item Arguments: none + +=item Result value: $name + +=back + +Returns the name of the result source, which will typically be the table +name. This may be a scalar reference if the result source has a non-standard +name. + =head2 source_name =over 4 @@ -1059,9 +1134,9 @@ its class name. =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: FROM clause +=item Return Value: FROM clause =back @@ -1071,13 +1146,17 @@ Returns an expression of the source to be supplied to storage to specify retrieval from this source. In the case of a database, the required FROM clause contents. +=cut + +sub from { die 'Virtual method!' } + =head2 schema =over 4 -=item Arguments: $schema +=item Arguments: L<$schema?|DBIx::Class::Schema> -=item Return value: A schema object +=item Return Value: L<$schema|DBIx::Class::Schema> =back @@ -1111,17 +1190,15 @@ sub schema { =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: A Storage object +=item Return Value: L<$storage|DBIx::Class::Storage> =back $source->storage->debug(1); -Returns the storage handle for the current schema. - -See also: L +Returns the L for the current schema. =cut @@ -1131,13 +1208,13 @@ sub storage { shift->schema->storage; } =over 4 -=item Arguments: $relname, $related_source_name, \%cond, [ \%attrs ] +=item Arguments: $rel_name, $related_source_name, \%cond, \%attrs? -=item Return value: 1/true if it succeeded +=item Return Value: 1/true if it succeeded =back - $source->add_relationship('relname', 'related_source', $cond, $attrs); + $source->add_relationship('rel_name', 'related_source', $cond, $attrs); L describes a series of methods which create pre-defined useful types of relationships. Look there first @@ -1257,9 +1334,9 @@ sub add_relationship { =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: List of relationship names +=item Return Value: L<@rel_names|DBIx::Class::Relationship> =back @@ -1277,29 +1354,29 @@ sub relationships { =over 4 -=item Arguments: $relname +=item Arguments: L<$rel_name|DBIx::Class::Relationship> -=item Return value: Hashref of relation data, +=item Return Value: L<\%rel_data|DBIx::Class::Relationship::Base/add_relationship> =back Returns a hash of relationship information for the specified relationship -name. The keys/values are as specified for L. +name. The keys/values are as specified for L. =cut sub relationship_info { - my ($self, $rel) = @_; - return $self->_relationships->{$rel}; + #my ($self, $rel) = @_; + return shift->_relationships->{+shift}; } =head2 has_relationship =over 4 -=item Arguments: $rel +=item Arguments: L<$rel_name|DBIx::Class::Relationship> -=item Return value: 1/0 (true/false) +=item Return Value: 1/0 (true/false) =back @@ -1308,17 +1385,17 @@ Returns true if the source has a relationship of this name, false otherwise. =cut sub has_relationship { - my ($self, $rel) = @_; - return exists $self->_relationships->{$rel}; + #my ($self, $rel) = @_; + return exists shift->_relationships->{+shift}; } =head2 reverse_relationship_info =over 4 -=item Arguments: $relname +=item Arguments: L<$rel_name|DBIx::Class::Relationship> -=item Return value: Hashref of relationship data +=item Return Value: L<\%rel_data|DBIx::Class::Relationship::Base/add_relationship> =back @@ -1422,6 +1499,32 @@ sub _compare_relationship_keys { ; } +# optionally takes either an arrayref of column names, or a hashref of already +# retrieved colinfos +# returns an arrayref of column names of the shortest unique constraint +# (matching some of the input if any), giving preference to the PK +sub _identifying_column_set { + my ($self, $cols) = @_; + + my %unique = $self->unique_constraints; + my $colinfos = ref $cols eq 'HASH' ? $cols : $self->columns_info($cols||()); + + # always prefer the PK first, and then shortest constraints first + USET: + for my $set (delete $unique{primary}, sort { @$a <=> @$b } (values %unique) ) { + next unless $set && @$set; + + for (@$set) { + next USET unless ($colinfos->{$_} && !$colinfos->{$_}{is_nullable} ); + } + + # copy so we can mangle it at will + return [ @$set ]; + } + + return undef; +} + # Returns the {from} structure used to express JOIN conditions sub _resolve_join { my ($self, $join, $alias, $seen, $jpath, $parent_force_left) = @_; @@ -1435,7 +1538,7 @@ sub _resolve_join { $jpath = [@$jpath]; # copy - if (not defined $join) { + if (not defined $join or not length $join) { return (); } elsif (ref $join eq 'ARRAY') { @@ -1498,7 +1601,7 @@ sub _resolve_join { -alias => $as, -relation_chain_depth => $seen->{-relation_chain_depth} || 0, }, - $self->_resolve_condition($rel_info->{cond}, $as, $alias, $join) + scalar $self->_resolve_condition($rel_info->{cond}, $as, $alias, $join) ]; } } @@ -1513,9 +1616,9 @@ sub pk_depends_on { # having already been inserted. Takes the name of the relationship and a # hashref of columns of the related object. sub _pk_depends_on { - my ($self, $relname, $rel_data) = @_; + my ($self, $rel_name, $rel_data) = @_; - my $relinfo = $self->relationship_info($relname); + my $relinfo = $self->relationship_info($rel_name); # don't assume things if the relationship direction is specified return $relinfo->{attrs}{is_foreign_key_constraint} @@ -1530,7 +1633,7 @@ sub _pk_depends_on { # assume anything that references our PK probably is dependent on us # rather than vice versa, unless the far side is (a) defined or (b) # auto-increment - my $rel_source = $self->related_source($relname); + my $rel_source = $self->related_source($rel_name); foreach my $p ($self->primary_columns) { if (exists $keyhash->{$p}) { @@ -1558,7 +1661,7 @@ our $UNRESOLVABLE_CONDITION = \ '1 = 0'; # list of non-triviail values (notmally conditions) returned as a part # of a joinfree condition hash sub _resolve_condition { - my ($self, $cond, $as, $for, $relname) = @_; + my ($self, $cond, $as, $for, $rel_name) = @_; my $obj_rel = !!blessed $for; @@ -1569,7 +1672,7 @@ sub _resolve_condition { self_alias => $obj_rel ? $as : $for, foreign_alias => $relalias, self_resultsource => $self, - foreign_relname => $relname || ($obj_rel ? $as : $for), + foreign_relname => $rel_name || ($obj_rel ? $as : $for), self_rowobj => $obj_rel ? $for : undef }); @@ -1578,7 +1681,7 @@ sub _resolve_condition { # FIXME sanity check until things stabilize, remove at some point $self->throw_exception ( - "A join-free condition returned for relationship '$relname' without a row-object to chain from" + "A join-free condition returned for relationship '$rel_name' without a row-object to chain from" ) unless $obj_rel; # FIXME another sanity check @@ -1588,7 +1691,7 @@ sub _resolve_condition { first { $_ !~ /^\Q$relalias.\E.+/ } keys %$joinfree_cond ) { $self->throw_exception ( - "The join-free condition returned for relationship '$relname' must be a hash " + "The join-free condition returned for relationship '$rel_name' must be a hash " .'reference with all keys being valid columns on the related result source' ); } @@ -1605,7 +1708,7 @@ sub _resolve_condition { } # see which parts of the joinfree cond are conditionals - my $relcol_list = { map { $_ => 1 } $self->related_source($relname)->columns }; + my $relcol_list = { map { $_ => 1 } $self->related_source($rel_name)->columns }; for my $c (keys %$joinfree_cond) { my ($colname) = $c =~ /^ (?: \Q$relalias.\E )? (.+)/x; @@ -1682,14 +1785,14 @@ sub _resolve_condition { elsif (ref $cond eq 'ARRAY') { my (@ret, $crosstable); for (@$cond) { - my ($cond, $crosstab) = $self->_resolve_condition($_, $as, $for, $relname); + my ($cond, $crosstab) = $self->_resolve_condition($_, $as, $for, $rel_name); push @ret, $cond; $crosstable ||= $crosstab; } return wantarray ? (\@ret, $crosstable) : \@ret; } else { - $self->throw_exception ("Can't handle condition $cond for relationship '$relname' yet :("); + $self->throw_exception ("Can't handle condition $cond for relationship '$rel_name' yet :("); } } @@ -1697,12 +1800,11 @@ sub _resolve_condition { # array of column names for each of those relationships. Column names are # prefixed relative to the current source, in accordance with where they appear # in the supplied relationships. - sub _resolve_prefetch { my ($self, $pre, $alias, $alias_map, $order, $collapse, $pref_path) = @_; $pref_path ||= []; - if (not defined $pre) { + if (not defined $pre or not length $pre) { return (); } elsif( ref $pre eq 'ARRAY' ) { @@ -1805,9 +1907,9 @@ sub _resolve_prefetch { =over 4 -=item Arguments: $relname +=item Arguments: $rel_name -=item Return value: $source +=item Return Value: $source =back @@ -1838,9 +1940,9 @@ sub related_source { =over 4 -=item Arguments: $relname +=item Arguments: $rel_name -=item Return value: $classname +=item Return Value: $classname =back @@ -1860,9 +1962,9 @@ sub related_class { =over 4 -=item Arguments: None +=item Arguments: none -=item Return value: $source_handle +=item Return Value: L<$source_handle|DBIx::Class::ResultSourceHandle> =back @@ -1885,16 +1987,9 @@ sub handle { }); } -{ - my $global_phase_destroy; - - # SpeedyCGI runs END blocks every cycle but keeps object instances - # hence we have to disable the globaldestroy hatch, and rely on the - # eval trap below (which appears to work, but is risky done so late) - END { $global_phase_destroy = 1 unless $CGI::SpeedyCGI::i_am_speedy } - - sub DESTROY { - return if $global_phase_destroy; +my $global_phase_destroy; +sub DESTROY { + return if $global_phase_destroy ||= in_global_destruction; ###### # !!! ACHTUNG !!!! @@ -1906,25 +2001,21 @@ sub handle { # we are trying to save to reattach back to the source we are destroying. # The relevant code checking refcounts is in ::Schema::DESTROY() - # if we are not a schema instance holder - we don't matter - return if( - ! ref $_[0]->{schema} - or - isweak $_[0]->{schema} - ); - - # weaken our schema hold forcing the schema to find somewhere else to live - # during global destruction (if we have not yet bailed out) this will throw - # which will serve as a signal to not try doing anything else - local $@; - eval { - weaken $_[0]->{schema}; - 1; - } or do { - $global_phase_destroy = 1; - return; - }; + # if we are not a schema instance holder - we don't matter + return if( + ! ref $_[0]->{schema} + or + isweak $_[0]->{schema} + ); + # weaken our schema hold forcing the schema to find somewhere else to live + # during global destruction (if we have not yet bailed out) this will throw + # which will serve as a signal to not try doing anything else + # however beware - on older perls the exception seems randomly untrappable + # due to some weird race condition during thread joining :((( + local $@; + eval { + weaken $_[0]->{schema}; # if schema is still there reintroduce ourselves with strong refs back to us if ($_[0]->{schema}) { @@ -1934,7 +2025,13 @@ sub handle { $srcregs->{$_} = $_[0] if $srcregs->{$_} == $_[0]; } } - } + + 1; + } or do { + $global_phase_destroy = 1; + }; + + return; } sub STORABLE_freeze { Storable::nfreeze($_[0]->handle) } @@ -1984,7 +2081,7 @@ Creates a new ResultSource object. Not normally called directly by end users. =item Arguments: 1/0 (default: 0) -=item Return value: 1/0 +=item Return Value: 1/0 =back @@ -1995,9 +2092,9 @@ metadata from storage as necessary. This is *deprecated*, and should not be used. It will be removed before 1.0. -=head1 AUTHORS +=head1 AUTHOR AND CONTRIBUTORS -Matt S. Trout +See L and L in DBIx::Class =head1 LICENSE