package M::Room; use Moose; #use SQL::Abstract; has 'ID' => ( is => 'rw' ); has 'name' => ( is => 'rw' ); has 'description' => ( is => 'rw' ); has 'dbh' => ( is => 'rw' ); sub create { my $self = shift; return unless $self->name =~ /^\w+$/; # without name, nothing to add die "M::Room::add() has no DBH!" unless ref $self->{dbh}; $self->{dbh}->do("INSERT INTO rooms SET name=(?), description=(?)", undef, $self->{name}, $self->{description}) or die $self->{dbh}->errstr; } sub retrieve { my $self = shift; return unless $self->ID =~ /^\d+$/; # without ID, nothing to find die "M::Room::find() has no DBH!" unless ref $self->{dbh}; my $sth = $self->{dbh}->prepare("SELECT * FROM rooms WHERE ID = (?) LIMIT 1") or die $self->{dbh}->errstr; $sth->execute($self->ID); my $ref = $sth->fetchrow_hashref(); $self->{name} = $ref->{name}; $self->{description} = $ref->{description}; $sth->finish(); } sub update { my $self = shift; return unless $self->ID =~ /^\d+$/; # without ID, no way to update die "M::Room::save() has no DBH!" unless ref $self->{dbh}; $self->{dbh}->do("UPDATE rooms SET name=(?), description=(?) WHERE ID =(?)", undef, $self->{name}, $self->{description}, $self->{ID}) or die $self->{dbh}->errstr; } sub del { my $self = shift; return unless $self->ID =~ /^\d+$/; # without ID, nothing to delete die "M::Room::del() has no DBH!" unless ref $self->{dbh}; $self->{dbh}->do("DELETE FROM rooms WHERE ID = (?) LIMIT 1", undef, $self->{ID}) or die $self->{dbh}->errstr; } 1;