cvs commit: p5ee/App-Repository/lib/App Repository.pm RepositoryObject.pm

[email protected] (Stephen Adkins)
Newsgroups perl.cvs.p5ee
Message-ID <[email protected]>
cvsuser     05/08/10 11:32:39

  Modified:    App-Repository/lib/App Repository.pm RepositoryObject.pm
  Log:
  new_object(): set column default, temp option, check not_null, check null in alternate key
  
  Revision  Changes    Path
  1.22      +77 -22    p5ee/App-Repository/lib/App/Repository.pm
  
  Index: Repository.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Repository/lib/App/Repository.pm,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- Repository.pm	9 Aug 2005 18:50:17 -0000	1.21
  +++ Repository.pm	10 Aug 2005 18:32:39 -0000	1.22
  @@ -1500,6 +1500,7 @@
   }
   
   # $ok = $rep->insert_row ($table, \@cols, \@row);
  +# $ok = $rep->insert_row ($table, \%obj);
   sub insert_row {
       &App::sub_entry if ($App::trace);
       my ($self, $table, $cols, $row, $options) = @_;
  @@ -1561,41 +1562,95 @@
   # set in the database that I don't know about, and I want them to be
   # reflected in the returned object.
   # NOTE 2: Tables which have
  +# $object = $rep->new_object($table, \@cols, \@row);
  +# $object = $rep->new_object($table, \%obj_values);
  +# $object = $rep->new_object($table, $col, $value);
  +# $object = $rep->new_object($table);
   sub new_object {
       &App::sub_entry if ($App::trace);
       my ($self, $table, $cols, $row, $options) = @_;
  +
  +    my $tabledef = $self->{table}{$table};
  +    my $class = $tabledef->{class} || "App::RepositoryObject";
  +
       my $ref = ref($cols);
  -    if ($ref && $ref ne "ARRAY") {
  -        $self->_set_defaults($table, $cols);
  -        $self->_check_required_fields($table, $cols);
  -    }
  -    my $retval = $self->insert_row($table, $cols, $row, $options);
  -    die "new($table) unable to create a new row" if (!$retval);
  -    my $params = $self->_last_inserted_id();
  -    if (!$params) {
  -        $params = {};
  +    my ($object);
  +    if ($ref && $ref eq "ARRAY") {
  +        $object = {};
           for (my $i = 0; $i <= $#$cols; $i++) {
  -            if (!$row->[$i] || $row->[$i] !~ /^@/) {
  -                $params->{$cols->[$i] . ".eq"} = $row->[$i];
  +            $object->{$cols->[$i]} = $row->[$i];
  +        }
  +    }
  +    elsif ($ref) {
  +        $object = { %$cols };
  +    }
  +    elsif ($cols) {
  +        $object = { $cols => $row };
  +    }
  +    else {
  +        $object = {};
  +    }
  +
  +    App->use($class);
  +    bless $object, $class;
  +    $object->_init();
  +    $self->_check_default_and_required_fields($object);
  +
  +    if (!$options->{temp}) {
  +        my $retval = $self->insert_row($table, $object, undef, $options);
  +        die "new($table) unable to create a new row" if (!$retval);
  +        my $params = $self->_last_inserted_id();
  +        if (!$params) {
  +            $params = {};
  +            foreach my $col (keys %$object) {
  +                $params->{$col . ".eq"} = $object->{$col};
               }
           }
  +        $object = $self->get_object($table, $params, undef, $options);
       }
  -    my $object = $self->get_object($table, $params, undef, $options);
  +
       &App::sub_exit($object) if ($App::trace);
       $object;
   }
   
  -sub _set_defaults {
  +sub _check_default_and_required_fields {
       &App::sub_entry if ($App::trace);
       my ($self, $table, $hash) = @_;
  -    # TODO: flesh this out
  -    &App::sub_exit() if ($App::trace);
  -}
  -
  -sub _check_required_fields {
  -    &App::sub_entry if ($App::trace);
  -    my ($self, $table, $hash) = @_;
  -    # TODO: flesh this out
  +    my $tabledef = $self->{table}{$table};
  +    my $columns = $tabledef->{column};
  +    if ($columns) {
  +        foreach my $column (keys %$columns) {
  +            if (!defined $hash->{$column}) {
  +                if (defined $columns->{$column}{default}) {
  +                    $hash->{$column} = $columns->{$column}{default};
  +                }
  +                elsif (defined $columns->{$column}{not_null}) {
  +                    die "Illegal object value for $table: $column cannot be NULL (i.e. undef)";
  +                }
  +            }
  +        }
  +    }
  +    my $primary_key = $tabledef->{primary_key};
  +    if ($primary_key) {
  +        # Watch out for auto-generated primary keys. It's OK for them to be NULL.
  +        #if ($#$primary_key > 0) {
  +        #    foreach my $column (@$primary_key) {
  +        #        if (!defined $hash->{$column}) {
  +        #            die "Illegal object value for $table: $column cannot be NULL because it exists in the primary key";
  +        #        }
  +        #    }
  +        #}
  +    }
  +    my $alternate_keys = $tabledef->{alternate_key};
  +    if ($alternate_keys) {
  +        foreach my $alternate_key (@$alternate_keys) {
  +            foreach my $column (@$alternate_key) {
  +                if (!defined $hash->{$column}) {
  +                    die "Illegal object value for $table: $column cannot be NULL because it exists in an alternate key";
  +                }
  +            }
  +        }
  +    }
       &App::sub_exit() if ($App::trace);
   }
   
  
  
  
  1.5       +35 -1     p5ee/App-Repository/lib/App/RepositoryObject.pm
  
  Index: RepositoryObject.pm
  ===================================================================
  RCS file: /cvs/public/p5ee/App-Repository/lib/App/RepositoryObject.pm,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RepositoryObject.pm	9 Aug 2005 18:54:33 -0000	1.4
  +++ RepositoryObject.pm	10 Aug 2005 18:32:39 -0000	1.5
  @@ -175,6 +175,40 @@
       return($nrows);
   }
   
  +#############################################################################
  +# PRIVATE METHODS
  +#############################################################################
  +
  +=head1 Private Methods
  +
  +=cut
  +
  +#############################################################################
  +# _init()
  +#############################################################################
  +
  +=head2 _init()
  +
  +    * Signature: $obj->_init();
  +    * Param:     void
  +    * Return:    void
  +    * Throws:    App::Exception
  +    * Since:     0.01
  +
  +    Sample Usage: 
  +
  +    $obj->_init();
  +
  +This method initializes the values of a newly created object.
  +The default implementation on the App::RepositoryObject base class does nothing.
  +It is provided so that it may be overridden in a subclass if desired.
  +
  +=cut
  +
  +sub _init {
  +    my ($self) = @_;
  +}
  +
   =head1 ACKNOWLEDGEMENTS
   
    * Author:  Stephen Adkins <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.