RDF/RDB2RDF/Simple.pm

filter: Statementstmt 65.5% 200 / 305 Branchbran 41.0% 69 / 168 Conditioncond 32.0% 34 / 106 MC/DCmcdc 10.7% 7 / 65 Subroutinesub 70.0% 28 / 40 Podpod 50.0% 7 / 14 totaltotal 49.4% 345 / 698 CC 127 SCAR 44.8
CC
127
Cov
52%
_export
420.0
handle_map
103.9
handle_table
77.9
CRAP 88.1
« RDF/RDB2RDF/DatatypeMapper.pm ↑ Summary RDF/RDB2RDF/R2RML.pm »
1 package RDF::RDB2RDF::Simple;
2  
3 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp qw[carp croak];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Digest::MD5 qw[md5_hex];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use JSON qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use overload qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use RDF::Trine qw[statement blank literal];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use RDF::Trine::Namespace qw[rdf rdfs owl xsd];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Scalar::Util qw[blessed];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use URI::Escape::Optimistic qw[uri_escape_optimistic];
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use base qw[
Subroutine BEGIN: called CC=1 SCAR=0.0
20 RDF::RDB2RDF
21 RDF::RDB2RDF::DatatypeMapper
22 5 ];
23  
24 5 our $AUTHORITY = 'cpan:TOBYINK';
25 5 our $VERSION = '0.008';
26  
27 sub new
28 4 ▶{
Subroutine new: called CC=6 SCAR=24.3
Pod documented
29 4 my ($class, %mappings) = @_;
30 4 ▶ my $ns = delete($mappings{-namespaces}) // +{};
Condition50%1/2
delete $mappings{"-namespaces"} // {}
l!l
04
MC/DC0%0/1
delete $mappings{"-namespaces"} // {}
delete $mappings{"-namespaces"}
Truth table50%1/2
A
A= delete $mappings{"-namespaces"}
Aresult
11
00
31 4 ▶ my $base = delete($mappings{-base}) // 'http://example.com/base/';
Condition50%1/2
delete $mappings{"-base"} // "http://example.com/base/"
l!l
04
MC/DC0%0/1
delete $mappings{"-base"} // "http://example.com/base/"
delete $mappings{"-base"}
Truth table50%1/2
A
A= delete $mappings{"-base"}
Aresult
11
00
32 4 ▶ while (my ($k, $v) = each %$ns)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ns) 0 4 4
33 {
34 0 ▶ $ns->{$k} = RDF::Trine::Namespace->new($v)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($v) and $v->isa("RDF::Trine::Namespace") 0 0 0
Condition0%0/3
builtin::blessed($v) and $v->isa("RDF::Trine::Namespace")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($v) and $v->isa("RDF::Trine::Namespace")
builtin::blessed($v) $v->isa("RDF::Trine::Namespace")
Truth table0%0/3
A and B
A= builtin::blessed($v)B= $v->isa("RDF::Trine::Namespace")
ABresult
0X0
100
111
35 unless (blessed($v) and $v->isa('RDF::Trine::Namespace'));
36 }
37 bless {
38 4 mappings => \%mappings,
39 namespaces => $ns,
40 base => $base,
41 }, $class;
42 }
43  
44 sub mappings
45 40 ▶{
Subroutine mappings: called CC=1 SCAR=0.0
Pod documented
46 40 my ($self) = @_;
47 40 return $self->{mappings};
48 }
49  
50 sub namespaces
51 1 ▶{
Subroutine namespaces: called CC=4 SCAR=15.0
Pod documented
52 1 my ($self) = @_;
53 1 my %NS;
54
55 1 %NS = %{ $self->{namespaces} }
56 1 ▶ if (ref $self->{namespaces} eq 'HASH' or blessed($self->{namespaces}));
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"namespaces"} eq "HASH" or builtin::blessed($self->{"namespaces"}) 1 0 1
Condition33%1/3
ref $self->{"namespaces"} eq "HASH" or builtin::blessed($self->{"namespaces"})
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self->{"namespaces"} eq "HASH" or builtin::blessed($self->{"namespaces"})
ref $self->{"namespaces"} eq "HASH" builtin::blessed($self->{"namespaces"})
Truth table33%1/3
A or B
A= ref $self->{"namespaces"} eq "HASH"B= builtin::blessed($self->{"namespaces"})
ABresult
1X1
011
000
57
58 1 ▶ %NS = (
Branch: 1 branch
BranchTrueFalseTotal
unless %NS 0 1 1
59 owl => "$owl",
60 rdf => "$rdf",
61 rdfs => "$rdfs",
62 xsd => "$xsd",
63 ) unless %NS;
64
65 1 return %NS;
66 }
67  
68 sub template
69 19 ▶{
Subroutine template: called CC=13 SCAR=29.8
Pod undocumented
70 19 my ($self, $template, $data, $types, $process) = @_;
71 0 ▶ $process = sub { +shift } unless ref $process eq 'CODE';
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless ref $process eq "CODE" 0 19 19
72
73 19 ▶ if (blessed($template) and $template->isa('RDF::Trine::Node'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($template) and $template->isa("RDF::Trine::Node")) 0 19 19
Condition66%2/3
builtin::blessed($template) and $template->isa("RDF::Trine::Node")
!ll&&!rl&&r
1630
MC/DC0%0/2
builtin::blessed($template) and $template->isa("RDF::Trine::Node")
builtin::blessed($template) $template->isa("RDF::Trine::Node")
Truth table66%2/3
A and B
A= builtin::blessed($template)B= $template->isa("RDF::Trine::Node")
ABresult
0X0
100
111
74 {
75 0 return $template;
76 }
77  
78 19 ▶ if (blessed($template) and $template->isa('RDF::RDB2RDF::R2RML::_COL_'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($template) and $template->isa("RDF::RDB2RDF::R2RML::_COL_")) 3 16 19
Condition66%2/3
builtin::blessed($template) and $template->isa("RDF::RDB2RDF::R2RML::_COL_")
!ll&&!rl&&r
1603
MC/DC50%1/2
builtin::blessed($template) and $template->isa("RDF::RDB2RDF::R2RML::_COL_")
builtin::blessed($template) $template->isa("RDF::RDB2RDF::R2RML::_COL_")
Truth table66%2/3
A and B
A= builtin::blessed($template)B= $template->isa("RDF::RDB2RDF::R2RML::_COL_")
ABresult
0X0
100
111
79 {
80 3 my $col = substr($template, 1, -1);
81 3 return $data->{$col};
82 }
83  
84 16 ▶ $self->{uuid} = Data::UUID->new unless $self->{uuid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uuid"} 3 13 16
85 16 $data->{'+uuid'} = $self->{uuid}->create_str;
86
87 16 $template =~ s(
88 (?<!\\) \{ # opening unescaped brace
89 ( # start capturing
90 (?: \\\} | [^}] )+ # escaped closing braces and non-brace characters
91 ) # finish capturing
92 \} # closing brace
93 )
94 {
95 3 (my $key = $1)
96 =~ s/\\\}/\}/g;
97 3 my ($value, $type);
98
99 3 ▶ if ($key =~ /^"(.+)"$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^"(.+)"$/) { } 0 3 3
100 {
101 0 $value = ($data->{$1});
102 0 ▶ $type = ($types->{$1} // 'varchar');
Condition0%0/2
$types->{$1} // "varchar"
l!l
00
MC/DC0%0/1
$types->{$1} // "varchar"
$types->{$1}
Truth table0%0/2
A
A= $types->{$1}
Aresult
11
00
103 }
104 else
105 {
106 3 ▶ $value = ($data->{$key} // $data->{lc $key});
Condition33%1/3
$data->{$key} // $$data{lc $key}
l!l&&r!l&&!r
300
MC/DC0%0/2
$data->{$key} // $$data{lc $key}
$data->{$key} $$data{lc $key}
Truth table33%1/3
A // B
A= $data->{$key}B= $$data{lc $key}
ABresult
1X1
011
000
107 3 ▶ $type = ($types->{$key} // $types->{lc $key} // '');
Condition40%2/5
($types->{$key} // $$types{lc $key}) // ""
l!l
30
$types->{$key} // $$types{lc $key}
l!l&&r!l&&!r
300
MC/DC0%0/2
($types->{$key} // $$types{lc $key}) // ""
$types->{$key} $$types{lc $key}
Truth table33%1/3
A // B
A= $types->{$key}B= $$types{lc $key}
ABresult
1X1
011
000
108 }
109
110 3 ▶ return unless defined $value; # Argh! return in a regexp!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
111 3 $process->( $self->datatyped_literal($value, $type)->literal_value );
112 }gex;
113
114 16 $template =~ s< \\ ( [{}\\] ) >< $1 >xg;
115
116 16 return $template;
117 }
118  
119 sub template_irisafe
120 19 ▶{
Subroutine template_irisafe: called CC=1 SCAR=0.0
Pod undocumented
121 19 template(@_, \&URI::Escape::Optimistic::uri_escape_optimistic);
122 }
123  
124 sub iri
125 49 ▶{
Subroutine iri: called CC=7 SCAR=26.3
Pod undocumented
126 49 my ($self, $iri, $graph) = @_;
127
128 return
129 49 ▶ unless defined $iri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iri 1 48 49
130 48 ▶ return $iri
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($iri) and $iri->isa("RDF::Trine::Node") 0 48 48
Condition33%1/3
builtin::blessed($iri) and $iri->isa("RDF::Trine::Node")
!ll&&!rl&&r
4800
MC/DC0%0/2
builtin::blessed($iri) and $iri->isa("RDF::Trine::Node")
builtin::blessed($iri) $iri->isa("RDF::Trine::Node")
Truth table33%1/3
A and B
A= builtin::blessed($iri)B= $iri->isa("RDF::Trine::Node")
ABresult
0X0
100
111
131 if blessed($iri) && $iri->isa('RDF::Trine::Node');
132 48 ▶ return blank()
Branch: 1 branch
BranchTrueFalseTotal
if $iri eq "[]" 0 48 48
133 if $iri eq '[]';
134
135 48 ▶ if ($iri =~ /^_:(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($iri =~ /^_:(.*)$/) 0 48 48
136 {
137 0 my $ident = $1;
138 0 $ident =~ s/([^0-9A-Za-wyz])/sprintf('x%04X', ord($1))/eg;
139 0 ▶ if ($graph)
Branch: 1 branch
BranchTrueFalseTotal
if ($graph) 0 0 0
140 {
141 0 $ident = md5_hex("$graph").$ident;
142 }
143 0 return blank($ident);
144 }
145
146 48 return RDF::Trine::Node::Resource->new("$iri", $self->{base});
147 }
148  
149 sub process
150 3 ▶{
Subroutine process: called CC=4 SCAR=14.0
Pod documented
151 3 my ($self, $dbh, $model) = @_;
152 3 ▶ $model = RDF::Trine::Model->temporary_model unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 3 0 3
153 my $callback = (ref $model eq 'CODE')
154 ? $model
155 3 ▶ : sub { $model->add_statement(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
ref $model eq 'CODE' ? : 0 3 3
156
157 3 my $mappings = $self->mappings;
158 3 TABLE: foreach my $table (keys %$mappings)
159 {
160 5 $self->handle_table($dbh, $callback, $table);
161 }
162  
163 3 return $model;
164 }
165  
166 sub _mktemplate
167 0 ▶{
Subroutine _mktemplate: not called CC=1 SCAR=6.9
168 0 my ($self, $string) = @_;
169 0 $string =~ s! ( [\\{}] ) !\\$1!gx;
170 0 return $string;
171 }
172  
173 sub _get_types
174 5 ▶{
Subroutine _get_types: called CC=3 SCAR=11.9
175 5 my ($self, $sth, $dbh) = @_;
176
177 5 my %types;
178 5 ▶ if (exists $sth->{pg_type})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sth->{'pg_type'}) { } 0 5 5
179 {
180 # For PostgreSQL, this appears to give better results.
181 # Particularly in the case of columns which don't exist
182 # in the table itself. (e.g. aggregation, casting,
183 # functions, etc)
184 0 @types{ @{$sth->{NAME}} } =
185 0 @{ $sth->{pg_type} };
186 }
187 else
188 5 {eval{
189 5 @types{ @{$sth->{NAME}} } =
190 map {
191 /^\d+$/
192 ? scalar($dbh->type_info($_)->{TYPE_NAME})
193 22 ▶ : $_
Branch: 1 branch
BranchTrueFalseTotal
/^\d+$/ ? : 0 22 22
194 }
195 5 @{ $sth->{TYPE} };
196 }}
197
198 5 return \%types;
199 }
200  
201 sub handle_table
202 5 ▶{
Subroutine handle_table: called CC=21 SCAR=43.6
Pod undocumented
203 5 my ($self, $dbh, $model, $table) = @_;
204 5 ▶ $model = RDF::Trine::Model->temporary_model unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 0 5 5
205 0 ▶ my $callback = (ref $model eq 'CODE')?$model:sub{$model->add_statement(@_)};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
ref $model eq 'CODE' ? : 5 0 5
206
207 5 my $mappings = $self->mappings;
208 5 my $tmap = $mappings->{$table};
209
210 # ->{from}
211 5 ▶ my $from = $tmap->{from} || $table;
Condition66%2/3
$tmap->{'from'} || $table
l!l&&r!l&&!r
410
MC/DC0%0/2
$tmap->{'from'} || $table
$tmap->{'from'} $table
Truth table66%2/3
A || B
A= $tmap->{'from'}B= $table
ABresult
1X1
011
000
212  
213 # ->{select}
214 5 ▶ my $select = $tmap->{select} || '*';
Condition50%1/2
$tmap->{'select'} || '*'
l!l
05
MC/DC0%0/1
$tmap->{'select'} || '*'
$tmap->{'select'}
Truth table50%1/2
A
A= $tmap->{'select'}
Aresult
11
00
215
216 # ->{sql}
217 5 my $sql = "SELECT $select FROM $from";
218 5 ▶ $sql = $tmap->{sql} if $tmap->{sql};# =~ /^\s*SELECT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $tmap->{"sql"} 1 4 5
219
220 ### Re-jig mapping structure.
221 {
222 5 $tmap->{-maps} = [];
223 5 $tmap->{-jmaps} = [];
224
225 5 my %c = %{$tmap->{columns}};
226 5 ▶ while (my ($col, $list) = each %c)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %c) 7 5 12
227 {
228 7 push @{ $tmap->{-maps} },
229 7 map { {%$_, column => $col}; }
230 7 grep { !defined $_->{join} }
231 @$list;
232 7 push @{ $tmap->{-jmaps} },
233 7 grep { defined $_->{join} }
234 @$list;
235 }
236 }
237
238 5 ▶ my $sth = $dbh->prepare($sql) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare($sql) or return 5 0 5
239 5 $sth->execute;
240 5 my $types = $self->_get_types($sth, $dbh);
241
242 5 my $row_count = 0;
243 5 ▶ ROW: while (my $row = $sth->fetchrow_hashref)
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 15 5 20
244 {
245 15 $row_count++;
246 15 $self->handle_row($dbh, $callback, $table, $row, $types, $row_count);
247 }
248
249 5 JMAP: foreach my $map (@{ $tmap->{-jmaps} })
250 {
251 0 my $method = lc $map->{method};
252 0 ▶ $method ||= 'subquery' if $map->{join} =~ /^\s*SELECT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $map->{"join"} =~ /^\s*SELECT/i 0 0 0
Condition0%0/2
$method ||= "subquery"
l!l
00
MC/DC0%0/1
$method ||= "subquery"
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
253 0 ▶ $method ||= 'table' if $map->{join} =~ /^[A-Za-z0-9_]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $map->{"join"} =~ /^[A-Za-z0-9_]+$/i 0 0 0
Condition0%0/2
$method ||= "table"
l!l
00
MC/DC0%0/1
$method ||= "table"
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
254 croak sprintf("Cannot join to '' without method.", $map->{join})
255 0 ▶ unless $method eq 'subquery' || $method eq 'table';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "subquery" or $method eq "table" 0 0 0
Condition0%0/3
$method eq "subquery" or $method eq "table"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "subquery" or $method eq "table"
$method eq "subquery" $method eq "table"
Truth table0%0/3
A or B
A= $method eq "subquery"B= $method eq "table"
ABresult
1X1
011
000
256
257 my $evil_sql = sprintf('SELECT * FROM %s AS "r2r_join_parent", %s AS "r2r_join_child"',
258 sprintf(($method eq 'subquery' ? '(%s)' : '"%s"'), $map->{join}),
259 0 ▶ ($tmap->{sql} or $tmap->{select}) ? "($sql)" : $from,
Branch: 2 branches
BranchTrueFalseTotal
$method eq 'subquery' ? : 0 0 0
$tmap->{'sql'} || $tmap->{'select'} ? : 0 0 0
Condition0%0/3
$tmap->{"sql"} or $tmap->{"select"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmap->{"sql"} or $tmap->{"select"}
$tmap->{"sql"} $tmap->{"select"}
Truth table0%0/3
A or B
A= $tmap->{"sql"}B= $tmap->{"select"}
ABresult
1X1
011
000
260 );
261
262 0 ▶ if (@{ $map->{on} })
Branch: 1 branch
BranchTrueFalseTotal
if (@{$map->{"on"};}) 0 0 0
263 {
264 $evil_sql .= ' WHERE ' .
265 join ' AND ',
266 0 map { sprintf('"r2r_join_parent"."%s"="r2r_join_child"."%s"', $_->{parent}, $_->{child}) }
267 0 @{ $map->{on} };
268 }
269
270 0 my $evil_sth = $dbh->prepare($evil_sql);
271 0 $sth->execute;
272 0 my $types = $self->_get_types($sth, $dbh);
273
274 0 my $evil_row_count = 0;
275 0 ▶ ROW: while (my $evil_row = $evil_sth->fetchrow_hashref)
Branch: 1 branch
BranchTrueFalseTotal
if (my $evil_row = $evil_sth->fetchrow_hashref) 0 0 0
276 {
277 0 $evil_row_count++;
278 0 $self->handle_jmap($dbh, $callback, $table, $map, $evil_row, $types, $evil_row_count);
279 }
280 }
281
282 5 delete $tmap->{-maps};
283 5 delete $tmap->{-jmaps};
284 5 return $callback;
285 }
286  
287 sub handle_row
288 15 ▶{
Subroutine handle_row: called CC=8 SCAR=21.3
Pod undocumented
289 15 my ($self, $dbh, $model, $table, $row, $types, $row_count) = @_;
290 15 ▶ $model = RDF::Trine::Model->temporary_model unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 0 15 15
291 0 ▶ my $callback = (ref $model eq 'CODE')?$model:sub{$model->add_statement(@_)};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
ref $model eq 'CODE' ? : 15 0 15
292
293 15 my $mappings = $self->mappings;
294 15 my $tmap = $mappings->{$table};
295
296 # ->{graph}
297 15 my $graph = undef;
298 $graph = $self->iri( $self->template_irisafe($tmap->{graph}, $row, $types) )
299 15 ▶ if defined $tmap->{graph};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmap->{"graph"} 0 15 15
300
301 # ->{about}
302 15 my $subject = $self->_extract_subject_from_row($tmap, $row, $types);
303 15 ▶ return unless defined $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject 3 12 15
304
305 # ->{typeof}
306 12 foreach (@{ $tmap->{typeof} })
307 {
308 12 ▶ $_ = $self->iri($_, $graph) unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 12 12
309 12 $callback->(statement($self->iri($subject, $graph), $rdf->type, $_));
310 }
311  
312 12 foreach (@{ $tmap->{-maps} })
313 {
314 17 $self->handle_map($dbh, $model, $table, $row, $types, $row_count, $_, $graph, $subject);
315 }
316 }
317  
318 sub _extract_subject_from_row
319 15 ▶{
Subroutine _extract_subject_from_row: called CC=9 SCAR=28.3
320 15 my ($self, $tmap, $row, $types) = @_;
321 15 ▶ if ($tmap->{about} and $tmap->{_about_is_template})
Branch: 3 branches
BranchTrueFalseTotal
if ($tmap->{'about'} and $tmap->{'_about_is_template'}) { } 3 12 15
elsif ($tmap->{'about'} and $tmap->{'about'} =~ / ^ {\" ([^}]+?) \"} $ /x) { } 0 12 12
elsif ($tmap->{'about'} and $tmap->{'about'} =~ / ^ { ([^}]+?) } $ /x) { } 12 0 12
Condition66%2/3
$tmap->{"about"} and $tmap->{"_about_is_template"}
!ll&&!rl&&r
0123
MC/DC50%1/2
$tmap->{"about"} and $tmap->{"_about_is_template"}
$tmap->{"about"} $tmap->{"_about_is_template"}
Truth table66%2/3
A and B
A= $tmap->{"about"}B= $tmap->{"_about_is_template"}
ABresult
0X0
100
111
322 {
323 3 return $self->template_irisafe($tmap->{about}, $row, $types);
324 }
325 ▶ elsif ($tmap->{about} and $tmap->{about} =~ m< ^ {\" ([^}]+?) \"} $ >x)
Condition33%1/3
$tmap->{"about"} and $tmap->{"about"} =~ / ^ {\" ([^}]+?) \"} $ /x
!ll&&!rl&&r
0120
MC/DC0%0/2
$tmap->{"about"} and $tmap->{"about"} =~ / ^ {\" ([^}]+?) \"} $ /x
$tmap->{"about"} $tmap->{"about"} =~ / ^ {\" ([^}]+?) \"} $ /x
Truth table33%1/3
A and B
A= $tmap->{"about"}B= $tmap->{"about"} =~ / ^ {\" ([^}]+?) \"} $ /x
ABresult
0X0
100
111
326 {
327 0 return $row->{$1};
328 }
329 ▶ elsif ($tmap->{about} and $tmap->{about} =~ m< ^ { ([^}]+?) } $ >x)
Condition33%1/3
$tmap->{"about"} and $tmap->{"about"} =~ / ^ { ([^}]+?) } $ /x
!ll&&!rl&&r
0012
MC/DC0%0/2
$tmap->{"about"} and $tmap->{"about"} =~ / ^ { ([^}]+?) } $ /x
$tmap->{"about"} $tmap->{"about"} =~ / ^ { ([^}]+?) } $ /x
Truth table33%1/3
A and B
A= $tmap->{"about"}B= $tmap->{"about"} =~ / ^ { ([^}]+?) } $ /x
ABresult
0X0
100
111
330 {
331 12 ▶ return $row->{$1} if exists $row->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if exists $row->{$1} 0 12 12
332 12 return $row->{lc $1};
333 }
334 0 ▶ return ($tmap->{about} // '[]');
Condition0%0/2
$tmap->{"about"} // "[]"
l!l
00
MC/DC0%0/1
$tmap->{"about"} // "[]"
$tmap->{"about"}
Truth table0%0/2
A
A= $tmap->{"about"}
Aresult
11
00
335 }
336  
337 sub handle_jmap
338 0 ▶{
Subroutine handle_jmap: not called CC=4 SCAR=30.0
Pod undocumented
339 0 my ($self, $dbh, $model, $table, $jmap, $row, $types, $row_count) = @_;
340 0 ▶ $model = RDF::Trine::Model->temporary_model unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 0 0 0
341 0 ▶ my $callback = (ref $model eq 'CODE')?$model:sub{$model->add_statement(@_)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $model eq 'CODE' ? : 0 0 0
342
343 0 my $mappings = $self->mappings;
344 0 my $tmap = $mappings->{$table};
345
346 # ->{graph}
347 0 my $graph = undef;
348 $graph = $self->iri( $self->template_irisafe($tmap->{graph}, $row, $types) )
349 0 ▶ if defined $tmap->{graph};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmap->{"graph"} 0 0 0
350
351 # ->{about}
352 0 my $subject = $self->_extract_subject_from_row($tmap, $row, $types);
353
354 0 $self->handle_map($dbh, $model, $table, $row, $types, $row_count, $jmap, $graph, $subject);
355 }
356  
357 sub handle_map
358 17 ▶{
Subroutine handle_map: called CC=36 SCAR=46.4
Pod undocumented
359 17 my ($self, $dbh, $model, $table, $row, $types, $row_count, $map, $graph, $subject) = @_;
360 17 state $parsers = {};
361
362 17 ▶ $model = RDF::Trine::Model->temporary_model unless defined $model;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $model 0 17 17
363 0 ▶ my $callback = (ref $model eq 'CODE')?$model:sub{$model->add_statement(@_)};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
ref $model eq 'CODE' ? : 17 0 17
364
365 17 my $mappings = $self->mappings;
366 17 my $tmap = $mappings->{$table};
367 17 my %row = %$row;
368 17 my $column = $map->{column};
369
370 17 my ($predicate, $value, $lang, $loose);
371 17 ▶ if ($column =~ /^"(.+)"$/)
Branch: 3 branches
BranchTrueFalseTotal
if ($column =~ /^"(.+)"$/) { } 0 17 17
elsif (exists $row{$column}) { } 12 5 17
elsif (exists $row{lc $column}) { } 5 0 5
372 0 { $column = $1; $value = $row{$column} }
373 elsif (exists $row{$column})
374 12 { $loose = 1; $value = $row{$column} }
375 elsif (exists $row{lc $column})
376 5 { $loose = 1; $value = $row{lc $column} }
377
378 17 ▶ if (my $lang_col = $map->{lang_col})
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang_col = $map->{"lang_col"}) 9 8 17
379 {
380 9 ▶ if ($lang_col =~ /^"(.+)"$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($lang_col =~ /^"(.+)"$/) { } 0 9 9
elsif (defined $lang_col) { } 9 0 9
381 0 { $lang = $row{$1} }
382 elsif (defined $lang_col)
383 9 ▶ { $lang = $row{$lang_col} // $row{lc $lang_col} }
Condition66%2/3
$row{$lang_col} // $row{lc $lang_col}
l!l&&r!l&&!r
405
MC/DC50%1/2
$row{$lang_col} // $row{lc $lang_col}
$row{$lang_col} $row{lc $lang_col}
Truth table66%2/3
A // B
A= $row{$lang_col}B= $row{lc $lang_col}
ABresult
1X1
011
000
384 }
385 17 ▶ $lang //= $map->{lang};
Condition100%3/3
$lang //= $map->{"lang"}
l!l&&r!l&&!r
449
MC/DC100%2/2
$lang //= $map->{"lang"}
$lang $map->{"lang"}
Truth table100%3/3
A //= B
A= $langB= $map->{"lang"}
ABresult
1X1
011
000
386
387 my $lgraph = defined $map->{graph}
388 17 ▶ ? $self->iri($self->template_irisafe($map->{graph}, $row, $types))
Branch: 1 branch
BranchTrueFalseTotal
defined $map->{'graph'} ? : 0 17 17
389 : $graph;
390
391 17 ▶ if (defined $map->{parse} and uc $map->{parse} eq 'TURTLE')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $map->{"parse"} and uc $map->{"parse"} eq "TURTLE") 0 17 17
Condition33%1/3
defined $map->{"parse"} and uc $map->{"parse"} eq "TURTLE"
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $map->{"parse"} and uc $map->{"parse"} eq "TURTLE"
defined $map->{"parse"} uc $map->{"parse"} eq "TURTLE"
Truth table33%1/3
A and B
A= defined $map->{"parse"}B= uc $map->{"parse"} eq "TURTLE"
ABresult
0X0
100
111
392 {
393 0 ▶ return unless length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 0 0
394
395 0 my %NS = $self->namespaces;
396 my $turtle =
397 join '',
398 0 (map { sprintf("\@prefix %s: <%s>.\n", $_, $NS{$_}) } keys %NS),
399 sprintf("\@base <%s>.\n", $subject->uri),
400 $value,
401 "\n";
402 0 return eval {
403 0 $parsers->{ $map->{parse} } = RDF::Trine::Parser->new($map->{parse});
404 $parsers->{ $map->{parse} }->parse(
405 $subject,
406 $turtle,
407 0 ▶ ($lgraph ? sub { $callback->($_[0], $lgraph) } : $callback),
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$lgraph ? : 0 0 0
408 );
409 };
410 }
411  
412 17 ▶ if ($map->{rev} || $map->{rel})
Branch: 2 branches
BranchTrueFalseTotal
if ($map->{'rev'} or $map->{'rel'}) { } 5 12 17
elsif ($map->{'property'}) { } 12 0 12
Condition66%2/3
$map->{"rev"} or $map->{"rel"}
l!l&&r!l&&!r
0512
MC/DC50%1/2
$map->{"rev"} or $map->{"rel"}
$map->{"rev"} $map->{"rel"}
Truth table66%2/3
A or B
A= $map->{"rev"}B= $map->{"rel"}
ABresult
1X1
011
000
413 {
414 5 ▶ $predicate = $map->{rev} || $map->{rel};
Condition33%1/3
$map->{'rev'} || $map->{'rel'}
l!l&&r!l&&!r
050
MC/DC0%0/2
$map->{'rev'} || $map->{'rel'}
$map->{'rev'} $map->{'rel'}
Truth table33%1/3
A || B
A= $map->{'rev'}B= $map->{'rel'}
ABresult
1X1
011
000
415
416 5 ▶ if ($map->{resource})
Branch: 1 branch
BranchTrueFalseTotal
if ($map->{"resource"}) 0 5 5
417 {
418 0 $value = $self->template_irisafe($map->{resource}, +{ %row, '_' => $value }, $types);
419 }
420 5 $value = $self->iri($value, $lgraph);
421 }
422
423 elsif ($map->{property})
424 {
425 12 $predicate = $map->{property};
426
427 12 ▶ if ($map->{content})
Branch: 1 branch
BranchTrueFalseTotal
if ($map->{"content"}) 0 12 12
428 {
429 0 $value = $self->template($map->{content}, +{ %row, '_' => $value }, $types);
430 }
431
432 12 ▶ if ($lang)
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) { } 8 4 12
433 {
434 8 $value = literal($value, $lang);
435 }
436 else
437 {
438 4 ▶ if ($map->{datatype})
Branch: 2 branches
BranchTrueFalseTotal
if ($map->{'datatype'}) { } 0 4 4
elsif (not defined $map->{'content'}) { } 4 0 4
439 {
440 0 $value = literal($value, undef, $map->{datatype});
441 }
442 elsif (!defined $map->{content})
443 {
444 4 my $type = $types->{$column};
445 4 ▶ $type = $types->{lc $column} if $loose && not exists $types->{$column};
Branch: 1 branch
BranchTrueFalseTotal
if $loose and not exists $types->{$column} 0 4 4
Condition33%1/3
$loose and not exists $types->{$column}
!ll&&!rl&&r
040
MC/DC0%0/2
$loose and not exists $types->{$column}
$loose not exists $types->{$column}
Truth table33%1/3
A and not(B)
A= $looseB= not exists $types->{$column}
ABresult
0X0
100
111
446 4 $value = $self->datatyped_literal($value, $type);
447 }
448 else
449 {
450 0 $value = literal($value);
451 }
452 }
453 }
454
455 17 ▶ if (defined $predicate and defined $value)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $predicate and defined $value) 16 1 17
Condition66%2/3
defined $predicate and defined $value
!ll&&!rl&&r
0116
MC/DC50%1/2
defined $predicate and defined $value
defined $predicate defined $value
Truth table66%2/3
A and B
A= defined $predicateB= defined $value
ABresult
0X0
100
111
456 {
457 16 ▶ if (blessed($predicate) && $predicate->isa('RDF::RDB2RDF::R2RML::_COL_')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($predicate) and $predicate->isa("RDF::RDB2RDF::R2RML::_COL_") or not ref $predicate) 16 0 16
Condition66%4/6
builtin::blessed($predicate) and $predicate->isa("RDF::RDB2RDF::R2RML::_COL_") or not ref $predicate
l!l&&r!l&&!r
3130
builtin::blessed($predicate) and $predicate->isa("RDF::RDB2RDF::R2RML::_COL_")
!ll&&!rl&&r
1303
MC/DC0%0/3
builtin::blessed($predicate) and $predicate->isa("RDF::RDB2RDF::R2RML::_COL_") or not ref $predicate
builtin::blessed($predicate) $predicate->isa("RDF::RDB2RDF::R2RML::_COL_") not ref $predicate
Truth table40%2/5
A and B or not(C)
A= builtin::blessed($predicate)B= $predicate->isa("RDF::RDB2RDF::R2RML::_COL_")C= not ref $predicate
ABCresult
11X1
0X11
1011
0X00
1000
458 or not ref $predicate)
459 {
460 16 $predicate = $self->template_irisafe($predicate, +{ %row, '_' => $value }, $types);
461 16 $predicate = $self->iri($predicate, $lgraph) ;
462 }
463
464 16 my $lsubject = $self->iri($subject, $lgraph);
465 16 ▶ if ($map->{about})
Branch: 1 branch
BranchTrueFalseTotal
if ($map->{"about"}) 0 16 16
466 {
467 0 $lsubject = $self->iri($self->template_irisafe($map->{about}, $row, $types), $lgraph);
468 }
469 16 ▶ return unless defined $lsubject;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lsubject 0 16 16
470  
471 my $st = $map->{rev}
472 16 ▶ ? statement($value, $predicate, $lsubject)
Branch: 1 branch
BranchTrueFalseTotal
$map->{'rev'} ? : 0 16 16
473 : statement($lsubject, $predicate, $value);
474
475 16 ▶ if ($lgraph)
Branch: 1 branch
BranchTrueFalseTotal
if ($lgraph) { } 0 16 16
476 {
477 0 $callback->($st, $lgraph);
478 }
479 else
480 {
481 16 $callback->($st);
482 }
483 }
484 }
485  
486 sub process_turtle
487 0 ▶{
Subroutine process_turtle: not called CC=2 SCAR=17.9
Pod documented
488 0 my ($self, $dbh, %options) = @_;
489  
490 0 my $rv;
491 0 ▶ unless ($options{no_json})
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_json"}) 0 0 0
492 {
493 0 my $json = $self->to_json(canonical=>1, pretty=>1);
494 0 $json =~ s/^/# /gm;
495 0 $json = "# MAPPING\n#\n${json}\n";
496 0 $rv .= $json;
497 }
498  
499 0 $rv .= $self->SUPER::process_turtle($dbh, namespaces => {$self->namespaces});
500 0 $rv;
501 }
502  
503 sub to_hashref
504 0 ▶{
Subroutine to_hashref: not called CC=1 SCAR=6.9
Pod documented
505 0 my ($self) = @_;
506
507 return {
508 -namespaces => $self->_export( {$self->namespaces} ),
509 0 %{ $self->_export( $self->mappings ) },
510 };
511 }
512  
513 5 *TO_JSON = \&to_hashref;
514  
515 sub to_json
516 0 ▶{
Subroutine to_json: not called CC=3 SCAR=24.8
Pod documented
517 0 ▶ my ($self, %opts) = (exists $_[1] and ref $_[1] eq 'HASH') ? ($_[0], %{ $_[1] }) : @_;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[1] && ref $_[1] eq 'HASH' ? : 0 0 0
Condition0%0/3
exists $_[1] and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $_[1] and ref $_[1] eq "HASH"
exists $_[1] ref $_[1] eq "HASH"
Truth table0%0/3
A and B
A= exists $_[1]B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
518 0 $opts{convert_blessed} = 1;
519 0 JSON::to_json($self, {%opts});
520 }
521  
522 sub _export
523 0 ▶{
Subroutine _export: not called CC=20 SCAR=60.4
524 0 my ($self, $thingy) = @_;
525
526 0 ▶ return undef unless defined $thingy;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thingy 0 0 0
527
528 0 ▶ if (ref $thingy eq 'HASH' or (blessed($thingy) and $thingy->isa('RDF::Trine::NamespaceMap')))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thingy eq "HASH" or builtin::blessed($thingy) and $thingy->isa("RDF::Trine::NamespaceMap")) 0 0 0
Condition0%0/6
ref $thingy eq "HASH" or builtin::blessed($thingy) and $thingy->isa("RDF::Trine::NamespaceMap")
l!l&&r!l&&!r
000
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::NamespaceMap")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $thingy eq "HASH" or builtin::blessed($thingy) and $thingy->isa("RDF::Trine::NamespaceMap")
ref $thingy eq "HASH" builtin::blessed($thingy) $thingy->isa("RDF::Trine::NamespaceMap")
Truth table0%0/4
A or B and C
A= ref $thingy eq "HASH"B= builtin::blessed($thingy)C= $thingy->isa("RDF::Trine::NamespaceMap")
ABCresult
1XX1
0111
00X0
0100
529 {
530 0 my $hash = {};
531 0 ▶ while (my ($k, $v) = each %$thingy)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$thingy) 0 0 0
532 {
533 0 ▶ $hash->{$k} = ref $v ? $self->_export($v) : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
534 }
535 0 return $hash;
536 }
537  
538 0 ▶ if (ref $thingy eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thingy eq "ARRAY") 0 0 0
539 {
540 0 ▶ return [ map { ref $_ ? $self->_export($_) : $_ } @$thingy ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
541 }
542
543 0 ▶ if (blessed($thingy) and $thingy->isa('RDF::RDB2RDF::R2RML::_COL_'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thingy) and $thingy->isa("RDF::RDB2RDF::R2RML::_COL_")) 0 0 0
Condition0%0/3
builtin::blessed($thingy) and $thingy->isa("RDF::RDB2RDF::R2RML::_COL_")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($thingy) and $thingy->isa("RDF::RDB2RDF::R2RML::_COL_")
builtin::blessed($thingy) $thingy->isa("RDF::RDB2RDF::R2RML::_COL_")
Truth table0%0/3
A and B
A= builtin::blessed($thingy)B= $thingy->isa("RDF::RDB2RDF::R2RML::_COL_")
ABresult
0X0
100
111
544 {
545 0 return "$thingy";
546 }
547  
548 0 ▶ if (blessed($thingy) and $thingy->isa('RDF::Trine::Node::Resource'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::Resource")) 0 0 0
Condition0%0/3
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::Resource")
builtin::blessed($thingy) $thingy->isa("RDF::Trine::Node::Resource")
Truth table0%0/3
A and B
A= builtin::blessed($thingy)B= $thingy->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
549 {
550 0 return $self->_mktemplate($thingy->uri);
551 }
552  
553 0 ▶ if (blessed($thingy) and $thingy->isa('RDF::Trine::Node::BlankNode'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::BlankNode")) 0 0 0
Condition0%0/3
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::BlankNode")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::BlankNode")
builtin::blessed($thingy) $thingy->isa("RDF::Trine::Node::BlankNode")
Truth table0%0/3
A and B
A= builtin::blessed($thingy)B= $thingy->isa("RDF::Trine::Node::BlankNode")
ABresult
0X0
100
111
554 {
555 0 return '_:'.$self->_mktemplate($thingy->identifier);
556 }
557  
558 0 ▶ if (blessed($thingy) and $thingy->isa('RDF::Trine::Node::Literal'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::Literal")) 0 0 0
Condition0%0/3
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Node::Literal")
builtin::blessed($thingy) $thingy->isa("RDF::Trine::Node::Literal")
Truth table0%0/3
A and B
A= builtin::blessed($thingy)B= $thingy->isa("RDF::Trine::Node::Literal")
ABresult
0X0
100
111
559 {
560 0 warn "This shouldn't happen!";
561 0 return $self->_mktemplate($thingy->literal_value);
562 }
563
564 0 ▶ if (blessed($thingy) and $thingy->isa('RDF::Trine::Namespace'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Namespace")) 0 0 0
Condition0%0/3
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Namespace")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($thingy) and $thingy->isa("RDF::Trine::Namespace")
builtin::blessed($thingy) $thingy->isa("RDF::Trine::Namespace")
Truth table0%0/3
A and B
A= builtin::blessed($thingy)B= $thingy->isa("RDF::Trine::Namespace")
ABresult
0X0
100
111
565 {
566 0 return $self->_mktemplate($thingy->uri->uri);
567 }
568
569 0 ▶ warn "This shouldn't happen either!" if ref $thingy;
Branch: 1 branch
BranchTrueFalseTotal
if ref $thingy 0 0 0
570 0 return "$thingy";
571 }
572  
573 5 1;
574  
575 __END__
« RDF/RDB2RDF/DatatypeMapper.pm ↑ Summary RDF/RDB2RDF/R2RML.pm »