3 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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;
3 Einträge, 1 Seite |