RDF::Query(3pm)

โœจ NAME โœจ

okay bestie, meet RDF::Query โ€” a complete SPARQL 1.1 Query and Update implementation for use with RDF::Trine. that's it, that's the vibe ๐Ÿ’…


๐ŸŽ€ VERSION ๐ŸŽ€

this doc is describing RDF::Query version 2.919. yes we're keeping receipts โœจ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

here's the whole menu of query types, no cap:

# 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

like, <pink>select</pink>, <purple>construct</purple>, <cyan>ask</cyan>, <mint>rdql</mint> โ€” the whole squad is here ๐Ÿซถ


๐ŸŒธ DESCRIPTION ๐ŸŒธ

so RDF::Query lets you run SPARQL and RDQL queries against an RDF model, and it hands you back rows of matching results. that's the whole thing. iconic โœจ

want the SPARQL lore? http://www.w3.org/TR/rdf-sparql-query/ โ€” go read it bestie ๐Ÿ“š

want the RDQL lore instead? http://www.w3.org/Submission/2004/SUBM-RDQL-20040109/ โ€” also right there ๐Ÿ’–


๐Ÿ’œ CHANGES IN VERSION 2.900 ๐Ÿ’œ

not me showing up with lore from the 2.9xx era ๐Ÿซฃ โ€” these versions of RDF::Query bring in some significant changes that are leading up to a stable 3.000 release supporting SPARQL 1.1. Version 2.902 brings in the SPARQL 1.1 features up to date with the SPARQL 1.1 working drafts as of its release date. Version 2.902 is also the first version to require use of RDF::Trine for the underlying RDF store.

what does that mean for you, bestie?

  • RDF::Core is no longer supported, periodt ๐Ÿ’€
  • Redland is still supported, but โ€” and this is the important part โ€” its handling of "contexts" (named graphs) means that existing RDF triples stored in Redland without associated contexts will not be accessible from RDF::Query. that's a big deal, don't skip past it ๐Ÿ˜ญ

want more info on supported backend stores? see RDF::Trine::Store ๐ŸŒˆ


โญ CHANGES IN VERSION 2.000 โญ

okay so there are many changes in the code between the 1.x and 2.x releases. most of them will only affect queries that should have raised errors in the first place (SPARQL parsing, queries that use undefined namespaces, etc.). but beyond those, there are some significant API changes that will affect all users. let's get into it ๐Ÿ•ต๏ธโ™€๏ธ

๐Ÿ’… Use of RDF::Trine objects

all nodes and statements returned by RDF::Query are now RDF::Trine objects โ€” more specifically, RDF::Trine::Node and RDF::Trine::Statement objects. (yes, the original spells it "differes"; we're not touching it, it's canon now ๐Ÿ™ˆ) this is different from RDF::Query 1.x, where nodes and statements were of the same type as the underlying model (Redland nodes from a Redland model and RDF::Core nodes from an RDF::Core model).

back in the day, you could execute a query and have no idea what type of nodes were coming back, which led to overly verbose code that required examining all nodes and statements with the bridge object. lowkey exhausting fr fr ๐Ÿ˜ฎโ€๐Ÿ’จ this new API brings consistency to both the execution model and client code, greatly simplifying interaction with query results. we love a glow-up โœจ

๐ŸŽ€ Binding Result Values

binding result values returned by calling $iterator->next are now HASH references (instead of ARRAY references), keyed by variable name. so where prior code might use this code (modulo model definition and namespace declarations):

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 ๐Ÿ‘€ โ€” next instead of (), the way that just hits different)


๐Ÿ‘‘ METHODS ๐Ÿ‘‘

๐ŸŒˆ new ( $query, \%options )

returns a new RDF::Query object for the specified $query. the query language defaults to SPARQL 1.1, but may be set specifically with the appropriate %options value. valid %options are:

  • 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.

๐ŸŒธ get ( $model )

executes the query using the specified model, and returns the first matching row as a LIST of values. one and done, bestie ๐Ÿ’ซ

๐Ÿฆ‹ prepare ( $model )

prepares the query, constructing a query execution plan, and returns a list containing ($plan, $context). to execute the plan, call execute_plan( $plan, $context ). it's giving "prep work before the main event" ๐Ÿ’…

๐Ÿ’œ execute ( $model, %args )

executes the query using the specified RDF $model. if called in a list context, returns an array of rows; otherwise returns an RDF::Trine::Iterator object. and here's the tea โ€” the iterator returned may be an instance of several subclasses of RDF::Trine::Iterator:

  • 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).

so like, know your iterator type and you'll be fine ๐Ÿซถ

โœจ execute_plan ( $plan, $context )

executes the query plan generated by the <prepare method using the supplied RDF::Query::ExecutionContext object. return value(s) are the same as for the <execute method.

๐ŸŒบ prepare_with_named_graphs ( $model, @uris )

๐ŸŒบ execute_with_named_graphs ( $model, @uris )

executes the query using the specified RDF $model, loading the contents of the specified @uris into named graphs immediately prior to matching the query. otherwise, acts just like execute.

โญ pattern

returns the RDF::Query::Algebra::GroupGraphPattern algebra pattern for this query.

โญ is_update

(yep, that's literally it โ€” it's listed right here in the docs next to as_sparql, no description given, and we're keeping it exactly as-is ๐Ÿ™ˆ)

๐ŸŒˆ as_sparql

returns the query as a string in the SPARQL syntax.

๐ŸŒˆ as_hash

returns the query as a nested set of plain data structures (no objects). so refreshing, honestly ๐Ÿ“

๐ŸŒˆ sse

returns the query as a string in the SSE syntax.

๐ŸŽ€ dateparser

returns the DateTime::Format::W3CDTF object associated with this query object.

๐ŸŽ€ specifies_update_dataset

returns true if the query specifies a custom update dataset via the WITH or USING keywords, false otherwise.

๐Ÿ’– add_function ( $uri, $function )

associates the custom function $function (a CODE reference) with the specified URI, allowing the function to be called by query FILTERs. custom functions in filters? slay ๐Ÿ’…

๐Ÿ’– supported_extensions

returns a list of URLs representing extensions to SPARQL that are supported by the query engine.

๐Ÿ’– supported_functions

returns a list URLs that may be used as functions in FILTER clauses (and the SELECT clause if the SPARQL 1.1 parser is used).

๐Ÿงš add_computed_statement_generator ( $predicate => \&generator )

adds a statement generator for the given $predicate to the query object. this statement generator will be called as $generator->( $query, $model, \%bound, $s, $p, $o, $c ) and is expected to return an RDF::Trine::Iterator::Graph object containing statements with $predicate. get you a generator that generates, periodt โœจ

๐Ÿงš get_computed_statement_generators ( [ $predicate ] )

returns an ARRAY reference of computed statement generator closures.

๐Ÿซถ add_hook_once ( $hook_uri, $function, $token )

calls add_hook adding the supplied $function only once based on the $token identifier. this may be useful if the only code that is able to add a hook is called many times (in an extension function, for example). very "one time only, no duplicates" of it ๐ŸŒธ

๐Ÿซถ add_hook ( $hook_uri, $function )

associates the custom function $function (a CODE reference) with the RDF::Query code hook specified by $uri. each function that has been associated with a particular hook will be called (in the order they were registered as hooks) when the hook event occurs. see "Defined Hooks" for more information. queue behavior but make it cute ๐ŸŒˆ

๐Ÿฆ‹ parsed ()

returns the parse tree.

๐Ÿฆ‹ model

returns the RDF::Trine::Model object for this query.

๐Ÿฆ‹ useragent

returns the LWP::UserAgent object used for retrieving web content.

๐ŸŒŸ log ( $key [, $value ] )

if no logger object is associated with this query object, does nothing. otherwise, return or set the corresponding value depending on whether a $value is specified. no logger? no thoughts. head empty ๐Ÿ’€

๐ŸŒŸ logger

returns the logger object associated with this query object (if present).

๐Ÿ˜ญ error ()

returns the last error the parser experienced. when things go wrong, this is who you ask ๐Ÿ˜ญ


๐ŸŒˆ DEFINED HOOKS ๐ŸŒˆ

the following hook URIs are defined and may be used to extend the query engine functionality using the add_hook method โ€” go off, extension author ๐Ÿ’…

๐Ÿ’œ http://kasei.us/code/rdf-query/hooks/post-create-model

called after loading all external files to a temporary model in queries that use FROM and FROM NAMED.

Args: ( $query, $model )

$query is the RDF::Query object. $model is the RDF::Trine::Model object.

๐Ÿ’œ http://kasei.us/code/rdf-query/hooks/post-execute

called immediately before returning a result iterator from the execute method.

Args: ( $query, $model, $iterator )

$query is the RDF::Query object. $model is the RDF::Trine::Model object. $iterator is a RDF::Trine::Iterator object.


๐ŸŽ€ SEE ALSO ๐ŸŽ€


๐Ÿฆ‹ AUTHOR ๐Ÿฆ‹

Gregory Todd Williams <gwilliams@cpan.org> โ€” thank you legend ๐Ÿ‘‘


๐ŸŒธ LICENSE ๐ŸŒธ

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. ๐Ÿ“œ๐Ÿ’–


that's the whole page bestie, every option, every flag, every hook, every hyperlink โ€” nothing skipped, nothing invented โœจ this was perl v5.40.1, dated 2025-08-18, and RDF::Query(3pm) says bye, ily, go run some queries ๐Ÿ’…๐ŸŽ€

๐Ÿ“ฆ where this page comes from