Skip to content

Commit

Permalink
add querySparql and checkPatternExistence methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dherault committed Dec 7, 2016
1 parent 4a25086 commit 71ab82e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
gitignore

# Logs
logs
*.log
Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

# tbd

**New features:**
- Added querySparql method
- Added checkPatternExistence method
- Added includeInferred option on readQuads

**Bug fixes:**
- Fix readQuads bug when resolving empty result
- Fixed readQuads bug when resolving empty result
- Use Trig Media type with UTF-8 charset for POSTing and PUTing data
- Fix concurrent parsing bug
- Fixed concurrent parsing bug

# 0.1.0

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const defaultOptions = {
};

function getClient(options = defaultOptions) {
const { host, port, namespace } = options;
const { host, port, namespace } = Object.assign({}, defaultOptions, options);
const blazegraphUrl = `http://${host}:${port}/blazegraph/namespace/${namespace}/sparql`;
const passUrl = fn => (...args) => fn(blazegraphUrl, ...args);

Expand Down
48 changes: 46 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ function serializeQuad({ subject, predicate, object, graph }) {
MIDDLEWARE
--------------- */

// Perform a SPARQL query
// NOTE: this does not allow to perform a SPARQL update query
function querySparql(blazegraphUrl, query, includeInferred = false) {
if (!isValidString(query)) return Promise.reject(new Error('Query must be a non-empty string'));

return makeRequest({
url: `${blazegraphUrl}?query=${encodeQuery(query)}&includeInferred=${!!includeInferred}`,
headers: {
Accept: 'application/json',
},
})
.then(body => {
if (typeof body !== 'string') throw new Error('TODO: Learn when this is possible');

// Can throw ? Can be something else ?
return JSON.parse(body).results.bindings;
});
}

const resultRegex = /<data result="(\w*)"/;

/*
Returns true is some quads match a pattern
{
subject?: <IRI>
predicate?: <IRI>
object?: <IRI> or "Literal"
graph?: <IRI>
graphs?: [<IRI>]
}
*/
function checkPatternExistence(blazegraphUrl, input, includeInferred = false) {
if (isNotObject(input)) return rejectInput(input);

let fullUrl = `${blazegraphUrl}?HASSTMT&${encodeQuad(input)}&includeInferred=${!!includeInferred}`;

if (Array.isArray(input.graphs)) input.graphs.forEach(g => fullUrl += `&c=${g}`);

return makeRequest(fullUrl)
.then(result => result && typeof result === 'string' && resultRegex.exec(result)[1] === 'true');
}

/*
Read all quads matching a pattern
{
Expand All @@ -68,10 +110,10 @@ Read all quads matching a pattern
graphs?: [<IRI>]
}
*/
function readQuads(blazegraphUrl, input) {
function readQuads(blazegraphUrl, input, includeInferred = false) {
if (isNotObject(input)) return rejectInput(input);

let fullUrl = `${blazegraphUrl}?GETSTMTS&includeInferred=false&${encodeQuad(input)}`;
let fullUrl = `${blazegraphUrl}?GETSTMTS&includeInferred=${!!includeInferred}&${encodeQuad(input)}`;

if (Array.isArray(input.graphs)) input.graphs.forEach(g => fullUrl += `&c=${g}`);

Expand Down Expand Up @@ -192,6 +234,8 @@ function deleteSparql(blazegraphUrl, query) {
}

module.exports = {
querySparql,
checkPatternExistence,
readQuads,
createQuads,
updateQuad,
Expand Down

0 comments on commit 71ab82e

Please sign in to comment.