line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MyLibrary::Message; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1361
|
use MyLibrary::DB; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
5
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
5
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
595
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
MyLibrary::Message |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use MyLibrary::Message; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Use this module to get and set messages from the librarians to a MyLibrary database. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 METHODS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head2 new() |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head2 message_id() |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 message_date() |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head2 message() |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 message_global() |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
message_global is a Boolean value. Set it to true ('1') to make this message intended for everybody. Set it to false ('2') to make it not. False is the default. Call message_global without any parameters to get it's value. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head2 commit() |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head2 delete() |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 get_messages() |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 HISTORY |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
This module is legacy code. I sometimes wonder if it is even needed, and the message_global method feels like a hack. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 AUTHOR |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Eric Lease Morgan |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub new { |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# declare local variables |
54
|
1
|
|
|
1
|
1
|
754
|
my ($class, %opts) = @_; |
55
|
1
|
|
|
|
|
2
|
my $self = {}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# check for an id |
58
|
1
|
50
|
|
|
|
4
|
if ($opts{id}) { |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# get a handle |
61
|
0
|
|
|
|
|
0
|
my $dbh = MyLibrary::DB->dbh(); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# find this record |
64
|
0
|
|
|
|
|
0
|
my $rv = $dbh->selectrow_hashref('SELECT * FROM messages WHERE message_id = ?', undef, $opts{id}); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# check for success |
67
|
0
|
0
|
|
|
|
0
|
if (ref($rv) eq "HASH") { $self = $rv } |
|
0
|
|
|
|
|
0
|
|
68
|
0
|
|
|
|
|
0
|
else { return } |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# return the object |
73
|
1
|
|
|
|
|
4
|
return bless $self, $class; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub message_id { |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
81
|
0
|
|
|
|
|
0
|
return $self->{message_id}; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub message_date { |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# declare local variables |
89
|
2
|
|
|
2
|
1
|
4
|
my ($self, $message_date) = @_; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# check for the existance of a date |
92
|
2
|
100
|
|
|
|
7
|
if ($message_date) { $self->{message_date} = $message_date } |
|
1
|
|
|
|
|
2
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# return it |
95
|
2
|
|
|
|
|
5
|
return $self->{message_date}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub message { |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# declare local variables |
103
|
2
|
|
|
2
|
1
|
411
|
my ($self, $message) = @_; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# check for the existance of a message |
106
|
2
|
100
|
|
|
|
9
|
if ($message) { $self->{message} = $message } |
|
1
|
|
|
|
|
6
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# return the date |
109
|
2
|
|
|
|
|
8
|
return $self->{message}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub message_global { |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# declare local variables |
117
|
4
|
|
|
4
|
1
|
22
|
my ($self, $message_global) = @_; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# check for the existance of a message |
120
|
4
|
100
|
66
|
|
|
18
|
if ( ! $message_global ) { } |
|
|
50
|
|
|
|
|
|
121
|
2
|
|
|
|
|
4
|
elsif ( lc( $message_global ) eq '1' || lc( $message_global ) eq '2' ) { $self->{message_global} = $message_global } |
122
|
0
|
|
|
|
|
0
|
else { croak("Invalid value for message_global: $message_global. Valid values are 1 (true) or 2 (false).") } |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# return the global message flag |
125
|
4
|
|
|
|
|
10
|
return $self->{message_global}; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub commit { |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# get myself, :-) |
133
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# get a database handle |
136
|
1
|
|
|
|
|
5
|
my $dbh = MyLibrary::DB->dbh(); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# see if the object has an id |
139
|
0
|
0
|
|
|
|
|
if ($self->message_id()) { |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# update the record with this id |
142
|
0
|
|
|
|
|
|
my $return = $dbh->do('UPDATE messages SET message = ?, message_date = ?, message_global = ? WHERE message_id = ?', undef, $self->message(), $self->message_date(), $self->message_global(), $self->message_id()); |
143
|
0
|
0
|
0
|
|
|
|
if ($return > 1 || ! $return) { croak "Message update in commit() failed. $return records were updated." } |
|
0
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
else { |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# get a new sequence |
150
|
0
|
|
|
|
|
|
my $id = MyLibrary::DB->nextID(); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# create a new record |
153
|
0
|
|
|
|
|
|
my $return = $dbh->do('INSERT INTO messages (message_id, message, message_date, message_global) VALUES (?, ?, ?, ?)', undef, $id, $self->message(), $self->message_date(), $self->message_global()); |
154
|
0
|
0
|
0
|
|
|
|
if ($return > 1 || ! $return) { croak 'Message commit() failed.'; } |
|
0
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
$self->{message_id} = $id; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# done |
160
|
0
|
|
|
|
|
|
return 1; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub delete { |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
168
|
|
|
|
|
|
|
|
169
|
0
|
0
|
|
|
|
|
if ($self->{message_id}) { |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
my $dbh = MyLibrary::DB->dbh(); |
172
|
0
|
|
|
|
|
|
my $rv = $dbh->do('DELETE FROM messages WHERE message_id = ?', undef, $self->{message_id}); |
173
|
0
|
0
|
|
|
|
|
if ($rv != 1) {croak ("Deleted $rv records. I'll bet this isn't what you wanted.");} |
|
0
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
return 1; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
return 0; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub get_messages { |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
186
|
0
|
|
|
|
|
|
my @rv = (); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# create and execute a query |
189
|
0
|
|
|
|
|
|
my $dbh = MyLibrary::DB->dbh(); |
190
|
0
|
|
|
|
|
|
my $rows = $dbh->prepare('SELECT * FROM messages ORDER BY message_date'); |
191
|
0
|
|
|
|
|
|
$rows->execute; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# process each found row |
194
|
0
|
|
|
|
|
|
while (my $row = $rows->fetchrow_hashref()) { |
195
|
|
|
|
|
|
|
|
196
|
0
|
|
|
|
|
|
push (@rv, bless ($row, 'MyLibrary::Message')); |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
return @rv; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# return true, or else |
206
|
|
|
|
|
|
|
1; |