KDE 4.9 PyKDE API Reference

- KDE's Python API

# QueryResultIterator Class Reference

`from PyKDE4.soprano import *`

Subclasses: Soprano.Client.DBusQueryResultIterator
Namespace: Soprano

## Detailed Description

\class QueryResultIterator queryresultiterator.h Soprano/QueryResultIterator

An iterator for query results.

%Query results in %Soprano are wrapped in a QueryResultIterator.

%Query iterators are returned by Model.executeQuery(). In contrast to NodeIterator or StatementIterator %QueryResultIterator has a set of different access methods for the current dataset which can be one of three things:

- A Statement: Graph query results are represented as a stream of statements.
See also currentStatement() and iterateStatements().
- A BindingSet: Tuple query results are represented by a set of variable bindings
according to the variables used in the query. The bindings can be accessed as a set through the normal Iterator method current() or separately through #binding(int) const or #binding(const QString&) const.
- A boolean value: This is a special case in which the query was a boolean query
(a SPARQL ASK query). In this case there is nothing to iterate but only a single boolean value which can be accessed through boolValue().

Example:

     QueryResultIterator it = model->executeQuery( someGraphQuery );
     while( it.next() ) {
        doSomething( it.currentStatement() );
     }

     QueryResultIterator it2 = model->executeQuery( someTupleQuery );
     while( it.next() ) {
        doSomethingElse( it.currentBindings() );
        doSomethingCompletelyDifferent( it.binding( "x" ) );
        doSomethingEntirelyDifferent( it.binding( 0 ) );
     }

Many backends do lock the underlying Model during iteration. Thus, it is always a good idea to cache the results if they are to be used to modify the model to prevent a deadlock:

     Soprano.QueryResultIterator it = model->executeQuery( someTupleQuery );
     QList<BindingSet> allBindings = it.allBindings();
     Q_FOREACH( Soprano.BindingSet bs, allBindings ) {
        modifyTheModel( model, bs );
     }

Iterators have to be closed. This can either be achieved by deleting the iterator, finishing it (next() does return false), or calling close(). Before that other operations on the Model may block.

Iterators are not thread-safe. Two threads using the same iterator may result in undefined behaviour and even crashes. An iterator needs to be closed by the same thread that opened it (except if the iterator contains special code to handle such a situation.)

Warning:
  Be aware that iterators in Soprano are shared objects which means that copies of one iterator object work on the same data.

For further details on %Soprano iterators see Iterator.

Author:
  Daniele Galdi <daniele.galdi@gmail.com>
  Sebastian Trueg <trueg@kde.org>

## Methods

| __init__ (self)
| __init__ (self, Soprano.QueryResultIterator a0)
| __init__ (self, Soprano.QueryResultIteratorBackend qr)
[Soprano.BindingSet] | allBindings (self)
[Soprano.BindingSet] | allElements (self)
Soprano.Node | binding (self, QString name)
Soprano.Node | binding (self, int offset)
int | bindingCount (self)
QStringList | bindingNames (self)
bool | boolValue (self)
| close (self)
Soprano.BindingSet | current (self)
Soprano.BindingSet | currentBindings (self)
Soprano.Statement | currentStatement (self)
bool | isBinding (self)
bool | isBool (self)
bool | isGraph (self)
bool | isValid (self)
Soprano.NodeIterator | iterateBindings (self, QString variableName)
Soprano.NodeIterator | iterateBindings (self, int offset)
Soprano.StatementIterator | iterateStatements (self)
Soprano.StatementIterator | iterateStatementsFromBindings (self, QString subjectBindingName, QString predicateBindingName, QString objectBindingName, QString contextBindingName=QString(), Soprano.Statement templateStatement=Soprano.Statement())
bool | next (self)
Soprano.Node | operator [] (self, int offset)
Soprano.Node | operator [] (self, QString name)

## Method Documentation

__init__ | ( |  | self ) |

Copy constructor. Copies of iterators share their data.

__init__ | ( | self, |
|  | Soprano.QueryResultIterator | a0
| ) |  |  |

Copy constructor. Copies of iterators share their data.

__init__ | ( | self, |
|  | Soprano.QueryResultIteratorBackend | qr
| ) |  |  |

Create a new QueryResultIterator which uses qr as backend. QueryResultIterator will take ownership of the QueryResultIteratorBackend.

[Soprano.BindingSet] allBindings | ( |  | self ) |

Convenience method that collects all binding sets that are left in the iterator.

[Soprano.BindingSet] allElements | ( |  | self ) |

Soprano.Node binding | ( | self, |
|  | QString | name
| ) |  |  |

Get the current binding for a variable by index.

Parameters:

  | offset | The index of the requested variable.

This method does only make sense for tuple queries.

Returns:
  The binding for the requested variable or and invalid node if offset is out of bounds, i.e. bigger or equal to bindingCount().

Soprano.Node binding | ( | self, |
|  | int | offset
| ) |  |  |

Get the current binding for a variable by index.

Parameters:

  | offset | The index of the requested variable.

This method does only make sense for tuple queries.

Returns:
  The binding for the requested variable or and invalid node if offset is out of bounds, i.e. bigger or equal to bindingCount().

int bindingCount | ( |  | self ) |

The number of bindings in this query result.

This method does only make sense for tuple queries.

Returns:
  The number of bindings.

QStringList bindingNames | ( |  | self ) |

This method does only make sense for tuple queries.

Returns:
  The names of the bound variables in this query result.

bool boolValue | ( |  | self ) |

This method does only make sense for boolean queries.

Returns:
  The result of a boolean query (SPARQL ASK).

\sa isBool()

close | ( |  | self ) |

Soprano.BindingSet current | ( |  | self ) |

Soprano.BindingSet currentBindings | ( |  | self ) |

Convenience method that puts all current bindings into one map. This method does only make sense for tuple queries.

Soprano.Statement currentStatement | ( |  | self ) |

Retrieve the current Statement after a call to next. This method does only make sense for graph queries.

bool isBinding | ( |  | self ) |

Check if this is a tuple result.

Returns:
  true if this result refers to a tuple query, i.e. currentBindings(), binding(), bindingCount(), bindingNames(), and allBindings() return valid values.

bool isBool | ( |  | self ) |

Check if this is a boolean result.

There is no need to call next() for boolean results.

Returns:
  true if this result refers to a boolean query (SPARQL ASK), i.e. boolValue() returns a valid value.

bool isGraph | ( |  | self ) |

Check if this is a graph result.

Returns:
  true if this result refers to a graph query, i.e. currentStatement() and iterateStatements() return valid values.

bool isValid | ( |  | self ) |

Soprano.NodeIterator iterateBindings | ( | self, |
|  | QString | variableName
| ) |  |  |

Convenience method that creates an iterator over one column of bindings in this query result. This method does only make sense for tuple queries.

Parameters:

  | offset | The index of the requested variable.

Warning:
  The new iterator is just a wrapper around this one. Thus, changing it will also change this one.

Returns:
  A wrapper iterator over one column in a tuple query or an invalid iterator if offset is out of bounds, i.e. bigger or equal to bindingCount().

Soprano.NodeIterator iterateBindings | ( | self, |
|  | int | offset
| ) |  |  |

Convenience method that creates an iterator over one column of bindings in this query result. This method does only make sense for tuple queries.

Parameters:

  | offset | The index of the requested variable.

Warning:
  The new iterator is just a wrapper around this one. Thus, changing it will also change this one.

Returns:
  A wrapper iterator over one column in a tuple query or an invalid iterator if offset is out of bounds, i.e. bigger or equal to bindingCount().

Soprano.StatementIterator iterateStatements | ( |  | self ) |

Convenience method that creates an iterator over the statements in this query result. This method does only make sense for graph queries.

Warning:
  The new iterator is just a wrapper around this one. Thus, changing it will also change this one.

Returns:
  A wrapper iterator over the statements in a graph query.

Soprano.StatementIterator iterateStatementsFromBindings | ( | self, |
|  | QString | subjectBindingName,
|  | QString | predicateBindingName,
|  | QString | objectBindingName,
|  | QString | contextBindingName=QString(),
|  | Soprano.Statement | templateStatement=Soprano.Statement()
| ) |  |  |

Convenience method that creates an iterator over statements constructed from the values of the provided bindings.

The typical usage would be with a query as follows:

     Soprano.StatementIterator it =
         model->executeQuery( "select * where { graph ?c { ?s ?p ?o . } }" )
         .iterateStatementsFromBindings( "s", "p", "o", "c" );

Parameters:

  | subjectBindingName | The name of the binding that will be used to set the subject of the constructed statements.

Parameters:

  | predicateBindingName | The name of the binding that will be used to set the predicate of the constructed statements.

Parameters:

  | objectBindingName | The name of the binding that will be used to set the object of the constructed statements.

Parameters:

  | contextBindingName | The name of the binding that will be used to set the context of the constructed statements.

Parameters:

  | templateStatement | If any of the provided binding names is empty the corresponding nodes in the resulting statements will be filled by templateStatement.

Warning:
  The new iterator is just a wrapper around this one. Thus, changing it will also change this one.

Returns:
  A wrapper iterator over statements constructed from the specified bindings.

Since:
  2.2

bool next | ( |  | self ) |

Soprano.Node operator [] | ( | self, |
|  | int | offset
| ) |  |  |

Get the current binding for a variable.

Parameters:

  | name | The name of the requested variable.

This is equivalent to binding(const QString&) const.

Returns:
  The binding for the requested variable or and invalid node if the bindings do not contain the variable.

Since:
  2.2

Soprano.Node operator [] | ( | self, |
|  | QString | name
| ) |  |  |

Get the current binding for a variable.

Parameters:

  | name | The name of the requested variable.

This is equivalent to binding(const QString&) const.

Returns:
  The binding for the requested variable or and invalid node if the bindings do not contain the variable.

Since:
  2.2