From: David Kamholz Date: Thu, 9 Mar 2006 00:18:03 +0000 (+0000) Subject: more assorted kleenoops X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aeb1bf757122d5260a1d84f17f2d0beec7822165;p=dbsrgits%2FDBIx-Class-Historic.git more assorted kleenoops --- diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index e146b0a..ef25069 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -9,20 +9,17 @@ sub belongs_to { if ($@) { $class->throw_exception($@) unless $@ =~ /Can't locate/; } - - my %f_primaries; - $f_primaries{$_} = 1 for eval { $f_class->primary_columns }; - my $f_loaded = !$@; - # single key relationship + # no join condition or just a column name if (!ref $cond) { + my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns }; $class->throw_exception("Can't infer join condition for ${rel} on ${class}; unable to load ${f_class}") - unless $f_loaded; + if $@; my ($pri, $too_many) = keys %f_primaries; $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has no primary keys") unless defined $pri; - $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary key") + $class->throw_exception("Can't infer join condition for ${rel} on ${class}; ${f_class} has multiple primary keys") if $too_many; my $fk = defined $cond ? $cond : $rel; @@ -35,7 +32,7 @@ sub belongs_to { { accessor => $acc_type, %{$attrs || {}} } ); } - # multiple key relationship + # explicit join condition elsif (ref $cond eq 'HASH') { my $cond_rel; for (keys %$cond) { diff --git a/lib/DBIx/Class/Relationship/HasMany.pm b/lib/DBIx/Class/Relationship/HasMany.pm index 864eaac..70438e3 100644 --- a/lib/DBIx/Class/Relationship/HasMany.pm +++ b/lib/DBIx/Class/Relationship/HasMany.pm @@ -15,9 +15,8 @@ sub has_many { my ($pri, $too_many) = $class->primary_columns; $class->throw_exception( "has_many can only infer join for a single primary key; ${class} has more" ) if $too_many; - my $f_key; - my $f_class_loaded = eval { $f_class->columns }; - my $guess; + + my ($f_key,$guess); if (defined $cond && length $cond) { $f_key = $cond; $guess = "caller specified foreign key '$f_key'"; @@ -26,8 +25,11 @@ sub has_many { $f_key = lc $1; # go ahead and guess; best we can do $guess = "using our class name '$class' as foreign key"; } + + my $f_class_loaded = eval { $f_class->columns }; $class->throw_exception("No such column ${f_key} on foreign class ${f_class} ($guess)") if $f_class_loaded && !$f_class->has_column($f_key); + $cond = { "foreign.${f_key}" => "self.${pri}" }; } diff --git a/lib/DBIx/Class/Relationship/ManyToMany.pm b/lib/DBIx/Class/Relationship/ManyToMany.pm index 0ecc915..efbd770 100644 --- a/lib/DBIx/Class/Relationship/ManyToMany.pm +++ b/lib/DBIx/Class/Relationship/ManyToMany.pm @@ -5,15 +5,13 @@ use warnings; sub many_to_many { my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_; - $rel_attrs ||= {}; - { no strict 'refs'; no warnings 'redefine'; *{"${class}::${meth}"} = sub { my $self = shift; my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; - $self->search_related($rel)->search_related($f_rel, @_ > 0 ? @_ : undef, { %$rel_attrs, %$attrs }); + $self->search_related($rel)->search_related($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }); }; } } diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 709f312..0837ba8 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -34,10 +34,10 @@ Creates a new row object from column => value mappings passed as a hash ref sub new { my ($class, $attrs) = @_; $class = ref $class if ref $class; - my $new = bless({ _column_data => { } }, $class); + my $new = bless { _column_data => {} }, $class; if ($attrs) { - $new->throw_exception("attrs must be a hashref" ) unless ref($attrs) eq 'HASH'; - while (my ($k, $v) = each %{$attrs}) { + $new->throw_exception("attrs must be a hashref") unless ref($attrs) eq 'HASH'; + while (my ($k, $v) = each %$attrs) { $new->throw_exception("No such column $k on $class") unless $class->has_column($k); $new->store_column($k => $v); } @@ -61,7 +61,8 @@ sub insert { $self->{result_source} ||= $self->result_source_instance if $self->can('result_source_instance'); my $source = $self->{result_source}; - $self->throw_exception("No result_source set on this object; can't insert") unless $source; + $self->throw_exception("No result_source set on this object; can't insert") + unless $source; #use Data::Dumper; warn Dumper($self); $source->storage->insert($source->from, { $self->get_columns }); $self->in_storage(1); @@ -133,8 +134,8 @@ sub delete { $self->throw_exception("Cannot safely delete a row in a PK-less table") if ! keys %$ident_cond; foreach my $column (keys %$ident_cond) { - $self->throw_exception("Can't delete the object unless it has loaded the primary keys") - unless exists $self->{_column_data}{$column}; + $self->throw_exception("Can't delete the object unless it has loaded the primary keys") + unless exists $self->{_column_data}{$column}; } $self->result_source->storage->delete( $self->result_source->from, $ident_cond); @@ -142,11 +143,8 @@ sub delete { } else { $self->throw_exception("Can't do class delete without a ResultSource instance") unless $self->can('result_source_instance'); - my $attrs = { }; - if (@_ > 1 && ref $_[$#_] eq 'HASH') { - $attrs = { %{ pop(@_) } }; - } - my $query = (ref $_[0] eq 'HASH' ? $_[0] : {@_}); + my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? { %{pop(@_)} } : {}; + my $query = ref $_[0] eq 'HASH' ? $_[0] : {@_}; $self->result_source_instance->resultset->search(@_)->delete; } return $self; @@ -165,8 +163,7 @@ the database and stored in the object. sub get_column { my ($self, $column) = @_; $self->throw_exception( "Can't fetch data as class method" ) unless ref $self; - return $self->{_column_data}{$column} - if exists $self->{_column_data}{$column}; + return $self->{_column_data}{$column} if exists $self->{_column_data}{$column}; $self->throw_exception( "No such column '${column}'" ) unless $self->has_column($column); return undef; } @@ -174,9 +171,7 @@ sub get_column { sub has_column_loaded { my ($self, $column) = @_; $self->throw_exception( "Can't call has_column data as class method" ) unless ref $self; - return 1 - if exists $self->{_column_data}{$column}; - return 0; + return exists $self->{_column_data}{$column}; } =head2 get_columns @@ -257,7 +252,7 @@ sub copy { delete $col_data->{$col} if $self->result_source->column_info($col)->{is_auto_increment}; } - my $new = bless({ _column_data => $col_data }, ref $self); + my $new = bless { _column_data => $col_data }, ref $self; $new->set_columns($changes); $new->insert; foreach my $rel ($self->result_source->relationships) {