package MyClass::DBI::SubClass;
use base MyClass::DBI;
__PACKAGE__->table('my_items');
__PACKAGE__->columns(All=>qw/id name qty parent/);
sub next_val {
my $self = shift;
my $sth = $self->db_Main->prepare(
qq(SELECT nextval(\'my_items_id_seq\') ) );
$sth->execute();
my $id = $sth->fetch();
return $id;
};
sub curr_value {
my $self = shift;
my $sth = $self->db_Main->prepare(
qq(SELECT currval(\'my_items_id_seq') ));
$sth->execute();
my $id = $sth->fetch();
return $id;
};
####
package MyClass::Items;
use 5.008008;
use strict;
use warnings;
use MyClass::DBI::SubClass;
sub addItem {
my $self = shift;
my %params = @_;
return undef if(! defined($params{name}));
return undef if(! defined($params{qty}));
my $record = MyClass::DBI::SubClass->insert(
{
id => MyClass::DBI::SubClass->next_val(),
name => $params{name},
qty => $params{qty}
});
if(!defined($record)) {
warn "Unable to create new item: ". $params{name};
return undef;
};
return $record;
};
1;
__END__
####
my $parent = MyClass::DBI::SubClass->insert( {name=>"dad", qty=>1} );
my $child = MyClass::DBI::SubClass->insert(
{
name => "junior",
qty => 1,
parent => $parent->id
});