lib/Data/TagDB.pm

filter: Statementstmt 8.9% 47 / 526 Branchbran 0.0% 0 / 208 Conditioncond 0.0% 0 / 100 MC/DCmcdc 0.0% 0 / 62 Subroutinesub 23.4% 15 / 64 Podpod 92.5% 25 / 27 totaltotal 8.8% 87 / 987 CC 150 SCAR 56.6
CC
150
Cov
6%
tag_by_specification
1122.0
_build_query
600.0
create_tag
272.0
CRAP 286.5
« lib/Data/TagDB/Migration.pm ↑ Summary lib/Data/TagDB/Iterator.pm »
1 # Copyright (c) 2024-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: Work with Tag databases
6  
7 package Data::TagDB;
8  
9 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Scalar::Util qw(weaken blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use Data::TagDB::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Data::TagDB::Relation;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Data::TagDB::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Data::TagDB::LinkIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Data::TagDB::MultiIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Data::TagDB::WellKnown;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Data::TagDB::Cloudlet;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Data::URIID::Colour;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 ▶use parent 'Data::TagDB::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 our $VERSION = v0.16;
30  
31 4 my %_queries = (
32 _default => {
33 tag_by_hint => 'SELECT tag FROM hint WHERE name = ?',
34 _tag_simple_identifier => 'SELECT data FROM metadata WHERE relation = (SELECT tag FROM hint WHERE name = \'also-shares-identifier\') AND type = (SELECT tag FROM hint WHERE name = ?) AND context = 0 AND encoding = 0 AND tag = ? ORDER BY data DESC',
35 _tag_by_dbid_type_and_data => 'SELECT tag FROM metadata WHERE relation = (SELECT tag FROM hint WHERE name = \'also-shares-identifier\') AND type = ? AND context = 0 AND encoding = 0 AND data = ?',
36 _create_tag => 'INSERT INTO tag DEFAULT VALUES',
37 _create_metadata => 'INSERT OR IGNORE INTO metadata (tag,relation,context,type,encoding,data) VALUES (?,?,?,?,?,?)',
38 _create_relation => 'INSERT OR IGNORE INTO relation (tag,relation,related,context,filter) VALUES (?,?,?,?,?)',
39 },
40 Pg => {
41 _create_tag => 'INSERT INTO tag DEFAULT VALUES RETURNING id',
42 _create_metadata => 'INSERT INTO metadata (tag,relation,context,type,encoding,data) VALUES (?,?,?,?,?,?) ON CONFLICT DO NOTHING',
43 _create_relation => 'INSERT INTO relation (tag,relation,related,context,filter) VALUES (?,?,?,?,?) ON CONFLICT DO NOTHING',
44 },
45 );
46  
47  
48  
49 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
50 0 my ($pkg, $first, @rest) = @_;
51 0 my $DBI_name;
52 my $dbh;
53 0 my %query;
54  
55 0 ▶ croak 'No dsn or dbh given to new' unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 0 0 0
56  
57 0 ▶ if (scalar(@rest) == 0 && eval { $first->can('prepare'); }) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rest == 0 and eval { do { $first->can('prepare') } }) { } 0 0 0
Condition0%0/3
scalar @rest == 0 and eval { do { $first->can("prepare") } }
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @rest == 0 and eval { do { $first->can("prepare") } }
scalar @rest == 0 eval { do { $first->can("prepare") } }
Truth table0%0/3
A and B
A= scalar @rest == 0B= eval { do { $first->can("prepare") } }
ABresult
0X0
100
111
58 0 $dbh = $first;
59 } else {
60 0 ▶ $dbh = DBI->connect($first, @rest) or croak 'Cannot connect to database';
Branch: 1 branch
BranchTrueFalseTotal
$dbh = "DBI"->connect($first, @rest) or croak("Cannot connect to database") 0 0 0
61 }
62  
63 0 $DBI_name = $dbh->{Driver}{Name};
64 0 foreach my $name (keys %{$_queries{_default}}) {
65 0 ▶ $query{$name} = $dbh->prepare($_queries{$DBI_name}{$name} // $_queries{_default}{$name});
Condition0%0/3
$_queries{$DBI_name}{$name} // $_queries{"_default"}{$name}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_queries{$DBI_name}{$name} // $_queries{"_default"}{$name}
$_queries{$DBI_name}{$name} $_queries{"_default"}{$name}
Truth table0%0/3
A // B
A= $_queries{$DBI_name}{$name}B= $_queries{"_default"}{$name}
ABresult
1X1
011
000
66 }
67  
68 0 return bless {
69 dbh => $dbh,
70 _DBI_name => $DBI_name,
71 cache_tag => {},
72 cache_ise => {},
73 cache_default_type => {},
74 cache_default_encoding => {},
75 backup_type => {},
76 query => \%query,
77 }, $pkg;
78 }
79  
80  
81 0 ▶sub dbh {
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
82 0 my ($self) = @_;
83 0 return $self->{dbh};
84 }
85  
86  
87 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
88 0 my ($self) = @_;
89 0 $self->assert_connected->disconnect;
90 0 $self->{dbh} = undef;
91 0 $self->clear_cache;
92 }
93  
94  
95 0 ▶sub tag_by_id {
Subroutine tag_by_id: not called CC=6 SCAR=37.4
Pod documented
96 0 my ($self, $type, $id, $autocreate) = @_;
97  
98 # Shift arguments into correct order as needed:
99 0 ▶ if (blessed($type) && $type->isa('Data::Identifier')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($type) and $type->isa("Data::Identifier")) 0 0 0
Condition0%0/3
builtin::blessed($type) and $type->isa("Data::Identifier")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($type) and $type->isa("Data::Identifier")
builtin::blessed($type) $type->isa("Data::Identifier")
Truth table0%0/3
A and B
A= builtin::blessed($type)B= $type->isa("Data::Identifier")
ABresult
0X0
100
111
100 0 ($self, $id, $autocreate) = @_;
101  
102 0 $type = $id->type;
103  
104 # Is $type === UUID?
105 # TODO: Make this a better check.
106 0 ▶ if ($type->eq('uuid')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->eq('uuid')) { } 0 0 0
107 0 $type = $self->tag_by_hint('uuid');
108 } else {
109 0 $type = $self->tag_by_id($type);
110 }
111  
112 0 $id = $id->id;
113 }
114  
115 0 ▶ $type = $self->tag_by_hint($type) unless eval { $type->isa('Data::TagDB::Tag') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $type->isa("Data::TagDB::Tag") } } 0 0 0
116  
117 0 ▶ if ($autocreate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autocreate) { } 0 0 0
118 0 return $self->create_tag([$type => $id]);
119 } else {
120 0 return $self->tag_by_dbid($self->_get_data(_tag_by_dbid_type_and_data => ($type->dbid, $id)));
121 }
122 }
123  
124  
125 0 ▶sub tag_by_specification {
Subroutine tag_by_specification: not called CC=33 SCAR=70.2
Pod documented
126 0 my ($self, $specification, %opts) = @_;
127 0 my $wk = $self->wk;
128 0 my $style = $opts{style};
129 0 my $important = $opts{important};
130 0 my $role = $opts{role};
131 0 my @candidates;
132  
133 0 ▶ croak 'No style given' unless defined($style) && length($style);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $style and length $style 0 0 0
Condition0%0/3
defined $style and length $style
!ll&&!rl&&r
000
MC/DC0%0/2
defined $style and length $style
defined $style length $style
Truth table0%0/3
A and B
A= defined $styleB= length $style
ABresult
0X0
100
111
134  
135 0 ▶ if ($style eq 'ise') {
Branch: 3 branches
BranchTrueFalseTotal
if ($style eq 'ise') { } 0 0 0
elsif ($style eq 'tagpool') { } 0 0 0
elsif ($style eq 'sirtx') { } 0 0 0
136 0 ▶ @candidates = (eval { $self->tag_by_id(uuid => $specification) } // eval { $self->tag_by_id(oid => $specification) } // eval { $self->tag_by_id(uri => $specification) });
Condition0%0/6
(eval { do { $self->tag_by_id("uuid", $specification) } } // eval { do { $self->tag_by_id("oid", $specification) } }) // eval { do { $self->tag_by_id("uri", $specification) } }
l!l&&r!l&&!r
000
eval { do { $self->tag_by_id("uuid", $specification) } } // eval { do { $self->tag_by_id("oid", $specification) } }
l!l&&r!l&&!r
000
MC/DC0%0/3
(eval { do { $self->tag_by_id("uuid", $specification) } } // eval { do { $self->tag_by_id("oid", $specification) } }) // eval { do { $self->tag_by_id("uri", $specification) } }
eval { do { $self->tag_by_id("uuid", $specification) } } eval { do { $self->tag_by_id("oid", $specification) } } eval { do { $self->tag_by_id("uri", $specification) } }
Truth table0%0/4
A // B // C
A= eval { do { $self->tag_by_id("uuid", $specification) } }B= eval { do { $self->tag_by_id("oid", $specification) } }C= eval { do { $self->tag_by_id("uri", $specification) } }
ABCresult
1XX1
01X1
0011
0000
137 } elsif ($style eq 'tagpool') {
138 0 ▶ unless ($opts{as_is}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"as_is"}) 0 0 0
139 0 ▶ $important ||= $specification =~ s/\!$//;
Condition0%0/3
$important ||= $specification =~ s/\!$//
l!l&&r!l&&!r
000
MC/DC0%0/2
$important ||= $specification =~ s/\!$//
$important $specification =~ s/\!$//
Truth table0%0/3
A ||= B
A= $importantB= $specification =~ s/\!$//
ABresult
1X1
011
000
140  
141 0 ▶ if (!defined($role) && $specification =~ s/^(.+)\@([^@]+)$/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $role and $specification =~ s/^(.+)\@([^\@]+)$/$2/) 0 0 0
Condition0%0/3
not defined $role and $specification =~ s/^(.+)\@([^\@]+)$/$2/
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $role and $specification =~ s/^(.+)\@([^\@]+)$/$2/
not defined $role $specification =~ s/^(.+)\@([^\@]+)$/$2/
Truth table0%0/3
not(A) and B
A= not defined $roleB= $specification =~ s/^(.+)\@([^\@]+)$/$2/
ABresult
0X0
100
111
142 0 $role = $self->tag_by_specification($1, %opts);
143 }
144 }
145  
146 0 ▶ if ($specification =~ /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($specification =~ /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/) 0 0 0
147 0 return $self->tag_by_id(uuid => $specification);
148 }
149  
150 0 @candidates = $self->metadata(
151 relation => $wk->also_shares_identifier,
152 type => $wk->tagname,
153 encoding => undef,
154 data_raw => $specification,
155 )->collect('tag');
156 } elsif ($style eq 'sirtx') {
157 0 my ($type, $id);
158 0 my $backup_type;
159  
160 0 $specification =~ s/^\[(.+)\]$/$1/;
161  
162 0 ▶ if ($specification =~ /^\/([0-9]+)$/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($specification =~ m[^/([0-9]+)$]) { } 0 0 0
elsif ($specification =~ m[^/([a-z_]+)$]) { } 0 0 0
elsif ($specification =~ /^\*([0-9]+)$/) { } 0 0 0
elsif ($specification eq '*') { } 0 0 0
elsif ($specification =~ /^\'([0-9]+)$/) { } 0 0 0
elsif ($specification eq q[']) { } 0 0 0
elsif ($specification =~ /^[\&\%]([0-9a-zA-Z_]+)$/) { } 0 0 0
elsif ($specification =~ /^(.+):(.+)$/) { } 0 0 0
163 0 $id = $1;
164 0 $type = $wk->sirtx_function_number;
165 } elsif ($specification =~ /^\/([a-z_]+)$/) {
166 0 $id = $1;
167 0 $type = $wk->sirtx_function_name;
168 } elsif ($specification =~ /^\*([0-9]+)$/) {
169 0 @candidates = ($opts{sirtx_local_ids}{int $1});
170 } elsif ($specification eq '*') {
171 0 @candidates = ($opts{sirtx_local_ids}{0});
172 } elsif ($specification =~ /^\'([0-9]+)$/) {
173 0 my $num = int($1);
174 0 require Data::Identifier::Generate;
175 0 my $id = Data::Identifier::Generate->integer($num);
176 0 @candidates = ($self->tag_by_id($id));
177 } elsif ($specification eq '\'') {
178 0 @candidates = ($wk->zero);
179 } elsif ($specification =~ /^[\&\%]([0-9a-zA-Z_]+)$/) {
180 0 my $port_tag = $self->tag_by_specification($1, %opts);
181 0 my $ports = $opts{sirtx_ports};
182 0 my $len = scalar(@{$ports});
183  
184 0 ▶ for (my $i = 0; !scalar(@candidates) && $i < $len; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar @candidates and $i < $len) 0 0 0
Condition0%0/3
not scalar @candidates and $i < $len
!ll&&!rl&&r
000
MC/DC0%0/2
not scalar @candidates and $i < $len
not scalar @candidates $i < $len
Truth table0%0/3
not(A) and B
A= not scalar @candidatesB= $i < $len
ABresult
0X0
100
111
185 0 my $p = $ports->[$i];
186 0 ▶ if ($port_tag == $p || $port_tag->dbid eq $p->dbid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port_tag == $p or $port_tag->dbid eq $p->dbid) 0 0 0
Condition0%0/3
$port_tag == $p or $port_tag->dbid eq $p->dbid
l!l&&r!l&&!r
000
MC/DC0%0/2
$port_tag == $p or $port_tag->dbid eq $p->dbid
$port_tag == $p $port_tag->dbid eq $p->dbid
Truth table0%0/3
A or B
A= $port_tag == $pB= $port_tag->dbid eq $p->dbid
ABresult
1X1
011
000
187 0 @candidates = ($ports->[$i+1]);
188 }
189 }
190 } elsif ($specification =~ /^(.+):(.+)$/) {
191 0 ($type, $id) = ($1, $2);
192 } else {
193 0 $type = $wk->sirtx_logical;
194 0 $backup_type = $wk->sirtx_function_name;
195 0 $id = $specification;
196 }
197  
198 0 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 0 0 0
199 0 ▶ unless (ref $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $type) 0 0 0
200 0 $type = $self->tag_by_specification($type, %opts);
201 }
202  
203 0 @candidates = $self->metadata(
204 relation => $wk->also_shares_identifier,
205 type => $type,
206 encoding => undef,
207 data_raw => $id,
208 )->collect('tag');
209  
210 0 ▶ if (scalar(@candidates) == 0 && defined($backup_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @candidates == 0 and defined $backup_type) 0 0 0
Condition0%0/3
scalar @candidates == 0 and defined $backup_type
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @candidates == 0 and defined $backup_type
scalar @candidates == 0 defined $backup_type
Truth table0%0/3
A and B
A= scalar @candidates == 0B= defined $backup_type
ABresult
0X0
100
111
211 0 @candidates = $self->metadata(
212 relation => $wk->also_shares_identifier,
213 type => $backup_type,
214 encoding => undef,
215 data_raw => $id,
216 )->collect('tag');
217 }
218 }
219 } else {
220 0 croak 'Invalid/unsupported style: '.$style;
221 }
222  
223 0 ▶ if ($important) {
Branch: 1 branch
BranchTrueFalseTotal
if ($important) 0 0 0
224 0 @candidates = $self->relation(
225 tag => \@candidates,
226 relation => $wk->flagged_as,
227 related => $wk->important,
228 )->collect('tag');
229 }
230  
231 0 ▶ if (defined $role) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $role) 0 0 0
232 @candidates = grep {
233 0 $_->cloudlet('roles')->is_entry($role)
234 } @candidates;
235 }
236  
237 0 ▶ if (scalar(@candidates) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @candidates == 1) { } 0 0 0
elsif (scalar @candidates > 1) { } 0 0 0
238 0 return $candidates[0];
239 } elsif (scalar(@candidates) > 1) {
240 0 croak 'Nore than one match found';
241 } else {
242 0 croak 'Tag not found';
243 }
244  
245 0 die 'BUG';
246 }
247  
248  
249 #@returns Data::TagDB::Iterator
250 0 ▶sub relation {
Subroutine relation: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($self, %opts) = @_;
252 0 return $self->_link_iterator(%opts, package => 'Data::TagDB::Relation');
253 }
254  
255  
256 #@returns Data::TagDB::Iterator
257 0 ▶sub metadata {
Subroutine metadata: not called CC=1 SCAR=6.9
Pod documented
258 0 my ($self, %opts) = @_;
259 0 return $self->_link_iterator(%opts, package => 'Data::TagDB::Metadata');
260 }
261  
262  
263 #@returns Data::TagDB::Iterator
264 0 ▶sub link {
Subroutine link: not called CC=1 SCAR=6.9
Pod documented
265 0 my ($self, %opts) = @_;
266 0 return Data::TagDB::MultiIterator->new(db => $self, iterators => [
267 $self->metadata(%opts),
268 $self->relation(%opts),
269 ]);
270 }
271  
272  
273 0 ▶sub wk {
Subroutine wk: not called CC=2 SCAR=17.9
Pod documented
274 0 my ($self) = @_;
275 0 ▶ return $self->{wk} //= Data::TagDB::WellKnown->_new(db => $self);
Condition0%0/3
$self->{"wk"} //= "Data::TagDB::WellKnown"->_new("db", $self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"wk"} //= "Data::TagDB::WellKnown"->_new("db", $self)
$self->{"wk"} "Data::TagDB::WellKnown"->_new("db", $self)
Truth table0%0/3
A //= B
A= $self->{"wk"}B= "Data::TagDB::WellKnown"->_new("db", $self)
ABresult
1X1
011
000
276 }
277  
278  
279 0 ▶sub register_decoder {
Subroutine register_decoder: not called CC=3 SCAR=24.8
Pod documented
280 0 my ($self, $type, $encoding, $decoder) = @_;
281 0 ▶ my $decoders = $self->{decoders} //= $self->_register_basic_decoders;
Condition0%0/3
$self->{"decoders"} //= $self->_register_basic_decoders
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"decoders"} //= $self->_register_basic_decoders
$self->{"decoders"} $self->_register_basic_decoders
Truth table0%0/3
A //= B
A= $self->{"decoders"}B= $self->_register_basic_decoders
ABresult
1X1
011
000
282 0 ▶ $decoders->{$type->dbid} //= {};
Condition0%0/2
$$decoders{$type->dbid} //= {}
l!l
00
MC/DC0%0/1
$$decoders{$type->dbid} //= {}
$$decoders{$type->dbid}
Truth table0%0/2
A
A= $$decoders{$type->dbid}
Aresult
11
00
283 0 $decoders->{$type->dbid}{$encoding->dbid} = $decoder;
284 }
285  
286  
287 0 ▶sub create_tag {
Subroutine create_tag: not called CC=16 SCAR=56.1
Pod documented
288 0 my ($self, $ids, $addional_ids) = @_;
289 0 my $asi = $self->wk->also_shares_identifier;
290 0 my $asi_dbid = $asi->dbid;
291 0 my $query_tpl = 'SELECT tag FROM metadata WHERE relation = '.$asi_dbid.' AND type = ? AND context = 0 AND encoding = 0 AND data = ?';
292 0 my $query = '';
293 0 my @bind;
294 my $row;
295 0 my $tag;
296  
297 0 ▶ if (blessed($ids) && $ids->isa('Data::Identifier')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($ids) and $ids->isa("Data::Identifier")) 0 0 0
Condition0%0/3
builtin::blessed($ids) and $ids->isa("Data::Identifier")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ids) and $ids->isa("Data::Identifier")
builtin::blessed($ids) $ids->isa("Data::Identifier")
Truth table0%0/3
A and B
A= builtin::blessed($ids)B= $ids->isa("Data::Identifier")
ABresult
0X0
100
111
298 0 $ids = [$self->tag_by_id($ids->type) => $ids->id];
299 }
300  
301 0 ▶ if (blessed($addional_ids) && $addional_ids->isa('Data::Identifier')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($addional_ids) and $addional_ids->isa("Data::Identifier")) 0 0 0
Condition0%0/3
builtin::blessed($addional_ids) and $addional_ids->isa("Data::Identifier")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($addional_ids) and $addional_ids->isa("Data::Identifier")
builtin::blessed($addional_ids) $addional_ids->isa("Data::Identifier")
Truth table0%0/3
A and B
A= builtin::blessed($addional_ids)B= $addional_ids->isa("Data::Identifier")
ABresult
0X0
100
111
302 0 $addional_ids = [$self->tag_by_id($addional_ids->type) => $addional_ids->id];
303 }
304  
305 0 ▶ for (my $i = 0; $i < scalar(@{$ids}); $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$ids;}) 0 0 0
306 0 my $type = $ids->[$i + 0];
307 0 my $value = $ids->[$i + 1];
308  
309 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
310  
311 0 ▶ $query .= ' UNION ' if length $query;
Branch: 1 branch
BranchTrueFalseTotal
if length $query 0 0 0
312  
313 0 $query .= $query_tpl;
314 0 push(@bind, $type->dbid, $value);
315 }
316  
317 0 $query = $self->dbh->prepare($query);
318 0 $query->execute(@bind);
319 0 $row = $query->fetchrow_arrayref;
320 0 $query->finish;
321  
322 0 ▶ if (defined($row) && defined($row->[0]) && $row->[0] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row and defined $row->[0] and $row->[0] > 0) { } 0 0 0
Condition0%0/6
defined $row and defined $row->[0] and $row->[0] > 0
!ll&&!rl&&r
000
defined $row and defined $row->[0]
!ll&&!rl&&r
000
MC/DC0%0/3
defined $row and defined $row->[0] and $row->[0] > 0
defined $row defined $row->[0] $row->[0] > 0
Truth table0%0/4
A and B and C
A= defined $rowB= defined $row->[0]C= $row->[0] > 0
ABCresult
0XX0
10X0
1100
1111
323 0 $tag = $self->tag_by_dbid($row->[0]);
324 } else {
325 0 $query = $self->_query('_create_tag');
326 0 $query->execute;
327 0 ▶ if ($self->_DBI_name eq 'Pg') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_DBI_name eq 'Pg') { } 0 0 0
328 0 my $row = $query->fetchrow_arrayref;
329 0 $tag = $self->tag_by_dbid($row->[0]);
330 } else {
331 0 $tag = $self->tag_by_dbid($query->last_insert_id);
332 }
333 0 $query->finish;
334 }
335  
336 0 ▶ for (my $i = 0; $i < scalar(@{$ids}); $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$ids;}) 0 0 0
337 0 my $type = $ids->[$i + 0];
338 0 my $value = $ids->[$i + 1];
339  
340 0 $self->create_metadata(tag => $tag, relation => $asi, type => $type, data_raw => $value);
341 }
342  
343 0 ▶ if (defined $addional_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addional_ids) 0 0 0
344 0 ▶ for (my $i = 0; $i < scalar(@{$addional_ids}); $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$addional_ids;}) 0 0 0
345 0 my $type = $addional_ids->[$i + 0];
346 0 my $value = $addional_ids->[$i + 1];
347  
348 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
349  
350 0 $self->create_metadata(tag => $tag, relation => $asi, type => $type, data_raw => $value);
351 }
352 }
353  
354 0 return $tag;
355 }
356  
357  
358 0 ▶sub create_metadata {
Subroutine create_metadata: not called CC=1 SCAR=6.9
Pod documented
359 0 my ($self, %opts) = @_;
360 0 my $query = $self->_query('_create_metadata');
361 my @bind = (
362 $self->_as_tag($opts{tag}, 1)->dbid,
363 $self->_as_tag($opts{relation}, 1)->dbid,
364 Data::TagDB::Tag::dbid($self->_as_tag($opts{context}, 1)),
365 Data::TagDB::Tag::dbid($self->_as_tag($opts{type}, 1)),
366 Data::TagDB::Tag::dbid($self->_as_tag($opts{encoding}, 1)),
367 $opts{data_raw},
368 0 );
369  
370 0 $query->execute(@bind);
371 0 $query->finish;
372  
373 0 return Data::TagDB::Metadata->_new(%opts, db => $self);
374 }
375  
376  
377 0 ▶sub create_relation {
Subroutine create_relation: not called CC=1 SCAR=6.9
Pod documented
378 0 my ($self, %opts) = @_;
379 0 my $query = $self->_query('_create_relation');
380 my @bind = (
381 $self->_as_tag($opts{tag}, 1)->dbid,
382 $self->_as_tag($opts{relation}, 1)->dbid,
383 $self->_as_tag($opts{related}, 1)->dbid,
384 Data::TagDB::Tag::dbid($self->_as_tag($opts{context}), 1),
385 0 Data::TagDB::Tag::dbid($self->_as_tag($opts{filter}), 1),
386 );
387  
388 0 $query->execute(@bind);
389 0 $query->finish;
390  
391 0 return Data::TagDB::Relation->_new(%opts, db => $self);
392 }
393  
394  
395 0 ▶sub create_cache {
Subroutine create_cache: not called CC=1 SCAR=6.9
Pod documented
396 0 my ($self) = @_;
397 0 require Data::TagDB::Cache;
398 0 return Data::TagDB::Cache->_new(db => $self);
399 }
400  
401  
402 0 ▶sub master_cache {
Subroutine master_cache: not called CC=2 SCAR=17.9
Pod documented
403 0 my ($self, $cache) = @_;
404  
405 0 ▶ if (defined $cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache) 0 0 0
406 0 $self->{master_cache} = $cache;
407 0 weaken($self->{master_cache});
408 }
409  
410 0 return $self->{master_cache};
411 }
412  
413  
414 0 ▶sub migration {
Subroutine migration: not called CC=2 SCAR=17.9
Pod documented
415 0 my ($self) = @_;
416 0 require Data::TagDB::Migration;
417 0 ▶ return $self->{migration} //= Data::TagDB::Migration->_new(db => $self);
Condition0%0/3
$self->{"migration"} //= "Data::TagDB::Migration"->_new("db", $self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"migration"} //= "Data::TagDB::Migration"->_new("db", $self)
$self->{"migration"} "Data::TagDB::Migration"->_new("db", $self)
Truth table0%0/3
A //= B
A= $self->{"migration"}B= "Data::TagDB::Migration"->_new("db", $self)
ABresult
1X1
011
000
418 }
419  
420  
421 0 ▶sub factory {
Subroutine factory: not called CC=2 SCAR=17.9
Pod documented
422 0 my ($self) = @_;
423 0 require Data::TagDB::Factory;
424 0 ▶ return $self->{factory} //= Data::TagDB::Factory->_new(db => $self);
Condition0%0/3
$self->{"factory"} //= "Data::TagDB::Factory"->_new("db", $self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"factory"} //= "Data::TagDB::Factory"->_new("db", $self)
$self->{"factory"} "Data::TagDB::Factory"->_new("db", $self)
Truth table0%0/3
A //= B
A= $self->{"factory"}B= "Data::TagDB::Factory"->_new("db", $self)
ABresult
1X1
011
000
425 }
426  
427  
428 0 ▶sub exporter {
Subroutine exporter: not called CC=1 SCAR=6.9
Pod documented
429 0 my ($self, $target, %opts) = @_;
430 0 require Data::TagDB::Exporter;
431 0 return Data::TagDB::Exporter->_new(db => $self, target => $target, %opts);
432 }
433  
434  
435 0 ▶sub begin_work {
Subroutine begin_work: not called CC=3 SCAR=24.8
Pod documented
436 0 my ($self, @args) = @_;
437 0 ▶ croak 'Transaction already in process' if $self->{transaction_refc} || defined($self->{transaction_type});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transaction_refc"} or defined $self->{"transaction_type"} 0 0 0
Condition0%0/3
$self->{"transaction_refc"} or defined $self->{"transaction_type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"transaction_refc"} or defined $self->{"transaction_type"}
$self->{"transaction_refc"} defined $self->{"transaction_type"}
Truth table0%0/3
A or B
A= $self->{"transaction_refc"}B= defined $self->{"transaction_type"}
ABresult
1X1
011
000
438 0 $self->{transaction_refc} = 1;
439 0 return $self->dbh->begin_work(@args);
440 }
441  
442 0 ▶sub commit {
Subroutine commit: not called CC=3 SCAR=24.8
Pod documented
443 0 my ($self, @args) = @_;
444 0 ▶ croak 'No transaction in process' unless $self->{transaction_refc};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transaction_refc"} 0 0 0
445 0 $self->{transaction_refc}--;
446 0 ▶ return if $self->{transaction_refc};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transaction_refc"} 0 0 0
447 0 return $self->dbh->commit(@args);
448 }
449  
450 0 ▶sub rollback {
Subroutine rollback: not called CC=3 SCAR=24.8
Pod documented
451 0 my ($self, @args) = @_;
452 0 ▶ croak 'No transaction in process' unless $self->{transaction_refc};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"transaction_refc"} 0 0 0
453 0 $self->{transaction_refc}--;
454 0 ▶ return if $self->{transaction_refc};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transaction_refc"} 0 0 0
455 0 return $self->dbh->rollback(@args);
456 }
457  
458  
459 0 ▶sub in_transaction {
Subroutine in_transaction: not called CC=12 SCAR=50.5
Pod documented
460 0 my ($self, $type, $code) = @_;
461 0 my $error;
462  
463 0 ▶ croak 'Bad transaction type' unless $type eq 'ro' || $type eq 'rw';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "ro" or $type eq "rw" 0 0 0
Condition0%0/3
$type eq "ro" or $type eq "rw"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "ro" or $type eq "rw"
$type eq "ro" $type eq "rw"
Truth table0%0/3
A or B
A= $type eq "ro"B= $type eq "rw"
ABresult
1X1
011
000
464 0 ▶ croak 'Transaction already in process' if $self->{transaction_refc};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transaction_refc"} 0 0 0
465  
466 0 ▶ unless (defined($self->{transaction_type})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"transaction_type"}) 0 0 0
467 0 $self->{transaction_type} = $type;
468 0 $self->{transaction_open} = 0;
469 0 $self->dbh->begin_work;
470 }
471  
472 0 ▶ if ($self->{transaction_type} eq $type || $self->{transaction_type} eq 'rw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'transaction_type'} eq $type or $self->{'transaction_type'} eq 'rw') { } 0 0 0
elsif ($self->{'transaction_type'} eq 'ro' and $type eq 'rw') { } 0 0 0
Condition0%0/3
$self->{"transaction_type"} eq $type or $self->{"transaction_type"} eq "rw"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"transaction_type"} eq $type or $self->{"transaction_type"} eq "rw"
$self->{"transaction_type"} eq $type $self->{"transaction_type"} eq "rw"
Truth table0%0/3
A or B
A= $self->{"transaction_type"} eq $typeB= $self->{"transaction_type"} eq "rw"
ABresult
1X1
011
000
473 # no-op
474 ▶ } elsif ($self->{transaction_type} eq 'ro' && $type eq 'rw') {
Condition0%0/3
$self->{"transaction_type"} eq "ro" and $type eq "rw"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"transaction_type"} eq "ro" and $type eq "rw"
$self->{"transaction_type"} eq "ro" $type eq "rw"
Truth table0%0/3
A and B
A= $self->{"transaction_type"} eq "ro"B= $type eq "rw"
ABresult
0X0
100
111
475 0 $self->{transaction_type} = $type;
476 } else {
477 0 $error = 'Transaction type missmatch';
478 }
479  
480 0 ▶ unless (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $error) 0 0 0
481 0 $self->{transaction_open}++;
482 0 eval { $code->() };
483 0 $self->{transaction_open}--;
484 }
485  
486 0 ▶ unless ($self->{transaction_open}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"transaction_open"}) 0 0 0
487 0 delete $self->{transaction_type};
488 0 $self->dbh->commit;
489 }
490  
491 0 ▶ croak $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
492 }
493  
494  
495 0 ▶sub load_plugin {
Subroutine load_plugin: not called CC=9 SCAR=45.0
Pod documented
496 0 my ($self, $plugins, @opts) = @_;
497 0 ▶ my $plugin_infos = $self->{plugin_infos} //= {};
Condition0%0/2
$self->{"plugin_infos"} //= {}
l!l
00
MC/DC0%0/1
$self->{"plugin_infos"} //= {}
$self->{"plugin_infos"}
Truth table0%0/2
A
A= $self->{"plugin_infos"}
Aresult
11
00
498  
499 0 require Data::TagDB::Interface::Plugin;
500  
501 0 ▶ croak 'Stray options passed' if scalar @opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @opts 0 0 0
502  
503 0 ▶ $plugins = [$plugins] unless ref $plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $plugins 0 0 0
504  
505 0 foreach my $plugin_name (@{$plugins}) {
506 0 my $info = $plugin_infos->{$plugin_name};
507  
508 0 ▶ unless (defined $info) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info) 0 0 0
509 0 ▶ unless (exists $Data::TagDB::Interface::Plugin::_known_plugins{$plugin_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Data::TagDB::Interface::Plugin::_known_plugins{$plugin_name}) 0 0 0
510 0 my $fn = $plugin_name =~ s{::|'}{/}gr; # from parent.pm!
511 0 $fn .= '.pm';
512 0 require $fn;
513 }
514  
515 0 ▶ unless (exists $Data::TagDB::Interface::Plugin::_known_plugins{$plugin_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Data::TagDB::Interface::Plugin::_known_plugins{$plugin_name}) 0 0 0
516 0 croak 'Plugin not found: '.$plugin_name;
517 }
518  
519 0 ▶ croak 'Not a valid plugin: '.$plugin_name unless $plugin_name->isa('Data::TagDB::Interface::Plugin');
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin_name->isa("Data::TagDB::Interface::Plugin") 0 0 0
520  
521 0 $plugin_infos->{$plugin_name} = $info = $plugin_name->plugin_attach($self, undef);
522 }
523 }
524 }
525  
526 # ---- Virtual methods ----
527  
528 # ---- Overrides for Data::TagDB::Base ----
529  
530 0 ▶sub db { return $_[0] }
Subroutine db: not called CC=1 SCAR=6.9
Pod documented
531  
532 # ---- Private helpers ----
533  
534 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
535 0 my ($self) = @_;
536 0 eval { $self->disconnect };
537 }
538  
539 0 ▶sub assert_connected {
Subroutine assert_connected: not called CC=2 SCAR=17.9
Pod undocumented
540 0 my ($self) = @_;
541 0 my $dbh = $self->{dbh};
542 0 ▶ confess 'Not connected to any database' unless defined $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh 0 0 0
543 0 return $dbh;
544 }
545  
546 0 ▶sub tag_by_dbid {
Subroutine tag_by_dbid: not called CC=3 SCAR=24.8
Pod undocumented
547 0 my ($self, $dbid) = @_;
548 0 my $cache = $self->{cache_tag};
549 0 ▶ if (defined $cache->{$dbid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache->{$dbid}) { } 0 0 0
550 0 return $cache->{$dbid};
551 } else {
552 0 state $done = 0;
553 0 my $tag = Data::TagDB::Tag->_new(db => $self, dbid => $dbid);
554  
555 0 ▶ if ($done++ > 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done++ > 1024) 0 0 0
556 0 $self->_cache_maintain;
557 0 $done = 0;
558 }
559  
560 0 $cache->{$dbid} = $tag;
561 0 weaken($cache->{$dbid});
562  
563 0 return $tag;
564 }
565 }
566  
567 0 ▶sub _get_plugin_info {
Subroutine _get_plugin_info: not called CC=2 SCAR=17.9
568 0 my ($self, $plugin) = @_;
569 0 ▶ my $plugin_infos = $self->{plugin_infos} //= {};
Condition0%0/2
$self->{"plugin_infos"} //= {}
l!l
00
MC/DC0%0/1
$self->{"plugin_infos"} //= {}
$self->{"plugin_infos"}
Truth table0%0/2
A
A= $self->{"plugin_infos"}
Aresult
11
00
570 0 return $plugin_infos->{$plugin};
571 }
572  
573 0 ▶sub _tag_by_ise_cached {
Subroutine _tag_by_ise_cached: not called CC=2 SCAR=17.9
574 0 my ($self, $ise, $autocreate) = @_;
575 0 ▶ if (defined $self->{cache_ise}{$ise}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cache_ise'}{$ise}) { } 0 0 0
576 0 return $self->tag_by_dbid($self->{cache_ise}{$ise});
577 } else {
578 0 my $tag = $self->tag_by_id(uuid => $ise, $autocreate); # TODO: Allow all ISE here.
579 0 $self->{cache_ise}{$ise} = $tag->dbid;
580 0 return $tag;
581 }
582 }
583  
584 0 ▶sub _cache_maintain {
Subroutine _cache_maintain: not called CC=3 SCAR=24.8
585 0 my ($self) = @_;
586 0 my $cache = $self->{cache_tag};
587  
588 0 foreach my $key (keys %{$cache}) {
589 0 ▶ delete $cache->{$key} unless defined $cache->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cache->{$key} 0 0 0
590 }
591 }
592  
593 0 ▶sub clear_cache {
Subroutine clear_cache: not called CC=2 SCAR=17.9
Pod documented
594 0 my ($self, @opts) = @_;
595 0 $self->SUPER::clear_cache(@opts);
596  
597 0 $self->_cache_maintain;
598 0 %{$self->{cache_ise}} = ();
599 0 $_->clear_cache foreach values %{$self->{cache_tag}};
600  
601 0 return $self;
602 }
603  
604 0 ▶sub _as_tag {
Subroutine _as_tag: not called CC=3 SCAR=24.8
605 0 my ($self, $id, $autocreate) = @_;
606 0 ▶ return undef unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
607 0 ▶ return $id if eval {$id->isa('Data::TagDB::Tag')};
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $id->isa("Data::TagDB::Tag") } } 0 0 0
608 0 return $self->tag_by_id(Data::Identifier->new(from => $id, db => $self), $autocreate);
609 }
610  
611 0 ▶sub _default_type {
Subroutine _default_type: not called CC=4 SCAR=30.0
612 0 my ($self, $relation) = @_;
613 0 my $relation_dbid = $relation->dbid;
614 0 ▶ if (defined $self->{cache_default_type}{$relation_dbid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cache_default_type'}{$relation_dbid}) { } 0 0 0
615 0 return $self->tag_by_dbid($self->{cache_default_type}{$relation_dbid});
616 } else {
617 0 my $type = eval {$self->relation(tag => $relation, relation => $self->wk->default_type)->one->related};
618 0 ▶ if (defined $type) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $type) { } 0 0 0
elsif (defined $self->{'backup_type'}{$relation_dbid}) { } 0 0 0
619 0 $self->{cache_default_type}{$relation_dbid} = $type->dbid;
620 } elsif (defined $self->{backup_type}{$relation_dbid}) {
621 0 return $self->tag_by_dbid($self->{cache_default_type}{$relation_dbid} = $self->{backup_type}{$relation_dbid});
622 } else {
623 0 die 'No default type known';
624 }
625 0 return $type;
626 }
627 }
628  
629 0 ▶sub _default_encoding {
Subroutine _default_encoding: not called CC=2 SCAR=17.9
630 0 my ($self, $type) = @_;
631 0 my $type_dbid = $type->dbid;
632 0 ▶ if (defined $self->{cache_default_encoding}{$type_dbid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'cache_default_encoding'}{$type_dbid}) { } 0 0 0
633 0 return $self->tag_by_dbid($self->{cache_default_encoding}{$type_dbid});
634 } else {
635 0 my $encoding = $self->relation(tag => $type, relation => $self->wk->default_encoding)->one->related;
636 0 $self->{cache_default_encoding}{$type_dbid} = $encoding->dbid;
637 0 return $encoding;
638 }
639 }
640  
641 0 ▶sub _register_backup_type {
Subroutine _register_backup_type: not called CC=1 SCAR=6.9
642 0 my ($self, $relation, $type) = @_;
643 0 $self->{backup_type}{$relation->dbid} = $type->dbid;
644 }
645  
646 0 ▶sub _register_basic_decoders {
Subroutine _register_basic_decoders: not called CC=2 SCAR=17.9
647 0 my ($self) = @_;
648 0 ▶ my $decoders = $self->{decoders} //= {};
Condition0%0/2
$self->{"decoders"} //= {}
l!l
00
MC/DC0%0/1
$self->{"decoders"} //= {}
$self->{"decoders"}
Truth table0%0/2
A
A= $self->{"decoders"}
Aresult
11
00
649 0 my $wk = $self->wk;
650 0 ▶ my $decode_string = sub { $_[0]->data_raw };
Subroutine __ANON__: not called CC=1 SCAR=6.9
651 0 ▶ my $decode_uri = sub { URI->new($_[0]->data_raw) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
652 0 ▶ my $decode_int = sub { my $v = $_[0]->data_raw; croak 'Bad data' unless $v =~ /^[0-9]+$/; int($v) };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /^[0-9]+$/ 0 0 0
653 0 ▶ my $decode_colour = sub { Data::URIID::Colour->new(rgb => $_[0]->data_raw) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
654  
655 0 eval { $self->register_decoder($wk->uuid, $wk->string_ise_uuid_encoding, $decode_string) };
656 0 eval { $self->register_decoder($wk->oid, $wk->string_ise_oid_encoding, $decode_string) };
657 0 eval { $self->register_decoder($wk->uri, $wk->ascii_uri_encoding, $decode_uri) };
658 0 eval { $self->register_decoder($wk->tagname, $wk->utf_8_string_encoding, $decode_string) };
659 0 eval { $self->register_decoder($wk->x11_colour_name, $wk->utf_8_string_encoding, $decode_string) };
660 0 eval { $self->register_decoder($wk->wikidata_identifier, $wk->utf_8_string_encoding, $decode_string) };
661 0 eval { $self->register_decoder($wk->small_identifier, $wk->ascii_decimal_integer_encoding, $decode_int) };
662 0 eval { $self->register_decoder($wk->unsigned_integer, $wk->ascii_decimal_integer_encoding, $decode_int) };
663 0 eval { $self->register_decoder($wk->unicode_string, $wk->utf_8_string_encoding, $decode_string) };
664 0 eval { $self->register_decoder($wk->colour_value, $wk->hex_rgb_encoding, $decode_colour) };
665  
666 0 eval { $self->_register_backup_type($wk->wd_unicode_character, $wk->unicode_string) };
667 0 eval { $self->_register_backup_type($wk->tagpool_tag_icontext, $wk->unicode_string) };
668 0 eval { $self->_register_backup_type($wk->also_has_description, $wk->unicode_string) };
669 0 eval { $self->_register_backup_type($wk->final_file_size, $wk->unsigned_integer) };
670  
671 0 return $decoders;
672 }
673  
674 0 ▶sub _get_decoder {
Subroutine _get_decoder: not called CC=4 SCAR=30.0
675 0 my ($self, $metadata) = @_;
676 0 ▶ my $decoders = $self->{decoders} //= $self->_register_basic_decoders;
Condition0%0/3
$self->{"decoders"} //= $self->_register_basic_decoders
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"decoders"} //= $self->_register_basic_decoders
$self->{"decoders"} $self->_register_basic_decoders
Truth table0%0/3
A //= B
A= $self->{"decoders"}B= $self->_register_basic_decoders
ABresult
1X1
011
000
677 0 ▶ my $for_type = $decoders->{$metadata->type_evaluated->dbid} //= {};
Condition0%0/2
$$decoders{$metadata->type_evaluated->dbid} //= {}
l!l
00
MC/DC0%0/1
$$decoders{$metadata->type_evaluated->dbid} //= {}
$$decoders{$metadata->type_evaluated->dbid}
Truth table0%0/2
A
A= $$decoders{$metadata->type_evaluated->dbid}
Aresult
11
00
678 0 ▶ return $for_type->{$metadata->encoding_evaluated->dbid} // croak 'No matching decoder found';
Condition0%0/2
$$for_type{$metadata->encoding_evaluated->dbid} // croak("No matching decoder found")
l!l
00
MC/DC0%0/1
$$for_type{$metadata->encoding_evaluated->dbid} // croak("No matching decoder found")
$$for_type{$metadata->encoding_evaluated->dbid}
Truth table0%0/2
A
A= $$for_type{$metadata->encoding_evaluated->dbid}
Aresult
11
00
679 }
680  
681 0 ▶sub _DBI_name {
Subroutine _DBI_name: not called CC=2 SCAR=17.9
682 0 my ($self) = @_;
683 0 ▶ return $self->{_DBI_name} //= $self->dbh->{Driver}{Name};
Condition0%0/3
$self->{"_DBI_name"} //= $self->dbh->{"Driver"}{"Name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_DBI_name"} //= $self->dbh->{"Driver"}{"Name"}
$self->{"_DBI_name"} $self->dbh->{"Driver"}{"Name"}
Truth table0%0/3
A //= B
A= $self->{"_DBI_name"}B= $self->dbh->{"Driver"}{"Name"}
ABresult
1X1
011
000
684 }
685  
686 0 ▶sub _query {
Subroutine _query: not called CC=2 SCAR=17.9
687 0 my ($self, $name) = @_;
688 0 $self->assert_connected;
689 0 ▶ return $self->{query}{$name} // confess 'No such query: '.$name;
Condition0%0/2
$self->{"query"}{$name} // confess('No such query: ' . $name)
l!l
00
MC/DC0%0/1
$self->{"query"}{$name} // confess('No such query: ' . $name)
$self->{"query"}{$name}
Truth table0%0/2
A
A= $self->{"query"}{$name}
Aresult
11
00
690 }
691  
692 0 ▶sub _get_data {
Subroutine _get_data: not called CC=2 SCAR=17.9
693 0 my ($self, $name, @args) = @_;
694 0 my $query = $self->_query($name);
695 0 my $row;
696  
697 0 $query->execute(@args);
698 0 $row = $query->fetchrow_arrayref;
699 0 $query->finish;
700  
701 0 ▶ croak 'No such entry' unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
702  
703 0 return $row->[0];
704 }
705  
706 0 ▶sub _build_query {
Subroutine _build_query: not called CC=24 SCAR=64.0
707 0 my ($self, %opts) = @_;
708 0 my %parts;
709 my @where;
710 0 my @binds;
711  
712 0 ▶ if ($opts{package} eq 'Data::TagDB::Metadata') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'package'} eq 'Data::TagDB::Metadata') { } 0 0 0
713 0 $parts{FROM} = 'metadata';
714 0 $parts{SELECT} = '*'; # TODO
715 } else {
716 0 $parts{FROM} = 'relation';
717 0 $parts{SELECT} = '*'; # TODO
718 }
719  
720 0 ▶ if (defined $opts{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"limit"}) 0 0 0
721 0 $parts{LIMIT} = $opts{limit};
722 }
723  
724 0 ▶ if (defined $opts{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"offset"}) 0 0 0
725 0 $parts{OFFSET} = $opts{offset};
726 }
727  
728  
729 0 foreach my $key (qw(tag relation context filter related type encoding)) {
730 0 foreach my $neg (0, 1) {
731 0 ▶ my $curkey = ($neg ? 'no_' : '').$key;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 0 0
732 0 ▶ if (defined $opts{$curkey}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$curkey}) 0 0 0
733 0 ▶ my @list = ref($opts{$curkey}) eq 'ARRAY' ? @{$opts{$curkey}} : ($opts{$curkey});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{$curkey} eq 'ARRAY' ? : 0 0 0
734  
735 0 foreach my $ent (@list) {
736 0 ▶ croak 'Something not a Data::TagDB::Tag used as Tag filter' unless $ent->isa('Data::TagDB::Tag');
Branch: 1 branch
BranchTrueFalseTotal
unless $ent->isa("Data::TagDB::Tag") 0 0 0
737 }
738  
739 0 ▶ push(@where, sprintf('%s %sIN (%s)', $key, $neg ? 'NOT ' : '', join(',', map {$_->dbid} @list)));
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 0 0
740 }
741 }
742 }
743  
744 0 ▶ if (defined $opts{data_raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"data_raw"}) 0 0 0
745 0 push(@where, 'data = ?');
746 0 push(@binds, $opts{data_raw});
747 }
748  
749 0 ▶ if (defined $opts{data_raw_nocase}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"data_raw_nocase"}) 0 0 0
750 0 my $DBI_name = $self->_DBI_name;
751  
752 0 ▶ if ($DBI_name eq 'Pg') {
Branch: 2 branches
BranchTrueFalseTotal
if ($DBI_name eq 'Pg') { } 0 0 0
elsif ($DBI_name eq 'SQLite') { } 0 0 0
753 0 push(@where, 'LOWER(data) = LOWER(?)');
754 0 push(@binds, $opts{data_raw_nocase});
755 } elsif ($DBI_name eq 'SQLite') {
756 0 push(@where, 'data = ? COLLATE NOCASE');
757 0 push(@binds, $opts{data_raw_nocase});
758 } else {
759 ...
760 0 }
761 }
762  
763 0 ▶ if (scalar(@where)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @where) 0 0 0
764 0 $parts{WHERE} = join(' AND ', @where);
765 }
766  
767 0 ▶ if (defined $opts{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"order_by"}) 0 0 0
768 0 ▶ my @list = ref($opts{order_by}) eq 'ARRAY' ? @{$opts{order_by}} : ($opts{order_by});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'order_by'} eq 'ARRAY' ? : 0 0 0
769 0 ▶ if (scalar @list) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @list) 0 0 0
770 $parts{ORDER} = 'BY '.join(', ',
771 0 ▶ map {sprintf('%s ASC', $_ eq 'data_int' ? 'CAST(data AS INTEGER)' : $_)} @list
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'data_int' ? : 0 0 0
772 );
773 }
774 }
775  
776 {
777 0 my $q = '';
778 0 my $sth;
779  
780 0 foreach my $key (qw(SELECT FROM WHERE ORDER LIMIT OFFSET)) {
781 0 ▶ if (defined $parts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parts{$key}) 0 0 0
782 0 ▶ $q .= ' ' if length $q;
Branch: 1 branch
BranchTrueFalseTotal
if length $q 0 0 0
783 0 $q .= $key.' '.$parts{$key};
784 }
785 }
786  
787 0 $sth = $self->dbh->prepare($q);
788 0 $sth->execute(@binds);
789 0 return $sth;
790 }
791 }
792  
793 0 ▶sub _link_iterator {
Subroutine _link_iterator: not called CC=3 SCAR=24.8
794 0 my ($self, %opts) = @_;
795 0 my $query = $self->_build_query(%opts);
796 0 my %args;
797  
798 0 ▶ if ($opts{package} eq 'Data::TagDB::Metadata') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'package'} eq 'Data::TagDB::Metadata') { } 0 0 0
799 0 $args{tag_keys} = {map{$_ => $_} qw(type encoding)};
800 0 $args{raw_keys} = {data_raw => 'data'};
801 } else {
802 0 $args{tag_keys} = {map{$_ => $_} qw(filter related)};
803 0 $args{raw_keys} = {}; # empty
804 }
805  
806 # Add common keys:
807 0 $args{tag_keys}{$_} = $_ foreach qw(tag relation context);
808  
809 0 return Data::TagDB::LinkIterator->new(%args, db => $self, query => $query, package => $opts{package});
810 }
811  
812 0 ▶sub _load_cloudlet {
Subroutine _load_cloudlet: not called CC=10 SCAR=47.0
813 0 my ($self, %opts) = @_;
814 0 my $direct = $opts{direct};
815 0 my $indirect = $opts{indirect};
816  
817 0 ▶ $direct = [$direct] unless ref($direct) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $direct eq "ARRAY" 0 0 0
818 0 ▶ $indirect = [$indirect] unless ref($indirect) eq 'ARRAY' || !defined($indirect);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $indirect eq "ARRAY" or not defined $indirect 0 0 0
Condition0%0/3
ref $indirect eq "ARRAY" or not defined $indirect
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $indirect eq "ARRAY" or not defined $indirect
ref $indirect eq "ARRAY" not defined $indirect
Truth table0%0/3
A or not(B)
A= ref $indirect eq "ARRAY"B= not defined $indirect
ABresult
1X1
011
000
819  
820 0 ▶ return Data::TagDB::Cloudlet->new(db => $self, root => []) unless scalar(@{$direct});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$direct;} 0 0 0
821  
822 0 ▶ if (defined($indirect) && !scalar(@{$indirect})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indirect and not scalar @{$indirect;}) 0 0 0
Condition0%0/3
defined $indirect and not scalar @{$indirect;}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $indirect and not scalar @{$indirect;}
defined $indirect not scalar @{$indirect;}
Truth table0%0/3
A and not(B)
A= defined $indirectB= not scalar @{$indirect;}
ABresult
0X0
100
111
823 0 $indirect = undef;
824 }
825  
826 0 ▶ if (defined $opts{indirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'indirect'}) { } 0 0 0
827 0 my $query = 'WITH RECURSIVE X(related,root) AS (SELECT related,true FROM relation WHERE tag = ? AND relation IN ('.join(',', map{'?'} @{$direct}).') UNION SELECT relation.related,false FROM relation, X WHERE relation.relation IN ('.join(',', map{'?'} @{$indirect}).') AND relation.tag = X.related) SELECT related,root FROM X';
828 0 my @bind = ($opts{tag}->dbid, map {$_->dbid} @{$direct}, @{$indirect});
829 0 my $sth = $self->dbh->prepare($query);
830 0 my @root;
831 my @entry;
832  
833 0 $sth->execute(@bind);
834 0 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
835 0 my $ent = $self->tag_by_dbid($row->[0]);
836 0 ▶ if ($row->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[1]) { } 0 0 0
837 0 push(@root, $ent);
838 } else {
839 0 push(@entry, $ent);
840 }
841 }
842 0 $sth->finish;
843 0 return Data::TagDB::Cloudlet->new(db => $self, root => \@root, entry => \@entry);
844 } else {
845 return Data::TagDB::Cloudlet->new(db => $self, root => [
846 0 $self->relation(tag => $opts{tag}, relation => $opts{direct})->collect('related')
847 ]);
848 }
849  
850 # WITH RECURSIVE X(related,root) AS (SELECT related,true FROM relation WHERE tag = 597 AND relation IN (7, 201) UNION SELECT relation.related,false FROM relation, X WHERE relation.relation = 140 AND relation.tag = X.related) SELECT *,(SELECT data FROM metadata WHERE tag = X.related AND relation = 1 AND type = 5 LIMIT 1) FROM X
851 }
852  
853 # Highly experimental!
854 0 ▶sub _delete_tag {
Subroutine _delete_tag: not called CC=1 SCAR=6.9
855 0 my ($self, $tag) = @_;
856 0 my $dbh = $self->dbh;
857 0 my $dbid = $tag->dbid;
858  
859 {
860 0 my @columns = qw(tag relation context type encoding);
861 0 $dbh->do('DELETE FROM metadata WHERE '.join(' OR ', map {$_.' = ?'} @columns), undef, map {$dbid} @columns);
862 }
863 {
864 0 my @columns = qw(tag relation context related filter);
865 0 $dbh->do('DELETE FROM relation WHERE '.join(' OR ', map {$_.' = ?'} @columns), undef, map {$dbid} @columns);
866 }
867  
868 0 $dbh->do('DELETE FROM hint WHERE tag = ?', undef, $dbid);
869 0 $dbh->do('DELETE FROM tag WHERE id = ?', undef, $dbid);
870 }
871  
872 # ---- AUTOLOAD ----
873  
874 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=5 SCAR=34.0
875 0 my ($self, @args) = @_;
876 0 our $AUTOLOAD;
877 0 my $function = $AUTOLOAD =~ s/^.*:://r;
878 0 ▶ my $query = $self->{query}{$function} // confess 'Bad function: '.$function;
Condition0%0/2
$self->{"query"}{$function} // confess('Bad function: ' . $function)
l!l
00
MC/DC0%0/1
$self->{"query"}{$function} // confess('Bad function: ' . $function)
$self->{"query"}{$function}
Truth table0%0/2
A
A= $self->{"query"}{$function}
Aresult
11
00
879  
880 0 ▶ if ($function =~ /^tag_by_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($function =~ /^tag_by_/) { } 0 0 0
881 0 my $row;
882  
883 0 $query->execute(@args);
884 0 $row = $query->fetchrow_hashref;
885 0 $query->finish;
886  
887 0 ▶ croak 'No such tag' unless defined($row->{tag}) && $row->{tag} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{"tag"} and $row->{"tag"} > 0 0 0 0
Condition0%0/3
defined $row->{"tag"} and $row->{"tag"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->{"tag"} and $row->{"tag"} > 0
defined $row->{"tag"} $row->{"tag"} > 0
Truth table0%0/3
A and B
A= defined $row->{"tag"}B= $row->{"tag"} > 0
ABresult
0X0
100
111
888  
889 0 return $self->tag_by_dbid($row->{tag});
890 } else {
891 0 confess 'Unsupported function with know query: '.$function;
892 }
893 }
894  
895 4 1;
896  
897 __END__
« lib/Data/TagDB/Migration.pm ↑ Summary lib/Data/TagDB/Iterator.pm »