Skip to content
This repository was archived by the owner on Feb 5, 2020. It is now read-only.

Commit f7a6757

Browse files
committed
Accept parsed query as argument.
1 parent 078bf41 commit f7a6757

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

lib/sparql/SparqlIterator.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*! @license ©2014 Ruben Verborgh - Multimedia Lab / iMinds / Ghent University */
2-
/* An SparqlIterator returns the results of a SPARQL query. */
2+
/* A SparqlIterator returns the results of a SPARQL query. */
33

44
var SparqlParser = require('sparqljs').Parser,
55
AsyncIterator = require('asynciterator'),
@@ -21,23 +21,28 @@ var queryConstructors = {
2121
};
2222

2323
// Creates an iterator from a SPARQL query
24-
function SparqlIterator(source, queryText, options) {
25-
// Shift arguments if `source` was omitted
26-
if (typeof source === 'string')
27-
options = queryText, queryText = source, source = null;
24+
function SparqlIterator(source, query, options) {
25+
// Set argument defaults
26+
if (typeof source.read !== 'function')
27+
options = query, query = source, source = null;
28+
options = options || {};
29+
source = source || AsyncIterator.single({});
2830

2931
// Transform the query into a cascade of iterators
3032
try {
33+
// Parse the query if needed
34+
if (typeof query === 'string')
35+
query = new SparqlParser(options.prefixes).parse(query);
36+
3137
// Create an iterator that projects the bindings according to the query type
32-
var query = new SparqlParser(options.prefixes).parse(queryText),
33-
queryIterator, QueryConstructor = queryConstructors[query.queryType];
38+
var queryIterator, QueryConstructor = queryConstructors[query.queryType];
3439
if (!QueryConstructor)
3540
throw new Error('No iterator available for query type: ' + query.queryType);
3641
queryIterator = new QueryConstructor(null, query, options);
37-
3842
// Create an iterator for bindings of the query's graph pattern
39-
var graphIterator = new SparqlGroupsIterator(source || AsyncIterator.single({}),
43+
var graphIterator = new SparqlGroupsIterator(source,
4044
queryIterator.patterns || query.where, options);
45+
4146
// Create iterators for each order
4247
for (var i = query.order && (query.order.length - 1); i >= 0; i--) {
4348
var order = new SparqlExpressionEvaluator(query.order[i].expression),
@@ -66,9 +71,9 @@ function SparqlIterator(source, queryText, options) {
6671
}
6772
catch (error) {
6873
if (/Parse error/.test(error.message))
69-
error = new InvalidQueryError(queryText, error);
74+
error = new InvalidQueryError(query, error);
7075
else
71-
error = new UnsupportedQueryError(queryText, error);
76+
error = new UnsupportedQueryError(query, error);
7277
throw error;
7378
}
7479
}

0 commit comments

Comments
 (0)