line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OurCal::Provider::DBI; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1193
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
4251
|
use DBI; |
|
1
|
|
|
|
|
31577
|
|
|
1
|
|
|
|
|
94
|
|
5
|
1
|
|
|
1
|
|
12
|
use Carp qw(confess cluck); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1554
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
OurCal::Provider::DBI - a DBI provider for OurCal |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
[a_dbi_provider] |
15
|
|
|
|
|
|
|
dsn = dbi:SQLite:ourcal |
16
|
|
|
|
|
|
|
type = dbi |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 CONFIG OPTIONS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=over 4 |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=item dsn |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
A C dsn. Required. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=item user |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
A user name. Optional. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item pass |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
A password. Optional. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=back |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 new |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Requires an C object as config param and a name param. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=cut |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new { |
47
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
48
|
0
|
|
|
|
|
|
my %what = @_; |
49
|
0
|
|
|
|
|
|
my $conf = $what{config}->config($what{name}); |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
0
|
|
|
|
$what{dbh} = DBI->connect($conf->{dsn}, $conf->{user}, $conf->{pass}) |
52
|
|
|
|
|
|
|
|| die "Erk - couldn't connect to db $conf->{dsn}: $DBI::errstr\n"; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return bless \%what, $class; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub events { |
58
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
59
|
0
|
|
|
|
|
|
my %opts = @_; |
60
|
0
|
|
|
|
|
|
my ($sth,$sql) = $self->_events(%opts); |
61
|
0
|
|
|
|
|
|
my @events; |
62
|
0
|
|
|
|
|
|
while (my $d = $sth->fetchrow_hashref()) { |
63
|
0
|
|
|
|
|
|
$d->{editable} = 1; |
64
|
0
|
|
|
|
|
|
my $e = OurCal::Event->new(%$d); |
65
|
0
|
|
|
|
|
|
push @events, $e; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
|
return @events; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _events { |
71
|
0
|
|
|
0
|
|
|
my $self = shift; |
72
|
0
|
|
|
|
|
|
my %opts = @_; |
73
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
my $what = ($opts{count})? "COUNT(*)" : "*"; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my @vals; |
78
|
0
|
|
|
|
|
|
my $sql = "SELECT $what FROM events WHERE "; |
79
|
0
|
0
|
|
|
|
|
if (defined $opts{date}) { |
80
|
0
|
|
|
|
|
|
$sql .= " date=? AND"; |
81
|
0
|
|
|
|
|
|
push @vals, $opts{date}; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
0
|
|
|
|
|
if (defined $opts{user}) { |
84
|
0
|
|
|
|
|
|
$sql .= " (user IS NULL OR user=?)"; |
85
|
0
|
|
|
|
|
|
push @vals, $opts{user}; |
86
|
|
|
|
|
|
|
} else { |
87
|
0
|
|
|
|
|
|
$sql .= " user IS NULL"; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
$sql .= " ORDER BY date DESC"; |
90
|
0
|
0
|
|
|
|
|
if (defined $opts{limit}) { |
91
|
0
|
|
|
|
|
|
$sql .= " LIMIT ?"; |
92
|
0
|
|
|
|
|
|
push @vals, $opts{limit}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
#confess "Executing $sql with ".join(", ", @vals)."\n"; |
96
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
97
|
0
|
0
|
|
|
|
|
$sth->execute(@vals) || die $sth->errstr; |
98
|
0
|
|
|
|
|
|
return ($sth, $sql); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub has_events { |
102
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
103
|
0
|
|
|
|
|
|
my %opts = @_; |
104
|
0
|
0
|
|
|
|
|
die "Can't call has_events without a date\n" unless $opts{date}; |
105
|
0
|
|
|
|
|
|
$opts{count} = 1; |
106
|
0
|
|
|
|
|
|
my ($sth) = $self->_events(%opts); |
107
|
0
|
|
|
|
|
|
my ($events) = $sth->fetchrow_array(); |
108
|
0
|
|
|
|
|
|
return $events; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub todos { |
113
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
114
|
0
|
|
|
|
|
|
my %opts = @_; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
my @vals; |
119
|
0
|
|
|
|
|
|
my $sql = "SELECT * FROM todos WHERE user IS NULL"; |
120
|
0
|
0
|
|
|
|
|
if (defined $opts{user}) { |
121
|
0
|
|
|
|
|
|
$sql .= " OR user=?"; |
122
|
0
|
|
|
|
|
|
push @vals, $self->{user}; |
123
|
|
|
|
|
|
|
} |
124
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
125
|
0
|
0
|
|
|
|
|
$sth->execute(@vals) || die $sth->errstr; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
my @todos; |
128
|
0
|
|
|
|
|
|
while (my $d = $sth->fetchrow_hashref()) { |
129
|
0
|
|
|
|
|
|
my $t = OurCal::Todo->new(%$d); |
130
|
0
|
|
|
|
|
|
push @todos, $t; |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
|
return @todos; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub users { |
136
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
137
|
0
|
|
|
|
|
|
my %opts = shift; |
138
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
139
|
0
|
|
|
|
|
|
my @tables = qw(todos events); |
140
|
0
|
|
|
|
|
|
my $sql = join " UNION ", map { "SELECT user from $_ WHERE user IS NOT NULL" } @tables; |
|
0
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
142
|
0
|
0
|
|
|
|
|
$sth->execute() || die $sth->errstr; |
143
|
0
|
|
|
|
|
|
my @users; |
144
|
0
|
|
|
|
|
|
while (my $row = $sth->fetchrow_arrayref) { |
145
|
0
|
|
|
|
|
|
push @users, $row->[0]; |
146
|
|
|
|
|
|
|
} |
147
|
0
|
|
|
|
|
|
return @users; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub save_event { |
151
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
152
|
0
|
|
|
|
|
|
my $event = shift; |
153
|
0
|
|
|
|
|
|
my %opts = @_; |
154
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# TODO we could check for id and do an update |
157
|
0
|
|
|
|
|
|
my $desc = $event->description; |
158
|
0
|
|
|
|
|
|
my $date = $event->date; |
159
|
0
|
|
|
|
|
|
my $sql; |
160
|
0
|
|
|
|
|
|
my @vals = ($date, $desc); |
161
|
0
|
0
|
|
|
|
|
if (defined $opts{user}) { |
162
|
0
|
|
|
|
|
|
$sql = "INSERT INTO events (date, description, user) VALUES (?, ?, ?)"; |
163
|
0
|
|
|
|
|
|
push @vals, $opts{user}; |
164
|
|
|
|
|
|
|
} else { |
165
|
0
|
|
|
|
|
|
$sql = "INSERT INTO events (date, description) VALUES (?, ?)"; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
169
|
0
|
0
|
|
|
|
|
$sth->execute(@vals) || die $sth->errstr; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub del_event { |
174
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
175
|
0
|
|
|
|
|
|
my $event = shift; |
176
|
0
|
|
|
|
|
|
my %opts = @_; |
177
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
178
|
0
|
|
|
|
|
|
my $sql = "DELETE FROM events WHERE id=?"; |
179
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
180
|
0
|
0
|
|
|
|
|
$sth->execute($event->id) || die $sth->errstr; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub save_todo { |
185
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
186
|
0
|
|
|
|
|
|
my $todo = shift; |
187
|
0
|
|
|
|
|
|
my %opts = @_; |
188
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# TODO we could check for id and do an update |
191
|
0
|
|
|
|
|
|
my $desc = $todo->full_description; |
192
|
0
|
|
|
|
|
|
my $sql; |
193
|
0
|
|
|
|
|
|
my @vals = ($desc); |
194
|
0
|
0
|
|
|
|
|
if (defined $opts{user}) { |
195
|
0
|
|
|
|
|
|
$sql = "INSERT INTO todos (description, user) VALUES (?, ?, ?)"; |
196
|
0
|
|
|
|
|
|
push @vals, $opts{user}; |
197
|
|
|
|
|
|
|
} else { |
198
|
0
|
|
|
|
|
|
$sql = "INSERT INTO todos (description) VALUES (?)"; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
1
|
|
|
1
|
|
46
|
use Carp qw(confess); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
257
|
|
202
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
203
|
0
|
0
|
|
|
|
|
$sth->execute(@vals) || die $sth->errstr; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub del_todo { |
207
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
208
|
0
|
|
|
|
|
|
my $todo = shift; |
209
|
0
|
|
|
|
|
|
my %opts = @_; |
210
|
0
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
211
|
0
|
|
|
|
|
|
my $sql = "DELETE FROM todos WHERE id=?"; |
212
|
0
|
|
|
|
|
|
my $sth = $dbh->prepare($sql); |
213
|
0
|
0
|
|
|
|
|
$sth->execute($todo->id) || die $sth->errstr; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head2 todos |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Returns all the todos on the system. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head2 has_events |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Returns whether there are events given the params. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 events |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Returns all the events for the given params. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head2 users |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Returns the name of all the users on the system. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 save_todo |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Save a todo. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head2 del_todo |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Delete a todo. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 save_event |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Save an event. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 del_event |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Delete an event.. |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=cut |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
1; |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|