DBIx-DBObj

 view release on metacpan or  search on metacpan

lib/DBIx/~  view on Meta::CPAN

    }
  }

  ## FTSO Perl
  {
    local $/ = ',' and chomp($fields) and chomp($values); 
  }

  $queryStr  = join(' ', 
    'INSERT','INTO',$table,'(',$fields,')','VALUES','(',$values,')'); 

  if(query($queryStr, \@values)) {
 
    ## Life is easy when you have the Insert ID!  
    ##
    if (${$class . "::DBObjIID"} == 1) {
      ## We either return the instert id, or we find and return an object.
      my $iid = getLastInsertID(); 
      return($class->find($iid)) if ($returnObject);
      return($iid); 
    }
    
    ## Its not as easy, but we have the PKey(s) so hey!
    ##
    if($returnObject == 1) {
      my @find_args = (); 
      foreach my $key (@{$class . "::DBObjPKeys"}) {
	push(@find_args, $createPairs->{$key}); 
      }
      return($class->find(@find_args));
    }

    ## If we don't want an object, and we don't have a IID.
    return(1);
  }	

  ## 
  ## If the query failed
  throw Error::Better::OperationFailed ("SQL: Insert failed.")
    if ($throw == 1); 
  return(0); 
}



sub delete {
  my ($this) = @_; 
  return(undef) unless (ref $this); 

  my $queryStr  = "DELETE FROM " . $this->{'_Table'} . " WHERE "; 
  foreach my $pkey (@{ref($this) . "::DBObjPKeys"}) {
    my $pkeyValue = $this->{"_Fields"}{"_$pkey"}{'_Value'}; 
    $queryStr .= " $pkey='$pkeyValue' AND"; 
  }
  $queryStr =~ s/AND$//; 
  return(query($queryStr));
}



sub update { 
  my ($this)   = @_; 
  my $queryStr = ""; 
  my $fieldStr = ""; 
  my $whereStr = ""; 

  my @updated_fields = (); 
  my $changed_ctr    = 0; 
  ## Setup the fieldStr
  foreach my $field (keys %{$this->{"_Fields"}}) {
    $field =~ s/\_//; 
    if ($this->{"_Fields"}{"_$field"}{"_ChangedP"} == 1) { 
      $changed_ctr++; 
      my $fieldValue  = $this->{"_Fields"}{"_$field"}{"_Value"};
      $fieldStr      .= "$field='" . $fieldValue . "',"; 
      push(@updated_fields, $field); 
    }
  }
  $fieldStr =~ s/\,$//; 

  ## Return true if update() was called without anything to update!
  return(1) if ($changed_ctr == 0); 

  ## Build our WHERE clause. 
  ##
  $whereStr = join(' AND ', 
    map { 
      join('', $_, '=', "'", $this->{"_Fields"}{"_$_"}{"_Value"}, "'")
    }
    @{$this->{'_PKeys'}}); 

  ## Build the rest of the QUERY.
  ##
  $queryStr =  join(' ', 
    'UPDATE',$this->{'_Table'},'SET',$fieldStr,'WHERE','(',$whereStr,')'); 

  if (query($queryStr)) {
    ## Now return the update-flage for the updated fields to '0'. 
    foreach my $field (@updated_fields) {
      $this->{_Fields}{"_$field"}{_ChangedP} = 0; 
    }
    return(1); 
  }
  return(0);  
}

sub commit { 
  print STDERR "Depricated use of dbobj->commit()\n"; 
  return($_[0]->dbobj_query("commit")); 
}

sub rollback {
  print STDERR "Depricated use of dbobj->rollback()\n"; 
  return($_[0]->dbobj_query("rollback")); 
}

sub dbobj_query { 
  print STDERR "Depricated use of dbobj->dbobj_query()\n"; 
  my $class = shift; 
  return(DBIx::SimpleDBI::query(@_)); 
}



( run in 1.172 second using v1.01-cache-2.11-cpan-5a3173703d6 )