| Redland documentation | Contained in the Redland distribution. |
RDF::Redland::QueryResults - Redland RDF Syntax Query Results Class
use RDF::Redland;
...
my $query=new RDF::Redland::Query("query string", undef, undef, "sparql");
my $results=$model->query_execute($query);
# or my $results=$query->execute($model);
while(!$results->finished) {
for (my $i=0; $i < $results->bindings_count(); $i++) {
my $name=$results->binding_name($i);
my $value=$results->binding_value($i);
# ... do something with the results
}
$results->next_result;
}
The $results in the example is an object of class RDF::Redland::QueryResults.
This class represents queries of various syntaxes over an RDF::Redland::Model returning a sequence of results that bind variable names to RDF::Redland::Node values.
There are no public constructors.
Return the number of current results from the query.
Return non-0 if the results have been exhausted.
Get the name of variable binding INDEX in the array of variable names.
Get the names all of the variable bindings as an array.
Get the value of the variable binding INDEX in the current query result.
Get the values of all of the variable bindings in the current query result.
Get the value of the variable binding NAME in the current query result.
Get the variable names and values of the current query result as a hash
Return the number of variable bindings.
Move to the next query result.
Return a new RDF::Redland::Stream object representing the query results as an RDF Graph.
Serialize to a string syntax in format FORMAT-URI using the optional BASE-URI. The default format when none is given is determined by librdf_query_results_to_string.
Return non-0 if the query results format is variable bindings
Return non-0 if the query results format is a boolean
Return non-0 if the query results format is an RDF graph
Get the boolean query result; non-0 is true.
Dave Beckett - http://purl.org/net/dajobe/
| Redland documentation | Contained in the Redland distribution. |
# -*- Mode: Perl -*- # # QueryResults.pm - Redland Perl RDF Query Results module # # $Id: QueryResults.pm 10593 2006-03-05 08:30:38Z dajobe $ # # Copyright (C) 2004-2005 David Beckett - http://purl.org/net/dajobe/ # Copyright (C) 2004-2005 University of Bristol - http://www.bristol.ac.uk/ # # This package is Free Software or Open Source available under the # following licenses (these are alternatives): # 1. GNU Lesser General Public License (LGPL) # 2. GNU General Public License (GPL) # 3. Mozilla Public License (MPL) # # See LICENSE.html or LICENSE.txt at the top of this package for the # full license terms. # # # package RDF::Redland::QueryResults; use RDF::Redland::Stream; use strict;
######################################################################
# CONSTRUCTOR sub new ($$) { my($proto,$object)=@_; my $class = ref($proto) || $proto; my $self = {}; $self->{QUERYRESULTS}=$object; bless ($self, $class); return $self; } # DESTRUCTOR sub DESTROY ($) { warn "RDF::Redland::QueryResults DESTROY\n" if $RDF::Redland::Debug; &RDF::Redland::CORE::librdf_free_query_results(shift->{QUERYRESULTS}); }
sub count ($) { my($self)=@_; return &RDF::Redland::CORE::librdf_query_results_get_count($self->{QUERYRESULTS}); }
sub finished ($) { my($self)=@_; return &RDF::Redland::CORE::librdf_query_results_finished($self->{QUERYRESULTS}); }
sub binding_name ($$) { my($self,$index)=@_; return &RDF::Redland::CORE::librdf_query_results_get_binding_name($self->{QUERYRESULTS},$index); }
sub binding_names ($) { my($self)=shift; my(@names); my $count=&RDF::Redland::CORE::librdf_query_results_get_bindings_count($self->{QUERYRESULTS}); for (my $i=0; $i < $count; $i++) { push(@names, $self->binding_name($i)); } return @names; }
sub binding_value ($$) { my($self,$index)=@_; my $node=&RDF::Redland::CORE::librdf_query_results_get_binding_value($self->{QUERYRESULTS},$index); RDF::Redland::Node->_new_from_object($node); }
sub binding_values ($) { my($self)=shift; my(@values); my $count=&RDF::Redland::CORE::librdf_query_results_get_bindings_count($self->{QUERYRESULTS}); for (my $i=0; $i < $count; $i++) { push(@values, $self->binding_value($i)); } return @values; }
sub binding_value_by_name ($$) { my($self,$name)=@_; my $node=&RDF::Redland::CORE::librdf_query_results_get_binding_value_by_name($self->{QUERYRESULTS},$name); RDF::Redland::Node->_new_from_object($node); }
sub bindings ($) { my($self)=shift; my(%results); my $count=&RDF::Redland::CORE::librdf_query_results_get_bindings_count($self->{QUERYRESULTS}); for (my $i=0; $i < $count; $i++) { $results{$self->binding_name($i)}=$self->binding_value($i); } return %results; }
sub bindings_count ($) { my($self)=@_; return &RDF::Redland::CORE::librdf_query_results_get_bindings_count($self->{QUERYRESULTS}); }
sub next_result ($) { my($self)=@_; return &RDF::Redland::CORE::librdf_query_results_next($self->{QUERYRESULTS}); }
sub as_stream($) { my($self)=@_; die "Query results is not in RDF graph format" unless $self->is_graph; my $stream=&RDF::Redland::CORE::librdf_query_results_as_stream($self->{QUERYRESULTS}); return new RDF::Redland::Stream($stream,$self); }
sub to_string($;$$) { my($self, $format_uri, $base_uri)=@_; $format_uri ||= undef; $base_uri ||= undef; if($self->is_graph) { my $tmpstorage=RDF::Redland::Storage->new("memory"); my $tmpmodel=RDF::Redland::Model->new($tmpstorage, ""); $tmpmodel->add_statements($self->as_stream); my $serializer=RDF::Redland::Serializer->new(); return $serializer->serialize_model_to_string($base_uri, $tmpmodel); } if(!$self->is_boolean && !$self->is_bindings) { die "Unknown query result format cannot be written as a string"; } if($format_uri && !ref($format_uri)) { $format_uri = RDF::Redland::URI->new($format_uri); } my $rformat_uri=$format_uri ? $format_uri->{URI} : undef; if($base_uri && !ref($base_uri)) { $base_uri = RDF::Redland::URI->new($base_uri); } my $rbase_uri=$base_uri ? $base_uri->{URI} : undef; warn "RDF::Redland::QueryResults format URI ".$format_uri->as_string." base URI ".($base_uri ? $base_uri->as_string : "undef")."\n" if $RDF::Redland::Debug; return &RDF::Redland::CORE::librdf_query_results_to_string($self->{QUERYRESULTS}, $rformat_uri, $rbase_uri); }
sub is_bindings($) { my($self)=shift; return &RDF::Redland::CORE::librdf_query_results_is_bindings($self->{QUERYRESULTS}); }
sub is_boolean($) { my($self)=shift; return &RDF::Redland::CORE::librdf_query_results_is_boolean($self->{QUERYRESULTS}); }
sub is_graph($) { my($self)=shift; return &RDF::Redland::CORE::librdf_query_results_is_graph($self->{QUERYRESULTS}); }
sub get_boolean($) { my($self)=shift; die "Query result is not in boolean format" unless $self->is_boolean; return &RDF::Redland::CORE::librdf_query_results_get_boolean($self->{QUERYRESULTS}); }
1;