DBIx-Frame

 view release on metacpan or  search on metacpan

DBIx/Frame.pm  view on Meta::CPAN

    return undef;
  }

  # Unless we get an 'admin' flag, then don't let us work on admin fields
  my %present;
  unless ($admin) { 
    foreach my $field ($self->admin($table)) { 
      $present{$field}++ if 
	(defined $$datahash{$field} && $$datahash{$field} !~ m/^\s*$/);
    }
  }

  # Are all fields in 'required' present?
  my (%missing, %check);
  foreach my $field ($self->required($table)) {
    next if ($field eq 'ID');
    my $info = $$datahash{$field};  
    next unless defined $info; $info =~ s%^\s+|\s+$%%g; next if $info ne '';
    $missing{$field}++;
  }

  # Are all fields in 'key' present, and the entry used by 'key' unique?
  foreach my $field ($self->key($table)) { 
    if (defined $$datahash{$field} && $$datahash{$field} !~ m/^\s*$/) { 
      $check{$field} = $$datahash{$field}; 
    } else { $missing{$field}++ }
  }

  if (scalar keys %missing) {
    $self->set_error("Not all required fields set: still need " . 
		join(', ', keys %missing) );  
    return undef;
  } else {
    my @existing = $self->select($table, \%check);
    if (scalar @existing) { 
      $self->set_error("Entry already exists") && return undef;
    }
  }

  # We're good to go - proceed
  my $keys   = join(', ', keys %{$hash} ); 
  my $values = join(', ', values %{$hash} );  
  my $query = "INSERT INTO $table ( $keys ) VALUES ( $values )";

  $self->invoke($query);
}

=item update ( TABLE, DATAHASH, SELECTHASH [, ALLOW_ADMIN] )

Creates and executes an SQL query to update an item from C<TABLE>, using 
the (new) data from C<DATAHASH> for the updated values and the (old) data 
from C<SELECTHASH> to determine which item to update.  Note that the
search terms won't be empty unless you specifically specify them so. Uses
C<invoke()>.  

If C<ALLOW_ADMIN> is set, then you may work with fields that are protected
by the C<ADMIN> array.  (This doesn't actually work yet.)

=cut

sub update {
  my ($self, $table, $datahash, $selecthash, $admin ) = @_;
  my $db = _db_or_die($self) || return undef;
  $self->_test_table($table) || $self->set_error("Bad table") &&  return undef;

  my $hash   = $self->_parse_hash($table, $datahash)    || {};
  my $select = $self->_make_select($table, $selecthash, 0, 1) || "";
  unless ($select) {
    my @return = keys %{$selecthash};  my $return = join(', ', @return);
    $self->set_error("Couldn't get a selection out of $return");
    return undef;
  }

  my @list;
  foreach (keys %{$hash}) { 
    next unless defined($_) && defined($$hash{$_}); 
    $$hash{$_} =~ s%(^\s+|\s+$)%%;	# Lose the leading/trailing whitespace
    push (@list, "$_ = $$hash{$_}") 
  }

  # Unless we get an 'admin' flag, then don't let us work on admin fields
  my %present;
  unless ($admin) { 
    foreach my $field ($self->admin($table)) { 
      $present{$field}++ if 
	(defined $$datahash{$field} && $$datahash{$field} !~ m/^\s*$/);
    }
  }

  my $setlist = join(', ', @list);
  unless ($table && $setlist && $select) {
    $self->set_error("Not enough unformation for the update " . 
		                    "(S: $setlist S: $select)" );
    return undef;
  }
  my $query = "UPDATE $table SET $setlist $select";

  $self->invoke($query);
}

=item select ( TABLE, DATAHASH [, LIMITHASH [, OTHER]] )

Creates and executes an SQL query to select an item or items from C<TABLE>, 
using the data from C<DATAHASH> for the selection.  Uses C<MATCHHASH> to
offer extra information to the select query - specifically, :
 
  Key	  Description
  MATCH	  What fields to return; defaults to '*'.
  ORDER	  What order to return the fields in.  Defaults to the value of 
	  order() for this database/table.  See below for more details.

C<OTHER> adds additional text to the end of the search.

Returns a list of hash refs, each of which is one entry from the table.

Note that this does *not* use C<invoke()>, though standard logging is
still done.  This is done to preserve the order that the database returned
its results in.

=cut



( run in 1.062 second using v1.01-cache-2.11-cpan-13bb782fe5a )