Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/SparqlGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ Generator.prototype.toQuery = function (q) {
Generator.prototype.baseAndPrefixes = function (q) {
var base = q.base ? ('BASE <' + q.base + '>' + this._newline) : '';
var prefixes = '';
var queryPrefixes=Object.keys(q.prefixes || {});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var queryPrefixes=Object.keys(q.prefixes || {});
var queryPrefixes = Object.keys(q.prefixes || {});

for (var key in q.prefixes) {
if (this._options.allPrefixes || this._usedPrefixes[key])
prefixes += 'PREFIX ' + key + ': <' + q.prefixes[key] + '>' + this._newline;
}
if (this._options.allPrefixes || this._usedPrefixes[key] ||
(this._options.keepQueryPrefixes && queryPrefixes.includes(key)))
prefixes += 'PREFIX ' + key + ': <' + q.prefixes[key] + '>' + this._newline;
}
return base + prefixes;
};

Expand Down Expand Up @@ -451,6 +453,7 @@ function mapJoin(array, sep, func, self) {
/**
* @param options {
* allPrefixes: boolean,
* keepQueryPrefixes: boolean,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you call it fixedPrefixes and make it a key/value object?

* indentation: string,
* newline: string
* }
Expand Down
14 changes: 14 additions & 0 deletions test/SparqlGenerator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ var parsedQueriesPath = __dirname + '/../test/parsedQueries/';
var unusedPrefixesPath = __dirname + '/../test/unusedPrefixes/';

describe('A SPARQL generator', function () {
it('should preserve mentioned prefixes in the query', function () {
var parser = new SparqlParser({
prefixes: {
schema: "https://schema.org/",
owl: "http://www.w3.org/2002/07/owl#"
}
});
var parsedQuery = parser.parse('PREFIX db_opt: <https://db.control.param_10> \n SELECT * WHERE {?s a owl:Class}');
var generator = new SparqlGenerator({allPrefixes: false, keepQueryPrefixes: true});
var generatedQuery = generator.stringify(parsedQuery);
var expectedQuery ='PREFIX db_opt: <https://db.control.param_10>\nPREFIX owl: <http://www.w3.org/2002/07/owl#>\nSELECT * WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Class. }';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var expectedQuery ='PREFIX db_opt: <https://db.control.param_10>\nPREFIX owl: <http://www.w3.org/2002/07/owl#>\nSELECT * WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Class. }';
var expectedQuery = 'PREFIX db_opt: <https://db.control.param_10>\nPREFIX owl: <http://www.w3.org/2002/07/owl#>\nSELECT * WHERE { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> owl:Class. }';

expect(generatedQuery).toEqual(expectedQuery);
});

var defaultGenerator = new SparqlGenerator();

describe('in SPARQL mode', () => {
Expand Down