Bases: surf.query.Group
A SPARQL triple pattern named group
Bases: surf.query.Group
A SPARQL triple pattern optional group
Bases: object
The Query object is used by SuRF to construct queries in a programatic manner. The class supports the major SPARQL query types: select, ask, describe, construct. Although it follows the SPARQL format the query can be translated to other Query formats such as PROLOG, for now though only SPARQL is supported.
Query objects should not be instatiated directly, instead use module-level ask(), construct(), describe(), select() functions.
Query methods can be chained.
Add FILTER construct to query WHERE clause.
filter must be either string/unicode or surf.query.Filter object, if it is None then no filter is appended.
Add graph URI(s) that will go in separate FROM clause.
Each argument can be either string or surf.rdf.URIRef.
Add graph URI(s) that will go in separate FROM NAMED clause.
Each argument can be either string or surf.rdf.URIRef.
Add GROUP ?name { ... } construct to WHERE clause.
name is the variable name that will be bound to graph IRI.
*statements is one or more graph patterns.
Example:
>>> import surf
>>> from surf.query import a, select
>>> query = select("?s", "?src").named_group("?src", ("?s", a, surf.ns.FOAF['Person']))
>>> print unicode(query)
SELECT ?s ?src WHERE { GRAPH ?src { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> } }
Add optional group graph pattern to WHERE clause.
optional_group() accepts multiple arguments, similarly to where().
the query data, internal structure representing the contents of the WHERE clause
list of URIs that will go into query FROM clauses
list of URIs that will go into query FROM NAMED clauses
the query limit, can be a number or None
the query modifier can be: DISTINCT, REDUCED, or None
the query offset, can be a number or None
the query order by variables
the query type can be: SELECT, ASK, DESCRIBE*or *CONSTRUCT
the query variables to return as the resultset
Add graph pattern(s) to WHERE clause.
where() accepts multiple arguments. Each argument represents a a graph pattern and will be added to default group graph pattern. Each argument can be tuple, list, surf.query.Query, surf.query.NamedGroup, surf.query.OptionalGroup.
Example:
>>> query = select("?s").where(("?s", a, surf.ns.FOAF["person"]))
Bases: surf.query.Group
A SPARQL union
Construct and return surf.query.Query object of type ASK
Construct and return surf.query.Query object of type CONSTRUCT
Construct and return surf.query.Query object of type DESCRIBE
Return group graph pattern.
Returned object can be used as argument in Query.where() method.
group()` accepts multiple arguments, similarly to Query.where().
Return named group graph pattern.
Returned object can be used as argument in Query.where() method.
*statements is one or more graph patterns.
Example:
>>> import surf
>>> from surf.query import a, select, named_group
>>> query = select("?s", "?src").where(named_group("?src", ("?s", a, surf.ns.FOAF['Person'])))
>>> print unicode(query)
SELECT ?s ?src WHERE { GRAPH ?src { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> } }
Return optional group graph pattern.
Returned object can be used as argument in Query.where() method.
optional_group() accepts multiple arguments, similarly to Query.where().
Construct and return surf.query.Query object of type SELECT
*vars are variables to be selected.
Example:
>>> query = select("?s", "?p", "?o")
Return union graph pattern.
Returned object can be used as argument in Query.where() method.
union()` accepts multiple arguments, similarly to Query.where().