RDF::Query(3pm) | User Contributed Perl Documentation | RDF::Query(3pm) |
RDF::Query - A complete SPARQL 1.1 Query and Update implementation for use with RDF::Trine.
This document describes RDF::Query version 2.918.
# SPARQL SELECT Query my $query = RDF::Query->new( 'SELECT * WHERE ...' ); my $iterator = $query->execute( $model ); while (my $row = $iterator->next) { # $row is a HASHref containing variable name -> RDF Term bindings print $row->{ 'var' }->as_string; } # SPARQL CONSTRUCT/DESCRIBE Query my $query = RDF::Query->new( 'CONSTRUCT { ... } WHERE ...' ); my $iterator = $query->execute( $model ); while (my $st = $iterator->next) { # $st is a RDF::Trine::Statement object representing an RDF triple print $st->as_string; } # SPARQL ASK Query my $query = RDF::Query->new( 'ASK WHERE ...' ); my $iterator = $query->execute( $model ); my $bool = $iterator->get_boolean; if ($bool) { print "Yes!\n"; } # RDQL Query my $query = new RDF::Query ( $rdql, { lang => 'rdql' } ); my @rows = $query->execute( $model ); # in list context, returns all results
RDF::Query allows SPARQL and RDQL queries to be run against an RDF model, returning rows of matching results.
See <http://www.w3.org/TR/rdf-sparql-query/> for more information on SPARQL.
See <http://www.w3.org/Submission/2004/SUBM-RDQL-20040109/> for more information on RDQL.
The 2.9xx versions of RDF::Query introduce some significant changes that will lead to a stable 3.000 release supporting SPARQL 1.1. Version 2.902 introduces the SPARQL 1.1 features up to date with the SPARQL 1.1 working drafts as of its release date. Version 2.902 also is the first version to require use of RDF::Trine for the underlying RDF store. This change means that RDF::Core is no longer supported, and while Redland is still supported, its handling of "contexts" (named graphs) means that existing RDF triples stored in Redland without associated contexts will not be accessible from RDF::Query. See RDF::Trine::Store for more information on supported backend stores.
There are many changes in the code between the 1.x and 2.x releases. Most of these changes will only affect queries that should have raised errors in the first place (SPARQL parsing, queries that use undefined namespaces, etc.). Beyond these changes, however, there are some significant API changes that will affect all users:
In the past, it was possible to execute a query and not know what type of nodes were going to be returned, leading to overly verbose code that required examining all nodes and statements with the bridge object. This new API brings consistency to both the execution model and client code, greatly simplifying interaction with query results.
my $sparql = 'SELECT ?name ?homepage WHERE { [ foaf:name ?name ; foaf:homepage ?homepage ] }'; my $query = RDF::Query->new( $sparql ); my $iterator = $query->execute( $model ); while (my $row = $iterator->()) { my ($name, $homepage) = @$row; # ... }
New code using RDF::Query 2.000 and later should instead use:
my $sparql = 'SELECT ?name ?homepage WHERE { [ foaf:name ?name ; foaf:homepage ?homepage ] }'; my $query = RDF::Query->new( $sparql ); my $iterator = $query->execute( $model ); while (my $row = $iterator->next) { my $name = $row->{ name }; my $homepage = $row->{ homepage }; # ... }
(Also notice the new method calling syntax for retrieving rows.)
* lang
Specifies the query language. Acceptable values are 'sparql11', 'sparql', or 'rdql'.
* base_uri
Specifies the base URI used in parsing the query.
* update
A boolean value indicating whether update operations are allowed during query execution.
* load_data
A boolean value indicating whether URIs used in SPARQL FROM and FROM NAMED clauses should be dereferenced and the resulting RDF content used to construct the dataset against which the query is run.
* A RDF::Trine::Iterator::Bindings object is returned for query forms producing variable binding results (SELECT queries).
* A RDF::Trine::Iterator::Graph object is returned for query forms producing in an RDF graph result (DESCRIBE and CONSTRUCT queries).
* A RDF::Trine::Iterator::Boolean object is returned for query forms producing a true/false result (ASK queries).
The following hook URIs are defined and may be used to extend the query engine functionality using the "add_hook" method:
Args: ( $query, $model )
$query is the RDF::Query object. $model is the RDF::Trine::Model object.
Args: ( $query, $model, $iterator )
$query is the RDF::Query object. $model is the RDF::Trine::Model object. $iterator is a RDF::Trine::Iterator object.
<http://www.perlrdf.org/>
Gregory Todd Williams <gwilliams@cpan.org>
Copyright (c) 2005-2012 Gregory Todd Williams. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2021-01-05 | perl v5.32.0 |