DBIx-DBO2

 view release on metacpan or  search on metacpan

DBO2/Record.pm  view on Meta::CPAN


=cut

use Class::MakeMethods::Composite::Inheritable(hook=>'ok_insert pre_insert post_insert'); 

# $record->insert_record()
sub insert_record {
  my $self = shift;
  my $class = ref( $self ) or croak("Not a class method");
  my $table = $class->demand_table();
  my @flags = $self->ok_insert();
  if ( grep { length $_ and ! $_ } @flags ) {
    # warn "Cancelling insert of $self, flags are " . join(', ', map "'$_'", @flags);
    return undef;
  } 
  $self->pre_insert();
  $table->insert_row( $self );
  $self->post_insert();
  $self;
}

########################################################################

=head2 Row Updates

After retrieving a record with one of the fetch methods, you may save any changes by calling update_record.

=over 4

=item update_record

Attempts to update the record using its primary key as a unique identifier.

  $record->update_record () : $record_or_undef

Calls ok_update to ensure that it's OK to update this row, and aborts if any of hook subroutines return 0. 

Calls any pre_update hooks, then calls its table's update_row method, then calls any post_update hooks.

Returns undef if the update was aborted, or the record if the update was successful.

=item ok_update

Inheritable Hook. Subclasses should override this with any functions they wish to use to validate rows before they are updated. Return 0 to abort the update.

=item pre_update

Inheritable Hook. Subclasses should override this with any functions they wish performed immediately before a row is updated.

=item post_update

Inheritable Hook. Subclasses should override this with any functions they wish performed immediately after a row is updated.

=back

=cut

use Class::MakeMethods::Composite::Inheritable(hook=>'ok_update pre_update post_update'); 

# $record->update_record()
sub update_record {
  my $self = shift;
  my $class = ref( $self ) or croak("Not a class method");
  my $table = $class->demand_table();
  my @flags = $self->ok_update;
  if ( grep { length $_ and ! $_ } @flags ) {
    # warn "Cancelling update of $self, flags are " . join(', ', map "'$_'", @flags );
    return undef;
  } 
  # warn "About to update $self, flags are " . join(', ', map "'$_'", @flags );
  $self->pre_update();
  $table->update_row( $self );
  $self->post_update();
  $self;
}

########################################################################

=head2 Deletion

=over 4

=item delete_record 

  $record->delete_record () : $boolean_completed

Checks to see if any of the pre_delete results is "0". If not, asks the table to delete the row.

Returns 1 if the deletion was successful, or 0 if it was aborted.

=item ok_delete

  $record->ok_delete () : @booleans

Inheritable Hook. Subclasses should override this with any functions they wish to use to validate rows before they are updated. Return 0 to abort the deletion.

=item pre_delete

  $record->pre_delete ()

Inheritable Hook. Subclasses should override this with any functions they wish performed before a row is deleted.

=item post_delete

  $record->post_delete ()

Inheritable Hook. Subclasses should override this with any functions they wish performed after a row is deleted.

=back

=cut

use Class::MakeMethods::Composite::Inheritable(hook=>'ok_delete pre_delete post_delete'); 

# $success = $record->delete_record();
sub delete_record {
  my $self = shift;
  my @flags = $self->ok_delete;
  if ( grep { length $_ and ! $_ } @flags ) {
    # warn "Cancelling delete of $self, flags are " . join(', ', map "'$_'", @flags );
    return 0;



( run in 0.471 second using v1.01-cache-2.11-cpan-995e09ba956 )