Command/Query.pm

filter: Statementstmt 29.0% 32 / 110 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 64.7% 11 / 17 Podpod 100% 2 / 2 totaltotal 22.9% 45 / 196 CC 34 SCAR 50.6
CC
34
Cov
19%
_process_sparql
380.0
_model
56.0
validate_args
30.0
CRAP 157.1
↑ Summary FileSpec/OutputBindings.pm »
1 package App::perlrdf::Command::Query;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $App::perlrdf::Command::Query::AUTHORITY = 'cpan:TOBYINK';
10 1 $App::perlrdf::Command::Query::VERSION = '0.004';
11 }
12  
13 1 ▶use App::perlrdf -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use constant abstract => q (query stores, files or remote endpoints with SPARQL);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant command_names => qw( query sparql q );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant description => <<'DESCRIPTION';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 Use SPARQL to query:
20  
21 * an RDF::Trine::Store;
22 * a remote SPARQL Protocol (1.0/1.1) endpoint; or
23 * one or more input files;
24  
25 But not a combination of the above.
26 DESCRIPTION
27  
28 1 ▶use constant opt_spec => (
Subroutine BEGIN: called CC=1 SCAR=0.0
29 __PACKAGE__->store_opt_spec,
30 []=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>,
31 [ 'input|i=s@', 'Input filename or URL' ],
32 [ 'input-spec|I=s@', 'Input file specification' ],
33 [ 'input-format|p=s', 'Input format (mnemonic: parse)' ],
34 [ 'input-base|b=s', 'Input base URI' ],
35 [ 'graph|g=s', 'Graph URI for input' ],
36 [ 'autograph|G', 'Generate graph URI based on input URI' ],
37 []=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>,
38 [ 'endpoint=s', 'Remote SPARQL Protocol endpoint' ],
39 [ 'query_method=s', 'Query method (GET/POST/etc)' ],
40 []=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>,
41 [ 'execute|e=s', 'Query to execute' ],
42 [ 'sparql-file|f=s', 'File containing query to execute' ],
43 []=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>
44 [ 'output|o=s@', 'Output filename or URL' ],
45 [ 'output-spec|O=s@', 'Output file specification' ],
46 [ 'output-format|s=s', 'Output format (mnemonic: serialise)' ],
47 1 );
48  
49 sub validate_args
50 0 ▶{
Subroutine validate_args: not called CC=5 SCAR=34.0
Pod documented
51 0 my ($self, $opt, $arg) = @_;
52
53 my %exclusions = (
54 execute => ['sparql_file'],
55 endpoint => [
56 qw[ store dbi sqlite username password host port dbname database ],
57 qw[ input input_spec input_format input_base ],
58 ],
59 query_method => [
60 qw[ store dbi sqlite username password host port dbname database ],
61 qw[ input input_spec input_format input_base ],
62 ],
63 0 map { ; $_ => [
64 qw[ store dbi sqlite username password host port dbname database ],
65 qw[ endpoint ],
66 ] } qw( input input_spec input_format input_base )
67 );
68
69 0 foreach my $k (keys %exclusions)
70 {
71 0 ▶ next unless exists $opt->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt->{$k} 0 0 0
72 0 foreach my $e (@{ $exclusions{$k} })
73 {
74 0 ▶ next unless exists $opt->{$e};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt->{$e} 0 0 0
75 0 $self->usage_error("Must not provide both '$k' and '$e' options.");
76 }
77 }
78 }
79  
80 sub _sparql
81 0 ▶{
Subroutine _sparql: not called CC=3 SCAR=24.8
82 0 require App::perlrdf::FileSpec::InputFile;
83 0 my ($self, $opt, $arg) = @_;
84
85 # SPARQL provided on command line
86 #
87 return $opt->{execute}
88 0 ▶ if $opt->{execute};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"execute"} 0 0 0
89
90 # SPARQL from input file
91 #
92 App::perlrdf::FileSpec::InputFile::
93 -> new_from_filespec(
94 0 ▶ ($opt->{sparql_file} // shift @$arg),
Condition0%0/3
$opt->{"sparql_file"} // shift @$arg
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"sparql_file"} // shift @$arg
$opt->{"sparql_file"} shift @$arg
Truth table0%0/3
A // B
A= $opt->{"sparql_file"}B= shift @$arg
ABresult
1X1
011
000
95 'SPARQL',
96 )
97 -> content;
98 }
99  
100 sub _model
101 0 ▶{
Subroutine _model: not called CC=7 SCAR=40.3
102 0 require App::perlrdf::FileSpec::InputRDF;
103 0 my ($self, $opt, $arg) = @_;
104
105 0 ▶ if (grep { exists $opt->{$_} } qw[ store dbi sqlite dbname database ])
Branch: 1 branch
BranchTrueFalseTotal
if (grep {exists $opt->{$_};} "store", "dbi", "sqlite", "dbname", "database") 0 0 0
106 {
107 0 return RDF::Trine::Model->new( $self->get_store($opt) );
108 }
109  
110 0 my $model = RDF::Trine::Model->new;
111
112 0 my @inputs = $self->get_filespecs(
113 'App::perlrdf::FileSpec::InputRDF',
114 input => $opt,
115 );
116
117 push @inputs, map {
118 0 App::perlrdf::FileSpec::InputRDF::->new_from_filespec(
119 $_,
120 $opt->{input_format},
121 $opt->{input_base},
122 )
123 0 } @$arg;
124  
125 push @inputs,
126 App::perlrdf::FileSpec::InputRDF::->new_from_filespec(
127 '-',
128 $opt->{input_format},
129 $opt->{input_base},
130 )
131 0 ▶ unless @inputs;
Branch: 1 branch
BranchTrueFalseTotal
unless @inputs 0 0 0
132  
133 0 for (@inputs)
134 {
135 0 printf STDERR "Loading %s\n", $_->uri;
136
137 0 my %params = ();
138 0 ▶ if ($opt->{autograph})
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'autograph'}) { } 0 0 0
elsif ($opt->{'graph'}) { } 0 0 0
139 0 { $params{graph} = $_->uri }
140 elsif ($opt->{graph})
141 0 { $params{graph} = $opt->{graph} }
142
143 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $@ = undef; $_->parse_into_model($model, %params); 1 } } or warn "$@\n" 0 0 0
144 0 local $@ = undef;
145 0 $_->parse_into_model($model, %params);
146 0 1;
147 } or warn "$@\n";
148 }
149  
150 0 return $model;
151 }
152  
153 sub _outputs
154 0 ▶{
Subroutine _outputs: not called CC=3 SCAR=24.8
155 0 require App::perlrdf::FileSpec::OutputRDF;
156 0 require App::perlrdf::FileSpec::OutputBindings;
157
158 0 my ($self, $opt, $arg, $class) = @_;
159
160 0 my @outputs = $self->get_filespecs(
161 $class,
162 output => $opt,
163 );
164 push @outputs,
165 $class->new_from_filespec(
166 '-',
167 0 ▶ $opt->{output_format}||'text',
Branch: 1 branch
BranchTrueFalseTotal
unless @outputs 0 0 0
Condition0%0/2
$opt->{'output_format'} || 'text'
l!l
00
MC/DC0%0/1
$opt->{'output_format'} || 'text'
$opt->{'output_format'}
Truth table0%0/2
A
A= $opt->{'output_format'}
Aresult
11
00
168 undef,
169 )
170 unless @outputs;
171
172 0 return @outputs;
173 }
174  
175 sub _process_sparql
176 0 ▶{
Subroutine _process_sparql: not called CC=19 SCAR=59.4
177 0 require RDF::Query;
178 0 require RDF::Query::Client;
179 0 my ($self, $opt, $arg, $sparql, $model) = @_;
180
181 0 ▶ my $qclass = ref $model ? 'RDF::Query' : 'RDF::Query::Client';
Branch: 1 branch
BranchTrueFalseTotal
ref $model ? : 0 0 0
182 my @params = ref $model ? () : ({
183 0 ▶ QueryMethod => ($opt->{query_method} // $ENV{PERLRDF_QUERY_METHOD} // "POST"),
Branch: 1 branch
BranchTrueFalseTotal
ref $model ? : 0 0 0
Condition0%0/5
($opt->{"query_method"} // $ENV{"PERLRDF_QUERY_METHOD"}) // "POST"
l!l
00
$opt->{"query_method"} // $ENV{"PERLRDF_QUERY_METHOD"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($opt->{"query_method"} // $ENV{"PERLRDF_QUERY_METHOD"}) // "POST"
$opt->{"query_method"} $ENV{"PERLRDF_QUERY_METHOD"}
Truth table0%0/3
A // B
A= $opt->{"query_method"}B= $ENV{"PERLRDF_QUERY_METHOD"}
ABresult
1X1
011
000
184 });
185 0 ▶ my $query = $qclass->new($sparql) or die RDF::Query->error;
Branch: 1 branch
BranchTrueFalseTotal
my $query = $qclass->new($sparql) or die "RDF::Query"->error 0 0 0
186 0 ▶ if ($query->can('useragent')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->can("useragent")) 0 0 0
187 0 $query->useragent->max_redirect(5);
188 0 $query->useragent->agent(
189 sprintf(
190 '%s/%s (%s) %s',
191 ref($self),
192 $self->VERSION,
193 $self->AUTHORITY,
194 $query->useragent->agent,
195 ),
196 );
197 }
198 0 ▶ my $result = $query->execute($model, @params) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $result = $query->execute($model, @params) or do { if ($ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")) { warn $query->http_response->request->as_string; foreach my $redir ($query->http_response->redirects) { warn $redir->status_line; } ; warn $query->http_response->as_string; } ; die $query->error } 0 0 0
199 0 ▶ if (($ENV{PERLRDF_QUERY_DEBUG}//'') and $query->can('http_response')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")) 0 0 0
Condition0%0/5
$ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")
!ll&&!rl&&r
000
$ENV{"PERLRDF_QUERY_DEBUG"} // ""
l!l
00
MC/DC0%0/2
$ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")
$ENV{"PERLRDF_QUERY_DEBUG"} $query->can("http_response")
Truth table0%0/3
A and B
A= $ENV{"PERLRDF_QUERY_DEBUG"}B= $query->can("http_response")
ABresult
0X0
100
111
200 0 warn $query->http_response->request->as_string;
201 0 for my $redir ($query->http_response->redirects) {
202 0 warn $redir->status_line;
203 }
204 0 warn $query->http_response->as_string;
205 }
206 0 die $query->error;
207 };
208
209 0 ▶ if ($result->is_graph)
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_graph) 0 0 0
210 {
211 0 my $m = RDF::Trine::Model->new;
212 0 $m->add_iterator($m);
213
214 0 my (@outputs) = $self->_outputs(
215 $opt,
216 $arg,
217 'App::perlrdf::FileSpec::OutputRDF',
218 );
219
220 0 foreach my $out (@outputs)
221 {
222 0 $out->serialize_model($m);
223 0 $out->handle->close;
224 }
225 }
226
227 0 ▶ if ($result->is_bindings)
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_bindings) 0 0 0
228 {
229 0 ▶ if (($ENV{PERLRDF_QUERY_DEBUG}//'') and $query->can('http_response')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")) 0 0 0
Condition0%0/5
$ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")
!ll&&!rl&&r
000
$ENV{"PERLRDF_QUERY_DEBUG"} // ""
l!l
00
MC/DC0%0/2
$ENV{"PERLRDF_QUERY_DEBUG"} // "" and $query->can("http_response")
$ENV{"PERLRDF_QUERY_DEBUG"} $query->can("http_response")
Truth table0%0/3
A and B
A= $ENV{"PERLRDF_QUERY_DEBUG"}B= $query->can("http_response")
ABresult
0X0
100
111
230 0 warn $query->http_response->as_string;
231 }
232
233 0 my $mat = $result->materialize;
234
235 0 my (@outputs) = $self->_outputs(
236 $opt,
237 $arg,
238 'App::perlrdf::FileSpec::OutputBindings',
239 );
240
241 0 foreach my $out (@outputs)
242 {
243 0 $out->serialize_iterator($mat);
244 0 $mat->reset;
245 }
246 }
247 }
248  
249 sub execute
250 0 ▶{
Subroutine execute: not called CC=2 SCAR=17.9
Pod documented
251 0 require RDF::Trine;
252 0 my ($self, $opt, $arg) = @_;
253
254 0 my $sparql = $self->_sparql($opt, $arg);
255  
256 0 ▶ if (exists $opt->{endpoint})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{"endpoint"}) 0 0 0
257 {
258 0 return $self->_process_sparql($opt, $arg, $sparql, $opt->{endpoint});
259 }
260  
261 0 my $model = $self->_model($opt, $arg);
262 0 $self->_process_sparql($opt, $arg, $sparql, $model);
263 }
264  
265 1 1;
266  
267  
268 __END__
↑ Summary FileSpec/OutputBindings.pm »