SQLiteStore.pm

filter: Statementstmt 53.0% 88 / 166 Branchbran 23.4% 15 / 64 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 10 / 18 Podpod 100% 11 / 11 totaltotal 46.8% 126 / 269 CC 39 SCAR 24.8
CC
39
Cov
44%
update
20.0
new
17.9
get
12.5
CRAP 11.9
« Source.pm ↑ Summary SOAPServer.pm »
1 package PICA::SQLiteStore;
2 {
3 4 $PICA::SQLiteStore::VERSION = '0.585';
4 }
5 #ABSTRACT: Store L<PICA::Record>s in a SQLite database with versioning
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use PICA::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use PICA::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 our @ISA=qw(PICA::Store);
15  
16  
17 1 ▶sub new {
Subroutine new: called CC=15 SCAR=28.8
Pod documented
18 1 my $class = shift;
19 1 ▶ my ($filename, %params) = (@_ % 2) ? (@_) : (undef, @_);
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 1 1
20  
21 PICA::Store::readconfigfile( \%params, $ENV{PICASTORE} )
22 1 ▶ if exists $params{config} or exists $params{conf} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"config"} or exists $params{"conf"} 1 0 1
Condition33%1/3
exists $params{"config"} or exists $params{"conf"}
l!l&&r!l&&!r
100
MC/DC0%0/2
exists $params{"config"} or exists $params{"conf"}
exists $params{"config"} exists $params{"conf"}
Truth table33%1/3
A or B
A= exists $params{"config"}B= exists $params{"conf"}
ABresult
1X1
011
000
23  
24 1 ▶ $filename = $params{SQLite} unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 1 0 1
25  
26 1 ▶ croak("filename for SQLite database not specified") unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 1 1
27  
28 1 my $rebuild = $params{rebuild};
29 # TODO: option to use PPN as ID !
30  
31 1 my $dbh = DBI->connect( "dbi:SQLite:dbname=$filename","","",
32 { AutoCommit => 0, RaiseError => 1 } );
33 1 $dbh->{sqlite_unicode} = 1;
34  
35 1 ▶ croak("SQLite database connection failed: $filename: " . DBD->errstr) unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 1 1
36  
37 #$dbh::DESCTROY = DESTROY {
38 # my $sth = shift;
39 # $sth->finish if $sth->FETCH('Active');
40 #}
41  
42 # tables and triggers
43 1 my %tables = (
44 record => [
45 'record_ppn INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT',
46 'record_first INTEGER NOT NULL DEFAULT 0', # first revision
47 'record_latest INTEGER NOT NULL DEFAULT 0', # current revision
48 ],
49 revision => [
50 'rev_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT', # key (version)
51 'rev_ppn INTEGER DEFAULT 0', # foreign key to record.record_ppn
52 'rev_data TEXT NOT NULL', # PICA+ data
53 'rev_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',
54 'rev_user TEXT DEFAULT 0',
55 'rev_deleted BOOLEAN NOT NULL DEFAULT 0', # delete action
56 'rev_is_new BOOLEAN NOT NULL DEFAULT 0'
57 ],
58 archive => [
59 'arc_ppn INTEGER PRIMARY KEY',
60 'arc_latest INTEGER NOT NULL DEFAULT 0'
61 ],
62 );
63 1 my %triggers = (
64 record_insert => q{CREATE TRIGGER record_insert AFTER INSERT ON revision WHEN new.rev_ppn = 0
65 BEGIN
66 INSERT INTO record (record_first,record_latest) VALUES (new.rev_id,new.rev_id);
67 UPDATE revision SET rev_ppn=last_insert_rowid(), rev_is_new=1 WHERE rev_id=new.rev_id;
68 END;},
69 record_update => q{CREATE TRIGGER record_update AFTER INSERT ON revision WHEN new.rev_ppn != 0
70 BEGIN
71 UPDATE record SET record_latest=new.rev_id WHERE record_ppn=new.rev_ppn;
72 END;},
73 record_delete => q{CREATE TRIGGER record_delete DELETE ON record
74 BEGIN
75 INSERT INTO archive (arc_ppn, arc_latest) VALUES (old.record_ppn, old.record_latest);
76 UPDATE revision SET rev_deleted=1 WHERE rev_id=old.record_latest;
77 END;},
78 );
79 # TODO: where is timestamp and user of deletion logged??
80 # INSERT INTO revision (rev_ppn,rev_deleted,rev_user) VALUES (old.record_ppn,1,''); -- TODO
81  
82 1 my @tb;
83 1 my $std_tab = $dbh->table_info('', '', '%', '');
84 1 ▶ while( my $tbl = $std_tab->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tbl = $std_tab->fetchrow_hashref) 0 1 1
85 0 ▶ push @tb, $tbl->{TABLE_NAME} if $tables{$tbl->{TABLE_NAME}};
Branch: 1 branch
BranchTrueFalseTotal
if $tables{$tbl->{"TABLE_NAME"}} 0 0 0
86 # TODO: check whether there is any difference in table definitions
87 }
88 1 ▶ $rebuild = 1 if (@tb != keys %tables);
Branch: 1 branch
BranchTrueFalseTotal
if @tb != keys %tables 1 0 1
89  
90 1 ▶ if ($rebuild) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rebuild) 1 0 1
91 1 eval {
92 1 foreach my $name (@tb) {
93 0 $dbh->do("DROP TABLE $name");
94 }
95 1 foreach my $name (keys %tables) {
96 3 my $sql = "CREATE TABLE $name (".join(",",@{$tables{$name}}).")";
97 3 $dbh->do($sql);
98 };
99 1 foreach my $name (keys %triggers) {
100 3 $dbh->do($triggers{$name});
101 }
102 1 $dbh->commit;
103 };
104 1 ▶ croak("Failed to create database structure: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
105 }
106  
107 1 my $self = bless {
108 dbh => $dbh,
109 user => 0, # current user id
110 }, $class;
111  
112 # init prepared statements
113 1 $self->{get_record} = $dbh->prepare(q{SELECT
114 rev_user AS user, rev_ppn AS id, rev_data AS record, rev_timestamp AS timestamp, rev_id AS version, rev_id AS latest
115 FROM revision, record WHERE revision.rev_id=record.record_latest AND revision.rev_ppn=record.record_ppn AND record_ppn=?;});
116 1 $self->{get_revision} = $dbh->prepare(q{SELECT
117 rev_user AS user, rev_ppn AS id, rev_data AS record, rev_timestamp AS timestamp, rev_id AS version, record_latest AS latest
118 FROM revision, record WHERE rev_ppn=record_ppn AND revision.rev_id=?;});
119 1 $self->{insert_record} = $dbh->prepare('INSERT INTO revision (rev_ppn,rev_data,rev_user) VALUES (0,?,?)');
120 1 $self->{update_record} = $dbh->prepare('INSERT INTO revision (rev_ppn,rev_data,rev_user) VALUES (?,?,?)');
121 1 $self->{delete_record} = $dbh->prepare('DELETE FROM record WHERE record_ppn=?');
122 1 $self->{recent_changes} = $dbh->prepare(q{SELECT
123 rev_id AS version, rev_ppn AS ppn, rev_user AS user, rev_timestamp AS timestamp, rev_is_new AS is_new, rev_deleted AS deleted FROM revision
124 ORDER BY version DESC LIMIT ? OFFSET ?});
125 1 $self->{record_history} = $dbh->prepare(q{SELECT
126 rev_ppn AS ppn, rev_id AS version, rev_user AS user, rev_timestamp AS timestamp, rev_is_new AS is_new, rev_deleted AS deleted FROM revision
127 WHERE rev_ppn=?
128 ORDER BY version DESC LIMIT ? OFFSET ?
129 });
130 1 $self->{next_rev} = $dbh->prepare(q{SELECT
131 rev_id AS version, rev_user AS user, rev_timestamp AS timestamp, rev_is_new AS is_new, rev_deleted AS deleted FROM revision
132 WHERE rev_ppn = ? AND rev_id > ?
133 ORDER BY version ASC LIMIT ?
134 });
135 1 $self->{prev_rev} = $dbh->prepare(q{SELECT
136 rev_id AS version, rev_user AS user, rev_timestamp AS timestamp, rev_is_new AS is_new, rev_deleted AS deleted FROM revision
137 WHERE rev_ppn = ? AND rev_id < ?
138 ORDER BY version DESC LIMIT ?
139 });
140 1 $self->{deleted} = $dbh->prepare(q{SELECT rev_timestamp AS timestamp, rev_user AS user, arc_ppn AS ppn, arc_latest AS version FROM archive, revision
141 WHERE rev_id=arc_latest ORDER BY arc_latest DESC LIMIT ? OFFSET ?
142 });
143 1 $self->{contributions} = $dbh->prepare(q{SELECT
144 rev_id AS version, rev_ppn AS ppn, rev_user AS user, rev_timestamp AS timestamp, rev_is_new AS is_new, rev_deleted AS s FROM revision
145 WHERE rev_user=? ORDER BY version DESC LIMIT ? OFFSET ?
146 });
147  
148 1 return $self;
149 }
150  
151  
152 1 ▶sub get {
Subroutine get: called CC=8 SCAR=25.2
Pod documented
153 1 my ($self, $id, $version) = @_;
154  
155 1 my %result;
156 1 eval {
157 1 my $stm;
158 1 ▶ if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) { } 1 0 1
159 1 $stm = $self->{get_revision};
160 1 $stm->execute( $version );
161 } else {
162 0 $stm = $self->{get_record};
163 0 $stm->execute( $id );
164 }
165 1 my $hashref = $stm->fetchrow_hashref;
166 1 ▶ croak( $version ? "version $version" : $id) unless $hashref;
Branch: 2 branches
BranchTrueFalseTotal
unless $hashref 0 1 1
$version ? : 0 0 0
167 1 $hashref->{record} = PICA::Record->new( $hashref->{record} );
168 1 ▶ if ($version && $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version and $id) { } 0 1 1
Condition33%1/3
$version and $id
!ll&&!rl&&r
010
MC/DC0%0/2
$version and $id
$version $id
Truth table33%1/3
A and B
A= $versionB= $id
ABresult
0X0
100
111
169 0 ▶ %result = $hashref->{id} == $id ? %$hashref : (
Branch: 1 branch
BranchTrueFalseTotal
$hashref->{'id'} == $id ? : 0 0 0
170 errorcode => 2, errormessage => "record id does not match version"
171 );
172 } else {
173 1 %result = %$hashref;
174 }
175 1 $stm->finish;
176 };
177 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
178 # TODO: remove line number
179 0 %result = ( errorcode => 1, errormessage => "get failed: $@" );
180 }
181 1 return %result;
182 }
183  
184  
185 1 ▶sub create {
Subroutine create: called CC=3 SCAR=11.4
Pod documented
186 1 my ($self, $record) = @_;
187  
188 1 ▶ croak('create needs a PICA::Record object')
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($record, "PICA::Record") 0 1 1
189 unless UNIVERSAL::isa($record,'PICA::Record');
190  
191 1 my %result = eval {
192 1 my $recorddata = $record->string;
193 1 $self->{insert_record}->execute( $recorddata, $self->{user} );
194 1 my $version = $self->{dbh}->func('last_insert_rowid');
195 1 $self->get( undef, $version );
196 };
197 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
198 0 %result = ( errorcode => 1, errormessage => "create failed: $@" );
199 0 $self->{dbh}->rollback;
200 } else {
201 1 $self->{dbh}->commit;
202 }
203 1 return %result;
204 }
205  
206  
207 0 ▶sub update {
Subroutine update: not called CC=4 SCAR=30.0
Pod documented
208 0 my ($self, $id, $record, $version) = @_;
209  
210 0 ▶ croak('update needs a PICA::Record object')
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($record, "PICA::Record") 0 0 0
211 unless UNIVERSAL::isa($record,'PICA::Record');
212  
213 0 my %result = eval {
214 0 ▶ if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 0 0 0
215 # TODO (version is ignored so far)
216 }
217 0 $self->{update_record}->execute( $id, $record->string, $self->{user} );
218 0 $self->get( $id );
219 };
220 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
221 0 %result = ( errorcode => 1, errormessage => "update failed: $@" );
222 0 $self->{dbh}->rollback;
223 } else {
224 0 $self->{dbh}->commit;
225 }
226 0 return %result;
227 }
228  
229  
230 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
231 0 my ($self, $id) = @_;
232  
233 0 my %result = eval {
234 # TODO: create a new version
235 0 $self->{update_record}->execute( $id, "", $self->{user} );
236 0 $self->{delete_record}->execute( $id );
237 0 ( 'id' => $id );
238 };
239 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
240 0 %result = ( errorcode => 1, errormessage => "delete failed: $@" );
241 0 $self->{dbh}->rollback;
242 } else {
243 0 $self->{dbh}->commit;
244 }
245 0 return %result;
246 }
247  
248  
249 0 ▶sub access {
Subroutine access: not called CC=2 SCAR=17.9
Pod documented
250 0 my ($self, %params) = @_;
251  
252 0 for my $key (qw(userkey password dbsid language)) {
253 # ...check whether access can be granted or not...
254 }
255  
256 0 $self->{user} = $params{userkey};
257  
258 0 return $self;
259 }
260  
261  
262 0 ▶sub history {
Subroutine history: not called CC=3 SCAR=24.8
Pod documented
263 0 my ($self, $id, $offset, $limit) = @_;
264  
265 0 ▶ $offset = 0 unless $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset 0 0 0
266 0 ▶ $limit = 30 unless $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 0 0 0
267  
268 0 eval {
269 0 $self->{record_history}->execute( $id, $limit, $offset );
270 0 my $result = $self->{record_history}->fetchall_arrayref({});
271 0 $self->{record_history}->finish();
272 0 return $result;
273 };
274 }
275  
276  
277 0 ▶sub prevnext {
Subroutine prevnext: not called CC=3 SCAR=24.8
Pod documented
278 0 my ($self, $id, $version, $limit) = @_;
279 0 ▶ $limit = 1 unless $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 0 0 0
280  
281 0 my $revisions = {};
282  
283 0 eval {
284 0 $self->{prev_rev}->execute( $id, $version, $limit );
285 0 $revisions = $self->{prev_rev}->fetchall_hashref('version');
286 0 $self->{prev_rev}->finish();
287 0 $self->{next_rev}->execute( $id, $version, $limit );
288 0 my $result = $self->{next_rev}->fetchall_hashref('version');
289 0 $self->{next_rev}->finish();
290 0 ▶ while (my ($k,$v) = each %$result) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$result) 0 0 0
291 0 $revisions->{$k} = $v;
292 }
293 };
294  
295 0 return $revisions;
296 }
297  
298  
299 0 ▶sub recentchanges {
Subroutine recentchanges: not called CC=3 SCAR=24.8
Pod documented
300 0 my ($self, $offset, $limit) = @_;
301  
302 0 ▶ $offset = 0 unless $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset 0 0 0
303 0 ▶ $limit = 30 unless $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 0 0 0
304  
305 0 eval {
306 0 $self->{recent_changes}->execute( $limit, $offset );
307 0 my $result = $self->{recent_changes}->fetchall_arrayref({});
308 0 $self->{recent_changes}->finish();
309 0 return $result;
310 };
311 }
312  
313  
314 0 ▶sub contributions {
Subroutine contributions: not called CC=3 SCAR=24.8
Pod documented
315 0 my ($self, $user, $offset, $limit) = @_;
316  
317 0 ▶ $offset = 0 unless $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset 0 0 0
318 0 ▶ $limit = 30 unless $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 0 0 0
319  
320 0 eval {
321 0 $self->{contributions}->execute( $user, $limit, $offset );
322 0 my $result = $self->{contributions}->fetchall_arrayref({});
323 0 $self->{contributions}->finish();
324 0 return $result;
325 };
326 }
327  
328  
329 0 ▶sub deletions {
Subroutine deletions: not called CC=3 SCAR=24.8
Pod documented
330 0 my ($self, $offset, $limit) = @_;
331  
332 0 ▶ $offset = 0 unless $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset 0 0 0
333 0 ▶ $limit = 30 unless $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit 0 0 0
334  
335 0 eval {
336 0 $self->{deleted}->execute( $limit, $offset );
337 0 my $result = $self->{deleted}->fetchall_arrayref({});
338 0 $self->{deleted}->finish();
339 0 return $result;
340 };
341 }
342  
343  
344 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
345 1 my $self = shift;
346 1 $self->{dbh}->disconnect;
347 }
348  
349  
350 4 1;
351  
352 __END__
« Source.pm ↑ Summary SOAPServer.pm »