Bio-Genex
view release on metacpan or search on metacpan
Contact/Contact.pm view on Meta::CPAN
;
}
attributes (no_lookup=>['fetched', 'fetch_all', 'fetched_attr', 'id'], lookup=>['con_pk', 'type', 'organization', 'contact_person', 'contact_person_phone', 'contact_person_email', 'org_phone', 'org_email', 'org_mail_address', 'org_toll_free_phone', '...
sub table_name {return 'Contact';} # probably unnecessary
sub fkeys {return $FKEYS;}
sub column2name {return $COLUMN2NAME;}
sub name2column {return $NAME2COLUMN;}
sub fkey_obj2raw {return $FKEY_OBJ2RAW;}
sub column_names {return $COLUMN_NAMES;}
sub pkey_name {return 'con_pk';}
sub linking_table {return 0;}
sub insert_db {
my ($self,$dbh) = @_;
assert_dbh($dbh);
# iterate over the fields and add them to the INSERT
my %values;
foreach my $col (@{$COLUMN_NAMES}) {
no strict 'refs';
# we don't want Bio::Genex::undefined() to get called
next unless defined $self->get_attribute($col);
$values{$col} = $self->$col();
}
# don't store a primary key
delete $values{'con_pk'};
if (grep {$_ eq 'last_updated'} @{$COLUMN_NAMES}) {
# we set the 'last_updated' field ourselves
my $timeformat = '%r %A %B %d %Y';
$values{last_updated} = strftime($timeformat, localtime);
}
# execute the INSERT
my $sql = create_insert_sql($dbh,'Contact',\%values);
$dbh->do($sql);
# on error
if ($dbh->err) {
warn "Bio::Genex::Contact::insert_db: SQL=<$sql>, DBI=<$DBI::errstr>";
return undef;
}
my $pkey = fetch_last_id($dbh,'Contact');
$self->id($pkey);
$self->con_pk($pkey);
return $pkey;
}
sub update_db {
my ($self,$dbh) = @_;
assert_dbh($dbh);
die "Bio::Genex::Contact::update_db: object not in DB"
unless defined $self->id() && defined $self->con_pk();
# we must pre-fetch all the attributes
$self->fetch();
# iterate over the fields and add them to the INSERT
my %values;
foreach my $col (@{$COLUMN_NAMES}) {
no strict 'refs';
# we don't want Bio::Genex::undefined() to get called
next unless defined $self->get_attribute($col);
$values{$col} = $self->$col();
}
if (grep {$_ eq 'last_updated'} @{$COLUMN_NAMES}) {
# we set the 'last_updated' field ourselves
my $timeformat = '%r %A %B %d %Y';
$values{last_updated} = strftime($timeformat, localtime);
}
# execute the UPDATE
my $WHERE = 'con_pk=' . $dbh->quote($self->con_pk());
my $sql = create_update_sql($dbh,
TABLE=>'Contact',
SET=>\%values,
WHERE=>$WHERE);
$dbh->do($sql);
# on error
if ($dbh->err) {
warn "Bio::Genex::Contact::update_db: SQL=<$sql>, DBI=<$DBI::errstr>";
return undef;
}
return 1;
}
#
# a workhorse function for retrieving ALL objects of a class
#
sub get_all_objects {
my ($class) = shift;
my @objects;
my $COLUMN2FETCH;
my $VALUE2FETCH;
my $pkey_name;
my $has_args = 0;
$pkey_name = $class->pkey_name();
if (ref($_[0]) eq 'HASH') {
# we were called with an anonymous hash as the first parameter
# grab it and parse the parameter => value pairs
my $hashref = shift;
$has_args = 1;
$COLUMN2FETCH = $hashref->{column} if exists $hashref->{column};
$VALUE2FETCH = $hashref->{value} if exists $hashref->{value};
die "Bio::Genex::Contact::get_all_objects: Must define both 'column' and 'value'"
if ((defined $VALUE2FETCH) && not (defined $COLUMN2FETCH)) ||
( run in 1.196 second using v1.01-cache-2.11-cpan-39bf76dae61 )