1
1
/*! @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. */
3
3
4
4
var SparqlParser = require ( 'sparqljs' ) . Parser ,
5
5
AsyncIterator = require ( 'asynciterator' ) ,
@@ -21,23 +21,28 @@ var queryConstructors = {
21
21
} ;
22
22
23
23
// 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 ( { } ) ;
28
30
29
31
// Transform the query into a cascade of iterators
30
32
try {
33
+ // Parse the query if needed
34
+ if ( typeof query === 'string' )
35
+ query = new SparqlParser ( options . prefixes ) . parse ( query ) ;
36
+
31
37
// 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 ] ;
34
39
if ( ! QueryConstructor )
35
40
throw new Error ( 'No iterator available for query type: ' + query . queryType ) ;
36
41
queryIterator = new QueryConstructor ( null , query , options ) ;
37
-
38
42
// 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 ,
40
44
queryIterator . patterns || query . where , options ) ;
45
+
41
46
// Create iterators for each order
42
47
for ( var i = query . order && ( query . order . length - 1 ) ; i >= 0 ; i -- ) {
43
48
var order = new SparqlExpressionEvaluator ( query . order [ i ] . expression ) ,
@@ -66,9 +71,9 @@ function SparqlIterator(source, queryText, options) {
66
71
}
67
72
catch ( error ) {
68
73
if ( / P a r s e e r r o r / . test ( error . message ) )
69
- error = new InvalidQueryError ( queryText , error ) ;
74
+ error = new InvalidQueryError ( query , error ) ;
70
75
else
71
- error = new UnsupportedQueryError ( queryText , error ) ;
76
+ error = new UnsupportedQueryError ( query , error ) ;
72
77
throw error ;
73
78
}
74
79
}
0 commit comments