Purple/SQLite.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 42.8% 6 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 85.7% 90 / 105 CC 10 SCAR 14.1
CC
10
Cov
85%
_table_exists
8.8
_New
4.1
getNIDs
2.0
CRAP 4.1
↑ Summary Purple/Server/REST.pm »
1 package Purple::SQLite;
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 Purple::Sequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.9';
8  
9 2 my $DEFAULT_DB_LOC = 'purple.db';
10 # XXX not positive we want url to NOT NULL
11 # XXX last_nid table is for speed handling
12 2 my $CREATE_SQL1 = q{
13 CREATE TABLE nids (
14 nid TEXT PRIMARY KEY NOT NULL,
15 url TEXT NOT NULL,
16 created_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
17 updated_on TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
18 );
19 };
20 2 my $CREATE_SQL2 = q{
21 CREATE TABLE lastnid (
22 nid TEXT NOT NULL
23 );
24 };
25 2 my $CREATE_SQL3 = q{
26 INSERT INTO lastnid (nid) VALUES ('0');
27 };
28  
29 2 ▶sub _New {
Subroutine _New: called CC=4 SCAR=14.2
30 2 my $class = shift;
31 2 my %p = @_;
32 2 my $self;
33  
34 my $db_loc;
35 2 ▶ if ($p{store}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"store"}) 2 0 2
36 2 $db_loc = $p{store} . '/' . $DEFAULT_DB_LOC;
37 }
38 2 ▶ $db_loc ||= $DEFAULT_DB_LOC;
Condition33%1/3
$db_loc ||= $DEFAULT_DB_LOC
l!l&&r!l&&!r
200
MC/DC0%0/2
$db_loc ||= $DEFAULT_DB_LOC
$db_loc $DEFAULT_DB_LOC
Truth table33%1/3
A ||= B
A= $db_locB= $DEFAULT_DB_LOC
ABresult
1X1
011
000
39  
40 2 $self->{db_loc} = $db_loc;
41  
42 2 $self->{dbh} = DBI->connect("dbi:SQLite:$db_loc", undef, undef);
43 # { AutoCommit => 0 });
44  
45 # create nids table if it doesn't already exist
46 2 ▶ if (!_table_exists($self->{dbh}, 'nids')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_table_exists($self->{"dbh"}, "nids")) 2 0 2
47 2 $self->{dbh}->do($CREATE_SQL1);
48 2 $self->{dbh}->do($CREATE_SQL2);
49 2 $self->{dbh}->do($CREATE_SQL3);
50 }
51 2 bless($self, $class);
52 }
53  
54 # XXX retrieving the lastnid is slow
55 # using max does not work when the nids are mixed numbers and letters
56 # last_row_id (see DBD::SQLite) was tested as well as the below,
57 # neither is great
58 1 ▶sub getNext {
Subroutine getNext: called CC=1 SCAR=0.0
Pod documented
59 1 my ($self, $url) = @_;
60  
61 1 $self->{dbh}->do('BEGIN TRANSACTION');
62 # get next NID
63 1 my $sth = $self->{dbh}->prepare('SELECT nid FROM lastnid');
64 1 $sth->execute();
65 1 my $currentNid = ($sth->fetchrow_array)[0];
66 1 my $nextNid = Purple::Sequence::increment_nid($currentNid);
67 # update NID->URL value
68 1 $self->{dbh}->do("INSERT INTO nids (nid, url) VALUES ('$nextNid', '$url')");
69 1 $self->{dbh}->do("UPDATE lastnid SET nid = '$nextNid'");
70 1 $self->{dbh}->do('COMMIT TRANSACTION');
71  
72 1 return $nextNid;
73 }
74  
75 1 ▶sub getURL {
Subroutine getURL: called CC=1 SCAR=0.0
Pod documented
76 1 my ($self, $nid) = @_;
77 1 my $sth = $self->{dbh}->prepare('SELECT url FROM nids WHERE nid = ?');
78 1 $sth->execute($nid);
79 1 return ($sth->fetchrow_array)[0];
80 }
81  
82 1 ▶sub updateURL {
Subroutine updateURL: called CC=1 SCAR=0.0
Pod documented
83 1 my ($self, $url, @nids) = @_;
84 1 my $questionMarks = join(', ', map('?', @nids));
85  
86 1 $self->{dbh}->do(qq{
87 UPDATE nids SET url = ?, updated_on = ? WHERE nid IN ($questionMarks)
88 }, undef, $url, &_timestamp, @nids);
89 }
90  
91 1 ▶sub getNIDs {
Subroutine getNIDs: called CC=2 SCAR=6.9
Pod documented
92 1 my ($self, $url) = @_;
93  
94 1 my $sth = $self->{dbh}->prepare('SELECT nid FROM nids WHERE url = ?');
95 1 $sth->execute($url);
96  
97 1 my @nids;
98 1 ▶ while (my $nid = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nid = $sth->fetchrow_array) 1 1 2
99 1 push @nids, $nid;
100 }
101 1 return @nids;
102 }
103  
104 1 ▶sub deleteNIDs {
Subroutine deleteNIDs: called CC=1 SCAR=0.0
Pod documented
105 1 my ($self, @nids) = @_;
106 1 my $questionMarks = join(', ', map('?', @nids));
107  
108 1 $self->{dbh}->do("DELETE FROM nids WHERE nid IN ($questionMarks)",
109 undef, @nids);
110 }
111  
112 ### private
113  
114 1 ▶sub _timestamp {
Subroutine _timestamp: called CC=1 SCAR=0.0
115 1 my @timestamp = localtime;
116 1 return sprintf('%d-%02d-%02d %02d:%02d:%02d', $timestamp[5] + 1900,
117 $timestamp[4] + 1, $timestamp[3], $timestamp[2],
118 $timestamp[1], $timestamp[0]);
119 }
120  
121 # stolen from
122 # http://gmax.oltrelinux.com/dbirecipes.html#checking_for_an_existing_table
123 2 ▶sub _table_exists {
Subroutine _table_exists: called CC=6 SCAR=21.8
124 2 my $dbh = shift;
125 2 my $table = shift;
126 2 my @tables = $dbh->tables('','','','TABLE');
127 2 ▶ if (@tables) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) { } 0 2 2
128 0 for (@tables) {
129 0 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
130 0 ▶ return 1 if $_ eq $table
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $table 0 0 0
131 }
132 }
133 else {
134 2 eval {
135 2 local $dbh->{PrintError} = 0;
136 2 local $dbh->{RaiseError} = 1;
137 2 $dbh->do(qq{SELECT * FROM $table WHERE 1 = 0 });
138 };
139 2 ▶ return 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 2 2
140 }
141 2 return 0;
142 }
143  
144 ### fini
145  
146 =head1 NAME
147  
148 Purple::SQLite - SQLite driver for Purple
149  
150 =head1 VERSION
151  
152 Version 0.9
153  
154 =head1 SYNOPSIS
155  
156 SQLite backend for storing and retrieving Purple nids.
157  
158 use Purple::SQLite;
159  
160 my $p = Purple::SQLite->new('purple.db');
161 my $nid = $p->getNext('http://i.love.purple/');
162 my $url = $p->getURL($nid); # http://i.love.purple/
163  
164 =head1 METHODS
165  
166 =head2 new($db_loc)
167  
168 Initializes NID database at $db_loc, creating it if it does not
169 already exist. Defaults to "purple.db" in the current directory if
170 $db_loc is not specified.
171  
172 =head2 getNext($url)
173  
174 Gets the next available NID, assigning it $url in the database.
175  
176 =head2 getURL($nid)
177  
178 Gets the URL associated with NID $nid.
179  
180 =head2 updateURL($url, @nids)
181  
182 Updates the NIDs in @nids with the URL $url.
183  
184 =head2 getNIDs($url)
185  
186 Gets all NIDs associated with $url.
187  
188 =head2 deleteNIDs(@nids)
189  
190 Deletes all NIDs in @nids.
191  
192 =head1 AUTHORS
193  
194 Chris Dent, E<lt>cdent@burningchrome.comE<gt>
195  
196 Eugene Eric Kim, E<lt>eekim@blueoxen.comE<gt>
197  
198 =head1 BUGS
199  
200 Please report any bugs or feature requests to
201 C<bug-purple@rt.cpan.org>, or through the web interface at
202 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Purple>.
203 I will be notified, and then you'll automatically be notified of progress on
204 your bug as I make changes.
205  
206 =head1 ACKNOWLEDGEMENTS
207  
208 Thanks to Geraldine's and El Sombrero in Seattle for sustaining us
209 while we coded away. In particular, Eugene would not have made it had
210 it not been for that macho margarita.
211  
212 =head1 COPYRIGHT & LICENSE
213  
214 (C) Copyright 2006 Blue Oxen Associates. All rights reserved.
215  
216 This program is free software; you can redistribute it and/or modify it
217 under the same terms as Perl itself.
218  
219 =cut
220  
221 2 1; # End of Purple::SQLite
↑ Summary Purple/Server/REST.pm »