Skip to content

Commit

Permalink
Support for VALUES - #1
Browse files Browse the repository at this point in the history
  • Loading branch information
pasqLisena committed Feb 5, 2018
1 parent b37603e commit f0021bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ The supported properties are:
|PROPERTY|INPUT|NOTE|
|--------|-----|----|
|`$where`|string, array| Add where clause in the triple format.<br>Ex. `"$where": "?id a dbo:City"`|
|`$values`|object| Set `VALUES` for specified variables as a map.<br>Ex. `"$values": {"?id": ["dbr:Bari", "http://dbpedia.org/resource/Bologna"]}`|
|`$limit` |number| `LIMIT` the SPARQL results |
|`$distinct`|boolean (default `true`)| Set the `DISTINCT` in the select|
|`$orderby`|string, array| Build an `ORDER BY` on the variables in the input.<br> Ex. `"$orderby":["DESC(?name)","?age"]`|
Expand Down
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ function parsePrefixes(prefixes) {
.map(p => `PREFIX ${p}: <${prefixes[p]}>`);
}

function parseValues(values) {
return Object.keys(values)
.map(p => {
let _v = asArray(values[p]).map(v => {
if (v.startsWith('http')) return `<${v}>`;
if (v.includes(':')) return v;
return `"${v}"`;
});
return `VALUES ${sparqlVar(p)} {${_v.join(' ')}}`;
});
}

/**
* Apply the prototype to a single line of query results
*/
Expand Down Expand Up @@ -241,13 +253,15 @@ function jsonld2query(input) {
var limit = modifiers.$limit ? 'LIMIT ' + modifiers.$limit : '';
var distinct = modifiers.$distinct === false ? '' : 'DISTINCT';
var prefixes = modifiers.$prefixes ? parsePrefixes(modifiers.$prefixes) : [];
var values = modifiers.$values ? parseValues(modifiers.$values) : [];
var orderby = modifiers.$orderby ? 'ORDER BY ' + asArray(modifiers.$orderby).join(' ') : '';
var groupby = modifiers.$groupby ? 'GROUP BY ' + asArray(modifiers.$groupby).join(' ') : '';
var having = modifiers.$having ? `HAVING (${asArray(modifiers.$having).join(' && ')})` : '';

var query = `${prefixes.join('\n')}
SELECT ${distinct} ${vars.join(',')}
WHERE {
${values.join('\n')}
${wheres.join('.\n')}
${filters.map(f=>`FILTER(${f})`).join('\n')}
}
Expand All @@ -274,8 +288,7 @@ function computeRootId(proto, prefix) {

let _var = modifiers.find(m => m.match('var:.+'));
if (_var) {
_rootId = _var.split(':')[1];
if (!_rootId.startsWith['?']) _rootId = '?' + _rootId;
_rootId = sparqlVar(_var.split(':')[1]);
}

if (!_rootId) {
Expand All @@ -287,6 +300,15 @@ function computeRootId(proto, prefix) {
return _rootId;
}

/**
* Add the "?" if absent
*/
function sparqlVar(input) {
'use strict';
if (input.startsWith['?']) return input;
return '?' + input;
}

/**
* Parse a single key in prototype
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sparql-transformer",
"version": "0.0.1",
"version": "1.0.0",
"description": "Transform the SPARQL Endpoint output in JSON or JSON-LD",
"main": "index.es5.js",
"jsnext:main": "index.js",
Expand Down

0 comments on commit f0021bd

Please sign in to comment.