if (grep { $_ eq $position_column } ( map { @$_ } (values %{{ $rsrc->unique_constraints }} ) ) ) {
- my @pcols = $rsrc->primary_columns;
+ my @pcols = $rsrc->_pri_cols;
my $cursor = $shift_rs->search ({}, { order_by => { "-$ord", $position_column }, columns => \@pcols } )->cursor;
my $rs = $self->result_source->resultset;
- while (my @pks = $cursor->next ) {
-
+ my @all_pks = $cusrsor->all;
+ while (my $pks = shift @all_pks) {
my $cond;
for my $i (0.. $#pcols) {
- $cond->{$pcols[$i]} = $pks[$i];
+ $cond->{$pcols[$i]} = $pks->[$i];
}
$rs->search($cond)->update ({ $position_column => \ "$position_column $op 1" } );
sub _ident_values {
my ($self) = @_;
- return (map { $self->{_column_data}{$_} } $self->primary_columns);
+ return (map { $self->{_column_data}{$_} } $self->_pri_cols);
}
=head2 ID
unless ref $self;
return undef unless $self->in_storage;
return $self->_create_ID(map { $_ => $self->{_column_data}{$_} }
- $self->primary_columns);
+ $self->_pri_cols);
}
sub _create_ID {
my ($self, $alias) = @_;
my %cond;
my $prefix = defined $alias ? $alias.'.' : '';
- $cond{$prefix.$_} = $self->get_column($_) for $self->primary_columns;
+ $cond{$prefix.$_} = $self->get_column($_) for $self->_pri_cols;
return \%cond;
}
# no join condition or just a column name
if (!ref $cond) {
$class->ensure_class_loaded($f_class);
- my %f_primaries = map { $_ => 1 } eval { $f_class->primary_columns };
+ my %f_primaries = map { $_ => 1 } eval { $f_class->_pri_cols };
$class->throw_exception(
- "Can't infer join condition for ${rel} on ${class}; ".
- "unable to load ${f_class}: $@"
+ "Can't infer join condition for ${rel} on ${class}: $@"
) 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 keys"
) if $too_many;
unless (ref $cond) {
$class->ensure_class_loaded($f_class);
- my ($pri, $too_many) = $class->primary_columns;
+ my ($pri, $too_many) = eval { $class->_pri_cols };
+ $class->throw_exception(
+ "Can't infer join condition for ${rel} on ${class}: $@"
+ ) if $@;
$class->throw_exception(
"has_many can only infer join for a single primary key; ".
$class->ensure_class_loaded($f_class);
my $pri = $class->_get_primary_key;
-
+
$class->throw_exception(
"might_have/has_one needs a primary key to infer a join; ".
"${class} has none"
sub _get_primary_key {
my ( $class, $target_class ) = @_;
$target_class ||= $class;
- my ($pri, $too_many) = $target_class->primary_columns;
+ my ($pri, $too_many) = eval { $target_class->_pri_cols };
+ $class->throw_exception(
+ "Can't infer join condition for ${rel} on ${target_class}: $@"
+ ) if $@;
+
$class->throw_exception(
"might_have/has_one can only infer join for a single primary key; ".
"${class} has more"
# if we multi-prefetch we group_by primary keys only as this is what we would
# get out of the rs via ->next/->all. We *DO WANT* to clobber old group_by regardless
if ( keys %{$attrs->{collapse}} ) {
- $sub_attrs->{group_by} = [ map { "$attrs->{alias}.$_" } ($rsrc->primary_columns) ]
+ $sub_attrs->{group_by} = [ map { "$attrs->{alias}.$_" } ($rsrc->_pri_cols) ]
}
$sub_attrs->{select} = $rsrc->storage->_subq_count_select ($rsrc, $sub_attrs);
my $attrs = $self->_resolved_attrs_copy;
delete $attrs->{$_} for qw/collapse select as/;
- $attrs->{columns} = [ map { "$attrs->{alias}.$_" } ($self->result_source->primary_columns) ];
+ $attrs->{columns} = [ map { "$attrs->{alias}.$_" } ($self->result_source->_pri_cols) ];
if ($needs_group_by_subq) {
# make sure no group_by was supplied, or if there is one - make sure it matches
return @{shift->_primaries||[]};
}
+sub _pri_cols {
+ my $self = shift;
+ my @pcols = $self->primary_columns
+ or $self->throw_exception (sprintf(
+ 'Operation requires a primary key to be declared on %s via set_primary_key',
+ ref $self,
+ ));
+ return @pcols;
+}
+
=head2 add_unique_constraint
=over 4
my $rsrc = $rs->result_source;
# quick check if we got a sane rs on our hands
- my @pcols = $rsrc->primary_columns;
- unless (@pcols) {
- $self->throw_exception (
- sprintf (
- "You must declare primary key(s) on source '%s' (via set_primary_key) in order to update or delete complex resultsets",
- $rsrc->source_name || $rsrc->from
- )
- );
- }
+ my @pcols = $rsrc->_pri_cols;
my $sel = $rs->_resolved_attrs->{select};
$sel = [ $sel ] unless ref $sel eq 'ARRAY';
my ($rs, $op, $values) = @_;
my $rsrc = $rs->result_source;
- my @pcols = $rsrc->primary_columns;
+ my @pcols = $rsrc->_pri_cols;
my $guard = $self->txn_scope_guard;
my ($rs, $op, $values) = @_;
my $rsrc = $rs->result_source;
- my @pcols = $rsrc->primary_columns;
+ my @pcols = $rsrc->_pri_cols;
my $attrs = $rs->_resolved_attrs;
# naive check - this is an internal method after all, we should know what we are doing
sub _update_blobs {
my ($self, $source, $blob_cols, $where) = @_;
- my (@primary_cols) = $source->primary_columns;
-
- $self->throw_exception('Cannot update TEXT/IMAGE column(s) without a primary key')
- unless @primary_cols;
+ my @primary_cols = eval { $source->_pri_cols };
+ $self->throw_exception("Cannot update TEXT/IMAGE column(s): $@")
+ if $@;
# check if we're updating a single row by PK
my $pk_cols_in_where = 0;
my $table = $source->name;
my %row = %$row;
- my (@primary_cols) = $source->primary_columns;
-
- $self->throw_exception('Cannot update TEXT/IMAGE column(s) without a primary key')
- unless @primary_cols;
+ my @primary_cols = eval { $source->_pri_cols} ;
+ $self->throw_exception("Cannot update TEXT/IMAGE column(s): $@")
+ if $@;
$self->throw_exception('Cannot update TEXT/IMAGE column(s) without primary key values')
if ((grep { defined $row{$_} } @primary_cols) != @primary_cols);