OurCal/Provider/DBI.pm

filter: Statementstmt 10.6% 13 / 122 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.5% 4 / 14 Podpod 100% 9 / 9 totaltotal 13.9% 26 / 186 CC 20 SCAR 27.1
CC
20
Cov
8%
_events
42.0
todos
20.0
users
12.0
CRAP 15.0
« OurCal/Provider/ICalendar.pm ↑ Summary OurCal/Provider.pm »
1 package OurCal::Provider::DBI;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
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<DBI> 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 <param[s]>
41  
42 Requires an C<OurCal::Config> object as config param and a name param.
43  
44 =cut
45  
46 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
47 0 my $class = shift;
48 0 my %what = @_;
49 0 my $conf = $what{config}->config($what{name});
50
51 $what{dbh} = DBI->connect($conf->{dsn}, $conf->{user}, $conf->{pass})
52 0 ▶ || die "Erk - couldn't connect to db $conf->{dsn}: $DBI::errstr\n";
Condition0%0/3
'DBI'->connect($conf->{'dsn'}, $conf->{'user'}, $conf->{'pass'}) || die("Erk - couldn't connect to db $conf->{'dsn'}: $DBI::errstr\n")
l!l&&r!l&&!r
000
MC/DC0%0/2
'DBI'->connect($conf->{'dsn'}, $conf->{'user'}, $conf->{'pass'}) || die("Erk - couldn't connect to db $conf->{'dsn'}: $DBI::errstr\n")
'DBI'->connect($conf->{'dsn'}, $conf->{'user'}, $conf->{'pass'}) die("Erk - couldn't connect to db $conf->{'dsn'}: $DBI::errstr\n")
Truth table0%0/3
A || B
A= 'DBI'->connect($conf->{'dsn'}, $conf->{'user'}, $conf->{'pass'})B= die("Erk - couldn't connect to db $conf->{'dsn'}: $DBI::errstr\n")
ABresult
1X1
011
000
53  
54 0 return bless \%what, $class;
55 }
56  
57 0 ▶sub events {
Subroutine events: not called CC=2 SCAR=17.9
Pod documented
58 0 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()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $sth->fetchrow_hashref) 0 0 0
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 0 ▶sub _events {
Subroutine _events: not called CC=6 SCAR=37.4
71 0 my $self = shift;
72 0 my %opts = @_;
73 0 my $dbh = $self->{dbh};
74  
75 0 ▶ my $what = ($opts{count})? "COUNT(*)" : "*";
Branch: 1 branch
BranchTrueFalseTotal
$opts{'count'} ? : 0 0 0
76  
77 0 my @vals;
78 0 my $sql = "SELECT $what FROM events WHERE ";
79 0 ▶ if (defined $opts{date}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"date"}) 0 0 0
80 0 $sql .= " date=? AND";
81 0 push @vals, $opts{date};
82 }
83 0 ▶ if (defined $opts{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'user'}) { } 0 0 0
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 ▶ if (defined $opts{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"limit"}) 0 0 0
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 ▶ $sth->execute(@vals) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@vals) or die($sth->errstr) 0 0 0
98 0 return ($sth, $sql);
99 }
100  
101 0 ▶sub has_events {
Subroutine has_events: not called CC=2 SCAR=17.9
Pod documented
102 0 my $self = shift;
103 0 my %opts = @_;
104 0 ▶ die "Can't call has_events without a date\n" unless $opts{date};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"date"} 0 0 0
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 0 ▶sub todos {
Subroutine todos: not called CC=4 SCAR=30.0
Pod documented
113 0 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 ▶ if (defined $opts{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"user"}) 0 0 0
121 0 $sql .= " OR user=?";
122 0 push @vals, $self->{user};
123 }
124 0 my $sth = $dbh->prepare($sql);
125 0 ▶ $sth->execute(@vals) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@vals) or die($sth->errstr) 0 0 0
126  
127 0 my @todos;
128 0 ▶ while (my $d = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $sth->fetchrow_hashref) 0 0 0
129 0 my $t = OurCal::Todo->new(%$d);
130 0 push @todos, $t;
131 }
132 0 return @todos;
133 }
134  
135 0 ▶sub users {
Subroutine users: not called CC=3 SCAR=24.8
Pod documented
136 0 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;
141 0 my $sth = $dbh->prepare($sql);
142 0 ▶ $sth->execute() || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die($sth->errstr) 0 0 0
143 0 my @users;
144 0 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
145 0 push @users, $row->[0];
146 }
147 0 return @users;
148 }
149  
150 0 ▶sub save_event {
Subroutine save_event: not called CC=3 SCAR=24.8
Pod documented
151 0 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 ▶ if (defined $opts{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'user'}) { } 0 0 0
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 ▶ $sth->execute(@vals) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@vals) or die($sth->errstr) 0 0 0
170  
171 }
172  
173 0 ▶sub del_event {
Subroutine del_event: not called CC=2 SCAR=17.9
Pod documented
174 0 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 ▶ $sth->execute($event->id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($event->id) or die($sth->errstr) 0 0 0
181 }
182  
183  
184 0 ▶sub save_todo {
Subroutine save_todo: not called CC=3 SCAR=21.4
Pod documented
185 0 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 ▶ if (defined $opts{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'user'}) { } 0 0 0
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 2 ▶ use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
202 0 my $sth = $dbh->prepare($sql);
203 0 ▶ $sth->execute(@vals) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@vals) or die($sth->errstr) 0 0 0
204 }
205  
206 0 ▶sub del_todo {
Subroutine del_todo: not called CC=2 SCAR=17.9
Pod documented
207 0 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 ▶ $sth->execute($todo->id) || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($todo->id) or die($sth->errstr) 0 0 0
214 }
215  
216 =head2 todos
217  
218 Returns all the todos on the system.
219  
220 =head2 has_events <param[s]>
221  
222 Returns whether there are events given the params.
223  
224 =head2 events <param[s]>
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 <OurCal::Todo>
233  
234 Save a todo.
235  
236 =head2 del_todo <OurCal::Todo>
237  
238 Delete a todo.
239  
240  
241 =head2 save_event <OurCal::Event>
242  
243 Save an event.
244  
245 =head2 del_event <OurCal::Event>
246  
247 Delete an event..
248  
249 =cut
250  
251  
252  
253 2 1;
254  
255  
« OurCal/Provider/ICalendar.pm ↑ Summary OurCal/Provider.pm »