| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
no warnings 'redefine'; |
| 5 |
|
| 6 |
use Cwd; |
| 7 |
use Benchmark; |
| 8 |
use Scalar::Util qw(blessed); |
| 9 |
use Data::Dumper; |
| 10 |
use RDF::Query; |
| 11 |
use RDF::Query::Error qw(:try); |
| 12 |
use RDF::Query::Util; |
| 13 |
use Term::ReadLine; |
| 14 |
use Term::ReadKey; |
| 15 |
use LWP::Simple; |
| 16 |
use LWP::MediaTypes qw(add_type); |
| 17 |
|
| 18 |
################################################################################ |
| 19 |
# Log::Log4perl::init( \q[ |
| 20 |
# log4perl.category.rdf.query.plan = DEBUG, Screen |
| 21 |
# log4perl.appender.Screen = Log::Log4perl::Appender::Screen |
| 22 |
# log4perl.appender.Screen.stderr = 0 |
| 23 |
# log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout |
| 24 |
# ] ); |
| 25 |
################################################################################ |
| 26 |
|
| 27 |
add_type( 'application/rdf+xml' => qw(rdf xrdf rdfx) ); |
| 28 |
add_type( 'text/turtle' => qw(ttl) ); |
| 29 |
add_type( 'text/plain' => qw(nt) ); |
| 30 |
add_type( 'text/x-nquads' => qw(nq) ); |
| 31 |
add_type( 'text/json' => qw(json) ); |
| 32 |
add_type( 'text/html' => qw(html xhtml htm) ); |
| 33 |
|
| 34 |
$| = 1; |
| 35 |
my $model; |
| 36 |
if (scalar(@ARGV) and $ARGV[ $#ARGV ] =~ /.sqlite/) { |
| 37 |
my $file = pop(@ARGV); |
| 38 |
my $dsn = "DBI:SQLite:dbname=" . $file; |
| 39 |
my $store = RDF::Trine::Store::DBI->new($model, $dsn, '', ''); |
| 40 |
$model = RDF::Trine::Model->new( $store ); |
| 41 |
} else { |
| 42 |
$model = memory_model(); |
| 43 |
} |
| 44 |
my %args = &RDF::Query::Util::cli_parse_args(); |
| 45 |
unless (exists $args{update}) { |
| 46 |
$args{update} = 1; |
| 47 |
} |
| 48 |
$args{ base } = 'file://' . getcwd . '/'; |
| 49 |
|
| 50 |
my $NAMESPACES = $RDF::Query::Util::PREFIXES; |
| 51 |
my $vb_format = 'table'; |
| 52 |
my $def_format = 'ntriples'; |
| 53 |
my $serializer = RDF::Trine::Serializer->new($def_format); |
| 54 |
my %serializer = ($def_format => $serializer); |
| 55 |
my $class = delete $args{ class } || 'RDF::Query'; |
| 56 |
my $term = Term::ReadLine->new('rqsh', \*STDIN, \*STDOUT); |
| 57 |
|
| 58 |
print "rqsh v1.0, RDF::Query v${RDF::Query::VERSION}\n\n"; |
| 59 |
while ( defined ($_ = $term->readline('rqsh> ')) ) { |
| 60 |
my $line = $_; |
| 61 |
next unless (length($line)); |
| 62 |
handle_cmd( $line ); |
| 63 |
} |
| 64 |
|
| 65 |
sub handle_cmd { |
| 66 |
my $line = shift; |
| 67 |
if ($line =~ /help/i) { |
| 68 |
help(); |
| 69 |
} elsif ($line =~ /^set (prefix (\w+): <([^>]+)>)/i) { |
| 70 |
my $query = RDF::Query->new( "$1 SELECT * WHERE {}" ); |
| 71 |
if ($query) { |
| 72 |
$NAMESPACES .= "PREFIX $2: <$3>\n"; |
| 73 |
} else { |
| 74 |
warn RDF::Query->error; |
| 75 |
} |
| 76 |
} elsif ($line =~ /^time (.*)/i) { |
| 77 |
timethis( 1, sub { |
| 78 |
handle_cmd( $1 ); |
| 79 |
} ); |
| 80 |
} elsif ($line =~ /^explain (.*)$/i) { |
| 81 |
explain($model, $term, $1); |
| 82 |
} elsif ($line =~ /^parse (.*)$/i) { |
| 83 |
parse($model, $term, $1); |
| 84 |
} elsif ($line =~ /^use (\w+)\s*;?\s*$/i) { |
| 85 |
my $name = $1; |
| 86 |
my $nmodel = model( $name ); |
| 87 |
if ($nmodel) { |
| 88 |
$model = $nmodel; |
| 89 |
} |
| 90 |
} elsif ($line =~ /init/i) { |
| 91 |
init( $model, $term, $line ); |
| 92 |
} elsif ($line =~ m/^results (table|srx)$/i) { |
| 93 |
$vb_format = lc($1); |
| 94 |
} elsif ($line =~ m/^serializer (\w+)$/i) { |
| 95 |
if (exists($serializer{ $1 })) { |
| 96 |
$serializer = $serializer{ $1 }; |
| 97 |
} else { |
| 98 |
my $ser; |
| 99 |
try { |
| 100 |
$ser = RDF::Trine::Serializer->new( $1 ); |
| 101 |
} catch RDF::Trine::Error::SerializationError with {}; |
| 102 |
if ($ser) { |
| 103 |
$serializer{ $1 } = $ser; |
| 104 |
$serializer = $ser; |
| 105 |
} else { |
| 106 |
print "Unrecognized serializer name '$1'\n"; |
| 107 |
print "Valid serializers are:\n"; |
| 108 |
foreach my $name (RDF::Trine::Serializer->serializer_names) { |
| 109 |
print " $name\n"; |
| 110 |
} |
| 111 |
print "\n"; |
| 112 |
} |
| 113 |
} |
| 114 |
} elsif ($line =~ /debug/i) { |
| 115 |
debug( $model, $term, $line ); |
| 116 |
} elsif ($line =~ /^execute <([^>]+)>$/) { |
| 117 |
my $url = URI->new_abs( $1, $args{ base } ); |
| 118 |
my $query = get( $url ); |
| 119 |
query( $model, $term, $query ); |
| 120 |
} else { |
| 121 |
query( $model, $term, $line ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
sub help { |
| 126 |
print <<"END"; |
| 127 |
Commands: |
| 128 |
help Show this help information. |
| 129 |
use [backend] Switch the storage backend (e.g. "use mysql"). |
| 130 |
init Initialize the storage backend (creating necessary indexes, etc.). |
| 131 |
set prefix [ns]: [uri] Set a namespace for use in subsequent queries. |
| 132 |
results (table|srx) Set the serializer used for tabular variable binding results. |
| 133 |
serializer [format] Set the serializer used for RDF results (e.g. "serializer turtle"). |
| 134 |
debug Print all the quads in the storage backend. |
| 135 |
parse [sparql] Print the parsed algebra for the SPARQL 1.1 query/update. |
| 136 |
explain [sparql] Explain the execution plan for the SPARQL 1.1 query/update. |
| 137 |
time [command] Time the execution of the command. |
| 138 |
execute <URI> Execute the SPARQL update/query obtained by dereferencing URI. |
| 139 |
SELECT ... Execute the SPARQL 1.1 query. |
| 140 |
ASK ... Execute the SPARQL 1.1 query. |
| 141 |
CONSTRUCT ... Execute the SPARQL 1.1 query. |
| 142 |
DESCRIBE ... Execute the SPARQL 1.1 query. |
| 143 |
INSERT ... Execute the SPARQL 1.1 update. |
| 144 |
DELETE ... Execute the SPARQL 1.1 update. |
| 145 |
LOAD <uri> Execute the SPARQL 1.1 update. |
| 146 |
CLEAR ... Execute the SPARQL 1.1 update. |
| 147 |
COPY ... Execute the SPARQL 1.1 update. |
| 148 |
MOVE ... Execute the SPARQL 1.1 update. |
| 149 |
|
| 150 |
END |
| 151 |
} |
| 152 |
|
| 153 |
sub init { |
| 154 |
my $model = shift; |
| 155 |
my $term = shift; |
| 156 |
my $line = shift; |
| 157 |
if (my $store = $model->_store) { |
| 158 |
$store->init; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
sub model { |
| 163 |
my $name = shift; |
| 164 |
my $sclass = RDF::Trine::Store->class_by_name( $name ); |
| 165 |
if ($sclass) { |
| 166 |
if ($sclass eq 'RDF::Trine::Store::Memory') { |
| 167 |
$model = RDF::Trine::Model->new( RDF::Trine::Store::Memory->new ); |
| 168 |
return; |
| 169 |
} else { |
| 170 |
if ($sclass->can('_config_meta')) { |
| 171 |
my $meta = $sclass->_config_meta; |
| 172 |
my $keys = $meta->{required_keys}; |
| 173 |
my $config = { storeclass => $sclass }; |
| 174 |
foreach my $k (@$keys) { |
| 175 |
get_value( $term, $meta, $k, $config ); |
| 176 |
} |
| 177 |
my $store = eval { $sclass->new_with_config( $config ) }; |
| 178 |
if ($store) { |
| 179 |
my $m = RDF::Trine::Model->new( $store ); |
| 180 |
if ($m) { |
| 181 |
return $m; |
| 182 |
} |
| 183 |
} |
| 184 |
print "Failed to construct '$name'-backed model. $@\n"; |
| 185 |
return; |
| 186 |
} else { |
| 187 |
print "Cannot construct model from '$name' storage class.\n"; |
| 188 |
} |
| 189 |
} |
| 190 |
} else { |
| 191 |
print "No storage class named '$name' found\n"; |
| 192 |
return; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
sub explain { |
| 197 |
my $model = shift; |
| 198 |
my $term = shift; |
| 199 |
my $sparql = shift; |
| 200 |
my $psparql = join("\n", $NAMESPACES, $sparql); |
| 201 |
my $query = $class->new( $psparql, \%args ); |
| 202 |
unless ($query) { |
| 203 |
print "Error: " . RDF::Query->error . "\n"; |
| 204 |
return; |
| 205 |
} |
| 206 |
my ($plan, $ctx) = $query->prepare( $model ); |
| 207 |
print $plan->explain(' ', 0); |
| 208 |
} |
| 209 |
|
| 210 |
sub parse { |
| 211 |
my $model = shift; |
| 212 |
my $term = shift; |
| 213 |
my $sparql = shift; |
| 214 |
my $psparql = join("\n", $NAMESPACES, $sparql); |
| 215 |
my $query = $class->new( $psparql, \%args ); |
| 216 |
unless ($query) { |
| 217 |
print "Error: " . RDF::Query->error . "\n"; |
| 218 |
return; |
| 219 |
} |
| 220 |
my $pattern = $query->pattern; |
| 221 |
print $pattern->sse . "\n"; |
| 222 |
} |
| 223 |
|
| 224 |
sub query { |
| 225 |
my $model = shift; |
| 226 |
my $term = shift; |
| 227 |
my $sparql = shift; |
| 228 |
|
| 229 |
my $psparql; |
| 230 |
if ($sparql =~ /^BASE <([^>]+)>/i) { |
| 231 |
$sparql =~ s/^BASE <([^>]+)>/BASE <$1>\n$NAMESPACES/; |
| 232 |
$psparql = $sparql; |
| 233 |
} else { |
| 234 |
$psparql = join("\n", $NAMESPACES, $sparql); |
| 235 |
} |
| 236 |
|
| 237 |
my $query = $class->new( $psparql, \%args ); |
| 238 |
unless ($query) { |
| 239 |
print "Error: " . RDF::Query->error . "\n"; |
| 240 |
return; |
| 241 |
} |
| 242 |
$term->addhistory($sparql); |
| 243 |
try { |
| 244 |
my ($plan, $ctx) = $query->prepare($model); |
| 245 |
my $iter = $query->execute_plan( $plan, $ctx ); |
| 246 |
my $count = -1; |
| 247 |
if (blessed($iter)) { |
| 248 |
if ($iter->isa('RDF::Trine::Iterator::Graph')) { |
| 249 |
$serializer->serialize_iterator_to_file( $term->OUT, $iter ); |
| 250 |
} else { |
| 251 |
if ($vb_format eq 'srx') { |
| 252 |
print $iter->as_xml( 0 ); |
| 253 |
} else { |
| 254 |
print $iter->as_string( 0, \$count ); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
if ($plan->is_update) { |
| 259 |
my $size = $model->size; |
| 260 |
print "$size statements\n"; |
| 261 |
} elsif ($count >= 0) { |
| 262 |
print "$count results\n"; |
| 263 |
} |
| 264 |
} catch RDF::Query::Error with { |
| 265 |
my $e = shift; |
| 266 |
print "Error: $e\n"; |
| 267 |
} otherwise { |
| 268 |
warn "died: " . Dumper(\@_); |
| 269 |
}; |
| 270 |
} |
| 271 |
|
| 272 |
sub debug { |
| 273 |
my $model = shift; |
| 274 |
my $term = shift; |
| 275 |
my $line = shift; |
| 276 |
print "# model = $model\n"; |
| 277 |
if (my $store = $model->_store) { |
| 278 |
print "# store = $store\n"; |
| 279 |
} |
| 280 |
my $iter = $model->get_statements( undef, undef, undef, undef ); |
| 281 |
my @rows; |
| 282 |
my @names = qw[subject predicate object context]; |
| 283 |
while (my $row = $iter->next) { |
| 284 |
push(@rows, [map {$row->$_()->as_string} @names]); |
| 285 |
} |
| 286 |
my @rule = qw(- +); |
| 287 |
my @headers = (\q"| "); |
| 288 |
push(@headers, map { $_ => \q" | " } @names); |
| 289 |
pop @headers; |
| 290 |
push @headers => (\q" |"); |
| 291 |
my $table = Text::Table->new(@names); |
| 292 |
$table->rule(@rule); |
| 293 |
$table->body_rule(@rule); |
| 294 |
$table->load(@rows); |
| 295 |
print join('', |
| 296 |
$table->rule(@rule), |
| 297 |
$table->title, |
| 298 |
$table->rule(@rule), |
| 299 |
map({ $table->body($_) } 0 .. @rows), |
| 300 |
$table->rule(@rule) |
| 301 |
); |
| 302 |
my $size = scalar(@rows); |
| 303 |
print "$size statements\n"; |
| 304 |
} |
| 305 |
|
| 306 |
sub get_value { |
| 307 |
my $term = shift; |
| 308 |
my $meta = shift; |
| 309 |
my $k = shift; |
| 310 |
my $config = shift; |
| 311 |
if (my $v = $config->{$k}) { |
| 312 |
return; |
| 313 |
} elsif (defined($meta->{fields}{$k}{'value'})) { |
| 314 |
$config->{ $k } = $meta->{fields}{$k}{'value'}; |
| 315 |
} elsif (defined($meta->{fields}{$k}{'template'})) { |
| 316 |
my $template = $meta->{fields}{$k}{'template'}; |
| 317 |
my @subkeys = ($template =~ m/\[%(\w+)%\]/g); |
| 318 |
foreach my $sk (@subkeys) { |
| 319 |
get_value( $term, $meta, $sk, $config ); |
| 320 |
} |
| 321 |
while ($template =~ m/\[%(\w+)%\]/) { |
| 322 |
my $key = $1; |
| 323 |
my $v = $config->{$key}; |
| 324 |
$template =~ s/\[%$key%\]/$v/e; |
| 325 |
} |
| 326 |
$config->{ $k } = $template; |
| 327 |
} else { |
| 328 |
my $desc = $meta->{fields}{$k}{description}; |
| 329 |
my $type = $meta->{fields}{$k}{type}; |
| 330 |
my $value; |
| 331 |
if ($type eq 'password') { |
| 332 |
print "$desc: "; |
| 333 |
ReadMode('noecho'); |
| 334 |
$value = ReadLine(0, $term->IN); |
| 335 |
chomp($value); |
| 336 |
} elsif ($type eq 'filename') { |
| 337 |
my $attribs = $term->Attribs; |
| 338 |
local($attribs->{completion_entry_function}) = $attribs->{filename_completion_function}; |
| 339 |
$value = $term->readline("$desc: "); |
| 340 |
} else { |
| 341 |
$value = $term->readline("$desc: ") |
| 342 |
} |
| 343 |
$config->{ $k } = $value; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
{ my $memory_model; |
| 348 |
sub memory_model { |
| 349 |
if (defined($memory_model)) { |
| 350 |
return $memory_model; |
| 351 |
} else { |
| 352 |
my $model = RDF::Trine::Model->temporary_model; |
| 353 |
$memory_model = $model; |
| 354 |
return $model; |
| 355 |
} |
| 356 |
}} |
| 357 |
|
| 358 |
__END__ |
| 359 |
|
| 360 |
=head1 NAME |
| 361 |
|
| 362 |
rqsh - SPARQL database shell |
| 363 |
|
| 364 |
=head1 DESCRIPTION |
| 365 |
|
| 366 |
rqsh provides a command-line interface to the SPARQL 1.1 implementation of |
| 367 |
RDF::Query. It defaults to using an in-memory database, but can be configured |
| 368 |
to use any database implemented as an L<RDF::Trine::Store>. |
| 369 |
|
| 370 |
=head1 COMMANDS |
| 371 |
|
| 372 |
=over 4 |
| 373 |
|
| 374 |
=item help |
| 375 |
|
| 376 |
Show help information on available commands. |
| 377 |
|
| 378 |
=item use [backend] |
| 379 |
|
| 380 |
Switch the storage backend (e.g. "use mysql"). |
| 381 |
You will be prompted to enter any necessary connection/configuration data. |
| 382 |
|
| 383 |
=item init |
| 384 |
|
| 385 |
Initialize the storage backend (creating necessary indexes, etc.). |
| 386 |
|
| 387 |
=item set prefix [ns]: [uri] |
| 388 |
|
| 389 |
Set a namespace for use in subsequent queries. |
| 390 |
|
| 391 |
=item results (table|srx) |
| 392 |
|
| 393 |
Set the serializer used for tabular variable binding results. |
| 394 |
|
| 395 |
=item serializer [format] |
| 396 |
|
| 397 |
Set the serializer used for RDF results (e.g. "serializer turtle"). |
| 398 |
|
| 399 |
=item debug |
| 400 |
|
| 401 |
Print all the quads in the storage backend. |
| 402 |
|
| 403 |
=item parse [sparql] |
| 404 |
|
| 405 |
Print the parsed algebra for the SPARQL 1.1 query/update. |
| 406 |
|
| 407 |
=item explain [sparql] |
| 408 |
|
| 409 |
Print the execution plan for the SPARQL 1.1 query/update. |
| 410 |
|
| 411 |
=item time [command] |
| 412 |
|
| 413 |
Time the execution of the command. |
| 414 |
|
| 415 |
=item execute <URI> |
| 416 |
|
| 417 |
Execute the SPARQL update/query obtained by dereferencing URI. |
| 418 |
|
| 419 |
=item SELECT ... |
| 420 |
|
| 421 |
=item ASK ... |
| 422 |
|
| 423 |
=item CONSTRUCT ... |
| 424 |
|
| 425 |
=item DESCRIBE ... |
| 426 |
|
| 427 |
Execute the SPARQL 1.1 query. |
| 428 |
|
| 429 |
=item INSERT ... |
| 430 |
|
| 431 |
=item DELETE ... |
| 432 |
|
| 433 |
=item LOAD <uri> |
| 434 |
|
| 435 |
=item CLEAR ... |
| 436 |
|
| 437 |
=item COPY ... |
| 438 |
|
| 439 |
=item MOVE ... |
| 440 |
|
| 441 |
Execute the SPARQL 1.1 update. |
| 442 |
|
| 443 |
=back |
| 444 |
|
| 445 |
=cut |