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;
{ accessor => $acc_type, %{$attrs || {}} }
);
}
- # multiple key relationship
+ # explicit join condition
elsif (ref $cond eq 'HASH') {
my $cond_rel;
for (keys %$cond) {
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'";
$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}" };
}
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);
}
$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);
$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);
} 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;
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;
}
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
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) {